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";
28 const char kHttpServer[] = "http-server";
30 // ASSERT_TRUE can only be used in void returning functions. This version
31 // should be used in non-void-returning functions.
32 inline void _ASSERT_TRUE(bool condition) {
33 ASSERT_TRUE(condition);
39 using extensions::Extension;
43 class RemoteDesktopBrowserTest : public extensions::PlatformAppBrowserTest {
45 RemoteDesktopBrowserTest();
46 virtual ~RemoteDesktopBrowserTest();
48 // InProcessBrowserTest Overrides
49 virtual void SetUp() OVERRIDE;
50 virtual void SetUpOnMainThread() OVERRIDE;
53 // InProcessBrowserTest Overrides
54 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
56 // InProcessBrowserTest Overrides
57 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE;
59 // The following helpers each perform a simple task.
61 // Verify the test has access to the internet (specifically google.com)
62 void VerifyInternetAccess();
64 // Open the client page for the browser test to get status of host actions
65 void OpenClientBrowserPage();
67 // Install the chromoting extension from a crx file.
68 void InstallChromotingAppCrx();
70 // Install the unpacked chromoting extension.
71 void InstallChromotingAppUnpacked();
73 // Uninstall the chromoting extension.
74 void UninstallChromotingApp();
76 // Test whether the chromoting extension is installed.
77 void VerifyChromotingLoaded(bool expected);
79 // Launch the chromoting app.
80 void LaunchChromotingApp();
82 // Authorize: grant extended access permission to the user's computer.
85 // Authenticate: sign in to google using the credentials provided.
88 // Approve: grant the chromoting app necessary permissions.
91 // Click on "Get Started" in the Me2Me section and show the host list.
94 // Disconnect the active Me2Me session.
95 void DisconnectMe2Me();
97 // Simulate a key event.
98 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode, const char* code);
100 void SimulateKeyPressWithCode(ui::KeyboardCode keyCode,
107 // Simulate typing a character
108 void SimulateCharInput(char c);
110 // Simulate typing a string
111 void SimulateStringInput(const std::string& input);
113 // Helper to simulate a left button mouse click.
114 void SimulateMouseLeftClickAt(int x, int y);
116 // Helper to simulate a mouse click.
117 void SimulateMouseClickAt(
118 int modifiers, blink::WebMouseEvent::Button button, int x, int y);
120 // The following helpers each perform a composite task.
122 // Install the chromoting extension
125 // Clean up after the test.
128 // Perform all the auth steps: authorization, authenticattion, etc.
129 // It starts from the chromoting main page unauthenticated and ends up back
130 // on the chromoting main page authenticated and ready to go.
133 // Connect to the local host through Me2Me.
134 void ConnectToLocalHost(bool remember_pin);
136 // Connect to a remote host through Me2Me.
137 void ConnectToRemoteHost(const std::string& host_name, bool remember_pin);
139 // Enter the pin number and connect.
140 void EnterPin(const std::string& name, bool remember_pin);
142 // Helper to get the pin number used for me2me authentication.
143 std::string me2me_pin() { return me2me_pin_; }
145 // Helper to get the name of the remote host to connect to.
146 std::string remote_host_name() { return remote_host_name_; }
148 // Helper to get the test controller URL.
149 std::string http_server() { return http_server_; }
151 // Change behavior of the default host resolver to allow DNS lookup
152 // to proceed instead of being blocked by the test infrastructure.
153 void EnableDNSLookupForThisTest(
154 net::RuleBasedHostResolverProc* host_resolver);
156 // We need to reset the DNS lookup when we finish, or the test will fail.
157 void DisableDNSLookupForThisTest();
159 void ParseCommandLine();
163 // Helper to get the path to the crx file of the webapp to be tested.
164 base::FilePath WebAppCrxPath() { return webapp_crx_; }
166 // Helper to get the extension ID of the installed chromoting webapp.
167 std::string ChromotingID() { return extension_->id(); }
169 // Is this a appsv2 web app?
170 bool is_platform_app() {
171 return extension_->GetType() == extensions::Manifest::TYPE_PLATFORM_APP;
174 // Are we testing an unpacked extension?
176 return !webapp_unpacked_.empty();
179 // The "active" WebContents instance the test needs to interact with.
180 content::WebContents* active_web_contents() {
181 DCHECK(!web_contents_stack_.empty());
182 return web_contents_stack_.back();
185 // The client WebContents instance the test needs to interact with.
186 content::WebContents* client_web_content() {
187 return client_web_content_;
190 content::WebContents* app_web_content() {
191 return app_web_content_;
194 // Whether to perform the cleanup tasks (uninstalling chromoting, etc).
195 // This is useful for diagnostic purposes.
196 bool NoCleanup() { return no_cleanup_; }
198 // Whether to install the chromoting extension before running the test cases.
199 // This is useful for diagnostic purposes.
200 bool NoInstall() { return no_install_; }
202 // Helper to construct the starting URL of the installed chromoting webapp.
203 GURL Chromoting_Main_URL() {
204 return GURL("chrome-extension://" + ChromotingID() + "/main.html");
207 // Helper to retrieve the current URL in the active WebContents.
208 GURL GetCurrentURL() {
209 return active_web_contents()->GetURL();
212 // Helpers to execute JavaScript code on a web page.
214 // Helper to execute a JavaScript code snippet in the active WebContents.
215 void ExecuteScript(const std::string& script);
217 // Helper to execute a JavaScript code snippet in the active WebContents
218 // and wait for page load to complete.
219 void ExecuteScriptAndWaitForAnyPageLoad(const std::string& script);
221 // Helper to execute a JavaScript code snippet in the active WebContents
222 // and extract the boolean result.
223 bool ExecuteScriptAndExtractBool(const std::string& script) {
224 return ExecuteScriptAndExtractBool(active_web_contents(), script);
227 // Helper to execute a JavaScript code snippet and extract the boolean result.
228 static bool ExecuteScriptAndExtractBool(content::WebContents* web_contents,
229 const std::string& script);
231 // Helper to execute a JavaScript code snippet in the active WebContents
232 // and extract the int result.
233 int ExecuteScriptAndExtractInt(const std::string& script) {
234 return ExecuteScriptAndExtractInt(active_web_contents(), script);
237 // Helper to execute a JavaScript code snippet and extract the int result.
238 static int ExecuteScriptAndExtractInt(content::WebContents* web_contents,
239 const std::string& script);
241 // Helper to execute a JavaScript code snippet in the active WebContents
242 // and extract the string result.
243 std::string ExecuteScriptAndExtractString(const std::string& script) {
244 return ExecuteScriptAndExtractString(active_web_contents(), script);
247 // Helper to execute a JavaScript code snippet and extract the string result.
248 static std::string ExecuteScriptAndExtractString(
249 content::WebContents* web_contents, const std::string& script);
251 // Helper to load a JavaScript file from |path| and inject it to
252 // current web_content. The variable |path| is relative to the directory of
253 // the |browsertest| executable.
254 static bool LoadScript(content::WebContents* web_contents,
255 const base::FilePath::StringType& path);
257 // Helper to execute a JavaScript browser test. It creates an object using
258 // the |browserTest.testName| ctor and calls |run| on the created object with
259 // |testData|, which can be any arbitrary object literal. The script
260 // browser_test.js must be loaded (using LoadScript) before calling this
262 void RunJavaScriptTest(content::WebContents* web_contents,
263 const std::string& testName,
264 const std::string& testData);
266 // Helper to check whether an html element with the given name exists in
267 // the active WebContents.
268 bool HtmlElementExists(const std::string& name) {
269 return ExecuteScriptAndExtractBool(
270 "document.getElementById(\"" + name + "\") != null");
273 // Helper to check whether a html element with the given name is visible in
274 // the active WebContents.
275 bool HtmlElementVisible(const std::string& name);
277 // Click on the named HTML control in the active WebContents.
278 void ClickOnControl(const std::string& name);
280 // Wait for the me2me connection to be established.
281 void WaitForConnection();
283 // Checking whether the localHost has been initialized.
284 bool IsLocalHostReady();
286 // Callback used by EnterPin to check whether the pin form is visible
287 // and to dismiss the host-needs-update dialog.
288 bool IsPinFormVisible();
290 // Callback used by WaitForConnection to check whether the connection
291 // has been established.
292 bool IsSessionConnected();
294 // Callback used by Approve to check whether the chromoting app has
295 // successfully authenticated with the Google services.
296 bool IsAuthenticated() {
297 return IsAuthenticatedInWindow(active_web_contents());
300 // If the "Host version out-of-date" form is visible, dismiss it.
301 void DismissHostVersionWarningIfVisible();
303 // Callback used by Approve to check whether the chromoting app has
304 // successfully authenticated with the Google services.
305 static bool IsAuthenticatedInWindow(content::WebContents* web_contents);
307 // Callback used to check whether a host action is completed.
308 // Used by browser tests while conditionally waiting for host actions.
309 static bool IsHostActionComplete(
310 content::WebContents* client_web_content, std::string host_action_var);
315 // This test needs to make live DNS requests for access to
316 // GAIA and sync server URLs under google.com. We use a scoped version
317 // to override the default resolver while the test is active.
318 scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_;
320 // Stores all the WebContents instance in a stack so that we can easily
321 // return to the previous instance.
322 // The active WebContents instance is always stored at the top of the stack.
323 // Initially the stack contains the WebContents instance created by
324 // InProcessBrowserTest as the initial context to run test in.
325 // Whenever a WebContents instance is spawned and needs attention we
326 // push it onto the stack and that becomes the active instance.
327 // And once we are done with the current WebContents instance
328 // we pop it off the stack, returning to the previous instance.
329 std::vector<content::WebContents*> web_contents_stack_;
331 // WebContent of the client page that facilitates communication with
332 // the HTTP server. This is how the remoting browser tests
333 // will get acknowledgments of actions completed on the host.
334 content::WebContents* client_web_content_;
336 // WebContent of the landing page in the chromoting app.
337 content::WebContents* app_web_content_;
341 const Extension* extension_;
342 base::FilePath webapp_crx_;
343 base::FilePath webapp_unpacked_;
344 std::string username_;
345 std::string password_;
346 std::string me2me_pin_;
347 std::string remote_host_name_;
348 std::string extension_name_;
349 std::string http_server_;
352 } // namespace remoting
354 #endif // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_