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 "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"
22 using apps::AppWindow;
23 using apps::AppWindowRegistry;
24 using content::WebContents;
26 namespace utils = extension_function_test_utils;
28 namespace extensions {
30 PlatformAppBrowserTest::PlatformAppBrowserTest() {
31 ChromeShellWindowDelegate::DisableExternalOpenForTesting();
34 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
35 // Skips ExtensionApiTest::SetUpCommandLine.
36 ExtensionBrowserTest::SetUpCommandLine(command_line);
38 // Make event pages get suspended quicker.
39 command_line->AppendSwitchASCII(switches::kEventPageIdleTime, "1000");
40 command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1000");
44 AppWindow* PlatformAppBrowserTest::GetFirstAppWindowForBrowser(
46 AppWindowRegistry* app_registry = AppWindowRegistry::Get(browser->profile());
47 const AppWindowRegistry::AppWindowList& app_windows =
48 app_registry->app_windows();
50 AppWindowRegistry::const_iterator iter = app_windows.begin();
51 if (iter != app_windows.end())
57 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
59 content::WindowedNotificationObserver app_loaded_observer(
60 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
61 content::NotificationService::AllSources());
63 const Extension* extension = LoadExtension(
64 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
65 EXPECT_TRUE(extension);
67 LaunchPlatformApp(extension);
69 app_loaded_observer.Wait();
74 const Extension* PlatformAppBrowserTest::InstallPlatformApp(
76 const Extension* extension = InstallExtension(
77 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
78 EXPECT_TRUE(extension);
83 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
85 content::WindowedNotificationObserver app_loaded_observer(
86 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
87 content::NotificationService::AllSources());
89 const Extension* extension = InstallPlatformApp(name);
91 LaunchPlatformApp(extension);
93 app_loaded_observer.Wait();
98 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension* extension) {
99 OpenApplication(AppLaunchParams(
100 browser()->profile(), extension, LAUNCH_CONTAINER_NONE, NEW_WINDOW));
103 WebContents* PlatformAppBrowserTest::GetFirstAppWindowWebContents() {
104 AppWindow* window = GetFirstAppWindow();
106 return window->web_contents();
111 AppWindow* PlatformAppBrowserTest::GetFirstAppWindow() {
112 return GetFirstAppWindowForBrowser(browser());
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);
122 AppWindowRegistry::const_iterator iter = app_windows.begin();
123 if (iter != app_windows.end())
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(),
137 return result->GetSize();
140 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
142 const Extension* extension) {
143 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
144 function->set_extension(extension);
147 base::StringPrintf("[%u]", window_id),
150 return function->GetResultList() != NULL;
153 size_t PlatformAppBrowserTest::GetAppWindowCount() {
154 return AppWindowRegistry::Get(browser()->profile())->app_windows().size();
157 size_t PlatformAppBrowserTest::GetAppWindowCountForApp(
158 const std::string& app_id) {
159 return AppWindowRegistry::Get(browser()->profile())
160 ->GetAppWindowsForApp(app_id)
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++)
170 command_line->InitFromArgv(argv);
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);
181 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
182 return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
185 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
186 const Extension* extension,
187 const AppWindow::CreateParams& params) {
188 AppWindow* window = new AppWindow(
189 browser()->profile(), new ChromeShellWindowDelegate(), extension);
191 GURL(std::string()), new apps::AppWindowContentsImpl(window), params);
195 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
196 content::WebContentsDestroyedWatcher destroyed_watcher(
197 window->web_contents());
198 window->GetBaseWindow()->Close();
199 destroyed_watcher.Wait();
202 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
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,
209 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
210 cached_screen_bounds,
211 current_screen_bounds,
216 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
217 CommandLine* command_line) {
218 PlatformAppBrowserTest::SetUpCommandLine(command_line);
219 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
222 } // namespace extensions