Upstream version 5.34.104.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/app_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::AppWindow;
23 using apps::AppWindowRegistry;
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 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForBrowser(
45     Browser* browser) {
46   AppWindowRegistry* app_registry = AppWindowRegistry::Get(browser->profile());
47   const AppWindowRegistry::AppWindowList& app_windows =
48       app_registry->app_windows();
49
50   AppWindowRegistry::const_iterator iter = app_windows.begin();
51   if (iter != app_windows.end())
52     return *iter;
53
54   return NULL;
55 }
56
57 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
58     const char* name) {
59   content::WindowedNotificationObserver app_loaded_observer(
60       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
61       content::NotificationService::AllSources());
62
63   const Extension* extension = LoadExtension(
64       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
65   EXPECT_TRUE(extension);
66
67   LaunchPlatformApp(extension);
68
69   app_loaded_observer.Wait();
70
71   return extension;
72 }
73
74 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
75     const char* name) {
76   const Extension* extension = InstallExtension(
77       test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
78   EXPECT_TRUE(extension);
79
80   return extension;
81 }
82
83 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
84     const char* name) {
85   content::WindowedNotificationObserver app_loaded_observer(
86       content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
87       content::NotificationService::AllSources());
88
89   const Extension* extension = InstallPlatformApp(name);
90
91   LaunchPlatformApp(extension);
92
93   app_loaded_observer.Wait();
94
95   return extension;
96 }
97
98 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
99   OpenApplication(AppLaunchParams(
100       browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
101 }
102
103 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
104   AppWindow* window = GetFirstAppWindow();
105   if (window)
106     return window->web_contents();
107
108   return NULL;
109 }
110
111 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
112   return GetFirstAppWindowForBrowser(browser());
113 }
114
115 apps::AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
116     const std::string& app_id) {
117   AppWindowRegistry* app_registry =
118       AppWindowRegistry::Get(browser()->profile());
119   const AppWindowRegistry::AppWindowList& app_windows =
120       app_registry->GetAppWindowsForApp(app_id);
121
122   AppWindowRegistry::const_iterator iter = app_windows.begin();
123   if (iter != app_windows.end())
124     return *iter;
125
126   return NULL;
127 }
128
129 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
130     const Extension* extension) {
131   scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
132   function->set_extension(extension);
133   scoped_ptr<base::ListValue> result(utils::ToList(
134       utils::RunFunctionAndReturnSingleResult(function.get(),
135                                               "[]",
136                                               browser())));
137   return result->GetSize();
138 }
139
140 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
141     int window_id,
142     const Extension* extension) {
143   scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
144   function->set_extension(extension);
145   utils::RunFunction(
146           function.get(),
147           base::StringPrintf("[%u]", window_id),
148           browser(),
149           utils::NONE);
150   return function->GetResultList() != NULL;
151 }
152
153 size_t PlatformAppBrowserTest::GetAppWindowCount() {
154   return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
155 }
156
157 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
158     const std::string& app_id) {
159   return AppWindowRegistry::Get(browser()->profile())
160       ->GetAppWindowsForApp(app_id)
161       .size();
162 }
163
164 void PlatformAppBrowserTest::ClearCommandLineArgs() {
165   CommandLine* command_line = CommandLine::ForCurrentProcess();
166   CommandLine::StringVector args = command_line->GetArgs();
167   CommandLine::StringVector argv = command_line->argv();
168   for (size_t i = 0; i < args.size(); i++)
169     argv.pop_back();
170   command_line->InitFromArgv(argv);
171 }
172
173 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
174   ClearCommandLineArgs();
175   CommandLine* command_line = CommandLine::ForCurrentProcess();
176   base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
177   test_doc = test_doc.NormalizePathSeparators();
178   command_line->AppendArgPath(test_doc);
179 }
180
181 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
182   return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
183 }
184
185 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
186     const Extension* extension,
187     const AppWindow::CreateParams& params) {
188   AppWindow* window = new AppWindow(
189       browser()->profile(), new ChromeShellWindowDelegate(), extension);
190   window->Init(
191       GURL(std::string()), new apps::AppWindowContentsImpl(window), params);
192   return window;
193 }
194
195 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
196   content::WebContentsDestroyedWatcher destroyed_watcher(
197       window->web_contents());
198   window->GetBaseWindow()->Close();
199   destroyed_watcher.Wait();
200 }
201
202 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
203     AppWindow* window,
204     const gfx::Rect& cached_bounds,
205     const gfx::Rect& cached_screen_bounds,
206     const gfx::Rect& current_screen_bounds,
207     const gfx::Size& minimum_size,
208     gfx::Rect* bounds) {
209   window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
210                                           cached_screen_bounds,
211                                           current_screen_bounds,
212                                           minimum_size,
213                                           bounds);
214 }
215
216 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
217     CommandLine* command_line) {
218   PlatformAppBrowserTest::SetUpCommandLine(command_line);
219   command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
220 }
221
222 }  // namespace extensions