OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <map> | |
6 | |
7 #include "chrome/browser/extensions/extension_apitest.h" | |
8 #include "chrome/browser/extensions/extension_service.h" | |
9 #include "chrome/browser/extensions/extension_system.h" | |
10 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
11 #include "chrome/browser/extensions/test_management_policy.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_list.h" | |
17 #include "chrome/common/chrome_notification_types.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "content/public/test/test_utils.h" | |
20 | |
21 using extensions::Extension; | |
22 | |
23 namespace { | |
24 | |
25 // Find a browser other than |browser|. | |
26 Browser* FindOtherBrowser(Browser* browser) { | |
27 Browser* found = NULL; | |
28 for (BrowserList::const_iterator it = BrowserList::begin(); | |
29 it != BrowserList::end(); ++it) { | |
30 if (*it == browser) | |
31 continue; | |
32 found = *it; | |
33 } | |
34 | |
35 return found; | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 class ExtensionManagementApiTest : public ExtensionApiTest { | |
41 public: | |
42 virtual void SetUpCommandLine(CommandLine* command_line) { | |
43 ExtensionApiTest::SetUpCommandLine(command_line); | |
44 command_line->AppendSwitch(switches::kEnablePanels); | |
45 } | |
46 | |
47 virtual void InstallExtensions() { | |
48 FilePath basedir = test_data_dir_.AppendASCII("management"); | |
49 | |
50 // Load 4 enabled items. | |
51 InstallNamedExtension(basedir, "enabled_extension"); | |
52 InstallNamedExtension(basedir, "enabled_app"); | |
53 InstallNamedExtension(basedir, "description"); | |
54 InstallNamedExtension(basedir, "permissions"); | |
55 | |
56 // Load 2 disabled items. | |
57 InstallNamedExtension(basedir, "disabled_extension"); | |
58 DisableExtension(extension_ids_["disabled_extension"]); | |
59 InstallNamedExtension(basedir, "disabled_app"); | |
60 DisableExtension(extension_ids_["disabled_app"]); | |
61 } | |
62 | |
63 // Load an app, and wait for a message from app "management/launch_on_install" | |
64 // indicating that the new app has been launched. | |
65 void LoadAndWaitForLaunch(const std::string& app_path, | |
66 std::string* out_app_id) { | |
67 ExtensionTestMessageListener launched_app("launched app", false); | |
68 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(app_path))); | |
69 | |
70 if (out_app_id) | |
71 *out_app_id = last_loaded_extension_id_; | |
72 | |
73 ASSERT_TRUE(launched_app.WaitUntilSatisfied()); | |
74 } | |
75 | |
76 protected: | |
77 void InstallNamedExtension(FilePath basedir, std::string name) { | |
78 const Extension* extension = LoadExtension(basedir.AppendASCII(name)); | |
79 ASSERT_TRUE(extension); | |
80 extension_ids_[name] = extension->id(); | |
81 } | |
82 | |
83 // Maps installed extension names to their IDs.. | |
84 std::map<std::string, std::string> extension_ids_; | |
85 }; | |
86 | |
87 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Basics) { | |
88 InstallExtensions(); | |
89 ASSERT_TRUE(RunExtensionSubtest("management/test", "basics.html")); | |
90 } | |
91 | |
92 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, Uninstall) { | |
93 InstallExtensions(); | |
94 ASSERT_TRUE(RunExtensionSubtest("management/test", "uninstall.html")); | |
95 } | |
96 | |
97 // Tests actions on extensions when no management policy is in place. | |
98 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, ManagementPolicyAllowed) { | |
99 InstallExtensions(); | |
100 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
101 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"], | |
102 false)); | |
103 | |
104 // Ensure that all actions are allowed. | |
105 extensions::ExtensionSystem::Get( | |
106 browser()->profile())->management_policy()->UnregisterAllProviders(); | |
107 | |
108 ASSERT_TRUE(RunExtensionSubtest("management/management_policy", | |
109 "allowed.html")); | |
110 // The last thing the test does is uninstall the "enabled_extension". | |
111 EXPECT_FALSE(service->GetExtensionById(extension_ids_["enabled_extension"], | |
112 true)); | |
113 } | |
114 | |
115 // Tests actions on extensions when management policy prohibits those actions. | |
116 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, ManagementPolicyProhibited) { | |
117 InstallExtensions(); | |
118 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
119 EXPECT_TRUE(service->GetExtensionById(extension_ids_["enabled_extension"], | |
120 false)); | |
121 | |
122 // Prohibit status changes. | |
123 extensions::ManagementPolicy* policy = extensions::ExtensionSystem::Get( | |
124 browser()->profile())->management_policy(); | |
125 policy->UnregisterAllProviders(); | |
126 extensions::TestManagementPolicyProvider provider( | |
127 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS); | |
128 policy->RegisterProvider(&provider); | |
129 ASSERT_TRUE(RunExtensionSubtest("management/management_policy", | |
130 "prohibited.html")); | |
131 } | |
132 | |
133 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, LaunchPanelApp) { | |
134 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
135 | |
136 // Load an extension that calls launchApp() on any app that gets | |
137 // installed. | |
138 ExtensionTestMessageListener launcher_loaded("launcher loaded", false); | |
139 ASSERT_TRUE(LoadExtension( | |
140 test_data_dir_.AppendASCII("management/launch_on_install"))); | |
141 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied()); | |
142 | |
143 // Load an app with app.launch.container = "panel". | |
144 std::string app_id; | |
145 LoadAndWaitForLaunch("management/launch_app_panel", &app_id); | |
146 ASSERT_FALSE(HasFatalFailure()); // Stop the test if any ASSERT failed. | |
147 | |
148 // Find the app's browser. Check that it is a panel. | |
149 ASSERT_EQ(2u, browser::GetBrowserCount(browser()->profile())); | |
150 Browser* app_browser = FindOtherBrowser(browser()); | |
151 ASSERT_TRUE(app_browser->is_type_panel()); | |
152 ASSERT_TRUE(app_browser->is_app()); | |
153 | |
154 // Close the app panel. | |
155 content::WindowedNotificationObserver signal( | |
156 chrome::NOTIFICATION_BROWSER_CLOSED, | |
157 content::Source<Browser>(app_browser)); | |
158 | |
159 chrome::CloseWindow(app_browser); | |
160 signal.Wait(); | |
161 | |
162 // Unload the extension. | |
163 UninstallExtension(app_id); | |
164 ASSERT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | |
165 ASSERT_FALSE(service->GetExtensionById(app_id, true)); | |
166 | |
167 // Set a pref indicating that the user wants to launch in a regular tab. | |
168 // This should be ignored, because panel apps always load in a panel. | |
169 service->extension_prefs()->SetLaunchType( | |
170 app_id, extensions::ExtensionPrefs::LAUNCH_REGULAR); | |
171 | |
172 // Load the extension again. | |
173 std::string app_id_new; | |
174 LoadAndWaitForLaunch("management/launch_app_panel", &app_id_new); | |
175 ASSERT_FALSE(HasFatalFailure()); | |
176 | |
177 // If the ID changed, then the pref will not apply to the app. | |
178 ASSERT_EQ(app_id, app_id_new); | |
179 | |
180 // Find the app's browser. Apps that should load in a panel ignore | |
181 // prefs, so we should still see the launch in a panel. | |
182 ASSERT_EQ(2u, browser::GetBrowserCount(browser()->profile())); | |
183 app_browser = FindOtherBrowser(browser()); | |
184 ASSERT_TRUE(app_browser->is_type_panel()); | |
185 ASSERT_TRUE(app_browser->is_app()); | |
186 } | |
187 | |
188 IN_PROC_BROWSER_TEST_F(ExtensionManagementApiTest, LaunchTabApp) { | |
189 ExtensionService* service = browser()->profile()->GetExtensionService(); | |
190 | |
191 // Load an extension that calls launchApp() on any app that gets | |
192 // installed. | |
193 ExtensionTestMessageListener launcher_loaded("launcher loaded", false); | |
194 ASSERT_TRUE(LoadExtension( | |
195 test_data_dir_.AppendASCII("management/launch_on_install"))); | |
196 ASSERT_TRUE(launcher_loaded.WaitUntilSatisfied()); | |
197 | |
198 // Code below assumes that the test starts with a single browser window | |
199 // hosting one tab. | |
200 ASSERT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | |
201 ASSERT_EQ(1, browser()->tab_count()); | |
202 | |
203 // Load an app with app.launch.container = "tab". | |
204 std::string app_id; | |
205 LoadAndWaitForLaunch("management/launch_app_tab", &app_id); | |
206 ASSERT_FALSE(HasFatalFailure()); | |
207 | |
208 // Check that the app opened in a new tab of the existing browser. | |
209 ASSERT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | |
210 ASSERT_EQ(2, browser()->tab_count()); | |
211 | |
212 // Unload the extension. | |
213 UninstallExtension(app_id); | |
214 ASSERT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | |
215 ASSERT_FALSE(service->GetExtensionById(app_id, true)); | |
216 | |
217 // Set a pref indicating that the user wants to launch in a window. | |
218 service->extension_prefs()->SetLaunchType( | |
219 app_id, extensions::ExtensionPrefs::LAUNCH_WINDOW); | |
220 | |
221 std::string app_id_new; | |
222 LoadAndWaitForLaunch("management/launch_app_tab", &app_id_new); | |
223 ASSERT_FALSE(HasFatalFailure()); | |
224 | |
225 // If the ID changed, then the pref will not apply to the app. | |
226 ASSERT_EQ(app_id, app_id_new); | |
227 | |
228 #if defined(OS_MACOSX) | |
229 // App windows are not yet implemented on mac os. We should fall back | |
230 // to a normal tab. | |
231 ASSERT_EQ(1u, browser::GetBrowserCount(browser()->profile())); | |
232 ASSERT_EQ(2, browser()->tab_count()); | |
233 #else | |
234 // Find the app's browser. Opening in a new window will create | |
235 // a new browser. | |
236 ASSERT_EQ(2u, browser::GetBrowserCount(browser()->profile())); | |
237 Browser* app_browser = FindOtherBrowser(browser()); | |
238 ASSERT_TRUE(app_browser->is_app()); | |
239 ASSERT_FALSE(app_browser->is_type_panel()); | |
240 #endif | |
241 } | |
OLD | NEW |