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

Side by Side Diff: chrome/browser/extensions/extension_prefs_unittest.cc

Issue 5213002: Fix for Bug 50726 "Save extension list and "winning" prefs from extensions" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Addressed Mattias' comments Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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-inl.h" 8 #include "base/stl_util-inl.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/browser_thread.h" 11 #include "chrome/browser/browser_thread.h"
12 #include "chrome/browser/extensions/extension_prefs.h" 12 #include "chrome/browser/extensions/extension_prefs.h"
13 #include "chrome/browser/extensions/test_extension_prefs.h" 13 #include "chrome/browser/extensions/test_extension_prefs.h"
14 #include "chrome/browser/prefs/pref_change_registrar.h"
14 #include "chrome/browser/prefs/pref_service.h" 15 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/extension_constants.h" 17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/test/mock_notification_observer.h"
17 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
18 20
19 using base::Time; 21 using base::Time;
20 using base::TimeDelta; 22 using base::TimeDelta;
21 23
24 const char kPref1[] = "path1.subpath";
25 const char kPref2[] = "path2";
26 const char kPref3[] = "path3";
27 const char kPref4[] = "path4";
28
29 // Default values in case an extension pref value is not overridden.
30 const char kDefaultPref1[] = "default pref 1";
31 const char kDefaultPref2[] = "default pref 2";
32 const char kDefaultPref3[] = "default pref 3";
33 const char kDefaultPref4[] = "default pref 4";
34
22 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) { 35 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) {
23 int schemes = URLPattern::SCHEME_ALL; 36 int schemes = URLPattern::SCHEME_ALL;
24 extent->AddPattern(URLPattern(schemes, pattern)); 37 extent->AddPattern(URLPattern(schemes, pattern));
25 } 38 }
26 39
27 static void AssertEqualExtents(ExtensionExtent* extent1, 40 static void AssertEqualExtents(ExtensionExtent* extent1,
28 ExtensionExtent* extent2) { 41 ExtensionExtent* extent2) {
29 std::vector<URLPattern> patterns1 = extent1->patterns(); 42 std::vector<URLPattern> patterns1 = extent1->patterns();
30 std::vector<URLPattern> patterns2 = extent2->patterns(); 43 std::vector<URLPattern> patterns2 = extent2->patterns();
31 std::set<std::string> strings1; 44 std::set<std::string> strings1;
(...skipping 16 matching lines...) Expand all
48 61
49 // This function will get called once, and is the right place to do operations 62 // This function will get called once, and is the right place to do operations
50 // on ExtensionPrefs that write data. 63 // on ExtensionPrefs that write data.
51 virtual void Initialize() = 0; 64 virtual void Initialize() = 0;
52 65
53 // This function will be called twice - once while the original ExtensionPrefs 66 // This function will be called twice - once while the original ExtensionPrefs
54 // object is still alive, and once after recreation. Thus, it tests that 67 // object is still alive, and once after recreation. Thus, it tests that
55 // things don't break after any ExtensionPrefs startup work. 68 // things don't break after any ExtensionPrefs startup work.
56 virtual void Verify() = 0; 69 virtual void Verify() = 0;
57 70
71 // This function is called to Register preference default values.
72 virtual void RegisterPreferences() {}
73
58 virtual void SetUp() { 74 virtual void SetUp() {
75 RegisterPreferences();
59 Initialize(); 76 Initialize();
60 } 77 }
61 78
62 virtual void TearDown() { 79 virtual void TearDown() {
63 Verify(); 80 Verify();
64 81
65 // Reset ExtensionPrefs, and re-verify. 82 // Reset ExtensionPrefs, and re-verify.
66 prefs_.RecreateExtensionPrefs(); 83 prefs_.RecreateExtensionPrefs();
84 RegisterPreferences();
67 Verify(); 85 Verify();
68 } 86 }
69 87
70 protected: 88 protected:
71 ExtensionPrefs* prefs() { return prefs_.prefs(); } 89 ExtensionPrefs* prefs() { return prefs_.prefs(); }
72 90
73 TestExtensionPrefs prefs_; 91 TestExtensionPrefs prefs_;
74 92
75 private: 93 private:
76 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefsTest); 94 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefsTest);
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 EXPECT_EQ(launch_index + 1, new_launch_index); 536 EXPECT_EQ(launch_index + 1, new_launch_index);
519 537
520 // This extension doesn't exist, so it should return -1. 538 // This extension doesn't exist, so it should return -1.
521 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); 539 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo"));
522 } 540 }
523 541
524 private: 542 private:
525 scoped_refptr<Extension> extension_; 543 scoped_refptr<Extension> extension_;
526 }; 544 };
527 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} 545 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {}
546
547 namespace keys = extension_manifest_keys;
548
549 class ExtensionPrefsPreferencesBase : public ExtensionPrefsTest {
550 public:
551 ExtensionPrefsPreferencesBase()
552 : ExtensionPrefsTest(),
553 ext1_(NULL),
554 ext2_(NULL),
555 ext3_(NULL),
556 installed() {
557 DictionaryValue simple_dict;
558 std::string error;
559
560 simple_dict.SetString(keys::kVersion, "1.0.0.0");
561 simple_dict.SetString(keys::kName, "unused");
562
563 ext1_scoped_ = Extension::Create(
564 prefs_.temp_dir().AppendASCII("ext1_"), Extension::INVALID,
565 simple_dict, false, &error);
566 ext2_scoped_ = Extension::Create(
567 prefs_.temp_dir().AppendASCII("ext2_"), Extension::INVALID,
568 simple_dict, false, &error);
569 ext3_scoped_ = Extension::Create(
570 prefs_.temp_dir().AppendASCII("ext3_"), Extension::INVALID,
571 simple_dict, false, &error);
572
573 ext1_ = ext1_scoped_.get();
574 ext2_ = ext2_scoped_.get();
575 ext3_ = ext3_scoped_.get();
576
577 for (size_t i = 0; i < arraysize(installed); ++i)
578 installed[i] = false;
579 }
580
581 void RegisterPreferences() {
582 prefs()->pref_service()->RegisterStringPref(kPref1, kDefaultPref1);
583 prefs()->pref_service()->RegisterStringPref(kPref2, kDefaultPref2);
584 prefs()->pref_service()->RegisterStringPref(kPref3, kDefaultPref3);
585 prefs()->pref_service()->RegisterStringPref(kPref4, kDefaultPref4);
586 }
587
588 void InstallExtControlledPref(Extension *ext,
589 const std::string& key,
590 Value* val) {
591 // Install extension the first time a preference is set for it.
592 Extension* extensions[] = {ext1_, ext2_, ext3_};
593 for (int i = 0; i < 3; ++i) {
594 if (ext == extensions[i] && !installed[i]) {
595 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, true);
596 installed[i] = true;
597 break;
598 }
599 }
600
601 prefs()->SetExtensionControlledPref(ext->id(), key, val);
602 }
603
604 void UninstallExtension(const std::string& extension_id) {
605 prefs()->OnExtensionUninstalled(extension_id, Extension::INTERNAL, false);
606 }
607
608 // Weak references, for convenience.
609 Extension* ext1_;
610 Extension* ext2_;
611 Extension* ext3_;
612
613 // Flags indicating whether each of the extensions has been installed, yet.
614 bool installed[3];
615
616 private:
617 scoped_refptr<Extension> ext1_scoped_;
618 scoped_refptr<Extension> ext2_scoped_;
619 scoped_refptr<Extension> ext3_scoped_;
620 };
621
622 class ExtensionPrefsInstallOneExtension
623 : public ExtensionPrefsPreferencesBase {
624 virtual void Initialize() {
625 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
626 }
627 virtual void Verify() {
628 std::string actual = prefs()->pref_service()->GetString(kPref1);
629 EXPECT_EQ("val1", actual);
630 }
631 };
632 TEST_F(ExtensionPrefsInstallOneExtension, ExtensionPrefsInstallOneExtension) {}
633
634 // Make sure the last-installed extension wins for each preference.
635 class ExtensionPrefsInstallOverwrittenExtensions
636 : public ExtensionPrefsPreferencesBase {
637 virtual void Initialize() {
638 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
639 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
640 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val3"));
641
642 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
643 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val5"));
644
645 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val6"));
646 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val7"));
647 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val8"));
648 }
649 virtual void Verify() {
650 std::string actual;
651 actual = prefs()->pref_service()->GetString(kPref1);
652 EXPECT_EQ("val3", actual);
653 actual = prefs()->pref_service()->GetString(kPref2);
654 EXPECT_EQ("val5", actual);
655 actual = prefs()->pref_service()->GetString(kPref3);
656 EXPECT_EQ("val8", actual);
657 }
658 };
659 TEST_F(ExtensionPrefsInstallOverwrittenExtensions,
660 ExtensionPrefsInstallOverwrittenExtensions) {}
661
662 // Make sure the last-installed extension wins even if other extensions set
663 // the same or different preferences later.
664 class ExtensionPrefsInstallInterleavedExtensions
665 : public ExtensionPrefsPreferencesBase {
666 virtual void Initialize() {
667 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
668 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val2"));
669 InstallExtControlledPref(ext3_, kPref3, Value::CreateStringValue("val3"));
670
671 InstallExtControlledPref(ext3_, kPref3, Value::CreateStringValue("val4"));
672 InstallExtControlledPref(ext2_, kPref3, Value::CreateStringValue("val5"));
673 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val6"));
674
675 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val7"));
676 }
677 virtual void Verify() {
678 std::string actual;
679 actual = prefs()->pref_service()->GetString(kPref1);
680 EXPECT_EQ("val7", actual);
681 actual = prefs()->pref_service()->GetString(kPref2);
682 EXPECT_EQ("val2", actual);
683 actual = prefs()->pref_service()->GetString(kPref3);
684 EXPECT_EQ("val4", actual);
685 }
686 };
687 TEST_F(ExtensionPrefsInstallInterleavedExtensions,
688 ExtensionPrefsInstallInterleavedExtensions) {}
689
690 class ExtensionPrefsUninstallOnlyExtension
691 : public ExtensionPrefsPreferencesBase {
692 virtual void Initialize() {
693 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
694 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val2"));
695
696 UninstallExtension(ext1_->id());
697 }
698 virtual void Verify() {
699 std::string actual;
700 actual = prefs()->pref_service()->GetString(kPref1);
701 EXPECT_EQ(kDefaultPref1, actual);
702 actual = prefs()->pref_service()->GetString(kPref2);
703 EXPECT_EQ(kDefaultPref2, actual);
704 }
705 };
706 TEST_F(ExtensionPrefsUninstallOnlyExtension,
707 ExtensionPrefsUninstallOnlyExtension) {}
708
709 // Tests uninstalling an extension that wasn't winning for any preferences.
710 class ExtensionPrefsUninstallIrrelevantExtension
711 : public ExtensionPrefsPreferencesBase {
712 virtual void Initialize() {
713 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
714 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
715
716 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val3"));
717 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val4"));
718
719 UninstallExtension(ext1_->id());
720 }
721 virtual void Verify() {
722 std::string actual;
723 actual = prefs()->pref_service()->GetString(kPref1);
724 EXPECT_EQ("val2", actual);
725 actual = prefs()->pref_service()->GetString(kPref2);
726 EXPECT_EQ("val4", actual);
727 }
728 };
729 TEST_F(ExtensionPrefsUninstallIrrelevantExtension,
730 ExtensionPrefsUninstallIrrelevantExtension) {}
731
732 // Tests uninstalling an extension that was winning for all preferences.
733 class ExtensionPrefsUninstallExtensionFromTop
734 : public ExtensionPrefsPreferencesBase {
735 virtual void Initialize() {
736 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
737 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
738 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val3"));
739
740 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
741 InstallExtControlledPref(ext3_, kPref2, Value::CreateStringValue("val5"));
742
743 UninstallExtension(ext3_->id());
744 }
745 virtual void Verify() {
746 std::string actual;
747 actual = prefs()->pref_service()->GetString(kPref1);
748 EXPECT_EQ("val2", actual);
749 actual = prefs()->pref_service()->GetString(kPref2);
750 EXPECT_EQ("val4", actual);
751 }
752 };
753 TEST_F(ExtensionPrefsUninstallExtensionFromTop,
754 ExtensionPrefsUninstallExtensionFromTop) {}
755
756 // Tests uninstalling an extension that was winning for only some preferences.
757 class ExtensionPrefsUninstallExtensionFromMiddle
758 : public ExtensionPrefsPreferencesBase {
759 virtual void Initialize() {
760 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
761 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
762 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val3"));
763
764 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
765 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val5"));
766
767 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val6"));
768
769 InstallExtControlledPref(ext2_, kPref4, Value::CreateStringValue("val7"));
770
771 UninstallExtension(ext2_->id());
772 }
773 virtual void Verify() {
774 std::string actual;
775 actual = prefs()->pref_service()->GetString(kPref1);
776 EXPECT_EQ("val3", actual);
777 actual = prefs()->pref_service()->GetString(kPref2);
778 EXPECT_EQ("val4", actual);
779 actual = prefs()->pref_service()->GetString(kPref3);
780 EXPECT_EQ("val6", actual);
781 actual = prefs()->pref_service()->GetString(kPref4);
782 EXPECT_EQ(kDefaultPref4, actual);
783 }
784 };
785 TEST_F(ExtensionPrefsUninstallExtensionFromMiddle,
786 ExtensionPrefsUninstallExtensionFromMiddle) {}
787
788 // Tests triggering of notifications to registered observers
789 class ExtensionPrefsNotifyWhenNeeded
790 : public ExtensionPrefsPreferencesBase {
791 virtual void Initialize() {
792 using testing::_;
793 using testing::Mock;
794 using testing::StrEq;
795
796 scoped_ptr<MockNotificationObserver> observer(
797 new MockNotificationObserver());
798 PrefChangeRegistrar registrar;
799 registrar.Init(prefs()->pref_service());
800 registrar.Add(kPref1, observer.get());
801
802 EXPECT_CALL(*observer, Observe(_, _, _));
803 InstallExtControlledPref(ext1_, kPref1,
804 Value::CreateStringValue("https://www.chromium.org"));
805 Mock::VerifyAndClearExpectations(observer.get());
806
807 EXPECT_CALL(*observer, Observe(_, _, _)).Times(0);
808 InstallExtControlledPref(ext1_, kPref1,
809 Value::CreateStringValue("https://www.chromium.org"));
810 Mock::VerifyAndClearExpectations(observer.get());
811
812 EXPECT_CALL(*observer, Observe(_, _, _)).Times(2);
813 InstallExtControlledPref(ext1_, kPref1,
814 Value::CreateStringValue("chrome://newtab"));
815
816 UninstallExtension(ext1_->id());
817 registrar.Remove(kPref1, observer.get());
818 }
819 virtual void Verify() {
820 std::string actual = prefs()->pref_service()->GetString(kPref1);
821 EXPECT_EQ(kDefaultPref1, actual);
822 }
823 };
824 TEST_F(ExtensionPrefsNotifyWhenNeeded,
825 ExtensionPrefsNotifyWhenNeeded) {}
826
827 // Tests disabling an extension
828 class ExtensionPrefsDisableExt
829 : public ExtensionPrefsPreferencesBase {
830 virtual void Initialize() {
831 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
832 std::string actual = prefs()->pref_service()->GetString(kPref1);
833 EXPECT_EQ("val1", actual);
834 prefs()->SetExtensionState(ext1_, Extension::DISABLED);
835 }
836 virtual void Verify() {
837 std::string actual = prefs()->pref_service()->GetString(kPref1);
838 EXPECT_EQ(kDefaultPref1, actual);
839 }
840 };
841 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsDisableExt) {}
842
843 // Tests disabling and reenabling an extension
844 class ExtensionPrefsReenableExt
845 : public ExtensionPrefsPreferencesBase {
846 virtual void Initialize() {
847 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
848 prefs()->SetExtensionState(ext1_, Extension::DISABLED);
849 prefs()->SetExtensionState(ext1_, Extension::ENABLED);
850 }
851 virtual void Verify() {
852 std::string actual = prefs()->pref_service()->GetString(kPref1);
853 EXPECT_EQ("val1", actual);
854 }
855 };
856 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsReenableExt) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698