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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" | 8 #include "chrome/browser/extensions/api/extension_action/extension_action_api.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 scoped_refptr<const Extension> extension2 = | 325 scoped_refptr<const Extension> extension2 = |
326 GetActionExtension("browser_action", manifest_keys::kBrowserAction); | 326 GetActionExtension("browser_action", manifest_keys::kBrowserAction); |
327 ASSERT_TRUE(AddExtension(extension2)); | 327 ASSERT_TRUE(AddExtension(extension2)); |
328 | 328 |
329 // We should now find our extension in the model. | 329 // We should now find our extension in the model. |
330 EXPECT_EQ(1u, observer()->inserted_count()); | 330 EXPECT_EQ(1u, observer()->inserted_count()); |
331 EXPECT_EQ(1u, num_toolbar_items()); | 331 EXPECT_EQ(1u, num_toolbar_items()); |
332 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); | 332 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); |
333 | 333 |
334 // Should be a no-op, but still fires the events. | 334 // Should be a no-op, but still fires the events. |
335 toolbar_model()->MoveExtensionIcon(extension2.get(), 0); | 335 toolbar_model()->MoveExtensionIcon(extension2->id(), 0); |
336 EXPECT_EQ(1u, observer()->moved_count()); | 336 EXPECT_EQ(1u, observer()->moved_count()); |
337 EXPECT_EQ(1u, num_toolbar_items()); | 337 EXPECT_EQ(1u, num_toolbar_items()); |
338 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); | 338 EXPECT_EQ(extension2.get(), GetExtensionAtIndex(0u)); |
339 | 339 |
340 // Remove the extension and verify. | 340 // Remove the extension and verify. |
341 ASSERT_TRUE(RemoveExtension(extension2)); | 341 ASSERT_TRUE(RemoveExtension(extension2)); |
342 EXPECT_EQ(1u, observer()->removed_count()); | 342 EXPECT_EQ(1u, observer()->removed_count()); |
343 EXPECT_EQ(0u, num_toolbar_items()); | 343 EXPECT_EQ(0u, num_toolbar_items()); |
344 EXPECT_EQ(NULL, GetExtensionAtIndex(0u)); | 344 EXPECT_EQ(NULL, GetExtensionAtIndex(0u)); |
345 } | 345 } |
346 | 346 |
347 // Test various different reorderings, removals, and reinsertions. | 347 // Test various different reorderings, removals, and reinsertions. |
348 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarReorderAndReinsert) { | 348 TEST_F(ExtensionToolbarModelUnitTest, ExtensionToolbarReorderAndReinsert) { |
349 Init(); | 349 Init(); |
350 | 350 |
351 // Add the three browser action extensions. | 351 // Add the three browser action extensions. |
352 ASSERT_TRUE(AddBrowserActionExtensions()); | 352 ASSERT_TRUE(AddBrowserActionExtensions()); |
353 | 353 |
354 // Verify the three extensions are in the model in the proper order. | 354 // Verify the three extensions are in the model in the proper order. |
355 EXPECT_EQ(3u, num_toolbar_items()); | 355 EXPECT_EQ(3u, num_toolbar_items()); |
356 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 356 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
357 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | 357 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
358 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | 358 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); |
359 | 359 |
360 // Order is now A, B, C. Let's put C first. | 360 // Order is now A, B, C. Let's put C first. |
361 toolbar_model()->MoveExtensionIcon(browser_action_c(), 0); | 361 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 0); |
362 EXPECT_EQ(1u, observer()->moved_count()); | 362 EXPECT_EQ(1u, observer()->moved_count()); |
363 EXPECT_EQ(3u, num_toolbar_items()); | 363 EXPECT_EQ(3u, num_toolbar_items()); |
364 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | 364 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); |
365 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); | 365 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); |
366 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); | 366 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); |
367 | 367 |
368 // Order is now C, A, B. Let's put A last. | 368 // Order is now C, A, B. Let's put A last. |
369 toolbar_model()->MoveExtensionIcon(browser_action_a(), 2); | 369 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 2); |
370 EXPECT_EQ(2u, observer()->moved_count()); | 370 EXPECT_EQ(2u, observer()->moved_count()); |
371 EXPECT_EQ(3u, num_toolbar_items()); | 371 EXPECT_EQ(3u, num_toolbar_items()); |
372 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | 372 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); |
373 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | 373 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
374 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(2u)); | 374 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(2u)); |
375 | 375 |
376 // Order is now C, B, A. Let's remove B. | 376 // Order is now C, B, A. Let's remove B. |
377 ASSERT_TRUE(RemoveExtension(browser_action_b())); | 377 ASSERT_TRUE(RemoveExtension(browser_action_b())); |
378 EXPECT_EQ(1u, observer()->removed_count()); | 378 EXPECT_EQ(1u, observer()->removed_count()); |
379 EXPECT_EQ(2u, num_toolbar_items()); | 379 EXPECT_EQ(2u, num_toolbar_items()); |
380 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); | 380 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(0u)); |
381 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); | 381 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(1u)); |
382 | 382 |
383 // Load extension B again. | 383 // Load extension B again. |
384 ASSERT_TRUE(AddExtension(browser_action_b())); | 384 ASSERT_TRUE(AddExtension(browser_action_b())); |
385 | 385 |
386 // Extension B loaded again. | 386 // Extension B loaded again. |
387 EXPECT_EQ(4u, observer()->inserted_count()); | 387 EXPECT_EQ(4u, observer()->inserted_count()); |
388 EXPECT_EQ(3u, num_toolbar_items()); | 388 EXPECT_EQ(3u, num_toolbar_items()); |
389 // Make sure it gets its old spot in the list. | 389 // Make sure it gets its old spot in the list. |
390 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | 390 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
391 | 391 |
392 // Unload B again. | 392 // Unload B again. |
393 ASSERT_TRUE(RemoveExtension(browser_action_b())); | 393 ASSERT_TRUE(RemoveExtension(browser_action_b())); |
394 EXPECT_EQ(2u, observer()->removed_count()); | 394 EXPECT_EQ(2u, observer()->removed_count()); |
395 EXPECT_EQ(2u, num_toolbar_items()); | 395 EXPECT_EQ(2u, num_toolbar_items()); |
396 | 396 |
397 // Order is now C, A. Flip it. | 397 // Order is now C, A. Flip it. |
398 toolbar_model()->MoveExtensionIcon(browser_action_a(), 0); | 398 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 0); |
399 EXPECT_EQ(3u, observer()->moved_count()); | 399 EXPECT_EQ(3u, observer()->moved_count()); |
400 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 400 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
401 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | 401 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); |
402 | 402 |
403 // Move A to the location it already occupies. | 403 // Move A to the location it already occupies. |
404 toolbar_model()->MoveExtensionIcon(browser_action_a(), 0); | 404 toolbar_model()->MoveExtensionIcon(browser_action_a()->id(), 0); |
405 EXPECT_EQ(4u, observer()->moved_count()); | 405 EXPECT_EQ(4u, observer()->moved_count()); |
406 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 406 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
407 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | 407 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); |
408 | 408 |
409 // Order is now A, C. Remove C. | 409 // Order is now A, C. Remove C. |
410 ASSERT_TRUE(RemoveExtension(browser_action_c())); | 410 ASSERT_TRUE(RemoveExtension(browser_action_c())); |
411 EXPECT_EQ(3u, observer()->removed_count()); | 411 EXPECT_EQ(3u, observer()->removed_count()); |
412 EXPECT_EQ(1u, num_toolbar_items()); | 412 EXPECT_EQ(1u, num_toolbar_items()); |
413 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 413 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
414 | 414 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 service()->EnableExtension(browser_action_c()->id()); | 464 service()->EnableExtension(browser_action_c()->id()); |
465 service()->EnableExtension(browser_action_a()->id()); | 465 service()->EnableExtension(browser_action_a()->id()); |
466 service()->EnableExtension(browser_action_b()->id()); | 466 service()->EnableExtension(browser_action_b()->id()); |
467 | 467 |
468 // Make sure we still get the original A, B, C order. | 468 // Make sure we still get the original A, B, C order. |
469 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 469 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
470 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); | 470 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(1u)); |
471 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); | 471 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(2u)); |
472 | 472 |
473 // Move browser_action_b() to be first. | 473 // Move browser_action_b() to be first. |
474 toolbar_model()->MoveExtensionIcon(browser_action_b(), 0); | 474 toolbar_model()->MoveExtensionIcon(browser_action_b()->id(), 0); |
475 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); | 475 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u)); |
476 | 476 |
477 // Uninstall Extension B. | 477 // Uninstall Extension B. |
478 service()->UninstallExtension(browser_action_b()->id(), | 478 service()->UninstallExtension(browser_action_b()->id(), |
479 UNINSTALL_REASON_FOR_TESTING, | 479 UNINSTALL_REASON_FOR_TESTING, |
480 base::Bind(&base::DoNothing), | 480 base::Bind(&base::DoNothing), |
481 NULL); // Ignore error. | 481 NULL); // Ignore error. |
482 // List contains only A and C now. Validate that. | 482 // List contains only A and C now. Validate that. |
483 EXPECT_EQ(2u, num_toolbar_items()); | 483 EXPECT_EQ(2u, num_toolbar_items()); |
484 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 484 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 // Give two extensions incognito access. | 782 // Give two extensions incognito access. |
783 // Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of | 783 // Note: We use ExtensionPrefs::SetIsIncognitoEnabled instead of |
784 // util::SetIsIncognitoEnabled because the latter tries to reload the | 784 // util::SetIsIncognitoEnabled because the latter tries to reload the |
785 // extension, which can cause problems in our unittest set up (and reloading | 785 // extension, which can cause problems in our unittest set up (and reloading |
786 // the extension is irrelevant to us). | 786 // the extension is irrelevant to us). |
787 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile()); | 787 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile()); |
788 extension_prefs->SetIsIncognitoEnabled(browser_action_b()->id(), true); | 788 extension_prefs->SetIsIncognitoEnabled(browser_action_b()->id(), true); |
789 extension_prefs->SetIsIncognitoEnabled(browser_action_c()->id(), true); | 789 extension_prefs->SetIsIncognitoEnabled(browser_action_c()->id(), true); |
790 | 790 |
791 // Move C to the second index. | 791 // Move C to the second index. |
792 toolbar_model()->MoveExtensionIcon(browser_action_c(), 1u); | 792 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 1u); |
793 // Set visible count to 2 so that c is overflowed. State is A C [B]. | 793 // Set visible count to 2 so that c is overflowed. State is A C [B]. |
794 toolbar_model()->SetVisibleIconCount(2); | 794 toolbar_model()->SetVisibleIconCount(2); |
795 EXPECT_EQ(1u, observer()->moved_count()); | 795 EXPECT_EQ(1u, observer()->moved_count()); |
796 | 796 |
797 // Get an incognito profile and toolbar. | 797 // Get an incognito profile and toolbar. |
798 ExtensionToolbarModel* incognito_model = | 798 ExtensionToolbarModel* incognito_model = |
799 CreateToolbarModelForProfile(profile()->GetOffTheRecordProfile()); | 799 CreateToolbarModelForProfile(profile()->GetOffTheRecordProfile()); |
800 | 800 |
801 ExtensionToolbarModelTestObserver incognito_observer(incognito_model); | 801 ExtensionToolbarModelTestObserver incognito_observer(incognito_model); |
802 EXPECT_EQ(0u, incognito_observer.moved_count()); | 802 EXPECT_EQ(0u, incognito_observer.moved_count()); |
(...skipping 15 matching lines...) Expand all Loading... |
818 EXPECT_EQ(0, incognito_model->GetVisibleIconCount()); | 818 EXPECT_EQ(0, incognito_model->GetVisibleIconCount()); |
819 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); | 819 EXPECT_EQ(2, toolbar_model()->GetVisibleIconCount()); |
820 | 820 |
821 // Expanding the incognito model to 2 should register as "all icons" (-1), | 821 // Expanding the incognito model to 2 should register as "all icons" (-1), |
822 // since it is all of the incognito-enabled extensions. | 822 // since it is all of the incognito-enabled extensions. |
823 incognito_model->SetVisibleIconCount(2u); | 823 incognito_model->SetVisibleIconCount(2u); |
824 EXPECT_EQ(-1, incognito_model->GetVisibleIconCount()); | 824 EXPECT_EQ(-1, incognito_model->GetVisibleIconCount()); |
825 | 825 |
826 // Moving icons in the incognito toolbar should not affect the regular | 826 // Moving icons in the incognito toolbar should not affect the regular |
827 // toolbar. Incognito currently has C B... | 827 // toolbar. Incognito currently has C B... |
828 incognito_model->MoveExtensionIcon(browser_action_b(), 0u); | 828 incognito_model->MoveExtensionIcon(browser_action_b()->id(), 0u); |
829 // So now it should be B C... | 829 // So now it should be B C... |
830 EXPECT_EQ(1u, incognito_observer.moved_count()); | 830 EXPECT_EQ(1u, incognito_observer.moved_count()); |
831 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); | 831 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(0u, incognito_model)); |
832 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); | 832 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u, incognito_model)); |
833 // ... and the regular toolbar should be unaffected. | 833 // ... and the regular toolbar should be unaffected. |
834 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); | 834 EXPECT_EQ(browser_action_a(), GetExtensionAtIndex(0u)); |
835 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); | 835 EXPECT_EQ(browser_action_c(), GetExtensionAtIndex(1u)); |
836 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); | 836 EXPECT_EQ(browser_action_b(), GetExtensionAtIndex(2u)); |
837 | 837 |
838 // Similarly, the observer for the regular model should not have received | 838 // Similarly, the observer for the regular model should not have received |
839 // any updates. | 839 // any updates. |
840 EXPECT_EQ(1u, observer()->moved_count()); | 840 EXPECT_EQ(1u, observer()->moved_count()); |
841 | 841 |
842 // And performing moves on the regular model should have no effect on the | 842 // And performing moves on the regular model should have no effect on the |
843 // incognito model or its observers. | 843 // incognito model or its observers. |
844 toolbar_model()->MoveExtensionIcon(browser_action_c(), 2u); | 844 toolbar_model()->MoveExtensionIcon(browser_action_c()->id(), 2u); |
845 EXPECT_EQ(2u, observer()->moved_count()); | 845 EXPECT_EQ(2u, observer()->moved_count()); |
846 EXPECT_EQ(1u, incognito_observer.moved_count()); | 846 EXPECT_EQ(1u, incognito_observer.moved_count()); |
847 } | 847 } |
848 | 848 |
849 // Test that hiding actions on the toolbar results in sending them to the | 849 // Test that hiding actions on the toolbar results in sending them to the |
850 // overflow menu when the redesign switch is enabled. | 850 // overflow menu when the redesign switch is enabled. |
851 TEST_F(ExtensionToolbarModelUnitTest, | 851 TEST_F(ExtensionToolbarModelUnitTest, |
852 ExtensionToolbarActionsVisibilityWithSwitch) { | 852 ExtensionToolbarActionsVisibilityWithSwitch) { |
853 FeatureSwitch::ScopedOverride enable_redesign( | 853 FeatureSwitch::ScopedOverride enable_redesign( |
854 FeatureSwitch::extension_action_redesign(), true); | 854 FeatureSwitch::extension_action_redesign(), true); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
918 ExtensionActionAPI::SetBrowserActionVisibility( | 918 ExtensionActionAPI::SetBrowserActionVisibility( |
919 prefs, extension_b->id(), true); | 919 prefs, extension_b->id(), true); |
920 EXPECT_EQ(3u, num_toolbar_items()); | 920 EXPECT_EQ(3u, num_toolbar_items()); |
921 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all' | 921 EXPECT_EQ(-1, toolbar_model()->GetVisibleIconCount()); // -1 = 'all' |
922 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); | 922 EXPECT_EQ(extension_c, GetExtensionAtIndex(0u)); |
923 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); | 923 EXPECT_EQ(extension_a, GetExtensionAtIndex(1u)); |
924 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); | 924 EXPECT_EQ(extension_b, GetExtensionAtIndex(2u)); |
925 } | 925 } |
926 | 926 |
927 } // namespace extensions | 927 } // namespace extensions |
OLD | NEW |