Chromium Code Reviews| 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 "components/scheduler/renderer/web_view_scheduler_impl.h" | |
| 6 | |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "content/public/renderer/render_view.h" | |
| 9 #include "content/public/test/render_view_test.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
| 12 #include "third_party/WebKit/public/web/WebScriptSource.h" | |
| 13 #include "third_party/WebKit/public/web/WebView.h" | |
| 14 #include "v8/include/v8.h" | |
| 15 | |
| 16 namespace scheduler { | |
| 17 | |
| 18 // RenderViewTest-based tests crash on Android | |
| 19 // http://crbug.com/187500 | |
| 20 #if defined(OS_ANDROID) | |
| 21 #define MAYBE_WebViewSchedulerBrowserTest DISABLED_WebViewSchedulerBrowserTest | |
| 22 #else | |
| 23 #define MAYBE_WebViewSchedulerBrowserTest WebViewSchedulerBrowserTest | |
| 24 #endif // defined(OS_ANDROID) | |
| 25 | |
| 26 class MAYBE_WebViewSchedulerBrowserTest : public content::RenderViewTest { | |
| 27 public: | |
| 28 MAYBE_WebViewSchedulerBrowserTest() : content::RenderViewTest() {} | |
| 29 ~MAYBE_WebViewSchedulerBrowserTest() override {} | |
| 30 | |
| 31 protected: | |
| 32 void SetUp() override { | |
| 33 content::RenderViewTest::SetUp(); | |
| 34 } | |
| 35 | |
| 36 void TearDown() override { | |
| 37 content::RenderViewTest::TearDown(); | |
| 38 } | |
| 39 | |
| 40 bool ExecuteJavaScriptAndReturnStringValue( | |
| 41 const base::string16& script, std::string* out_result); | |
| 42 | |
| 43 private: | |
| 44 DISALLOW_COPY_AND_ASSIGN(MAYBE_WebViewSchedulerBrowserTest); | |
| 45 }; | |
| 46 | |
| 47 bool MAYBE_WebViewSchedulerBrowserTest::ExecuteJavaScriptAndReturnStringValue( | |
| 48 const base::string16& script, | |
| 49 std::string* out_result) { | |
| 50 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | |
| 51 v8::Local<v8::Value> result = GetMainFrame()->executeScriptAndReturnValue( | |
| 52 blink::WebScriptSource(script)); | |
| 53 if (result.IsEmpty() || !result->IsString()) | |
| 54 return false; | |
| 55 | |
| 56 v8::String::Utf8Value utf8_result(result); | |
| 57 *out_result = std::string(*utf8_result, utf8_result.length()); | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 TEST_F(MAYBE_WebViewSchedulerBrowserTest, DOMTimersFireInExpectedOrder) { | |
| 62 ExecuteJavaScriptForTests( | |
| 63 "window.run_order = [];" | |
| 64 "window.timerFn = function(delay, value){" | |
|
Sami
2016/02/03 18:03:44
nit: missing space before {
alex clarke (OOO till 29th)
2016/02/04 15:00:10
Done.
| |
| 65 " setTimeout(function(){ window.run_order.push(value);}, delay);" | |
|
Sami
2016/02/03 18:03:44
nit: missing space before {
Same comment for all
alex clarke (OOO till 29th)
2016/02/04 15:00:09
Done.
| |
| 66 "};" | |
| 67 "var one_hour = 60 * 60 * 1000;" | |
| 68 "window.timerFn(one_hour * 100, 'a');" | |
| 69 "window.timerFn(one_hour * 10, 'b');" | |
| 70 "window.timerFn(one_hour, 'c');"); | |
| 71 | |
| 72 view_->GetWebView()->scheduler()->enableVirtualTime(); | |
| 73 base::TimeTicks start = base::TimeTicks::Now(); | |
| 74 ProcessPendingMessages(); | |
| 75 base::TimeTicks end = base::TimeTicks::Now(); | |
| 76 | |
| 77 std::string result; | |
| 78 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue( | |
| 79 base::ASCIIToUTF16("window.run_order.join(', ')"), &result)); | |
| 80 EXPECT_EQ("c, b, a", result); | |
| 81 | |
| 82 base::TimeDelta elapsed_time = end - start; | |
| 83 // Normally the above JS would take 100 hours to run, but thanks to timer | |
|
Sami
2016/02/03 18:03:44
nit: The JS runs pretty much instantly but the tim
alex clarke (OOO till 29th)
2016/02/04 15:00:09
Done.
| |
| 84 // fast forwarding we can run it in milliseconds. Note I've used 1s here to | |
| 85 // try and avoid a flaky test. | |
| 86 EXPECT_LT(elapsed_time, base::TimeDelta::FromSeconds(1)); | |
|
Sami
2016/02/03 18:03:44
One second may be a little too optimistic for Andr
Sami
2016/02/05 13:54:34
Should we bump this to 10s to increase the flakine
alex clarke (OOO till 29th)
2016/02/05 15:20:39
Done.
| |
| 87 } | |
| 88 | |
| 89 TEST_F(MAYBE_WebViewSchedulerBrowserTest, SetInterval) { | |
| 90 ExecuteJavaScriptForTests( | |
| 91 "window.run_order = [];" | |
| 92 "window.count = 10;" | |
| 93 "window.interval_handle = setInterval(function(){" | |
| 94 " if (--window.count == 0) {" | |
| 95 " clearInterval(window.interval_handle);" | |
| 96 " }" | |
| 97 " window.run_order.push(window.count);" | |
| 98 "}, 1000);" | |
| 99 "setTimeout(function(){ window.run_order.push('timer');}, 1500);"); | |
| 100 | |
| 101 view_->GetWebView()->scheduler()->enableVirtualTime(); | |
| 102 ProcessPendingMessages(); | |
| 103 | |
| 104 std::string result; | |
| 105 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue( | |
| 106 base::ASCIIToUTF16("window.run_order.join(', ')"), &result)); | |
| 107 // If virtual time is not supplied to TimeBase then the setInterval won't | |
|
Sami
2016/02/03 18:03:44
Did you mean blink::TimerBase?
alex clarke (OOO till 29th)
2016/02/04 15:00:09
Done.
| |
| 108 // fire 10x. | |
| 109 EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", result); | |
| 110 } | |
| 111 | |
| 112 TEST_F(MAYBE_WebViewSchedulerBrowserTest, AllowVirtualTimeToAdvance) { | |
| 113 ExecuteJavaScriptForTests( | |
| 114 "window.run_order = [];" | |
| 115 "window.timerFn = function(delay, value){" | |
| 116 " setTimeout(function(){ window.run_order.push(value);}, delay);" | |
| 117 "};" | |
| 118 "window.timerFn(100, 'a');" | |
| 119 "window.timerFn(10, 'b');" | |
| 120 "window.timerFn(1, 'c');"); | |
| 121 | |
| 122 view_->GetWebView()->scheduler()->enableVirtualTime(); | |
| 123 view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(false); | |
| 124 ProcessPendingMessages(); | |
|
Sami
2016/02/03 18:03:44
If the thread gets descheduled, we might end up ru
alex clarke (OOO till 29th)
2016/02/04 15:00:09
As discussed offline I think we're OK because if v
| |
| 125 | |
| 126 std::string result; | |
| 127 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue( | |
| 128 base::ASCIIToUTF16("window.run_order.join(', ')"), &result)); | |
| 129 EXPECT_EQ("", result); | |
| 130 | |
| 131 view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(true); | |
| 132 ProcessPendingMessages(); | |
| 133 | |
| 134 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue( | |
| 135 base::ASCIIToUTF16("window.run_order.join(', ')"), &result)); | |
| 136 EXPECT_EQ("c, b, a", result); | |
| 137 } | |
| 138 | |
| 139 TEST_F(MAYBE_WebViewSchedulerBrowserTest, Normal_NonVirtualTime) { | |
| 140 ExecuteJavaScriptForTests( | |
| 141 "window.run_order = [];" | |
| 142 "window.timerFn = function(delay, value){" | |
| 143 " setTimeout(function(){ window.run_order.push(value);}, delay);" | |
| 144 "};" | |
| 145 "window.timerFn(20, 'a');" | |
| 146 "window.timerFn(10, 'b');" | |
| 147 "window.timerFn(1, 'c');"); | |
| 148 | |
| 149 msg_loop_.task_runner()->PostDelayedTask( | |
| 150 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | |
| 151 base::TimeDelta::FromMilliseconds(30)); | |
| 152 msg_loop_.Run(); | |
| 153 | |
| 154 std::string result; | |
| 155 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue( | |
| 156 base::ASCIIToUTF16("window.run_order.join(', ')"), &result)); | |
| 157 EXPECT_EQ("c, b, a", result); | |
| 158 } | |
| 159 | |
|
Sami
2016/02/03 18:03:44
Should we have a test that combines throttling and
alex clarke (OOO till 29th)
2016/02/04 15:00:09
We have a unit test RepeatingTimer_PageInBackgroun
Sami
2016/02/05 13:54:34
Ah okay, that should be sufficient.
alex clarke (OOO till 29th)
2016/02/05 15:20:39
Acknowledged.
| |
| 160 } // namespace scheduler | |
| OLD | NEW |