| 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 "chrome/browser/ui/toolbar/toolbar_actions_model.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 #include "content/public/test/test_renderer_host.h" | 36 #include "content/public/test/test_renderer_host.h" |
| 37 #include "content/public/test/web_contents_tester.h" | 37 #include "content/public/test/web_contents_tester.h" |
| 38 #include "extensions/browser/extension_prefs.h" | 38 #include "extensions/browser/extension_prefs.h" |
| 39 #include "extensions/browser/extension_registry.h" | 39 #include "extensions/browser/extension_registry.h" |
| 40 #include "extensions/browser/extension_system.h" | 40 #include "extensions/browser/extension_system.h" |
| 41 #include "extensions/browser/pref_names.h" | 41 #include "extensions/browser/pref_names.h" |
| 42 #include "extensions/browser/test_extension_registry_observer.h" | 42 #include "extensions/browser/test_extension_registry_observer.h" |
| 43 #include "extensions/browser/uninstall_reason.h" | 43 #include "extensions/browser/uninstall_reason.h" |
| 44 #include "extensions/common/extension.h" | 44 #include "extensions/common/extension.h" |
| 45 #include "extensions/common/extension_builder.h" | 45 #include "extensions/common/extension_builder.h" |
| 46 #include "extensions/common/feature_switch.h" | |
| 47 #include "extensions/common/manifest.h" | 46 #include "extensions/common/manifest.h" |
| 48 #include "extensions/common/value_builder.h" | 47 #include "extensions/common/value_builder.h" |
| 49 | 48 |
| 50 namespace { | 49 namespace { |
| 51 | 50 |
| 52 // A simple observer that tracks the number of times certain events occur. | 51 // A simple observer that tracks the number of times certain events occur. |
| 53 class ToolbarActionsModelTestObserver : public ToolbarActionsModel::Observer { | 52 class ToolbarActionsModelTestObserver : public ToolbarActionsModel::Observer { |
| 54 public: | 53 public: |
| 55 explicit ToolbarActionsModelTestObserver(ToolbarActionsModel* model); | 54 explicit ToolbarActionsModelTestObserver(ToolbarActionsModel* model); |
| 56 ~ToolbarActionsModelTestObserver() override; | 55 ~ToolbarActionsModelTestObserver() override; |
| (...skipping 772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 829 | 828 |
| 830 // Should be at max size. | 829 // Should be at max size. |
| 831 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | 830 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
| 832 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); | 831 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); |
| 833 toolbar_model()->OnActionToolbarPrefChange(); | 832 toolbar_model()->OnActionToolbarPrefChange(); |
| 834 // Should still be at max size. | 833 // Should still be at max size. |
| 835 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | 834 EXPECT_TRUE(toolbar_model()->all_icons_visible()); |
| 836 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); | 835 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); |
| 837 } | 836 } |
| 838 | 837 |
| 839 // Test that, in the absence of the extension-action-redesign switch, the | |
| 840 // model only contains extensions with browser actions and component actions. | |
| 841 TEST_F(ToolbarActionsModelUnitTest, TestToolbarExtensionTypesDisabledSwitch) { | |
| 842 extensions::FeatureSwitch::ScopedOverride enable_redesign( | |
| 843 extensions::FeatureSwitch::extension_action_redesign(), false); | |
| 844 Init(); | |
| 845 ASSERT_TRUE(AddActionExtensions()); | |
| 846 | |
| 847 EXPECT_EQ(1u, num_toolbar_items()); | |
| 848 EXPECT_EQ(browser_action()->id(), GetActionIdAtIndex(0u)); | |
| 849 } | |
| 850 | |
| 851 // Test that, with the extension-action-redesign switch, the model contains | 838 // Test that, with the extension-action-redesign switch, the model contains |
| 852 // all types of extensions, except those which should not be displayed on the | 839 // all types of extensions, except those which should not be displayed on the |
| 853 // toolbar (like component extensions). | 840 // toolbar (like component extensions). |
| 854 TEST_F(ToolbarActionsModelUnitTest, TestToolbarExtensionTypesEnabledSwitch) { | 841 TEST_F(ToolbarActionsModelUnitTest, TestToolbarExtensionTypesEnabledSwitch) { |
| 855 extensions::FeatureSwitch::ScopedOverride enable_redesign( | |
| 856 extensions::FeatureSwitch::extension_action_redesign(), true); | |
| 857 Init(); | 842 Init(); |
| 858 | 843 |
| 859 ASSERT_TRUE(AddActionExtensions()); | 844 ASSERT_TRUE(AddActionExtensions()); |
| 860 | 845 |
| 861 // With the switch on, extensions with page actions and no action should also | 846 // With the switch on, extensions with page actions and no action should also |
| 862 // be displayed in the toolbar. | 847 // be displayed in the toolbar. |
| 863 EXPECT_EQ(3u, num_toolbar_items()); | 848 EXPECT_EQ(3u, num_toolbar_items()); |
| 864 EXPECT_EQ(browser_action()->id(), GetActionIdAtIndex(0u)); | 849 EXPECT_EQ(browser_action()->id(), GetActionIdAtIndex(0u)); |
| 865 EXPECT_EQ(page_action()->id(), GetActionIdAtIndex(1u)); | 850 EXPECT_EQ(page_action()->id(), GetActionIdAtIndex(1u)); |
| 866 EXPECT_EQ(no_action()->id(), GetActionIdAtIndex(2u)); | 851 EXPECT_EQ(no_action()->id(), GetActionIdAtIndex(2u)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 897 scoped_refptr<const extensions::Extension> internal_extension_no_action = | 882 scoped_refptr<const extensions::Extension> internal_extension_no_action = |
| 898 extensions::extension_action_test_util::CreateActionExtension( | 883 extensions::extension_action_test_util::CreateActionExtension( |
| 899 "internal ext no action", | 884 "internal ext no action", |
| 900 extensions::extension_action_test_util::NO_ACTION, | 885 extensions::extension_action_test_util::NO_ACTION, |
| 901 extensions::Manifest::INTERNAL); | 886 extensions::Manifest::INTERNAL); |
| 902 EXPECT_TRUE(AddExtension(internal_extension_no_action.get())); | 887 EXPECT_TRUE(AddExtension(internal_extension_no_action.get())); |
| 903 EXPECT_EQ(4u, num_toolbar_items()); | 888 EXPECT_EQ(4u, num_toolbar_items()); |
| 904 EXPECT_TRUE(ModelHasActionForId(internal_extension_no_action->id())); | 889 EXPECT_TRUE(ModelHasActionForId(internal_extension_no_action->id())); |
| 905 } | 890 } |
| 906 | 891 |
| 907 // Test that hiding actions on the toolbar results in their removal from the | |
| 908 // model when the redesign switch is not enabled. | |
| 909 TEST_F(ToolbarActionsModelUnitTest, ActionsToolbarActionsVisibilityNoSwitch) { | |
| 910 extensions::FeatureSwitch::ScopedOverride enable_redesign( | |
| 911 extensions::FeatureSwitch::extension_action_redesign(), false); | |
| 912 Init(); | |
| 913 | |
| 914 extensions::ExtensionActionAPI* action_api = | |
| 915 extensions::ExtensionActionAPI::Get(profile()); | |
| 916 | |
| 917 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 918 // Sanity check: Order should start as A , B, C. | |
| 919 EXPECT_EQ(3u, num_toolbar_items()); | |
| 920 EXPECT_EQ(browser_action_a()->id(), GetActionIdAtIndex(0u)); | |
| 921 EXPECT_EQ(browser_action_b()->id(), GetActionIdAtIndex(1u)); | |
| 922 EXPECT_EQ(browser_action_c()->id(), GetActionIdAtIndex(2u)); | |
| 923 | |
| 924 // By default, all actions should be visible. | |
| 925 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_a()->id())); | |
| 926 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_b()->id())); | |
| 927 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_c()->id())); | |
| 928 | |
| 929 // Hiding an action should result in its removal from the toolbar. | |
| 930 action_api->SetBrowserActionVisibility(browser_action_b()->id(), false); | |
| 931 EXPECT_FALSE( | |
| 932 action_api->GetBrowserActionVisibility(browser_action_b()->id())); | |
| 933 // Thus, there should now only be two items on the toolbar - A and C. | |
| 934 EXPECT_EQ(2u, num_toolbar_items()); | |
| 935 EXPECT_EQ(browser_action_a()->id(), GetActionIdAtIndex(0u)); | |
| 936 EXPECT_EQ(browser_action_c()->id(), GetActionIdAtIndex(1u)); | |
| 937 | |
| 938 // Resetting the visibility to 'true' should result in the extension being | |
| 939 // added back at its original position. | |
| 940 action_api->SetBrowserActionVisibility(browser_action_b()->id(), true); | |
| 941 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_b()->id())); | |
| 942 // So the toolbar order should be A, B, C. | |
| 943 EXPECT_EQ(3u, num_toolbar_items()); | |
| 944 EXPECT_EQ(browser_action_a()->id(), GetActionIdAtIndex(0u)); | |
| 945 EXPECT_EQ(browser_action_b()->id(), GetActionIdAtIndex(1u)); | |
| 946 EXPECT_EQ(browser_action_c()->id(), GetActionIdAtIndex(2u)); | |
| 947 } | |
| 948 | |
| 949 TEST_F(ToolbarActionsModelUnitTest, ActionsToolbarIncognitoModeTest) { | 892 TEST_F(ToolbarActionsModelUnitTest, ActionsToolbarIncognitoModeTest) { |
| 950 Init(); | 893 Init(); |
| 951 ASSERT_TRUE(AddBrowserActionExtensions()); | 894 ASSERT_TRUE(AddBrowserActionExtensions()); |
| 952 | 895 |
| 953 // Give two extensions incognito access. | 896 // Give two extensions incognito access. |
| 954 // Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of | 897 // Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of |
| 955 // util::SetIsIncognitoEnabled because the latter tries to reload the | 898 // util::SetIsIncognitoEnabled because the latter tries to reload the |
| 956 // extension, which requries a filepath associated with the extension (and, | 899 // extension, which requries a filepath associated with the extension (and, |
| 957 // for this test, reloading the extension is irrelevant to us). | 900 // for this test, reloading the extension is irrelevant to us). |
| 958 extensions::ExtensionPrefs* extension_prefs = | 901 extensions::ExtensionPrefs* extension_prefs = |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1113 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); | 1056 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); |
| 1114 EXPECT_EQ(extension_a, GetActionIdAtIndex(0u, incognito_model)); | 1057 EXPECT_EQ(extension_a, GetActionIdAtIndex(0u, incognito_model)); |
| 1115 EXPECT_EQ(extension_b, GetActionIdAtIndex(1u, incognito_model)); | 1058 EXPECT_EQ(extension_b, GetActionIdAtIndex(1u, incognito_model)); |
| 1116 EXPECT_EQ(1u, incognito_model->visible_icon_count()); | 1059 EXPECT_EQ(1u, incognito_model->visible_icon_count()); |
| 1117 } | 1060 } |
| 1118 | 1061 |
| 1119 // Test that hiding actions on the toolbar results in sending them to the | 1062 // Test that hiding actions on the toolbar results in sending them to the |
| 1120 // overflow menu when the redesign switch is enabled. | 1063 // overflow menu when the redesign switch is enabled. |
| 1121 TEST_F(ToolbarActionsModelUnitTest, | 1064 TEST_F(ToolbarActionsModelUnitTest, |
| 1122 ActionsToolbarActionsVisibilityWithSwitchAndComponentActions) { | 1065 ActionsToolbarActionsVisibilityWithSwitchAndComponentActions) { |
| 1123 extensions::FeatureSwitch::ScopedOverride enable_redesign( | |
| 1124 extensions::FeatureSwitch::extension_action_redesign(), true); | |
| 1125 Init(); | 1066 Init(); |
| 1126 | 1067 |
| 1127 // We choose to use all types of extensions here, since the misnamed | 1068 // We choose to use all types of extensions here, since the misnamed |
| 1128 // BrowserActionVisibility is now for toolbar visibility. | 1069 // BrowserActionVisibility is now for toolbar visibility. |
| 1129 ASSERT_TRUE(AddActionExtensions()); | 1070 ASSERT_TRUE(AddActionExtensions()); |
| 1130 | 1071 |
| 1131 // For readability, alias extensions A B C. | 1072 // For readability, alias extensions A B C. |
| 1132 const extensions::Extension* extension_a = browser_action(); | 1073 const extensions::Extension* extension_a = browser_action(); |
| 1133 const extensions::Extension* extension_b = page_action(); | 1074 const extensions::Extension* extension_b = page_action(); |
| 1134 const extensions::Extension* extension_c = no_action(); | 1075 const extensions::Extension* extension_c = no_action(); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1242 EXPECT_EQ(browser_action_c()->id(), GetActionIdAtIndex(1)); | 1183 EXPECT_EQ(browser_action_c()->id(), GetActionIdAtIndex(1)); |
| 1243 EXPECT_EQ(browser_action_a()->id(), GetActionIdAtIndex(2)); | 1184 EXPECT_EQ(browser_action_a()->id(), GetActionIdAtIndex(2)); |
| 1244 EXPECT_EQ(inserted_and_removed_difference, | 1185 EXPECT_EQ(inserted_and_removed_difference, |
| 1245 observer()->inserted_count() - observer()->removed_count()); | 1186 observer()->inserted_count() - observer()->removed_count()); |
| 1246 } | 1187 } |
| 1247 | 1188 |
| 1248 // Test various different reorderings, removals, and reinsertions of the | 1189 // Test various different reorderings, removals, and reinsertions of the |
| 1249 // toolbar with component actions. | 1190 // toolbar with component actions. |
| 1250 TEST_F(ToolbarActionsModelUnitTest, | 1191 TEST_F(ToolbarActionsModelUnitTest, |
| 1251 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) { | 1192 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) { |
| 1252 extensions::FeatureSwitch::ScopedOverride enable_redesign( | |
| 1253 extensions::FeatureSwitch::extension_action_redesign(), true); | |
| 1254 InitWithMockActionsFactory(); | 1193 InitWithMockActionsFactory(); |
| 1255 | 1194 |
| 1256 // One component action was added when the model was initialized. | 1195 // One component action was added when the model was initialized. |
| 1257 EXPECT_EQ(1u, num_toolbar_items()); | 1196 EXPECT_EQ(1u, num_toolbar_items()); |
| 1258 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u)); | 1197 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u)); |
| 1259 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id())); | 1198 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id())); |
| 1260 | 1199 |
| 1261 // Add the three browser action extensions. | 1200 // Add the three browser action extensions. |
| 1262 ASSERT_TRUE(AddBrowserActionExtensions()); | 1201 ASSERT_TRUE(AddBrowserActionExtensions()); |
| 1263 | 1202 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1464 InitializeEmptyExtensionService(); | 1403 InitializeEmptyExtensionService(); |
| 1465 ToolbarActionsModel* toolbar_model = extensions::extension_action_test_util:: | 1404 ToolbarActionsModel* toolbar_model = extensions::extension_action_test_util:: |
| 1466 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); | 1405 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); |
| 1467 ASSERT_FALSE(toolbar_model->actions_initialized()); | 1406 ASSERT_FALSE(toolbar_model->actions_initialized()); |
| 1468 | 1407 |
| 1469 // AddComponentAction() should be a no-op if actions_initialized() is false. | 1408 // AddComponentAction() should be a no-op if actions_initialized() is false. |
| 1470 toolbar_model->AddComponentAction(component_action_id()); | 1409 toolbar_model->AddComponentAction(component_action_id()); |
| 1471 EXPECT_EQ(0u, toolbar_model->toolbar_items().size()); | 1410 EXPECT_EQ(0u, toolbar_model->toolbar_items().size()); |
| 1472 EXPECT_FALSE(toolbar_model->HasComponentAction(component_action_id())); | 1411 EXPECT_FALSE(toolbar_model->HasComponentAction(component_action_id())); |
| 1473 } | 1412 } |
| OLD | NEW |