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"
8 #include "chrome/test/remoting/waiter.h"
12 Event::Event() : action(Action::None
), value(0), modifiers(0) {}
14 RemoteTestHelper::RemoteTestHelper(content::WebContents
* web_content
)
15 : web_content_(web_content
) {}
18 bool RemoteTestHelper::ExecuteScriptAndExtractBool(
19 content::WebContents
* web_contents
, const std::string
& script
) {
21 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
23 "window.domAutomationController.send(" + script
+ ");",
30 int RemoteTestHelper::ExecuteScriptAndExtractInt(
31 content::WebContents
* web_contents
, const std::string
& script
) {
33 _ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
35 "window.domAutomationController.send(" + script
+ ");",
42 std::string
RemoteTestHelper::ExecuteScriptAndExtractString(
43 content::WebContents
* web_contents
, const std::string
& script
) {
45 _ASSERT_TRUE(content::ExecuteScriptAndExtractString(
47 "window.domAutomationController.send(" + script
+ ");",
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(
64 &RemoteTestHelper::ExecuteScriptAndExtractBool
,
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(
96 "jsonRpc.responseObject.action"));
97 event
->value
= ExecuteScriptAndExtractInt(
99 "jsonRpc.responseObject.value");
100 event
->modifiers
= ExecuteScriptAndExtractInt(
102 "jsonRpc.responseObject.modifiers");
105 } // namespace remoting