| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/files/file_util.h" | |
| 6 #include "base/macros.h" | |
| 7 #include "base/memory/ref_counted.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | |
| 11 #include "chrome/browser/extensions/extension_action_manager.h" | |
| 12 #include "chrome/browser/extensions/extension_action_test_util.h" | |
| 13 #include "chrome/browser/extensions/extension_service.h" | |
| 14 #include "chrome/browser/extensions/extension_service_test_base.h" | |
| 15 #include "chrome/browser/extensions/extension_toolbar_model.h" | |
| 16 #include "chrome/browser/extensions/extension_util.h" | |
| 17 #include "chrome/browser/extensions/test_extension_dir.h" | |
| 18 #include "chrome/browser/extensions/test_extension_system.h" | |
| 19 #include "chrome/browser/extensions/unpacked_installer.h" | |
| 20 #include "chrome/browser/profiles/profile.h" | |
| 21 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 22 #include "chrome/browser/ui/extensions/extension_toolbar_icon_surfacing_bubble_d
elegate.h" | |
| 23 #include "chrome/common/extensions/api/extension_action/action_info.h" | |
| 24 #include "components/crx_file/id_util.h" | |
| 25 #include "content/public/test/test_renderer_host.h" | |
| 26 #include "content/public/test/web_contents_tester.h" | |
| 27 #include "extensions/browser/extension_prefs.h" | |
| 28 #include "extensions/browser/extension_registry.h" | |
| 29 #include "extensions/browser/extension_system.h" | |
| 30 #include "extensions/browser/pref_names.h" | |
| 31 #include "extensions/browser/test_extension_registry_observer.h" | |
| 32 #include "extensions/common/extension.h" | |
| 33 #include "extensions/common/extension_builder.h" | |
| 34 #include "extensions/common/feature_switch.h" | |
| 35 #include "extensions/common/value_builder.h" | |
| 36 | |
| 37 namespace extensions { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 // A simple observer that tracks the number of times certain events occur. | |
| 42 class ExtensionToolbarModelTestObserver | |
| 43 : public ExtensionToolbarModel::Observer { | |
| 44 public: | |
| 45 explicit ExtensionToolbarModelTestObserver(ExtensionToolbarModel* model); | |
| 46 ~ExtensionToolbarModelTestObserver() override; | |
| 47 | |
| 48 size_t inserted_count() const { return inserted_count_; } | |
| 49 size_t removed_count() const { return removed_count_; } | |
| 50 size_t moved_count() const { return moved_count_; } | |
| 51 int highlight_mode_count() const { return highlight_mode_count_; } | |
| 52 size_t initialized_count() const { return initialized_count_; } | |
| 53 | |
| 54 private: | |
| 55 // ExtensionToolbarModel::Observer: | |
| 56 void OnToolbarExtensionAdded(const Extension* extension, int index) override { | |
| 57 ++inserted_count_; | |
| 58 } | |
| 59 | |
| 60 void OnToolbarExtensionRemoved(const Extension* extension) override { | |
| 61 ++removed_count_; | |
| 62 } | |
| 63 | |
| 64 void OnToolbarExtensionMoved(const Extension* extension, int index) override { | |
| 65 ++moved_count_; | |
| 66 } | |
| 67 | |
| 68 void OnToolbarExtensionUpdated(const Extension* extension) override {} | |
| 69 | |
| 70 bool ShowExtensionActionPopup(const Extension* extension, | |
| 71 bool grant_active_tab) override { | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 void OnToolbarVisibleCountChanged() override {} | |
| 76 | |
| 77 void OnToolbarHighlightModeChanged(bool is_highlighting) override { | |
| 78 // Add one if highlighting, subtract one if not. | |
| 79 highlight_mode_count_ += is_highlighting ? 1 : -1; | |
| 80 } | |
| 81 | |
| 82 void OnToolbarModelInitialized() override { ++initialized_count_; } | |
| 83 | |
| 84 Browser* GetBrowser() override { return NULL; } | |
| 85 | |
| 86 ExtensionToolbarModel* model_; | |
| 87 | |
| 88 size_t inserted_count_; | |
| 89 size_t removed_count_; | |
| 90 size_t moved_count_; | |
| 91 // Int because it could become negative (if something goes wrong). | |
| 92 int highlight_mode_count_; | |
| 93 size_t initialized_count_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(ExtensionToolbarModelTestObserver); | |
| 96 }; | |
| 97 | |
| 98 ExtensionToolbarModelTestObserver::ExtensionToolbarModelTestObserver( | |
| 99 ExtensionToolbarModel* model) : model_(model), | |
| 100 inserted_count_(0), | |
| 101 removed_count_(0), | |
| 102 moved_count_(0), | |
| 103 highlight_mode_count_(0), | |
| 104 initialized_count_(0) { | |
| 105 model_->AddObserver(this); | |
| 106 } | |
| 107 | |
| 108 ExtensionToolbarModelTestObserver::~ExtensionToolbarModelTestObserver() { | |
| 109 model_->RemoveObserver(this); | |
| 110 } | |
| 111 | |
| 112 } // namespace | |
| 113 | |
| 114 class ExtensionToolbarModelUnitTest : public ExtensionServiceTestBase { | |
| 115 protected: | |
| 116 // Initialize the ExtensionService, ExtensionToolbarModel, and | |
| 117 // ExtensionSystem. | |
| 118 void Init(); | |
| 119 | |
| 120 void TearDown() override; | |
| 121 | |
| 122 // Adds or removes the given |extension| and verify success. | |
| 123 testing::AssertionResult AddExtension( | |
| 124 const scoped_refptr<const Extension>& extension) WARN_UNUSED_RESULT; | |
| 125 testing::AssertionResult RemoveExtension( | |
| 126 const scoped_refptr<const Extension>& extension) WARN_UNUSED_RESULT; | |
| 127 | |
| 128 // Adds three extensions, all with browser actions. | |
| 129 testing::AssertionResult AddBrowserActionExtensions() WARN_UNUSED_RESULT; | |
| 130 | |
| 131 // Adds three extensions, one each for browser action, page action, and no | |
| 132 // action, and are added in that order. | |
| 133 testing::AssertionResult AddActionExtensions() WARN_UNUSED_RESULT; | |
| 134 | |
| 135 // Returns the extension at the given index in the toolbar model, or NULL | |
| 136 // if one does not exist. | |
| 137 // If |model| is specified, it is used. Otherwise, this defaults to | |
| 138 // |toolbar_model_|. | |
| 139 const Extension* GetExtensionAtIndex( | |
| 140 size_t index, const ExtensionToolbarModel* model) const; | |
| 141 const Extension* GetExtensionAtIndex(size_t index) const; | |
| 142 | |
| 143 ExtensionToolbarModel* toolbar_model() { return toolbar_model_; } | |
| 144 | |
| 145 const ExtensionToolbarModelTestObserver* observer() const { | |
| 146 return model_observer_.get(); | |
| 147 } | |
| 148 size_t num_toolbar_items() const { | |
| 149 return toolbar_model_->toolbar_items().size(); | |
| 150 } | |
| 151 const Extension* browser_action_a() const { return browser_action_a_.get(); } | |
| 152 const Extension* browser_action_b() const { return browser_action_b_.get(); } | |
| 153 const Extension* browser_action_c() const { return browser_action_c_.get(); } | |
| 154 const Extension* browser_action() const { | |
| 155 return browser_action_extension_.get(); | |
| 156 } | |
| 157 const Extension* page_action() const { return page_action_extension_.get(); } | |
| 158 const Extension* no_action() const { return no_action_extension_.get(); } | |
| 159 | |
| 160 private: | |
| 161 // Verifies that all extensions in |extensions| are added successfully. | |
| 162 testing::AssertionResult AddAndVerifyExtensions( | |
| 163 const ExtensionList& extensions); | |
| 164 | |
| 165 // The toolbar model associated with the testing profile. | |
| 166 ExtensionToolbarModel* toolbar_model_; | |
| 167 | |
| 168 // The test observer to track events. Must come after toolbar_model_ so that | |
| 169 // it is destroyed and removes itself as an observer first. | |
| 170 scoped_ptr<ExtensionToolbarModelTestObserver> model_observer_; | |
| 171 | |
| 172 // Sample extensions with only browser actions. | |
| 173 scoped_refptr<const Extension> browser_action_a_; | |
| 174 scoped_refptr<const Extension> browser_action_b_; | |
| 175 scoped_refptr<const Extension> browser_action_c_; | |
| 176 | |
| 177 // Sample extensions with different kinds of actions. | |
| 178 scoped_refptr<const Extension> browser_action_extension_; | |
| 179 scoped_refptr<const Extension> page_action_extension_; | |
| 180 scoped_refptr<const Extension> no_action_extension_; | |
| 181 }; | |
| 182 | |
| 183 void ExtensionToolbarModelUnitTest::Init() { | |
| 184 InitializeEmptyExtensionService(); | |
| 185 toolbar_model_ = | |
| 186 extension_action_test_util::CreateToolbarModelForProfile(profile()); | |
| 187 model_observer_.reset(new ExtensionToolbarModelTestObserver(toolbar_model_)); | |
| 188 } | |
| 189 | |
| 190 void ExtensionToolbarModelUnitTest::TearDown() { | |
| 191 model_observer_.reset(); | |
| 192 ExtensionServiceTestBase::TearDown(); | |
| 193 } | |
| 194 | |
| 195 testing::AssertionResult ExtensionToolbarModelUnitTest::AddExtension( | |
| 196 const scoped_refptr<const Extension>& extension) { | |
| 197 if (registry()->enabled_extensions().GetByID(extension->id())) { | |
| 198 return testing::AssertionFailure() << "Extension " << extension->name() << | |
| 199 " already installed!"; | |
| 200 } | |
| 201 service()->AddExtension(extension.get()); | |
| 202 if (!registry()->enabled_extensions().GetByID(extension->id())) { | |
| 203 return testing::AssertionFailure() << "Failed to install extension: " << | |
| 204 extension->name(); | |
| 205 } | |
| 206 return testing::AssertionSuccess(); | |
| 207 } | |
| 208 | |
| 209 testing::AssertionResult ExtensionToolbarModelUnitTest::RemoveExtension( | |
| 210 const scoped_refptr<const Extension>& extension) { | |
| 211 if (!registry()->enabled_extensions().GetByID(extension->id())) { | |
| 212 return testing::AssertionFailure() << "Extension " << extension->name() << | |
| 213 " not installed!"; | |
| 214 } | |
| 215 service()->UnloadExtension(extension->id(), | |
| 216 UnloadedExtensionInfo::REASON_DISABLE); | |
| 217 if (registry()->enabled_extensions().GetByID(extension->id())) { | |
| 218 return testing::AssertionFailure() << "Failed to unload extension: " << | |
| 219 extension->name(); | |
| 220 } | |
| 221 return testing::AssertionSuccess(); | |
| 222 } | |
| 223 | |
| 224 testing::AssertionResult ExtensionToolbarModelUnitTest::AddActionExtensions() { | |
| 225 browser_action_extension_ = extension_action_test_util::CreateActionExtension( | |
| 226 "browser_action", extension_action_test_util::BROWSER_ACTION); | |
| 227 page_action_extension_ = extension_action_test_util::CreateActionExtension( | |
| 228 "page_action", extension_action_test_util::PAGE_ACTION); | |
| 229 no_action_extension_ = extension_action_test_util::CreateActionExtension( | |
| 230 "no_action", extension_action_test_util::NO_ACTION); | |
| 231 | |
| 232 ExtensionList extensions; | |
| 233 extensions.push_back(browser_action_extension_); | |
| 234 extensions.push_back(page_action_extension_); | |
| 235 extensions.push_back(no_action_extension_); | |
| 236 | |
| 237 return AddAndVerifyExtensions(extensions); | |
| 238 } | |
| 239 | |
| 240 testing::AssertionResult | |
| 241 ExtensionToolbarModelUnitTest::AddBrowserActionExtensions() { | |
| 242 browser_action_a_ = extension_action_test_util::CreateActionExtension( | |
| 243 "browser_actionA", extension_action_test_util::BROWSER_ACTION); | |
| 244 browser_action_b_ = extension_action_test_util::CreateActionExtension( | |
| 245 "browser_actionB", extension_action_test_util::BROWSER_ACTION); | |
| 246 browser_action_c_ = extension_action_test_util::CreateActionExtension( | |
| 247 "browser_actionC", extension_action_test_util::BROWSER_ACTION); | |
| 248 | |
| 249 ExtensionList extensions; | |
| 250 extensions.push_back(browser_action_a_); | |
| 251 extensions.push_back(browser_action_b_); | |
| 252 extensions.push_back(browser_action_c_); | |
| 253 | |
| 254 return AddAndVerifyExtensions(extensions); | |
| 255 } | |
| 256 | |
| 257 const Extension* ExtensionToolbarModelUnitTest::GetExtensionAtIndex( | |
| 258 size_t index, const ExtensionToolbarModel* model) const { | |
| 259 return index < model->toolbar_items().size() | |
| 260 ? model->toolbar_items()[index].get() | |
| 261 : NULL; | |
| 262 } | |
| 263 | |
| 264 const Extension* ExtensionToolbarModelUnitTest::GetExtensionAtIndex( | |
| 265 size_t index) const { | |
| 266 return GetExtensionAtIndex(index, toolbar_model_); | |
| 267 } | |
| 268 | |
| 269 testing::AssertionResult ExtensionToolbarModelUnitTest::AddAndVerifyExtensions( | |
| 270 const ExtensionList& extensions) { | |
| 271 for (ExtensionList::const_iterator iter = extensions.begin(); | |
| 272 iter != extensions.end(); ++iter) { | |
| 273 if (!AddExtension(*iter)) { | |
| 274 return testing::AssertionFailure() << "Failed to install extension: " << | |
| 275 (*iter)->name(); | |
| 276 } | |
| 277 } | |
| 278 return testing::AssertionSuccess(); | |
| 279 } | |
| 280 | |
| 281 // A basic test for extensions with browser actions showing up in the toolbar. | |
| 282 TEST_F(ExtensionToolbarModelUnitTest, BasicExtensionToolbarModelTest) { | |
| 283 Init(); | |
| 284 | |
| 285 // Load an extension with no browser action. | |
| 286 scoped_refptr<const Extension> extension1 = | |
| 287 extension_action_test_util::CreateActionExtension( | |
| 288 "no_action", extension_action_test_util::NO_ACTION); | |
| 289 ASSERT_TRUE(AddExtension(extension1)); | |
| 290 | |
| 291 // This extension should not be in the model (has no browser action). | |
| 292 EXPECT_EQ(0u, observer()->inserted_count()); | |
| 293 EXPECT_EQ(0u, num_toolbar_items()); | |
| 294 EXPECT_EQ(NULL, GetExtensionAtIndex(0u)); | |
| 295 | |
| 296 // Load an extension with a browser action. | |
| 297 scoped_refptr<const Extension> extension2 = | |
| 298 extension_action_test_util::CreateActionExtension( | |
| 299 "browser_action", extension_action_test_util::BROWSER_ACTION); | |
| 300 ASSERT_TRUE(AddExtension(extension2)); | |
| 301 | |
| 302 // We should now find our extension in the model. | |
| 303 EXPECT_EQ(1u, observer()->inserted_count()); | |
| 304 EXPECT_EQ(1u, num_toolbar_items()); | |
| 305 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); | |
| 306 | |
| 307 // Should be a no-op, but still fires the events. | |
| 308 toolbar_model()->MoveExtensionIcon(extension2->id(), 0); | |
| 309 EXPECT_EQ(1u, observer()->moved_count()); | |
| 310 EXPECT_EQ(1u, num_toolbar_items()); | |
| 311 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); | |
| 312 | |
| 313 // Remove the extension and verify. | |
| 314 ASSERT_TRUE(RemoveExtension(extension2)); | |
| 315 EXPECT_EQ(1u, observer()->removed_count()); | |
| 316 EXPECT_EQ(0u, num_toolbar_items()); | |
| 317 EXPECT_EQ(NULL, GetExtensionAtIndex(0u)); | |
| 318 } | |
| 319 | |
| 320 // Test various different reorderings, removals, and reinsertions. | |
| 321 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarReorderAndReinsert) { | |
| 322 Init(); | |
| 323 | |
| 324 // Add the three browser action extensions. | |
| 325 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 326 | |
| 327 // Verify the three extensions are in the model in the proper order. | |
| 328 EXPECT_EQ(3u, num_toolbar_items()); | |
| 329 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 330 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 331 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 332 | |
| 333 // Order is now A, B, C. Let's put C first. | |
| 334 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 0); | |
| 335 EXPECT_EQ(1u, observer()->moved_count()); | |
| 336 EXPECT_EQ(3u, num_toolbar_items()); | |
| 337 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 338 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); | |
| 339 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); | |
| 340 | |
| 341 // Order is now C, A, B. Let's put A last. | |
| 342 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 2); | |
| 343 EXPECT_EQ(2u, observer()->moved_count()); | |
| 344 EXPECT_EQ(3u, num_toolbar_items()); | |
| 345 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 346 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 347 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(2u)); | |
| 348 | |
| 349 // Order is now C, B, A. Let's remove B. | |
| 350 ASSERT_TRUE(RemoveExtension(browser_action_b())); | |
| 351 EXPECT_EQ(1u, observer()->removed_count()); | |
| 352 EXPECT_EQ(2u, num_toolbar_items()); | |
| 353 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 354 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); | |
| 355 | |
| 356 // Load extension B again. | |
| 357 ASSERT_TRUE(AddExtension(browser_action_b())); | |
| 358 | |
| 359 // Extension B loaded again. | |
| 360 EXPECT_EQ(4u, observer()->inserted_count()); | |
| 361 EXPECT_EQ(3u, num_toolbar_items()); | |
| 362 // Make sure it gets its old spot in the list. | |
| 363 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 364 | |
| 365 // Unload B again. | |
| 366 ASSERT_TRUE(RemoveExtension(browser_action_b())); | |
| 367 EXPECT_EQ(2u, observer()->removed_count()); | |
| 368 EXPECT_EQ(2u, num_toolbar_items()); | |
| 369 | |
| 370 // Order is now C, A. Flip it. | |
| 371 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 0); | |
| 372 EXPECT_EQ(3u, observer()->moved_count()); | |
| 373 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 374 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 375 | |
| 376 // Move A to the location it already occupies. | |
| 377 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 0); | |
| 378 EXPECT_EQ(4u, observer()->moved_count()); | |
| 379 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 380 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 381 | |
| 382 // Order is now A, C. Remove C. | |
| 383 ASSERT_TRUE(RemoveExtension(browser_action_c())); | |
| 384 EXPECT_EQ(3u, observer()->removed_count()); | |
| 385 EXPECT_EQ(1u, num_toolbar_items()); | |
| 386 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 387 | |
| 388 // Load extension C again. | |
| 389 ASSERT_TRUE(AddExtension(browser_action_c())); | |
| 390 | |
| 391 // Extension C loaded again. | |
| 392 EXPECT_EQ(5u, observer()->inserted_count()); | |
| 393 EXPECT_EQ(2u, num_toolbar_items()); | |
| 394 // Make sure it gets its old spot in the list (at the very end). | |
| 395 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 396 } | |
| 397 | |
| 398 // Test that order persists after unloading and disabling, but not across | |
| 399 // uninstallation. | |
| 400 TEST_F(ExtensionToolbarModelUnitTest, | |
| 401 ExtensionToolbarUnloadDisableAndUninstall) { | |
| 402 Init(); | |
| 403 | |
| 404 // Add the three browser action extensions. | |
| 405 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 406 | |
| 407 // Verify the three extensions are in the model in the proper order: A, B, C. | |
| 408 EXPECT_EQ(3u, num_toolbar_items()); | |
| 409 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 410 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 411 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 412 | |
| 413 // Unload B, then C, then A, and then reload C, then A, then B. | |
| 414 ASSERT_TRUE(RemoveExtension(browser_action_b())); | |
| 415 ASSERT_TRUE(RemoveExtension(browser_action_c())); | |
| 416 ASSERT_TRUE(RemoveExtension(browser_action_a())); | |
| 417 EXPECT_EQ(0u, num_toolbar_items()); // Sanity check: all gone? | |
| 418 ASSERT_TRUE(AddExtension(browser_action_c())); | |
| 419 ASSERT_TRUE(AddExtension(browser_action_a())); | |
| 420 ASSERT_TRUE(AddExtension(browser_action_b())); | |
| 421 EXPECT_EQ(3u, num_toolbar_items()); // Sanity check: all back? | |
| 422 EXPECT_EQ(0u, observer()->moved_count()); | |
| 423 | |
| 424 // Even though we unloaded and reloaded in a different order, the original | |
| 425 // order (A, B, C) should be preserved. | |
| 426 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 427 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 428 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 429 | |
| 430 // Disabling extensions should also preserve order. | |
| 431 service()->DisableExtension(browser_action_b()->id(), | |
| 432 Extension::DISABLE_USER_ACTION); | |
| 433 service()->DisableExtension(browser_action_c()->id(), | |
| 434 Extension::DISABLE_USER_ACTION); | |
| 435 service()->DisableExtension(browser_action_a()->id(), | |
| 436 Extension::DISABLE_USER_ACTION); | |
| 437 service()->EnableExtension(browser_action_c()->id()); | |
| 438 service()->EnableExtension(browser_action_a()->id()); | |
| 439 service()->EnableExtension(browser_action_b()->id()); | |
| 440 | |
| 441 // Make sure we still get the original A, B, C order. | |
| 442 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 443 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 444 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 445 | |
| 446 // Move browser_action_b() to be first. | |
| 447 toolbar_model()->MoveExtensionIcon(browser_action_b()->id(), 0); | |
| 448 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); | |
| 449 | |
| 450 // Uninstall Extension B. | |
| 451 service()->UninstallExtension(browser_action_b()->id(), | |
| 452 UNINSTALL_REASON_FOR_TESTING, | |
| 453 base::Bind(&base::DoNothing), | |
| 454 NULL); // Ignore error. | |
| 455 // List contains only A and C now. Validate that. | |
| 456 EXPECT_EQ(2u, num_toolbar_items()); | |
| 457 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 458 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 459 | |
| 460 ASSERT_TRUE(AddExtension(browser_action_b())); | |
| 461 | |
| 462 // Make sure Extension B is _not_ first (its old position should have been | |
| 463 // forgotten at uninstall time). Order should be A, C, B. | |
| 464 EXPECT_EQ(3u, num_toolbar_items()); | |
| 465 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 466 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 467 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); | |
| 468 } | |
| 469 | |
| 470 TEST_F(ExtensionToolbarModelUnitTest, ReorderOnPrefChange) { | |
| 471 Init(); | |
| 472 | |
| 473 // Add the three browser action extensions. | |
| 474 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 475 EXPECT_EQ(3u, num_toolbar_items()); | |
| 476 | |
| 477 // Change the value of the toolbar preference. | |
| 478 ExtensionIdList new_order; | |
| 479 new_order.push_back(browser_action_c()->id()); | |
| 480 new_order.push_back(browser_action_b()->id()); | |
| 481 ExtensionPrefs::Get(profile())->SetToolbarOrder(new_order); | |
| 482 | |
| 483 // Verify order is changed. | |
| 484 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 485 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 486 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(2u)); | |
| 487 } | |
| 488 | |
| 489 // Test that new extensions are always visible on installation and inserted at | |
| 490 // the "end" of the visible section. | |
| 491 TEST_F(ExtensionToolbarModelUnitTest, NewToolbarExtensionsAreVisible) { | |
| 492 Init(); | |
| 493 | |
| 494 // Three extensions with actions. | |
| 495 scoped_refptr<const Extension> extension_a = | |
| 496 extension_action_test_util::CreateActionExtension( | |
| 497 "a", extension_action_test_util::BROWSER_ACTION); | |
| 498 scoped_refptr<const Extension> extension_b = | |
| 499 extension_action_test_util::CreateActionExtension( | |
| 500 "b", extension_action_test_util::BROWSER_ACTION); | |
| 501 scoped_refptr<const Extension> extension_c = | |
| 502 extension_action_test_util::CreateActionExtension( | |
| 503 "c", extension_action_test_util::BROWSER_ACTION); | |
| 504 scoped_refptr<const Extension> extension_d = | |
| 505 extension_action_test_util::CreateActionExtension( | |
| 506 "d", extension_action_test_util::BROWSER_ACTION); | |
| 507 | |
| 508 // We should start off without any extensions. | |
| 509 EXPECT_EQ(0u, num_toolbar_items()); | |
| 510 EXPECT_EQ(0u, toolbar_model()->visible_icon_count()); | |
| 511 | |
| 512 // Add one extension. It should be visible. | |
| 513 service()->AddExtension(extension_a.get()); | |
| 514 EXPECT_EQ(1u, num_toolbar_items()); | |
| 515 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); | |
| 516 EXPECT_EQ(extension_a.get(), GetExtensionAtIndex(0u)); | |
| 517 | |
| 518 // Hide all extensions. | |
| 519 toolbar_model()->SetVisibleIconCount(0); | |
| 520 EXPECT_EQ(0u, toolbar_model()->visible_icon_count()); | |
| 521 | |
| 522 // Add a new extension - it should be visible, so it should be in the first | |
| 523 // index. The other extension should remain hidden. | |
| 524 service()->AddExtension(extension_b.get()); | |
| 525 EXPECT_EQ(2u, num_toolbar_items()); | |
| 526 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); | |
| 527 EXPECT_EQ(extension_b.get(), GetExtensionAtIndex(0u)); | |
| 528 EXPECT_EQ(extension_a.get(), GetExtensionAtIndex(1u)); | |
| 529 | |
| 530 // Show all extensions. | |
| 531 toolbar_model()->SetVisibleIconCount(2); | |
| 532 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 533 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 534 | |
| 535 // Add the third extension. Since all extensions are visible, it should go in | |
| 536 // the last index. | |
| 537 service()->AddExtension(extension_c.get()); | |
| 538 EXPECT_EQ(3u, num_toolbar_items()); | |
| 539 EXPECT_EQ(3u, toolbar_model()->visible_icon_count()); | |
| 540 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 541 EXPECT_EQ(extension_b.get(), GetExtensionAtIndex(0u)); | |
| 542 EXPECT_EQ(extension_a.get(), GetExtensionAtIndex(1u)); | |
| 543 EXPECT_EQ(extension_c.get(), GetExtensionAtIndex(2u)); | |
| 544 | |
| 545 // Hide one extension (two remaining visible). | |
| 546 toolbar_model()->SetVisibleIconCount(2); | |
| 547 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 548 | |
| 549 // Add a fourth extension. It should go at the end of the visible section and | |
| 550 // be visible, so it increases visible count by 1, and goes into the third | |
| 551 // index. The hidden extension should remain hidden. | |
| 552 service()->AddExtension(extension_d.get()); | |
| 553 EXPECT_EQ(4u, num_toolbar_items()); | |
| 554 EXPECT_EQ(3u, toolbar_model()->visible_icon_count()); | |
| 555 EXPECT_EQ(extension_b.get(), GetExtensionAtIndex(0u)); | |
| 556 EXPECT_EQ(extension_a.get(), GetExtensionAtIndex(1u)); | |
| 557 EXPECT_EQ(extension_d.get(), GetExtensionAtIndex(2u)); | |
| 558 EXPECT_EQ(extension_c.get(), GetExtensionAtIndex(3u)); | |
| 559 } | |
| 560 | |
| 561 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarHighlightMode) { | |
| 562 Init(); | |
| 563 | |
| 564 EXPECT_FALSE(toolbar_model()->HighlightExtensions( | |
| 565 ExtensionIdList(), ExtensionToolbarModel::HIGHLIGHT_WARNING)); | |
| 566 EXPECT_EQ(0, observer()->highlight_mode_count()); | |
| 567 | |
| 568 // Add the three browser action extensions. | |
| 569 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 570 EXPECT_EQ(3u, num_toolbar_items()); | |
| 571 | |
| 572 // Start with a visible count of 2 (non-zero, and not all). | |
| 573 toolbar_model()->SetVisibleIconCount(2u); | |
| 574 | |
| 575 // Highlight one extension. | |
| 576 ExtensionIdList extension_ids; | |
| 577 extension_ids.push_back(browser_action_b()->id()); | |
| 578 toolbar_model()->HighlightExtensions( | |
| 579 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 580 EXPECT_EQ(1, observer()->highlight_mode_count()); | |
| 581 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 582 | |
| 583 EXPECT_EQ(1u, num_toolbar_items()); | |
| 584 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); | |
| 585 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); | |
| 586 | |
| 587 // Stop highlighting. | |
| 588 toolbar_model()->StopHighlighting(); | |
| 589 EXPECT_EQ(0, observer()->highlight_mode_count()); | |
| 590 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 591 | |
| 592 // Verify that the extensions are back to normal. | |
| 593 EXPECT_EQ(3u, num_toolbar_items()); | |
| 594 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 595 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 596 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 597 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 598 | |
| 599 // Call stop highlighting a second time (shouldn't be notified). | |
| 600 toolbar_model()->StopHighlighting(); | |
| 601 EXPECT_EQ(0, observer()->highlight_mode_count()); | |
| 602 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 603 | |
| 604 // Highlight all extensions. | |
| 605 extension_ids.clear(); | |
| 606 extension_ids.push_back(browser_action_a()->id()); | |
| 607 extension_ids.push_back(browser_action_b()->id()); | |
| 608 extension_ids.push_back(browser_action_c()->id()); | |
| 609 toolbar_model()->HighlightExtensions( | |
| 610 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 611 EXPECT_EQ(1, observer()->highlight_mode_count()); | |
| 612 EXPECT_EQ(3u, num_toolbar_items()); | |
| 613 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 614 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 615 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 616 EXPECT_EQ(3u, toolbar_model()->visible_icon_count()); | |
| 617 // Even though the visible count is 3, we shouldn't adjust the stored | |
| 618 // preference. | |
| 619 EXPECT_EQ(2, profile()->GetPrefs()->GetInteger(pref_names::kToolbarSize)); | |
| 620 | |
| 621 // Highlight only extension b (shrink the highlight list). | |
| 622 extension_ids.clear(); | |
| 623 extension_ids.push_back(browser_action_b()->id()); | |
| 624 toolbar_model()->HighlightExtensions( | |
| 625 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 626 EXPECT_EQ(2, observer()->highlight_mode_count()); | |
| 627 EXPECT_EQ(1u, num_toolbar_items()); | |
| 628 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); | |
| 629 | |
| 630 // Highlight extensions a and b (grow the highlight list). | |
| 631 extension_ids.clear(); | |
| 632 extension_ids.push_back(browser_action_a()->id()); | |
| 633 extension_ids.push_back(browser_action_b()->id()); | |
| 634 toolbar_model()->HighlightExtensions( | |
| 635 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 636 EXPECT_EQ(3, observer()->highlight_mode_count()); | |
| 637 EXPECT_EQ(2u, num_toolbar_items()); | |
| 638 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 639 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 640 | |
| 641 // Highlight no extensions (empty the highlight list). | |
| 642 extension_ids.clear(); | |
| 643 toolbar_model()->HighlightExtensions( | |
| 644 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 645 EXPECT_EQ(2, observer()->highlight_mode_count()); | |
| 646 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 647 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 648 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 649 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 650 | |
| 651 // Our toolbar size should be back to normal. | |
| 652 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 653 EXPECT_EQ(2, profile()->GetPrefs()->GetInteger(pref_names::kToolbarSize)); | |
| 654 } | |
| 655 | |
| 656 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarHighlightModeRemove) { | |
| 657 Init(); | |
| 658 | |
| 659 // Add the three browser action extensions. | |
| 660 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 661 EXPECT_EQ(3u, num_toolbar_items()); | |
| 662 | |
| 663 // Highlight two of the extensions. | |
| 664 ExtensionIdList extension_ids; | |
| 665 extension_ids.push_back(browser_action_a()->id()); | |
| 666 extension_ids.push_back(browser_action_b()->id()); | |
| 667 toolbar_model()->HighlightExtensions( | |
| 668 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 669 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 670 EXPECT_EQ(1, observer()->highlight_mode_count()); | |
| 671 EXPECT_EQ(2u, num_toolbar_items()); | |
| 672 | |
| 673 // Disable one of them - only one should remain highlighted. | |
| 674 service()->DisableExtension(browser_action_a()->id(), | |
| 675 Extension::DISABLE_USER_ACTION); | |
| 676 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 677 EXPECT_EQ(1u, num_toolbar_items()); | |
| 678 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); | |
| 679 | |
| 680 // Uninstall the remaining highlighted extension. This should result in | |
| 681 // highlight mode exiting. | |
| 682 service()->UninstallExtension(browser_action_b()->id(), | |
| 683 UNINSTALL_REASON_FOR_TESTING, | |
| 684 base::Bind(&base::DoNothing), | |
| 685 NULL); // Ignore error. | |
| 686 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 687 EXPECT_EQ(0, observer()->highlight_mode_count()); | |
| 688 EXPECT_EQ(1u, num_toolbar_items()); | |
| 689 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 690 | |
| 691 // Test that removing an unhighlighted extension still works. | |
| 692 // Reinstall extension b, and then highlight extension c. | |
| 693 ASSERT_TRUE(AddExtension(browser_action_b())); | |
| 694 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 695 extension_ids.clear(); | |
| 696 extension_ids.push_back(browser_action_c()->id()); | |
| 697 toolbar_model()->HighlightExtensions( | |
| 698 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 699 EXPECT_EQ(1, observer()->highlight_mode_count()); | |
| 700 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 701 EXPECT_EQ(1u, num_toolbar_items()); | |
| 702 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 703 | |
| 704 // Uninstalling b should not have visible impact. | |
| 705 service()->UninstallExtension(browser_action_b()->id(), | |
| 706 UNINSTALL_REASON_FOR_TESTING, | |
| 707 base::Bind(&base::DoNothing), | |
| 708 NULL); // Ignore error. | |
| 709 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 710 EXPECT_EQ(1, observer()->highlight_mode_count()); | |
| 711 EXPECT_EQ(1u, num_toolbar_items()); | |
| 712 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 713 | |
| 714 // When we stop, only extension c should remain. | |
| 715 toolbar_model()->StopHighlighting(); | |
| 716 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 717 EXPECT_EQ(0, observer()->highlight_mode_count()); | |
| 718 EXPECT_EQ(1u, num_toolbar_items()); | |
| 719 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | |
| 720 } | |
| 721 | |
| 722 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarHighlightModeAdd) { | |
| 723 Init(); | |
| 724 | |
| 725 // Add the three browser action extensions. | |
| 726 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 727 EXPECT_EQ(3u, num_toolbar_items()); | |
| 728 | |
| 729 // Remove one (down to two). | |
| 730 ASSERT_TRUE(RemoveExtension(browser_action_c())); | |
| 731 | |
| 732 // Highlight one of the two extensions. | |
| 733 ExtensionIdList extension_ids; | |
| 734 extension_ids.push_back(browser_action_a()->id()); | |
| 735 toolbar_model()->HighlightExtensions( | |
| 736 extension_ids, ExtensionToolbarModel::HIGHLIGHT_WARNING); | |
| 737 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 738 EXPECT_EQ(1u, num_toolbar_items()); | |
| 739 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 740 | |
| 741 // Adding a new extension should have no visible effect. | |
| 742 ASSERT_TRUE(AddExtension(browser_action_c())); | |
| 743 EXPECT_TRUE(toolbar_model()->is_highlighting()); | |
| 744 EXPECT_EQ(1u, num_toolbar_items()); | |
| 745 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 746 | |
| 747 // When we stop highlighting, we should see the new extension show up. | |
| 748 toolbar_model()->StopHighlighting(); | |
| 749 EXPECT_FALSE(toolbar_model()->is_highlighting()); | |
| 750 EXPECT_EQ(3u, num_toolbar_items()); | |
| 751 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 752 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 753 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 754 } | |
| 755 | |
| 756 // Test that the extension toolbar maintains the proper size, even after a pref | |
| 757 // change. | |
| 758 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarSizeAfterPrefChange) { | |
| 759 Init(); | |
| 760 | |
| 761 // Add the three browser action extensions. | |
| 762 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 763 EXPECT_EQ(3u, num_toolbar_items()); | |
| 764 | |
| 765 // Should be at max size. | |
| 766 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 767 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); | |
| 768 toolbar_model()->OnExtensionToolbarPrefChange(); | |
| 769 // Should still be at max size. | |
| 770 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 771 EXPECT_EQ(num_toolbar_items(), toolbar_model()->visible_icon_count()); | |
| 772 } | |
| 773 | |
| 774 // Test that, in the absence of the extension-action-redesign switch, the | |
| 775 // model only contains extensions with browser actions. | |
| 776 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesNoSwitch) { | |
| 777 Init(); | |
| 778 ASSERT_TRUE(AddActionExtensions()); | |
| 779 | |
| 780 EXPECT_EQ(1u, num_toolbar_items()); | |
| 781 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); | |
| 782 } | |
| 783 | |
| 784 // Test that, with the extension-action-redesign switch, the model contains | |
| 785 // all types of extensions, except those which should not be displayed on the | |
| 786 // toolbar (like component extensions). | |
| 787 TEST_F(ExtensionToolbarModelUnitTest, TestToolbarExtensionTypesSwitch) { | |
| 788 FeatureSwitch::ScopedOverride enable_redesign( | |
| 789 FeatureSwitch::extension_action_redesign(), true); | |
| 790 Init(); | |
| 791 ASSERT_TRUE(AddActionExtensions()); | |
| 792 | |
| 793 // With the switch on, extensions with page actions and no action should also | |
| 794 // be displayed in the toolbar. | |
| 795 EXPECT_EQ(3u, num_toolbar_items()); | |
| 796 EXPECT_EQ(browser_action(), GetExtensionAtIndex(0u)); | |
| 797 EXPECT_EQ(page_action(), GetExtensionAtIndex(1u)); | |
| 798 EXPECT_EQ(no_action(), GetExtensionAtIndex(2u)); | |
| 799 } | |
| 800 | |
| 801 // Test that hiding actions on the toolbar results in their removal from the | |
| 802 // model when the redesign switch is not enabled. | |
| 803 TEST_F(ExtensionToolbarModelUnitTest, | |
| 804 ExtensionToolbarActionsVisibilityNoSwitch) { | |
| 805 Init(); | |
| 806 | |
| 807 ExtensionActionAPI* action_api = ExtensionActionAPI::Get(profile()); | |
| 808 | |
| 809 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 810 // Sanity check: Order should start as A B C. | |
| 811 EXPECT_EQ(3u, num_toolbar_items()); | |
| 812 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 813 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 814 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 815 | |
| 816 // By default, all actions should be visible. | |
| 817 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_a()->id())); | |
| 818 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_b()->id())); | |
| 819 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_c()->id())); | |
| 820 | |
| 821 // Hiding an action should result in its removal from the toolbar. | |
| 822 action_api->SetBrowserActionVisibility(browser_action_b()->id(), false); | |
| 823 EXPECT_FALSE(action_api->GetBrowserActionVisibility( | |
| 824 browser_action_b()->id())); | |
| 825 // Thus, there should now only be two items on the toolbar - A and C. | |
| 826 EXPECT_EQ(2u, num_toolbar_items()); | |
| 827 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 828 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 829 | |
| 830 // Resetting the visibility to 'true' should result in the extension being | |
| 831 // added back at its original position. | |
| 832 action_api->SetBrowserActionVisibility(browser_action_b()->id(), true); | |
| 833 EXPECT_TRUE(action_api->GetBrowserActionVisibility(browser_action_b()->id())); | |
| 834 // So the toolbar order should be A B C. | |
| 835 EXPECT_EQ(3u, num_toolbar_items()); | |
| 836 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 837 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | |
| 838 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | |
| 839 } | |
| 840 | |
| 841 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarIncognitoModeTest) { | |
| 842 Init(); | |
| 843 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 844 | |
| 845 // Give two extensions incognito access. | |
| 846 // Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of | |
| 847 // util::SetIsIncognitoEnabled because the latter tries to reload the | |
| 848 // extension, which requries a filepath associated with the extension (and, | |
| 849 // for this test, reloading the extension is irrelevant to us). | |
| 850 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile()); | |
| 851 extension_prefs->SetIsIncognitoEnabled(browser_action_b()->id(), true); | |
| 852 extension_prefs->SetIsIncognitoEnabled(browser_action_c()->id(), true); | |
| 853 | |
| 854 util::SetIsIncognitoEnabled(browser_action_b()->id(), profile(), true); | |
| 855 util::SetIsIncognitoEnabled(browser_action_c()->id(), profile(), true); | |
| 856 | |
| 857 // Move C to the second index. | |
| 858 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 1u); | |
| 859 // Set visible count to 2 so that c is overflowed. State is A C [B]. | |
| 860 toolbar_model()->SetVisibleIconCount(2); | |
| 861 EXPECT_EQ(1u, observer()->moved_count()); | |
| 862 | |
| 863 // Get an incognito profile and toolbar. | |
| 864 ExtensionToolbarModel* incognito_model = | |
| 865 extension_action_test_util::CreateToolbarModelForProfile( | |
| 866 profile()->GetOffTheRecordProfile()); | |
| 867 | |
| 868 ExtensionToolbarModelTestObserver incognito_observer(incognito_model); | |
| 869 EXPECT_EQ(0u, incognito_observer.moved_count()); | |
| 870 | |
| 871 // We should have two items, C and B, and the order should be preserved from | |
| 872 // the original model. | |
| 873 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); | |
| 874 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u, incognito_model)); | |
| 875 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u, incognito_model)); | |
| 876 | |
| 877 // Extensions in the overflow menu in the regular toolbar should remain in | |
| 878 // overflow in the incognito toolbar. So, we should have C [B]. | |
| 879 EXPECT_EQ(1u, incognito_model->visible_icon_count()); | |
| 880 // The regular model should still have two icons visible. | |
| 881 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 882 | |
| 883 // Changing the incognito model size should not affect the regular model. | |
| 884 incognito_model->SetVisibleIconCount(0); | |
| 885 EXPECT_EQ(0u, incognito_model->visible_icon_count()); | |
| 886 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 887 | |
| 888 // Expanding the incognito model to 2 should register as "all icons" | |
| 889 // since it is all of the incognito-enabled extensions. | |
| 890 incognito_model->SetVisibleIconCount(2u); | |
| 891 EXPECT_EQ(2u, incognito_model->visible_icon_count()); | |
| 892 EXPECT_TRUE(incognito_model->all_icons_visible()); | |
| 893 | |
| 894 // Moving icons in the incognito toolbar should not affect the regular | |
| 895 // toolbar. Incognito currently has C B... | |
| 896 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u); | |
| 897 // So now it should be B C... | |
| 898 EXPECT_EQ(1u, incognito_observer.moved_count()); | |
| 899 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); | |
| 900 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); | |
| 901 // ... and the regular toolbar should be unaffected. | |
| 902 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | |
| 903 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | |
| 904 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); | |
| 905 | |
| 906 // Similarly, the observer for the regular model should not have received | |
| 907 // any updates. | |
| 908 EXPECT_EQ(1u, observer()->moved_count()); | |
| 909 | |
| 910 // And performing moves on the regular model should have no effect on the | |
| 911 // incognito model or its observers. | |
| 912 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 2u); | |
| 913 EXPECT_EQ(2u, observer()->moved_count()); | |
| 914 EXPECT_EQ(1u, incognito_observer.moved_count()); | |
| 915 } | |
| 916 | |
| 917 // Test that enabling extensions incognito with an active incognito profile | |
| 918 // works. | |
| 919 TEST_F(ExtensionToolbarModelUnitTest, | |
| 920 ExtensionToolbarIncognitoEnableExtension) { | |
| 921 Init(); | |
| 922 | |
| 923 const char* kManifest = | |
| 924 "{" | |
| 925 " \"name\": \"%s\"," | |
| 926 " \"version\": \"1.0\"," | |
| 927 " \"manifest_version\": 2," | |
| 928 " \"browser_action\": {}" | |
| 929 "}"; | |
| 930 | |
| 931 // For this test, we need to have "real" extension files, because we need to | |
| 932 // be able to reload them during the incognito process. Since the toolbar | |
| 933 // needs to be notified of the reload, we need it this time (as opposed to | |
| 934 // above, where we simply set the prefs before the incognito bar was | |
| 935 // created. | |
| 936 TestExtensionDir dir1; | |
| 937 dir1.WriteManifest(base::StringPrintf(kManifest, "incognito1")); | |
| 938 TestExtensionDir dir2; | |
| 939 dir2.WriteManifest(base::StringPrintf(kManifest, "incognito2")); | |
| 940 | |
| 941 TestExtensionDir* dirs[] = { &dir1, &dir2 }; | |
| 942 const Extension* extensions[] = { nullptr, nullptr }; | |
| 943 for (size_t i = 0; i < arraysize(dirs); ++i) { | |
| 944 // The extension id will be calculated from the file path; we need this to | |
| 945 // wait for the extension to load. | |
| 946 base::FilePath path_for_id = | |
| 947 base::MakeAbsoluteFilePath(dirs[i]->unpacked_path()); | |
| 948 std::string id = crx_file::id_util::GenerateIdForPath(path_for_id); | |
| 949 TestExtensionRegistryObserver observer(registry(), id); | |
| 950 UnpackedInstaller::Create(service())->Load(dirs[i]->unpacked_path()); | |
| 951 observer.WaitForExtensionLoaded(); | |
| 952 extensions[i] = registry()->enabled_extensions().GetByID(id); | |
| 953 ASSERT_TRUE(extensions[i]); | |
| 954 } | |
| 955 | |
| 956 // For readability, alias to A and B. Since we'll be reloading these | |
| 957 // extensions, we also can't rely on pointers. | |
| 958 std::string extension_a = extensions[0]->id(); | |
| 959 std::string extension_b = extensions[1]->id(); | |
| 960 | |
| 961 // The first model should have both extensions visible. | |
| 962 EXPECT_EQ(2u, toolbar_model()->toolbar_items().size()); | |
| 963 EXPECT_EQ(extension_a, GetExtensionAtIndex(0)->id()); | |
| 964 EXPECT_EQ(extension_b, GetExtensionAtIndex(1)->id()); | |
| 965 | |
| 966 // Set the model to only show one extension, so the order is A [B]. | |
| 967 toolbar_model()->SetVisibleIconCount(1u); | |
| 968 | |
| 969 // Get an incognito profile and toolbar. | |
| 970 ExtensionToolbarModel* incognito_model = | |
| 971 extension_action_test_util::CreateToolbarModelForProfile( | |
| 972 profile()->GetOffTheRecordProfile()); | |
| 973 ExtensionToolbarModelTestObserver incognito_observer(incognito_model); | |
| 974 | |
| 975 // Right now, no extensions are enabled in incognito mode. | |
| 976 EXPECT_EQ(0u, incognito_model->toolbar_items().size()); | |
| 977 | |
| 978 // Set extension b (which is overflowed) to be enabled in incognito. This | |
| 979 // results in b reloading, so wait for it. | |
| 980 { | |
| 981 TestExtensionRegistryObserver observer(registry(), extension_b); | |
| 982 util::SetIsIncognitoEnabled(extension_b, profile(), true); | |
| 983 observer.WaitForExtensionLoaded(); | |
| 984 } | |
| 985 | |
| 986 // Now, we should have one icon in the incognito bar. But, since B is | |
| 987 // overflowed in the main bar, it shouldn't be visible. | |
| 988 EXPECT_EQ(1u, incognito_model->toolbar_items().size()); | |
| 989 EXPECT_EQ(extension_b, GetExtensionAtIndex(0u, incognito_model)->id()); | |
| 990 EXPECT_EQ(0u, incognito_model->visible_icon_count()); | |
| 991 | |
| 992 // Also enable extension a for incognito (again, wait for the reload). | |
| 993 { | |
| 994 TestExtensionRegistryObserver observer(registry(), extension_a); | |
| 995 util::SetIsIncognitoEnabled(extension_a, profile(), true); | |
| 996 observer.WaitForExtensionLoaded(); | |
| 997 } | |
| 998 | |
| 999 // Now, both extensions should be enabled in incognito mode. In addition, the | |
| 1000 // incognito toolbar should have expanded to show extension a (since it isn't | |
| 1001 // overflowed in the main bar). | |
| 1002 EXPECT_EQ(2u, incognito_model->toolbar_items().size()); | |
| 1003 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u, incognito_model)->id()); | |
| 1004 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u, incognito_model)->id()); | |
| 1005 EXPECT_EQ(1u, incognito_model->visible_icon_count()); | |
| 1006 } | |
| 1007 | |
| 1008 // Test that hiding actions on the toolbar results in sending them to the | |
| 1009 // overflow menu when the redesign switch is enabled. | |
| 1010 TEST_F(ExtensionToolbarModelUnitTest, | |
| 1011 ExtensionToolbarActionsVisibilityWithSwitch) { | |
| 1012 FeatureSwitch::ScopedOverride enable_redesign( | |
| 1013 FeatureSwitch::extension_action_redesign(), true); | |
| 1014 Init(); | |
| 1015 | |
| 1016 // We choose to use all types of extensions here, since the misnamed | |
| 1017 // BrowserActionVisibility is now for toolbar visibility. | |
| 1018 ASSERT_TRUE(AddActionExtensions()); | |
| 1019 | |
| 1020 // For readability, alias extensions A B C. | |
| 1021 const Extension* extension_a = browser_action(); | |
| 1022 const Extension* extension_b = page_action(); | |
| 1023 const Extension* extension_c = no_action(); | |
| 1024 | |
| 1025 // Sanity check: Order should start as A B C, with all three visible. | |
| 1026 EXPECT_EQ(3u, num_toolbar_items()); | |
| 1027 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 1028 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); | |
| 1029 EXPECT_EQ(extension_b, GetExtensionAtIndex(1u)); | |
| 1030 EXPECT_EQ(extension_c, GetExtensionAtIndex(2u)); | |
| 1031 | |
| 1032 ExtensionActionAPI* action_api = ExtensionActionAPI::Get(profile()); | |
| 1033 | |
| 1034 // By default, all actions should be visible. | |
| 1035 EXPECT_TRUE(action_api->GetBrowserActionVisibility(extension_a->id())); | |
| 1036 EXPECT_TRUE(action_api->GetBrowserActionVisibility(extension_c->id())); | |
| 1037 EXPECT_TRUE(action_api->GetBrowserActionVisibility(extension_b->id())); | |
| 1038 | |
| 1039 // Hiding an action should result in it being sent to the overflow menu. | |
| 1040 action_api->SetBrowserActionVisibility(extension_b->id(), false); | |
| 1041 | |
| 1042 // Thus, the order should be A C B, with B in the overflow. | |
| 1043 EXPECT_EQ(3u, num_toolbar_items()); | |
| 1044 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 1045 EXPECT_EQ(extension_a, GetExtensionAtIndex(0u)); | |
| 1046 EXPECT_EQ(extension_c, GetExtensionAtIndex(1u)); | |
| 1047 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | |
| 1048 | |
| 1049 // Hiding an extension's action should result in it being sent to the overflow | |
| 1050 // as well, but as the _first_ extension in the overflow. | |
| 1051 action_api->SetBrowserActionVisibility(extension_a->id(), false); | |
| 1052 // Thus, the order should be C A B, with A and B in the overflow. | |
| 1053 EXPECT_EQ(3u, num_toolbar_items()); | |
| 1054 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); | |
| 1055 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | |
| 1056 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | |
| 1057 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | |
| 1058 | |
| 1059 // Resetting A's visibility to true should send it back to the visible icons | |
| 1060 // (and should grow visible icons by 1), but it should be added to the end of | |
| 1061 // the visible icon list (not to its original position). | |
| 1062 action_api->SetBrowserActionVisibility(extension_a->id(), true); | |
| 1063 // So order is C A B, with only B in the overflow. | |
| 1064 EXPECT_EQ(3u, num_toolbar_items()); | |
| 1065 EXPECT_EQ(2u, toolbar_model()->visible_icon_count()); | |
| 1066 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | |
| 1067 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | |
| 1068 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | |
| 1069 | |
| 1070 // Resetting B to be visible should make the order C A B, with no overflow. | |
| 1071 action_api->SetBrowserActionVisibility(extension_b->id(), true); | |
| 1072 EXPECT_EQ(3u, num_toolbar_items()); | |
| 1073 EXPECT_TRUE(toolbar_model()->all_icons_visible()); | |
| 1074 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | |
| 1075 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | |
| 1076 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | |
| 1077 | |
| 1078 // Regression test for crbug.com/515963. Check that an extension's visibility | |
| 1079 // is updated when it is moved out because another extension was removed. | |
| 1080 toolbar_model()->SetVisibleIconCount(1); | |
| 1081 base::RunLoop().RunUntilIdle(); | |
| 1082 EXPECT_FALSE(action_api->GetBrowserActionVisibility(extension_a->id())); | |
| 1083 service()->DisableExtension(extension_c->id(), | |
| 1084 Extension::DISABLE_USER_ACTION); | |
| 1085 EXPECT_EQ(1u, toolbar_model()->visible_icon_count()); | |
| 1086 EXPECT_TRUE(action_api->GetBrowserActionVisibility(extension_a->id())); | |
| 1087 } | |
| 1088 | |
| 1089 // Test that observers receive no Added notifications until after the | |
| 1090 // ExtensionSystem has initialized. | |
| 1091 TEST_F(ExtensionToolbarModelUnitTest, ModelWaitsForExtensionSystemReady) { | |
| 1092 InitializeEmptyExtensionService(); | |
| 1093 ExtensionToolbarModel* toolbar_model = | |
| 1094 extension_action_test_util:: | |
| 1095 CreateToolbarModelForProfileWithoutWaitingForReady(profile()); | |
| 1096 ExtensionToolbarModelTestObserver model_observer(toolbar_model); | |
| 1097 EXPECT_TRUE(AddBrowserActionExtensions()); | |
| 1098 | |
| 1099 // Since the model hasn't been initialized (the ExtensionSystem::ready task | |
| 1100 // hasn't been run), there should be no insertion notifications. | |
| 1101 EXPECT_EQ(0u, model_observer.inserted_count()); | |
| 1102 EXPECT_EQ(0u, model_observer.initialized_count()); | |
| 1103 EXPECT_FALSE(toolbar_model->extensions_initialized()); | |
| 1104 | |
| 1105 // Run the ready task. | |
| 1106 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile()))-> | |
| 1107 SetReady(); | |
| 1108 // Run tasks posted to TestExtensionSystem. | |
| 1109 base::RunLoop().RunUntilIdle(); | |
| 1110 | |
| 1111 // We should still have no insertions, but should have an initialized count. | |
| 1112 EXPECT_TRUE(toolbar_model->extensions_initialized()); | |
| 1113 EXPECT_EQ(0u, model_observer.inserted_count()); | |
| 1114 EXPECT_EQ(1u, model_observer.initialized_count()); | |
| 1115 } | |
| 1116 | |
| 1117 // Check that the toolbar model correctly clears and reorders when it detects | |
| 1118 // a preference change. | |
| 1119 TEST_F(ExtensionToolbarModelUnitTest, ToolbarModelPrefChange) { | |
| 1120 Init(); | |
| 1121 | |
| 1122 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 1123 | |
| 1124 // We should start in the basic A B C order. | |
| 1125 ASSERT_TRUE(browser_action_a()); | |
| 1126 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0)); | |
| 1127 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1)); | |
| 1128 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2)); | |
| 1129 // Record the difference between the inserted and removed counts. The actual | |
| 1130 // value of the counts is not important, but we need to be sure that if we | |
| 1131 // call to remove any, we also add them back. | |
| 1132 size_t inserted_and_removed_difference = | |
| 1133 observer()->inserted_count() - observer()->removed_count(); | |
| 1134 | |
| 1135 // Assign a new order, B C A, and write it in the prefs. | |
| 1136 ExtensionIdList new_order; | |
| 1137 new_order.push_back(browser_action_b()->id()); | |
| 1138 new_order.push_back(browser_action_c()->id()); | |
| 1139 new_order.push_back(browser_action_a()->id()); | |
| 1140 ExtensionPrefs::Get(profile())->SetToolbarOrder(new_order); | |
| 1141 | |
| 1142 // Ensure everything has time to run. | |
| 1143 base::RunLoop().RunUntilIdle(); | |
| 1144 | |
| 1145 // The new order should be reflected in the model. | |
| 1146 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0)); | |
| 1147 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1)); | |
| 1148 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(2)); | |
| 1149 EXPECT_EQ(inserted_and_removed_difference, | |
| 1150 observer()->inserted_count() - observer()->removed_count()); | |
| 1151 } | |
| 1152 | |
| 1153 TEST_F(ExtensionToolbarModelUnitTest, ComponentExtesionsAddedToEnd) { | |
| 1154 Init(); | |
| 1155 | |
| 1156 ASSERT_TRUE(AddBrowserActionExtensions()); | |
| 1157 | |
| 1158 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0)); | |
| 1159 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1)); | |
| 1160 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2)); | |
| 1161 | |
| 1162 const char kName[] = "component"; | |
| 1163 DictionaryBuilder manifest; | |
| 1164 manifest.Set("name", kName) | |
| 1165 .Set("description", "An extension") | |
| 1166 .Set("manifest_version", 2) | |
| 1167 .Set("version", "1.0.0") | |
| 1168 .Set("browser_action", DictionaryBuilder().Pass()); | |
| 1169 scoped_refptr<const Extension> component_extension = | |
| 1170 ExtensionBuilder().SetManifest(manifest.Pass()) | |
| 1171 .SetID(crx_file::id_util::GenerateId(kName)) | |
| 1172 .SetLocation(Manifest::COMPONENT) | |
| 1173 .Build(); | |
| 1174 service()->AddExtension(component_extension.get()); | |
| 1175 | |
| 1176 EXPECT_EQ(component_extension.get(), GetExtensionAtIndex(0)); | |
| 1177 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1)); | |
| 1178 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2)); | |
| 1179 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(3)); | |
| 1180 } | |
| 1181 | |
| 1182 TEST_F(ExtensionToolbarModelUnitTest, | |
| 1183 ToolbarModelHighlightsForToolbarRedesign) { | |
| 1184 FeatureSwitch::ScopedOverride enable_redesign( | |
| 1185 FeatureSwitch::extension_action_redesign(), true); | |
| 1186 InitializeEmptyExtensionService(); | |
| 1187 EXPECT_TRUE(AddActionExtensions()); | |
| 1188 ExtensionToolbarModel* toolbar_model = | |
| 1189 extension_action_test_util::CreateToolbarModelForProfile(profile()); | |
| 1190 EXPECT_TRUE(toolbar_model); | |
| 1191 base::RunLoop().RunUntilIdle(); | |
| 1192 | |
| 1193 EXPECT_TRUE(ExtensionToolbarIconSurfacingBubbleDelegate::ShouldShowForProfile( | |
| 1194 profile())); | |
| 1195 EXPECT_TRUE(toolbar_model->is_highlighting()); | |
| 1196 EXPECT_EQ(ExtensionToolbarModel::HIGHLIGHT_INFO, | |
| 1197 toolbar_model->highlight_type()); | |
| 1198 EXPECT_EQ(3u, toolbar_model->visible_icon_count()); | |
| 1199 EXPECT_EQ(3u, toolbar_model->toolbar_items().size()); | |
| 1200 | |
| 1201 scoped_ptr<ToolbarActionsBarBubbleDelegate> bubble( | |
| 1202 new ExtensionToolbarIconSurfacingBubbleDelegate(profile())); | |
| 1203 bubble->OnBubbleClosed(ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS); | |
| 1204 | |
| 1205 EXPECT_FALSE(toolbar_model->is_highlighting()); | |
| 1206 EXPECT_EQ(ExtensionToolbarModel::HIGHLIGHT_NONE, | |
| 1207 toolbar_model->highlight_type()); | |
| 1208 } | |
| 1209 | |
| 1210 } // namespace extensions | |
| OLD | NEW |