Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / app_browsertest_util.cc
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 #include "chrome/browser/apps/app_browsertest_util.h"
6
7 #include "apps/app_window_contents.h"
8 #include "apps/shell_window_registry.h"
9 #include "apps/ui/native_app_window.h"
10 #include "base/command_line.h"
11 #include "base/strings/stringprintf.h"
12 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
13 #include "chrome/browser/extensions/extension_function_test_utils.h"
14 #include "chrome/browser/ui/apps/chrome_shell_window_delegate.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/extensions/application_launch.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "content/public/test/test_utils.h"
20 #include "extensions/common/switches.h"
21
22 using apps::ShellWindow;
23 using apps::ShellWindowRegistry;
24 using content::WebContents;
25
26 namespace utils = extension_function_test_utils;
27
28 namespace extensions {
29
30 PlatformAppBrowserTest::PlatformAppBrowserTest() {
31   ChromeShellWindowDelegate::DisableExternalOpenForTesting();
32 }
33
34 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
35   // Skips ExtensionApiTest::SetUpCommandLine.
36   ExtensionBrowserTest::SetUpCommandLine(command_line);
37
38   // Make event pages get suspended quicker.
39   command_line->AppendSwitchASCII(switches::kEventPageIdleTime, "1000");
40   command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1000");
41 }
42
43 // static
44 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindowForBrowser(
45     Browser* browser) {
46   ShellWindowRegistry* app_registry =
47       ShellWindowRegistry::Get(browser->profile());
48   const ShellWindowRegistry::ShellWindowList& shell_windows =
49       app_registry->shell_windows();
50
51   ShellWindowRegistry::const_iterator iter = shell_windows.begin();
52   if (iter != shell_windows.end())
53     return *iter;
54
55   return NULL;
56 }
57
58 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
59     const char* name) {
60   content::WindowedNotificationObserver app_loaded_observer(
61       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
62       content::NotificationService::AllSources());
63
64   const Extension* extension = LoadExtension(
65       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
66   EXPECT_TRUE(extension);
67
68   LaunchPlatformApp(extension);
69
70   app_loaded_observer.Wait();
71
72   return extension;
73 }
74
75 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
76     const char* name) {
77   const Extension* extension = InstallExtension(
78       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
79   EXPECT_TRUE(extension);
80
81   return extension;
82 }
83
84 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
85     const char* name) {
86   content::WindowedNotificationObserver app_loaded_observer(
87       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
88       content::NotificationService::AllSources());
89
90   const Extension* extension = InstallPlatformApp(name);
91
92   LaunchPlatformApp(extension);
93
94   app_loaded_observer.Wait();
95
96   return extension;
97 }
98
99 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
100   OpenApplication(AppLaunchParams(
101       browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
102 }
103
104 WebContents* PlatformAppBrowserTest::GetFirstShellWindowWebContents() {
105   ShellWindow* window = GetFirstShellWindow();
106   if (window)
107     return window->web_contents();
108
109   return NULL;
110 }
111
112 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindow() {
113   return GetFirstShellWindowForBrowser(browser());
114 }
115
116 apps::ShellWindow* PlatformAppBrowserTest::GetFirstShellWindowForApp(
117     const std::string& app_id) {
118   ShellWindowRegistry* app_registry =
119       ShellWindowRegistry::Get(browser()->profile());
120   const ShellWindowRegistry::ShellWindowList& shell_windows =
121       app_registry->GetShellWindowsForApp(app_id);
122
123   ShellWindowRegistry::const_iterator iter = shell_windows.begin();
124   if (iter != shell_windows.end())
125     return *iter;
126
127   return NULL;
128 }
129
130 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
131     const Extension* extension) {
132   scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
133   function->set_extension(extension);
134   scoped_ptr<base::ListValue> result(utils::ToList(
135       utils::RunFunctionAndReturnSingleResult(function.get(),
136                                               "[]",
137                                               browser())));
138   return result->GetSize();
139 }
140
141 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
142     int window_id,
143     const Extension* extension) {
144   scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
145   function->set_extension(extension);
146   utils::RunFunction(
147           function.get(),
148           base::StringPrintf("[%u]", window_id),
149           browser(),
150           utils::NONE);
151   return function->GetResultList() != NULL;
152 }
153
154 size_t PlatformAppBrowserTest::GetShellWindowCount() {
155   return ShellWindowRegistry::Get(browser()->profile())->
156       shell_windows().size();
157 }
158
159 size_t PlatformAppBrowserTest::GetShellWindowCountForApp(
160     const std::string& app_id) {
161   return ShellWindowRegistry::Get(browser()->profile())->
162       GetShellWindowsForApp(app_id).size();
163 }
164
165 void PlatformAppBrowserTest::ClearCommandLineArgs() {
166   CommandLine* command_line = CommandLine::ForCurrentProcess();
167   CommandLine::StringVector args = command_line->GetArgs();
168   CommandLine::StringVector argv = command_line->argv();
169   for (size_t i = 0; i < args.size(); i++)
170     argv.pop_back();
171   command_line->InitFromArgv(argv);
172 }
173
174 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
175   ClearCommandLineArgs();
176   CommandLine* command_line = CommandLine::ForCurrentProcess();
177   base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
178   test_doc = test_doc.NormalizePathSeparators();
179   command_line->AppendArgPath(test_doc);
180 }
181
182 ShellWindow* PlatformAppBrowserTest::CreateShellWindow(
183     const Extension* extension) {
184   return CreateShellWindowFromParams(extension, ShellWindow::CreateParams());
185 }
186
187 ShellWindow* PlatformAppBrowserTest::CreateShellWindowFromParams(
188     const Extension* extension, const ShellWindow::CreateParams& params) {
189   ShellWindow* window = new ShellWindow(browser()->profile(),
190                                         new ChromeShellWindowDelegate(),
191                                         extension);
192   window->Init(GURL(std::string()),
193                new apps::AppWindowContents(window),
194                params);
195   return window;
196 }
197
198 void PlatformAppBrowserTest::CloseShellWindow(ShellWindow* window) {
199   content::WebContentsDestroyedWatcher destroyed_watcher(
200       window->web_contents());
201   window->GetBaseWindow()->Close();
202   destroyed_watcher.Wait();
203 }
204
205 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
206     ShellWindow* window,
207     const gfx::Rect& cached_bounds,
208     const gfx::Rect& cached_screen_bounds,
209     const gfx::Rect& current_screen_bounds,
210     const gfx::Size& minimum_size,
211     gfx::Rect* bounds) {
212   window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
213                                           cached_screen_bounds,
214                                           current_screen_bounds,
215                                           minimum_size,
216                                           bounds);
217 }
218
219 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
220     CommandLine* command_line) {
221   PlatformAppBrowserTest::SetUpCommandLine(command_line);
222   command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
223 }
224
225 }  // namespace extensions