[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest_util.cc
blob169883fb3117e6e73f89d450a022f4886459a1db
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.
5 #include "chrome/browser/apps/app_browsertest_util.h"
7 #include "base/command_line.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/apps/scoped_keep_alive.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
11 #include "chrome/browser/extensions/extension_function_test_utils.h"
12 #include "chrome/browser/ui/apps/chrome_app_delegate.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/extensions/app_launch_params.h"
15 #include "chrome/browser/ui/extensions/application_launch.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/test/browser_test_utils.h"
18 #include "content/public/test/test_utils.h"
19 #include "extensions/browser/app_window/app_window_contents.h"
20 #include "extensions/browser/app_window/app_window_registry.h"
21 #include "extensions/browser/app_window/native_app_window.h"
22 #include "extensions/browser/process_manager.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/switches.h"
25 #include "extensions/test/extension_test_message_listener.h"
27 using content::WebContents;
29 namespace {
31 const char kAppWindowTestApp[] = "app_window/generic";
33 } // namespace
35 namespace utils = extension_function_test_utils;
37 namespace extensions {
39 PlatformAppBrowserTest::PlatformAppBrowserTest() {
40 ChromeAppDelegate::DisableExternalOpenForTesting();
43 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
44 // Skips ExtensionApiTest::SetUpCommandLine.
45 ExtensionBrowserTest::SetUpCommandLine(command_line);
47 // Make event pages get suspended quicker.
48 ProcessManager::SetEventPageIdleTimeForTesting(1000);
49 ProcessManager::SetEventPageSuspendingTimeForTesting(1000);
52 // static
53 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForBrowser(
54 Browser* browser) {
55 AppWindowRegistry* app_registry = AppWindowRegistry::Get(browser->profile());
56 const AppWindowRegistry::AppWindowList& app_windows =
57 app_registry->app_windows();
59 AppWindowRegistry::const_iterator iter = app_windows.begin();
60 if (iter != app_windows.end())
61 return *iter;
63 return NULL;
66 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
67 const char* name,
68 ExtensionTestMessageListener* listener) {
69 DCHECK(listener);
70 const Extension* extension = LoadExtension(
71 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
72 EXPECT_TRUE(extension);
74 LaunchPlatformApp(extension);
76 EXPECT_TRUE(listener->WaitUntilSatisfied()) << "'" << listener->message()
77 << "' message was not receieved";
79 return extension;
82 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
83 const char* name,
84 const std::string& message) {
85 ExtensionTestMessageListener launched_listener(message, false);
86 const Extension* extension =
87 LoadAndLaunchPlatformApp(name, &launched_listener);
89 return extension;
92 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
93 const char* name) {
94 const Extension* extension = InstallExtension(
95 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
96 EXPECT_TRUE(extension);
98 return extension;
101 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
102 const char* name) {
103 content::WindowedNotificationObserver app_loaded_observer(
104 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
105 content::NotificationService::AllSources());
107 const Extension* extension = InstallPlatformApp(name);
109 LaunchPlatformApp(extension);
111 app_loaded_observer.Wait();
113 return extension;
116 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
117 OpenApplication(AppLaunchParams(browser()->profile(), extension,
118 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
119 extensions::SOURCE_UNTRACKED));
122 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
123 AppWindow* window = GetFirstAppWindow();
124 if (window)
125 return window->web_contents();
127 return NULL;
130 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
131 return GetFirstAppWindowForBrowser(browser());
134 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
135 const std::string& app_id) {
136 AppWindowRegistry* app_registry =
137 AppWindowRegistry::Get(browser()->profile());
138 const AppWindowRegistry::AppWindowList& app_windows =
139 app_registry->GetAppWindowsForApp(app_id);
141 AppWindowRegistry::const_iterator iter = app_windows.begin();
142 if (iter != app_windows.end())
143 return *iter;
145 return NULL;
148 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
149 const Extension* extension) {
150 scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
151 function->set_extension(extension);
152 scoped_ptr<base::ListValue> result(utils::ToList(
153 utils::RunFunctionAndReturnSingleResult(function.get(),
154 "[]",
155 browser())));
156 return result->GetSize();
159 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
160 int window_id,
161 const Extension* extension) {
162 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
163 function->set_extension(extension);
164 utils::RunFunction(
165 function.get(),
166 base::StringPrintf("[%u]", window_id),
167 browser(),
168 utils::NONE);
169 return function->GetResultList() != NULL;
172 size_t PlatformAppBrowserTest::GetAppWindowCount() {
173 return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
176 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
177 const std::string& app_id) {
178 return AppWindowRegistry::Get(browser()->profile())
179 ->GetAppWindowsForApp(app_id)
180 .size();
183 void PlatformAppBrowserTest::ClearCommandLineArgs() {
184 CommandLine* command_line = CommandLine::ForCurrentProcess();
185 CommandLine::StringVector args = command_line->GetArgs();
186 CommandLine::StringVector argv = command_line->argv();
187 for (size_t i = 0; i < args.size(); i++)
188 argv.pop_back();
189 command_line->InitFromArgv(argv);
192 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
193 ClearCommandLineArgs();
194 CommandLine* command_line = CommandLine::ForCurrentProcess();
195 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
196 test_doc = test_doc.NormalizePathSeparators();
197 command_line->AppendArgPath(test_doc);
200 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
201 return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
204 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
205 const Extension* extension,
206 const AppWindow::CreateParams& params) {
207 AppWindow* window =
208 new AppWindow(browser()->profile(),
209 new ChromeAppDelegate(make_scoped_ptr(new ScopedKeepAlive)),
210 extension);
211 window->Init(GURL(std::string()), new AppWindowContentsImpl(window), params);
212 return window;
215 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
216 content::WebContentsDestroyedWatcher destroyed_watcher(
217 window->web_contents());
218 window->GetBaseWindow()->Close();
219 destroyed_watcher.Wait();
222 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
223 AppWindow* window,
224 const gfx::Rect& cached_bounds,
225 const gfx::Rect& cached_screen_bounds,
226 const gfx::Rect& current_screen_bounds,
227 const gfx::Size& minimum_size,
228 gfx::Rect* bounds) {
229 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
230 cached_screen_bounds,
231 current_screen_bounds,
232 minimum_size,
233 bounds);
236 AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
237 const std::string& window_create_options) {
238 ExtensionTestMessageListener launched_listener("launched", true);
239 ExtensionTestMessageListener loaded_listener("window_loaded", false);
241 // Load and launch the test app.
242 const Extension* extension =
243 LoadAndLaunchPlatformApp(kAppWindowTestApp, &launched_listener);
244 EXPECT_TRUE(extension);
245 EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
247 // Send the options for window creation.
248 launched_listener.Reply(window_create_options);
250 // Wait for the window to be opened and loaded.
251 EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());
253 EXPECT_EQ(1U, GetAppWindowCount());
254 AppWindow* app_window = GetFirstAppWindow();
255 return app_window;
258 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
259 CommandLine* command_line) {
260 PlatformAppBrowserTest::SetUpCommandLine(command_line);
261 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
264 } // namespace extensions