| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_context_menu_model.h" | 5 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 6 | 6 |
| 7 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 7 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_service_test_base.h" | 9 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 10 #include "chrome/browser/extensions/menu_manager.h" | 10 #include "chrome/browser/extensions/menu_manager.h" |
| 11 #include "chrome/browser/extensions/menu_manager_factory.h" | 11 #include "chrome/browser/extensions/menu_manager_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/host_desktop.h" | 13 #include "chrome/browser/ui/host_desktop.h" |
| 14 #include "chrome/common/extensions/api/context_menus.h" | 14 #include "chrome/common/extensions/api/context_menus.h" |
| 15 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "chrome/test/base/test_browser_window.h" | 16 #include "chrome/test/base/test_browser_window.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "components/crx_file/id_util.h" | 18 #include "components/crx_file/id_util.h" |
| 19 #include "extensions/browser/extension_prefs.h" | 19 #include "extensions/browser/extension_prefs.h" |
| 20 #include "extensions/browser/extension_system.h" | 20 #include "extensions/browser/extension_system.h" |
| 21 #include "extensions/browser/test_management_policy.h" | 21 #include "extensions/browser/test_management_policy.h" |
| 22 #include "extensions/common/extension_builder.h" | 22 #include "extensions/common/extension_builder.h" |
| 23 #include "extensions/common/feature_switch.h" | 23 #include "extensions/common/feature_switch.h" |
| 24 #include "extensions/common/manifest.h" |
| 24 #include "extensions/common/manifest_constants.h" | 25 #include "extensions/common/manifest_constants.h" |
| 25 #include "extensions/common/value_builder.h" | 26 #include "extensions/common/value_builder.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
| 28 | 29 |
| 29 namespace extensions { | 30 namespace extensions { |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 // Build an extension to pass to the menu constructor, with the an action | 34 // Build an extension to pass to the menu constructor, with the an action |
| 34 // specified by |action_key|. | 35 // specified by |action_key|. |
| 35 scoped_refptr<const Extension> BuildExtension(const std::string& name, | 36 scoped_refptr<const Extension> BuildExtension(const std::string& name, |
| 36 const char* action_key) { | 37 const char* action_key, |
| 38 Manifest::Location location) { |
| 37 return ExtensionBuilder() | 39 return ExtensionBuilder() |
| 38 .SetManifest(DictionaryBuilder() | 40 .SetManifest(DictionaryBuilder() |
| 39 .Set("name", name) | 41 .Set("name", name) |
| 40 .Set("version", "1") | 42 .Set("version", "1") |
| 41 .Set("manifest_version", 2) | 43 .Set("manifest_version", 2) |
| 42 .Set(action_key, DictionaryBuilder().Pass())) | 44 .Set(action_key, DictionaryBuilder().Pass())) |
| 43 .SetID(crx_file::id_util::GenerateId(name)) | 45 .SetID(crx_file::id_util::GenerateId(name)) |
| 46 .SetLocation(location) |
| 44 .Build(); | 47 .Build(); |
| 45 } | 48 } |
| 46 | 49 |
| 47 // Create a Browser for the ExtensionContextMenuModel to use. | 50 // Create a Browser for the ExtensionContextMenuModel to use. |
| 48 scoped_ptr<Browser> CreateBrowser(Profile* profile) { | 51 scoped_ptr<Browser> CreateBrowser(Profile* profile) { |
| 49 Browser::CreateParams params(profile, chrome::GetActiveDesktop()); | 52 Browser::CreateParams params(profile, chrome::GetActiveDesktop()); |
| 50 TestBrowserWindow test_window; | 53 TestBrowserWindow test_window; |
| 51 params.window = &test_window; | 54 params.window = &test_window; |
| 52 return scoped_ptr<Browser>(new Browser(params)); | 55 return scoped_ptr<Browser>(new Browser(params)); |
| 53 } | 56 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 model->InitMenu(model->GetExtension()); | 119 model->InitMenu(model->GetExtension()); |
| 117 } | 120 } |
| 118 | 121 |
| 119 int ExtensionContextMenuModelTest::CountExtensionItems( | 122 int ExtensionContextMenuModelTest::CountExtensionItems( |
| 120 ExtensionContextMenuModel* model) { | 123 ExtensionContextMenuModel* model) { |
| 121 return model->extension_items_count_; | 124 return model->extension_items_count_; |
| 122 } | 125 } |
| 123 | 126 |
| 124 // Tests that applicable menu items are disabled when a ManagementPolicy | 127 // Tests that applicable menu items are disabled when a ManagementPolicy |
| 125 // prohibits them. | 128 // prohibits them. |
| 126 TEST_F(ExtensionContextMenuModelTest, PolicyDisablesItems) { | 129 TEST_F(ExtensionContextMenuModelTest, RequiredInstallationsDisablesItems) { |
| 127 InitializeEmptyExtensionService(); | 130 InitializeEmptyExtensionService(); |
| 131 |
| 132 // First, test that a component extension cannot be uninstalled by the |
| 133 // standard management policy. |
| 128 scoped_refptr<const Extension> extension = | 134 scoped_refptr<const Extension> extension = |
| 129 BuildExtension("extension", manifest_keys::kPageAction); | 135 BuildExtension("component", |
| 136 manifest_keys::kBrowserAction, |
| 137 Manifest::COMPONENT); |
| 130 ASSERT_TRUE(extension.get()); | 138 ASSERT_TRUE(extension.get()); |
| 131 service()->AddExtension(extension.get()); | 139 service()->AddExtension(extension.get()); |
| 132 | 140 |
| 133 scoped_ptr<Browser> browser = CreateBrowser(profile()); | 141 scoped_ptr<Browser> browser = CreateBrowser(profile()); |
| 134 | 142 |
| 135 scoped_refptr<ExtensionContextMenuModel> menu( | 143 scoped_refptr<ExtensionContextMenuModel> menu( |
| 136 new ExtensionContextMenuModel(extension.get(), browser.get())); | 144 new ExtensionContextMenuModel(extension.get(), browser.get())); |
| 137 | 145 |
| 146 // Uninstallation should be disabled. |
| 147 EXPECT_FALSE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); |
| 148 |
| 149 // Also test that management policy can determine whether or not |
| 150 // policy-installed extensions can be installed/uninstalled. |
| 151 extension = BuildExtension("extension", |
| 152 manifest_keys::kPageAction, |
| 153 Manifest::INTERNAL); |
| 154 ASSERT_TRUE(extension.get()); |
| 155 service()->AddExtension(extension.get()); |
| 156 |
| 157 menu = new ExtensionContextMenuModel(extension.get(), browser.get()); |
| 158 |
| 138 ExtensionSystem* system = ExtensionSystem::Get(profile()); | 159 ExtensionSystem* system = ExtensionSystem::Get(profile()); |
| 139 system->management_policy()->UnregisterAllProviders(); | 160 system->management_policy()->UnregisterAllProviders(); |
| 140 | 161 |
| 141 // Actions should be enabled. | 162 // Actions should be enabled. |
| 142 ASSERT_TRUE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); | 163 ASSERT_TRUE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); |
| 143 | 164 |
| 144 TestManagementPolicyProvider policy_provider( | 165 TestManagementPolicyProvider policy_provider( |
| 145 TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS); | 166 TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS); |
| 146 system->management_policy()->RegisterProvider(&policy_provider); | 167 system->management_policy()->RegisterProvider(&policy_provider); |
| 147 | 168 |
| 148 // Now the actions are disabled. | 169 // Now the actions are disabled. |
| 149 ASSERT_FALSE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); | 170 ASSERT_FALSE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); |
| 150 | 171 |
| 151 // Don't leave |policy_provider| dangling. | 172 // Don't leave |policy_provider| dangling. |
| 152 system->management_policy()->UnregisterAllProviders(); | 173 system->management_policy()->UnregisterProvider(&policy_provider); |
| 153 } | 174 } |
| 154 | 175 |
| 155 TEST_F(ExtensionContextMenuModelTest, ExtensionItemTest) { | 176 TEST_F(ExtensionContextMenuModelTest, ExtensionItemTest) { |
| 156 InitializeEmptyExtensionService(); | 177 InitializeEmptyExtensionService(); |
| 157 scoped_refptr<const Extension> extension = | 178 scoped_refptr<const Extension> extension = |
| 158 BuildExtension("extension", manifest_keys::kPageAction); | 179 BuildExtension("extension", |
| 180 manifest_keys::kPageAction, |
| 181 Manifest::INTERNAL); |
| 159 ASSERT_TRUE(extension.get()); | 182 ASSERT_TRUE(extension.get()); |
| 160 service()->AddExtension(extension.get()); | 183 service()->AddExtension(extension.get()); |
| 161 | 184 |
| 162 scoped_ptr<Browser> browser = CreateBrowser(profile()); | 185 scoped_ptr<Browser> browser = CreateBrowser(profile()); |
| 163 | 186 |
| 164 // Create a MenuManager for adding context items. | 187 // Create a MenuManager for adding context items. |
| 165 MenuManager* manager = static_cast<MenuManager*>( | 188 MenuManager* manager = static_cast<MenuManager*>( |
| 166 (MenuManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 189 (MenuManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 167 profile(), | 190 profile(), |
| 168 &MenuManagerFactory::BuildServiceInstanceForTesting))); | 191 &MenuManagerFactory::BuildServiceInstanceForTesting))); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // Adding another top level item should not increase the count. | 230 // Adding another top level item should not increase the count. |
| 208 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, | 231 EXPECT_EQ(api::context_menus::ACTION_MENU_TOP_LEVEL_LIMIT, |
| 209 CountExtensionItems(menu.get())); | 232 CountExtensionItems(menu.get())); |
| 210 } | 233 } |
| 211 | 234 |
| 212 // Test that the "show" and "hide" menu items appear correctly in the extension | 235 // Test that the "show" and "hide" menu items appear correctly in the extension |
| 213 // context menu. | 236 // context menu. |
| 214 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHide) { | 237 TEST_F(ExtensionContextMenuModelTest, ExtensionContextMenuShowAndHide) { |
| 215 InitializeEmptyExtensionService(); | 238 InitializeEmptyExtensionService(); |
| 216 scoped_refptr<const Extension> page_action = | 239 scoped_refptr<const Extension> page_action = |
| 217 BuildExtension("page_action_extension", manifest_keys::kPageAction); | 240 BuildExtension("page_action_extension", |
| 241 manifest_keys::kPageAction, |
| 242 Manifest::INTERNAL); |
| 218 ASSERT_TRUE(page_action.get()); | 243 ASSERT_TRUE(page_action.get()); |
| 219 scoped_refptr<const Extension> browser_action = | 244 scoped_refptr<const Extension> browser_action = |
| 220 BuildExtension("browser_action_extension", manifest_keys::kBrowserAction); | 245 BuildExtension("browser_action_extension", |
| 246 manifest_keys::kBrowserAction, |
| 247 Manifest::INTERNAL); |
| 221 ASSERT_TRUE(browser_action.get()); | 248 ASSERT_TRUE(browser_action.get()); |
| 222 | 249 |
| 223 service()->AddExtension(page_action.get()); | 250 service()->AddExtension(page_action.get()); |
| 224 service()->AddExtension(browser_action.get()); | 251 service()->AddExtension(browser_action.get()); |
| 225 | 252 |
| 226 scoped_ptr<Browser> browser = CreateBrowser(profile()); | 253 scoped_ptr<Browser> browser = CreateBrowser(profile()); |
| 227 | 254 |
| 228 scoped_refptr<ExtensionContextMenuModel> menu( | 255 scoped_refptr<ExtensionContextMenuModel> menu( |
| 229 new ExtensionContextMenuModel(page_action.get(), browser.get())); | 256 new ExtensionContextMenuModel(page_action.get(), browser.get())); |
| 230 | 257 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 // Hide the browser action. This should mean the string is "show". | 293 // Hide the browser action. This should mean the string is "show". |
| 267 ExtensionActionAPI::SetBrowserActionVisibility( | 294 ExtensionActionAPI::SetBrowserActionVisibility( |
| 268 prefs, browser_action->id(), false); | 295 prefs, browser_action->id(), false); |
| 269 menu = new ExtensionContextMenuModel(browser_action.get(), browser.get()); | 296 menu = new ExtensionContextMenuModel(browser_action.get(), browser.get()); |
| 270 index = GetCommandIndex(menu, visibility_command); | 297 index = GetCommandIndex(menu, visibility_command); |
| 271 EXPECT_NE(-1, index); | 298 EXPECT_NE(-1, index); |
| 272 EXPECT_EQ(show_string, menu->GetLabelAt(index)); | 299 EXPECT_EQ(show_string, menu->GetLabelAt(index)); |
| 273 } | 300 } |
| 274 | 301 |
| 275 } // namespace extensions | 302 } // namespace extensions |
| OLD | NEW |