| 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" |
| 11 #include "public/web/WebView.h" | 11 #include "public/web/WebView.h" |
| 12 #include "web/tests/sim/SimRequest.h" |
| 12 #include "web/tests/sim/SimTest.h" | 13 #include "web/tests/sim/SimTest.h" |
| 13 | 14 |
| 14 namespace blink { | 15 namespace blink { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { | 18 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { |
| 18 public: | 19 public: |
| 19 const String result() const { return m_result; } | 20 const String result() const { return m_result; } |
| 20 | 21 |
| 21 private: | 22 private: |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 "setTimeout(function() { run_order.push('timer'); }, 1500);"); | 80 "setTimeout(function() { run_order.push('timer'); }, 1500);"); |
| 80 | 81 |
| 81 // If virtual time is not supplied to TimerBase then the setInterval | 82 // If virtual time is not supplied to TimerBase then the setInterval |
| 82 // won't fire 10x. | 83 // won't fire 10x. |
| 83 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(', ')")); |
| 84 } | 85 } |
| 85 | 86 |
| 86 TEST_F(VirtualTimeTest, AllowVirtualTimeToAdvance) | 87 TEST_F(VirtualTimeTest, AllowVirtualTimeToAdvance) |
| 87 { | 88 { |
| 88 webView().scheduler()->enableVirtualTime(); | 89 webView().scheduler()->enableVirtualTime(); |
| 89 webView().scheduler()->setAllowVirtualTimeToAdvance(false); | 90 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::PAUSE); |
| 90 | 91 |
| 91 ExecuteJavaScript( | 92 ExecuteJavaScript( |
| 92 "var run_order = [];" | 93 "var run_order = [];" |
| 93 "timerFn = function(delay, value) {" | 94 "timerFn = function(delay, value) {" |
| 94 " setTimeout(function() { run_order.push(value); }, delay);" | 95 " setTimeout(function() { run_order.push(value); }, delay);" |
| 95 "};" | 96 "};" |
| 96 "timerFn(100, 'a');" | 97 "timerFn(100, 'a');" |
| 97 "timerFn(10, 'b');" | 98 "timerFn(10, 'b');" |
| 98 "timerFn(1, 'c');"); | 99 "timerFn(1, 'c');"); |
| 99 | 100 |
| 100 EXPECT_EQ("", ExecuteJavaScript("run_order.join(', ')")); | 101 EXPECT_EQ("", ExecuteJavaScript("run_order.join(', ')")); |
| 101 | 102 |
| 102 webView().scheduler()->setAllowVirtualTimeToAdvance(true); | 103 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::ADVANCE); |
| 103 testing::runPendingTasks(); | 104 testing::runPendingTasks(); |
| 104 | 105 |
| 105 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); | 106 EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); |
| 106 } | 107 } |
| 107 | 108 |
| 109 TEST_F(VirtualTimeTest, VirtualTimeNotAllowedToAdvanceWhileResourcesLoading) |
| 110 { |
| 111 webView().scheduler()->enableVirtualTime(); |
| 112 webView().scheduler()->setVirtualTimePolicy(WebViewScheduler::VirtualTimePol
icy::PAUSE_IF_NETWORK_FETCHES_PENDING); |
| 113 |
| 114 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 115 |
| 116 SimRequest mainResource("https://example.com/test.html", "text/html"); |
| 117 SimRequest cssResource("https://example.com/test.css", "text/css"); |
| 118 |
| 119 // Not loading, virtual time should be able to advance. |
| 120 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 121 |
| 122 // Loading, virtual time should not advance. |
| 123 loadURL("https://example.com/test.html"); |
| 124 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 125 |
| 126 mainResource.start(); |
| 127 |
| 128 // Still Loading, virtual time should not advance. |
| 129 mainResource.write("<!DOCTYPE html><link rel=stylesheet href=test.css>"); |
| 130 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 131 |
| 132 // Still Loading, virtual time should not advance. |
| 133 cssResource.start(); |
| 134 cssResource.write("a { color: red; }"); |
| 135 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 136 |
| 137 // Still Loading, virtual time should not advance. |
| 138 cssResource.finish(); |
| 139 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 140 |
| 141 // Still Loading, virtual time should not advance. |
| 142 mainResource.write("<body>"); |
| 143 EXPECT_FALSE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 144 |
| 145 // Finished loading, virtual time should be able to advance. |
| 146 mainResource.finish(); |
| 147 EXPECT_TRUE(webView().scheduler()->virtualTimeAllowedToAdvance()); |
| 148 } |
| 149 |
| 108 } // namespace blink | 150 } // namespace blink |
| OLD | NEW |