OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/dom/ClassicPendingScript.h" |
| 6 |
| 7 #include "core/dom/MockScriptElementBase.h" |
| 8 #include "core/loader/resource/ScriptResource.h" |
| 9 #include "platform/exported/WrappedResourceResponse.h" |
| 10 #include "platform/loader/fetch/ResourceFetcher.h" |
| 11 #include "platform/loader/fetch/ResourceLoader.h" |
| 12 #include "platform/loader/testing/MockFetchContext.h" |
| 13 #include "platform/testing/ScopedMockedURL.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace blink { |
| 18 |
| 19 namespace { |
| 20 |
| 21 class MockPendingScriptClient |
| 22 : public GarbageCollectedFinalized<MockPendingScriptClient>, |
| 23 public PendingScriptClient { |
| 24 USING_GARBAGE_COLLECTED_MIXIN(MockPendingScriptClient); |
| 25 |
| 26 public: |
| 27 static MockPendingScriptClient* Create() { |
| 28 return new ::testing::StrictMock<MockPendingScriptClient>(); |
| 29 } |
| 30 |
| 31 virtual ~MockPendingScriptClient() {} |
| 32 MOCK_METHOD1(PendingScriptFinished, void(PendingScript*)); |
| 33 |
| 34 DEFINE_INLINE_VIRTUAL_TRACE() { PendingScriptClient::Trace(visitor); } |
| 35 }; |
| 36 |
| 37 class ClassicPendingScriptTest : public ::testing::Test { |
| 38 public: |
| 39 ClassicPendingScriptTest() |
| 40 : test_url_(kParsedURLString, "http://example.com/test.js"), |
| 41 fetcher_(ResourceFetcher::Create(MockFetchContext::Create( |
| 42 MockFetchContext::kShouldLoadNewResource))), |
| 43 scoped_mocked_url_load_(test_url_, "") {} |
| 44 |
| 45 ScriptResource* Fetch(const ResourceRequest& request) { |
| 46 FetchParameters fetch_params(request); |
| 47 return ScriptResource::Fetch(fetch_params, fetcher_); |
| 48 } |
| 49 |
| 50 void ReceiveResponse(Resource* resource, const char* data = "fox") { |
| 51 size_t length = strlen(data); |
| 52 ResourceResponse response(TestUrl(), "text/javascript", length, |
| 53 g_null_atom); |
| 54 resource->Loader()->DidReceiveResponse(WrappedResourceResponse(response)); |
| 55 resource->Loader()->DidReceiveData(data, length); |
| 56 resource->Loader()->DidFinishLoading(0.0, length, length, length); |
| 57 } |
| 58 |
| 59 const KURL& TestUrl() const { return test_url_; } |
| 60 |
| 61 private: |
| 62 const KURL test_url_; |
| 63 Persistent<ResourceFetcher> fetcher_; |
| 64 testing::ScopedMockedURLLoad scoped_mocked_url_load_; |
| 65 }; |
| 66 |
| 67 TEST_F(ClassicPendingScriptTest, GetSource) { |
| 68 ScriptResource* resource = Fetch(ResourceRequest(TestUrl())); |
| 69 |
| 70 MockScriptElementBase* element = MockScriptElementBase::Create(); |
| 71 ClassicPendingScript* pending_script = |
| 72 ClassicPendingScript::Create(element, resource); |
| 73 Persistent<MockPendingScriptClient> mock_client = |
| 74 MockPendingScriptClient::Create(); |
| 75 pending_script->WatchForLoad(mock_client); |
| 76 |
| 77 EXPECT_FALSE(pending_script->IsReady()); |
| 78 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script)); |
| 79 EXPECT_CALL(*element, IntegrityAttributeValue()) |
| 80 .WillOnce(::testing::Return(String())); |
| 81 |
| 82 ReceiveResponse(resource); |
| 83 |
| 84 EXPECT_TRUE(pending_script->IsReady()); |
| 85 |
| 86 bool error_occurred = false; |
| 87 Script* script = pending_script->GetSource(KURL(), error_occurred); |
| 88 |
| 89 EXPECT_FALSE(error_occurred); |
| 90 ASSERT_TRUE(script); |
| 91 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType()); |
| 92 EXPECT_EQ("fox", script->InlineSourceTextForCSP()); |
| 93 } |
| 94 |
| 95 // Following tests related to revalidation are for crbug.com/692856. |
| 96 // |
| 97 // GetSource() should return the original source even after revalidation starts. |
| 98 TEST_F(ClassicPendingScriptTest, GetSourceAfterRevalidationStarts) { |
| 99 ScriptResource* resource = Fetch(ResourceRequest(TestUrl())); |
| 100 |
| 101 MockScriptElementBase* element = MockScriptElementBase::Create(); |
| 102 ClassicPendingScript* pending_script = |
| 103 ClassicPendingScript::Create(element, resource); |
| 104 Persistent<MockPendingScriptClient> mock_client = |
| 105 MockPendingScriptClient::Create(); |
| 106 pending_script->WatchForLoad(mock_client); |
| 107 |
| 108 EXPECT_FALSE(pending_script->IsReady()); |
| 109 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script)); |
| 110 EXPECT_CALL(*element, IntegrityAttributeValue()) |
| 111 .WillOnce(::testing::Return(String())); |
| 112 |
| 113 ReceiveResponse(resource); |
| 114 |
| 115 EXPECT_TRUE(resource->IsLoaded()); |
| 116 EXPECT_TRUE(pending_script->IsReady()); |
| 117 |
| 118 // Simulates revalidation start. |
| 119 resource->SetRevalidatingRequest(ResourceRequest(TestUrl())); |
| 120 |
| 121 EXPECT_FALSE(resource->IsLoaded()); |
| 122 EXPECT_TRUE(pending_script->IsReady()); |
| 123 |
| 124 bool error_occurred = false; |
| 125 Script* script = pending_script->GetSource(KURL(), error_occurred); |
| 126 |
| 127 EXPECT_FALSE(error_occurred); |
| 128 ASSERT_TRUE(script); |
| 129 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType()); |
| 130 EXPECT_EQ("fox", script->InlineSourceTextForCSP()); |
| 131 } |
| 132 |
| 133 // GetSource() should return the original source even after revalidation starts |
| 134 // and revalidation response is received. |
| 135 TEST_F(ClassicPendingScriptTest, GetSourceAfterRevalidationFailed) { |
| 136 ScriptResource* resource = Fetch(ResourceRequest(TestUrl())); |
| 137 |
| 138 MockScriptElementBase* element = MockScriptElementBase::Create(); |
| 139 ClassicPendingScript* pending_script = |
| 140 ClassicPendingScript::Create(element, resource); |
| 141 Persistent<MockPendingScriptClient> mock_client = |
| 142 MockPendingScriptClient::Create(); |
| 143 pending_script->WatchForLoad(mock_client); |
| 144 |
| 145 EXPECT_FALSE(pending_script->IsReady()); |
| 146 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script)); |
| 147 EXPECT_CALL(*element, IntegrityAttributeValue()) |
| 148 .WillOnce(::testing::Return(String())); |
| 149 |
| 150 ReceiveResponse(resource); |
| 151 |
| 152 EXPECT_TRUE(resource->IsLoaded()); |
| 153 EXPECT_TRUE(pending_script->IsReady()); |
| 154 |
| 155 // Simulates revalidation start. |
| 156 resource->SetRevalidatingRequest(ResourceRequest(TestUrl())); |
| 157 |
| 158 EXPECT_FALSE(resource->IsLoaded()); |
| 159 EXPECT_TRUE(pending_script->IsReady()); |
| 160 |
| 161 // Simulates failed revalidation response received. |
| 162 ResourceResponse response(TestUrl(), "text/javascript", 7, g_null_atom); |
| 163 response.SetHTTPStatusCode(200); |
| 164 resource->ResponseReceived(response, nullptr); |
| 165 |
| 166 EXPECT_FALSE(resource->IsLoaded()); |
| 167 EXPECT_TRUE(pending_script->IsReady()); |
| 168 |
| 169 bool error_occurred = false; |
| 170 Script* script = pending_script->GetSource(KURL(), error_occurred); |
| 171 |
| 172 EXPECT_FALSE(error_occurred); |
| 173 ASSERT_TRUE(script); |
| 174 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType()); |
| 175 EXPECT_EQ("fox", script->InlineSourceTextForCSP()); |
| 176 } |
| 177 |
| 178 // GetSource() should return the original source even after revalidation is |
| 179 // finished. |
| 180 TEST_F(ClassicPendingScriptTest, GetSourceAfterFailedRevalidationFinished) { |
| 181 ScriptResource* resource = Fetch(ResourceRequest(TestUrl())); |
| 182 |
| 183 MockScriptElementBase* element = MockScriptElementBase::Create(); |
| 184 ClassicPendingScript* pending_script = |
| 185 ClassicPendingScript::Create(element, resource); |
| 186 Persistent<MockPendingScriptClient> mock_client = |
| 187 MockPendingScriptClient::Create(); |
| 188 pending_script->WatchForLoad(mock_client); |
| 189 |
| 190 EXPECT_FALSE(pending_script->IsReady()); |
| 191 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script)); |
| 192 EXPECT_CALL(*element, IntegrityAttributeValue()) |
| 193 .WillOnce(::testing::Return(String())); |
| 194 |
| 195 ReceiveResponse(resource); |
| 196 |
| 197 EXPECT_TRUE(resource->IsLoaded()); |
| 198 EXPECT_TRUE(pending_script->IsReady()); |
| 199 |
| 200 // Simulates revalidation start. |
| 201 resource->SetRevalidatingRequest(ResourceRequest(TestUrl())); |
| 202 |
| 203 EXPECT_FALSE(resource->IsLoaded()); |
| 204 EXPECT_TRUE(pending_script->IsReady()); |
| 205 |
| 206 // Simulates failed revalidation to finish. |
| 207 ResourceResponse response(TestUrl(), "text/javascript", 7, g_null_atom); |
| 208 response.SetHTTPStatusCode(200); |
| 209 resource->ResponseReceived(response, nullptr); |
| 210 // This new data shouldn't be returned from pending_script->GetSource(). |
| 211 resource->AppendData("kitsune", 7); |
| 212 resource->Finish(); |
| 213 |
| 214 EXPECT_TRUE(resource->IsLoaded()); |
| 215 EXPECT_TRUE(pending_script->IsReady()); |
| 216 |
| 217 bool error_occurred = false; |
| 218 Script* script = pending_script->GetSource(KURL(), error_occurred); |
| 219 |
| 220 EXPECT_FALSE(error_occurred); |
| 221 ASSERT_TRUE(script); |
| 222 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType()); |
| 223 EXPECT_EQ("fox", script->InlineSourceTextForCSP()); |
| 224 } |
| 225 |
| 226 } // namespace |
| 227 |
| 228 } // namespace blink |
OLD | NEW |