| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "base/scoped_temp_dir.h" | 7 #include "base/scoped_temp_dir.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "chrome/browser/extensions/extension_prefs.h" | 11 #include "chrome/browser/extensions/extension_prefs.h" |
| 12 #include "chrome/browser/extensions/test_extension_prefs.h" | 12 #include "chrome/browser/extensions/test_extension_prefs.h" |
| 13 #include "chrome/browser/prefs/pref_change_registrar.h" | 13 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 14 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 15 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/extensions/extension_constants.h" | 16 #include "chrome/common/extensions/extension_constants.h" |
| 17 #include "chrome/common/extensions/extension_permission_set.h" | 17 #include "chrome/common/extensions/extension_permission_set.h" |
| 18 #include "chrome/common/string_ordinal.h" |
| 18 #include "content/public/browser/notification_details.h" | 19 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 20 #include "content/test/notification_observer_mock.h" | 21 #include "content/test/notification_observer_mock.h" |
| 21 #include "content/test/test_browser_thread.h" | 22 #include "content/test/test_browser_thread.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 24 |
| 24 using base::Time; | 25 using base::Time; |
| 25 using base::TimeDelta; | 26 using base::TimeDelta; |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 | 28 |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 std::string id4_; | 648 std::string id4_; |
| 648 }; | 649 }; |
| 649 TEST_F(ExtensionPrefsIdleInstallInfo, IdleInstallInfo) {} | 650 TEST_F(ExtensionPrefsIdleInstallInfo, IdleInstallInfo) {} |
| 650 | 651 |
| 651 class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest { | 652 class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest { |
| 652 public: | 653 public: |
| 653 virtual void Initialize() { | 654 virtual void Initialize() { |
| 654 extension_ = prefs_.AddExtension("on_extension_installed"); | 655 extension_ = prefs_.AddExtension("on_extension_installed"); |
| 655 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id())); | 656 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id())); |
| 656 prefs()->OnExtensionInstalled( | 657 prefs()->OnExtensionInstalled( |
| 657 extension_.get(), Extension::DISABLED, false, -1); | 658 extension_.get(), Extension::DISABLED, false, |
| 659 StringOrdinal()); |
| 658 } | 660 } |
| 659 | 661 |
| 660 virtual void Verify() { | 662 virtual void Verify() { |
| 661 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_->id())); | 663 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_->id())); |
| 662 } | 664 } |
| 663 | 665 |
| 664 private: | 666 private: |
| 665 scoped_refptr<Extension> extension_; | 667 scoped_refptr<Extension> extension_; |
| 666 }; | 668 }; |
| 667 TEST_F(ExtensionPrefsOnExtensionInstalled, | 669 TEST_F(ExtensionPrefsOnExtensionInstalled, |
| 668 ExtensionPrefsOnExtensionInstalled) {} | 670 ExtensionPrefsOnExtensionInstalled) {} |
| 669 | 671 |
| 670 class ExtensionPrefsAppLaunchIndex : public ExtensionPrefsTest { | 672 class ExtensionPrefsAppLaunchIndex : public ExtensionPrefsTest { |
| 671 public: | 673 public: |
| 672 virtual void Initialize() { | 674 virtual void Initialize() { |
| 673 // No extensions yet. | 675 // No extensions yet. |
| 674 EXPECT_EQ(0, prefs()->GetNextAppLaunchIndex(0)); | 676 StringOrdinal page = StringOrdinal::CreateValidOrdinal(); |
| 677 EXPECT_TRUE(StringOrdinal::CreateValidOrdinal().Equal( |
| 678 prefs()->GetNextAppLaunchIndex(page))); |
| 675 | 679 |
| 676 extension_ = prefs_.AddApp("on_extension_installed"); | 680 extension_ = prefs_.AddApp("on_extension_installed"); |
| 677 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id())); | 681 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id())); |
| 678 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 682 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 679 false, -1); | 683 false, StringOrdinal()); |
| 680 } | 684 } |
| 681 | 685 |
| 682 virtual void Verify() { | 686 virtual void Verify() { |
| 683 int launch_index = prefs()->GetAppLaunchIndex(extension_->id()); | 687 StringOrdinal launch_index = prefs()->GetAppLaunchIndex(extension_->id()); |
| 684 // Extension should have been assigned a launch index > 0. | 688 StringOrdinal page_index = StringOrdinal::CreateValidOrdinal(); |
| 685 EXPECT_GT(launch_index, 0); | 689 |
| 686 EXPECT_EQ(launch_index + 1, prefs()->GetNextAppLaunchIndex(0)); | 690 // Extension should have been assigned a valid StringOrdinal. |
| 687 // Set a new launch index of one higher and verify. | 691 EXPECT_TRUE(launch_index.IsValid()); |
| 692 EXPECT_TRUE(launch_index.LessThan( |
| 693 prefs()->GetNextAppLaunchIndex(page_index))); |
| 694 // Set a new launch index of and verify it comes after. |
| 688 prefs()->SetAppLaunchIndex(extension_->id(), | 695 prefs()->SetAppLaunchIndex(extension_->id(), |
| 689 prefs()->GetNextAppLaunchIndex(0)); | 696 prefs()->GetNextAppLaunchIndex(page_index)); |
| 690 int new_launch_index = prefs()->GetAppLaunchIndex(extension_->id()); | 697 StringOrdinal new_launch_index = |
| 691 EXPECT_EQ(launch_index + 1, new_launch_index); | 698 prefs()->GetAppLaunchIndex(extension_->id()); |
| 699 EXPECT_TRUE(launch_index.LessThan(new_launch_index)); |
| 692 | 700 |
| 693 // This extension doesn't exist, so it should return -1. | 701 // This extension doesn't exist, so it should return an invalid |
| 694 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); | 702 // StringOrdinal. |
| 703 StringOrdinal invalid_app_launch_index = prefs()->GetAppLaunchIndex("foo"); |
| 704 EXPECT_FALSE(invalid_app_launch_index.IsValid()); |
| 705 EXPECT_EQ(-1, prefs()->PageIndexAsInteger(invalid_app_launch_index)); |
| 695 | 706 |
| 696 // The second page doesn't have any apps so its next launch index should | 707 // The second page doesn't have any apps so its next launch index should |
| 697 // still be 0. | 708 // be the first launch index. |
| 698 EXPECT_EQ(prefs()->GetNextAppLaunchIndex(1), 0); | 709 StringOrdinal next_page = page_index.CreateAfter(); |
| 710 StringOrdinal next_page_app_launch_index = prefs()->GetNextAppLaunchIndex( |
| 711 next_page); |
| 712 EXPECT_TRUE(next_page_app_launch_index.Equal( |
| 713 prefs()->CreateFirstAppLaunchIndex(next_page))); |
| 699 } | 714 } |
| 700 | 715 |
| 701 private: | 716 private: |
| 702 scoped_refptr<Extension> extension_; | 717 scoped_refptr<Extension> extension_; |
| 703 }; | 718 }; |
| 704 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} | 719 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} |
| 705 | 720 |
| 706 class ExtensionPrefsPageIndex : public ExtensionPrefsTest { | 721 class ExtensionPrefsPageIndex : public ExtensionPrefsTest { |
| 707 public: | 722 public: |
| 708 virtual void Initialize() { | 723 virtual void Initialize() { |
| 709 extension_ = prefs_.AddApp("page_index"); | 724 extension_ = prefs_.AddApp("page_index"); |
| 710 // Install to page 3 (index 2). | 725 // Install with a page preference. |
| 726 StringOrdinal page = StringOrdinal::CreateValidOrdinal(); |
| 711 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 727 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 712 false, 2); | 728 false, page); |
| 713 EXPECT_EQ(2, prefs()->GetPageIndex(extension_->id())); | 729 EXPECT_TRUE(page.Equal(prefs()->GetPageIndex(extension_->id()))); |
| 730 EXPECT_EQ(0, prefs()->PageIndexAsInteger(page)); |
| 714 | 731 |
| 715 scoped_refptr<Extension> extension2 = prefs_.AddApp("page_index_2"); | 732 scoped_refptr<Extension> extension2 = prefs_.AddApp("page_index_2"); |
| 716 // Install without any page preference. | 733 // Install without any page preference. |
| 717 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 734 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 718 false, -1); | 735 false, StringOrdinal()); |
| 719 EXPECT_EQ(0, prefs()->GetPageIndex(extension_->id())); | 736 EXPECT_TRUE(prefs()->GetPageIndex(extension_->id()).IsValid()); |
| 720 } | 737 } |
| 721 | 738 |
| 722 virtual void Verify() { | 739 virtual void Verify() { |
| 740 StringOrdinal old_page = prefs()->GetPageIndex(extension_->id()); |
| 741 StringOrdinal new_page = old_page.CreateAfter(); |
| 742 |
| 723 // Set the page index. | 743 // Set the page index. |
| 724 prefs()->SetPageIndex(extension_->id(), 1); | 744 prefs()->SetPageIndex(extension_->id(), new_page); |
| 725 // Verify the page index. | 745 // Verify the page index. |
| 726 EXPECT_EQ(1, prefs()->GetPageIndex(extension_->id())); | 746 EXPECT_TRUE(new_page.Equal(prefs()->GetPageIndex(extension_->id()))); |
| 747 EXPECT_EQ(1, prefs()->PageIndexAsInteger(new_page)); |
| 727 | 748 |
| 728 // This extension doesn't exist, so it should return -1. | 749 // This extension doesn't exist, so it should return an invalid |
| 729 EXPECT_EQ(-1, prefs()->GetPageIndex("foo")); | 750 // StringOrdinal. |
| 751 EXPECT_FALSE(prefs()->GetPageIndex("foo").IsValid()); |
| 730 } | 752 } |
| 731 | 753 |
| 732 private: | 754 private: |
| 733 scoped_refptr<Extension> extension_; | 755 scoped_refptr<Extension> extension_; |
| 734 }; | 756 }; |
| 735 TEST_F(ExtensionPrefsPageIndex, ExtensionPrefsPageIndex) {} | 757 TEST_F(ExtensionPrefsPageIndex, ExtensionPrefsPageIndex) {} |
| 736 | 758 |
| 737 class ExtensionPrefsAppLocation : public ExtensionPrefsTest { | 759 class ExtensionPrefsAppLocation : public ExtensionPrefsTest { |
| 738 public: | 760 public: |
| 739 virtual void Initialize() { | 761 virtual void Initialize() { |
| 740 extension_ = prefs_.AddExtension("not_an_app"); | 762 extension_ = prefs_.AddExtension("not_an_app"); |
| 741 // Non-apps should not have any app launch index or page index. | 763 // Non-apps should not have any app launch index or page index. |
| 742 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 764 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 743 false, 0); | 765 false, StringOrdinal::CreateValidOrdinal()); |
| 744 } | 766 } |
| 745 | 767 |
| 746 virtual void Verify() { | 768 virtual void Verify() { |
| 747 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex(extension_->id())); | 769 EXPECT_FALSE(prefs()->GetAppLaunchIndex(extension_->id()).IsValid()); |
| 748 EXPECT_EQ(-1, prefs()->GetPageIndex(extension_->id())); | 770 EXPECT_FALSE(prefs()->GetPageIndex(extension_->id()).IsValid()); |
| 749 } | 771 } |
| 750 | 772 |
| 751 private: | 773 private: |
| 752 scoped_refptr<Extension> extension_; | 774 scoped_refptr<Extension> extension_; |
| 753 }; | 775 }; |
| 754 TEST_F(ExtensionPrefsAppLocation, ExtensionPrefsAppLocation) {} | 776 TEST_F(ExtensionPrefsAppLocation, ExtensionPrefsAppLocation) {} |
| 755 | 777 |
| 756 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest { | 778 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest { |
| 757 public: | 779 public: |
| 758 virtual void Initialize() { | 780 virtual void Initialize() { |
| 759 extension_ = prefs_.AddExtension("on_extension_installed"); | 781 extension_ = prefs_.AddExtension("on_extension_installed"); |
| 760 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id())); | 782 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id())); |
| 761 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 783 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 762 false, -1); | 784 false, StringOrdinal()); |
| 763 } | 785 } |
| 764 | 786 |
| 765 virtual void Verify() { | 787 virtual void Verify() { |
| 766 // Set the flag and see if it persisted. | 788 // Set the flag and see if it persisted. |
| 767 prefs()->SetAppDraggedByUser(extension_->id()); | 789 prefs()->SetAppDraggedByUser(extension_->id()); |
| 768 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); | 790 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); |
| 769 | 791 |
| 770 // Make sure it doesn't change on consecutive calls. | 792 // Make sure it doesn't change on consecutive calls. |
| 771 prefs()->SetAppDraggedByUser(extension_->id()); | 793 prefs()->SetAppDraggedByUser(extension_->id()); |
| 772 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); | 794 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 | 924 |
| 903 // Flags indicating whether each of the extensions has been installed, yet. | 925 // Flags indicating whether each of the extensions has been installed, yet. |
| 904 bool installed[3]; | 926 bool installed[3]; |
| 905 | 927 |
| 906 private: | 928 private: |
| 907 void EnsureExtensionInstalled(Extension *ext) { | 929 void EnsureExtensionInstalled(Extension *ext) { |
| 908 // Install extension the first time a preference is set for it. | 930 // Install extension the first time a preference is set for it. |
| 909 Extension* extensions[] = {ext1_, ext2_, ext3_}; | 931 Extension* extensions[] = {ext1_, ext2_, ext3_}; |
| 910 for (int i = 0; i < 3; ++i) { | 932 for (int i = 0; i < 3; ++i) { |
| 911 if (ext == extensions[i] && !installed[i]) { | 933 if (ext == extensions[i] && !installed[i]) { |
| 912 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, false, -1); | 934 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, |
| 935 false, StringOrdinal()); |
| 913 installed[i] = true; | 936 installed[i] = true; |
| 914 break; | 937 break; |
| 915 } | 938 } |
| 916 } | 939 } |
| 917 } | 940 } |
| 918 | 941 |
| 919 void EnsureExtensionUninstalled(const std::string& extension_id) { | 942 void EnsureExtensionUninstalled(const std::string& extension_id) { |
| 920 Extension* extensions[] = {ext1_, ext2_, ext3_}; | 943 Extension* extensions[] = {ext1_, ext2_, ext3_}; |
| 921 for (int i = 0; i < 3; ++i) { | 944 for (int i = 0; i < 3; ++i) { |
| 922 if (extensions[i]->id() == extension_id) { | 945 if (extensions[i]->id() == extension_id) { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 ++iteration_; | 1232 ++iteration_; |
| 1210 } else { | 1233 } else { |
| 1211 EXPECT_EQ(kDefaultPref1, actual); | 1234 EXPECT_EQ(kDefaultPref1, actual); |
| 1212 } | 1235 } |
| 1213 } | 1236 } |
| 1214 | 1237 |
| 1215 private: | 1238 private: |
| 1216 int iteration_; | 1239 int iteration_; |
| 1217 }; | 1240 }; |
| 1218 TEST_F(ExtensionPrefsDisableExtensions, ExtensionPrefsDisableExtensions) {} | 1241 TEST_F(ExtensionPrefsDisableExtensions, ExtensionPrefsDisableExtensions) {} |
| OLD | NEW |