| 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 "platform/testing/UnitTestHelpers.h" | 5 #include "platform/testing/UnitTestHelpers.h" |
| 6 #include "public/platform/Platform.h" | 6 #include "public/platform/Platform.h" |
| 7 #include "public/platform/WebViewScheduler.h" | 7 #include "public/platform/WebViewScheduler.h" |
| 8 #include "public/web/WebLocalFrame.h" | 8 #include "public/web/WebLocalFrame.h" |
| 9 #include "public/web/WebScriptExecutionCallback.h" | 9 #include "public/web/WebScriptExecutionCallback.h" |
| 10 #include "public/web/WebScriptSource.h" | 10 #include "public/web/WebScriptSource.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 }; | 31 }; |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 class VirtualTimeTest : public SimTest { | 34 class VirtualTimeTest : public SimTest { |
| 35 protected: | 35 protected: |
| 36 String ExecuteJavaScript(String scriptSource) | 36 String ExecuteJavaScript(String scriptSource) |
| 37 { | 37 { |
| 38 ScriptExecutionCallbackHelper callbackHelper; | 38 ScriptExecutionCallbackHelper callbackHelper; |
| 39 webView().mainFrame()->toWebLocalFrame()->requestExecuteScriptAndReturnV
alue( | 39 webView().mainFrame()->toWebLocalFrame()->requestExecuteScriptAndReturnV
alue( |
| 40 WebScriptSource(WebString(scriptSource)), false, &callbackHelper); | 40 WebScriptSource(WebString(scriptSource)), false, &callbackHelper); |
| 41 testing::runPendingTasks(); |
| 41 return callbackHelper.result(); | 42 return callbackHelper.result(); |
| 42 } | 43 } |
| 43 }; | 44 }; |
| 44 | 45 |
| 45 namespace { | |
| 46 void quitRunLoop() | |
| 47 { | |
| 48 base::MessageLoop::current()->QuitNow(); | |
| 49 } | |
| 50 | |
| 51 // Some task queues may have repeating v8 tasks that run forever so we impos
e a hard time limit. | |
| 52 void runTasksForPeriod(double delayMs) | |
| 53 { | |
| 54 Platform::current()->currentThread()->getWebTaskRunner()->postDelayedTas
k(BLINK_FROM_HERE, WTF::bind(&quitRunLoop), delayMs); | |
| 55 testing::enterRunLoop(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 TEST_F(VirtualTimeTest, DOMTimersFireInExpectedOrder) | 46 TEST_F(VirtualTimeTest, DOMTimersFireInExpectedOrder) |
| 60 { | 47 { |
| 61 webView().scheduler()->enableVirtualTime(); | 48 webView().scheduler()->enableVirtualTime(); |
| 62 | 49 |
| 63 ExecuteJavaScript( | 50 ExecuteJavaScript( |
| 64 "var run_order = [];" | 51 "var run_order = [];" |
| 65 "function timerFn(delay, value) {" | 52 "function timerFn(delay, value) {" |
| 66 " setTimeout(function() { run_order.push(value); }, delay);" | 53 " setTimeout(function() { run_order.push(value); }, delay);" |
| 67 "};" | 54 "};" |
| 68 "var one_hour = 60 * 60 * 1000;" | 55 "var one_hour = 60 * 60 * 1000;" |
| 69 "timerFn(one_hour * 100, 'a');" | 56 "timerFn(one_hour * 100, 'a');" |
| 70 "timerFn(one_hour * 10, 'b');" | 57 "timerFn(one_hour * 10, 'b');" |
| 71 "timerFn(one_hour, 'c');"); | 58 "timerFn(one_hour, 'c');"); |
| 72 | 59 |
| 73 // Normally the JS runs pretty much instantly but the timer callbacks will | 60 // Normally the JS runs pretty much instantly but the timer callbacks will |
| 74 // take 100h to fire, but thanks to timer fast forwarding we can make them | 61 // take 100h to fire, but thanks to timer fast forwarding we can make them |
| 75 // fire immediatly. | 62 // fire immediatly. |
| 76 | 63 |
| 77 testing::runPendingTasks(); | |
| 78 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); | 64 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); |
| 79 } | 65 } |
| 80 | 66 |
| 81 TEST_F(VirtualTimeTest, SetInterval) | 67 TEST_F(VirtualTimeTest, SetInterval) |
| 82 { | 68 { |
| 83 webView().scheduler()->enableVirtualTime(); | 69 webView().scheduler()->enableVirtualTime(); |
| 84 | 70 |
| 85 ExecuteJavaScript( | 71 ExecuteJavaScript( |
| 86 "var run_order = [];" | 72 "var run_order = [];" |
| 87 "var count = 10;" | 73 "var count = 10;" |
| 88 "var interval_handle = setInterval(function() {" | 74 "var interval_handle = setInterval(function() {" |
| 89 " if (--window.count == 0) {" | 75 " if (--window.count == 0) {" |
| 90 " clearInterval(interval_handle);" | 76 " clearInterval(interval_handle);" |
| 91 " }" | 77 " }" |
| 92 " run_order.push(count);" | 78 " run_order.push(count);" |
| 93 "}, 1000);" | 79 "}, 1000);" |
| 94 "setTimeout(function() { run_order.push('timer'); }, 1500);"); | 80 "setTimeout(function() { run_order.push('timer'); }, 1500);"); |
| 95 | 81 |
| 96 runTasksForPeriod(12000); | 82 // If virtual time is not supplied to TimerBase then the setInterval |
| 97 | 83 // won't fire 10x. |
| 98 EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", ExecuteJavaScript("run_orde
r.join(', ')")); | 84 EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", ExecuteJavaScript("run_orde
r.join(', ')")); |
| 99 } | 85 } |
| 100 | 86 |
| 101 TEST_F(VirtualTimeTest, AllowVirtualTimeToAdvance) | 87 TEST_F(VirtualTimeTest, AllowVirtualTimeToAdvance) |
| 102 { | 88 { |
| 103 webView().scheduler()->enableVirtualTime(); | 89 webView().scheduler()->enableVirtualTime(); |
| 104 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::PAUSE); | 90 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::PAUSE); |
| 105 | 91 |
| 106 ExecuteJavaScript( | 92 ExecuteJavaScript( |
| 107 "var run_order = [];" | 93 "var run_order = [];" |
| 108 "timerFn = function(delay, value) {" | 94 "timerFn = function(delay, value) {" |
| 109 " setTimeout(function() { run_order.push(value); }, delay);" | 95 " setTimeout(function() { run_order.push(value); }, delay);" |
| 110 "};" | 96 "};" |
| 111 "timerFn(100, 'a');" | 97 "timerFn(100, 'a');" |
| 112 "timerFn(10, 'b');" | 98 "timerFn(10, 'b');" |
| 113 "timerFn(1, 'c');"); | 99 "timerFn(1, 'c');"); |
| 114 | 100 |
| 115 testing::runPendingTasks(); | |
| 116 EXPECT_EQ("", ExecuteJavaScript("run_order.join(', ')")); | 101 EXPECT_EQ("", ExecuteJavaScript("run_order.join(', ')")); |
| 117 | 102 |
| 118 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::ADVANCE); | 103 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::ADVANCE); |
| 119 runTasksForPeriod(1000); | 104 testing::runPendingTasks(); |
| 120 | 105 |
| 121 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); | 106 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); |
| 122 } | 107 } |
| 123 | 108 |
| 124 TEST_F(VirtualTimeTest, VirtualTimeNotAllowedToAdvanceWhileResourcesLoading) | 109 TEST_F(VirtualTimeTest, VirtualTimeNotAllowedToAdvanceWhileResourcesLoading) |
| 125 { | 110 { |
| 126 webView().scheduler()->enableVirtualTime(); | 111 webView().scheduler()->enableVirtualTime(); |
| 127 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::DETERMINISTIC_LOADING); | 112 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::DETERMINISTIC_LOADING); |
| 128 | 113 |
| 129 // To ensure determinism virtual time is not allowed to advance until we hav
e seen at least one load. | 114 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 130 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | |
| 131 | 115 |
| 132 SimRequest mainResource("https://example.com/test.html", "text/html"); | 116 SimRequest mainResource("https://example.com/test.html", "text/html"); |
| 133 SimRequest cssResource("https://example.com/test.css", "text/css"); | 117 SimRequest cssResource("https://example.com/test.css", "text/css"); |
| 134 | 118 |
| 119 // Not loading, virtual time should be able to advance. |
| 120 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 121 |
| 135 // Loading, virtual time should not advance. | 122 // Loading, virtual time should not advance. |
| 136 loadURL("https://example.com/test.html"); | 123 loadURL("https://example.com/test.html"); |
| 137 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 124 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 138 | 125 |
| 139 mainResource.start(); | 126 mainResource.start(); |
| 140 | 127 |
| 141 // Still Loading, virtual time should not advance. | 128 // Still Loading, virtual time should not advance. |
| 142 mainResource.write("<!DOCTYPE html><link rel=stylesheet href=test.css>"); | 129 mainResource.write("<!DOCTYPE html><link rel=stylesheet href=test.css>"); |
| 143 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 130 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 144 | 131 |
| 145 // Still Loading, virtual time should not advance. | 132 // Still Loading, virtual time should not advance. |
| 146 cssResource.start(); | 133 cssResource.start(); |
| 147 cssResource.write("a { color: red; }"); | 134 cssResource.write("a { color: red; }"); |
| 148 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 135 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 149 | 136 |
| 150 // Still Loading, virtual time should not advance. | 137 // Still Loading, virtual time should not advance. |
| 151 cssResource.finish(); | 138 cssResource.finish(); |
| 152 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 139 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 153 | 140 |
| 154 // Still Loading, virtual time should not advance. | 141 // Still Loading, virtual time should not advance. |
| 155 mainResource.write("<body>"); | 142 mainResource.write("<body>"); |
| 156 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 143 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 157 | 144 |
| 158 // Finished loading, virtual time should be able to advance. | 145 // Finished loading, virtual time should be able to advance. |
| 159 mainResource.finish(); | 146 mainResource.finish(); |
| 160 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); | 147 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 161 } | 148 } |
| 162 | 149 |
| 163 } // namespace blink | 150 } // namespace blink |
| OLD | NEW |