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

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: Lint issues and factored out renaming of InMemoryPrefStore Created 10 years, 1 month 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 // Base class for tests. 35 // Base class for tests.
23 class ExtensionPrefsTest : public testing::Test { 36 class ExtensionPrefsTest : public testing::Test {
24 public: 37 public:
25 ExtensionPrefsTest() {} 38 ExtensionPrefsTest() {}
26 39
27 // This function will get called once, and is the right place to do operations 40 // This function will get called once, and is the right place to do operations
28 // on ExtensionPrefs that write data. 41 // on ExtensionPrefs that write data.
29 virtual void Initialize() = 0; 42 virtual void Initialize() = 0;
30 43
31 // This function will be called twice - once while the original ExtensionPrefs 44 // This function will be called twice - once while the original ExtensionPrefs
32 // object is still alive, and once after recreation. Thus, it tests that 45 // object is still alive, and once after recreation. Thus, it tests that
33 // things don't break after any ExtensionPrefs startup work. 46 // things don't break after any ExtensionPrefs startup work.
34 virtual void Verify() = 0; 47 virtual void Verify() = 0;
35 48
49 // This function is called to Register preference default values.
50 virtual void RegisterPreferences() {}
51
36 virtual void SetUp() { 52 virtual void SetUp() {
53 RegisterPreferences();
37 Initialize(); 54 Initialize();
38 } 55 }
39 56
40 virtual void TearDown() { 57 virtual void TearDown() {
41 Verify(); 58 Verify();
42 59
43 // Reset ExtensionPrefs, and re-verify. 60 // Reset ExtensionPrefs, and re-verify.
44 prefs_.RecreateExtensionPrefs(); 61 prefs_.RecreateExtensionPrefs();
62 RegisterPreferences();
45 Verify(); 63 Verify();
46 } 64 }
47 65
48 protected: 66 protected:
49 ExtensionPrefs* prefs() { return prefs_.prefs(); } 67 ExtensionPrefs* prefs() { return prefs_.prefs(); }
50 68
51 TestExtensionPrefs prefs_; 69 TestExtensionPrefs prefs_;
52 70
53 private: 71 private:
54 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefsTest); 72 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefsTest);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 EXPECT_EQ(NULL, prefs()->GetInstalledExtensionInfo(not_installed_id_)); 197 EXPECT_EQ(NULL, prefs()->GetInstalledExtensionInfo(not_installed_id_));
180 198
181 ExtensionList::const_iterator iter; 199 ExtensionList::const_iterator iter;
182 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) { 200 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
183 EXPECT_FALSE(prefs()->IsExtensionBlacklisted((*iter)->id())); 201 EXPECT_FALSE(prefs()->IsExtensionBlacklisted((*iter)->id()));
184 } 202 }
185 // Blacklist one installed and one not-installed extension id. 203 // Blacklist one installed and one not-installed extension id.
186 std::set<std::string> blacklisted_ids; 204 std::set<std::string> blacklisted_ids;
187 blacklisted_ids.insert(extensions_[0]->id()); 205 blacklisted_ids.insert(extensions_[0]->id());
188 blacklisted_ids.insert(not_installed_id_); 206 blacklisted_ids.insert(not_installed_id_);
207
189 prefs()->UpdateBlacklist(blacklisted_ids); 208 prefs()->UpdateBlacklist(blacklisted_ids);
190 } 209 }
191 210
192 virtual void Verify() { 211 virtual void Verify() {
193 // Make sure the two id's we expect to be blacklisted are. 212 // Make sure the two id's we expect to be blacklisted are.
194 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extensions_[0]->id())); 213 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extensions_[0]->id()));
195 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(not_installed_id_)); 214 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(not_installed_id_));
196 215
197 // Make sure the other id's are not blacklisted. 216 // Make sure the other id's are not blacklisted.
198 ExtensionList::const_iterator iter; 217 ExtensionList::const_iterator iter;
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 EXPECT_EQ(launch_index + 1, new_launch_index); 415 EXPECT_EQ(launch_index + 1, new_launch_index);
397 416
398 // This extension doesn't exist, so it should return -1. 417 // This extension doesn't exist, so it should return -1.
399 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); 418 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo"));
400 } 419 }
401 420
402 private: 421 private:
403 scoped_refptr<Extension> extension_; 422 scoped_refptr<Extension> extension_;
404 }; 423 };
405 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} 424 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {}
425
426 namespace keys = extension_manifest_keys;
427
428 class ExtensionPrefsPreferencesBase : public ExtensionPrefsTest {
429 public:
430 ExtensionPrefsPreferencesBase()
431 : ExtensionPrefsTest(),
432 ext1(NULL),
433 ext2(NULL),
434 ext3(NULL),
435 installed() {
436 DictionaryValue simple_dict;
437 std::string error;
438
439 simple_dict.SetString(keys::kVersion, "1.0.0.0");
440 simple_dict.SetString(keys::kName, "unused");
441
442 ext1_scoped_ = Extension::Create(
443 prefs_.temp_dir().AppendASCII("ext1"), Extension::INVALID,
444 simple_dict, false, &error);
445 ext2_scoped_ = Extension::Create(
446 prefs_.temp_dir().AppendASCII("ext2"), Extension::INVALID,
447 simple_dict, false, &error);
448 ext3_scoped_ = Extension::Create(
449 prefs_.temp_dir().AppendASCII("ext3"), Extension::INVALID,
450 simple_dict, false, &error);
451
452 ext1 = ext1_scoped_.get();
453 ext2 = ext2_scoped_.get();
454 ext3 = ext3_scoped_.get();
455
456 for (size_t i = 0; i < arraysize(installed); ++i)
457 installed[i] = false;
458 }
459
460 void RegisterPreferences() {
461 prefs()->pref_service()->RegisterStringPref(kPref1, kDefaultPref1);
462 prefs()->pref_service()->RegisterStringPref(kPref2, kDefaultPref2);
463 prefs()->pref_service()->RegisterStringPref(kPref3, kDefaultPref3);
464 prefs()->pref_service()->RegisterStringPref(kPref4, kDefaultPref4);
465 }
466
467 void InstallExtControlledPref(Extension *ext,
468 const std::string& key,
469 Value* val) {
470 // Install extension the first time a preference is set for it.
471 Extension* extensions[] = {ext1, ext2, ext3};
472 for (int i = 0; i < 3; ++i) {
473 if (ext == extensions[i] && !installed[i]) {
474 prefs()->OnExtensionInstalled(ext, Extension::ENABLED, true);
475 installed[i] = true;
476 break;
477 }
478 }
479
480 prefs()->SetExtensionControlledPref(ext->id(), key, val);
481 }
482
483 void UninstallExtension(const std::string& extension_id) {
484 prefs()->OnExtensionUninstalled(extension_id, Extension::INTERNAL, false);
485 }
486
487 // Weak references, for convenience.
488 Extension* ext1;
489 Extension* ext2;
490 Extension* ext3;
491
492 // Flags indicating whether each of the extensions has been installed, yet.
493 bool installed[3];
494
495 private:
496 scoped_refptr<Extension> ext1_scoped_;
497 scoped_refptr<Extension> ext2_scoped_;
498 scoped_refptr<Extension> ext3_scoped_;
499 };
500
501 class ExtensionPrefsInstallOneExtension
502 : public ExtensionPrefsPreferencesBase {
503 virtual void Initialize() {
504 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
505 }
506 virtual void Verify() {
507 std::string actual = prefs()->pref_service()->GetString(kPref1);
508 EXPECT_EQ("val1", actual);
509 }
510 };
511 TEST_F(ExtensionPrefsInstallOneExtension, ExtensionPrefsInstallOneExtension) {}
512
513 // Make sure the last-installed extension wins.
514 class ExtensionPrefsInstallMultipleExtensions
515 : public ExtensionPrefsPreferencesBase {
516 virtual void Initialize() {
517 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
518 InstallExtControlledPref(ext2, kPref1, Value::CreateStringValue("val2"));
519 InstallExtControlledPref(ext3, kPref1, Value::CreateStringValue("val3"));
520 }
521 virtual void Verify() {
522 std::string actual = prefs()->pref_service()->GetString(kPref1);
523 EXPECT_EQ("val3", actual);
524 }
525 };
526 TEST_F(ExtensionPrefsInstallMultipleExtensions,
527 ExtensionPrefsInstallMultipleExtensions) {}
528
529 // Make sure the last-installed extension wins for each preference.
530 class ExtensionPrefsInstallOverwrittenExtensions
531 : public ExtensionPrefsPreferencesBase {
532 virtual void Initialize() {
533 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
534 InstallExtControlledPref(ext2, kPref1, Value::CreateStringValue("val2"));
535 InstallExtControlledPref(ext3, kPref1, Value::CreateStringValue("val3"));
536
537 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val4"));
538 InstallExtControlledPref(ext2, kPref2, Value::CreateStringValue("val5"));
539
540 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val6"));
541 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val7"));
542 InstallExtControlledPref(ext1, kPref3, Value::CreateStringValue("val8"));
543 }
544 virtual void Verify() {
545 std::string actual;
546 actual = prefs()->pref_service()->GetString(kPref1);
547 EXPECT_EQ("val3", actual);
548 actual = prefs()->pref_service()->GetString(kPref2);
549 EXPECT_EQ("val5", actual);
550 actual = prefs()->pref_service()->GetString(kPref3);
551 EXPECT_EQ("val8", actual);
552 }
553 };
554 TEST_F(ExtensionPrefsInstallOverwrittenExtensions,
555 ExtensionPrefsInstallOverwrittenExtensions) {}
556
557 // Make sure the last-installed extension wins even if other extensions set
558 // the same or different preferences later.
559 class ExtensionPrefsInstallInterleavedExtensions
560 : public ExtensionPrefsPreferencesBase {
561 virtual void Initialize() {
562 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
563 InstallExtControlledPref(ext2, kPref2, Value::CreateStringValue("val2"));
564 InstallExtControlledPref(ext3, kPref3, Value::CreateStringValue("val3"));
565
566 InstallExtControlledPref(ext3, kPref3, Value::CreateStringValue("val4"));
567 InstallExtControlledPref(ext2, kPref3, Value::CreateStringValue("val5"));
568 InstallExtControlledPref(ext1, kPref3, Value::CreateStringValue("val6"));
569
570 InstallExtControlledPref(ext3, kPref1, Value::CreateStringValue("val7"));
571 }
572 virtual void Verify() {
573 std::string actual;
574 actual = prefs()->pref_service()->GetString(kPref1);
575 EXPECT_EQ("val7", actual);
576 actual = prefs()->pref_service()->GetString(kPref2);
577 EXPECT_EQ("val2", actual);
578 actual = prefs()->pref_service()->GetString(kPref3);
579 EXPECT_EQ("val4", actual);
580 }
581 };
582 TEST_F(ExtensionPrefsInstallInterleavedExtensions,
583 ExtensionPrefsInstallInterleavedExtensions) {}
584
585 class ExtensionPrefsUninstallOnlyExtension
586 : public ExtensionPrefsPreferencesBase {
587 virtual void Initialize() {
588 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
589 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val2"));
590
591 UninstallExtension(ext1->id());
592 }
593 virtual void Verify() {
594 std::string actual;
595 actual = prefs()->pref_service()->GetString(kPref1);
596 EXPECT_EQ(kDefaultPref1, actual);
597 actual = prefs()->pref_service()->GetString(kPref2);
598 EXPECT_EQ(kDefaultPref2, actual);
599 }
600 };
601 TEST_F(ExtensionPrefsUninstallOnlyExtension,
602 ExtensionPrefsUninstallOnlyExtension) {}
603
604 // Tests uninstalling an extension that wasn't winning for any preferences.
605 class ExtensionPrefsUninstallIrrelevantExtension
606 : public ExtensionPrefsPreferencesBase {
607 virtual void Initialize() {
608 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
609 InstallExtControlledPref(ext2, kPref1, Value::CreateStringValue("val2"));
610
611 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val3"));
612 InstallExtControlledPref(ext2, kPref2, Value::CreateStringValue("val4"));
613
614 UninstallExtension(ext1->id());
615 }
616 virtual void Verify() {
617 std::string actual;
618 actual = prefs()->pref_service()->GetString(kPref1);
619 EXPECT_EQ("val2", actual);
620 actual = prefs()->pref_service()->GetString(kPref2);
621 EXPECT_EQ("val4", actual);
622 }
623 };
624 TEST_F(ExtensionPrefsUninstallIrrelevantExtension,
625 ExtensionPrefsUninstallIrrelevantExtension) {}
626
627 // Tests uninstalling an extension that was winning for all preferences.
628 class ExtensionPrefsUninstallExtensionFromTop
629 : public ExtensionPrefsPreferencesBase {
630 virtual void Initialize() {
631 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
632 InstallExtControlledPref(ext2, kPref1, Value::CreateStringValue("val2"));
633 InstallExtControlledPref(ext3, kPref1, Value::CreateStringValue("val3"));
634
635 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val4"));
636 InstallExtControlledPref(ext3, kPref2, Value::CreateStringValue("val5"));
637
638 UninstallExtension(ext3->id());
639 }
640 virtual void Verify() {
641 std::string actual;
642 actual = prefs()->pref_service()->GetString(kPref1);
643 EXPECT_EQ("val2", actual);
644 actual = prefs()->pref_service()->GetString(kPref2);
645 EXPECT_EQ("val4", actual);
646 }
647 };
648 TEST_F(ExtensionPrefsUninstallExtensionFromTop,
649 ExtensionPrefsUninstallExtensionFromTop) {}
650
651 // Tests uninstalling an extension that was winning for only some preferences.
652 class ExtensionPrefsUninstallExtensionFromMiddle
653 : public ExtensionPrefsPreferencesBase {
654 virtual void Initialize() {
655 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
656 InstallExtControlledPref(ext2, kPref1, Value::CreateStringValue("val2"));
657 InstallExtControlledPref(ext3, kPref1, Value::CreateStringValue("val3"));
658
659 InstallExtControlledPref(ext1, kPref2, Value::CreateStringValue("val4"));
660 InstallExtControlledPref(ext2, kPref2, Value::CreateStringValue("val5"));
661
662 InstallExtControlledPref(ext1, kPref3, Value::CreateStringValue("val6"));
663
664 InstallExtControlledPref(ext2, kPref4, Value::CreateStringValue("val7"));
665
666 UninstallExtension(ext2->id());
667 }
668 virtual void Verify() {
669 std::string actual;
670 actual = prefs()->pref_service()->GetString(kPref1);
671 EXPECT_EQ("val3", actual);
672 actual = prefs()->pref_service()->GetString(kPref2);
673 EXPECT_EQ("val4", actual);
674 actual = prefs()->pref_service()->GetString(kPref3);
675 EXPECT_EQ("val6", actual);
676 actual = prefs()->pref_service()->GetString(kPref4);
677 EXPECT_EQ(kDefaultPref4, actual);
678 }
679 };
680 TEST_F(ExtensionPrefsUninstallExtensionFromMiddle,
681 ExtensionPrefsUninstallExtensionFromMiddle) {}
682
683 // Tests uninstalling an extension that was winning for only some preferences.
684 class ExtensionPrefsNotifyWhenNeeded
685 : public ExtensionPrefsPreferencesBase {
686 virtual void Initialize() {
687 using testing::_;
688 using testing::Mock;
689 using testing::StrEq;
690
691 scoped_ptr<MockNotificationObserver> observer(
692 new MockNotificationObserver());
693 PrefChangeRegistrar registrar;
694 registrar.Init(prefs()->pref_service());
695 registrar.Add(kPref1, observer.get());
696
697 EXPECT_CALL(*observer, Observe(_, _, _));
698 InstallExtControlledPref(ext1, kPref1,
699 Value::CreateStringValue("https://www.chromium.org"));
700 Mock::VerifyAndClearExpectations(observer.get());
701
702 EXPECT_CALL(*observer, Observe(_, _, _)).Times(0);
703 InstallExtControlledPref(ext1, kPref1,
704 Value::CreateStringValue("https://www.chromium.org"));
705 Mock::VerifyAndClearExpectations(observer.get());
706
707 EXPECT_CALL(*observer, Observe(_, _, _)).Times(2);
708 InstallExtControlledPref(ext1, kPref1,
709 Value::CreateStringValue("chrome://newtab"));
710
711 UninstallExtension(ext1->id());
712 registrar.Remove(kPref1, observer.get());
713 }
714 virtual void Verify() {
715 std::string actual = prefs()->pref_service()->GetString(kPref1);
716 EXPECT_EQ(kDefaultPref1, actual);
717 }
718 };
719 TEST_F(ExtensionPrefsNotifyWhenNeeded,
720 ExtensionPrefsNotifyWhenNeeded) {}
721
722 // Tests disabling an extension
723 class ExtensionPrefsDisableExt
724 : public ExtensionPrefsPreferencesBase {
725 virtual void Initialize() {
726 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
727 std::string actual = prefs()->pref_service()->GetString(kPref1);
728 EXPECT_EQ("val1", actual);
729 prefs()->SetExtensionState(ext1, Extension::DISABLED);
730 }
731 virtual void Verify() {
732 std::string actual = prefs()->pref_service()->GetString(kPref1);
733 EXPECT_EQ(kDefaultPref1, actual);
734 }
735 };
736 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsDisableExt) {}
737
738 // Tests disabling and reenabling an extension
739 class ExtensionPrefsReenableExt
740 : public ExtensionPrefsPreferencesBase {
741 virtual void Initialize() {
742 InstallExtControlledPref(ext1, kPref1, Value::CreateStringValue("val1"));
743 prefs()->SetExtensionState(ext1, Extension::DISABLED);
744 prefs()->SetExtensionState(ext1, Extension::ENABLED);
745 }
746 virtual void Verify() {
747 std::string actual = prefs()->pref_service()->GetString(kPref1);
748 EXPECT_EQ("val1", actual);
749 }
750 };
751 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsReenableExt) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698