Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1002)

Side by Side Diff: components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc

Issue 1646583002: [Reland] Per WebViewScheduler virtual time (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove some unneeded changes Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 { content::RenderViewTest::SetUp(); }
33
34 void TearDown() override { content::RenderViewTest::TearDown(); }
35
36 bool ExecuteJavaScriptAndReturnStringValue(const base::string16& script,
37 std::string* out_result);
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(MAYBE_WebViewSchedulerBrowserTest);
41 };
42
43 bool MAYBE_WebViewSchedulerBrowserTest::ExecuteJavaScriptAndReturnStringValue(
44 const base::string16& script,
45 std::string* out_result) {
46 v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
47 v8::Local<v8::Value> result = GetMainFrame()->executeScriptAndReturnValue(
48 blink::WebScriptSource(script));
49 if (result.IsEmpty() || !result->IsString())
50 return false;
51
52 v8::String::Utf8Value utf8_result(result);
53 *out_result = std::string(*utf8_result, utf8_result.length());
54 return true;
55 }
56
57 TEST_F(MAYBE_WebViewSchedulerBrowserTest, DOMTimersFireInExpectedOrder) {
58 ExecuteJavaScriptForTests(
59 "window.run_order = [];"
60 "window.timerFn = function(delay, value) {"
61 " setTimeout(function() { window.run_order.push(value);}, delay);"
Sami 2016/02/05 13:54:34 nit: Missing space before the } too. Same below in
alex clarke (OOO till 29th) 2016/02/05 15:20:39 Done.
62 "};"
63 "var one_hour = 60 * 60 * 1000;"
64 "window.timerFn(one_hour * 100, 'a');"
65 "window.timerFn(one_hour * 10, 'b');"
66 "window.timerFn(one_hour, 'c');");
67
68 view_->GetWebView()->scheduler()->enableVirtualTime();
69 base::TimeTicks start = base::TimeTicks::Now();
70 ProcessPendingMessages();
71 base::TimeTicks end = base::TimeTicks::Now();
72
73 std::string result;
74 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
75 base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
76 EXPECT_EQ("c, b, a", result);
77
78 base::TimeDelta elapsed_time = end - start;
79 // Normally the JS runs pretty much instantly but the timer callbacks will
80 // take 100h to fire, but thanks to timer fast forwarding we can make them
81 // fire immediatly. Note I've used 1s here to try and avoid a flaky test.
82 EXPECT_LT(elapsed_time, base::TimeDelta::FromSeconds(1));
83 }
84
85 TEST_F(MAYBE_WebViewSchedulerBrowserTest, SetInterval) {
86 ExecuteJavaScriptForTests(
87 "window.run_order = [];"
88 "window.count = 10;"
89 "window.interval_handle = setInterval(function() {"
90 " if (--window.count == 0) {"
91 " clearInterval(window.interval_handle);"
92 " }"
93 " window.run_order.push(window.count);"
94 "}, 1000);"
95 "setTimeout(function() { window.run_order.push('timer');}, 1500);");
96
97 view_->GetWebView()->scheduler()->enableVirtualTime();
98 ProcessPendingMessages();
99
100 std::string result;
101 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
102 base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
103 // If virtual time is not supplied to blink::TimeBase then the setInterval
Sami 2016/02/05 13:54:34 typo: TimerBase
alex clarke (OOO till 29th) 2016/02/05 15:20:40 Done.
104 // won't fire 10x.
105 EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", result);
106 }
107
108 TEST_F(MAYBE_WebViewSchedulerBrowserTest, AllowVirtualTimeToAdvance) {
109 ExecuteJavaScriptForTests(
110 "window.run_order = [];"
111 "window.timerFn = function(delay, value) {"
112 " setTimeout(function() { window.run_order.push(value);}, delay);"
113 "};"
114 "window.timerFn(100, 'a');"
115 "window.timerFn(10, 'b');"
116 "window.timerFn(1, 'c');");
117
118 view_->GetWebView()->scheduler()->enableVirtualTime();
119 view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(false);
120 ProcessPendingMessages();
121
122 std::string result;
123 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
124 base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
125 EXPECT_EQ("", result);
126
127 view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(true);
128 ProcessPendingMessages();
129
130 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
131 base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
132 EXPECT_EQ("c, b, a", result);
133 }
134
135 TEST_F(MAYBE_WebViewSchedulerBrowserTest, Normal_NonVirtualTime) {
136 ExecuteJavaScriptForTests(
137 "window.run_order = [];"
138 "window.timerFn = function(delay, value) {"
139 " setTimeout(function() { window.run_order.push(value);}, delay);"
140 "};"
141 "window.timerFn(20, 'a');"
142 "window.timerFn(10, 'b');"
143 "window.timerFn(1, 'c');");
144
145 msg_loop_.task_runner()->PostDelayedTask(
146 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
147 base::TimeDelta::FromMilliseconds(30));
148 msg_loop_.Run();
149
150 std::string result;
151 EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
152 base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
153 EXPECT_EQ("c, b, a", result);
154 }
155
156 } // namespace scheduler
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698