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 "base/debug/stack_trace.h"
9 #include "chrome/browser/apps/app_browsertest_util.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "net/dns/mock_host_resolver.h"
18 // Command line arguments specific to the chromoting browser tests.
19 const char kOverrideUserDataDir
[] = "override-user-data-dir";
20 const char kNoCleanup
[] = "no-cleanup";
21 const char kNoInstall
[] = "no-install";
22 const char kWebAppCrx
[] = "webapp-crx";
23 const char kWebAppUnpacked
[] = "webapp-unpacked";
24 const char kUserName
[] = "username";
25 const char kUserPassword
[] = "password";
26 const char kAccountsFile
[] = "accounts-file";
27 const char kAccountType
[] = "account-type";
28 const char kMe2MePin
[] = "me2me-pin";
29 const char kRemoteHostName
[] = "remote-host-name";
30 const char kExtensionName
[] = "extension-name";
31 const char kHttpServer
[] = "http-server";
33 // ASSERT_TRUE can only be used in void returning functions. This version
34 // should be used in non-void-returning functions.
35 inline void _ASSERT_TRUE(bool condition
) {
37 // ASSERT_TRUE only prints the first call frame in the error message.
38 // In our case, this is the _ASSERT_TRUE wrapper function, which is not
39 // useful. To help with debugging, we will dump the full callstack.
40 LOG(ERROR
) << "Assertion failed.";
41 LOG(ERROR
) << base::debug::StackTrace().ToString();
43 ASSERT_TRUE(condition
);
49 using extensions::Extension
;
53 class RemoteDesktopBrowserTest
: public extensions::PlatformAppBrowserTest
{
55 RemoteDesktopBrowserTest();
56 virtual ~RemoteDesktopBrowserTest();
58 // InProcessBrowserTest Overrides
59 virtual void SetUp() override
;
60 virtual void SetUpOnMainThread() override
;
63 // InProcessBrowserTest Overrides
64 virtual void SetUpInProcessBrowserTestFixture() override
;
66 // InProcessBrowserTest Overrides
67 virtual void TearDownInProcessBrowserTestFixture() override
;
69 // The following helpers each perform a simple task.
71 // Verify the test has access to the internet (specifically google.com)
72 void VerifyInternetAccess();
74 // Open the client page for the browser test to get status of host actions
75 void OpenClientBrowserPage();
77 // Install the chromoting extension from a crx file.
78 void InstallChromotingAppCrx();
80 // Install the unpacked chromoting extension.
81 void InstallChromotingAppUnpacked();
83 // Uninstall the chromoting extension.
84 void UninstallChromotingApp();
86 // Test whether the chromoting extension is installed.
87 void VerifyChromotingLoaded(bool expected
);
89 // Launch the chromoting app.
90 void LaunchChromotingApp();
92 // Authorize: grant extended access permission to the user's computer.
95 // Authenticate: sign in to google using the credentials provided.
98 // Approve: grant the chromoting app necessary permissions.
101 // Click on "Get Started" in the Me2Me section and show the host list.
104 // Disconnect the active Me2Me session.
105 void DisconnectMe2Me();
107 // Simulate a key event.
108 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
, const char* code
);
110 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
,
117 // Simulate typing a character
118 void SimulateCharInput(char c
);
120 // Simulate typing a string
121 void SimulateStringInput(const std::string
& input
);
123 // Helper to simulate a left button mouse click.
124 void SimulateMouseLeftClickAt(int x
, int y
);
126 // Helper to simulate a mouse click.
127 void SimulateMouseClickAt(
128 int modifiers
, blink::WebMouseEvent::Button button
, int x
, int y
);
130 void SetUserNameAndPassword(
131 const base::FilePath
&accounts_file
, const std::string
& account_type
);
133 // The following helpers each perform a composite task.
135 // Install the chromoting extension
138 // Perform all necessary steps (installation, authorization, authentication,
139 // expanding the me2me section) so that the app is ready for a me2me
141 void SetUpTestForMe2Me();
143 // Clean up after the test.
146 // Perform all the auth steps: authorization, authentication, etc.
147 // It starts from the chromoting main page unauthenticated and ends up back
148 // on the chromoting main page authenticated and ready to go.
151 // Ensures that the host is started locally with |me2me_pin()|.
152 // Browser_test.js must be loaded before calling this function.
153 void EnsureRemoteConnectionEnabled();
155 // Connect to the local host through Me2Me.
156 void ConnectToLocalHost(bool remember_pin
);
158 // Connect to a remote host through Me2Me.
159 void ConnectToRemoteHost(const std::string
& host_name
, bool remember_pin
);
161 // Enter the pin number and connect.
162 void EnterPin(const std::string
& name
, bool remember_pin
);
164 // Helper to get the pin number used for me2me authentication.
165 std::string
me2me_pin() { return me2me_pin_
; }
167 // Helper to get the name of the remote host to connect to.
168 std::string
remote_host_name() { return remote_host_name_
; }
170 // Helper to get the test controller URL.
171 std::string
http_server() { return http_server_
; }
173 // Change behavior of the default host resolver to allow DNS lookup
174 // to proceed instead of being blocked by the test infrastructure.
175 void EnableDNSLookupForThisTest(
176 net::RuleBasedHostResolverProc
* host_resolver
);
178 // We need to reset the DNS lookup when we finish, or the test will fail.
179 void DisableDNSLookupForThisTest();
181 void ParseCommandLine();
185 // Helper to get the path to the crx file of the webapp to be tested.
186 base::FilePath
WebAppCrxPath() { return webapp_crx_
; }
188 // Helper to get the extension ID of the installed chromoting webapp.
189 std::string
ChromotingID() { return extension_
->id(); }
191 // Is this a appsv2 web app?
192 bool is_platform_app() {
193 return extension_
->GetType() == extensions::Manifest::TYPE_PLATFORM_APP
;
196 // Are we testing an unpacked extension?
198 return !webapp_unpacked_
.empty();
201 // The "active" WebContents instance the test needs to interact with.
202 content::WebContents
* active_web_contents() {
203 DCHECK(!web_contents_stack_
.empty());
204 return web_contents_stack_
.back();
207 // The client WebContents instance the test needs to interact with.
208 content::WebContents
* client_web_content() {
209 return client_web_content_
;
212 content::WebContents
* app_web_content() {
213 return app_web_content_
;
216 // Whether to perform the cleanup tasks (uninstalling chromoting, etc).
217 // This is useful for diagnostic purposes.
218 bool NoCleanup() { return no_cleanup_
; }
220 // Whether to install the chromoting extension before running the test cases.
221 // This is useful for diagnostic purposes.
222 bool NoInstall() { return no_install_
; }
224 // Helper to construct the starting URL of the installed chromoting webapp.
225 GURL
Chromoting_Main_URL() {
226 return GURL("chrome-extension://" + ChromotingID() + "/main.html");
229 // Helper to retrieve the current URL in the active WebContents.
230 GURL
GetCurrentURL() {
231 return active_web_contents()->GetURL();
234 // Helpers to execute JavaScript code on a web page.
236 // Helper to execute a JavaScript code snippet in the active WebContents.
237 void ExecuteScript(const std::string
& script
);
239 // Helper to execute a JavaScript code snippet in the active WebContents
240 // and wait for page load to complete.
241 void ExecuteScriptAndWaitForAnyPageLoad(const std::string
& script
);
243 // Helper to execute a JavaScript code snippet in the active WebContents
244 // and extract the boolean result.
245 bool ExecuteScriptAndExtractBool(const std::string
& script
) {
246 return ExecuteScriptAndExtractBool(active_web_contents(), script
);
249 // Helper to execute a JavaScript code snippet and extract the boolean result.
250 static bool ExecuteScriptAndExtractBool(content::WebContents
* web_contents
,
251 const std::string
& script
);
253 // Helper to execute a JavaScript code snippet in the active WebContents
254 // and extract the int result.
255 int ExecuteScriptAndExtractInt(const std::string
& script
) {
256 return ExecuteScriptAndExtractInt(active_web_contents(), script
);
259 // Helper to execute a JavaScript code snippet and extract the int result.
260 static int ExecuteScriptAndExtractInt(content::WebContents
* web_contents
,
261 const std::string
& script
);
263 // Helper to execute a JavaScript code snippet in the active WebContents
264 // and extract the string result.
265 std::string
ExecuteScriptAndExtractString(const std::string
& script
) {
266 return ExecuteScriptAndExtractString(active_web_contents(), script
);
269 // Helper to execute a JavaScript code snippet and extract the string result.
270 static std::string
ExecuteScriptAndExtractString(
271 content::WebContents
* web_contents
, const std::string
& script
);
273 // Helper to load a JavaScript file from |path| and inject it to
274 // current web_content. The variable |path| is relative to the directory of
275 // the |browsertest| executable.
276 static bool LoadScript(content::WebContents
* web_contents
,
277 const base::FilePath::StringType
& path
);
279 // Helper to execute a JavaScript browser test. It creates an object using
280 // the |browserTest.testName| ctor and calls |run| on the created object with
281 // |testData|, which can be any arbitrary object literal. The script
282 // browser_test.js must be loaded (using LoadScript) before calling this
284 void RunJavaScriptTest(content::WebContents
* web_contents
,
285 const std::string
& testName
,
286 const std::string
& testData
);
288 // Helper to check whether an html element with the given name exists in
289 // the active WebContents.
290 bool HtmlElementExists(const std::string
& name
) {
291 return ExecuteScriptAndExtractBool(
292 "document.getElementById(\"" + name
+ "\") != null");
295 // Helper to check whether a html element with the given name is visible in
296 // the active WebContents.
297 bool HtmlElementVisible(const std::string
& name
);
299 // Click on the named HTML control in the active WebContents.
300 void ClickOnControl(const std::string
& name
);
302 // Wait for the me2me connection to be established.
303 void WaitForConnection();
305 // Checking whether the localHost has been initialized.
306 bool IsLocalHostReady();
308 // Callback used by EnterPin to check whether the pin form is visible
309 // and to dismiss the host-needs-update dialog.
310 bool IsPinFormVisible();
312 // Callback used by WaitForConnection to check whether the connection
313 // has been established.
314 bool IsSessionConnected();
316 // Callback used by Approve to check whether the chromoting app has
317 // successfully authenticated with the Google services.
318 bool IsAuthenticated() {
319 return IsAuthenticatedInWindow(active_web_contents());
322 // Callback used by Approve to check whether the Accept button is enabled
323 // and ready to receive a click.
324 static bool IsEnabled(
325 content::WebContents
* web_contents
, const std::string
& name
);
327 // If the "Host version out-of-date" form is visible, dismiss it.
328 void DismissHostVersionWarningIfVisible();
330 // Callback used by Approve to check whether the chromoting app has
331 // successfully authenticated with the Google services.
332 static bool IsAuthenticatedInWindow(content::WebContents
* web_contents
);
334 // Callback used to check whether a host action is completed.
335 // Used by browser tests while conditionally waiting for host actions.
336 static bool IsHostActionComplete(
337 content::WebContents
* client_web_content
, std::string host_action_var
);
342 // This test needs to make live DNS requests for access to
343 // GAIA and sync server URLs under google.com. We use a scoped version
344 // to override the default resolver while the test is active.
345 scoped_ptr
<net::ScopedDefaultHostResolverProc
> mock_host_resolver_override_
;
347 // Stores all the WebContents instance in a stack so that we can easily
348 // return to the previous instance.
349 // The active WebContents instance is always stored at the top of the stack.
350 // Initially the stack contains the WebContents instance created by
351 // InProcessBrowserTest as the initial context to run test in.
352 // Whenever a WebContents instance is spawned and needs attention we
353 // push it onto the stack and that becomes the active instance.
354 // And once we are done with the current WebContents instance
355 // we pop it off the stack, returning to the previous instance.
356 std::vector
<content::WebContents
*> web_contents_stack_
;
358 // WebContent of the client page that facilitates communication with
359 // the HTTP server. This is how the remoting browser tests
360 // will get acknowledgments of actions completed on the host.
361 content::WebContents
* client_web_content_
;
363 // WebContent of the landing page in the chromoting app.
364 content::WebContents
* app_web_content_
;
368 const Extension
* extension_
;
369 base::FilePath webapp_crx_
;
370 base::FilePath webapp_unpacked_
;
371 std::string username_
;
372 std::string password_
;
373 std::string me2me_pin_
;
374 std::string remote_host_name_
;
375 std::string extension_name_
;
376 std::string http_server_
;
379 } // namespace remoting
381 #endif // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_