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