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/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"
22 using apps::ShellWindow
;
23 using apps::ShellWindowRegistry
;
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 ShellWindow
* PlatformAppBrowserTest::GetFirstShellWindowForBrowser(
46 ShellWindowRegistry
* app_registry
=
47 ShellWindowRegistry::Get(browser
->profile());
48 const ShellWindowRegistry::ShellWindowList
& shell_windows
=
49 app_registry
->shell_windows();
51 ShellWindowRegistry::const_iterator iter
= shell_windows
.begin();
52 if (iter
!= shell_windows
.end())
58 const Extension
* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
60 content::WindowedNotificationObserver
app_loaded_observer(
61 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
62 content::NotificationService::AllSources());
64 const Extension
* extension
= LoadExtension(
65 test_data_dir_
.AppendASCII("platform_apps").AppendASCII(name
));
66 EXPECT_TRUE(extension
);
68 LaunchPlatformApp(extension
);
70 app_loaded_observer
.Wait();
75 const Extension
* PlatformAppBrowserTest::InstallPlatformApp(
77 const Extension
* extension
= InstallExtension(
78 test_data_dir_
.AppendASCII("platform_apps").AppendASCII(name
), 1);
79 EXPECT_TRUE(extension
);
84 const Extension
* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
86 content::WindowedNotificationObserver
app_loaded_observer(
87 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME
,
88 content::NotificationService::AllSources());
90 const Extension
* extension
= InstallPlatformApp(name
);
92 LaunchPlatformApp(extension
);
94 app_loaded_observer
.Wait();
99 void PlatformAppBrowserTest::LaunchPlatformApp(const Extension
* extension
) {
100 OpenApplication(AppLaunchParams(
101 browser()->profile(), extension
, LAUNCH_CONTAINER_NONE
, NEW_WINDOW
));
104 WebContents
* PlatformAppBrowserTest::GetFirstShellWindowWebContents() {
105 ShellWindow
* window
= GetFirstShellWindow();
107 return window
->web_contents();
112 ShellWindow
* PlatformAppBrowserTest::GetFirstShellWindow() {
113 return GetFirstShellWindowForBrowser(browser());
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
);
123 ShellWindowRegistry::const_iterator iter
= shell_windows
.begin();
124 if (iter
!= shell_windows
.end())
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(),
138 return result
->GetSize();
141 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
143 const Extension
* extension
) {
144 scoped_refptr
<WindowsGetFunction
> function
= new WindowsGetFunction();
145 function
->set_extension(extension
);
148 base::StringPrintf("[%u]", window_id
),
151 return function
->GetResultList() != NULL
;
154 size_t PlatformAppBrowserTest::GetShellWindowCount() {
155 return ShellWindowRegistry::Get(browser()->profile())->
156 shell_windows().size();
159 size_t PlatformAppBrowserTest::GetShellWindowCountForApp(
160 const std::string
& app_id
) {
161 return ShellWindowRegistry::Get(browser()->profile())->
162 GetShellWindowsForApp(app_id
).size();
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
++)
171 command_line
->InitFromArgv(argv
);
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
);
182 ShellWindow
* PlatformAppBrowserTest::CreateShellWindow(
183 const Extension
* extension
) {
184 return CreateShellWindowFromParams(extension
, ShellWindow::CreateParams());
187 ShellWindow
* PlatformAppBrowserTest::CreateShellWindowFromParams(
188 const Extension
* extension
, const ShellWindow::CreateParams
& params
) {
189 ShellWindow
* window
= new ShellWindow(browser()->profile(),
190 new ChromeShellWindowDelegate(),
192 window
->Init(GURL(std::string()),
193 new apps::AppWindowContents(window
),
198 void PlatformAppBrowserTest::CloseShellWindow(ShellWindow
* window
) {
199 content::WebContentsDestroyedWatcher
destroyed_watcher(
200 window
->web_contents());
201 window
->GetBaseWindow()->Close();
202 destroyed_watcher
.Wait();
205 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
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
,
212 window
->AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
213 cached_screen_bounds
,
214 current_screen_bounds
,
219 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
220 CommandLine
* command_line
) {
221 PlatformAppBrowserTest::SetUpCommandLine(command_line
);
222 command_line
->AppendSwitch(switches::kEnableExperimentalExtensionApis
);
225 } // namespace extensions