Index: chrome/test/remoting/remote_test_helper.cc |
diff --git a/chrome/test/remoting/remote_test_helper.cc b/chrome/test/remoting/remote_test_helper.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..54f00f875b150bd1da5f6b98f0c2345df516fa64 |
--- /dev/null |
+++ b/chrome/test/remoting/remote_test_helper.cc |
@@ -0,0 +1,111 @@ |
+// Copyright 2014 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 "chrome/test/remoting/remote_test_helper.h" |
+ |
+#include "base/bind.h" |
+#include "chrome/test/remoting/waiter.h" |
+ |
+namespace remoting { |
+ |
+Event::Event() : action(Action::NONE), value(0), modifiers(0) {} |
+ |
+RemoteTestHelper::RemoteTestHelper(content::WebContents* web_content) |
+:web_content_(web_content) {} |
Jamie
2015/01/06 19:31:38
Nit: indentation.
Mike Meade
2015/01/08 18:58:42
Done.
|
+ |
+// static |
+bool RemoteTestHelper::ExecuteScriptAndExtractBool( |
+ content::WebContents* web_contents, const std::string& script) { |
+ bool result; |
+ EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
+ web_contents, |
+ "window.domAutomationController.send(" + script + ");", |
+ &result)); |
+ |
+ return result; |
+} |
+ |
+// static |
+int RemoteTestHelper::ExecuteScriptAndExtractInt( |
+ content::WebContents* web_contents, const std::string& script) { |
+ int result; |
+ _ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
+ web_contents, |
+ "window.domAutomationController.send(" + script + ");", |
+ &result)); |
+ |
+ return result; |
+} |
+ |
+// static |
+std::string RemoteTestHelper::ExecuteScriptAndExtractString( |
+ content::WebContents* web_contents, const std::string& script) { |
+ std::string result; |
+ _ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
+ web_contents, |
+ "window.domAutomationController.send(" + script + ");", |
+ &result)); |
+ |
+ return result; |
+} |
+ |
+void RemoteTestHelper::ExecuteRpc(const std::string& method, |
+ base::TimeDelta timeout, |
+ base::TimeDelta interval) { |
+ ASSERT_TRUE( |
+ content::ExecuteScript( |
Jamie
2015/01/06 19:31:39
This indentation is inconsistent with the _ASSERT_
Mike Meade
2015/01/08 18:58:42
Done.
|
+ web_content_, |
+ "jsonRpc.responseObject = null")); |
+ ASSERT_TRUE( |
+ content::ExecuteScript(web_content_, method)); |
+ // Wait until the client tab sets the right variables |
Jamie
2015/01/06 19:31:38
This comment is a bit vague. If I'm reading the co
Mike Meade
2015/01/08 18:58:42
Done.
|
+ ConditionalTimeoutWaiter waiter( |
+ timeout, |
Jamie
2015/01/06 19:31:38
Nit: Indentation.
Mike Meade
2015/01/08 18:58:42
Done.
|
+ interval, |
+ base::Bind( |
+ &RemoteTestHelper::ExecuteScriptAndExtractBool, |
+ web_content_, |
+ "jsonRpc.responseObject != null")); |
+ EXPECT_TRUE(waiter.Wait()); |
+} |
+ |
+void RemoteTestHelper::ClearLastEvent() { |
+ ExecuteRpc("jsonRpc.clearLastEvent();"); |
+} |
+ |
+bool RemoteTestHelper::IsValidEvent() { |
+ // Reset the response |
+ EXPECT_TRUE(content::ExecuteScript( |
+ web_content_, "jsonRpc.responseObject = null")); |
Jamie
2015/01/06 19:31:39
ExecuteRpc already does this, so you don't need th
Mike Meade
2015/01/08 18:58:42
Done.
|
+ // Call GetLastEvent on the server |
+ ExecuteRpc("jsonRpc.getLastEvent()", |
+ base::TimeDelta::FromMilliseconds(250), |
+ base::TimeDelta::FromMilliseconds(50)); |
+ return ExecuteScriptAndExtractBool(web_content_, |
+ "jsonRpc.responseObject.action != 0"); |
+} |
+ |
+void RemoteTestHelper::GetLastEvent(Event* event) { |
+ // Wait for a valid event |
+ ConditionalTimeoutWaiter waiter( |
+ base::TimeDelta::FromSeconds(2), |
Jamie
2015/01/06 19:31:39
Indentation.
Mike Meade
2015/01/08 18:58:42
Done.
|
+ base::TimeDelta::FromMilliseconds(500), |
+ base::Bind(&RemoteTestHelper::IsValidEvent, |
+ base::Unretained(this))); |
+ EXPECT_TRUE(waiter.Wait()); |
+ |
+ // Extract the event's values |
+ event->action = static_cast<Action>( |
+ ExecuteScriptAndExtractInt( |
+ web_content_, |
+ "jsonRpc.responseObject.action")); |
+ event->value = ExecuteScriptAndExtractInt( |
+ web_content_, |
+ "jsonRpc.responseObject.value"); |
+ event->modifiers = ExecuteScriptAndExtractInt( |
+ web_content_, |
+ "jsonRpc.responseObject.modifiers"); |
+} |
+ |
+} // namespace remoting |