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.
5 #ifndef CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_
6 #define CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_
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"
17 // Command line arguments specific to the chromoting browser tests.
18 const char kOverrideUserDataDir
[] = "override-user-data-dir";
19 const char kNoCleanup
[] = "no-cleanup";
20 const char kNoInstall
[] = "no-install";
21 const char kWebAppCrx
[] = "webapp-crx";
22 const char kWebAppUnpacked
[] = "webapp-unpacked";
23 const char kUsername
[] = "username";
24 const char kkPassword
[] = "password";
25 const char kMe2MePin
[] = "me2me-pin";
26 const char kRemoteHostName
[] = "remote-host-name";
27 const char kExtensionName
[] = "extension-name";
29 // ASSERT_TRUE can only be used in void returning functions. This version
30 // should be used in non-void-returning functions.
31 inline void _ASSERT_TRUE(bool condition
) {
32 ASSERT_TRUE(condition
);
38 using extensions::Extension
;
42 class RemoteDesktopBrowserTest
: public extensions::PlatformAppBrowserTest
{
44 RemoteDesktopBrowserTest();
45 virtual ~RemoteDesktopBrowserTest();
47 // InProcessBrowserTest Overrides
48 virtual void SetUp() OVERRIDE
;
49 virtual void SetUpOnMainThread() OVERRIDE
;
52 // InProcessBrowserTest Overrides
53 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
;
55 // InProcessBrowserTest Overrides
56 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE
;
58 // The following helpers each perform a simple task.
60 // Verify the test has access to the internet (specifically google.com)
61 void VerifyInternetAccess();
63 // Install the chromoting extension from a crx file.
64 void InstallChromotingAppCrx();
66 // Install the unpacked chromoting extension.
67 void InstallChromotingAppUnpacked();
69 // Uninstall the chromoting extension.
70 void UninstallChromotingApp();
72 // Test whether the chromoting extension is installed.
73 void VerifyChromotingLoaded(bool expected
);
75 // Launch the chromoting app.
76 void LaunchChromotingApp();
78 // Authorize: grant extended access permission to the user's computer.
81 // Authenticate: sign in to google using the credentials provided.
84 // Approve: grant the chromoting app necessary permissions.
87 // Click on "Get Started" in the Me2Me section and show the host list.
90 // Disconnect the active Me2Me session.
91 void DisconnectMe2Me();
93 // Simulate a key event.
94 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
, const char* code
);
96 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
,
103 // Simulate typing a character
104 void SimulateCharInput(char c
);
106 // Simulate typing a string
107 void SimulateStringInput(const std::string
& input
);
109 // Helper to simulate a left button mouse click.
110 void SimulateMouseLeftClickAt(int x
, int y
);
112 // Helper to simulate a mouse click.
113 void SimulateMouseClickAt(
114 int modifiers
, blink::WebMouseEvent::Button button
, int x
, int y
);
116 // The following helpers each perform a composite task.
118 // Install the chromoting extension
121 // Clean up after the test.
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.
129 // Connect to the local host through Me2Me.
130 void ConnectToLocalHost(bool remember_pin
);
132 // Connect to a remote host through Me2Me.
133 void ConnectToRemoteHost(const std::string
& host_name
, bool remember_pin
);
135 // Enter the pin number and connect.
136 void EnterPin(const std::string
& name
, bool remember_pin
);
138 // Helper to get the pin number used for me2me authentication.
139 std::string
me2me_pin() { return me2me_pin_
; }
141 // Helper to get the name of the remote host to connect to.
142 std::string
remote_host_name() { return remote_host_name_
; }
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
);
149 // We need to reset the DNS lookup when we finish, or the test will fail.
150 void DisableDNSLookupForThisTest();
152 void ParseCommandLine();
156 // Helper to get the path to the crx file of the webapp to be tested.
157 base::FilePath
WebAppCrxPath() { return webapp_crx_
; }
159 // Helper to get the extension ID of the installed chromoting webapp.
160 std::string
ChromotingID() { return extension_
->id(); }
162 // Is this a appsv2 web app?
163 bool is_platform_app() {
164 return extension_
->GetType() == extensions::Manifest::TYPE_PLATFORM_APP
;
167 // Are we testing an unpacked extension?
169 return !webapp_unpacked_
.empty();
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();
178 // Whether to perform the cleanup tasks (uninstalling chromoting, etc).
179 // This is useful for diagnostic purposes.
180 bool NoCleanup() { return no_cleanup_
; }
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_
; }
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");
191 // Helper to retrieve the current URL in the active WebContents.
192 GURL
GetCurrentURL() {
193 return active_web_contents()->GetURL();
196 // Helpers to execute javascript code on a web page.
198 // Helper to execute a javascript code snippet in the active WebContents.
199 void ExecuteScript(const std::string
& script
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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");
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
);
246 // Click on the named HTML control in the active WebContents.
247 void ClickOnControl(const std::string
& name
);
249 // Wait for the me2me connection to be established.
250 void WaitForConnection();
252 // Checking whether the localHost has been initialized.
253 bool IsLocalHostReady();
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();
259 // Callback used by WaitForConnection to check whether the connection
260 // has been established.
261 bool IsSessionConnected();
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());
269 // If the "Host version out-of-date" form is visible, dismiss it.
270 void DismissHostVersionWarningIfVisible();
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
);
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_
;
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_
;
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_
;
307 } // namespace remoting
309 #endif // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_