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

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

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

Powered by Google App Engine
This is Rietveld 408576698