Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1647)

Side by Side Diff: third_party/WebKit/Source/core/dom/ClassicPendingScriptTest.cpp

Issue 2846673008: Add unit tests for ClassicPendingScript
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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, FetchInitiatorInfo());
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 TEST_F(ClassicPendingScriptTest, GetSourceAfterRevalidationStarts) {
97 ScriptResource* resource = Fetch(ResourceRequest(TestUrl()));
98
99 MockScriptElementBase* element = MockScriptElementBase::Create();
100 ClassicPendingScript* pending_script =
101 ClassicPendingScript::Create(element, resource);
102 Persistent<MockPendingScriptClient> mock_client =
103 MockPendingScriptClient::Create();
104 pending_script->WatchForLoad(mock_client);
105
106 EXPECT_FALSE(pending_script->IsReady());
107 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script));
108 EXPECT_CALL(*element, IntegrityAttributeValue())
109 .WillOnce(::testing::Return(String()));
110
111 ReceiveResponse(resource);
112
113 EXPECT_TRUE(resource->IsLoaded());
114 EXPECT_TRUE(pending_script->IsReady());
115
116 // Simulates revalidation start.
117 resource->SetRevalidatingRequest(ResourceRequest(TestUrl()));
118
119 EXPECT_FALSE(resource->IsLoaded());
120 EXPECT_TRUE(pending_script->IsReady());
121
122 bool error_occurred = false;
123 Script* script = pending_script->GetSource(KURL(), error_occurred);
124
125 EXPECT_FALSE(error_occurred);
126 ASSERT_TRUE(script);
127 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType());
128 EXPECT_EQ("fox", script->InlineSourceTextForCSP());
129 }
130
131 TEST_F(ClassicPendingScriptTest, GetSourceAfterRevalidationFailed) {
132 ScriptResource* resource = Fetch(ResourceRequest(TestUrl()));
133
134 MockScriptElementBase* element = MockScriptElementBase::Create();
135 ClassicPendingScript* pending_script =
136 ClassicPendingScript::Create(element, resource);
137 Persistent<MockPendingScriptClient> mock_client =
138 MockPendingScriptClient::Create();
139 pending_script->WatchForLoad(mock_client);
140
141 EXPECT_FALSE(pending_script->IsReady());
142 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script));
143 EXPECT_CALL(*element, IntegrityAttributeValue())
144 .WillOnce(::testing::Return(String()));
145
146 ReceiveResponse(resource);
147
148 EXPECT_TRUE(resource->IsLoaded());
149 EXPECT_TRUE(pending_script->IsReady());
150
151 // Simulates revalidation start.
152 resource->SetRevalidatingRequest(ResourceRequest(TestUrl()));
153
154 EXPECT_FALSE(resource->IsLoaded());
155 EXPECT_TRUE(pending_script->IsReady());
156
157 // Simulates failed revalidation response received.
158 ResourceResponse response(TestUrl(), "text/javascript", 7, g_null_atom);
159 response.SetHTTPStatusCode(200);
160 resource->ResponseReceived(response, nullptr);
161
162 EXPECT_FALSE(resource->IsLoaded());
163 EXPECT_TRUE(pending_script->IsReady());
164
165 bool error_occurred = false;
166 Script* script = pending_script->GetSource(KURL(), error_occurred);
167
168 EXPECT_FALSE(error_occurred);
169 ASSERT_TRUE(script);
170 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType());
171 EXPECT_EQ("fox", script->InlineSourceTextForCSP());
172 }
173
174 TEST_F(ClassicPendingScriptTest, GetSourceAfterFailedRevalidationFinished) {
175 ScriptResource* resource = Fetch(ResourceRequest(TestUrl()));
176
177 MockScriptElementBase* element = MockScriptElementBase::Create();
178 ClassicPendingScript* pending_script =
179 ClassicPendingScript::Create(element, resource);
180 Persistent<MockPendingScriptClient> mock_client =
181 MockPendingScriptClient::Create();
182 pending_script->WatchForLoad(mock_client);
183
184 EXPECT_FALSE(pending_script->IsReady());
185 EXPECT_CALL(*mock_client, PendingScriptFinished(pending_script));
186 EXPECT_CALL(*element, IntegrityAttributeValue())
187 .WillOnce(::testing::Return(String()));
188
189 ReceiveResponse(resource);
190
191 EXPECT_TRUE(resource->IsLoaded());
192 EXPECT_TRUE(pending_script->IsReady());
193
194 // Simulates revalidation start.
195 resource->SetRevalidatingRequest(ResourceRequest(TestUrl()));
196
197 EXPECT_FALSE(resource->IsLoaded());
198 EXPECT_TRUE(pending_script->IsReady());
199
200 // Simulates failed revalidation to finish.
201 ResourceResponse response(TestUrl(), "text/javascript", 7, g_null_atom);
202 response.SetHTTPStatusCode(200);
203 resource->ResponseReceived(response, nullptr);
204 resource->AppendData("kitsune", 7);
205 resource->Finish();
206
207 EXPECT_TRUE(resource->IsLoaded());
208 EXPECT_TRUE(pending_script->IsReady());
209
210 bool error_occurred = false;
211 Script* script = pending_script->GetSource(KURL(), error_occurred);
212
213 EXPECT_FALSE(error_occurred);
214 ASSERT_TRUE(script);
215 EXPECT_EQ(ScriptType::kClassic, script->GetScriptType());
216 EXPECT_EQ("fox", script->InlineSourceTextForCSP());
217 }
218
219 } // namespace
220
221 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698