Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chrome / test / remoting / remote_test_helper.cc
blob622ee2ede66f3a3c56dc286bf4c4c26b4c9f22fe
1 // Copyright 2014 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.
5 #include "chrome/test/remoting/remote_test_helper.h"
7 #include "base/bind.h"
8 #include "chrome/test/remoting/waiter.h"
10 namespace remoting {
12 Event::Event() : action(Action::None), value(0), modifiers(0) {}
14 RemoteTestHelper::RemoteTestHelper(content::WebContents* web_content)
15 : web_content_(web_content) {}
17 // static
18 bool RemoteTestHelper::ExecuteScriptAndExtractBool(
19 content::WebContents* web_contents, const std::string& script) {
20 bool result;
21 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
22 web_contents,
23 "window.domAutomationController.send(" + script + ");",
24 &result));
26 return result;
29 // static
30 int RemoteTestHelper::ExecuteScriptAndExtractInt(
31 content::WebContents* web_contents, const std::string& script) {
32 int result;
33 _ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
34 web_contents,
35 "window.domAutomationController.send(" + script + ");",
36 &result));
38 return result;
41 // static
42 std::string RemoteTestHelper::ExecuteScriptAndExtractString(
43 content::WebContents* web_contents, const std::string& script) {
44 std::string result;
45 _ASSERT_TRUE(content::ExecuteScriptAndExtractString(
46 web_contents,
47 "window.domAutomationController.send(" + script + ");",
48 &result));
50 return result;
53 void RemoteTestHelper::ExecuteRpc(const std::string& method,
54 base::TimeDelta timeout,
55 base::TimeDelta interval) {
56 ASSERT_TRUE(content::ExecuteScript(web_content_, method));
58 // Wait until we receive a response object from the server.
59 // When this happens the jsonRpc.reponseObject becomes non-null.
60 ConditionalTimeoutWaiter waiter(
61 timeout,
62 interval,
63 base::Bind(
64 &RemoteTestHelper::ExecuteScriptAndExtractBool,
65 web_content_,
66 "jsonRpc.responseObject != null"));
67 EXPECT_TRUE(waiter.Wait());
70 void RemoteTestHelper::ClearLastEvent() {
71 ExecuteRpc("jsonRpc.clearLastEvent();");
74 bool RemoteTestHelper::IsValidEvent() {
75 // Call GetLastEvent on the server
76 ExecuteRpc("jsonRpc.getLastEvent()",
77 base::TimeDelta::FromMilliseconds(250),
78 base::TimeDelta::FromMilliseconds(50));
79 return ExecuteScriptAndExtractBool(web_content_,
80 "jsonRpc.responseObject.action != 0");
83 void RemoteTestHelper::GetLastEvent(Event* event) {
84 // Wait for a valid event
85 ConditionalTimeoutWaiter waiter(
86 base::TimeDelta::FromSeconds(2),
87 base::TimeDelta::FromMilliseconds(500),
88 base::Bind(&RemoteTestHelper::IsValidEvent,
89 base::Unretained(this)));
90 EXPECT_TRUE(waiter.Wait());
92 // Extract the event's values
93 event->action = static_cast<Action>(
94 ExecuteScriptAndExtractInt(
95 web_content_,
96 "jsonRpc.responseObject.action"));
97 event->value = ExecuteScriptAndExtractInt(
98 web_content_,
99 "jsonRpc.responseObject.value");
100 event->modifiers = ExecuteScriptAndExtractInt(
101 web_content_,
102 "jsonRpc.responseObject.modifiers");
105 } // namespace remoting