Move ShellWindow into apps component.
[chromium-blink-merge.git] / chrome / browser / extensions / platform_app_browsertest_util.cc
blob2d1cdd0fd9b89c320d374b48a52d9768e6164021
1 // Copyright (c) 2012 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/extensions/platform_app_browsertest_util.h"
7 #include "base/command_line.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/api/tabs/tabs_api.h"
10 #include "chrome/browser/extensions/extension_function_test_utils.h"
11 #include "chrome/browser/extensions/shell_window_registry.h"
12 #include "chrome/browser/ui/apps/chrome_shell_window_delegate.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/extensions/application_launch.h"
15 #include "chrome/browser/ui/extensions/native_app_window.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/test_utils.h"
20 using apps::ShellWindow;
21 using content::WebContents;
23 namespace utils = extension_function_test_utils;
25 namespace extensions {
27 PlatformAppBrowserTest::PlatformAppBrowserTest() {
28 chrome::ChromeShellWindowDelegate::DisableExternalOpenForTesting();
31 void PlatformAppBrowserTest::SetUpCommandLine(CommandLine* command_line) {
32 // Skips ExtensionApiTest::SetUpCommandLine.
33 ExtensionBrowserTest::SetUpCommandLine(command_line);
35 // Make event pages get suspended quicker.
36 command_line->AppendSwitchASCII(switches::kEventPageIdleTime, "1");
37 command_line->AppendSwitchASCII(switches::kEventPageSuspendingTime, "1");
40 const Extension* PlatformAppBrowserTest::LoadAndLaunchPlatformApp(
41 const char* name) {
42 content::WindowedNotificationObserver app_loaded_observer(
43 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
44 content::NotificationService::AllSources());
46 const Extension* extension = LoadExtension(
47 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name));
48 EXPECT_TRUE(extension);
50 chrome::OpenApplication(chrome::AppLaunchParams(browser()->profile(),
51 extension,
52 extension_misc::LAUNCH_NONE,
53 NEW_WINDOW));
55 app_loaded_observer.Wait();
57 return extension;
60 const Extension* PlatformAppBrowserTest::InstallAndLaunchPlatformApp(
61 const char* name) {
62 content::WindowedNotificationObserver app_loaded_observer(
63 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
64 content::NotificationService::AllSources());
66 const Extension* extension = InstallExtension(
67 test_data_dir_.AppendASCII("platform_apps").AppendASCII(name), 1);
68 EXPECT_TRUE(extension);
70 chrome::OpenApplication(chrome::AppLaunchParams(browser()->profile(),
71 extension,
72 extension_misc::LAUNCH_NONE,
73 NEW_WINDOW));
75 app_loaded_observer.Wait();
77 return extension;
80 WebContents* PlatformAppBrowserTest::GetFirstShellWindowWebContents() {
81 ShellWindow* window = GetFirstShellWindow();
82 if (window)
83 return window->web_contents();
85 return NULL;
88 ShellWindow* PlatformAppBrowserTest::GetFirstShellWindow() {
89 ShellWindowRegistry* app_registry =
90 ShellWindowRegistry::Get(browser()->profile());
91 const ShellWindowRegistry::ShellWindowList& shell_windows =
92 app_registry->shell_windows();
94 ShellWindowRegistry::const_iterator iter = shell_windows.begin();
95 if (iter != shell_windows.end())
96 return *iter;
98 return NULL;
101 size_t PlatformAppBrowserTest::RunGetWindowsFunctionForExtension(
102 const Extension* extension) {
103 scoped_refptr<WindowsGetAllFunction> function = new WindowsGetAllFunction();
104 function->set_extension(extension);
105 scoped_ptr<base::ListValue> result(utils::ToList(
106 utils::RunFunctionAndReturnSingleResult(function.get(),
107 "[]",
108 browser())));
109 return result->GetSize();
112 bool PlatformAppBrowserTest::RunGetWindowFunctionForExtension(
113 int window_id,
114 const Extension* extension) {
115 scoped_refptr<WindowsGetFunction> function = new WindowsGetFunction();
116 function->set_extension(extension);
117 utils::RunFunction(
118 function.get(),
119 base::StringPrintf("[%u]", window_id),
120 browser(),
121 utils::NONE);
122 return function->GetResultList() != NULL;
125 size_t PlatformAppBrowserTest::GetShellWindowCount() {
126 return ShellWindowRegistry::Get(browser()->profile())->
127 shell_windows().size();
130 void PlatformAppBrowserTest::ClearCommandLineArgs() {
131 CommandLine* command_line = CommandLine::ForCurrentProcess();
132 CommandLine::StringVector args = command_line->GetArgs();
133 CommandLine::StringVector argv = command_line->argv();
134 for (size_t i = 0; i < args.size(); i++)
135 argv.pop_back();
136 command_line->InitFromArgv(argv);
139 void PlatformAppBrowserTest::SetCommandLineArg(const std::string& test_file) {
140 ClearCommandLineArgs();
141 CommandLine* command_line = CommandLine::ForCurrentProcess();
142 base::FilePath test_doc(test_data_dir_.AppendASCII(test_file));
143 test_doc = test_doc.NormalizePathSeparators();
144 command_line->AppendArgPath(test_doc);
147 ShellWindow* PlatformAppBrowserTest::CreateShellWindow(
148 const Extension* extension) {
149 ShellWindow::CreateParams params;
150 return ShellWindow::Create(
151 browser()->profile(), new chrome::ChromeShellWindowDelegate(),
152 extension, GURL(std::string()), params);
155 ShellWindow* PlatformAppBrowserTest::CreateShellWindowFromParams(
156 const Extension* extension, const ShellWindow::CreateParams& params) {
157 return ShellWindow::Create(
158 browser()->profile(), new chrome::ChromeShellWindowDelegate(),
159 extension, GURL(std::string()), params);
162 void PlatformAppBrowserTest::CloseShellWindow(ShellWindow* window) {
163 content::WindowedNotificationObserver destroyed_observer(
164 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
165 content::NotificationService::AllSources());
166 window->GetBaseWindow()->Close();
167 destroyed_observer.Wait();
170 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForShellWindow(
171 ShellWindow* window,
172 const gfx::Rect& cached_bounds,
173 const gfx::Rect& cached_screen_bounds,
174 const gfx::Rect& current_screen_bounds,
175 const gfx::Size& minimum_size,
176 gfx::Rect* bounds) {
177 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
178 cached_screen_bounds,
179 current_screen_bounds,
180 minimum_size,
181 bounds);
184 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
185 CommandLine* command_line) {
186 PlatformAppBrowserTest::SetUpCommandLine(command_line);
187 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
190 } // namespace extensions