| OLD | NEW | 
|---|
|  | (Empty) | 
| 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 |  | 
| 3 // found in the LICENSE file. |  | 
| 4 |  | 
| 5 #include "platform/testing/UnitTestHelpers.h" |  | 
| 6 #include "public/platform/Platform.h" |  | 
| 7 #include "public/platform/WebViewScheduler.h" |  | 
| 8 #include "public/web/WebLocalFrame.h" |  | 
| 9 #include "public/web/WebScriptExecutionCallback.h" |  | 
| 10 #include "public/web/WebScriptSource.h" |  | 
| 11 #include "public/web/WebView.h" |  | 
| 12 #include "web/tests/sim/SimTest.h" |  | 
| 13 |  | 
| 14 namespace blink { |  | 
| 15 |  | 
| 16 namespace { |  | 
| 17 class ScriptExecutionCallbackHelper : public WebScriptExecutionCallback { |  | 
| 18 public: |  | 
| 19     const String result() const { return m_result; } |  | 
| 20 |  | 
| 21 private: |  | 
| 22     void completed(const WebVector<v8::Local<v8::Value>>& values) override |  | 
| 23     { |  | 
| 24         if (!values.isEmpty() && !values[0].IsEmpty() && values[0]->IsString()) 
     { |  | 
| 25             m_result = toCoreString(v8::Local<v8::String>::Cast(values[0])); |  | 
| 26         } |  | 
| 27     } |  | 
| 28 |  | 
| 29     String m_result; |  | 
| 30 }; |  | 
| 31 } // namespace |  | 
| 32 |  | 
| 33 class VirtualTimeTest : public SimTest { |  | 
| 34 protected: |  | 
| 35     String ExecuteJavaScript(String scriptSource) |  | 
| 36     { |  | 
| 37         ScriptExecutionCallbackHelper callbackHelper; |  | 
| 38         webView().mainFrame()->toWebLocalFrame()->requestExecuteScriptAndReturnV
     alue( |  | 
| 39             WebScriptSource(WebString(scriptSource)), false, &callbackHelper); |  | 
| 40         testing::runPendingTasks(); |  | 
| 41         return callbackHelper.result(); |  | 
| 42     } |  | 
| 43 }; |  | 
| 44 |  | 
| 45 TEST_F(VirtualTimeTest, DOMTimersFireInExpectedOrder) |  | 
| 46 { |  | 
| 47     ExecuteJavaScript( |  | 
| 48         "var run_order = [];" |  | 
| 49         "function timerFn(delay, value) {" |  | 
| 50         "  setTimeout(function() { run_order.push(value); }, delay);" |  | 
| 51         "};" |  | 
| 52         "var one_hour = 60 * 60 * 1000;" |  | 
| 53         "timerFn(one_hour * 100, 'a');" |  | 
| 54         "timerFn(one_hour * 10, 'b');" |  | 
| 55         "timerFn(one_hour, 'c');"); |  | 
| 56 |  | 
| 57     webView().scheduler()->enableVirtualTime(); |  | 
| 58 |  | 
| 59     // Normally the JS runs pretty much instantly but the timer callbacks will |  | 
| 60     // take 100h to fire, but thanks to timer fast forwarding we can make them |  | 
| 61     // fire immediatly. |  | 
| 62     testing::runPendingTasks(); |  | 
| 63 |  | 
| 64     EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); |  | 
| 65 } |  | 
| 66 |  | 
| 67 TEST_F(VirtualTimeTest, SetInterval) |  | 
| 68 { |  | 
| 69     ExecuteJavaScript( |  | 
| 70         "var run_order = [];" |  | 
| 71         "var count = 10;" |  | 
| 72         "var interval_handle = setInterval(function() {" |  | 
| 73         "  if (--window.count == 0) {" |  | 
| 74         "     clearInterval(interval_handle);" |  | 
| 75         "  }" |  | 
| 76         "  run_order.push(count);" |  | 
| 77         "}, 1000);" |  | 
| 78         "setTimeout(function() { run_order.push('timer'); }, 1500);"); |  | 
| 79 |  | 
| 80     webView().scheduler()->enableVirtualTime(); |  | 
| 81     testing::runPendingTasks(); |  | 
| 82 |  | 
| 83     // If virtual time is not supplied to TimerBase then the setInterval |  | 
| 84     // won't fire 10x. |  | 
| 85     EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", ExecuteJavaScript("run_orde
     r.join(', ')")); |  | 
| 86 } |  | 
| 87 |  | 
| 88 TEST_F(VirtualTimeTest, AllowVirtualTimeToAdvance) |  | 
| 89 { |  | 
| 90     ExecuteJavaScript( |  | 
| 91         "var run_order = [];" |  | 
| 92         "timerFn = function(delay, value) {" |  | 
| 93         "  setTimeout(function() { run_order.push(value); }, delay);" |  | 
| 94         "};" |  | 
| 95         "timerFn(100, 'a');" |  | 
| 96         "timerFn(10, 'b');" |  | 
| 97         "timerFn(1, 'c');"); |  | 
| 98 |  | 
| 99     webView().scheduler()->enableVirtualTime(); |  | 
| 100     webView().scheduler()->setAllowVirtualTimeToAdvance(false); |  | 
| 101     testing::runPendingTasks(); |  | 
| 102 |  | 
| 103     EXPECT_EQ("", ExecuteJavaScript("run_order.join(', ')")); |  | 
| 104 |  | 
| 105     webView().scheduler()->setAllowVirtualTimeToAdvance(true); |  | 
| 106     testing::runPendingTasks(); |  | 
| 107 |  | 
| 108     EXPECT_EQ("c, b, a", ExecuteJavaScript("run_order.join(', ')")); |  | 
| 109 } |  | 
| 110 |  | 
| 111 } // namespace blink |  | 
| OLD | NEW | 
|---|