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