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.
5 #include "chrome/browser/extensions/extension_context_menu_model.h"
7 #include "chrome/browser/extensions/extension_service_unittest.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/host_desktop.h"
10 #include "chrome/test/base/test_browser_window.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "extensions/browser/extension_system.h"
13 #include "extensions/browser/test_management_policy.h"
14 #include "extensions/common/extension_builder.h"
15 #include "extensions/common/value_builder.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace extensions
{
21 class ExtensionContextMenuModelTest
: public ExtensionServiceTestBase
{
24 // Tests that applicable menu items are disabled when a ManagementPolicy
26 TEST_F(ExtensionContextMenuModelTest
, PolicyDisablesItems
) {
27 InitializeEmptyExtensionService();
28 // Build an extension to pass to the menu constructor. It needs an
30 scoped_refptr
<Extension
> extension
= ExtensionBuilder()
31 .SetManifest(DictionaryBuilder()
32 .Set("name", "Page Action Extension")
34 .Set("manifest_version", 2)
35 .Set("page_action", DictionaryBuilder()
36 .Set("default_title", "Hello")))
38 ASSERT_TRUE(extension
.get());
39 service_
->AddExtension(extension
.get());
41 // Create a Browser for the ExtensionContextMenuModel to use.
42 Browser::CreateParams
params(profile_
.get(), chrome::GetActiveDesktop());
43 TestBrowserWindow test_window
;
44 params
.window
= &test_window
;
45 Browser
browser(params
);
47 scoped_refptr
<ExtensionContextMenuModel
> menu(
48 new ExtensionContextMenuModel(extension
.get(), &browser
, NULL
));
50 extensions::ExtensionSystem
* system
=
51 extensions::ExtensionSystem::Get(profile_
.get());
52 system
->management_policy()->UnregisterAllProviders();
54 // Actions should be enabled.
55 ASSERT_TRUE(menu
->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL
));
57 extensions::TestManagementPolicyProvider
policy_provider(
58 extensions::TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
);
59 system
->management_policy()->RegisterProvider(&policy_provider
);
61 // Now the actions are disabled.
62 ASSERT_FALSE(menu
->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL
));
64 // Don't leave |policy_provider| dangling.
65 system
->management_policy()->UnregisterAllProviders();
69 } // namespace extensions