| 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 "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
| 6 #include "chrome/app/chrome_command_ids.h" | 6 #include "chrome/app/chrome_command_ids.h" |
| 7 #include "chrome/browser/extensions/extension_browsertest.h" | 7 #include "chrome/browser/extensions/extension_browsertest.h" |
| 8 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 11 #include "chrome/browser/extensions/test_extension_management_policy.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 13 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/common/context_menu_params.h" | 17 #include "content/public/common/context_menu_params.h" |
| 18 #include "grit/generated_resources.h" |
| 16 #include "net/base/mock_host_resolver.h" | 19 #include "net/base/mock_host_resolver.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/models/menu_model.h" | 22 #include "ui/base/models/menu_model.h" |
| 19 | 23 |
| 20 using WebKit::WebContextMenuData; | 24 using WebKit::WebContextMenuData; |
| 21 using content::WebContents; | 25 using content::WebContents; |
| 22 using ui::MenuModel; | 26 using ui::MenuModel; |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 // This test class helps us sidestep platform-specific issues with popping up a | 29 // This test class helps us sidestep platform-specific issues with popping up a |
| 26 // real context menu, while still running through the actual code in | 30 // real context menu, while still running through the actual code in |
| 27 // RenderViewContextMenu where extension items get added and executed. | 31 // RenderViewContextMenu where extension items get added and executed. |
| 28 class TestRenderViewContextMenu : public RenderViewContextMenu { | 32 class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 29 public: | 33 public: |
| 30 TestRenderViewContextMenu(WebContents* web_contents, | 34 TestRenderViewContextMenu(WebContents* web_contents, |
| 31 const content::ContextMenuParams& params) | 35 const content::ContextMenuParams& params) |
| 32 : RenderViewContextMenu(web_contents, params) {} | 36 : RenderViewContextMenu(web_contents, params) {} |
| 33 | 37 |
| 34 virtual ~TestRenderViewContextMenu() {} | 38 virtual ~TestRenderViewContextMenu() {} |
| 35 | 39 |
| 36 bool HasExtensionItemWithLabel(const std::string& label) { | 40 // *** revert *** |
| 41 int ExtensionItemIdWithLabel(const std::string& label) { |
| 37 string16 label16 = UTF8ToUTF16(label); | 42 string16 label16 = UTF8ToUTF16(label); |
| 38 std::map<int, ExtensionMenuItem::Id>::iterator i; | 43 std::map<int, ExtensionMenuItem::Id>::iterator i; |
| 39 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { | 44 for (i = extension_item_map_.begin(); i != extension_item_map_.end(); ++i) { |
| 40 const ExtensionMenuItem::Id& id = i->second; | 45 const ExtensionMenuItem::Id& id = i->second; |
| 41 string16 tmp_label; | 46 string16 tmp_label; |
| 42 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); | 47 EXPECT_TRUE(GetItemLabel(id, &tmp_label)); |
| 43 if (tmp_label == label16) | 48 if (tmp_label == label16) |
| 44 return true; | 49 return id.uid; |
| 45 } | 50 } |
| 46 return false; | 51 return -1; |
| 52 } |
| 53 |
| 54 bool HasExtensionItemWithLabel(const std::string& label) { |
| 55 return (ExtensionItemIdWithLabel(label) != -1); |
| 47 } | 56 } |
| 48 | 57 |
| 49 // Looks in the menu for an extension item with |id|, and if it is found and | 58 // Looks in the menu for an extension item with |id|, and if it is found and |
| 50 // has a label, that is put in |result| and we return true. Otherwise returns | 59 // has a label, that is put in |result| and we return true. Otherwise returns |
| 51 // false. | 60 // false. |
| 52 bool GetItemLabel(const ExtensionMenuItem::Id& id, string16* result) { | 61 bool GetItemLabel(const ExtensionMenuItem::Id& id, string16* result) { |
| 53 int command_id = 0; | 62 int command_id = 0; |
| 54 if (!FindCommandId(id, &command_id)) | 63 if (!FindCommandId(id, &command_id)) |
| 55 return false; | 64 return false; |
| 56 | 65 |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 page_url, GURL(), frame_url, std::string("Page item"))); | 510 page_url, GURL(), frame_url, std::string("Page item"))); |
| 502 ASSERT_TRUE(MenuHasItemWithLabel( | 511 ASSERT_TRUE(MenuHasItemWithLabel( |
| 503 page_url, GURL(), frame_url, std::string("Frame item"))); | 512 page_url, GURL(), frame_url, std::string("Frame item"))); |
| 504 } | 513 } |
| 505 | 514 |
| 506 // Tests enabling and disabling a context menu item. | 515 // Tests enabling and disabling a context menu item. |
| 507 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) { | 516 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, Enabled) { |
| 508 TestEnabledContextMenu(true); | 517 TestEnabledContextMenu(true); |
| 509 TestEnabledContextMenu(false); | 518 TestEnabledContextMenu(false); |
| 510 } | 519 } |
| 520 |
| 521 // Tests that applicable menu items are disabled when an |
| 522 // ExtensionManagementPolicy prohibits them. |
| 523 IN_PROC_BROWSER_TEST_F(ExtensionContextMenuBrowserTest, PolicyDisablesItems) { |
| 524 ASSERT_TRUE(LoadContextMenuExtension("simple")); |
| 525 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 526 ASSERT_TRUE(service != NULL); |
| 527 ASSERT_FALSE(service->extensions()->is_empty()); |
| 528 // We need an extension to pass to the menu constructor, but we don't care |
| 529 // which one. |
| 530 ExtensionSet::const_iterator i = service->extensions()->begin(); |
| 531 const Extension* extension = *i; |
| 532 ASSERT_TRUE(extension != NULL); |
| 533 |
| 534 scoped_refptr<ExtensionContextMenuModel> menu( |
| 535 new ExtensionContextMenuModel(extension, browser())); |
| 536 |
| 537 ExtensionManagementPolicy* policy = service->extension_management_policy(); |
| 538 policy->UnregisterAllDelegates(); |
| 539 |
| 540 // Actions should be enabled. |
| 541 ASSERT_TRUE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::DISABLE)); |
| 542 ASSERT_TRUE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); |
| 543 |
| 544 TestExtensionManagementPolicyDelegate policy_delegate( |
| 545 TestExtensionManagementPolicyDelegate::PROHIBIT_MODIFY_STATUS); |
| 546 policy->RegisterDelegate(&policy_delegate); |
| 547 |
| 548 // Now the actions are disabled. |
| 549 ASSERT_FALSE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::DISABLE)); |
| 550 ASSERT_FALSE(menu->IsCommandIdEnabled(ExtensionContextMenuModel::UNINSTALL)); |
| 551 } |
| OLD | NEW |