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

Unified 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: Try to fix the broken tests Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc
diff --git a/components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc b/components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..131854af0d72e4cd80d7c41e4cfc7b11619de374
--- /dev/null
+++ b/components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc
@@ -0,0 +1,160 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/scheduler/renderer/web_view_scheduler_impl.h"
+
+#include "base/strings/utf_string_conversions.h"
+#include "content/public/renderer/render_view.h"
+#include "content/public/test/render_view_test.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/public/web/WebLocalFrame.h"
+#include "third_party/WebKit/public/web/WebScriptSource.h"
+#include "third_party/WebKit/public/web/WebView.h"
+#include "v8/include/v8.h"
+
+namespace scheduler {
+
+// RenderViewTest-based tests crash on Android
+// http://crbug.com/187500
+#if defined(OS_ANDROID)
+#define MAYBE_WebViewSchedulerBrowserTest DISABLED_WebViewSchedulerBrowserTest
+#else
+#define MAYBE_WebViewSchedulerBrowserTest WebViewSchedulerBrowserTest
+#endif // defined(OS_ANDROID)
+
+class MAYBE_WebViewSchedulerBrowserTest : public content::RenderViewTest {
+ public:
+ MAYBE_WebViewSchedulerBrowserTest() : content::RenderViewTest() {}
+ ~MAYBE_WebViewSchedulerBrowserTest() override {}
+
+ protected:
+ void SetUp() override {
+ content::RenderViewTest::SetUp();
+ }
+
+ void TearDown() override {
+ content::RenderViewTest::TearDown();
+ }
+
+ bool ExecuteJavaScriptAndReturnStringValue(
+ const base::string16& script, std::string* out_result);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MAYBE_WebViewSchedulerBrowserTest);
+};
+
+bool MAYBE_WebViewSchedulerBrowserTest::ExecuteJavaScriptAndReturnStringValue(
+ const base::string16& script,
+ std::string* out_result) {
+ v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
+ v8::Local<v8::Value> result = GetMainFrame()->executeScriptAndReturnValue(
+ blink::WebScriptSource(script));
+ if (result.IsEmpty() || !result->IsString())
+ return false;
+
+ v8::String::Utf8Value utf8_result(result);
+ *out_result = std::string(*utf8_result, utf8_result.length());
+ return true;
+}
+
+TEST_F(MAYBE_WebViewSchedulerBrowserTest, DOMTimersFireInExpectedOrder) {
+ ExecuteJavaScriptForTests(
+ "window.run_order = [];"
+ "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.
+ " 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.
+ "};"
+ "var one_hour = 60 * 60 * 1000;"
+ "window.timerFn(one_hour * 100, 'a');"
+ "window.timerFn(one_hour * 10, 'b');"
+ "window.timerFn(one_hour, 'c');");
+
+ view_->GetWebView()->scheduler()->enableVirtualTime();
+ base::TimeTicks start = base::TimeTicks::Now();
+ ProcessPendingMessages();
+ base::TimeTicks end = base::TimeTicks::Now();
+
+ std::string result;
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
+ EXPECT_EQ("c, b, a", result);
+
+ base::TimeDelta elapsed_time = end - start;
+ // 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.
+ // fast forwarding we can run it in milliseconds. Note I've used 1s here to
+ // try and avoid a flaky test.
+ 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.
+}
+
+TEST_F(MAYBE_WebViewSchedulerBrowserTest, SetInterval) {
+ ExecuteJavaScriptForTests(
+ "window.run_order = [];"
+ "window.count = 10;"
+ "window.interval_handle = setInterval(function(){"
+ " if (--window.count == 0) {"
+ " clearInterval(window.interval_handle);"
+ " }"
+ " window.run_order.push(window.count);"
+ "}, 1000);"
+ "setTimeout(function(){ window.run_order.push('timer');}, 1500);");
+
+ view_->GetWebView()->scheduler()->enableVirtualTime();
+ ProcessPendingMessages();
+
+ std::string result;
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
+ // 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.
+ // fire 10x.
+ EXPECT_EQ("9, timer, 8, 7, 6, 5, 4, 3, 2, 1, 0", result);
+}
+
+TEST_F(MAYBE_WebViewSchedulerBrowserTest, AllowVirtualTimeToAdvance) {
+ ExecuteJavaScriptForTests(
+ "window.run_order = [];"
+ "window.timerFn = function(delay, value){"
+ " setTimeout(function(){ window.run_order.push(value);}, delay);"
+ "};"
+ "window.timerFn(100, 'a');"
+ "window.timerFn(10, 'b');"
+ "window.timerFn(1, 'c');");
+
+ view_->GetWebView()->scheduler()->enableVirtualTime();
+ view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(false);
+ 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
+
+ std::string result;
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
+ EXPECT_EQ("", result);
+
+ view_->GetWebView()->scheduler()->setAllowVirtualTimeToAdvance(true);
+ ProcessPendingMessages();
+
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
+ EXPECT_EQ("c, b, a", result);
+}
+
+TEST_F(MAYBE_WebViewSchedulerBrowserTest, Normal_NonVirtualTime) {
+ ExecuteJavaScriptForTests(
+ "window.run_order = [];"
+ "window.timerFn = function(delay, value){"
+ " setTimeout(function(){ window.run_order.push(value);}, delay);"
+ "};"
+ "window.timerFn(20, 'a');"
+ "window.timerFn(10, 'b');"
+ "window.timerFn(1, 'c');");
+
+ msg_loop_.task_runner()->PostDelayedTask(
+ FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
+ base::TimeDelta::FromMilliseconds(30));
+ msg_loop_.Run();
+
+ std::string result;
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
+ EXPECT_EQ("c, b, a", result);
+}
+
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.
+} // namespace scheduler

Powered by Google App Engine
This is Rietveld 408576698