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 "chrome/test/remoting/remote_test_helper.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "net/dns/mock_host_resolver.h"
19 // Command line arguments specific to the chromoting browser tests.
20 const char kOverrideUserDataDir
[] = "override-user-data-dir";
21 const char kNoCleanup
[] = "no-cleanup";
22 const char kNoInstall
[] = "no-install";
23 const char kWebAppCrx
[] = "webapp-crx";
24 const char kWebAppUnpacked
[] = "webapp-unpacked";
25 const char kUserName
[] = "username";
26 const char kUserPassword
[] = "password";
27 const char kAccountsFile
[] = "accounts-file";
28 const char kAccountType
[] = "account-type";
29 const char kMe2MePin
[] = "me2me-pin";
30 const char kRemoteHostName
[] = "remote-host-name";
31 const char kExtensionName
[] = "extension-name";
32 const char kHttpServer
[] = "http-server";
36 using extensions::Extension
;
40 class RemoteDesktopBrowserTest
: public extensions::PlatformAppBrowserTest
{
42 RemoteDesktopBrowserTest();
43 ~RemoteDesktopBrowserTest() override
;
45 // InProcessBrowserTest Overrides
46 void SetUp() override
;
47 void SetUpOnMainThread() override
;
50 // InProcessBrowserTest Overrides
51 void SetUpInProcessBrowserTestFixture() override
;
53 // InProcessBrowserTest Overrides
54 void TearDownInProcessBrowserTestFixture() override
;
56 // The following helpers each perform a simple task.
58 // Verify the test has access to the internet (specifically google.com)
59 void VerifyInternetAccess();
61 // Open the client page for the browser test to get status of host actions
62 void OpenClientBrowserPage();
64 // Install the chromoting extension from a crx file.
65 void InstallChromotingAppCrx();
67 // Install the unpacked chromoting extension.
68 void InstallChromotingAppUnpacked();
70 // Uninstall the chromoting extension.
71 void UninstallChromotingApp();
73 // Test whether the chromoting extension is installed.
74 void VerifyChromotingLoaded(bool expected
);
76 // Launch the Chromoting app. If |defer_start| is true, an additional URL
77 // parameter is passed to the application, causing it to defer start-up
78 // until StartChromotingApp is invoked. Test code can execute arbitrary
79 // JavaScript in the context of the app between these two calls, for example
80 // to set up appropriate mocks.
81 // |window_open_disposition| controls where the app will be launched. For v2
82 // app, the value of |window_open_disposition| will always be NEW_WINDOW.
83 // Returns the content::Webconetns of the launched app. The lifetime of the
84 // returned value is managed by LaunchChromotingApp().
85 content::WebContents
* LaunchChromotingApp(bool defer_start
);
86 content::WebContents
* LaunchChromotingApp(
88 WindowOpenDisposition window_open_disposition
);
90 // If the Chromoting app was launched in deferred mode, tell it to continue
91 // its regular start-up sequence.
92 void StartChromotingApp();
94 // Authorize: grant extended access permission to the user's computer.
97 // Authenticate: sign in to google using the credentials provided.
100 // Approve: grant the chromoting app necessary permissions.
103 // Click on "Get Started" in the Me2Me section and show the host list.
106 // Disconnect the active Me2Me session.
107 void DisconnectMe2Me();
109 // Simulate a key event.
110 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
, const char* code
);
112 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode
,
119 // Simulate typing a character
120 void SimulateCharInput(char c
);
122 // Simulate typing a string
123 void SimulateStringInput(const std::string
& input
);
125 // Helper to simulate a left button mouse click.
126 void SimulateMouseLeftClickAt(int x
, int y
);
128 // Helper to simulate a mouse click.
129 void SimulateMouseClickAt(
130 int modifiers
, blink::WebMouseEvent::Button button
, int x
, int y
);
132 void SetUserNameAndPassword(
133 const base::FilePath
&accounts_file
, const std::string
& account_type
);
135 // The following helpers each perform a composite task.
137 // Install the chromoting extension
140 // Load the browser-test support JavaScript files, including helper functions
142 void LoadBrowserTestJavaScript(content::WebContents
* content
);
144 // Perform all necessary steps (installation, authorization, authentication,
145 // expanding the me2me section) so that the app is ready for a connection.
146 // Returns the content::WebContents instance of the Chromoting app.
147 content::WebContents
* SetUpTest();
149 // Clean up after the test.
152 // Perform all the auth steps: authorization, authentication, etc.
153 // It starts from the chromoting main page unauthenticated and ends up back
154 // on the chromoting main page authenticated and ready to go.
157 // Ensures that the host is started locally with |me2me_pin()|.
158 // Browser_test.js must be loaded before calling this function.
159 void EnsureRemoteConnectionEnabled(content::WebContents
* app_web_content
);
161 // Connect to the local host through Me2Me.
162 void ConnectToLocalHost(bool remember_pin
);
164 // Connect to a remote host through Me2Me.
165 void ConnectToRemoteHost(const std::string
& host_name
, bool remember_pin
);
167 // Enter the pin number and connect.
168 void EnterPin(const std::string
& name
, bool remember_pin
);
170 // Helper to get the pin number used for me2me authentication.
171 std::string
me2me_pin() { return me2me_pin_
; }
173 // Helper to get the name of the remote host to connect to.
174 std::string
remote_host_name() { return remote_host_name_
; }
176 // Helper to get the test controller URL.
177 std::string
http_server() { return http_server_
; }
179 // Change behavior of the default host resolver to allow DNS lookup
180 // to proceed instead of being blocked by the test infrastructure.
181 void EnableDNSLookupForThisTest(
182 net::RuleBasedHostResolverProc
* host_resolver
);
184 // We need to reset the DNS lookup when we finish, or the test will fail.
185 void DisableDNSLookupForThisTest();
187 void ParseCommandLine();
191 // Helper to get the path to the crx file of the webapp to be tested.
192 base::FilePath
WebAppCrxPath() { return webapp_crx_
; }
194 // Helper to get the extension ID of the installed chromoting webapp.
195 std::string
ChromotingID() { return extension_
->id(); }
197 // Is this a appsv2 web app?
198 bool is_platform_app() {
199 return extension_
->GetType() == extensions::Manifest::TYPE_PLATFORM_APP
;
202 // Are we testing an unpacked extension?
204 return !webapp_unpacked_
.empty();
207 // The "active" WebContents instance the test needs to interact with.
208 content::WebContents
* active_web_contents() {
209 DCHECK(!web_contents_stack_
.empty());
210 return web_contents_stack_
.back();
213 // The client WebContents instance the test needs to interact with.
214 content::WebContents
* client_web_content() {
215 return client_web_content_
;
218 RemoteTestHelper
* remote_test_helper() const {
219 return remote_test_helper_
.get();
222 // Whether to perform the cleanup tasks (uninstalling chromoting, etc).
223 // This is useful for diagnostic purposes.
224 bool NoCleanup() { return no_cleanup_
; }
226 // Whether to install the chromoting extension before running the test cases.
227 // This is useful for diagnostic purposes.
228 bool NoInstall() { return no_install_
; }
230 // Helper to construct the starting URL of the installed chromoting webapp.
231 GURL
Chromoting_Main_URL() {
232 return GURL("chrome-extension://" + ChromotingID() + "/main.html");
235 // Helper to retrieve the current URL in the active WebContents. This function
236 // strips all query parameters from the URL.
237 GURL
GetCurrentURL() {
238 GURL current_url
= active_web_contents()->GetURL();
239 GURL::Replacements strip_query
;
240 strip_query
.ClearQuery();
241 return current_url
.ReplaceComponents(strip_query
);
244 // Helpers to execute JavaScript code on a web page.
246 // Helper to execute a JavaScript code snippet in the active WebContents.
247 void ExecuteScript(const std::string
& script
);
249 // Helper to execute a JavaScript code snippet in the active WebContents
250 // and wait for page load to complete.
251 void ExecuteScriptAndWaitForAnyPageLoad(const std::string
& script
);
253 // Helper to execute a JavaScript code snippet in the active WebContents
254 // and extract the boolean result.
255 bool ExecuteScriptAndExtractBool(const std::string
& script
) {
256 return RemoteTestHelper::ExecuteScriptAndExtractBool(
257 active_web_contents(), script
);
260 // Helper to execute a JavaScript code snippet in the active WebContents
261 // and extract the int result.
262 int ExecuteScriptAndExtractInt(const std::string
& script
) {
263 return RemoteTestHelper::ExecuteScriptAndExtractInt(
264 active_web_contents(), script
);
267 // Helper to execute a JavaScript code snippet in the active WebContents
268 // and extract the string result.
269 std::string
ExecuteScriptAndExtractString(const std::string
& script
) {
270 return RemoteTestHelper::ExecuteScriptAndExtractString(
271 active_web_contents(), script
);
274 // Helper to load a JavaScript file from |path| and inject it to
275 // current web_content. The variable |path| is relative to the directory of
276 // the |browsertest| executable.
277 static bool LoadScript(content::WebContents
* web_contents
,
278 const base::FilePath::StringType
& path
);
280 // Helper to execute a JavaScript browser test. It creates an object using
281 // the |browserTest.testName| ctor and calls |run| on the created object with
282 // |testData|, which can be any arbitrary object literal. The script
283 // browser_test.js must be loaded (using LoadScript) before calling this
285 void RunJavaScriptTest(content::WebContents
* web_contents
,
286 const std::string
& testName
,
287 const std::string
& testData
);
289 // Helper to check whether an html element with the given name exists in
290 // the active WebContents.
291 bool HtmlElementExists(const std::string
& name
) {
292 return ExecuteScriptAndExtractBool(
294 "if (document.getElementById('" + name
+ "')) {"
297 "console.log('body.innerHTML:=' + document.body.innerHTML);"
298 "console.log('window.location:= ' + window.location);"
303 // Helper to check whether a html element with the given name is visible in
304 // the active WebContents.
305 bool HtmlElementVisible(const std::string
& name
);
307 // Click on the named HTML control in the active WebContents.
308 void ClickOnControl(const std::string
& name
);
310 // Wait for the me2me connection to be established.
311 void WaitForConnection();
313 // Checking whether the localHost has been initialized.
314 bool IsLocalHostReady();
316 // Checking whether the hosts list has been initialized.
317 bool IsHostListReady();
319 // Callback used by EnterPin to check whether the pin form is visible
320 // and to dismiss the host-needs-update dialog.
321 bool IsPinFormVisible();
323 // Callback used by WaitForConnection to check whether the connection
324 // has been established.
325 bool IsSessionConnected();
327 // Callback used by Approve to check whether the chromoting app has
328 // successfully authenticated with the Google services.
329 bool IsAuthenticated() {
330 return IsAuthenticatedInWindow(active_web_contents());
333 // Callback used by Approve to check whether the Accept button is enabled
334 // and ready to receive a click.
335 static bool IsEnabled(
336 content::WebContents
* web_contents
, const std::string
& name
);
338 // If the "Host version out-of-date" form is visible, dismiss it.
339 void DismissHostVersionWarningIfVisible();
341 // Callback used by Approve to check whether the chromoting app has
342 // successfully authenticated with the Google services.
343 static bool IsAuthenticatedInWindow(content::WebContents
* web_contents
);
345 // Callback used to check whether a host action is completed.
346 // Used by browser tests while conditionally waiting for host actions.
347 static bool IsHostActionComplete(
348 content::WebContents
* client_web_content
, std::string host_action_var
);
350 // Test if the remoting app mode is equal to the given mode
351 bool IsAppModeEqualTo(const std::string
& mode
);
353 // Disable remote connection while the remote connection is enabled
354 void DisableRemoteConnection();
359 // This test needs to make live DNS requests for access to
360 // GAIA and sync server URLs under google.com. We use a scoped version
361 // to override the default resolver while the test is active.
362 scoped_ptr
<net::ScopedDefaultHostResolverProc
> mock_host_resolver_override_
;
364 // Stores all the WebContents instance in a stack so that we can easily
365 // return to the previous instance.
366 // The active WebContents instance is always stored at the top of the stack.
367 // Initially the stack contains the WebContents instance created by
368 // InProcessBrowserTest as the initial context to run test in.
369 // Whenever a WebContents instance is spawned and needs attention we
370 // push it onto the stack and that becomes the active instance.
371 // And once we are done with the current WebContents instance
372 // we pop it off the stack, returning to the previous instance.
373 std::vector
<content::WebContents
*> web_contents_stack_
;
375 // WebContent of the client page that facilitates communication with
376 // the HTTP server. This is how the remoting browser tests
377 // will get acknowledgments of actions completed on the host.
378 content::WebContents
* client_web_content_
;
380 // Helper class to assist in performing and verifying remote operations.
381 scoped_ptr
<RemoteTestHelper
> remote_test_helper_
;
385 const Extension
* extension_
;
386 base::FilePath webapp_crx_
;
387 base::FilePath webapp_unpacked_
;
388 std::string username_
;
389 std::string password_
;
390 std::string me2me_pin_
;
391 std::string remote_host_name_
;
392 std::string extension_name_
;
393 std::string http_server_
;
396 } // namespace remoting
398 #endif // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_