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.
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/launch_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "content/public/test/test_utils.h"
21 #include "extensions/browser/api/management/management_api.h"
22 #include "extensions/browser/extension_dialog_auto_confirm.h"
23 #include "extensions/browser/extension_system.h"
24 #include "extensions/browser/test_management_policy.h"
25 #include "extensions/common/manifest.h"
26 #include "extensions/test/extension_test_message_listener.h"
28 using extensions::Extension
;
29 using extensions::Manifest
;
33 // Find a browser other than |browser|.
34 Browser
* FindOtherBrowser(Browser
* browser
) {
35 Browser
* found
= NULL
;
36 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
46 class ExtensionManagementApiTest
: public ExtensionApiTest
{
48 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
49 ExtensionApiTest::SetUpCommandLine(command_line
);
50 command_line
->AppendSwitch(switches::kEnablePanels
);
53 virtual void LoadExtensions() {
54 base::FilePath basedir
= test_data_dir_
.AppendASCII("management");
56 // Load 5 enabled items.
57 LoadNamedExtension(basedir
, "enabled_extension");
58 LoadNamedExtension(basedir
, "enabled_app");
59 LoadNamedExtension(basedir
, "description");
60 LoadNamedExtension(basedir
, "permissions");
61 LoadNamedExtension(basedir
, "short_name");
63 // Load 2 disabled items.
64 LoadNamedExtension(basedir
, "disabled_extension");
65 DisableExtension(extension_ids_
["disabled_extension"]);
66 LoadNamedExtension(basedir
, "disabled_app");
67 DisableExtension(extension_ids_
["disabled_app"]);
70 // Load an app, and wait for a message from app "management/launch_on_install"
71 // indicating that the new app has been launched.
72 void LoadAndWaitForLaunch(const std::string
& app_path
,
73 std::string
* out_app_id
) {
74 ExtensionTestMessageListener
launched_app("launched app", false);
75 ASSERT_TRUE(LoadExtension(test_data_dir_
.AppendASCII(app_path
)));
78 *out_app_id
= last_loaded_extension_id();
80 ASSERT_TRUE(launched_app
.WaitUntilSatisfied());
84 void LoadNamedExtension(const base::FilePath
& path
,
85 const std::string
& name
) {
86 const Extension
* extension
= LoadExtension(path
.AppendASCII(name
));
87 ASSERT_TRUE(extension
);
88 extension_ids_
[name
] = extension
->id();
91 void InstallNamedExtension(const base::FilePath
& path
,
92 const std::string
& name
,
93 Manifest::Location install_source
) {
94 const Extension
* extension
= InstallExtension(path
.AppendASCII(name
), 1,
96 ASSERT_TRUE(extension
);
97 extension_ids_
[name
] = extension
->id();
100 // Maps installed extension names to their IDs.
101 std::map
<std::string
, std::string
> extension_ids_
;
104 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, Basics
) {
107 base::FilePath basedir
= test_data_dir_
.AppendASCII("management");
108 InstallNamedExtension(basedir
, "internal_extension", Manifest::INTERNAL
);
109 InstallNamedExtension(basedir
, "external_extension",
110 Manifest::EXTERNAL_PREF
);
111 InstallNamedExtension(basedir
, "admin_extension",
112 Manifest::EXTERNAL_POLICY_DOWNLOAD
);
114 ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html"));
117 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, NoPermission
) {
119 ASSERT_TRUE(RunExtensionSubtest("management/no_permission", "test.html"));
122 // Disabled: http://crbug.com/174411
124 #define MAYBE_Uninstall DISABLED_Uninstall
126 #define MAYBE_Uninstall Uninstall
129 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, MAYBE_Uninstall
) {
131 // Confirmation dialog will be shown for uninstallations except for self.
132 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
133 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
134 ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html"));
137 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, CreateAppShortcut
) {
139 base::FilePath basedir
= test_data_dir_
.AppendASCII("management");
140 LoadNamedExtension(basedir
, "packaged_app");
142 extensions::ManagementCreateAppShortcutFunction::SetAutoConfirmForTest(true);
143 ASSERT_TRUE(RunExtensionSubtest("management/test",
144 "createAppShortcut.html"));
147 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, GenerateAppForLink
) {
149 ASSERT_TRUE(RunExtensionSubtest("management/test",
150 "generateAppForLink.html"));
153 // Fails often on Windows dbg bots. http://crbug.com/177163
155 #define MAYBE_ManagementPolicyAllowed DISABLED_ManagementPolicyAllowed
157 #define MAYBE_ManagementPolicyAllowed ManagementPolicyAllowed
158 #endif // defined(OS_WIN)
159 // Tests actions on extensions when no management policy is in place.
160 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
,
161 MAYBE_ManagementPolicyAllowed
) {
163 extensions::ScopedTestDialogAutoConfirm
auto_confirm(
164 extensions::ScopedTestDialogAutoConfirm::ACCEPT
);
165 ExtensionService
* service
= extensions::ExtensionSystem::Get(
166 browser()->profile())->extension_service();
167 EXPECT_TRUE(service
->GetExtensionById(extension_ids_
["enabled_extension"],
170 // Ensure that all actions are allowed.
171 extensions::ExtensionSystem::Get(
172 browser()->profile())->management_policy()->UnregisterAllProviders();
174 ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
176 // The last thing the test does is uninstall the "enabled_extension".
177 EXPECT_FALSE(service
->GetExtensionById(extension_ids_
["enabled_extension"],
181 // Fails often on Windows dbg bots. http://crbug.com/177163
183 #define MAYBE_ManagementPolicyProhibited DISABLED_ManagementPolicyProhibited
185 #define MAYBE_ManagementPolicyProhibited ManagementPolicyProhibited
186 #endif // defined(OS_WIN)
187 // Tests actions on extensions when management policy prohibits those actions.
188 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
,
189 MAYBE_ManagementPolicyProhibited
) {
191 ExtensionService
* service
= extensions::ExtensionSystem::Get(
192 browser()->profile())->extension_service();
193 EXPECT_TRUE(service
->GetExtensionById(extension_ids_
["enabled_extension"],
196 // Prohibit status changes.
197 extensions::ManagementPolicy
* policy
= extensions::ExtensionSystem::Get(
198 browser()->profile())->management_policy();
199 policy
->UnregisterAllProviders();
200 extensions::TestManagementPolicyProvider
provider(
201 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
202 policy
->RegisterProvider(&provider
);
203 ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
207 // Disabled. See http://crbug.com/176023
208 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, DISABLED_LaunchPanelApp
) {
209 ExtensionService
* service
= extensions::ExtensionSystem::Get(
210 browser()->profile())->extension_service();
212 // Load an extension that calls launchApp() on any app that gets
214 ExtensionTestMessageListener
launcher_loaded("launcher loaded", false);
215 ASSERT_TRUE(LoadExtension(
216 test_data_dir_
.AppendASCII("management/launch_on_install")));
217 ASSERT_TRUE(launcher_loaded
.WaitUntilSatisfied());
219 // Load an app with app.launch.container = "panel".
221 LoadAndWaitForLaunch("management/launch_app_panel", &app_id
);
222 ASSERT_FALSE(HasFatalFailure()); // Stop the test if any ASSERT failed.
224 // Find the app's browser. Check that it is a popup.
225 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
226 browser()->host_desktop_type()));
227 Browser
* app_browser
= FindOtherBrowser(browser());
228 ASSERT_TRUE(app_browser
->is_type_popup());
229 ASSERT_TRUE(app_browser
->is_app());
231 // Close the app panel.
232 content::WindowedNotificationObserver
signal(
233 chrome::NOTIFICATION_BROWSER_CLOSED
,
234 content::Source
<Browser
>(app_browser
));
236 chrome::CloseWindow(app_browser
);
239 // Unload the extension.
240 UninstallExtension(app_id
);
241 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
242 browser()->host_desktop_type()));
243 ASSERT_FALSE(service
->GetExtensionById(app_id
, true));
245 // Set a pref indicating that the user wants to launch in a regular tab.
246 // This should be ignored, because panel apps always load in a popup.
247 extensions::SetLaunchType(browser()->profile(), app_id
,
248 extensions::LAUNCH_TYPE_REGULAR
);
250 // Load the extension again.
251 std::string app_id_new
;
252 LoadAndWaitForLaunch("management/launch_app_panel", &app_id_new
);
253 ASSERT_FALSE(HasFatalFailure());
255 // If the ID changed, then the pref will not apply to the app.
256 ASSERT_EQ(app_id
, app_id_new
);
258 // Find the app's browser. Apps that should load in a panel ignore
259 // prefs, so we should still see the launch in a popup.
260 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
261 browser()->host_desktop_type()));
262 app_browser
= FindOtherBrowser(browser());
263 ASSERT_TRUE(app_browser
->is_type_popup());
264 ASSERT_TRUE(app_browser
->is_app());
267 // Disabled: http://crbug.com/230165
269 #define MAYBE_LaunchTabApp DISABLED_LaunchTabApp
271 #define MAYBE_LaunchTabApp LaunchTabApp
273 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, MAYBE_LaunchTabApp
) {
274 ExtensionService
* service
= extensions::ExtensionSystem::Get(
275 browser()->profile())->extension_service();
277 // Load an extension that calls launchApp() on any app that gets
279 ExtensionTestMessageListener
launcher_loaded("launcher loaded", false);
280 ASSERT_TRUE(LoadExtension(
281 test_data_dir_
.AppendASCII("management/launch_on_install")));
282 ASSERT_TRUE(launcher_loaded
.WaitUntilSatisfied());
284 // Code below assumes that the test starts with a single browser window
286 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
287 browser()->host_desktop_type()));
288 ASSERT_EQ(1, browser()->tab_strip_model()->count());
290 // Load an app with app.launch.container = "tab".
292 LoadAndWaitForLaunch("management/launch_app_tab", &app_id
);
293 ASSERT_FALSE(HasFatalFailure());
295 // Check that the app opened in a new tab of the existing browser.
296 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
297 browser()->host_desktop_type()));
298 ASSERT_EQ(2, browser()->tab_strip_model()->count());
300 // Unload the extension.
301 UninstallExtension(app_id
);
302 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
303 browser()->host_desktop_type()));
304 ASSERT_FALSE(service
->GetExtensionById(app_id
, true));
306 // Set a pref indicating that the user wants to launch in a window.
307 extensions::SetLaunchType(browser()->profile(), app_id
,
308 extensions::LAUNCH_TYPE_WINDOW
);
310 std::string app_id_new
;
311 LoadAndWaitForLaunch("management/launch_app_tab", &app_id_new
);
312 ASSERT_FALSE(HasFatalFailure());
314 // If the ID changed, then the pref will not apply to the app.
315 ASSERT_EQ(app_id
, app_id_new
);
317 unsigned expected_browser_count
= 2;
318 #if defined(OS_MACOSX)
319 // Without hosted apps being allowed to open in windows, Mac has no way of
320 // making standalone browser windows for apps, so it will add to the
322 EXPECT_TRUE(extensions::util::IsNewBookmarkAppsEnabled());
323 EXPECT_FALSE(extensions::util::CanHostedAppsOpenInWindows());
324 expected_browser_count
= 1;
325 ASSERT_EQ(2, browser()->tab_strip_model()->count());
327 // Find the app's browser. Opening in a new window will create
329 ASSERT_EQ(expected_browser_count
,
330 chrome::GetBrowserCount(browser()->profile(),
331 browser()->host_desktop_type()));
332 if (expected_browser_count
== 2) {
333 Browser
* app_browser
= FindOtherBrowser(browser());
334 ASSERT_TRUE(app_browser
->is_app());
338 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest
, LaunchType
) {
340 base::FilePath basedir
= test_data_dir_
.AppendASCII("management");
341 LoadNamedExtension(basedir
, "packaged_app");
343 ASSERT_TRUE(RunExtensionSubtest("management/test", "launchType.html"));