This source file includes following definitions.
- SetUp
- SetUpOnMainThread
- SetUpInProcessBrowserTestFixture
- TearDownInProcessBrowserTestFixture
- VerifyInternetAccess
- OpenClientBrowserPage
- HtmlElementVisible
- InstallChromotingAppCrx
- InstallChromotingAppUnpacked
- UninstallChromotingApp
- VerifyChromotingLoaded
- LaunchChromotingApp
- Authorize
- Authenticate
- Approve
- ExpandMe2Me
- DisconnectMe2Me
- SimulateKeyPressWithCode
- SimulateKeyPressWithCode
- SimulateCharInput
- SimulateStringInput
- SimulateMouseLeftClickAt
- SimulateMouseClickAt
- Install
- Cleanup
- Auth
- ConnectToLocalHost
- ConnectToRemoteHost
- EnableDNSLookupForThisTest
- DisableDNSLookupForThisTest
- ParseCommandLine
- ExecuteScript
- ExecuteScriptAndWaitForAnyPageLoad
- ExecuteScriptAndExtractBool
- ExecuteScriptAndExtractInt
- ExecuteScriptAndExtractString
- ClickOnControl
- EnterPin
- WaitForConnection
- IsLocalHostReady
- IsSessionConnected
- IsPinFormVisible
- DismissHostVersionWarningIfVisible
- IsAuthenticatedInWindow
- IsHostActionComplete
#include "chrome/test/remoting/remote_desktop_browsertest.h"
#include "base/command_line.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "chrome/test/remoting/key_code_conv.h"
#include "chrome/test/remoting/page_load_notification_observer.h"
#include "chrome/test/remoting/waiter.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/test/test_utils.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/switches.h"
#include "ui/base/window_open_disposition.h"
namespace remoting {
RemoteDesktopBrowserTest::RemoteDesktopBrowserTest()
: extension_(NULL) {
}
RemoteDesktopBrowserTest::~RemoteDesktopBrowserTest() {}
void RemoteDesktopBrowserTest::SetUp() {
ParseCommandLine();
PlatformAppBrowserTest::SetUp();
}
void RemoteDesktopBrowserTest::SetUpOnMainThread() {
PlatformAppBrowserTest::SetUpOnMainThread();
web_contents_stack_.push_back(
browser()->tab_strip_model()->GetActiveWebContents());
}
void RemoteDesktopBrowserTest::SetUpInProcessBrowserTestFixture() {
EnableDNSLookupForThisTest(
new net::RuleBasedHostResolverProc(host_resolver()));
}
void RemoteDesktopBrowserTest::TearDownInProcessBrowserTestFixture() {
DisableDNSLookupForThisTest();
}
void RemoteDesktopBrowserTest::VerifyInternetAccess() {
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), GURL("http://www.google.com"), 1);
EXPECT_EQ(GetCurrentURL().host(), "www.google.com");
}
void RemoteDesktopBrowserTest::OpenClientBrowserPage() {
ui_test_utils::NavigateToURLWithDisposition(
browser(),
GURL(http_server() + "/clientpage.html"),
NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB);
client_web_content_ = browser()->tab_strip_model()->GetActiveWebContents();
browser()->tab_strip_model()->SelectPreviousTab();
}
bool RemoteDesktopBrowserTest::HtmlElementVisible(const std::string& name) {
_ASSERT_TRUE(HtmlElementExists(name));
ExecuteScript(
"function isElementVisible(name) {"
" var element = document.getElementById(name);"
" /* The existence of the element has already been ASSERTed. */"
" do {"
" if (element.hidden) {"
" return false;"
" }"
" element = element.parentNode;"
" } while (element != null);"
" return true;"
"};");
return ExecuteScriptAndExtractBool(
"isElementVisible(\"" + name + "\")");
}
void RemoteDesktopBrowserTest::InstallChromotingAppCrx() {
ASSERT_TRUE(!is_unpacked());
base::FilePath install_dir(WebAppCrxPath());
scoped_refptr<const Extension> extension(InstallExtensionWithUIAutoConfirm(
install_dir, 1, browser()));
EXPECT_FALSE(extension.get() == NULL);
extension_ = extension.get();
}
void RemoteDesktopBrowserTest::InstallChromotingAppUnpacked() {
ASSERT_TRUE(is_unpacked());
scoped_refptr<extensions::UnpackedInstaller> installer =
extensions::UnpackedInstaller::Create(extension_service());
installer->set_prompt_for_plugins(false);
content::WindowedNotificationObserver observer(
chrome::NOTIFICATION_EXTENSION_LOADED,
content::NotificationService::AllSources());
installer->Load(webapp_unpacked_);
observer.Wait();
}
void RemoteDesktopBrowserTest::UninstallChromotingApp() {
UninstallExtension(ChromotingID());
extension_ = NULL;
}
void RemoteDesktopBrowserTest::VerifyChromotingLoaded(bool expected) {
const extensions::ExtensionSet* extensions =
extension_service()->extensions();
scoped_refptr<const extensions::Extension> extension;
bool installed = false;
for (extensions::ExtensionSet::const_iterator iter = extensions->begin();
iter != extensions->end(); ++iter) {
extension = *iter;
if (extension->name() == extension_name_) {
installed = true;
break;
}
}
if (installed) {
if (extension_)
EXPECT_EQ(extension, extension_);
else
extension_ = extension.get();
extensions::Manifest::Type type = extension->GetType();
EXPECT_TRUE(type == extensions::Manifest::TYPE_PLATFORM_APP ||
type == extensions::Manifest::TYPE_LEGACY_PACKAGED_APP);
EXPECT_TRUE(extension->ShouldDisplayInAppLauncher());
}
ASSERT_EQ(installed, expected);
}
void RemoteDesktopBrowserTest::LaunchChromotingApp() {
ASSERT_TRUE(extension_);
GURL chromoting_main = Chromoting_Main_URL();
PageLoadNotificationObserver observer(chromoting_main);
OpenApplication(AppLaunchParams(
browser()->profile(),
extension_,
is_platform_app() ? extensions::LAUNCH_CONTAINER_NONE :
extensions::LAUNCH_CONTAINER_TAB,
is_platform_app() ? NEW_WINDOW : CURRENT_TAB));
observer.Wait();
content::NavigationController* controller =
content::Source<content::NavigationController>(observer.source()).ptr();
content::WebContents* web_contents = controller->GetWebContents();
if (web_contents != active_web_contents())
web_contents_stack_.push_back(web_contents);
if (is_platform_app()) {
EXPECT_EQ(GetFirstAppWindowWebContents(), active_web_contents());
} else {
ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromSeconds(5)).Wait());
}
EXPECT_EQ(Chromoting_Main_URL(), GetCurrentURL());
}
void RemoteDesktopBrowserTest::Authorize() {
ASSERT_TRUE(extension_);
ASSERT_EQ(Chromoting_Main_URL(), GetCurrentURL());
ASSERT_FALSE(IsAuthenticated());
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
ClickOnControl("auth-button");
observer.Wait();
content::NavigationController* controller =
content::Source<content::NavigationController>(observer.source()).ptr();
web_contents_stack_.push_back(controller->GetWebContents());
EXPECT_EQ("accounts.google.com", GetCurrentURL().host());
EXPECT_TRUE(HtmlElementExists("Email"));
EXPECT_TRUE(HtmlElementExists("Passwd"));
}
void RemoteDesktopBrowserTest::Authenticate() {
ASSERT_TRUE(extension_);
ASSERT_EQ("accounts.google.com", GetCurrentURL().host());
ASSERT_TRUE(HtmlElementExists("Email"));
ASSERT_TRUE(HtmlElementExists("Passwd"));
ExecuteScriptAndWaitForAnyPageLoad(
"document.getElementById(\"Email\").value = \"" + username_ + "\";" +
"document.getElementById(\"Passwd\").value = \"" + password_ +"\";" +
"document.forms[\"gaia_loginform\"].submit();");
if (!is_platform_app()) {
EXPECT_EQ(GetCurrentURL().host(), "accounts.google.com");
EXPECT_TRUE(HtmlElementExists("submit_approve_access"));
}
}
void RemoteDesktopBrowserTest::Approve() {
ASSERT_TRUE(extension_);
if (is_platform_app()) {
web_contents_stack_.pop_back();
ConditionalTimeoutWaiter waiter(
base::TimeDelta::FromSeconds(7),
base::TimeDelta::FromSeconds(1),
base::Bind(
&RemoteDesktopBrowserTest::IsAuthenticatedInWindow,
active_web_contents()));
ASSERT_TRUE(waiter.Wait());
} else {
ASSERT_EQ("accounts.google.com", GetCurrentURL().host());
ASSERT_TRUE(HtmlElementExists("submit_approve_access"));
const GURL chromoting_main = Chromoting_Main_URL();
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
base::Bind(
&RemoteDesktopBrowserTest::IsAuthenticatedInWindow,
browser()->tab_strip_model()->GetActiveWebContents()));
ExecuteScript(
"lso.approveButtonAction();"
"document.forms[\"connect-approve\"].submit();");
observer.Wait();
web_contents_stack_.pop_back();
}
ASSERT_TRUE(GetCurrentURL() == Chromoting_Main_URL());
EXPECT_TRUE(IsAuthenticated());
}
void RemoteDesktopBrowserTest::ExpandMe2Me() {
ASSERT_TRUE(extension_);
ASSERT_EQ(Chromoting_Main_URL(), GetCurrentURL());
EXPECT_TRUE(IsAuthenticated());
ASSERT_FALSE(HtmlElementVisible("me2me-content"));
ASSERT_TRUE(HtmlElementVisible("get-started-me2me"));
ExecuteScript("remoting.showMe2MeUiAndSave();");
EXPECT_TRUE(HtmlElementVisible("me2me-content"));
EXPECT_FALSE(HtmlElementVisible("me2me-first-run"));
ConditionalTimeoutWaiter waiter(
base::TimeDelta::FromSeconds(3),
base::TimeDelta::FromSeconds(1),
base::Bind(&RemoteDesktopBrowserTest::IsLocalHostReady, this));
EXPECT_TRUE(waiter.Wait());
EXPECT_TRUE(ExecuteScriptAndExtractBool(
"remoting.hostList.localHost_.hostName && "
"remoting.hostList.localHost_.hostId && "
"remoting.hostList.localHost_.status && "
"remoting.hostList.localHost_.status == 'ONLINE'"));
}
void RemoteDesktopBrowserTest::DisconnectMe2Me() {
ASSERT_TRUE(extension_);
ASSERT_EQ(Chromoting_Main_URL(), GetCurrentURL());
ASSERT_TRUE(RemoteDesktopBrowserTest::IsSessionConnected());
ClickOnControl("toolbar-stub");
EXPECT_TRUE(HtmlElementVisible("session-toolbar"));
ClickOnControl("toolbar-disconnect");
EXPECT_TRUE(HtmlElementVisible("client-dialog"));
EXPECT_TRUE(HtmlElementVisible("client-reconnect-button"));
EXPECT_TRUE(HtmlElementVisible("client-finished-me2me-button"));
ClickOnControl("client-finished-me2me-button");
EXPECT_FALSE(HtmlElementVisible("client-dialog"));
}
void RemoteDesktopBrowserTest::SimulateKeyPressWithCode(
ui::KeyboardCode keyCode,
const char* code) {
SimulateKeyPressWithCode(keyCode, code, false, false, false, false);
}
void RemoteDesktopBrowserTest::SimulateKeyPressWithCode(
ui::KeyboardCode keyCode,
const char* code,
bool control,
bool shift,
bool alt,
bool command) {
content::SimulateKeyPressWithCode(
active_web_contents(),
keyCode,
code,
control,
shift,
alt,
command);
}
void RemoteDesktopBrowserTest::SimulateCharInput(char c) {
const char* code;
ui::KeyboardCode keyboard_code;
bool shift;
GetKeyValuesFromChar(c, &code, &keyboard_code, &shift);
ASSERT_TRUE(code != NULL);
SimulateKeyPressWithCode(keyboard_code, code, false, shift, false, false);
}
void RemoteDesktopBrowserTest::SimulateStringInput(const std::string& input) {
for (size_t i = 0; i < input.length(); ++i)
SimulateCharInput(input[i]);
}
void RemoteDesktopBrowserTest::SimulateMouseLeftClickAt(int x, int y) {
SimulateMouseClickAt(0, blink::WebMouseEvent::ButtonLeft, x, y);
}
void RemoteDesktopBrowserTest::SimulateMouseClickAt(
int modifiers, blink::WebMouseEvent::Button button, int x, int y) {
ExecuteScript(
"var clientPluginElement = "
"document.getElementById('session-client-plugin');"
"var clientPluginRect = clientPluginElement.getBoundingClientRect();");
int top = ExecuteScriptAndExtractInt("clientPluginRect.top");
int left = ExecuteScriptAndExtractInt("clientPluginRect.left");
int width = ExecuteScriptAndExtractInt("clientPluginRect.width");
int height = ExecuteScriptAndExtractInt("clientPluginRect.height");
ASSERT_GT(x, 0);
ASSERT_LT(x, width);
ASSERT_GT(y, 0);
ASSERT_LT(y, height);
content::SimulateMouseClickAt(
browser()->tab_strip_model()->GetActiveWebContents(),
modifiers,
button,
gfx::Point(left + x, top + y));
}
void RemoteDesktopBrowserTest::Install() {
if (!NoInstall()) {
VerifyChromotingLoaded(false);
if (is_unpacked())
InstallChromotingAppUnpacked();
else
InstallChromotingAppCrx();
}
VerifyChromotingLoaded(true);
}
void RemoteDesktopBrowserTest::Cleanup() {
ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromSeconds(2)).Wait());
if (!NoCleanup()) {
UninstallChromotingApp();
VerifyChromotingLoaded(false);
}
ASSERT_TRUE(TimeoutWaiter(base::TimeDelta::FromSeconds(2)).Wait());
}
void RemoteDesktopBrowserTest::Auth() {
Authorize();
Authenticate();
Approve();
}
void RemoteDesktopBrowserTest::ConnectToLocalHost(bool remember_pin) {
ASSERT_TRUE(ExecuteScriptAndExtractBool(
"remoting.hostList.localHost_.hostName && "
"remoting.hostList.localHost_.hostId && "
"remoting.hostList.localHost_.status && "
"remoting.hostList.localHost_.status == 'ONLINE'"));
ClickOnControl("this-host-connect");
EnterPin(me2me_pin(), remember_pin);
WaitForConnection();
}
void RemoteDesktopBrowserTest::ConnectToRemoteHost(
const std::string& host_name, bool remember_pin) {
std::string host_id = ExecuteScriptAndExtractString(
"remoting.hostList.getHostIdForName('" + host_name + "')");
EXPECT_FALSE(host_id.empty());
std::string element_id = "host_" + host_id;
std::string host_div_class = ExecuteScriptAndExtractString(
"document.getElementById('" + element_id + "').parentNode.className");
EXPECT_NE(std::string::npos, host_div_class.find("host-online"));
ClickOnControl(element_id);
EnterPin(me2me_pin(), remember_pin);
WaitForConnection();
}
void RemoteDesktopBrowserTest::EnableDNSLookupForThisTest(
net::RuleBasedHostResolverProc* host_resolver) {
scoped_refptr<net::RuleBasedHostResolverProc> resolver =
new net::RuleBasedHostResolverProc(host_resolver);
resolver->AllowDirectLookup("*.google.com");
resolver->AllowDirectLookup("*.thawte.com");
resolver->AllowDirectLookup("*.geotrust.com");
resolver->AllowDirectLookup("*.gstatic.com");
resolver->AllowDirectLookup("*.googleapis.com");
mock_host_resolver_override_.reset(
new net::ScopedDefaultHostResolverProc(resolver.get()));
}
void RemoteDesktopBrowserTest::DisableDNSLookupForThisTest() {
mock_host_resolver_override_.reset();
}
void RemoteDesktopBrowserTest::ParseCommandLine() {
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(kOverrideUserDataDir)) {
const base::FilePath& override_user_data_dir =
command_line->GetSwitchValuePath(kOverrideUserDataDir);
ASSERT_FALSE(override_user_data_dir.empty());
command_line->AppendSwitchPath(switches::kUserDataDir,
override_user_data_dir);
}
username_ = command_line->GetSwitchValueASCII(kUsername);
password_ = command_line->GetSwitchValueASCII(kkPassword);
me2me_pin_ = command_line->GetSwitchValueASCII(kMe2MePin);
remote_host_name_ = command_line->GetSwitchValueASCII(kRemoteHostName);
extension_name_ = command_line->GetSwitchValueASCII(kExtensionName);
http_server_ = command_line->GetSwitchValueASCII(kHttpServer);
no_cleanup_ = command_line->HasSwitch(kNoCleanup);
no_install_ = command_line->HasSwitch(kNoInstall);
if (!no_install_) {
webapp_crx_ = command_line->GetSwitchValuePath(kWebAppCrx);
webapp_unpacked_ = command_line->GetSwitchValuePath(kWebAppUnpacked);
ASSERT_NE(webapp_crx_.empty(), webapp_unpacked_.empty());
}
command_line->AppendSwitch(switches::kEnableWebBasedSignin);
command_line->AppendSwitch(
extensions::switches::kEnableExperimentalExtensionApis);
}
void RemoteDesktopBrowserTest::ExecuteScript(const std::string& script) {
ASSERT_TRUE(content::ExecuteScript(active_web_contents(), script));
}
void RemoteDesktopBrowserTest::ExecuteScriptAndWaitForAnyPageLoad(
const std::string& script) {
content::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<content::NavigationController>(
&active_web_contents()->
GetController()));
ExecuteScript(script);
observer.Wait();
}
bool RemoteDesktopBrowserTest::ExecuteScriptAndExtractBool(
content::WebContents* web_contents, const std::string& script) {
bool result;
EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
web_contents,
"window.domAutomationController.send(" + script + ");",
&result));
return result;
}
int RemoteDesktopBrowserTest::ExecuteScriptAndExtractInt(
content::WebContents* web_contents, const std::string& script) {
int result;
_ASSERT_TRUE(content::ExecuteScriptAndExtractInt(
web_contents,
"window.domAutomationController.send(" + script + ");",
&result));
return result;
}
std::string RemoteDesktopBrowserTest::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 RemoteDesktopBrowserTest::ClickOnControl(const std::string& name) {
ASSERT_TRUE(HtmlElementVisible(name));
ExecuteScript("document.getElementById(\"" + name + "\").click();");
}
void RemoteDesktopBrowserTest::EnterPin(const std::string& pin,
bool remember_pin) {
ConditionalTimeoutWaiter waiter(
base::TimeDelta::FromSeconds(5),
base::TimeDelta::FromSeconds(1),
base::Bind(&RemoteDesktopBrowserTest::IsPinFormVisible, this));
EXPECT_TRUE(waiter.Wait());
ExecuteScript(
"document.getElementById(\"pin-entry\").value = \"" + pin + "\";");
if (remember_pin) {
EXPECT_TRUE(HtmlElementVisible("remember-pin"));
EXPECT_FALSE(ExecuteScriptAndExtractBool(
"document.getElementById('remember-pin-checkbox').checked"));
ClickOnControl("remember-pin");
EXPECT_TRUE(ExecuteScriptAndExtractBool(
"document.getElementById('remember-pin-checkbox').checked"));
}
ClickOnControl("pin-connect-button");
}
void RemoteDesktopBrowserTest::WaitForConnection() {
ConditionalTimeoutWaiter waiter(
base::TimeDelta::FromSeconds(4),
base::TimeDelta::FromSeconds(1),
base::Bind(&RemoteDesktopBrowserTest::IsSessionConnected, this));
EXPECT_TRUE(waiter.Wait());
TimeoutWaiter(base::TimeDelta::FromSeconds(2)).Wait();
}
bool RemoteDesktopBrowserTest::IsLocalHostReady() {
return ExecuteScriptAndExtractBool("remoting.hostList.localHost_ != null");
}
bool RemoteDesktopBrowserTest::IsSessionConnected() {
DismissHostVersionWarningIfVisible();
return ExecuteScriptAndExtractBool(
"remoting.clientSession != null && "
"remoting.clientSession.getState() == "
"remoting.ClientSession.State.CONNECTED");
}
bool RemoteDesktopBrowserTest::IsPinFormVisible() {
DismissHostVersionWarningIfVisible();
return HtmlElementVisible("pin-form");
}
void RemoteDesktopBrowserTest::DismissHostVersionWarningIfVisible() {
if (HtmlElementVisible("host-needs-update-connect-button"))
ClickOnControl("host-needs-update-connect-button");
}
bool RemoteDesktopBrowserTest::IsAuthenticatedInWindow(
content::WebContents* web_contents) {
return ExecuteScriptAndExtractBool(
web_contents, "remoting.identity.isAuthenticated()");
}
bool RemoteDesktopBrowserTest::IsHostActionComplete(
content::WebContents* client_web_content,
std::string host_action_var) {
return ExecuteScriptAndExtractBool(
client_web_content,
host_action_var);
}
}