| 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 ExtensionPrefsAppLaunchOrdinal : 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::CreateInitialOrdinal(); |
| 677 EXPECT_TRUE(StringOrdinal::CreateInitialOrdinal().Equal( |
| 678 prefs()->GetNextAppLaunchOrdinal(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_ordinal = |
| 684 // Extension should have been assigned a launch index > 0. | 688 prefs()->GetAppLaunchOrdinal(extension_->id()); |
| 685 EXPECT_GT(launch_index, 0); | 689 StringOrdinal page_ordinal = StringOrdinal::CreateInitialOrdinal(); |
| 686 EXPECT_EQ(launch_index + 1, prefs()->GetNextAppLaunchIndex(0)); | |
| 687 // Set a new launch index of one higher and verify. | |
| 688 prefs()->SetAppLaunchIndex(extension_->id(), | |
| 689 prefs()->GetNextAppLaunchIndex(0)); | |
| 690 int new_launch_index = prefs()->GetAppLaunchIndex(extension_->id()); | |
| 691 EXPECT_EQ(launch_index + 1, new_launch_index); | |
| 692 | 690 |
| 693 // This extension doesn't exist, so it should return -1. | 691 // Extension should have been assigned a valid StringOrdinal. |
| 694 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); | 692 EXPECT_TRUE(launch_ordinal.IsValid()); |
| 693 EXPECT_TRUE(launch_ordinal.LessThan( |
| 694 prefs()->GetNextAppLaunchOrdinal(page_ordinal))); |
| 695 // Set a new launch ordinal of and verify it comes after. |
| 696 prefs()->SetAppLaunchOrdinal( |
| 697 extension_->id(), |
| 698 prefs()->GetNextAppLaunchOrdinal(page_ordinal)); |
| 699 StringOrdinal new_launch_ordinal = |
| 700 prefs()->GetAppLaunchOrdinal(extension_->id()); |
| 701 EXPECT_TRUE(launch_ordinal.LessThan(new_launch_ordinal)); |
| 695 | 702 |
| 696 // The second page doesn't have any apps so its next launch index should | 703 // This extension doesn't exist, so it should return an invalid |
| 697 // still be 0. | 704 // StringOrdinal. |
| 698 EXPECT_EQ(prefs()->GetNextAppLaunchIndex(1), 0); | 705 StringOrdinal invalid_app_launch_ordinal = |
| 706 prefs()->GetAppLaunchOrdinal("foo"); |
| 707 EXPECT_FALSE(invalid_app_launch_ordinal.IsValid()); |
| 708 EXPECT_EQ(-1, prefs()->PageStringOrdinalAsInteger( |
| 709 invalid_app_launch_ordinal)); |
| 710 |
| 711 // The second page doesn't have any apps so its next launch ordinal should |
| 712 // be the first launch ordinal. |
| 713 StringOrdinal next_page = page_ordinal.CreateAfter(); |
| 714 StringOrdinal next_page_app_launch_ordinal = |
| 715 prefs()->GetNextAppLaunchOrdinal(next_page); |
| 716 EXPECT_TRUE(next_page_app_launch_ordinal.Equal( |
| 717 prefs()->CreateFirstAppLaunchOrdinal(next_page))); |
| 699 } | 718 } |
| 700 | 719 |
| 701 private: | 720 private: |
| 702 scoped_refptr<Extension> extension_; | 721 scoped_refptr<Extension> extension_; |
| 703 }; | 722 }; |
| 704 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} | 723 TEST_F(ExtensionPrefsAppLaunchOrdinal, ExtensionPrefsAppLaunchOrdinal) {} |
| 705 | 724 |
| 706 class ExtensionPrefsPageIndex : public ExtensionPrefsTest { | 725 class ExtensionPrefsPageOrdinal : public ExtensionPrefsTest { |
| 707 public: | 726 public: |
| 708 virtual void Initialize() { | 727 virtual void Initialize() { |
| 709 extension_ = prefs_.AddApp("page_index"); | 728 extension_ = prefs_.AddApp("page_ordinal"); |
| 710 // Install to page 3 (index 2). | 729 // Install with a page preference. |
| 730 StringOrdinal page = StringOrdinal::CreateInitialOrdinal(); |
| 711 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 731 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 712 false, 2); | 732 false, page); |
| 713 EXPECT_EQ(2, prefs()->GetPageIndex(extension_->id())); | 733 EXPECT_TRUE(page.Equal(prefs()->GetPageOrdinal(extension_->id()))); |
| 734 EXPECT_EQ(0, prefs()->PageStringOrdinalAsInteger(page)); |
| 714 | 735 |
| 715 scoped_refptr<Extension> extension2 = prefs_.AddApp("page_index_2"); | 736 scoped_refptr<Extension> extension2 = prefs_.AddApp("page_ordinal_2"); |
| 716 // Install without any page preference. | 737 // Install without any page preference. |
| 717 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 738 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 718 false, -1); | 739 false, StringOrdinal()); |
| 719 EXPECT_EQ(0, prefs()->GetPageIndex(extension_->id())); | 740 EXPECT_TRUE(prefs()->GetPageOrdinal(extension_->id()).IsValid()); |
| 720 } | 741 } |
| 721 | 742 |
| 722 virtual void Verify() { | 743 virtual void Verify() { |
| 723 // Set the page index. | 744 StringOrdinal old_page = prefs()->GetPageOrdinal(extension_->id()); |
| 724 prefs()->SetPageIndex(extension_->id(), 1); | 745 StringOrdinal new_page = old_page.CreateAfter(); |
| 725 // Verify the page index. | |
| 726 EXPECT_EQ(1, prefs()->GetPageIndex(extension_->id())); | |
| 727 | 746 |
| 728 // This extension doesn't exist, so it should return -1. | 747 // Set the page ordinal. |
| 729 EXPECT_EQ(-1, prefs()->GetPageIndex("foo")); | 748 prefs()->SetPageOrdinal(extension_->id(), new_page); |
| 749 // Verify the page ordinal. |
| 750 EXPECT_TRUE(new_page.Equal(prefs()->GetPageOrdinal(extension_->id()))); |
| 751 EXPECT_EQ(1, prefs()->PageStringOrdinalAsInteger(new_page)); |
| 752 |
| 753 // This extension doesn't exist, so it should return an invalid |
| 754 // StringOrdinal. |
| 755 EXPECT_FALSE(prefs()->GetPageOrdinal("foo").IsValid()); |
| 730 } | 756 } |
| 731 | 757 |
| 732 private: | 758 private: |
| 733 scoped_refptr<Extension> extension_; | 759 scoped_refptr<Extension> extension_; |
| 734 }; | 760 }; |
| 735 TEST_F(ExtensionPrefsPageIndex, ExtensionPrefsPageIndex) {} | 761 TEST_F(ExtensionPrefsPageOrdinal, ExtensionPrefsPageOrdinal) {} |
| 736 | 762 |
| 737 class ExtensionPrefsAppLocation : public ExtensionPrefsTest { | 763 class ExtensionPrefsAppLocation : public ExtensionPrefsTest { |
| 738 public: | 764 public: |
| 739 virtual void Initialize() { | 765 virtual void Initialize() { |
| 740 extension_ = prefs_.AddExtension("not_an_app"); | 766 extension_ = prefs_.AddExtension("not_an_app"); |
| 741 // Non-apps should not have any app launch index or page index. | 767 // Non-apps should not have any app launch ordinal or page ordinal. |
| 742 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 768 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 743 false, 0); | 769 false, StringOrdinal()); |
| 744 } | 770 } |
| 745 | 771 |
| 746 virtual void Verify() { | 772 virtual void Verify() { |
| 747 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex(extension_->id())); | 773 EXPECT_FALSE(prefs()->GetAppLaunchOrdinal(extension_->id()).IsValid()); |
| 748 EXPECT_EQ(-1, prefs()->GetPageIndex(extension_->id())); | 774 EXPECT_FALSE(prefs()->GetPageOrdinal(extension_->id()).IsValid()); |
| 749 } | 775 } |
| 750 | 776 |
| 751 private: | 777 private: |
| 752 scoped_refptr<Extension> extension_; | 778 scoped_refptr<Extension> extension_; |
| 753 }; | 779 }; |
| 754 TEST_F(ExtensionPrefsAppLocation, ExtensionPrefsAppLocation) {} | 780 TEST_F(ExtensionPrefsAppLocation, ExtensionPrefsAppLocation) {} |
| 755 | 781 |
| 756 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest { | 782 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest { |
| 757 public: | 783 public: |
| 758 virtual void Initialize() { | 784 virtual void Initialize() { |
| 759 extension_ = prefs_.AddExtension("on_extension_installed"); | 785 extension_ = prefs_.AddExtension("on_extension_installed"); |
| 760 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id())); | 786 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id())); |
| 761 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, | 787 prefs()->OnExtensionInstalled(extension_.get(), Extension::ENABLED, |
| 762 false, -1); | 788 false, StringOrdinal()); |
| 763 } | 789 } |
| 764 | 790 |
| 765 virtual void Verify() { | 791 virtual void Verify() { |
| 766 // Set the flag and see if it persisted. | 792 // Set the flag and see if it persisted. |
| 767 prefs()->SetAppDraggedByUser(extension_->id()); | 793 prefs()->SetAppDraggedByUser(extension_->id()); |
| 768 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); | 794 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); |
| 769 | 795 |
| 770 // Make sure it doesn't change on consecutive calls. | 796 // Make sure it doesn't change on consecutive calls. |
| 771 prefs()->SetAppDraggedByUser(extension_->id()); | 797 prefs()->SetAppDraggedByUser(extension_->id()); |
| 772 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); | 798 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id())); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 | 928 |
| 903 // Flags indicating whether each of the extensions has been installed, yet. | 929 // Flags indicating whether each of the extensions has been installed, yet. |
| 904 bool installed[3]; | 930 bool installed[3]; |
| 905 | 931 |
| 906 private: | 932 private: |
| 907 void EnsureExtensionInstalled(Extension *ext) { | 933 void EnsureExtensionInstalled(Extension *ext) { |
| 908 // Install extension the first time a preference is set for it. | 934 // Install extension the first time a preference is set for it. |
| 909 Extension* extensions[] = {ext1_, ext2_, ext3_}; | 935 Extension* extensions[] = {ext1_, ext2_, ext3_}; |
| 910 for (int i = 0; i < 3; ++i) { | 936 for (int i = 0; i < 3; ++i) { |
| 911 if (ext == extensions[i] && !installed[i]) { | 937 if (ext == extensions[i] && !installed[i]) { |
| 912 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, false, -1); | 938 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, |
| 939 false, StringOrdinal()); |
| 913 installed[i] = true; | 940 installed[i] = true; |
| 914 break; | 941 break; |
| 915 } | 942 } |
| 916 } | 943 } |
| 917 } | 944 } |
| 918 | 945 |
| 919 void EnsureExtensionUninstalled(const std::string& extension_id) { | 946 void EnsureExtensionUninstalled(const std::string& extension_id) { |
| 920 Extension* extensions[] = {ext1_, ext2_, ext3_}; | 947 Extension* extensions[] = {ext1_, ext2_, ext3_}; |
| 921 for (int i = 0; i < 3; ++i) { | 948 for (int i = 0; i < 3; ++i) { |
| 922 if (extensions[i]->id() == extension_id) { | 949 if (extensions[i]->id() == extension_id) { |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 ++iteration_; | 1236 ++iteration_; |
| 1210 } else { | 1237 } else { |
| 1211 EXPECT_EQ(kDefaultPref1, actual); | 1238 EXPECT_EQ(kDefaultPref1, actual); |
| 1212 } | 1239 } |
| 1213 } | 1240 } |
| 1214 | 1241 |
| 1215 private: | 1242 private: |
| 1216 int iteration_; | 1243 int iteration_; |
| 1217 }; | 1244 }; |
| 1218 TEST_F(ExtensionPrefsDisableExtensions, ExtensionPrefsDisableExtensions) {} | 1245 TEST_F(ExtensionPrefsDisableExtensions, ExtensionPrefsDisableExtensions) {} |
| OLD | NEW |