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