Chromium Code Reviews| 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..0f47f331d6d1540e45b38dc151f47df627eeed10 |
| --- /dev/null |
| +++ b/chrome/test/remoting/remote_test_helper.cc |
| @@ -0,0 +1,99 @@ |
| +// 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" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace remoting { |
| + |
| +// static |
| +bool RemoteTestHelper::ExecuteScriptAndExtractBool( |
| + content::WebContents* web_content, const std::string& script) { |
| + bool result; |
| + EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| + web_content, |
| + "window.domAutomationController.send(" + script + ");", |
| + &result)); |
| + |
| + return result; |
| +} |
| + |
| +// static |
| +int RemoteTestHelper::ExecuteScriptAndExtractInt( |
| + content::WebContents* web_content, const std::string& script) { |
| + int result; |
| + EXPECT_TRUE(content::ExecuteScriptAndExtractInt( |
| + web_content, |
| + "window.domAutomationController.send(" + script + ");", |
| + &result)); |
| + |
| + return result; |
| +} |
| + |
| +// Static |
| +void RemoteTestHelper::ExecuteRpc( |
| + content::WebContents* web_content, |
|
Jamie
2014/12/17 03:15:40
Nit: Line-wrap for these parameters looks odd. I t
Mike Meade
2014/12/18 18:54:24
Done.
|
| + const std::string& method, |
| + int timeout_secs, |
| + int poll_ms) { |
| + ASSERT_TRUE( |
| + content::ExecuteScript( |
| + web_content, |
| + "jsonRpc.responseObject = null")); |
| + ASSERT_TRUE( |
| + content::ExecuteScript(web_content, method)); |
| + // Wait until the client tab sets the right variables |
| + ConditionalTimeoutWaiter waiter( |
| + base::TimeDelta::FromSeconds(timeout_secs), |
| + base::TimeDelta::FromMilliseconds(poll_ms), |
| + base::Bind(&ExecuteScriptAndExtractBool, |
| + web_content, |
| + "jsonRpc.responseObject != null")); |
| + EXPECT_TRUE(waiter.Wait()); |
| +} |
| + |
| +// static |
| +void RemoteTestHelper::ClearLastEvent(content::WebContents* web_content) { |
| + ExecuteRpc(web_content, "jsonRpc.clearLastEvent();"); |
| +} |
| + |
| +// static |
| +bool RemoteTestHelper::CheckForLastEvent(content::WebContents* web_content) { |
| + // Reset the response |
| + EXPECT_TRUE(content::ExecuteScript( |
| + web_content, "jsonRpc.responseObject = null")); |
| + // Call GetLastEvent on the server |
| + ExecuteRpc(web_content, "jsonRpc.getLastEvent()", 250, 50); |
| + return ExecuteScriptAndExtractBool(web_content, |
| + "jsonRpc.responseObject.action != 0"); |
| +} |
| + |
| +// static |
| +void RemoteTestHelper::GetLastEvent( |
| + content::WebContents* web_content, |
| + Event& event) { |
| + // Wait for a valid event |
| + ConditionalTimeoutWaiter waiter( |
| + base::TimeDelta::FromSeconds(2), |
| + base::TimeDelta::FromMilliseconds(500), |
| + base::Bind(&CheckForLastEvent, |
| + web_content)); |
| + 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 |