OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/test/remoting/remote_test_helper.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/test/remoting/waiter.h" |
| 9 |
| 10 namespace remoting { |
| 11 |
| 12 Event::Event() : action(Action::None), value(0), modifiers(0) {} |
| 13 |
| 14 RemoteTestHelper::RemoteTestHelper(content::WebContents* web_content) |
| 15 : web_content_(web_content) {} |
| 16 |
| 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)); |
| 25 |
| 26 return result; |
| 27 } |
| 28 |
| 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)); |
| 37 |
| 38 return result; |
| 39 } |
| 40 |
| 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)); |
| 49 |
| 50 return result; |
| 51 } |
| 52 |
| 53 void RemoteTestHelper::ExecuteRpc(const std::string& method, |
| 54 base::TimeDelta timeout, |
| 55 base::TimeDelta interval) { |
| 56 ASSERT_TRUE(content::ExecuteScript(web_content_, method)); |
| 57 |
| 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()); |
| 68 } |
| 69 |
| 70 void RemoteTestHelper::ClearLastEvent() { |
| 71 ExecuteRpc("jsonRpc.clearLastEvent();"); |
| 72 } |
| 73 |
| 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"); |
| 81 } |
| 82 |
| 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()); |
| 91 |
| 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"); |
| 103 } |
| 104 |
| 105 } // namespace remoting |
OLD | NEW |