Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/browser/extensions/extension_toolbar_model_unittest.cc

Issue 700453003: Revert of Make extensions that desire to act pop out if in overflow (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/files/file_util.h" 5 #include "base/files/file_util.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
11 #include "chrome/browser/extensions/extension_action_manager.h"
12 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_service_test_base.h" 12 #include "chrome/browser/extensions/extension_service_test_base.h"
14 #include "chrome/browser/extensions/extension_toolbar_model.h" 13 #include "chrome/browser/extensions/extension_toolbar_model.h"
15 #include "chrome/browser/extensions/extension_toolbar_model_factory.h" 14 #include "chrome/browser/extensions/extension_toolbar_model_factory.h"
16 #include "chrome/browser/extensions/extension_util.h" 15 #include "chrome/browser/extensions/extension_util.h"
17 #include "chrome/browser/extensions/test_extension_dir.h" 16 #include "chrome/browser/extensions/test_extension_dir.h"
18 #include "chrome/browser/extensions/test_extension_system.h" 17 #include "chrome/browser/extensions/test_extension_system.h"
19 #include "chrome/browser/extensions/unpacked_installer.h" 18 #include "chrome/browser/extensions/unpacked_installer.h"
20 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/sessions/session_tab_helper.h"
22 #include "chrome/common/extensions/api/extension_action/action_info.h" 20 #include "chrome/common/extensions/api/extension_action/action_info.h"
23 #include "components/crx_file/id_util.h" 21 #include "components/crx_file/id_util.h"
24 #include "content/public/test/web_contents_tester.h"
25 #include "extensions/browser/extension_prefs.h" 22 #include "extensions/browser/extension_prefs.h"
26 #include "extensions/browser/extension_registry.h" 23 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/extension_system.h" 24 #include "extensions/browser/extension_system.h"
28 #include "extensions/browser/test_extension_registry_observer.h" 25 #include "extensions/browser/test_extension_registry_observer.h"
29 #include "extensions/common/extension.h" 26 #include "extensions/common/extension.h"
30 #include "extensions/common/extension_builder.h" 27 #include "extensions/common/extension_builder.h"
31 #include "extensions/common/feature_switch.h" 28 #include "extensions/common/feature_switch.h"
32 #include "extensions/common/manifest_constants.h" 29 #include "extensions/common/manifest_constants.h"
33 #include "extensions/common/value_builder.h" 30 #include "extensions/common/value_builder.h"
34 31
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 class ExtensionToolbarModelTestObserver 87 class ExtensionToolbarModelTestObserver
91 : public ExtensionToolbarModel::Observer { 88 : public ExtensionToolbarModel::Observer {
92 public: 89 public:
93 explicit ExtensionToolbarModelTestObserver(ExtensionToolbarModel* model); 90 explicit ExtensionToolbarModelTestObserver(ExtensionToolbarModel* model);
94 ~ExtensionToolbarModelTestObserver() override; 91 ~ExtensionToolbarModelTestObserver() override;
95 92
96 size_t inserted_count() const { return inserted_count_; } 93 size_t inserted_count() const { return inserted_count_; }
97 size_t removed_count() const { return removed_count_; } 94 size_t removed_count() const { return removed_count_; }
98 size_t moved_count() const { return moved_count_; } 95 size_t moved_count() const { return moved_count_; }
99 int highlight_mode_count() const { return highlight_mode_count_; } 96 int highlight_mode_count() const { return highlight_mode_count_; }
100 size_t reorder_count() const { return reorder_count_; }
101 97
102 private: 98 private:
103 // ExtensionToolbarModel::Observer: 99 // ExtensionToolbarModel::Observer:
104 void ToolbarExtensionAdded(const Extension* extension, int index) override { 100 void ToolbarExtensionAdded(const Extension* extension, int index) override {
105 ++inserted_count_; 101 ++inserted_count_;
106 } 102 }
107 103
108 void ToolbarExtensionRemoved(const Extension* extension) override { 104 void ToolbarExtensionRemoved(const Extension* extension) override {
109 ++removed_count_; 105 ++removed_count_;
110 } 106 }
111 107
112 void ToolbarExtensionMoved(const Extension* extension, int index) override { 108 void ToolbarExtensionMoved(const Extension* extension, int index) override {
113 ++moved_count_; 109 ++moved_count_;
114 } 110 }
115 111
116 void ToolbarExtensionUpdated(const Extension* extension) override {} 112 void ToolbarExtensionUpdated(const Extension* extension) override {}
117 113
118 bool ShowExtensionActionPopup(const Extension* extension, 114 bool ShowExtensionActionPopup(const Extension* extension,
119 bool grant_active_tab) override { 115 bool grant_active_tab) override {
120 return false; 116 return false;
121 } 117 }
122 118
123 void ToolbarVisibleCountChanged() override {} 119 void ToolbarVisibleCountChanged() override {}
124 120
125 void ToolbarHighlightModeChanged(bool is_highlighting) override { 121 void ToolbarHighlightModeChanged(bool is_highlighting) override {
126 // Add one if highlighting, subtract one if not. 122 // Add one if highlighting, subtract one if not.
127 highlight_mode_count_ += is_highlighting ? 1 : -1; 123 highlight_mode_count_ += is_highlighting ? 1 : -1;
128 } 124 }
129 125
130 void OnToolbarReorderNecessary(content::WebContents* web_contents) override {
131 ++reorder_count_;
132 }
133
134 Browser* GetBrowser() override { return NULL; } 126 Browser* GetBrowser() override { return NULL; }
135 127
136 ExtensionToolbarModel* model_; 128 ExtensionToolbarModel* model_;
137 129
138 size_t inserted_count_; 130 size_t inserted_count_;
139 size_t removed_count_; 131 size_t removed_count_;
140 size_t moved_count_; 132 size_t moved_count_;
141 // Int because it could become negative (if something goes wrong). 133 // Int because it could become negative (if something goes wrong).
142 int highlight_mode_count_; 134 int highlight_mode_count_;
143 size_t reorder_count_;
144 }; 135 };
145 136
146 ExtensionToolbarModelTestObserver::ExtensionToolbarModelTestObserver( 137 ExtensionToolbarModelTestObserver::ExtensionToolbarModelTestObserver(
147 ExtensionToolbarModel* model) : model_(model), 138 ExtensionToolbarModel* model) : model_(model),
148 inserted_count_(0), 139 inserted_count_(0u),
149 removed_count_(0), 140 removed_count_(0u),
150 moved_count_(0), 141 moved_count_(0u),
151 highlight_mode_count_(0), 142 highlight_mode_count_(0) {
152 reorder_count_(0) {
153 model_->AddObserver(this); 143 model_->AddObserver(this);
154 } 144 }
155 145
156 ExtensionToolbarModelTestObserver::~ExtensionToolbarModelTestObserver() { 146 ExtensionToolbarModelTestObserver::~ExtensionToolbarModelTestObserver() {
157 model_->RemoveObserver(this); 147 model_->RemoveObserver(this);
158 } 148 }
159 149
160 } // namespace 150 } // namespace
161 151
162 class ExtensionToolbarModelUnitTest : public ExtensionServiceTestBase { 152 class ExtensionToolbarModelUnitTest : public ExtensionServiceTestBase {
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
698 688
699 // Test that the extension toolbar maintains the proper size, even after a pref 689 // Test that the extension toolbar maintains the proper size, even after a pref
700 // change. 690 // change.
701 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarSizeAfterPrefChange) { 691 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarSizeAfterPrefChange) {
702 Init(); 692 Init();
703 693
704 // Add the three browser action extensions. 694 // Add the three browser action extensions.
705 ASSERT_TRUE(AddBrowserActionExtensions()); 695 ASSERT_TRUE(AddBrowserActionExtensions());
706 EXPECT_EQ(3u, num_toolbar_items()); 696 EXPECT_EQ(3u, num_toolbar_items());
707 697
708 // Should be at max size. 698 // Should be at max size (-1).
709 EXPECT_TRUE(toolbar_model()->all_icons_visible()); 699 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount());
710 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count());
711 toolbar_model()->OnExtensionToolbarPrefChange(); 700 toolbar_model()->OnExtensionToolbarPrefChange();
712 // Should still be at max size. 701 // Should still be at max size.
713 EXPECT_TRUE(toolbar_model()->all_icons_visible()); 702 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount());
714 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count());
715 } 703 }
716 704
717 // Test that, in the absence of the extension-action-redesign switch, the 705 // Test that, in the absence of the extension-action-redesign switch, the
718 // model only contains extensions with browser actions. 706 // model only contains extensions with browser actions.
719 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesNoSwitch) { 707 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesNoSwitch) {
720 Init(); 708 Init();
721 ASSERT_TRUE(AddActionExtensions()); 709 ASSERT_TRUE(AddActionExtensions());
722 710
723 EXPECT_EQ(1u, num_toolbar_items()); 711 EXPECT_EQ(1u, num_toolbar_items());
724 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); 712 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u));
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 EXPECT_EQ(0u, incognito_observer.moved_count()); 805 EXPECT_EQ(0u, incognito_observer.moved_count());
818 806
819 // We should have two items, C and B, and the order should be preserved from 807 // We should have two items, C and B, and the order should be preserved from
820 // the original model. 808 // the original model.
821 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); 809 EXPECT_EQ(2u, incognito_model->toolbar_items().size());
822 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u, incognito_model)); 810 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u, incognito_model));
823 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u, incognito_model)); 811 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u, incognito_model));
824 812
825 // Extensions in the overflow menu in the regular toolbar should remain in 813 // Extensions in the overflow menu in the regular toolbar should remain in
826 // overflow in the incognito toolbar. So, we should have C [B]. 814 // overflow in the incognito toolbar. So, we should have C [B].
827 EXPECT_EQ(1u, incognito_model->visible_icon_count()); 815 EXPECT_EQ(1, incognito_model->GetVisibleIconCount());
828 // The regular model should still have two icons visible. 816 // The regular model should still have two icons visible.
829 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); 817 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount());
830 818
831 // Changing the incognito model size should not affect the regular model. 819 // Changing the incognito model size should not affect the regular model.
832 incognito_model->SetVisibleIconCount(0); 820 incognito_model->SetVisibleIconCount(0);
833 EXPECT_EQ(0u, incognito_model->visible_icon_count()); 821 EXPECT_EQ(0, incognito_model->GetVisibleIconCount());
834 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); 822 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount());
835 823
836 // Expanding the incognito model to 2 should register as "all icons" 824 // Expanding the incognito model to 2 should register as "all icons" (-1),
837 // since it is all of the incognito-enabled extensions. 825 // since it is all of the incognito-enabled extensions.
838 incognito_model->SetVisibleIconCount(2u); 826 incognito_model->SetVisibleIconCount(2u);
839 EXPECT_EQ(2u, incognito_model->visible_icon_count()); 827 EXPECT_EQ(-1, incognito_model->GetVisibleIconCount());
840 EXPECT_TRUE(incognito_model->all_icons_visible());
841 828
842 // Moving icons in the incognito toolbar should not affect the regular 829 // Moving icons in the incognito toolbar should not affect the regular
843 // toolbar. Incognito currently has C B... 830 // toolbar. Incognito currently has C B...
844 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u); 831 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u);
845 // So now it should be B C... 832 // So now it should be B C...
846 EXPECT_EQ(1u, incognito_observer.moved_count()); 833 EXPECT_EQ(1u, incognito_observer.moved_count());
847 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); 834 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model));
848 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); 835 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model));
849 // ... and the regular toolbar should be unaffected. 836 // ... and the regular toolbar should be unaffected.
850 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); 837 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u));
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
927 { 914 {
928 TestExtensionRegistryObserver observer(registry(), extension_b); 915 TestExtensionRegistryObserver observer(registry(), extension_b);
929 util::SetIsIncognitoEnabled(extension_b, profile(), true); 916 util::SetIsIncognitoEnabled(extension_b, profile(), true);
930 observer.WaitForExtensionLoaded(); 917 observer.WaitForExtensionLoaded();
931 } 918 }
932 919
933 // Now, we should have one icon in the incognito bar. But, since B is 920 // Now, we should have one icon in the incognito bar. But, since B is
934 // overflowed in the main bar, it shouldn't be visible. 921 // overflowed in the main bar, it shouldn't be visible.
935 EXPECT_EQ(1u, incognito_model->toolbar_items().size()); 922 EXPECT_EQ(1u, incognito_model->toolbar_items().size());
936 EXPECT_EQ(extension_b, GetExtensionAtIndex(0u, incognito_model)->id()); 923 EXPECT_EQ(extension_b, GetExtensionAtIndex(0u, incognito_model)->id());
937 EXPECT_EQ(0u, incognito_model->visible_icon_count()); 924 EXPECT_EQ(0, incognito_model->GetVisibleIconCount());
938 925
939 // Also enable extension a for incognito (again, wait for the reload). 926 // Also enable extension a for incognito (again, wait for the reload).
940 { 927 {
941 TestExtensionRegistryObserver observer(registry(), extension_a); 928 TestExtensionRegistryObserver observer(registry(), extension_a);
942 util::SetIsIncognitoEnabled(extension_a, profile(), true); 929 util::SetIsIncognitoEnabled(extension_a, profile(), true);
943 observer.WaitForExtensionLoaded(); 930 observer.WaitForExtensionLoaded();
944 } 931 }
945 932
946 // Now, both extensions should be enabled in incognito mode. In addition, the 933 // Now, both extensions should be enabled in incognito mode. In addition, the
947 // incognito toolbar should have expanded to show extension a (since it isn't 934 // incognito toolbar should have expanded to show extension a (since it isn't
948 // overflowed in the main bar). 935 // overflowed in the main bar).
949 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); 936 EXPECT_EQ(2u, incognito_model->toolbar_items().size());
950 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u, incognito_model)->id()); 937 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u, incognito_model)->id());
951 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u, incognito_model)->id()); 938 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u, incognito_model)->id());
952 EXPECT_EQ(1u, incognito_model->visible_icon_count()); 939 EXPECT_EQ(1, incognito_model->GetVisibleIconCount());
953 } 940 }
954 941
955 // Test that hiding actions on the toolbar results in sending them to the 942 // Test that hiding actions on the toolbar results in sending them to the
956 // overflow menu when the redesign switch is enabled. 943 // overflow menu when the redesign switch is enabled.
957 TEST_F(ExtensionToolbarModelUnitTest, 944 TEST_F(ExtensionToolbarModelUnitTest,
958 ExtensionToolbarActionsVisibilityWithSwitch) { 945 ExtensionToolbarActionsVisibilityWithSwitch) {
959 FeatureSwitch::ScopedOverride enable_redesign( 946 FeatureSwitch::ScopedOverride enable_redesign(
960 FeatureSwitch::extension_action_redesign(), true); 947 FeatureSwitch::extension_action_redesign(), true);
961 Init(); 948 Init();
962 949
963 // We choose to use all types of extensions here, since the misnamed 950 // We choose to use all types of extensions here, since the misnamed
964 // BrowserActionVisibility is now for toolbar visibility. 951 // BrowserActionVisibility is now for toolbar visibility.
965 ASSERT_TRUE(AddActionExtensions()); 952 ASSERT_TRUE(AddActionExtensions());
966 953
967 // For readability, alias extensions A B C. 954 // For readability, alias extensions A B C.
968 const Extension* extension_a = browser_action(); 955 const Extension* extension_a = browser_action();
969 const Extension* extension_b = page_action(); 956 const Extension* extension_b = page_action();
970 const Extension* extension_c = no_action(); 957 const Extension* extension_c = no_action();
971 958
972 // Sanity check: Order should start as A B C, with all three visible. 959 // Sanity check: Order should start as A B C, with all three visible.
973 EXPECT_EQ(3u, num_toolbar_items()); 960 EXPECT_EQ(3u, num_toolbar_items());
974 EXPECT_TRUE(toolbar_model()->all_icons_visible()); 961 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all'.
975 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); 962 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u));
976 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u)); 963 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u));
977 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u)); 964 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u));
978 965
979 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); 966 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
980 967
981 // By default, all actions should be visible. 968 // By default, all actions should be visible.
982 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( 969 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility(
983 prefs, extension_a->id())); 970 prefs, extension_a->id()));
984 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( 971 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility(
985 prefs, extension_c->id())); 972 prefs, extension_c->id()));
986 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( 973 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility(
987 prefs, extension_b->id())); 974 prefs, extension_b->id()));
988 975
989 // Hiding an action should result in it being sent to the overflow menu. 976 // Hiding an action should result in it being sent to the overflow menu.
990 ExtensionActionAPI::SetBrowserActionVisibility( 977 ExtensionActionAPI::SetBrowserActionVisibility(
991 prefs, extension_b->id(), false); 978 prefs, extension_b->id(), false);
992 979
993 // Thus, the order should be A C B, with B in the overflow. 980 // Thus, the order should be A C B, with B in the overflow.
994 EXPECT_EQ(3u, num_toolbar_items()); 981 EXPECT_EQ(3u, num_toolbar_items());
995 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); 982 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount());
996 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); 983 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u));
997 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u)); 984 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u));
998 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); 985 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u));
999 986
1000 // Hiding an extension's action should result in it being sent to the overflow 987 // Hiding an extension's action should result in it being sent to the overflow
1001 // as well, but as the _first_ extension in the overflow. 988 // as well, but as the _first_ extension in the overflow.
1002 ExtensionActionAPI::SetBrowserActionVisibility( 989 ExtensionActionAPI::SetBrowserActionVisibility(
1003 prefs, extension_a->id(), false); 990 prefs, extension_a->id(), false);
1004 // Thus, the order should be C A B, with A and B in the overflow. 991 // Thus, the order should be C A B, with A and B in the overflow.
1005 EXPECT_EQ(3u, num_toolbar_items()); 992 EXPECT_EQ(3u, num_toolbar_items());
1006 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); 993 EXPECT_EQ(1, toolbar_model()->GetVisibleIconCount());
1007 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); 994 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u));
1008 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); 995 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u));
1009 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); 996 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u));
1010 997
1011 // Resetting A's visibility to true should send it back to the visible icons 998 // Resetting A's visibility to true should send it back to the visible icons
1012 // (and should grow visible icons by 1), but it should be added to the end of 999 // (and should grow visible icons by 1), but it should be added to the end of
1013 // the visible icon list (not to its original position). 1000 // the visible icon list (not to its original position).
1014 ExtensionActionAPI::SetBrowserActionVisibility( 1001 ExtensionActionAPI::SetBrowserActionVisibility(
1015 prefs, extension_a->id(), true); 1002 prefs, extension_a->id(), true);
1016 // So order is C A B, with only B in the overflow. 1003 // So order is C A B, with only B in the overflow.
1017 EXPECT_EQ(3u, num_toolbar_items()); 1004 EXPECT_EQ(3u, num_toolbar_items());
1018 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); 1005 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount());
1019 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); 1006 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u));
1020 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); 1007 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u));
1021 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); 1008 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u));
1022 1009
1023 // Resetting B to be visible should make the order C A B, with no overflow. 1010 // Resetting B to be visible should make the order C A B, with no overflow.
1024 ExtensionActionAPI::SetBrowserActionVisibility( 1011 ExtensionActionAPI::SetBrowserActionVisibility(
1025 prefs, extension_b->id(), true); 1012 prefs, extension_b->id(), true);
1026 EXPECT_EQ(3u, num_toolbar_items()); 1013 EXPECT_EQ(3u, num_toolbar_items());
1027 EXPECT_TRUE(toolbar_model()->all_icons_visible()); 1014 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all'
1028 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); 1015 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u));
1029 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); 1016 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u));
1030 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); 1017 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u));
1031 } 1018 }
1032 1019
1033 // Test that toolbar actions can pop themselves out of overflow if they want
1034 // to act on a given web contents.
1035 TEST_F(ExtensionToolbarModelUnitTest, ToolbarActionsPopOutToAct) {
1036 // Extensions popping themselves out to act is part of the toolbar redesign,
1037 // and hidden behind a flag.
1038 FeatureSwitch::ScopedOverride enable_redesign(
1039 FeatureSwitch::extension_action_redesign(), true);
1040 Init();
1041
1042 ASSERT_TRUE(AddActionExtensions());
1043
1044 // We should start in the order of "browser action" "page action" "no action"
1045 // and have all extensions visible.
1046 EXPECT_EQ(3u, num_toolbar_items());
1047 EXPECT_TRUE(toolbar_model()->all_icons_visible());
1048 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u));
1049 EXPECT_EQ(page_action(), GetExtensionAtIndex(1u));
1050 EXPECT_EQ(no_action(), GetExtensionAtIndex(2u));
1051
1052 // Shrink the model to only show one action, and move the page action to the
1053 // end.
1054 toolbar_model()->SetVisibleIconCount(1);
1055 toolbar_model()->MoveExtensionIcon(page_action()->id(), 2u);
1056
1057 // Quickly verify that the move/visible count worked.
1058 EXPECT_EQ(1u, toolbar_model()->visible_icon_count());
1059 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u));
1060 EXPECT_EQ(no_action(), GetExtensionAtIndex(1u));
1061 EXPECT_EQ(page_action(), GetExtensionAtIndex(2u));
1062
1063 // Create two test web contents, and a session tab helper for each. We need
1064 // a session tab helper, since we rely on tab ids.
1065 content::WebContents* web_contents =
1066 content::WebContentsTester::CreateTestWebContents(profile(), NULL);
1067 ASSERT_TRUE(web_contents);
1068 SessionTabHelper::CreateForWebContents(web_contents);
1069 content::WebContents* second_web_contents =
1070 content::WebContentsTester::CreateTestWebContents(profile(), NULL);
1071 ASSERT_TRUE(second_web_contents);
1072 SessionTabHelper::CreateForWebContents(second_web_contents);
1073
1074 // Find the tab ids, ensure that the two web contents have different ids, and
1075 // verify that neither is -1 (invalid).
1076 int tab_id = SessionTabHelper::IdForTab(web_contents);
1077 int second_tab_id = SessionTabHelper::IdForTab(second_web_contents);
1078 EXPECT_NE(tab_id, second_tab_id);
1079 EXPECT_NE(-1, second_tab_id);
1080 EXPECT_NE(-1, tab_id);
1081
1082 // First, check the model order for the first tab. Since we haven't changed
1083 // anything (i.e., no extensions want to act), this should be the same as we
1084 // left it: "browser action", "no action", "page action", with only one
1085 // visible.
1086 ExtensionList tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1087 ASSERT_EQ(3u, tab_order.size());
1088 EXPECT_EQ(browser_action(), tab_order[0]);
1089 EXPECT_EQ(no_action(), tab_order[1]);
1090 EXPECT_EQ(page_action(), tab_order[2]);
1091 EXPECT_EQ(1u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1092 // And we should have no notifications to reorder the toolbar.
1093 EXPECT_EQ(0u, observer()->reorder_count());
1094
1095 // Make "page action" want to act by making it's page action visible on the
1096 // first tab, and notify the API of the change.
1097 ExtensionActionManager* action_manager =
1098 ExtensionActionManager::Get(profile());
1099 ExtensionAction* action = action_manager->GetExtensionAction(*page_action());
1100 ASSERT_TRUE(action);
1101 action->SetIsVisible(tab_id, true);
1102 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile());
1103 extension_action_api->NotifyChange(action, web_contents, profile());
1104
1105 // This should result in "page action" being popped out of the overflow menu.
1106 // This has two visible effects:
1107 // - page action should move to the second index (the one right after the last
1108 // originally-visible).
1109 // - The visible count should increase by one (so page action is visible).
1110 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1111 ASSERT_EQ(3u, tab_order.size());
1112 EXPECT_EQ(browser_action(), tab_order[0]);
1113 EXPECT_EQ(page_action(), tab_order[1]);
1114 EXPECT_EQ(no_action(), tab_order[2]);
1115 EXPECT_EQ(2u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1116 // We should also have been told to reorder the toolbar.
1117 EXPECT_EQ(1u, observer()->reorder_count());
1118
1119 // This should not have any effect on the second tab, which should still have
1120 // the original order and visible count.
1121 tab_order = toolbar_model()->GetItemOrderForTab(second_web_contents);
1122 ASSERT_EQ(3u, tab_order.size());
1123 EXPECT_EQ(browser_action(), tab_order[0]);
1124 EXPECT_EQ(no_action(), tab_order[1]);
1125 EXPECT_EQ(page_action(), tab_order[2]);
1126 EXPECT_EQ(1u,
1127 toolbar_model()->GetVisibleIconCountForTab(second_web_contents));
1128
1129 // Now, set the action to be hidden again, and notify of the change.
1130 action->SetIsVisible(tab_id, false);
1131 extension_action_api->NotifyChange(action, web_contents, profile());
1132 // The order and visible count should return to normal (the page action should
1133 // move back to its original index in overflow). So, order should be "browser
1134 // action", "no action", "page action".
1135 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1136 ASSERT_EQ(3u, tab_order.size());
1137 EXPECT_EQ(browser_action(), tab_order[0]);
1138 EXPECT_EQ(no_action(), tab_order[1]);
1139 EXPECT_EQ(page_action(), tab_order[2]);
1140 EXPECT_EQ(1u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1141 // This should also result in a reorder.
1142 EXPECT_EQ(2u, observer()->reorder_count());
1143
1144 // Move page action to the first index (so it's naturally visible), and make
1145 // it want to act.
1146 toolbar_model()->MoveExtensionIcon(page_action()->id(), 0u);
1147 action->SetIsVisible(tab_id, true);
1148 extension_action_api->NotifyChange(action, web_contents, profile());
1149 // Since the action is already visible, this should have no effect - the order
1150 // and visible count should remain unchanged. Order is "page action", "browser
1151 // action", "no action".
1152 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1153 ASSERT_EQ(3u, tab_order.size());
1154 EXPECT_EQ(page_action(), tab_order[0]);
1155 EXPECT_EQ(browser_action(), tab_order[1]);
1156 EXPECT_EQ(no_action(), tab_order[2]);
1157 EXPECT_EQ(1u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1158
1159 // We should still be able to increase the size of the model, and to move the
1160 // page action.
1161 toolbar_model()->SetVisibleIconCount(2);
1162 toolbar_model()->MoveExtensionIcon(page_action()->id(), 1u);
1163 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1164 ASSERT_EQ(3u, tab_order.size());
1165 EXPECT_EQ(browser_action(), tab_order[0]);
1166 EXPECT_EQ(page_action(), tab_order[1]);
1167 EXPECT_EQ(no_action(), tab_order[2]);
1168 EXPECT_EQ(2u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1169
1170 // Neither of the above operations should have precipitated a reorder.
1171 EXPECT_EQ(2u, observer()->reorder_count());
1172
1173 // If we moved the page action, the move should remain in effect even after
1174 // the action no longer wants to act.
1175 action->SetIsVisible(tab_id, false);
1176 extension_action_api->NotifyChange(action, web_contents, profile());
1177 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1178 ASSERT_EQ(3u, tab_order.size());
1179 EXPECT_EQ(browser_action(), tab_order[0]);
1180 EXPECT_EQ(page_action(), tab_order[1]);
1181 EXPECT_EQ(no_action(), tab_order[2]);
1182 EXPECT_EQ(2u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1183 // The above change should *not* require a reorder, because the extension is
1184 // in a new, visible spot and doesn't need to change its position.
1185 EXPECT_EQ(2u, observer()->reorder_count());
1186
1187 // Test the edge case of having no icons visible.
1188 toolbar_model()->SetVisibleIconCount(0);
1189 EXPECT_EQ(0u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1190 action->SetIsVisible(tab_id, true);
1191 extension_action_api->NotifyChange(action, web_contents, profile());
1192 tab_order = toolbar_model()->GetItemOrderForTab(web_contents);
1193 ASSERT_EQ(3u, tab_order.size());
1194 EXPECT_EQ(page_action(), tab_order[0]);
1195 EXPECT_EQ(browser_action(), tab_order[1]);
1196 EXPECT_EQ(no_action(), tab_order[2]);
1197 EXPECT_EQ(1u, toolbar_model()->GetVisibleIconCountForTab(web_contents));
1198 EXPECT_EQ(3u, observer()->reorder_count());
1199 }
1200
1201 } // namespace extensions 1020 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_toolbar_model.cc ('k') | chrome/browser/ui/cocoa/extensions/browser_actions_controller.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698