Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / apps / app_browsertest_util.cc
blob3cfc59465ea2614220cf723fc7be5e23d77edeba
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 AppWindow* PlatformAppBrowserTest::CreateAppWindow(const Extension* extension) {
199 return CreateAppWindowFromParams(extension, AppWindow::CreateParams());
202 AppWindow* PlatformAppBrowserTest::CreateAppWindowFromParams(
203 const Extension* extension,
204 const AppWindow::CreateParams& params) {
205 AppWindow* window =
206 new AppWindow(browser()->profile(),
207 new ChromeAppDelegate(make_scoped_ptr(new ScopedKeepAlive)),
208 extension);
209 window->Init(GURL(std::string()), new AppWindowContentsImpl(window), params);
210 return window;
213 void PlatformAppBrowserTest::CloseAppWindow(AppWindow* window) {
214 content::WebContentsDestroyedWatcher destroyed_watcher(
215 window->web_contents());
216 window->GetBaseWindow()->Close();
217 destroyed_watcher.Wait();
220 void PlatformAppBrowserTest::CallAdjustBoundsToBeVisibleOnScreenForAppWindow(
221 AppWindow* window,
222 const gfx::Rect& cached_bounds,
223 const gfx::Rect& cached_screen_bounds,
224 const gfx::Rect& current_screen_bounds,
225 const gfx::Size& minimum_size,
226 gfx::Rect* bounds) {
227 window->AdjustBoundsToBeVisibleOnScreen(cached_bounds,
228 cached_screen_bounds,
229 current_screen_bounds,
230 minimum_size,
231 bounds);
234 AppWindow* PlatformAppBrowserTest::CreateTestAppWindow(
235 const std::string& window_create_options) {
236 ExtensionTestMessageListener launched_listener("launched", true);
237 ExtensionTestMessageListener loaded_listener("window_loaded", false);
239 // Load and launch the test app.
240 const Extension* extension =
241 LoadAndLaunchPlatformApp(kAppWindowTestApp, &launched_listener);
242 EXPECT_TRUE(extension);
243 EXPECT_TRUE(launched_listener.WaitUntilSatisfied());
245 // Send the options for window creation.
246 launched_listener.Reply(window_create_options);
248 // Wait for the window to be opened and loaded.
249 EXPECT_TRUE(loaded_listener.WaitUntilSatisfied());
251 EXPECT_EQ(1U, GetAppWindowCount());
252 AppWindow* app_window = GetFirstAppWindow();
253 return app_window;
256 void ExperimentalPlatformAppBrowserTest::SetUpCommandLine(
257 base::CommandLine* command_line) {
258 PlatformAppBrowserTest::SetUpCommandLine(command_line);
259 command_line->AppendSwitch(switches::kEnableExperimentalExtensionApis);
262 } // namespace extensions