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

Side by Side Diff: chrome/browser/ui/toolbar/toolbar_actions_model_unittest.cc

Issue 2613713005: Make ToolbarActionsModel own ComponentToolbarActionsFactory (Closed)
Patch Set: Created 3 years, 11 months 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 "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
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
127 // ExtensionSystem.
128 void Init();
129
130 enum class MigrationStatus { 126 enum class MigrationStatus {
131 // The feature is enabled without any extension to migrate. 127 // The feature is enabled without any extension to migrate.
132 FEATURE_ENABLED_NO_EXTENSION, 128 FEATURE_ENABLED_NO_EXTENSION,
133 // The feature is enabled and the user has installed an extension. 129 // The feature is enabled and the user has installed an extension.
134 FEATURE_ENABLED_EXTENSION_INSTALLED, 130 FEATURE_ENABLED_EXTENSION_INSTALLED,
135 // Feature is enabled with extension and a pref of false. 131 // Feature is enabled with extension and a pref of false.
136 FEATURE_ENABLED_WITH_PREF_FALSE, 132 FEATURE_ENABLED_WITH_PREF_FALSE,
137 // Feature is enabled with extension and a pref of true. 133 // Feature is enabled with extension and a pref of true.
138 FEATURE_ENABLED_WITH_PREF_TRUE, 134 FEATURE_ENABLED_WITH_PREF_TRUE,
139 // The feature is now disabled after previously being enabled, and the user 135 // The feature is now disabled after previously being enabled, and the user
140 // has a pref reflecting a previous migration. 136 // has a pref reflecting a previous migration.
141 FEATURE_DISABLED_WITH_PREF_TRUE, 137 FEATURE_DISABLED_WITH_PREF_TRUE,
142 }; 138 };
143 139
144 // Initialize the ExtensionService, ToolbarActionsModel, and ExtensionSystem, 140 // Initializes the ExtensionService, ToolbarActionsModel, and ExtensionSystem.
141 void Init();
142
143 // Initializes the ExtensionService, ToolbarActionsModel, and ExtensionSystem,
144 // making ToolbarActionsModel use a MockComponentToolbarActionsFactory.
145 void InitWithMockActionsFactory();
146
147 // Initializes the ExtensionService, ToolbarActionsModel, and ExtensionSystem,
145 // and an action extension to migrate to a component. |migration_status| 148 // and an action extension to migrate to a component. |migration_status|
146 // is used to configure the user's initial migration status. 149 // is used to configure the user's initial migration status.
147 void InitForMigrationTest(MigrationStatus migration_status); 150 void InitForMigrationTest(MigrationStatus migration_status);
148 151
152 // Initializes ExtensionSystem and triggers ToolbarActionsModel::OnReady() to
153 // be called with |mock_actions_factory| in use. The ownership of the actions
154 // factory gets transferred to |toolbar_model_|.
155 void ReadyWithMockActionsFactory(
156 MockComponentToolbarActionsFactory* mock_actions_factory);
157
149 void TearDown() override; 158 void TearDown() override;
150 159
151 // Adds or removes the given |extension| and verify success. 160 // Adds or removes the given |extension| and verify success.
152 testing::AssertionResult AddExtension( 161 testing::AssertionResult AddExtension(
153 const scoped_refptr<const extensions::Extension>& extension) 162 const scoped_refptr<const extensions::Extension>& extension)
154 WARN_UNUSED_RESULT; 163 WARN_UNUSED_RESULT;
155 testing::AssertionResult RemoveExtension( 164 testing::AssertionResult RemoveExtension(
156 const scoped_refptr<const extensions::Extension>& extension) 165 const scoped_refptr<const extensions::Extension>& extension)
157 WARN_UNUSED_RESULT; 166 WARN_UNUSED_RESULT;
158 167
159 // Adds three extensions, all with browser actions. 168 // Adds three extensions, all with browser actions.
160 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; 169 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT;
161 170
162 // Adds three extensions, one each for browser action, page action, and no 171 // Adds three extensions, one each for browser action, page action, and no
163 // action, and are added in that order. 172 // action, and are added in that order.
164 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; 173 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT;
165 174
166 // Creates an extension that is to be migrated to a component action. 175 // Creates an extension that is to be migrated to a component action.
167 void CreateMigratedActionExtension(); 176 void CreateMigratedActionExtension(
177 MockComponentToolbarActionsFactory* mock_actions_factory);
168 178
169 // Returns the action's id at the given index in the toolbar model, or empty 179 // Returns the action's id at the given index in the toolbar model, or empty
170 // if one does not exist. 180 // if one does not exist.
171 // If |model| is specified, it is used. Otherwise, this defaults to 181 // If |model| is specified, it is used. Otherwise, this defaults to
172 // |toolbar_model_|. 182 // |toolbar_model_|.
173 const std::string GetActionIdAtIndex(size_t index, 183 const std::string GetActionIdAtIndex(size_t index,
174 const ToolbarActionsModel* model) const; 184 const ToolbarActionsModel* model) const;
175 const std::string GetActionIdAtIndex(size_t index) const; 185 const std::string GetActionIdAtIndex(size_t index) const;
176 186
177 // Returns true if the |toobar_model_| has an action with the given |id|. 187 // Returns true if the |toobar_model_| has an action with the given |id|.
178 bool ModelHasActionForId(const std::string& id) const; 188 bool ModelHasActionForId(const std::string& id) const;
179 189
180 void SetMockActionsFactory(MockComponentToolbarActionsFactory* factory);
181
182 ToolbarActionsModel* toolbar_model() { return toolbar_model_; } 190 ToolbarActionsModel* toolbar_model() { return toolbar_model_; }
183 191
184 const ToolbarActionsModelTestObserver* observer() const { 192 const ToolbarActionsModelTestObserver* observer() const {
185 return model_observer_.get(); 193 return model_observer_.get();
186 } 194 }
187 size_t num_toolbar_items() const { 195 size_t num_toolbar_items() const {
188 return toolbar_model_->toolbar_items().size(); 196 return toolbar_model_->toolbar_items().size();
189 } 197 }
190 const extensions::Extension* browser_action_a() const { 198 const extensions::Extension* browser_action_a() const {
191 return browser_action_a_.get(); 199 return browser_action_a_.get();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 scoped_refptr<const extensions::Extension> browser_action_a_; 238 scoped_refptr<const extensions::Extension> browser_action_a_;
231 scoped_refptr<const extensions::Extension> browser_action_b_; 239 scoped_refptr<const extensions::Extension> browser_action_b_;
232 scoped_refptr<const extensions::Extension> browser_action_c_; 240 scoped_refptr<const extensions::Extension> browser_action_c_;
233 scoped_refptr<const extensions::Extension> browser_action_migrated_; 241 scoped_refptr<const extensions::Extension> browser_action_migrated_;
234 242
235 // Sample extensions with different kinds of actions. 243 // Sample extensions with different kinds of actions.
236 scoped_refptr<const extensions::Extension> browser_action_extension_; 244 scoped_refptr<const extensions::Extension> browser_action_extension_;
237 scoped_refptr<const extensions::Extension> page_action_extension_; 245 scoped_refptr<const extensions::Extension> page_action_extension_;
238 scoped_refptr<const extensions::Extension> no_action_extension_; 246 scoped_refptr<const extensions::Extension> no_action_extension_;
239 247
240 std::unique_ptr<MockComponentToolbarActionsFactory> mock_actions_factory_;
241
242 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsModelUnitTest); 248 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsModelUnitTest);
243 }; 249 };
244 250
245 void ToolbarActionsModelUnitTest::Init() { 251 void ToolbarActionsModelUnitTest::Init() {
246 InitializeEmptyExtensionService(); 252 InitializeEmptyExtensionService();
247 toolbar_model_ = 253 toolbar_model_ =
248 extensions::extension_action_test_util::CreateToolbarModelForProfile( 254 extensions::extension_action_test_util::CreateToolbarModelForProfile(
249 profile()); 255 profile());
250 model_observer_.reset(new ToolbarActionsModelTestObserver(toolbar_model_)); 256 model_observer_.reset(new ToolbarActionsModelTestObserver(toolbar_model_));
251 } 257 }
252 258
259 void ToolbarActionsModelUnitTest::InitWithMockActionsFactory() {
msw 2017/01/10 23:23:59 nit: this is called once, maybe inline the functio
takumif 2017/02/17 03:22:52 The test that calls this was already very long, so
260 InitializeEmptyExtensionService();
261 toolbar_model_ = extensions::extension_action_test_util::
262 CreateToolbarModelForProfileWithoutWaitingForReady(profile());
263 MockComponentToolbarActionsFactory* mock_actions_factory =
264 new MockComponentToolbarActionsFactory(profile(), toolbar_model_);
msw 2017/01/10 23:23:59 Use base::MakeUnique
takumif 2017/02/17 03:22:52 Done.
265 ReadyWithMockActionsFactory(mock_actions_factory);
266 }
267
268 void ToolbarActionsModelUnitTest::ReadyWithMockActionsFactory(
269 MockComponentToolbarActionsFactory* mock_actions_factory) {
msw 2017/01/10 23:23:59 Use unique_ptr for ownership passing
takumif 2017/02/17 03:22:52 Done.
270 mock_actions_factory->RegisterComponentMigrations();
271 toolbar_model_->SetMockActionsFactory(mock_actions_factory);
272
273 // Trigger ToolbarActionsModel::OnReady() after the actions factory has been
274 // swapped out for a mock one.
275 static_cast<extensions::TestExtensionSystem*>(
276 extensions::ExtensionSystem::Get(profile()))
277 ->SetReady();
278 base::RunLoop().RunUntilIdle();
279
280 model_observer_.reset(new ToolbarActionsModelTestObserver(toolbar_model_));
281 }
282
253 void ToolbarActionsModelUnitTest::InitForMigrationTest( 283 void ToolbarActionsModelUnitTest::InitForMigrationTest(
254 MigrationStatus migration_status) { 284 MigrationStatus migration_status) {
255 InitializeEmptyExtensionService(); 285 InitializeEmptyExtensionService();
256 SetMockActionsFactory(new MockComponentToolbarActionsFactory(nullptr)); 286 toolbar_model_ = extensions::extension_action_test_util::
257 CreateMigratedActionExtension(); 287 CreateToolbarModelForProfileWithoutWaitingForReady(profile());
288 MockComponentToolbarActionsFactory* mock_actions_factory =
289 new MockComponentToolbarActionsFactory(profile(), toolbar_model_);
290 CreateMigratedActionExtension(mock_actions_factory);
258 291
259 { 292 {
260 DictionaryPrefUpdate update(profile()->GetPrefs(), 293 DictionaryPrefUpdate update(profile()->GetPrefs(),
261 ::prefs::kToolbarMigratedComponentActionStatus); 294 ::prefs::kToolbarMigratedComponentActionStatus);
262 switch (migration_status) { 295 switch (migration_status) {
263 case MigrationStatus::FEATURE_ENABLED_EXTENSION_INSTALLED: 296 case MigrationStatus::FEATURE_ENABLED_EXTENSION_INSTALLED:
264 mock_actions_factory_->set_migrated_feature_enabled(true); 297 mock_actions_factory->set_migrated_feature_enabled(true);
265 ASSERT_TRUE(AddExtension(browser_action_migrated())); 298 ASSERT_TRUE(AddExtension(browser_action_migrated()));
266 break; 299 break;
267 case MigrationStatus::FEATURE_ENABLED_NO_EXTENSION: 300 case MigrationStatus::FEATURE_ENABLED_NO_EXTENSION:
268 mock_actions_factory_->set_migrated_feature_enabled(true); 301 mock_actions_factory->set_migrated_feature_enabled(true);
269 break; 302 break;
270 case MigrationStatus::FEATURE_ENABLED_WITH_PREF_TRUE: 303 case MigrationStatus::FEATURE_ENABLED_WITH_PREF_TRUE:
271 mock_actions_factory_->set_migrated_feature_enabled(true); 304 mock_actions_factory->set_migrated_feature_enabled(true);
272 ASSERT_TRUE(AddExtension(browser_action_migrated())); 305 ASSERT_TRUE(AddExtension(browser_action_migrated()));
273 update->SetBoolean(component_action_id(), true); 306 update->SetBoolean(component_action_id(), true);
274 break; 307 break;
275 case MigrationStatus::FEATURE_ENABLED_WITH_PREF_FALSE: 308 case MigrationStatus::FEATURE_ENABLED_WITH_PREF_FALSE:
276 mock_actions_factory_->set_migrated_feature_enabled(true); 309 mock_actions_factory->set_migrated_feature_enabled(true);
277 ASSERT_TRUE(AddExtension(browser_action_migrated())); 310 ASSERT_TRUE(AddExtension(browser_action_migrated()));
278 update->SetBoolean(component_action_id(), false); 311 update->SetBoolean(component_action_id(), false);
279 break; 312 break;
280 case MigrationStatus::FEATURE_DISABLED_WITH_PREF_TRUE: 313 case MigrationStatus::FEATURE_DISABLED_WITH_PREF_TRUE:
281 mock_actions_factory_->set_migrated_feature_enabled(false); 314 mock_actions_factory->set_migrated_feature_enabled(false);
282 ASSERT_TRUE(AddExtension(browser_action_migrated())); 315 ASSERT_TRUE(AddExtension(browser_action_migrated()));
283 update->SetBoolean(component_action_id(), true); 316 update->SetBoolean(component_action_id(), true);
284 break; 317 break;
285 } 318 }
286 } 319 }
287 320
288 toolbar_model_ = 321 ReadyWithMockActionsFactory(mock_actions_factory);
289 extensions::extension_action_test_util::CreateToolbarModelForProfile(
290 profile());
291 model_observer_.reset(new ToolbarActionsModelTestObserver(toolbar_model_));
292 } 322 }
293 323
294 void ToolbarActionsModelUnitTest::TearDown() { 324 void ToolbarActionsModelUnitTest::TearDown() {
295 model_observer_.reset(); 325 model_observer_.reset();
296 extensions::ExtensionServiceTestBase::TearDown(); 326 extensions::ExtensionServiceTestBase::TearDown();
297 } 327 }
298 328
299 testing::AssertionResult ToolbarActionsModelUnitTest::AddExtension( 329 testing::AssertionResult ToolbarActionsModelUnitTest::AddExtension(
300 const scoped_refptr<const extensions::Extension>& extension) { 330 const scoped_refptr<const extensions::Extension>& extension) {
301 if (registry()->enabled_extensions().GetByID(extension->id())) { 331 if (registry()->enabled_extensions().GetByID(extension->id())) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 extensions::extension_action_test_util::BROWSER_ACTION); 391 extensions::extension_action_test_util::BROWSER_ACTION);
362 392
363 extensions::ExtensionList extensions; 393 extensions::ExtensionList extensions;
364 extensions.push_back(browser_action_a_); 394 extensions.push_back(browser_action_a_);
365 extensions.push_back(browser_action_b_); 395 extensions.push_back(browser_action_b_);
366 extensions.push_back(browser_action_c_); 396 extensions.push_back(browser_action_c_);
367 397
368 return AddAndVerifyExtensions(extensions); 398 return AddAndVerifyExtensions(extensions);
369 } 399 }
370 400
371 void ToolbarActionsModelUnitTest::CreateMigratedActionExtension() { 401 void ToolbarActionsModelUnitTest::CreateMigratedActionExtension(
402 MockComponentToolbarActionsFactory* mock_actions_factory) {
372 browser_action_migrated_ = 403 browser_action_migrated_ =
373 extensions::extension_action_test_util::CreateActionExtension( 404 extensions::extension_action_test_util::CreateActionExtension(
374 "browser_actionMigrated", 405 "browser_actionMigrated",
375 extensions::extension_action_test_util::BROWSER_ACTION); 406 extensions::extension_action_test_util::BROWSER_ACTION);
376 mock_actions_factory_->set_migrated_extension_id( 407 mock_actions_factory->set_migrated_extension_id(
377 browser_action_migrated_->id()); 408 browser_action_migrated_->id());
378 } 409 }
379 410
380 const std::string ToolbarActionsModelUnitTest::GetActionIdAtIndex( 411 const std::string ToolbarActionsModelUnitTest::GetActionIdAtIndex(
381 size_t index, 412 size_t index,
382 const ToolbarActionsModel* model) const { 413 const ToolbarActionsModel* model) const {
383 return index < model->toolbar_items().size() 414 return index < model->toolbar_items().size()
384 ? model->toolbar_items()[index].id 415 ? model->toolbar_items()[index].id
385 : std::string(); 416 : std::string();
386 } 417 }
(...skipping 17 matching lines...) Expand all
404 for (extensions::ExtensionList::const_iterator iter = extensions.begin(); 435 for (extensions::ExtensionList::const_iterator iter = extensions.begin();
405 iter != extensions.end(); ++iter) { 436 iter != extensions.end(); ++iter) {
406 if (!AddExtension(*iter)) { 437 if (!AddExtension(*iter)) {
407 return testing::AssertionFailure() << "Failed to install extension: " 438 return testing::AssertionFailure() << "Failed to install extension: "
408 << (*iter)->name(); 439 << (*iter)->name();
409 } 440 }
410 } 441 }
411 return testing::AssertionSuccess(); 442 return testing::AssertionSuccess();
412 } 443 }
413 444
414 void ToolbarActionsModelUnitTest::SetMockActionsFactory(
415 MockComponentToolbarActionsFactory* factory) {
416 mock_actions_factory_.reset(factory);
417 }
418
419 // A basic test for component actions and extensions with browser actions 445 // A basic test for component actions and extensions with browser actions
420 // showing up in the toolbar. 446 // showing up in the toolbar.
421 TEST_F(ToolbarActionsModelUnitTest, BasicToolbarActionsModelTest) { 447 TEST_F(ToolbarActionsModelUnitTest, BasicToolbarActionsModelTest) {
422 Init(); 448 Init();
423 449
424 // Load an extension with a browser action. 450 // Load an extension with a browser action.
425 scoped_refptr<const extensions::Extension> extension = 451 scoped_refptr<const extensions::Extension> extension =
426 extensions::extension_action_test_util::CreateActionExtension( 452 extensions::extension_action_test_util::CreateActionExtension(
427 "browser_action", 453 "browser_action",
428 extensions::extension_action_test_util::BROWSER_ACTION); 454 extensions::extension_action_test_util::BROWSER_ACTION);
(...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after
1307 EXPECT_EQ(inserted_and_removed_difference, 1333 EXPECT_EQ(inserted_and_removed_difference,
1308 observer()->inserted_count() - observer()->removed_count()); 1334 observer()->inserted_count() - observer()->removed_count());
1309 } 1335 }
1310 1336
1311 // Test various different reorderings, removals, and reinsertions of the 1337 // Test various different reorderings, removals, and reinsertions of the
1312 // toolbar with component actions. 1338 // toolbar with component actions.
1313 TEST_F(ToolbarActionsModelUnitTest, 1339 TEST_F(ToolbarActionsModelUnitTest,
1314 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) { 1340 ActionsToolbarReorderAndReinsertWithSwitchAndComponentActions) {
1315 extensions::FeatureSwitch::ScopedOverride enable_redesign( 1341 extensions::FeatureSwitch::ScopedOverride enable_redesign(
1316 extensions::FeatureSwitch::extension_action_redesign(), true); 1342 extensions::FeatureSwitch::extension_action_redesign(), true);
1317 SetMockActionsFactory(new MockComponentToolbarActionsFactory(nullptr)); 1343 InitWithMockActionsFactory();
1318 Init();
1319 1344
1320 // One component action was added when the model was initialized. 1345 // One component action was added when the model was initialized.
1321 EXPECT_EQ(1u, num_toolbar_items()); 1346 EXPECT_EQ(1u, num_toolbar_items());
1322 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u)); 1347 EXPECT_EQ(component_action_id(), GetActionIdAtIndex(0u));
1323 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id())); 1348 EXPECT_TRUE(toolbar_model()->HasComponentAction(component_action_id()));
1324 1349
1325 // Add the three browser action extensions. 1350 // Add the three browser action extensions.
1326 ASSERT_TRUE(AddBrowserActionExtensions()); 1351 ASSERT_TRUE(AddBrowserActionExtensions());
1327 1352
1328 // Verify the four actions are in the model in the proper order. 1353 // Verify the four actions are in the model in the proper order.
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
1585 } 1610 }
1586 1611
1587 TEST_F(ToolbarActionsModelUnitTest, 1612 TEST_F(ToolbarActionsModelUnitTest,
1588 MigrationToExtensionWithoutExtensionActionRedesign) { 1613 MigrationToExtensionWithoutExtensionActionRedesign) {
1589 InitForMigrationTest(MigrationStatus::FEATURE_DISABLED_WITH_PREF_TRUE); 1614 InitForMigrationTest(MigrationStatus::FEATURE_DISABLED_WITH_PREF_TRUE);
1590 1615
1591 // Initialization re-enables the extension. 1616 // Initialization re-enables the extension.
1592 EXPECT_EQ(1u, num_toolbar_items()); 1617 EXPECT_EQ(1u, num_toolbar_items());
1593 EXPECT_EQ(browser_action_migrated()->id(), GetActionIdAtIndex(0u)); 1618 EXPECT_EQ(browser_action_migrated()->id(), GetActionIdAtIndex(0u));
1594 } 1619 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698