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