| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 #include <vector> | |
| 7 | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/extensions/component_migration_helper.h" | |
| 11 #include "chrome/browser/extensions/extension_action_test_util.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/extensions/extension_service_test_base.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "components/prefs/pref_registry_simple.h" | |
| 17 #include "extensions/browser/extension_registry.h" | |
| 18 #include "extensions/common/feature_switch.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 using ::testing::Return; | |
| 23 using ::testing::StrictMock; | |
| 24 | |
| 25 namespace extensions { | |
| 26 namespace { | |
| 27 | |
| 28 const char kTestActionId[] = "toolbar-action"; | |
| 29 | |
| 30 class MockComponentActionDelegate | |
| 31 : public ComponentMigrationHelper::ComponentActionDelegate { | |
| 32 public: | |
| 33 MOCK_METHOD1(AddComponentAction, void(const std::string&)); | |
| 34 MOCK_METHOD1(RemoveComponentAction, void(const std::string&)); | |
| 35 MOCK_CONST_METHOD1(HasComponentAction, bool(const std::string&)); | |
| 36 }; | |
| 37 | |
| 38 class MockComponentMigrationHelper : public ComponentMigrationHelper { | |
| 39 public: | |
| 40 MockComponentMigrationHelper(Profile* profile, | |
| 41 ComponentActionDelegate* delegate) | |
| 42 : ComponentMigrationHelper(profile, delegate) {} | |
| 43 | |
| 44 ~MockComponentMigrationHelper() override{}; | |
| 45 | |
| 46 void SetTestComponentActionPref(bool enabled) { | |
| 47 SetComponentActionPref(kTestActionId, enabled); | |
| 48 } | |
| 49 | |
| 50 void EnableTestFeature() { enabled_actions_.insert(kTestActionId); } | |
| 51 | |
| 52 void DisableTestFeature() { enabled_actions_.erase(kTestActionId); } | |
| 53 }; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 class ComponentMigrationHelperTest : public ExtensionServiceTestBase { | |
| 58 protected: | |
| 59 ComponentMigrationHelperTest() {} | |
| 60 ~ComponentMigrationHelperTest() override {} | |
| 61 | |
| 62 void SetUp() override { | |
| 63 extension_action_redesign_.reset(new FeatureSwitch::ScopedOverride( | |
| 64 FeatureSwitch::extension_action_redesign(), | |
| 65 FeatureSwitch::OVERRIDE_ENABLED)); | |
| 66 | |
| 67 ExtensionServiceTestBase::SetUp(); | |
| 68 InitializeEmptyExtensionService(); | |
| 69 | |
| 70 migrated_extension_a_ = extension_action_test_util::CreateActionExtension( | |
| 71 "migrated_browser_action_a", | |
| 72 extension_action_test_util::BROWSER_ACTION); | |
| 73 | |
| 74 migrated_extension_b_ = extension_action_test_util::CreateActionExtension( | |
| 75 "migrated_browser_action_b", | |
| 76 extension_action_test_util::BROWSER_ACTION); | |
| 77 | |
| 78 unregistered_extension_ = extension_action_test_util::CreateActionExtension( | |
| 79 "unregistered_extension", extension_action_test_util::BROWSER_ACTION); | |
| 80 | |
| 81 mock_helper_.reset(new StrictMock<MockComponentMigrationHelper>( | |
| 82 profile(), &mock_delegate_)); | |
| 83 mock_helper_->Register(kTestActionId, migrated_extension_a_->id()); | |
| 84 mock_helper_->Register(kTestActionId, migrated_extension_b_->id()); | |
| 85 } | |
| 86 | |
| 87 bool IsTestComponentActionEnabled() { | |
| 88 const base::DictionaryValue* migration_pref = | |
| 89 profile()->GetPrefs()->GetDictionary( | |
| 90 ::prefs::kToolbarMigratedComponentActionStatus); | |
| 91 if (!migration_pref->HasKey(kTestActionId)) | |
| 92 return false; | |
| 93 bool enable_value = false; | |
| 94 CHECK(migration_pref->GetBoolean(kTestActionId, &enable_value)); | |
| 95 return enable_value; | |
| 96 } | |
| 97 | |
| 98 StrictMock<MockComponentActionDelegate> mock_delegate_; | |
| 99 std::unique_ptr<StrictMock<MockComponentMigrationHelper>> mock_helper_; | |
| 100 std::unique_ptr<FeatureSwitch::ScopedOverride> extension_action_redesign_; | |
| 101 | |
| 102 // Migrated extensions with browser actions. | |
| 103 scoped_refptr<const Extension> migrated_extension_a_; | |
| 104 scoped_refptr<const Extension> migrated_extension_b_; | |
| 105 // An extension that is not migrated. | |
| 106 scoped_refptr<const Extension> unregistered_extension_; | |
| 107 | |
| 108 private: | |
| 109 DISALLOW_COPY_AND_ASSIGN(ComponentMigrationHelperTest); | |
| 110 }; | |
| 111 | |
| 112 TEST_F(ComponentMigrationHelperTest, FeatureEnabledWhenExtensionInstalled) { | |
| 113 service()->AddExtension(migrated_extension_a_.get()); | |
| 114 service()->AddExtension(migrated_extension_b_.get()); | |
| 115 | |
| 116 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 117 .WillOnce(Return(false)); | |
| 118 EXPECT_CALL(mock_delegate_, AddComponentAction(kTestActionId)); | |
| 119 | |
| 120 mock_helper_->OnFeatureEnabled(kTestActionId); | |
| 121 EXPECT_TRUE(IsTestComponentActionEnabled()); | |
| 122 EXPECT_FALSE( | |
| 123 registry()->enabled_extensions().Contains(migrated_extension_a_->id())); | |
| 124 EXPECT_FALSE( | |
| 125 registry()->enabled_extensions().Contains(migrated_extension_b_->id())); | |
| 126 } | |
| 127 | |
| 128 TEST_F(ComponentMigrationHelperTest, FeatureEnabledWithNoPref) { | |
| 129 mock_helper_->OnFeatureEnabled(kTestActionId); | |
| 130 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 131 } | |
| 132 | |
| 133 TEST_F(ComponentMigrationHelperTest, FeatureEnabledWithPrefFalse) { | |
| 134 mock_helper_->SetTestComponentActionPref(false); | |
| 135 | |
| 136 mock_helper_->OnFeatureEnabled(kTestActionId); | |
| 137 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(ComponentMigrationHelperTest, FeatureEnabledWithPrefTrue) { | |
| 141 mock_helper_->SetTestComponentActionPref(true); | |
| 142 | |
| 143 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 144 .WillOnce(Return(false)); | |
| 145 EXPECT_CALL(mock_delegate_, AddComponentAction(kTestActionId)); | |
| 146 | |
| 147 mock_helper_->OnFeatureEnabled(kTestActionId); | |
| 148 EXPECT_TRUE(IsTestComponentActionEnabled()); | |
| 149 } | |
| 150 | |
| 151 TEST_F(ComponentMigrationHelperTest, FeatureDisabledWithAction) { | |
| 152 mock_helper_->EnableTestFeature(); | |
| 153 | |
| 154 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 155 .WillOnce(Return(true)); | |
| 156 EXPECT_CALL(mock_delegate_, RemoveComponentAction(kTestActionId)); | |
| 157 | |
| 158 mock_helper_->OnFeatureDisabled(kTestActionId); | |
| 159 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 160 } | |
| 161 | |
| 162 TEST_F(ComponentMigrationHelperTest, InstallWithFeatureEnabled) { | |
| 163 mock_helper_->EnableTestFeature(); | |
| 164 | |
| 165 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 166 .WillOnce(Return(false)); | |
| 167 EXPECT_CALL(mock_delegate_, AddComponentAction(kTestActionId)); | |
| 168 | |
| 169 service()->AddExtension(migrated_extension_a_.get()); | |
| 170 // The test framework does not call OnExtensionReady :-/ | |
| 171 mock_helper_->OnExtensionReady(browser_context(), | |
| 172 migrated_extension_a_.get()); | |
| 173 | |
| 174 EXPECT_TRUE(IsTestComponentActionEnabled()); | |
| 175 EXPECT_FALSE( | |
| 176 registry()->enabled_extensions().Contains(migrated_extension_a_->id())); | |
| 177 } | |
| 178 | |
| 179 TEST_F(ComponentMigrationHelperTest, InstallWithFeatureDisabled) { | |
| 180 mock_helper_->DisableTestFeature(); | |
| 181 service()->AddExtension(migrated_extension_a_.get()); | |
| 182 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 183 EXPECT_TRUE( | |
| 184 registry()->enabled_extensions().Contains(migrated_extension_a_->id())); | |
| 185 } | |
| 186 | |
| 187 TEST_F(ComponentMigrationHelperTest, InstallUnregisteredExtension) { | |
| 188 service()->AddExtension(unregistered_extension_.get()); | |
| 189 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 190 EXPECT_TRUE( | |
| 191 registry()->enabled_extensions().Contains(unregistered_extension_->id())); | |
| 192 } | |
| 193 | |
| 194 TEST_F(ComponentMigrationHelperTest, RemoveComponentAction) { | |
| 195 mock_helper_->SetTestComponentActionPref(true); | |
| 196 | |
| 197 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 198 .WillOnce(Return(false)); | |
| 199 EXPECT_CALL(mock_delegate_, AddComponentAction(kTestActionId)); | |
| 200 | |
| 201 mock_helper_->OnFeatureEnabled(kTestActionId); | |
| 202 EXPECT_TRUE(IsTestComponentActionEnabled()); | |
| 203 | |
| 204 EXPECT_CALL(mock_delegate_, HasComponentAction(kTestActionId)) | |
| 205 .WillOnce(Return(true)); | |
| 206 EXPECT_CALL(mock_delegate_, RemoveComponentAction(kTestActionId)); | |
| 207 | |
| 208 mock_helper_->OnActionRemoved(kTestActionId); | |
| 209 EXPECT_FALSE(IsTestComponentActionEnabled()); | |
| 210 } | |
| 211 | |
| 212 } // namespace extensions | |
| OLD | NEW |