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

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: Rebased 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 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..c4c61a938d4ddf663bfc79de2524eac6e0f02cb0
--- /dev/null
+++ b/components/scheduler/renderer/web_view_scheduler_impl_browsertest.cc
@@ -0,0 +1,156 @@
+// 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
esprehn 2016/02/18 03:22:59 is this really true? https://code.google.com/p/ch
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Lets find out :) It would be nice if it does work
+#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(
esprehn 2016/02/18 03:23:00 why not return a std::string instead and just empt
alex clarke (OOO till 29th) 2016/02/18 16:07:56 Done.
+ const base::string16& script,
esprehn 2016/02/18 03:22:59 I think you want to use std::string
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Done.
+ std::string* out_result) {
+ v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
+ v8::Local<v8::Value> result = GetMainFrame()->executeScriptAndReturnValue(
esprehn 2016/02/18 03:22:59 the docs claim this function is deprecated and you
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Done.
+ blink::WebScriptSource(script));
esprehn 2016/02/18 03:22:59 WebString::fromUTF8 ?
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Done.
+ 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 = [];"
esprehn 2016/02/18 03:22:59 var, no need to write window. all over here
alex clarke (OOO till 29th) 2016/02/18 16:07:56 Done.
+ "window.timerFn = function(delay, value) {"
+ " setTimeout(function() { window.run_order.push(value); }, delay);"
+ "};"
+ "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));
esprehn 2016/02/18 03:22:59 ditto, also just pass a const char* ? No need for
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Done.
+ EXPECT_EQ("c, b, a", result);
+
+ base::TimeDelta elapsed_time = end - start;
+ // Normally the JS runs pretty much instantly but the timer callbacks will
+ // take 100h to fire, but thanks to timer fast forwarding we can make them
+ // fire immediatly. Note I've used 10s here to try and avoid a flaky test.
+ EXPECT_LT(elapsed_time, base::TimeDelta::FromSeconds(10));
+}
+
+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 blink::TimerBase then the setInterval
+ // won't 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();
+
+ std::string result;
+ EXPECT_TRUE(ExecuteJavaScriptAndReturnStringValue(
+ base::ASCIIToUTF16("window.run_order.join(', ')"), &result));
esprehn 2016/02/18 03:22:59 I think your test wants to return window.run_order
alex clarke (OOO till 29th) 2016/02/18 16:07:56 Acknowledged.
+ 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);
esprehn 2016/02/18 03:22:59 EXPECT_EQ("c, b, a", ExecuteJavaScriptAndReturnStr
alex clarke (OOO till 29th) 2016/02/18 16:07:56 Done.
+}
+
+TEST_F(MAYBE_WebViewSchedulerBrowserTest, Normal_NonVirtualTime) {
+ ExecuteJavaScriptForTests(
+ "window.run_order = [];"
+ "window.timerFn = function(delay, value) {"
esprehn 2016/02/18 03:22:59 function scheduleTimer, also with the other ones
alex clarke (OOO till 29th) 2016/02/18 16:07:57 Done.
+ " 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);
+}
+
+} // namespace scheduler

Powered by Google App Engine
This is Rietveld 408576698