remote_desktop_browsertest.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 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#ifndef CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_ 6#define CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_ 7 8#include "chrome/browser/apps/app_browsertest_util.h" 9#include "chrome/browser/chrome_notification_types.h" 10#include "chrome/browser/ui/tabs/tab_strip_model.h" 11#include "chrome/test/base/ui_test_utils.h" 12#include "content/public/browser/notification_service.h" 13#include "content/public/test/browser_test_utils.h" 14#include "net/dns/mock_host_resolver.h" 15 16namespace { 17// Command line arguments specific to the chromoting browser tests. 18const char kOverrideUserDataDir[] = "override-user-data-dir"; 19const char kNoCleanup[] = "no-cleanup"; 20const char kNoInstall[] = "no-install"; 21const char kWebAppCrx[] = "webapp-crx"; 22const char kWebAppUnpacked[] = "webapp-unpacked"; 23const char kUsername[] = "username"; 24const char kkPassword[] = "password"; 25const char kMe2MePin[] = "me2me-pin"; 26const char kRemoteHostName[] = "remote-host-name"; 27const char kExtensionName[] = "extension-name"; 28 29// ASSERT_TRUE can only be used in void returning functions. This version 30// should be used in non-void-returning functions. 31inline void _ASSERT_TRUE(bool condition) { 32 ASSERT_TRUE(condition); 33 return; 34} 35 36} // namespace 37 38using extensions::Extension; 39 40namespace remoting { 41 42class RemoteDesktopBrowserTest : public extensions::PlatformAppBrowserTest { 43 public: 44 RemoteDesktopBrowserTest(); 45 virtual ~RemoteDesktopBrowserTest(); 46 47 // InProcessBrowserTest Overrides 48 virtual void SetUp() OVERRIDE; 49 virtual void SetUpOnMainThread() OVERRIDE; 50 51 protected: 52 // InProcessBrowserTest Overrides 53 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; 54 55 // InProcessBrowserTest Overrides 56 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; 57 58 // The following helpers each perform a simple task. 59 60 // Verify the test has access to the internet (specifically google.com) 61 void VerifyInternetAccess(); 62 63 // Install the chromoting extension from a crx file. 64 void InstallChromotingAppCrx(); 65 66 // Install the unpacked chromoting extension. 67 void InstallChromotingAppUnpacked(); 68 69 // Uninstall the chromoting extension. 70 void UninstallChromotingApp(); 71 72 // Test whether the chromoting extension is installed. 73 void VerifyChromotingLoaded(bool expected); 74 75 // Launch the chromoting app. 76 void LaunchChromotingApp(); 77 78 // Authorize: grant extended access permission to the user's computer. 79 void Authorize(); 80 81 // Authenticate: sign in to google using the credentials provided. 82 void Authenticate(); 83 84 // Approve: grant the chromoting app necessary permissions. 85 void Approve(); 86 87 // Click on "Get Started" in the Me2Me section and show the host list. 88 void ExpandMe2Me(); 89 90 // Disconnect the active Me2Me session. 91 void DisconnectMe2Me(); 92 93 // Simulate a key event. 94 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode, const char* code); 95 96 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode, 97 const char* code, 98 bool control, 99 bool shift, 100 bool alt, 101 bool command); 102 103 // Simulate typing a character 104 void SimulateCharInput(char c); 105 106 // Simulate typing a string 107 void SimulateStringInput(const std::string& input); 108 109 // Helper to simulate a left button mouse click. 110 void SimulateMouseLeftClickAt(int x, int y); 111 112 // Helper to simulate a mouse click. 113 void SimulateMouseClickAt( 114 int modifiers, blink::WebMouseEvent::Button button, int x, int y); 115 116 // The following helpers each perform a composite task. 117 118 // Install the chromoting extension 119 void Install(); 120 121 // Clean up after the test. 122 void Cleanup(); 123 124 // Perform all the auth steps: authorization, authenticattion, etc. 125 // It starts from the chromoting main page unauthenticated and ends up back 126 // on the chromoting main page authenticated and ready to go. 127 void Auth(); 128 129 // Connect to the local host through Me2Me. 130 void ConnectToLocalHost(bool remember_pin); 131 132 // Connect to a remote host through Me2Me. 133 void ConnectToRemoteHost(const std::string& host_name, bool remember_pin); 134 135 // Enter the pin number and connect. 136 void EnterPin(const std::string& name, bool remember_pin); 137 138 // Helper to get the pin number used for me2me authentication. 139 std::string me2me_pin() { return me2me_pin_; } 140 141 // Helper to get the name of the remote host to connect to. 142 std::string remote_host_name() { return remote_host_name_; } 143 144 // Change behavior of the default host resolver to allow DNS lookup 145 // to proceed instead of being blocked by the test infrastructure. 146 void EnableDNSLookupForThisTest( 147 net::RuleBasedHostResolverProc* host_resolver); 148 149 // We need to reset the DNS lookup when we finish, or the test will fail. 150 void DisableDNSLookupForThisTest(); 151 152 void ParseCommandLine(); 153 154 // Accessor methods. 155 156 // Helper to get the path to the crx file of the webapp to be tested. 157 base::FilePath WebAppCrxPath() { return webapp_crx_; } 158 159 // Helper to get the extension ID of the installed chromoting webapp. 160 std::string ChromotingID() { return extension_->id(); } 161 162 // Is this a appsv2 web app? 163 bool is_platform_app() { 164 return extension_->GetType() == extensions::Manifest::TYPE_PLATFORM_APP; 165 } 166 167 // Are we testing an unpacked extension? 168 bool is_unpacked() { 169 return !webapp_unpacked_.empty(); 170 } 171 172 // The "active" WebContents instance the test needs to interact with. 173 content::WebContents* active_web_contents() { 174 DCHECK(!web_contents_stack_.empty()); 175 return web_contents_stack_.back(); 176 } 177 178 // Whether to perform the cleanup tasks (uninstalling chromoting, etc). 179 // This is useful for diagnostic purposes. 180 bool NoCleanup() { return no_cleanup_; } 181 182 // Whether to install the chromoting extension before running the test cases. 183 // This is useful for diagnostic purposes. 184 bool NoInstall() { return no_install_; } 185 186 // Helper to construct the starting URL of the installed chromoting webapp. 187 GURL Chromoting_Main_URL() { 188 return GURL("chrome-extension://" + ChromotingID() + "/main.html"); 189 } 190 191 // Helper to retrieve the current URL in the active WebContents. 192 GURL GetCurrentURL() { 193 return active_web_contents()->GetURL(); 194 } 195 196 // Helpers to execute javascript code on a web page. 197 198 // Helper to execute a javascript code snippet in the active WebContents. 199 void ExecuteScript(const std::string& script); 200 201 // Helper to execute a javascript code snippet in the active WebContents 202 // and wait for page load to complete. 203 void ExecuteScriptAndWaitForAnyPageLoad(const std::string& script); 204 205 // Helper to execute a javascript code snippet in the active WebContents 206 // and extract the boolean result. 207 bool ExecuteScriptAndExtractBool(const std::string& script) { 208 return ExecuteScriptAndExtractBool(active_web_contents(), script); 209 } 210 211 // Helper to execute a javascript code snippet and extract the boolean result. 212 static bool ExecuteScriptAndExtractBool(content::WebContents* web_contents, 213 const std::string& script); 214 215 // Helper to execute a javascript code snippet in the active WebContents 216 // and extract the int result. 217 int ExecuteScriptAndExtractInt(const std::string& script) { 218 return ExecuteScriptAndExtractInt(active_web_contents(), script); 219 } 220 221 // Helper to execute a javascript code snippet and extract the int result. 222 static int ExecuteScriptAndExtractInt(content::WebContents* web_contents, 223 const std::string& script); 224 225 // Helper to execute a javascript code snippet in the active WebContents 226 // and extract the string result. 227 std::string ExecuteScriptAndExtractString(const std::string& script) { 228 return ExecuteScriptAndExtractString(active_web_contents(), script); 229 } 230 231 // Helper to execute a javascript code snippet and extract the string result. 232 static std::string ExecuteScriptAndExtractString( 233 content::WebContents* web_contents, const std::string& script); 234 235 // Helper to check whether an html element with the given name exists in 236 // the active WebContents. 237 bool HtmlElementExists(const std::string& name) { 238 return ExecuteScriptAndExtractBool( 239 "document.getElementById(\"" + name + "\") != null"); 240 } 241 242 // Helper to check whether a html element with the given name is visible in 243 // the active WebContents. 244 bool HtmlElementVisible(const std::string& name); 245 246 // Click on the named HTML control in the active WebContents. 247 void ClickOnControl(const std::string& name); 248 249 // Wait for the me2me connection to be established. 250 void WaitForConnection(); 251 252 // Checking whether the localHost has been initialized. 253 bool IsLocalHostReady(); 254 255 // Callback used by EnterPin to check whether the pin form is visible 256 // and to dismiss the host-needs-update dialog. 257 bool IsPinFormVisible(); 258 259 // Callback used by WaitForConnection to check whether the connection 260 // has been established. 261 bool IsSessionConnected(); 262 263 // Callback used by Approve to check whether the chromoting app has 264 // successfully authenticated with the Google services. 265 bool IsAuthenticated() { 266 return IsAuthenticatedInWindow(active_web_contents()); 267 } 268 269 // If the "Host version out-of-date" form is visible, dismiss it. 270 void DismissHostVersionWarningIfVisible(); 271 272 // Callback used by Approve to check whether the chromoting app has 273 // successfully authenticated with the Google services. 274 static bool IsAuthenticatedInWindow(content::WebContents* web_contents); 275 276 private: 277 // Fields 278 279 // This test needs to make live DNS requests for access to 280 // GAIA and sync server URLs under google.com. We use a scoped version 281 // to override the default resolver while the test is active. 282 scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_; 283 284 // Stores all the WebContents instance in a stack so that we can easily 285 // return to the previous instance. 286 // The active WebContents instance is always stored at the top of the stack. 287 // Initially the stack contains the WebContents instance created by 288 // InProcessBrowserTest as the initial context to run test in. 289 // Whenever a WebContents instance is spawned and needs attention we 290 // push it onto the stack and that becomes the active instance. 291 // And once we are done with the current WebContents instance 292 // we pop it off the stack, returning to the previous instance. 293 std::vector<content::WebContents*> web_contents_stack_; 294 295 bool no_cleanup_; 296 bool no_install_; 297 const Extension* extension_; 298 base::FilePath webapp_crx_; 299 base::FilePath webapp_unpacked_; 300 std::string username_; 301 std::string password_; 302 std::string me2me_pin_; 303 std::string remote_host_name_; 304 std::string extension_name_; 305}; 306 307} // namespace remoting 308 309#endif // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_ 310