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/api/management/management_api.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/extensions/launch_util.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/browser_finder.h"
17 #include "chrome/browser/ui/browser_iterator.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "content/public/test/test_utils.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/test_management_policy.h"
24 #include "extensions/common/manifest.h"
26 using extensions::Extension;
27 using extensions::Manifest;
31 // Find a browser other than |browser|.
32 Browser* FindOtherBrowser(Browser* browser) {
33 Browser* found = NULL;
34 for (chrome::BrowserIterator it; !it.done(); it.Next()) {
44 class ExtensionManagementApiTest : public ExtensionApiTest {
46 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
47 ExtensionApiTest::SetUpCommandLine(command_line);
48 command_line->AppendSwitch(switches::kEnablePanels);
51 virtual void LoadExtensions() {
52 base::FilePath basedir = test_data_dir_.AppendASCII("management");
54 // Load 5 enabled items.
55 LoadNamedExtension(basedir, "enabled_extension");
56 LoadNamedExtension(basedir, "enabled_app");
57 LoadNamedExtension(basedir, "description");
58 LoadNamedExtension(basedir, "permissions");
59 LoadNamedExtension(basedir, "short_name");
61 // Load 2 disabled items.
62 LoadNamedExtension(basedir, "disabled_extension");
63 DisableExtension(extension_ids_["disabled_extension"]);
64 LoadNamedExtension(basedir, "disabled_app");
65 DisableExtension(extension_ids_["disabled_app"]);
68 // Load an app, and wait for a message from app "management/launch_on_install"
69 // indicating that the new app has been launched.
70 void LoadAndWaitForLaunch(const std::string& app_path,
71 std::string* out_app_id) {
72 ExtensionTestMessageListener launched_app("launched app", false);
73 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_path)));
76 *out_app_id = last_loaded_extension_id();
78 ASSERT_TRUE(launched_app.WaitUntilSatisfied());
82 void LoadNamedExtension(const base::FilePath& path,
83 const std::string& name) {
84 const Extension* extension = LoadExtension(path.AppendASCII(name));
85 ASSERT_TRUE(extension);
86 extension_ids_[name] = extension->id();
89 void InstallNamedExtension(const base::FilePath& path,
90 const std::string& name,
91 Manifest::Location install_source) {
92 const Extension* extension = InstallExtension(path.AppendASCII(name), 1,
94 ASSERT_TRUE(extension);
95 extension_ids_[name] = extension->id();
98 // Maps installed extension names to their IDs.
99 std::map<std::string, std::string> extension_ids_;
102 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) {
105 base::FilePath basedir = test_data_dir_.AppendASCII("management");
106 InstallNamedExtension(basedir, "internal_extension", Manifest::INTERNAL);
107 InstallNamedExtension(basedir, "external_extension",
108 Manifest::EXTERNAL_PREF);
109 InstallNamedExtension(basedir, "admin_extension",
110 Manifest::EXTERNAL_POLICY_DOWNLOAD);
112 ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html"));
115 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, NoPermission) {
117 ASSERT_TRUE(RunExtensionSubtest("management/no_permission", "test.html"));
120 // Disabled: http://crbug.com/174411
122 #define MAYBE_Uninstall DISABLED_Uninstall
124 #define MAYBE_Uninstall Uninstall
127 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, MAYBE_Uninstall) {
129 // Confirmation dialog will be shown for uninstallations except for self.
130 extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true);
131 ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html"));
134 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, CreateAppShortcut) {
136 base::FilePath basedir = test_data_dir_.AppendASCII("management");
137 LoadNamedExtension(basedir, "packaged_app");
139 extensions::ManagementCreateAppShortcutFunction::SetAutoConfirmForTest(true);
140 ASSERT_TRUE(RunExtensionSubtest("management/test",
141 "createAppShortcut.html"));
144 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest,
145 CreateAppShortcutNotInStable) {
146 extensions::ScopedCurrentChannel channel(
147 chrome::VersionInfo::CHANNEL_STABLE);
148 ASSERT_TRUE(RunExtensionSubtest("management/test",
149 "createAppShortcutNotInStable.html"));
152 // Fails often on Windows dbg bots. http://crbug.com/177163
154 #define MAYBE_ManagementPolicyAllowed DISABLED_ManagementPolicyAllowed
156 #define MAYBE_ManagementPolicyAllowed ManagementPolicyAllowed
157 #endif // defined(OS_WIN)
158 // Tests actions on extensions when no management policy is in place.
159 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest,
160 MAYBE_ManagementPolicyAllowed) {
162 extensions::ManagementUninstallFunction::SetAutoConfirmForTest(true);
163 ExtensionService* service = extensions::ExtensionSystem::Get(
164 browser()->profile())->extension_service();
165 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"],
168 // Ensure that all actions are allowed.
169 extensions::ExtensionSystem::Get(
170 browser()->profile())->management_policy()->UnregisterAllProviders();
172 ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
174 // The last thing the test does is uninstall the "enabled_extension".
175 EXPECT_FALSE(service->GetExtensionById(extension_ids_["enabled_extension"],
179 // Fails often on Windows dbg bots. http://crbug.com/177163
181 #define MAYBE_ManagementPolicyProhibited DISABLED_ManagementPolicyProhibited
183 #define MAYBE_ManagementPolicyProhibited ManagementPolicyProhibited
184 #endif // defined(OS_WIN)
185 // Tests actions on extensions when management policy prohibits those actions.
186 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest,
187 MAYBE_ManagementPolicyProhibited) {
189 ExtensionService* service = extensions::ExtensionSystem::Get(
190 browser()->profile())->extension_service();
191 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"],
194 // Prohibit status changes.
195 extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get(
196 browser()->profile())->management_policy();
197 policy->UnregisterAllProviders();
198 extensions::TestManagementPolicyProvider provider(
199 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS);
200 policy->RegisterProvider(&provider);
201 ASSERT_TRUE(RunExtensionSubtest("management/management_policy",
205 // Disabled. See http://crbug.com/176023
206 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, DISABLED_LaunchPanelApp) {
207 ExtensionService* service = extensions::ExtensionSystem::Get(
208 browser()->profile())->extension_service();
210 // Load an extension that calls launchApp() on any app that gets
212 ExtensionTestMessageListener launcher_loaded("launcher loaded", false);
213 ASSERT_TRUE(LoadExtension(
214 test_data_dir_.AppendASCII("management/launch_on_install")));
215 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied());
217 // Load an app with app.launch.container = "panel".
219 LoadAndWaitForLaunch("management/launch_app_panel", &app_id);
220 ASSERT_FALSE(HasFatalFailure()); // Stop the test if any ASSERT failed.
222 // Find the app's browser. Check that it is a popup.
223 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
224 browser()->host_desktop_type()));
225 Browser* app_browser = FindOtherBrowser(browser());
226 ASSERT_TRUE(app_browser->is_type_popup());
227 ASSERT_TRUE(app_browser->is_app());
229 // Close the app panel.
230 content::WindowedNotificationObserver signal(
231 chrome::NOTIFICATION_BROWSER_CLOSED,
232 content::Source<Browser>(app_browser));
234 chrome::CloseWindow(app_browser);
237 // Unload the extension.
238 UninstallExtension(app_id);
239 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
240 browser()->host_desktop_type()));
241 ASSERT_FALSE(service->GetExtensionById(app_id, true));
243 // Set a pref indicating that the user wants to launch in a regular tab.
244 // This should be ignored, because panel apps always load in a popup.
245 extensions::SetLaunchType(service, app_id, extensions::LAUNCH_TYPE_REGULAR);
247 // Load the extension again.
248 std::string app_id_new;
249 LoadAndWaitForLaunch("management/launch_app_panel", &app_id_new);
250 ASSERT_FALSE(HasFatalFailure());
252 // If the ID changed, then the pref will not apply to the app.
253 ASSERT_EQ(app_id, app_id_new);
255 // Find the app's browser. Apps that should load in a panel ignore
256 // prefs, so we should still see the launch in a popup.
257 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
258 browser()->host_desktop_type()));
259 app_browser = FindOtherBrowser(browser());
260 ASSERT_TRUE(app_browser->is_type_popup());
261 ASSERT_TRUE(app_browser->is_app());
264 // Disabled: http://crbug.com/230165
266 #define MAYBE_LaunchTabApp DISABLED_LaunchTabApp
268 #define MAYBE_LaunchTabApp LaunchTabApp
270 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, MAYBE_LaunchTabApp) {
271 ExtensionService* service = extensions::ExtensionSystem::Get(
272 browser()->profile())->extension_service();
274 // Load an extension that calls launchApp() on any app that gets
276 ExtensionTestMessageListener launcher_loaded("launcher loaded", false);
277 ASSERT_TRUE(LoadExtension(
278 test_data_dir_.AppendASCII("management/launch_on_install")));
279 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied());
281 // Code below assumes that the test starts with a single browser window
283 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
284 browser()->host_desktop_type()));
285 ASSERT_EQ(1, browser()->tab_strip_model()->count());
287 // Load an app with app.launch.container = "tab".
289 LoadAndWaitForLaunch("management/launch_app_tab", &app_id);
290 ASSERT_FALSE(HasFatalFailure());
292 // Check that the app opened in a new tab of the existing browser.
293 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
294 browser()->host_desktop_type()));
295 ASSERT_EQ(2, browser()->tab_strip_model()->count());
297 // Unload the extension.
298 UninstallExtension(app_id);
299 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
300 browser()->host_desktop_type()));
301 ASSERT_FALSE(service->GetExtensionById(app_id, true));
303 // Set a pref indicating that the user wants to launch in a window.
304 extensions::SetLaunchType(service, app_id, extensions::LAUNCH_TYPE_WINDOW);
306 std::string app_id_new;
307 LoadAndWaitForLaunch("management/launch_app_tab", &app_id_new);
308 ASSERT_FALSE(HasFatalFailure());
310 // If the ID changed, then the pref will not apply to the app.
311 ASSERT_EQ(app_id, app_id_new);
313 #if defined(OS_MACOSX)
314 // App windows are not yet implemented on mac os. We should fall back
316 ASSERT_EQ(1u, chrome::GetBrowserCount(browser()->profile(),
317 browser()->host_desktop_type()));
318 ASSERT_EQ(2, browser()->tab_strip_model()->count());
320 // Find the app's browser. Opening in a new window will create
322 ASSERT_EQ(2u, chrome::GetBrowserCount(browser()->profile(),
323 browser()->host_desktop_type()));
324 Browser* app_browser = FindOtherBrowser(browser());
325 ASSERT_TRUE(app_browser->is_app());