Disabling NativeViewAcccessibilityWinTest.RetrieveAllAlerts.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest_util.cc
blob712a2c6258898628e5a526420eb3655988a4d941
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(base::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::InstallHostedApp() {
102 const Extension* extension =
103 InstallExtension(test_data_dir_.AppendASCII("hosted_app"), 1);
104 EXPECT_TRUE(extension);
106 return extension;
109 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
110 const char* name) {
111 content::WindowedNotificationObserver app_loaded_observer(
112 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
113 content::NotificationService::AllSources());
115 const Extension* extension = InstallPlatformApp(name);
117 LaunchPlatformApp(extension);
119 app_loaded_observer.Wait();
121 return extension;
124 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
125 OpenApplication(AppLaunchParams(browser()->profile(), extension,
126 LAUNCH_CONTAINER_NONE, NEW_WINDOW,
127 extensions::SOURCE_TEST));
130 void PlatformAppBrowserTest::LaunchHostedApp(const Extension* extension) {
131 AppLaunchParams launch_params(browser()->profile(), extension,
132 NEW_FOREGROUND_TAB,
133 extensions::SOURCE_COMMAND_LINE);
134 OpenApplication(launch_params);
137 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
138 AppWindow* window = GetFirstAppWindow();
139 if (window)
140 return window->web_contents();
142 return NULL;
145 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
146 return GetFirstAppWindowForBrowser(browser());
149 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForApp(
150 const std::string& app_id) {
151 AppWindowRegistry* app_registry =
152 AppWindowRegistry::Get(browser()->profile());
153 const AppWindowRegistry::AppWindowList& app_windows =
154 app_registry->GetAppWindowsForApp(app_id);
156 AppWindowRegistry::const_iterator iter = app_windows.begin();
157 if (iter != app_windows.end())
158 return *iter;
160 return NULL;
163 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
164 const Extension* extension) {
165 scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
166 function->set_extension(extension);
167 scoped_ptr<base::ListValue> result(utils::ToList(
168 utils::RunFunctionAndReturnSingleResult(function.get(),
169 "[]",
170 browser())));
171 return result->GetSize();
174 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
175 int window_id,
176 const Extension* extension) {
177 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
178 function->set_extension(extension);
179 utils::RunFunction(
180 function.get(),
181 base::StringPrintf("[%u]", window_id),
182 browser(),
183 utils::NONE);
184 return function->GetResultList() != NULL;
187 size_t PlatformAppBrowserTest::GetAppWindowCount() {
188 return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
191 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
192 const std::string& app_id) {
193 return AppWindowRegistry::Get(browser()->profile())
194 ->GetAppWindowsForApp(app_id)
195 .size();
198 void PlatformAppBrowserTest::ClearCommandLineArgs() {
199 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
200 base::CommandLine::StringVector args = command_line->GetArgs();
201 base::CommandLine::StringVector argv = command_line->argv();
202 for (size_t i = 0; i < args.size(); i++)
203 argv.pop_back();
204 command_line->InitFromArgv(argv);
207 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
208 ClearCommandLineArgs();
209 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
210 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
211 test_doc = test_doc.NormalizePathSeparators();
212 command_line->AppendArgPath(test_doc);
215 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
216 return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
219 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
220 const Extension* extension,
221 const AppWindow::CreateParams& params) {
222 AppWindow* window =
223 new AppWindow(browser()->profile(),
224 new ChromeAppDelegate(make_scoped_ptr(new ScopedKeepAlive)),
225 extension);
226 window->Init(GURL(std::string()), new AppWindowContentsImpl(window), params);
227 return window;
230 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
231 content::WebContentsDestroyedWatcher destroyed_watcher(
232 window->web_contents());
233 window->GetBaseWindow()->Close();
234 destroyed_watcher.Wait();
237 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
238 AppWindow* window,
239 const gfx::Rect& cached_bounds,
240 const gfx::Rect& cached_screen_bounds,
241 const gfx::Rect& current_screen_bounds,
242 const gfx::Size& minimum_size,
243 gfx::Rect* bounds) {
244 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
245 cached_screen_bounds,
246 current_screen_bounds,
247 minimum_size,
248 bounds);
251 AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
252 const std::string& window_create_options) {
253 ExtensionTestMessageListener launched_listener("launched", true);
254 ExtensionTestMessageListener loaded_listener("window_loaded", false);
256 // Load and launch the test app.
257 const Extension* extension =
258 LoadAndLaunchPlatformApp(kAppWindowTestApp, &launched_listener);
259 EXPECT_TRUE(extension);
260 EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
262 // Send the options for window creation.
263 launched_listener.Reply(window_create_options);
265 // Wait for the window to be opened and loaded.
266 EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());
268 EXPECT_EQ(1U, GetAppWindowCount());
269 AppWindow* app_window = GetFirstAppWindow();
270 return app_window;
273 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
274 base::CommandLine* command_line) {
275 PlatformAppBrowserTest::SetUpCommandLine(command_line);
276 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
279 } // namespace extensions