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