Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / test / remoting / remote_desktop_browsertest.h
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
16 namespace {
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";
29
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);
34   return;
35 }
36
37 }  // namespace
38
39 using extensions::Extension;
40
41 namespace remoting {
42
43 class 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   content::WebContents* app_web_content() {
191     return app_web_content_;
192   }
193
194   // Whether to perform the cleanup tasks (uninstalling chromoting, etc).
195   // This is useful for diagnostic purposes.
196   bool NoCleanup() { return no_cleanup_; }
197
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_; }
201
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");
205   }
206
207   // Helper to retrieve the current URL in the active WebContents.
208   GURL GetCurrentURL() {
209     return active_web_contents()->GetURL();
210   }
211
212   // Helpers to execute JavaScript code on a web page.
213
214   // Helper to execute a JavaScript code snippet in the active WebContents.
215   void ExecuteScript(const std::string& script);
216
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);
220
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);
225   }
226
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);
230
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);
235   }
236
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);
240
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);
245   }
246
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);
250
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);
256
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
261   // function.
262   void RunJavaScriptTest(content::WebContents* web_contents,
263                          const std::string& testName,
264                          const std::string& testData);
265
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");
271   }
272
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);
276
277   // Click on the named HTML control in the active WebContents.
278   void ClickOnControl(const std::string& name);
279
280   // Wait for the me2me connection to be established.
281   void WaitForConnection();
282
283   // Checking whether the localHost has been initialized.
284   bool IsLocalHostReady();
285
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();
289
290   // Callback used by WaitForConnection to check whether the connection
291   // has been established.
292   bool IsSessionConnected();
293
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());
298   }
299
300   // If the "Host version out-of-date" form is visible, dismiss it.
301   void DismissHostVersionWarningIfVisible();
302
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);
306
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);
311
312  private:
313   // Fields
314
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_;
319
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_;
330
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_;
335
336   // WebContent of the landing page in the chromoting app.
337   content::WebContents* app_web_content_;
338
339   bool no_cleanup_;
340   bool no_install_;
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_;
350 };
351
352 }  // namespace remoting
353
354 #endif  // CHROME_TEST_REMOTING_REMOTE_DESKTOP_BROWSERTEST_H_