| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
| 6 #include "core/dom/TaskRunnerHelper.h" |
| 6 #include "platform/testing/UnitTestHelpers.h" | 7 #include "platform/testing/UnitTestHelpers.h" |
| 7 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
| 8 #include "public/platform/WebViewScheduler.h" | 9 #include "public/platform/WebViewScheduler.h" |
| 9 #include "public/web/WebLocalFrame.h" | 10 #include "public/web/WebLocalFrame.h" |
| 10 #include "public/web/WebScriptExecutionCallback.h" | 11 #include "public/web/WebScriptExecutionCallback.h" |
| 11 #include "public/web/WebScriptSource.h" | 12 #include "public/web/WebScriptSource.h" |
| 12 #include "public/web/WebView.h" | 13 #include "public/web/WebView.h" |
| 13 #include "web/tests/sim/SimRequest.h" | 14 #include "web/tests/sim/SimRequest.h" |
| 14 #include "web/tests/sim/SimTest.h" | 15 #include "web/tests/sim/SimTest.h" |
| 15 | 16 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 39 .mainFrame() | 40 .mainFrame() |
| 40 ->toWebLocalFrame() | 41 ->toWebLocalFrame() |
| 41 ->requestExecuteScriptAndReturnValue( | 42 ->requestExecuteScriptAndReturnValue( |
| 42 WebScriptSource(WebString(scriptSource)), false, &callbackHelper); | 43 WebScriptSource(WebString(scriptSource)), false, &callbackHelper); |
| 43 return callbackHelper.result(); | 44 return callbackHelper.result(); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void TearDown() override { | 47 void TearDown() override { |
| 47 // The SimTest destructor calls runPendingTasks. This is a problem because | 48 // The SimTest destructor calls runPendingTasks. This is a problem because |
| 48 // if there are any repeating tasks, advancing virtual time will cause the | 49 // if there are any repeating tasks, advancing virtual time will cause the |
| 49 // runloop to busy loop. Pausing virtual time here fixes that. | 50 // runloop to busy loop. Disabling virtual time here fixes that. |
| 50 webView().scheduler()->setVirtualTimePolicy( | 51 webView().scheduler()->disableVirtualTimeForTesting(); |
| 51 WebViewScheduler::VirtualTimePolicy::PAUSE); | |
| 52 } | 52 } |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 namespace { | 55 namespace { |
| 56 void quitRunLoop() { | 56 void quitRunLoop() { |
| 57 base::MessageLoop::current()->QuitNow(); | 57 base::MessageLoop::current()->QuitNow(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Some task queues may have repeating v8 tasks that run forever so we impose a | 60 // Some task queues may have repeating v8 tasks that run forever so we impose a |
| 61 // hard time limit. | 61 // hard time limit. |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 191 |
| 192 // Still Loading, virtual time should not advance. | 192 // Still Loading, virtual time should not advance. |
| 193 mainResource.write("<body>"); | 193 mainResource.write("<body>"); |
| 194 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 194 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 195 | 195 |
| 196 // Finished loading, virtual time should be able to advance. | 196 // Finished loading, virtual time should be able to advance. |
| 197 mainResource.finish(); | 197 mainResource.finish(); |
| 198 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 198 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // http://crbug.com/633321 |
| 202 #if OS(ANDROID) |
| 203 #define MAYBE_DOMTimersSuspended DISABLED_DOMTimersSuspended |
| 204 #else |
| 205 #define MAYBE_DOMTimersSuspended DOMTimersSuspended |
| 206 #endif |
| 207 TEST_F(VirtualTimeTest, MAYBE_DOMTimersSuspended) { |
| 208 webView().scheduler()->enableVirtualTime(); |
| 209 |
| 210 // Schedule a normal DOM timer to run at 1s in the future. |
| 211 ExecuteJavaScript( |
| 212 "var run_order = [];" |
| 213 "setTimeout(() => { run_order.push(1); }, 1000);"); |
| 214 |
| 215 RefPtr<WebTaskRunner> runner = |
| 216 TaskRunnerHelper::get(TaskType::Timer, window().getExecutionContext()); |
| 217 |
| 218 // Schedule a task to suspend virtual time at the same point in time. |
| 219 runner->postDelayedTask(BLINK_FROM_HERE, |
| 220 WTF::bind( |
| 221 [](WebViewScheduler* scheduler) { |
| 222 scheduler->setVirtualTimePolicy( |
| 223 WebViewScheduler::VirtualTimePolicy::PAUSE); |
| 224 }, |
| 225 WTF::unretained(webView().scheduler())), |
| 226 1000); |
| 227 |
| 228 // ALso schedule a second timer for the same point in time. |
| 229 ExecuteJavaScript("setTimeout(() => { run_order.push(2); }, 1000);"); |
| 230 |
| 231 // The second DOM timer shouldn't have run because pausing virtual time also |
| 232 // atomically pauses DOM timers. |
| 233 testing::runPendingTasks(); |
| 234 EXPECT_EQ("1", ExecuteJavaScript("run_order.join(', ')")); |
| 235 } |
| 236 |
| 201 } // namespace blink | 237 } // namespace blink |
| OLD | NEW |