| 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 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 116 |
| 117 } // namespace | 117 } // namespace |
| 118 | 118 |
| 119 class ToolbarActionsModelUnitTest | 119 class ToolbarActionsModelUnitTest |
| 120 : public extensions::ExtensionServiceTestBase { | 120 : public extensions::ExtensionServiceTestBase { |
| 121 public: | 121 public: |
| 122 ToolbarActionsModelUnitTest() {} | 122 ToolbarActionsModelUnitTest() {} |
| 123 ~ToolbarActionsModelUnitTest() override {} | 123 ~ToolbarActionsModelUnitTest() override {} |
| 124 | 124 |
| 125 protected: | 125 protected: |
| 126 // Initialize the ExtensionService, ToolbarActionsModel, and | 126 // Initialize the ExtensionService, ToolbarActionsModel, and ExtensionSystem. |
| 127 // ExtensionSystem. | |
| 128 void Init(); | 127 void Init(); |
| 129 | 128 |
| 129 // Initializes the ExtensionService, ToolbarActionsModel, and ExtensionSystem, |
| 130 // making ToolbarActionsModel use a MockComponentToolbarActionsFactory. |
| 131 void InitWithMockActionsFactory(); |
| 132 |
| 130 void TearDown() override; | 133 void TearDown() override; |
| 131 | 134 |
| 132 // Adds or removes the given |extension| and verify success. | 135 // Adds or removes the given |extension| and verify success. |
| 133 testing::AssertionResult AddExtension( | 136 testing::AssertionResult AddExtension( |
| 134 const scoped_refptr<const extensions::Extension>& extension) | 137 const scoped_refptr<const extensions::Extension>& extension) |
| 135 WARN_UNUSED_RESULT; | 138 WARN_UNUSED_RESULT; |
| 136 testing::AssertionResult RemoveExtension( | 139 testing::AssertionResult RemoveExtension( |
| 137 const scoped_refptr<const extensions::Extension>& extension) | 140 const scoped_refptr<const extensions::Extension>& extension) |
| 138 WARN_UNUSED_RESULT; | 141 WARN_UNUSED_RESULT; |
| 139 | 142 |
| 140 // Adds three extensions, all with browser actions. | 143 // Adds three extensions, all with browser actions. |
| 141 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; | 144 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; |
| 142 | 145 |
| 143 // Adds three extensions, one each for browser action, page action, and no | 146 // Adds three extensions, one each for browser action, page action, and no |
| 144 // action, and are added in that order. | 147 // action, and are added in that order. |
| 145 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; | 148 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; |
| 146 | 149 |
| 147 // Returns the action's id at the given index in the toolbar model, or empty | 150 // Returns the action's id at the given index in the toolbar model, or empty |
| 148 // if one does not exist. | 151 // if one does not exist. |
| 149 // If |model| is specified, it is used. Otherwise, this defaults to | 152 // If |model| is specified, it is used. Otherwise, this defaults to |
| 150 // |toolbar_model_|. | 153 // |toolbar_model_|. |
| 151 const std::string GetActionIdAtIndex(size_t index, | 154 const std::string GetActionIdAtIndex(size_t index, |
| 152 const ToolbarActionsModel* model) const; | 155 const ToolbarActionsModel* model) const; |
| 153 const std::string GetActionIdAtIndex(size_t index) const; | 156 const std::string GetActionIdAtIndex(size_t index) const; |
| 154 | 157 |
| 155 // Returns true if the |toobar_model_| has an action with the given |id|. | 158 // Returns true if the |toobar_model_| has an action with the given |id|. |
| 156 bool ModelHasActionForId(const std::string& id) const; | 159 bool ModelHasActionForId(const std::string& id) const; |
| 157 | 160 |
| 158 void SetMockActionsFactory(MockComponentToolbarActionsFactory* factory); | |
| 159 | |
| 160 ToolbarActionsModel* toolbar_model() { return toolbar_model_; } | 161 ToolbarActionsModel* toolbar_model() { return toolbar_model_; } |
| 161 | 162 |
| 162 const ToolbarActionsModelTestObserver* observer() const { | 163 const ToolbarActionsModelTestObserver* observer() const { |
| 163 return model_observer_.get(); | 164 return model_observer_.get(); |
| 164 } | 165 } |
| 165 size_t num_toolbar_items() const { | 166 size_t num_toolbar_items() const { |
| 166 return toolbar_model_->toolbar_items().size(); | 167 return toolbar_model_->toolbar_items().size(); |
| 167 } | 168 } |
| 168 const extensions::Extension* browser_action_a() const { | 169 const extensions::Extension* browser_action_a() const { |
| 169 return browser_action_a_.get(); | 170 return browser_action_a_.get(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 // Sample extensions with only browser actions. | 205 // Sample extensions with only browser actions. |
| 205 scoped_refptr<const extensions::Extension> browser_action_a_; | 206 scoped_refptr<const extensions::Extension> browser_action_a_; |
| 206 scoped_refptr<const extensions::Extension> browser_action_b_; | 207 scoped_refptr<const extensions::Extension> browser_action_b_; |
| 207 scoped_refptr<const extensions::Extension> browser_action_c_; | 208 scoped_refptr<const extensions::Extension> browser_action_c_; |
| 208 | 209 |
| 209 // Sample extensions with different kinds of actions. | 210 // Sample extensions with different kinds of actions. |
| 210 scoped_refptr<const extensions::Extension> browser_action_extension_; | 211 scoped_refptr<const extensions::Extension> browser_action_extension_; |
| 211 scoped_refptr<const extensions::Extension> page_action_extension_; | 212 scoped_refptr<const extensions::Extension> page_action_extension_; |
| 212 scoped_refptr<const extensions::Extension> no_action_extension_; | 213 scoped_refptr<const extensions::Extension> no_action_extension_; |
| 213 | 214 |
| 214 std::unique_ptr<MockComponentToolbarActionsFactory> mock_actions_factory_; | |
| 215 | |
| 216 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsModelUnitTest); | 215 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsModelUnitTest); |
| 217 }; | 216 }; |
| 218 | 217 |
| 219 void ToolbarActionsModelUnitTest::Init() { | 218 void ToolbarActionsModelUnitTest::Init() { |
| 220 InitializeEmptyExtensionService(); | 219 InitializeEmptyExtensionService(); |
| 221 toolbar_model_ = | 220 toolbar_model_ = |
| 222 extensions::extension_action_test_util::CreateToolbarModelForProfile( | 221 extensions::extension_action_test_util::CreateToolbarModelForProfile( |
| 223 profile()); | 222 profile()); |
| 224 model_observer_.reset(new ToolbarActionsModelTestObserver(toolbar_model_)); | 223 model_observer_ = |
| 224 base::MakeUnique<ToolbarActionsModelTestObserver>(toolbar_model_); |
| 225 } |
| 226 |
| 227 void ToolbarActionsModelUnitTest::InitWithMockActionsFactory() { |
| 228 InitializeEmptyExtensionService(); |
| 229 toolbar_model_ = extensions::extension_action_test_util:: |
| 230 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); |
| 231 toolbar_model_->SetMockActionsFactoryForTest( |
| 232 base::MakeUnique<MockComponentToolbarActionsFactory>(profile())); |
| 233 |
| 234 // Trigger ToolbarActionsModel::OnReady() after the actions factory has been |
| 235 // swapped out for a mock one. |
| 236 static_cast<extensions::TestExtensionSystem*>( |
| 237 extensions::ExtensionSystem::Get(profile())) |
| 238 ->SetReady(); |
| 239 base::RunLoop().RunUntilIdle(); |
| 240 |
| 241 model_observer_ = |
| 242 base::MakeUnique<ToolbarActionsModelTestObserver>(toolbar_model_); |
| 225 } | 243 } |
| 226 | 244 |
| 227 void ToolbarActionsModelUnitTest::TearDown() { | 245 void ToolbarActionsModelUnitTest::TearDown() { |
| 228 model_observer_.reset(); | 246 model_observer_.reset(); |
| 229 extensions::ExtensionServiceTestBase::TearDown(); | 247 extensions::ExtensionServiceTestBase::TearDown(); |
| 230 } | 248 } |
| 231 | 249 |
| 232 testing::AssertionResult ToolbarActionsModelUnitTest::AddExtension( | 250 testing::AssertionResult ToolbarActionsModelUnitTest::AddExtension( |
| 233 const scoped_refptr<const extensions::Extension>& extension) { | 251 const scoped_refptr<const extensions::Extension>& extension) { |
| 234 if (registry()->enabled_extensions().GetByID(extension->id())) { | 252 if (registry()->enabled_extensions().GetByID(extension->id())) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 for (extensions::ExtensionList::const_iterator iter = extensions.begin(); | 346 for (extensions::ExtensionList::const_iterator iter = extensions.begin(); |
| 329 iter != extensions.end(); ++iter) { | 347 iter != extensions.end(); ++iter) { |
| 330 if (!AddExtension(*iter)) { | 348 if (!AddExtension(*iter)) { |
| 331 return testing::AssertionFailure() << "Failed to install extension: " | 349 return testing::AssertionFailure() << "Failed to install extension: " |
| 332 << (*iter)->name(); | 350 << (*iter)->name(); |
| 333 } | 351 } |
| 334 } | 352 } |
| 335 return testing::AssertionSuccess(); | 353 return testing::AssertionSuccess(); |
| 336 } | 354 } |
| 337 | 355 |
| 338 void ToolbarActionsModelUnitTest::SetMockActionsFactory( | |
| 339 MockComponentToolbarActionsFactory* factory) { | |
| 340 mock_actions_factory_.reset(factory); | |
| 341 } | |
| 342 | |
| 343 // A basic test for component actions and extensions with browser actions | 356 // A basic test for component actions and extensions with browser actions |
| 344 // showing up in the toolbar. | 357 // showing up in the toolbar. |
| 345 TEST_F(ToolbarActionsModelUnitTest, BasicToolbarActionsModelTest) { | 358 TEST_F(ToolbarActionsModelUnitTest, BasicToolbarActionsModelTest) { |
| 346 Init(); | 359 Init(); |
| 347 | 360 |
| 348 // Load an extension with a browser action. | 361 // Load an extension with a browser action. |
| 349 scoped_refptr<const extensions::Extension> extension = | 362 scoped_refptr<const extensions::Extension> extension = |
| 350 extensions::extension_action_test_util::CreateActionExtension( | 363 extensions::extension_action_test_util::CreateActionExtension( |
| 351 "browser_action", | 364 "browser_action", |
| 352 extensions::extension_action_test_util::BROWSER_ACTION); | 365 extensions::extension_action_test_util::BROWSER_ACTION); |
| (...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1231 EXPECT_EQ(inserted_and_removed_difference, | 1244 EXPECT_EQ(inserted_and_removed_difference, |
| 1232 observer()->inserted_count() - observer()->removed_count()); | 1245 observer()->inserted_count() - observer()->removed_count()); |
| 1233 } | 1246 } |
| 1234 | 1247 |
| 1235 // Test various different reorderings, removals, and reinsertions of the | 1248 // Test various different reorderings, removals, and reinsertions of the |
| 1236 // toolbar with component actions. | 1249 // toolbar with component actions. |
| 1237 TEST_F(ToolbarActionsModelUnitTest, | 1250 TEST_F(ToolbarActionsModelUnitTest, |
| 1238 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) { | 1251 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) { |
| 1239 extensions::FeatureSwitch::ScopedOverride enable_redesign( | 1252 extensions::FeatureSwitch::ScopedOverride enable_redesign( |
| 1240 extensions::FeatureSwitch::extension_action_redesign(), true); | 1253 extensions::FeatureSwitch::extension_action_redesign(), true); |
| 1241 SetMockActionsFactory(new MockComponentToolbarActionsFactory(nullptr)); | 1254 InitWithMockActionsFactory(); |
| 1242 Init(); | |
| 1243 | 1255 |
| 1244 // One component action was added when the model was initialized. | 1256 // One component action was added when the model was initialized. |
| 1245 EXPECT_EQ(1u, num_toolbar_items()); | 1257 EXPECT_EQ(1u, num_toolbar_items()); |
| 1246 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u)); | 1258 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u)); |
| 1247 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id())); | 1259 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id())); |
| 1248 | 1260 |
| 1249 // Add the three browser action extensions. | 1261 // Add the three browser action extensions. |
| 1250 ASSERT_TRUE(AddBrowserActionExtensions()); | 1262 ASSERT_TRUE(AddBrowserActionExtensions()); |
| 1251 | 1263 |
| 1252 // Verify the four actions are in the model in the proper order. | 1264 // Verify the four actions are in the model in the proper order. |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1452 InitializeEmptyExtensionService(); | 1464 InitializeEmptyExtensionService(); |
| 1453 ToolbarActionsModel* toolbar_model = extensions::extension_action_test_util:: | 1465 ToolbarActionsModel* toolbar_model = extensions::extension_action_test_util:: |
| 1454 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); | 1466 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); |
| 1455 ASSERT_FALSE(toolbar_model->actions_initialized()); | 1467 ASSERT_FALSE(toolbar_model->actions_initialized()); |
| 1456 | 1468 |
| 1457 // AddComponentAction() should be a no-op if actions_initialized() is false. | 1469 // AddComponentAction() should be a no-op if actions_initialized() is false. |
| 1458 toolbar_model->AddComponentAction(component_action_id()); | 1470 toolbar_model->AddComponentAction(component_action_id()); |
| 1459 EXPECT_EQ(0u, toolbar_model->toolbar_items().size()); | 1471 EXPECT_EQ(0u, toolbar_model->toolbar_items().size()); |
| 1460 EXPECT_FALSE(toolbar_model->HasComponentAction(component_action_id())); | 1472 EXPECT_FALSE(toolbar_model->HasComponentAction(component_action_id())); |
| 1461 } | 1473 } |
| OLD | NEW |