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/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" |
11 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/extension_service_test_base.h" | 13 #include "chrome/browser/extensions/extension_service_test_base.h" |
13 #include "chrome/browser/extensions/extension_toolbar_model.h" | 14 #include "chrome/browser/extensions/extension_toolbar_model.h" |
14 #include "chrome/browser/extensions/extension_toolbar_model_factory.h" | 15 #include "chrome/browser/extensions/extension_toolbar_model_factory.h" |
15 #include "chrome/browser/extensions/extension_util.h" | 16 #include "chrome/browser/extensions/extension_util.h" |
16 #include "chrome/browser/extensions/test_extension_dir.h" | 17 #include "chrome/browser/extensions/test_extension_dir.h" |
17 #include "chrome/browser/extensions/test_extension_system.h" | 18 #include "chrome/browser/extensions/test_extension_system.h" |
18 #include "chrome/browser/extensions/unpacked_installer.h" | 19 #include "chrome/browser/extensions/unpacked_installer.h" |
19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/sessions/session_tab_helper.h" |
20 #include "chrome/common/extensions/api/extension_action/action_info.h" | 22 #include "chrome/common/extensions/api/extension_action/action_info.h" |
21 #include "components/crx_file/id_util.h" | 23 #include "components/crx_file/id_util.h" |
| 24 #include "content/public/test/web_contents_tester.h" |
22 #include "extensions/browser/extension_prefs.h" | 25 #include "extensions/browser/extension_prefs.h" |
23 #include "extensions/browser/extension_registry.h" | 26 #include "extensions/browser/extension_registry.h" |
24 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
25 #include "extensions/browser/test_extension_registry_observer.h" | 28 #include "extensions/browser/test_extension_registry_observer.h" |
26 #include "extensions/common/extension.h" | 29 #include "extensions/common/extension.h" |
27 #include "extensions/common/extension_builder.h" | 30 #include "extensions/common/extension_builder.h" |
28 #include "extensions/common/feature_switch.h" | 31 #include "extensions/common/feature_switch.h" |
29 #include "extensions/common/manifest_constants.h" | 32 #include "extensions/common/manifest_constants.h" |
30 #include "extensions/common/value_builder.h" | 33 #include "extensions/common/value_builder.h" |
31 | 34 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 class ExtensionToolbarModelTestObserver | 90 class ExtensionToolbarModelTestObserver |
88 : public ExtensionToolbarModel::Observer { | 91 : public ExtensionToolbarModel::Observer { |
89 public: | 92 public: |
90 explicit ExtensionToolbarModelTestObserver(ExtensionToolbarModel* model); | 93 explicit ExtensionToolbarModelTestObserver(ExtensionToolbarModel* model); |
91 ~ExtensionToolbarModelTestObserver() override; | 94 ~ExtensionToolbarModelTestObserver() override; |
92 | 95 |
93 size_t inserted_count() const { return inserted_count_; } | 96 size_t inserted_count() const { return inserted_count_; } |
94 size_t removed_count() const { return removed_count_; } | 97 size_t removed_count() const { return removed_count_; } |
95 size_t moved_count() const { return moved_count_; } | 98 size_t moved_count() const { return moved_count_; } |
96 int highlight_mode_count() const { return highlight_mode_count_; } | 99 int highlight_mode_count() const { return highlight_mode_count_; } |
| 100 size_t reorder_count() const { return reorder_count_; } |
97 | 101 |
98 private: | 102 private: |
99 // ExtensionToolbarModel::Observer: | 103 // ExtensionToolbarModel::Observer: |
100 void ToolbarExtensionAdded(const Extension* extension, int index) override { | 104 void ToolbarExtensionAdded(const Extension* extension, int index) override { |
101 ++inserted_count_; | 105 ++inserted_count_; |
102 } | 106 } |
103 | 107 |
104 void ToolbarExtensionRemoved(const Extension* extension) override { | 108 void ToolbarExtensionRemoved(const Extension* extension) override { |
105 ++removed_count_; | 109 ++removed_count_; |
106 } | 110 } |
107 | 111 |
108 void ToolbarExtensionMoved(const Extension* extension, int index) override { | 112 void ToolbarExtensionMoved(const Extension* extension, int index) override { |
109 ++moved_count_; | 113 ++moved_count_; |
110 } | 114 } |
111 | 115 |
112 void ToolbarExtensionUpdated(const Extension* extension) override {} | 116 void ToolbarExtensionUpdated(const Extension* extension) override {} |
113 | 117 |
114 bool ShowExtensionActionPopup(const Extension* extension, | 118 bool ShowExtensionActionPopup(const Extension* extension, |
115 bool grant_active_tab) override { | 119 bool grant_active_tab) override { |
116 return false; | 120 return false; |
117 } | 121 } |
118 | 122 |
119 void ToolbarVisibleCountChanged() override {} | 123 void ToolbarVisibleCountChanged() override {} |
120 | 124 |
121 void ToolbarHighlightModeChanged(bool is_highlighting) override { | 125 void ToolbarHighlightModeChanged(bool is_highlighting) override { |
122 // Add one if highlighting, subtract one if not. | 126 // Add one if highlighting, subtract one if not. |
123 highlight_mode_count_ += is_highlighting ? 1 : -1; | 127 highlight_mode_count_ += is_highlighting ? 1 : -1; |
124 } | 128 } |
125 | 129 |
| 130 void OnToolbarReorderNecessary(content::WebContents* web_contents) override { |
| 131 ++reorder_count_; |
| 132 } |
| 133 |
126 Browser* GetBrowser() override { return NULL; } | 134 Browser* GetBrowser() override { return NULL; } |
127 | 135 |
128 ExtensionToolbarModel* model_; | 136 ExtensionToolbarModel* model_; |
129 | 137 |
130 size_t inserted_count_; | 138 size_t inserted_count_; |
131 size_t removed_count_; | 139 size_t removed_count_; |
132 size_t moved_count_; | 140 size_t moved_count_; |
133 // Int because it could become negative (if something goes wrong). | 141 // Int because it could become negative (if something goes wrong). |
134 int highlight_mode_count_; | 142 int highlight_mode_count_; |
| 143 size_t reorder_count_; |
135 }; | 144 }; |
136 | 145 |
137 ExtensionToolbarModelTestObserver::ExtensionToolbarModelTestObserver( | 146 ExtensionToolbarModelTestObserver::ExtensionToolbarModelTestObserver( |
138 ExtensionToolbarModel* model) : model_(model), | 147 ExtensionToolbarModel* model) : model_(model), |
139 inserted_count_(0u), | 148 inserted_count_(0), |
140 removed_count_(0u), | 149 removed_count_(0), |
141 moved_count_(0u), | 150 moved_count_(0), |
142 highlight_mode_count_(0) { | 151 highlight_mode_count_(0), |
| 152 reorder_count_(0) { |
143 model_->AddObserver(this); | 153 model_->AddObserver(this); |
144 } | 154 } |
145 | 155 |
146 ExtensionToolbarModelTestObserver::~ExtensionToolbarModelTestObserver() { | 156 ExtensionToolbarModelTestObserver::~ExtensionToolbarModelTestObserver() { |
147 model_->RemoveObserver(this); | 157 model_->RemoveObserver(this); |
148 } | 158 } |
149 | 159 |
150 } // namespace | 160 } // namespace |
151 | 161 |
152 class ExtensionToolbarModelUnitTest : public ExtensionServiceTestBase { | 162 class ExtensionToolbarModelUnitTest : public ExtensionServiceTestBase { |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 | 698 |
689 // Test that the extension toolbar maintains the proper size, even after a pref | 699 // Test that the extension toolbar maintains the proper size, even after a pref |
690 // change. | 700 // change. |
691 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarSizeAfterPrefChange) { | 701 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarSizeAfterPrefChange) { |
692 Init(); | 702 Init(); |
693 | 703 |
694 // Add the three browser action extensions. | 704 // Add the three browser action extensions. |
695 ASSERT_TRUE(AddBrowserActionExtensions()); | 705 ASSERT_TRUE(AddBrowserActionExtensions()); |
696 EXPECT_EQ(3u, num_toolbar_items()); | 706 EXPECT_EQ(3u, num_toolbar_items()); |
697 | 707 |
698 // Should be at max size (-1). | 708 // Should be at max size. |
699 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); | 709 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
| 710 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); |
700 toolbar_model()->OnExtensionToolbarPrefChange(); | 711 toolbar_model()->OnExtensionToolbarPrefChange(); |
701 // Should still be at max size. | 712 // Should still be at max size. |
702 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); | 713 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
| 714 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); |
703 } | 715 } |
704 | 716 |
705 // Test that, in the absence of the extension-action-redesign switch, the | 717 // Test that, in the absence of the extension-action-redesign switch, the |
706 // model only contains extensions with browser actions. | 718 // model only contains extensions with browser actions. |
707 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesNoSwitch) { | 719 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesNoSwitch) { |
708 Init(); | 720 Init(); |
709 ASSERT_TRUE(AddActionExtensions()); | 721 ASSERT_TRUE(AddActionExtensions()); |
710 | 722 |
711 EXPECT_EQ(1u, num_toolbar_items()); | 723 EXPECT_EQ(1u, num_toolbar_items()); |
712 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); | 724 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
805 EXPECT_EQ(0u, incognito_observer.moved_count()); | 817 EXPECT_EQ(0u, incognito_observer.moved_count()); |
806 | 818 |
807 // We should have two items, C and B, and the order should be preserved from | 819 // We should have two items, C and B, and the order should be preserved from |
808 // the original model. | 820 // the original model. |
809 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); | 821 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); |
810 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u, incognito_model)); | 822 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u, incognito_model)); |
811 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u, incognito_model)); | 823 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u, incognito_model)); |
812 | 824 |
813 // Extensions in the overflow menu in the regular toolbar should remain in | 825 // Extensions in the overflow menu in the regular toolbar should remain in |
814 // overflow in the incognito toolbar. So, we should have C [B]. | 826 // overflow in the incognito toolbar. So, we should have C [B]. |
815 EXPECT_EQ(1, incognito_model->GetVisibleIconCount()); | 827 EXPECT_EQ(1u, incognito_model->visible_icon_count()); |
816 // The regular model should still have two icons visible. | 828 // The regular model should still have two icons visible. |
817 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); | 829 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); |
818 | 830 |
819 // Changing the incognito model size should not affect the regular model. | 831 // Changing the incognito model size should not affect the regular model. |
820 incognito_model->SetVisibleIconCount(0); | 832 incognito_model->SetVisibleIconCount(0); |
821 EXPECT_EQ(0, incognito_model->GetVisibleIconCount()); | 833 EXPECT_EQ(0u, incognito_model->visible_icon_count()); |
822 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); | 834 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); |
823 | 835 |
824 // Expanding the incognito model to 2 should register as "all icons" (-1), | 836 // Expanding the incognito model to 2 should register as "all icons" |
825 // since it is all of the incognito-enabled extensions. | 837 // since it is all of the incognito-enabled extensions. |
826 incognito_model->SetVisibleIconCount(2u); | 838 incognito_model->SetVisibleIconCount(2u); |
827 EXPECT_EQ(-1, incognito_model->GetVisibleIconCount()); | 839 EXPECT_EQ(2u, incognito_model->visible_icon_count()); |
| 840 EXPECT_TRUE(incognito_model->all_icons_visible()); |
828 | 841 |
829 // Moving icons in the incognito toolbar should not affect the regular | 842 // Moving icons in the incognito toolbar should not affect the regular |
830 // toolbar. Incognito currently has C B... | 843 // toolbar. Incognito currently has C B... |
831 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u); | 844 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u); |
832 // So now it should be B C... | 845 // So now it should be B C... |
833 EXPECT_EQ(1u, incognito_observer.moved_count()); | 846 EXPECT_EQ(1u, incognito_observer.moved_count()); |
834 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); | 847 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); |
835 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); | 848 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); |
836 // ... and the regular toolbar should be unaffected. | 849 // ... and the regular toolbar should be unaffected. |
837 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 850 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
914 { | 927 { |
915 TestExtensionRegistryObserver observer(registry(), extension_b); | 928 TestExtensionRegistryObserver observer(registry(), extension_b); |
916 util::SetIsIncognitoEnabled(extension_b, profile(), true); | 929 util::SetIsIncognitoEnabled(extension_b, profile(), true); |
917 observer.WaitForExtensionLoaded(); | 930 observer.WaitForExtensionLoaded(); |
918 } | 931 } |
919 | 932 |
920 // Now, we should have one icon in the incognito bar. But, since B is | 933 // Now, we should have one icon in the incognito bar. But, since B is |
921 // overflowed in the main bar, it shouldn't be visible. | 934 // overflowed in the main bar, it shouldn't be visible. |
922 EXPECT_EQ(1u, incognito_model->toolbar_items().size()); | 935 EXPECT_EQ(1u, incognito_model->toolbar_items().size()); |
923 EXPECT_EQ(extension_b, GetExtensionAtIndex(0u, incognito_model)->id()); | 936 EXPECT_EQ(extension_b, GetExtensionAtIndex(0u, incognito_model)->id()); |
924 EXPECT_EQ(0, incognito_model->GetVisibleIconCount()); | 937 EXPECT_EQ(0u, incognito_model->visible_icon_count()); |
925 | 938 |
926 // Also enable extension a for incognito (again, wait for the reload). | 939 // Also enable extension a for incognito (again, wait for the reload). |
927 { | 940 { |
928 TestExtensionRegistryObserver observer(registry(), extension_a); | 941 TestExtensionRegistryObserver observer(registry(), extension_a); |
929 util::SetIsIncognitoEnabled(extension_a, profile(), true); | 942 util::SetIsIncognitoEnabled(extension_a, profile(), true); |
930 observer.WaitForExtensionLoaded(); | 943 observer.WaitForExtensionLoaded(); |
931 } | 944 } |
932 | 945 |
933 // Now, both extensions should be enabled in incognito mode. In addition, the | 946 // Now, both extensions should be enabled in incognito mode. In addition, the |
934 // incognito toolbar should have expanded to show extension a (since it isn't | 947 // incognito toolbar should have expanded to show extension a (since it isn't |
935 // overflowed in the main bar). | 948 // overflowed in the main bar). |
936 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); | 949 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); |
937 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u, incognito_model)->id()); | 950 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u, incognito_model)->id()); |
938 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u, incognito_model)->id()); | 951 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u, incognito_model)->id()); |
939 EXPECT_EQ(1, incognito_model->GetVisibleIconCount()); | 952 EXPECT_EQ(1u, incognito_model->visible_icon_count()); |
940 } | 953 } |
941 | 954 |
942 // Test that hiding actions on the toolbar results in sending them to the | 955 // Test that hiding actions on the toolbar results in sending them to the |
943 // overflow menu when the redesign switch is enabled. | 956 // overflow menu when the redesign switch is enabled. |
944 TEST_F(ExtensionToolbarModelUnitTest, | 957 TEST_F(ExtensionToolbarModelUnitTest, |
945 ExtensionToolbarActionsVisibilityWithSwitch) { | 958 ExtensionToolbarActionsVisibilityWithSwitch) { |
946 FeatureSwitch::ScopedOverride enable_redesign( | 959 FeatureSwitch::ScopedOverride enable_redesign( |
947 FeatureSwitch::extension_action_redesign(), true); | 960 FeatureSwitch::extension_action_redesign(), true); |
948 Init(); | 961 Init(); |
949 | 962 |
950 // We choose to use all types of extensions here, since the misnamed | 963 // We choose to use all types of extensions here, since the misnamed |
951 // BrowserActionVisibility is now for toolbar visibility. | 964 // BrowserActionVisibility is now for toolbar visibility. |
952 ASSERT_TRUE(AddActionExtensions()); | 965 ASSERT_TRUE(AddActionExtensions()); |
953 | 966 |
954 // For readability, alias extensions A B C. | 967 // For readability, alias extensions A B C. |
955 const Extension* extension_a = browser_action(); | 968 const Extension* extension_a = browser_action(); |
956 const Extension* extension_b = page_action(); | 969 const Extension* extension_b = page_action(); |
957 const Extension* extension_c = no_action(); | 970 const Extension* extension_c = no_action(); |
958 | 971 |
959 // Sanity check: Order should start as A B C, with all three visible. | 972 // Sanity check: Order should start as A B C, with all three visible. |
960 EXPECT_EQ(3u, num_toolbar_items()); | 973 EXPECT_EQ(3u, num_toolbar_items()); |
961 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all'. | 974 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
962 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); | 975 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); |
963 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u)); | 976 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u)); |
964 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u)); | 977 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u)); |
965 | 978 |
966 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); | 979 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
967 | 980 |
968 // By default, all actions should be visible. | 981 // By default, all actions should be visible. |
969 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( | 982 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
970 prefs, extension_a->id())); | 983 prefs, extension_a->id())); |
971 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( | 984 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
972 prefs, extension_c->id())); | 985 prefs, extension_c->id())); |
973 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( | 986 EXPECT_TRUE(ExtensionActionAPI::GetBrowserActionVisibility( |
974 prefs, extension_b->id())); | 987 prefs, extension_b->id())); |
975 | 988 |
976 // Hiding an action should result in it being sent to the overflow menu. | 989 // Hiding an action should result in it being sent to the overflow menu. |
977 ExtensionActionAPI::SetBrowserActionVisibility( | 990 ExtensionActionAPI::SetBrowserActionVisibility( |
978 prefs, extension_b->id(), false); | 991 prefs, extension_b->id(), false); |
979 | 992 |
980 // Thus, the order should be A C B, with B in the overflow. | 993 // Thus, the order should be A C B, with B in the overflow. |
981 EXPECT_EQ(3u, num_toolbar_items()); | 994 EXPECT_EQ(3u, num_toolbar_items()); |
982 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); | 995 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); |
983 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); | 996 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); |
984 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u)); | 997 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u)); |
985 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | 998 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
986 | 999 |
987 // Hiding an extension's action should result in it being sent to the overflow | 1000 // Hiding an extension's action should result in it being sent to the overflow |
988 // as well, but as the _first_ extension in the overflow. | 1001 // as well, but as the _first_ extension in the overflow. |
989 ExtensionActionAPI::SetBrowserActionVisibility( | 1002 ExtensionActionAPI::SetBrowserActionVisibility( |
990 prefs, extension_a->id(), false); | 1003 prefs, extension_a->id(), false); |
991 // Thus, the order should be C A B, with A and B in the overflow. | 1004 // Thus, the order should be C A B, with A and B in the overflow. |
992 EXPECT_EQ(3u, num_toolbar_items()); | 1005 EXPECT_EQ(3u, num_toolbar_items()); |
993 EXPECT_EQ(1, toolbar_model()->GetVisibleIconCount()); | 1006 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); |
994 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | 1007 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
995 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | 1008 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
996 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | 1009 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
997 | 1010 |
998 // Resetting A's visibility to true should send it back to the visible icons | 1011 // Resetting A's visibility to true should send it back to the visible icons |
999 // (and should grow visible icons by 1), but it should be added to the end of | 1012 // (and should grow visible icons by 1), but it should be added to the end of |
1000 // the visible icon list (not to its original position). | 1013 // the visible icon list (not to its original position). |
1001 ExtensionActionAPI::SetBrowserActionVisibility( | 1014 ExtensionActionAPI::SetBrowserActionVisibility( |
1002 prefs, extension_a->id(), true); | 1015 prefs, extension_a->id(), true); |
1003 // So order is C A B, with only B in the overflow. | 1016 // So order is C A B, with only B in the overflow. |
1004 EXPECT_EQ(3u, num_toolbar_items()); | 1017 EXPECT_EQ(3u, num_toolbar_items()); |
1005 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); | 1018 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); |
1006 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | 1019 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
1007 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | 1020 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
1008 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | 1021 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
1009 | 1022 |
1010 // Resetting B to be visible should make the order C A B, with no overflow. | 1023 // Resetting B to be visible should make the order C A B, with no overflow. |
1011 ExtensionActionAPI::SetBrowserActionVisibility( | 1024 ExtensionActionAPI::SetBrowserActionVisibility( |
1012 prefs, extension_b->id(), true); | 1025 prefs, extension_b->id(), true); |
1013 EXPECT_EQ(3u, num_toolbar_items()); | 1026 EXPECT_EQ(3u, num_toolbar_items()); |
1014 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all' | 1027 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
1015 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | 1028 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
1016 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | 1029 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
1017 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | 1030 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
1018 } | 1031 } |
1019 | 1032 |
| 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 |
1020 } // namespace extensions | 1201 } // namespace extensions |
OLD | NEW |