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