OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/macros.h" | 5 #include "base/macros.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" |
10 #include "chrome/browser/extensions/extension_toolbar_model.h" | 11 #include "chrome/browser/extensions/extension_toolbar_model.h" |
11 #include "chrome/browser/extensions/test_extension_system.h" | 12 #include "chrome/browser/extensions/test_extension_system.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/extensions/api/extension_action/action_info.h" | 14 #include "chrome/common/extensions/api/extension_action/action_info.h" |
14 #include "components/crx_file/id_util.h" | 15 #include "components/crx_file/id_util.h" |
15 #include "extensions/browser/extension_prefs.h" | 16 #include "extensions/browser/extension_prefs.h" |
16 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
17 #include "extensions/browser/extension_system.h" | 18 #include "extensions/browser/extension_system.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 // Adds or removes the given |extension| and verify success. | 120 // Adds or removes the given |extension| and verify success. |
120 testing::AssertionResult AddExtension( | 121 testing::AssertionResult AddExtension( |
121 scoped_refptr<const Extension> extension) WARN_UNUSED_RESULT; | 122 scoped_refptr<const Extension> extension) WARN_UNUSED_RESULT; |
122 testing::AssertionResult RemoveExtension( | 123 testing::AssertionResult RemoveExtension( |
123 scoped_refptr<const Extension> extension) WARN_UNUSED_RESULT; | 124 scoped_refptr<const Extension> extension) WARN_UNUSED_RESULT; |
124 | 125 |
125 // Adds three extensions, all with browser actions. | 126 // Adds three extensions, all with browser actions. |
126 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; | 127 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; |
127 | 128 |
128 // Adds three extensions, one each for browser action, page action, and no | 129 // Adds three extensions, one each for browser action, page action, and no |
129 // action. | 130 // action, and are added in that order. |
130 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; | 131 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; |
131 | 132 |
132 // Returns the extension at the given index in the toolbar model, or NULL | 133 // Returns the extension at the given index in the toolbar model, or NULL |
133 // if one does not exist. | 134 // if one does not exist. |
134 const Extension* GetExtensionAtIndex(size_t index) const; | 135 const Extension* GetExtensionAtIndex(size_t index) const; |
135 | 136 |
136 ExtensionToolbarModel* toolbar_model() { return toolbar_model_.get(); } | 137 ExtensionToolbarModel* toolbar_model() { return toolbar_model_.get(); } |
137 | 138 |
138 const ExtensionToolbarModelTestObserver* observer() const { | 139 const ExtensionToolbarModelTestObserver* observer() const { |
139 return model_observer_.get(); | 140 return model_observer_.get(); |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 ASSERT_TRUE(AddActionExtensions()); | 686 ASSERT_TRUE(AddActionExtensions()); |
686 | 687 |
687 // With the switch on, extensions with page actions and no action should also | 688 // With the switch on, extensions with page actions and no action should also |
688 // be displayed in the toolbar. | 689 // be displayed in the toolbar. |
689 EXPECT_EQ(3u, num_toolbar_items()); | 690 EXPECT_EQ(3u, num_toolbar_items()); |
690 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); | 691 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); |
691 EXPECT_EQ(page_action(), GetExtensionAtIndex(1u)); | 692 EXPECT_EQ(page_action(), GetExtensionAtIndex(1u)); |
692 EXPECT_EQ(no_action(), GetExtensionAtIndex(2u)); | 693 EXPECT_EQ(no_action(), GetExtensionAtIndex(2u)); |
693 } | 694 } |
694 | 695 |
| 696 // Test that hiding actions on the toolbar results in their removal from the |
| 697 // model when the redesign switch is not enabled. |
| 698 TEST_F(ExtensionToolbarModelUnitTest, |
| 699 ExtensionToolbarActionsVisibilityNoSwitch) { |
| 700 Init(); |
| 701 |
| 702 ASSERT_TRUE(AddBrowserActionExtensions()); |
| 703 // Sanity check: Order should start as A B C. |
| 704 EXPECT_EQ(3u, num_toolbar_items()); |
| 705 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
| 706 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
| 707 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); |
| 708 |
| 709 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
| 710 |
| 711 // By default, all actions should be visible. |
| 712 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 713 prefs, browser_action_a()->id())); |
| 714 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 715 prefs, browser_action_b()->id())); |
| 716 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 717 prefs, browser_action_c()->id())); |
| 718 |
| 719 // Hiding an action should result in its removal from the toolbar. |
| 720 ExtensionActionAPI::SetBrowserActionVisibility( |
| 721 prefs, browser_action_b()->id(), false); |
| 722 EXPECT_FALSE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 723 prefs, browser_action_b()->id())); |
| 724 // Thus, there should now only be two items on the toolbar - A and C. |
| 725 EXPECT_EQ(2u, num_toolbar_items()); |
| 726 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
| 727 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); |
| 728 |
| 729 // Resetting the visibility to 'true' should result in the extension being |
| 730 // added back at its original position. |
| 731 ExtensionActionAPI::SetBrowserActionVisibility( |
| 732 prefs, browser_action_b()->id(), true); |
| 733 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 734 prefs, browser_action_b()->id())); |
| 735 // So the toolbar order should be A B C. |
| 736 EXPECT_EQ(3u, num_toolbar_items()); |
| 737 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
| 738 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
| 739 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); |
| 740 } |
| 741 |
| 742 // Test that hiding actions on the toolbar results in sending them to the |
| 743 // overflow menu when the redesign switch is enabled. |
| 744 TEST_F(ExtensionToolbarModelUnitTest, |
| 745 ExtensionToolbarActionsVisibilityWithSwitch) { |
| 746 FeatureSwitch::ScopedOverride enable_redesign( |
| 747 FeatureSwitch::extension_action_redesign(), true); |
| 748 Init(); |
| 749 |
| 750 // We choose to use all types of extensions here, since the misnamed |
| 751 // BrowserActionVisibility is now for toolbar visibility. |
| 752 ASSERT_TRUE(AddActionExtensions()); |
| 753 |
| 754 // For readability, alias extensions A B C. |
| 755 const Extension* extension_a = browser_action(); |
| 756 const Extension* extension_b = page_action(); |
| 757 const Extension* extension_c = no_action(); |
| 758 |
| 759 // Sanity check: Order should start as A B C, with all three visible. |
| 760 EXPECT_EQ(3u, num_toolbar_items()); |
| 761 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all'. |
| 762 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); |
| 763 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u)); |
| 764 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u)); |
| 765 |
| 766 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
| 767 |
| 768 // By default, all actions should be visible. |
| 769 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 770 prefs, extension_a->id())); |
| 771 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 772 prefs, extension_c->id())); |
| 773 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
| 774 prefs, extension_b->id())); |
| 775 |
| 776 // Hiding an action should result in it being sent to the overflow menu. |
| 777 ExtensionActionAPI::SetBrowserActionVisibility( |
| 778 prefs, extension_b->id(), false); |
| 779 |
| 780 // Thus, the order should be A C B, with B in the overflow. |
| 781 EXPECT_EQ(3u, num_toolbar_items()); |
| 782 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); |
| 783 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); |
| 784 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u)); |
| 785 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
| 786 |
| 787 // Hiding an extension's action should result in it being sent to the overflow |
| 788 // as well, but as the _first_ extension in the overflow. |
| 789 ExtensionActionAPI::SetBrowserActionVisibility( |
| 790 prefs, extension_a->id(), false); |
| 791 // Thus, the order should be C A B, with A and B in the overflow. |
| 792 EXPECT_EQ(3u, num_toolbar_items()); |
| 793 EXPECT_EQ(1, toolbar_model()->GetVisibleIconCount()); |
| 794 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
| 795 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
| 796 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
| 797 |
| 798 // Resetting A's visibility to true should send it back to the visible icons |
| 799 // (and should grow visible icons by 1), but it should be added to the end of |
| 800 // the visible icon list (not to its original position). |
| 801 ExtensionActionAPI::SetBrowserActionVisibility( |
| 802 prefs, extension_a->id(), true); |
| 803 // So order is C A B, with only B in the overflow. |
| 804 EXPECT_EQ(3u, num_toolbar_items()); |
| 805 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); |
| 806 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
| 807 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
| 808 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
| 809 |
| 810 // Resetting B to be visible should make the order C A B, with no overflow. |
| 811 ExtensionActionAPI::SetBrowserActionVisibility( |
| 812 prefs, extension_b->id(), true); |
| 813 EXPECT_EQ(3u, num_toolbar_items()); |
| 814 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all' |
| 815 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
| 816 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
| 817 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
| 818 } |
| 819 |
695 } // namespace extensions | 820 } // namespace extensions |
OLD | NEW |