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

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

Issue 2588403002: TestingPlatformSupport: register Platform instance correctly (Closed)
Patch Set: new plan Created 3 years, 11 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/dom/ScriptRunner.h" 5 #include "core/dom/ScriptRunner.h"
6 6
7 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/dom/Element.h" 8 #include "core/dom/Element.h"
9 #include "core/dom/ScriptLoader.h" 9 #include "core/dom/ScriptLoader.h"
10 #include "platform/heap/Handle.h" 10 #include "platform/heap/Handle.h"
(...skipping 25 matching lines...) Expand all
36 explicit MockScriptLoader(Element* element) 36 explicit MockScriptLoader(Element* element)
37 : ScriptLoader(element, false, false, false) {} 37 : ScriptLoader(element, false, false, false) {}
38 }; 38 };
39 39
40 class ScriptRunnerTest : public testing::Test { 40 class ScriptRunnerTest : public testing::Test {
41 public: 41 public:
42 ScriptRunnerTest() 42 ScriptRunnerTest()
43 : m_document(Document::create()), 43 : m_document(Document::create()),
44 m_element(m_document->createElement("foo")) {} 44 m_element(m_document->createElement("foo")) {}
45 45
46 void SetUp() override {
Takashi Toyoshima 2017/01/12 08:56:13 This change is unfortunate, but I have no better i
47 // We have to create ScriptRunner after initializing platform, because we
48 // need Platform::current()->currentThread()->scheduler()->
49 // loadingTaskRunner() to be initialized before creating ScriptRunner to
50 // save it in constructor.
51 m_scriptRunner = ScriptRunner::create(m_document.get());
52 }
53
54 void TearDown() override { m_scriptRunner.release(); } 46 void TearDown() override { m_scriptRunner.release(); }
55 47
56 Persistent<Document> m_document; 48 Persistent<Document> m_document;
57 Persistent<Element> m_element; 49 Persistent<Element> m_element;
58 TestingPlatformSupportWithMockScheduler m_platform;
59 Persistent<ScriptRunner> m_scriptRunner; 50 Persistent<ScriptRunner> m_scriptRunner;
60 WTF::Vector<int> m_order; 51 WTF::Vector<int> m_order;
61 }; 52 };
62 53
63 TEST_F(ScriptRunnerTest, QueueSingleScript_Async) { 54 TEST_F(ScriptRunnerTest, QueueSingleScript_Async) {
55 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
56 platform;
57 // We have to create ScriptRunner after initializing platform, because we need
58 // Platform::current()->currentThread()->scheduler()->loadingTaskRunner() to
59 // be initialized before creating ScriptRunner to save it in constructor.
60 m_scriptRunner = ScriptRunner::create(m_document.get());
haraken 2017/01/12 11:21:00 It might be slightly cleaner to move this logic in
Takashi Toyoshima 2017/01/12 12:13:01 Done.
61
64 MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get()); 62 MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get());
65 m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::Async); 63 m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::Async);
66 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async); 64 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async);
67 65
68 EXPECT_CALL(*scriptLoader, execute()); 66 EXPECT_CALL(*scriptLoader, execute());
69 m_platform.runUntilIdle(); 67 platform->runUntilIdle();
70 } 68 }
71 69
72 TEST_F(ScriptRunnerTest, QueueSingleScript_InOrder) { 70 TEST_F(ScriptRunnerTest, QueueSingleScript_InOrder) {
71 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
72 platform;
73 m_scriptRunner = ScriptRunner::create(m_document.get());
74
73 MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get()); 75 MockScriptLoader* scriptLoader = MockScriptLoader::create(m_element.get());
74 m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::InOrder); 76 m_scriptRunner->queueScriptForExecution(scriptLoader, ScriptRunner::InOrder);
75 77
76 EXPECT_CALL(*scriptLoader, isReady()).WillOnce(Return(true)); 78 EXPECT_CALL(*scriptLoader, isReady()).WillOnce(Return(true));
77 EXPECT_CALL(*scriptLoader, execute()); 79 EXPECT_CALL(*scriptLoader, execute());
78 80
79 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::InOrder); 81 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::InOrder);
80 82
81 m_platform.runUntilIdle(); 83 platform->runUntilIdle();
82 } 84 }
83 85
84 TEST_F(ScriptRunnerTest, QueueMultipleScripts_InOrder) { 86 TEST_F(ScriptRunnerTest, QueueMultipleScripts_InOrder) {
87 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
88 platform;
89 m_scriptRunner = ScriptRunner::create(m_document.get());
90
85 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 91 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
86 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 92 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
87 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 93 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
88 94
89 HeapVector<Member<MockScriptLoader>> scriptLoaders; 95 HeapVector<Member<MockScriptLoader>> scriptLoaders;
90 scriptLoaders.push_back(scriptLoader1); 96 scriptLoaders.push_back(scriptLoader1);
91 scriptLoaders.push_back(scriptLoader2); 97 scriptLoaders.push_back(scriptLoader2);
92 scriptLoaders.push_back(scriptLoader3); 98 scriptLoaders.push_back(scriptLoader3);
93 99
94 for (ScriptLoader* scriptLoader : scriptLoaders) { 100 for (ScriptLoader* scriptLoader : scriptLoaders) {
(...skipping 11 matching lines...) Expand all
106 bool isReady[] = {false, false, false}; 112 bool isReady[] = {false, false, false};
107 113
108 for (size_t i = 0; i < scriptLoaders.size(); ++i) { 114 for (size_t i = 0; i < scriptLoaders.size(); ++i) {
109 EXPECT_CALL(*scriptLoaders[i], isReady()) 115 EXPECT_CALL(*scriptLoaders[i], isReady())
110 .WillRepeatedly(Invoke([&isReady, i] { return isReady[i]; })); 116 .WillRepeatedly(Invoke([&isReady, i] { return isReady[i]; }));
111 } 117 }
112 118
113 for (int i = 2; i >= 0; i--) { 119 for (int i = 2; i >= 0; i--) {
114 isReady[i] = true; 120 isReady[i] = true;
115 m_scriptRunner->notifyScriptReady(scriptLoaders[i], ScriptRunner::InOrder); 121 m_scriptRunner->notifyScriptReady(scriptLoaders[i], ScriptRunner::InOrder);
116 m_platform.runUntilIdle(); 122 platform->runUntilIdle();
117 } 123 }
118 124
119 // But ensure the scripts were run in the expected order. 125 // But ensure the scripts were run in the expected order.
120 EXPECT_THAT(m_order, ElementsAre(1, 2, 3)); 126 EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
121 } 127 }
122 128
123 TEST_F(ScriptRunnerTest, QueueMixedScripts) { 129 TEST_F(ScriptRunnerTest, QueueMixedScripts) {
130 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
131 platform;
132 m_scriptRunner = ScriptRunner::create(m_document.get());
133
124 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 134 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
125 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 135 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
126 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 136 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
127 MockScriptLoader* scriptLoader4 = MockScriptLoader::create(m_element.get()); 137 MockScriptLoader* scriptLoader4 = MockScriptLoader::create(m_element.get());
128 MockScriptLoader* scriptLoader5 = MockScriptLoader::create(m_element.get()); 138 MockScriptLoader* scriptLoader5 = MockScriptLoader::create(m_element.get());
129 139
130 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder); 140 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder);
131 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder); 141 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder);
132 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::InOrder); 142 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::InOrder);
133 m_scriptRunner->queueScriptForExecution(scriptLoader4, ScriptRunner::Async); 143 m_scriptRunner->queueScriptForExecution(scriptLoader4, ScriptRunner::Async);
(...skipping 22 matching lines...) Expand all
156 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] { 166 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] {
157 m_order.push_back(3); 167 m_order.push_back(3);
158 })); 168 }));
159 EXPECT_CALL(*scriptLoader4, execute()).WillOnce(Invoke([this] { 169 EXPECT_CALL(*scriptLoader4, execute()).WillOnce(Invoke([this] {
160 m_order.push_back(4); 170 m_order.push_back(4);
161 })); 171 }));
162 EXPECT_CALL(*scriptLoader5, execute()).WillOnce(Invoke([this] { 172 EXPECT_CALL(*scriptLoader5, execute()).WillOnce(Invoke([this] {
163 m_order.push_back(5); 173 m_order.push_back(5);
164 })); 174 }));
165 175
166 m_platform.runUntilIdle(); 176 platform->runUntilIdle();
167 177
168 // Async tasks are expected to run first. 178 // Async tasks are expected to run first.
169 EXPECT_THAT(m_order, ElementsAre(4, 5, 1, 2, 3)); 179 EXPECT_THAT(m_order, ElementsAre(4, 5, 1, 2, 3));
170 } 180 }
171 181
172 TEST_F(ScriptRunnerTest, QueueReentrantScript_Async) { 182 TEST_F(ScriptRunnerTest, QueueReentrantScript_Async) {
183 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
184 platform;
185 m_scriptRunner = ScriptRunner::create(m_document.get());
186
173 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 187 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
174 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 188 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
175 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 189 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
176 190
177 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async); 191 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async);
178 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async); 192 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async);
179 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::Async); 193 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::Async);
180 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async); 194 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async);
181 195
182 MockScriptLoader* scriptLoader = scriptLoader2; 196 MockScriptLoader* scriptLoader = scriptLoader2;
183 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([scriptLoader, this] { 197 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([scriptLoader, this] {
184 m_order.push_back(1); 198 m_order.push_back(1);
185 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async); 199 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async);
186 })); 200 }));
187 201
188 scriptLoader = scriptLoader3; 202 scriptLoader = scriptLoader3;
189 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([scriptLoader, this] { 203 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([scriptLoader, this] {
190 m_order.push_back(2); 204 m_order.push_back(2);
191 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async); 205 m_scriptRunner->notifyScriptReady(scriptLoader, ScriptRunner::Async);
192 })); 206 }));
193 207
194 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] { 208 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] {
195 m_order.push_back(3); 209 m_order.push_back(3);
196 })); 210 }));
197 211
198 // Make sure that re-entrant calls to notifyScriptReady don't cause 212 // Make sure that re-entrant calls to notifyScriptReady don't cause
199 // ScriptRunner::execute to do more work than expected. 213 // ScriptRunner::execute to do more work than expected.
200 m_platform.runSingleTask(); 214 platform->runSingleTask();
201 EXPECT_THAT(m_order, ElementsAre(1)); 215 EXPECT_THAT(m_order, ElementsAre(1));
202 216
203 m_platform.runSingleTask(); 217 platform->runSingleTask();
204 EXPECT_THAT(m_order, ElementsAre(1, 2)); 218 EXPECT_THAT(m_order, ElementsAre(1, 2));
205 219
206 m_platform.runSingleTask(); 220 platform->runSingleTask();
207 EXPECT_THAT(m_order, ElementsAre(1, 2, 3)); 221 EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
208 } 222 }
209 223
210 TEST_F(ScriptRunnerTest, QueueReentrantScript_InOrder) { 224 TEST_F(ScriptRunnerTest, QueueReentrantScript_InOrder) {
225 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
226 platform;
227 m_scriptRunner = ScriptRunner::create(m_document.get());
228
211 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 229 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
212 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 230 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
213 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 231 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
214 232
215 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true)); 233 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true));
216 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true)); 234 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true));
217 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(true)); 235 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(true));
218 236
219 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder); 237 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder);
220 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder); 238 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder);
(...skipping 15 matching lines...) Expand all
236 ScriptRunner::InOrder); 254 ScriptRunner::InOrder);
237 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::InOrder); 255 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::InOrder);
238 })); 256 }));
239 257
240 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] { 258 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] {
241 m_order.push_back(3); 259 m_order.push_back(3);
242 })); 260 }));
243 261
244 // Make sure that re-entrant calls to queueScriptForExecution don't cause 262 // Make sure that re-entrant calls to queueScriptForExecution don't cause
245 // ScriptRunner::execute to do more work than expected. 263 // ScriptRunner::execute to do more work than expected.
246 m_platform.runSingleTask(); 264 platform->runSingleTask();
247 EXPECT_THAT(m_order, ElementsAre(1)); 265 EXPECT_THAT(m_order, ElementsAre(1));
248 266
249 m_platform.runSingleTask(); 267 platform->runSingleTask();
250 EXPECT_THAT(m_order, ElementsAre(1, 2)); 268 EXPECT_THAT(m_order, ElementsAre(1, 2));
251 269
252 m_platform.runSingleTask(); 270 platform->runSingleTask();
253 EXPECT_THAT(m_order, ElementsAre(1, 2, 3)); 271 EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
254 } 272 }
255 273
256 TEST_F(ScriptRunnerTest, QueueReentrantScript_ManyAsyncScripts) { 274 TEST_F(ScriptRunnerTest, QueueReentrantScript_ManyAsyncScripts) {
275 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
276 platform;
277 m_scriptRunner = ScriptRunner::create(m_document.get());
278
257 MockScriptLoader* scriptLoaders[20]; 279 MockScriptLoader* scriptLoaders[20];
258 for (int i = 0; i < 20; i++) 280 for (int i = 0; i < 20; i++)
259 scriptLoaders[i] = nullptr; 281 scriptLoaders[i] = nullptr;
260 282
261 for (int i = 0; i < 20; i++) { 283 for (int i = 0; i < 20; i++) {
262 scriptLoaders[i] = MockScriptLoader::create(m_element.get()); 284 scriptLoaders[i] = MockScriptLoader::create(m_element.get());
263 EXPECT_CALL(*scriptLoaders[i], isReady()).WillRepeatedly(Return(true)); 285 EXPECT_CALL(*scriptLoaders[i], isReady()).WillRepeatedly(Return(true));
264 286
265 m_scriptRunner->queueScriptForExecution(scriptLoaders[i], 287 m_scriptRunner->queueScriptForExecution(scriptLoaders[i],
266 ScriptRunner::Async); 288 ScriptRunner::Async);
267 289
268 if (i > 0) { 290 if (i > 0) {
269 EXPECT_CALL(*scriptLoaders[i], execute()).WillOnce(Invoke([this, i] { 291 EXPECT_CALL(*scriptLoaders[i], execute()).WillOnce(Invoke([this, i] {
270 m_order.push_back(i); 292 m_order.push_back(i);
271 })); 293 }));
272 } 294 }
273 } 295 }
274 296
275 m_scriptRunner->notifyScriptReady(scriptLoaders[0], ScriptRunner::Async); 297 m_scriptRunner->notifyScriptReady(scriptLoaders[0], ScriptRunner::Async);
276 m_scriptRunner->notifyScriptReady(scriptLoaders[1], ScriptRunner::Async); 298 m_scriptRunner->notifyScriptReady(scriptLoaders[1], ScriptRunner::Async);
277 299
278 EXPECT_CALL(*scriptLoaders[0], execute()) 300 EXPECT_CALL(*scriptLoaders[0], execute())
279 .WillOnce(Invoke([&scriptLoaders, this] { 301 .WillOnce(Invoke([&scriptLoaders, this] {
280 for (int i = 2; i < 20; i++) 302 for (int i = 2; i < 20; i++)
281 m_scriptRunner->notifyScriptReady(scriptLoaders[i], 303 m_scriptRunner->notifyScriptReady(scriptLoaders[i],
282 ScriptRunner::Async); 304 ScriptRunner::Async);
283 m_order.push_back(0); 305 m_order.push_back(0);
284 })); 306 }));
285 307
286 m_platform.runUntilIdle(); 308 platform->runUntilIdle();
287 309
288 int expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 310 int expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
289 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; 311 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
290 312
291 EXPECT_THAT(m_order, testing::ElementsAreArray(expected)); 313 EXPECT_THAT(m_order, testing::ElementsAreArray(expected));
292 } 314 }
293 315
294 TEST_F(ScriptRunnerTest, ResumeAndSuspend_InOrder) { 316 TEST_F(ScriptRunnerTest, ResumeAndSuspend_InOrder) {
317 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
318 platform;
319 m_scriptRunner = ScriptRunner::create(m_document.get());
320
295 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 321 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
296 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 322 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
297 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 323 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
298 324
299 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder); 325 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder);
300 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder); 326 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder);
301 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::InOrder); 327 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::InOrder);
302 328
303 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] { 329 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] {
304 m_order.push_back(1); 330 m_order.push_back(1);
(...skipping 12 matching lines...) Expand all
317 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(false)); 343 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(false));
318 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder); 344 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder);
319 345
320 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true)); 346 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true));
321 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(false)); 347 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(false));
322 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::InOrder); 348 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::InOrder);
323 349
324 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(true)); 350 EXPECT_CALL(*scriptLoader3, isReady()).WillRepeatedly(Return(true));
325 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::InOrder); 351 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::InOrder);
326 352
327 m_platform.runSingleTask(); 353 platform->runSingleTask();
328 m_scriptRunner->suspend(); 354 m_scriptRunner->suspend();
329 m_scriptRunner->resume(); 355 m_scriptRunner->resume();
330 m_platform.runUntilIdle(); 356 platform->runUntilIdle();
331 357
332 // Make sure elements are correct and in right order. 358 // Make sure elements are correct and in right order.
333 EXPECT_THAT(m_order, ElementsAre(1, 2, 3)); 359 EXPECT_THAT(m_order, ElementsAre(1, 2, 3));
334 } 360 }
335 361
336 TEST_F(ScriptRunnerTest, ResumeAndSuspend_Async) { 362 TEST_F(ScriptRunnerTest, ResumeAndSuspend_Async) {
363 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
364 platform;
365 m_scriptRunner = ScriptRunner::create(m_document.get());
366
337 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 367 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
338 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 368 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
339 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get()); 369 MockScriptLoader* scriptLoader3 = MockScriptLoader::create(m_element.get());
340 370
341 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async); 371 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async);
342 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async); 372 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async);
343 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::Async); 373 m_scriptRunner->queueScriptForExecution(scriptLoader3, ScriptRunner::Async);
344 374
345 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async); 375 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async);
346 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::Async); 376 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::Async);
347 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::Async); 377 m_scriptRunner->notifyScriptReady(scriptLoader3, ScriptRunner::Async);
348 378
349 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] { 379 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] {
350 m_order.push_back(1); 380 m_order.push_back(1);
351 })); 381 }));
352 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([this] { 382 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([this] {
353 m_order.push_back(2); 383 m_order.push_back(2);
354 })); 384 }));
355 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] { 385 EXPECT_CALL(*scriptLoader3, execute()).WillOnce(Invoke([this] {
356 m_order.push_back(3); 386 m_order.push_back(3);
357 })); 387 }));
358 388
359 m_platform.runSingleTask(); 389 platform->runSingleTask();
360 m_scriptRunner->suspend(); 390 m_scriptRunner->suspend();
361 m_scriptRunner->resume(); 391 m_scriptRunner->resume();
362 m_platform.runUntilIdle(); 392 platform->runUntilIdle();
363 393
364 // Make sure elements are correct. 394 // Make sure elements are correct.
365 EXPECT_THAT(m_order, WhenSorted(ElementsAre(1, 2, 3))); 395 EXPECT_THAT(m_order, WhenSorted(ElementsAre(1, 2, 3)));
366 } 396 }
367 397
368 TEST_F(ScriptRunnerTest, LateNotifications) { 398 TEST_F(ScriptRunnerTest, LateNotifications) {
399 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
400 platform;
401 m_scriptRunner = ScriptRunner::create(m_document.get());
402
369 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get()); 403 MockScriptLoader* scriptLoader1 = MockScriptLoader::create(m_element.get());
370 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get()); 404 MockScriptLoader* scriptLoader2 = MockScriptLoader::create(m_element.get());
371 405
372 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true)); 406 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true));
373 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true)); 407 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true));
374 408
375 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder); 409 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::InOrder);
376 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder); 410 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::InOrder);
377 411
378 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] { 412 EXPECT_CALL(*scriptLoader1, execute()).WillOnce(Invoke([this] {
379 m_order.push_back(1); 413 m_order.push_back(1);
380 })); 414 }));
381 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([this] { 415 EXPECT_CALL(*scriptLoader2, execute()).WillOnce(Invoke([this] {
382 m_order.push_back(2); 416 m_order.push_back(2);
383 })); 417 }));
384 418
385 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder); 419 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::InOrder);
386 m_platform.runUntilIdle(); 420 platform->runUntilIdle();
387 421
388 // At this moment all tasks can be already executed. Make sure that we do not 422 // At this moment all tasks can be already executed. Make sure that we do not
389 // crash here. 423 // crash here.
390 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::InOrder); 424 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::InOrder);
391 m_platform.runUntilIdle(); 425 platform->runUntilIdle();
392 426
393 EXPECT_THAT(m_order, ElementsAre(1, 2)); 427 EXPECT_THAT(m_order, ElementsAre(1, 2));
394 } 428 }
395 429
396 TEST_F(ScriptRunnerTest, TasksWithDeadScriptRunner) { 430 TEST_F(ScriptRunnerTest, TasksWithDeadScriptRunner) {
431 ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
432 platform;
433 m_scriptRunner = ScriptRunner::create(m_document.get());
434
397 Persistent<MockScriptLoader> scriptLoader1 = 435 Persistent<MockScriptLoader> scriptLoader1 =
398 MockScriptLoader::create(m_element.get()); 436 MockScriptLoader::create(m_element.get());
399 Persistent<MockScriptLoader> scriptLoader2 = 437 Persistent<MockScriptLoader> scriptLoader2 =
400 MockScriptLoader::create(m_element.get()); 438 MockScriptLoader::create(m_element.get());
401 439
402 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true)); 440 EXPECT_CALL(*scriptLoader1, isReady()).WillRepeatedly(Return(true));
403 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true)); 441 EXPECT_CALL(*scriptLoader2, isReady()).WillRepeatedly(Return(true));
404 442
405 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async); 443 m_scriptRunner->queueScriptForExecution(scriptLoader1, ScriptRunner::Async);
406 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async); 444 m_scriptRunner->queueScriptForExecution(scriptLoader2, ScriptRunner::Async);
407 445
408 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async); 446 m_scriptRunner->notifyScriptReady(scriptLoader1, ScriptRunner::Async);
409 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::Async); 447 m_scriptRunner->notifyScriptReady(scriptLoader2, ScriptRunner::Async);
410 448
411 m_scriptRunner.release(); 449 m_scriptRunner.release();
412 450
413 ThreadState::current()->collectAllGarbage(); 451 ThreadState::current()->collectAllGarbage();
414 452
415 // m_scriptRunner is gone. We need to make sure that ScriptRunner::Task do not 453 // m_scriptRunner is gone. We need to make sure that ScriptRunner::Task do not
416 // access dead object. 454 // access dead object.
417 EXPECT_CALL(*scriptLoader1, execute()).Times(0); 455 EXPECT_CALL(*scriptLoader1, execute()).Times(0);
418 EXPECT_CALL(*scriptLoader2, execute()).Times(0); 456 EXPECT_CALL(*scriptLoader2, execute()).Times(0);
419 457
420 m_platform.runUntilIdle(); 458 platform->runUntilIdle();
421 } 459 }
422 460
423 } // namespace blink 461 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698