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

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: Temporary hack to fix regression in unit tests 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 for each preference.
514 class ExtensionPrefsInstallOverwrittenExtensions
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 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
522 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val5"));
523
524 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val6"));
525 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val7"));
526 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val8"));
527 }
528 virtual void Verify() {
529 std::string actual;
530 actual = prefs()->pref_service()->GetString(kPref1);
531 EXPECT_EQ("val3", actual);
532 actual = prefs()->pref_service()->GetString(kPref2);
533 EXPECT_EQ("val5", actual);
534 actual = prefs()->pref_service()->GetString(kPref3);
535 EXPECT_EQ("val8", actual);
536 }
537 };
538 TEST_F(ExtensionPrefsInstallOverwrittenExtensions,
539 ExtensionPrefsInstallOverwrittenExtensions) {}
540
541 // Make sure the last-installed extension wins even if other extensions set
542 // the same or different preferences later.
543 class ExtensionPrefsInstallInterleavedExtensions
544 : public ExtensionPrefsPreferencesBase {
545 virtual void Initialize() {
546 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
547 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val2"));
548 InstallExtControlledPref(ext3_, kPref3, Value::CreateStringValue("val3"));
549
550 InstallExtControlledPref(ext3_, kPref3, Value::CreateStringValue("val4"));
551 InstallExtControlledPref(ext2_, kPref3, Value::CreateStringValue("val5"));
552 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val6"));
553
554 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val7"));
555 }
556 virtual void Verify() {
557 std::string actual;
558 actual = prefs()->pref_service()->GetString(kPref1);
559 EXPECT_EQ("val7", actual);
560 actual = prefs()->pref_service()->GetString(kPref2);
561 EXPECT_EQ("val2", actual);
562 actual = prefs()->pref_service()->GetString(kPref3);
563 EXPECT_EQ("val4", actual);
564 }
565 };
566 TEST_F(ExtensionPrefsInstallInterleavedExtensions,
567 ExtensionPrefsInstallInterleavedExtensions) {}
568
569 class ExtensionPrefsUninstallOnlyExtension
570 : public ExtensionPrefsPreferencesBase {
571 virtual void Initialize() {
572 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
573 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val2"));
574
575 UninstallExtension(ext1_->id());
576 }
577 virtual void Verify() {
578 std::string actual;
579 actual = prefs()->pref_service()->GetString(kPref1);
580 EXPECT_EQ(kDefaultPref1, actual);
581 actual = prefs()->pref_service()->GetString(kPref2);
582 EXPECT_EQ(kDefaultPref2, actual);
583 }
584 };
585 TEST_F(ExtensionPrefsUninstallOnlyExtension,
586 ExtensionPrefsUninstallOnlyExtension) {}
587
588 // Tests uninstalling an extension that wasn't winning for any preferences.
589 class ExtensionPrefsUninstallIrrelevantExtension
590 : public ExtensionPrefsPreferencesBase {
591 virtual void Initialize() {
592 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
593 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
594
595 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val3"));
596 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val4"));
597
598 UninstallExtension(ext1_->id());
599 }
600 virtual void Verify() {
601 std::string actual;
602 actual = prefs()->pref_service()->GetString(kPref1);
603 EXPECT_EQ("val2", actual);
604 actual = prefs()->pref_service()->GetString(kPref2);
605 EXPECT_EQ("val4", actual);
606 }
607 };
608 TEST_F(ExtensionPrefsUninstallIrrelevantExtension,
609 ExtensionPrefsUninstallIrrelevantExtension) {}
610
611 // Tests uninstalling an extension that was winning for all preferences.
612 class ExtensionPrefsUninstallExtensionFromTop
613 : public ExtensionPrefsPreferencesBase {
614 virtual void Initialize() {
615 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
616 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
617 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val3"));
618
619 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
620 InstallExtControlledPref(ext3_, kPref2, Value::CreateStringValue("val5"));
621
622 UninstallExtension(ext3_->id());
623 }
624 virtual void Verify() {
625 std::string actual;
626 actual = prefs()->pref_service()->GetString(kPref1);
627 EXPECT_EQ("val2", actual);
628 actual = prefs()->pref_service()->GetString(kPref2);
629 EXPECT_EQ("val4", actual);
630 }
631 };
632 TEST_F(ExtensionPrefsUninstallExtensionFromTop,
633 ExtensionPrefsUninstallExtensionFromTop) {}
634
635 // Tests uninstalling an extension that was winning for only some preferences.
636 class ExtensionPrefsUninstallExtensionFromMiddle
637 : public ExtensionPrefsPreferencesBase {
638 virtual void Initialize() {
639 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
640 InstallExtControlledPref(ext2_, kPref1, Value::CreateStringValue("val2"));
641 InstallExtControlledPref(ext3_, kPref1, Value::CreateStringValue("val3"));
642
643 InstallExtControlledPref(ext1_, kPref2, Value::CreateStringValue("val4"));
644 InstallExtControlledPref(ext2_, kPref2, Value::CreateStringValue("val5"));
645
646 InstallExtControlledPref(ext1_, kPref3, Value::CreateStringValue("val6"));
647
648 InstallExtControlledPref(ext2_, kPref4, Value::CreateStringValue("val7"));
649
650 UninstallExtension(ext2_->id());
651 }
652 virtual void Verify() {
653 std::string actual;
654 actual = prefs()->pref_service()->GetString(kPref1);
655 EXPECT_EQ("val3", actual);
656 actual = prefs()->pref_service()->GetString(kPref2);
657 EXPECT_EQ("val4", actual);
658 actual = prefs()->pref_service()->GetString(kPref3);
659 EXPECT_EQ("val6", actual);
660 actual = prefs()->pref_service()->GetString(kPref4);
661 EXPECT_EQ(kDefaultPref4, actual);
662 }
663 };
664 TEST_F(ExtensionPrefsUninstallExtensionFromMiddle,
665 ExtensionPrefsUninstallExtensionFromMiddle) {}
666
667 // Tests triggering of notifications to registered observers
668 class ExtensionPrefsNotifyWhenNeeded
669 : public ExtensionPrefsPreferencesBase {
670 virtual void Initialize() {
671 using testing::_;
672 using testing::Mock;
673 using testing::StrEq;
674
675 scoped_ptr<MockNotificationObserver> observer(
676 new MockNotificationObserver());
677 PrefChangeRegistrar registrar;
678 registrar.Init(prefs()->pref_service());
679 registrar.Add(kPref1, observer.get());
680
681 EXPECT_CALL(*observer, Observe(_, _, _));
682 InstallExtControlledPref(ext1_, kPref1,
683 Value::CreateStringValue("https://www.chromium.org"));
684 Mock::VerifyAndClearExpectations(observer.get());
685
686 EXPECT_CALL(*observer, Observe(_, _, _)).Times(0);
687 InstallExtControlledPref(ext1_, kPref1,
688 Value::CreateStringValue("https://www.chromium.org"));
689 Mock::VerifyAndClearExpectations(observer.get());
690
691 EXPECT_CALL(*observer, Observe(_, _, _)).Times(2);
692 InstallExtControlledPref(ext1_, kPref1,
693 Value::CreateStringValue("chrome://newtab"));
694
695 UninstallExtension(ext1_->id());
696 registrar.Remove(kPref1, observer.get());
697 }
698 virtual void Verify() {
699 std::string actual = prefs()->pref_service()->GetString(kPref1);
700 EXPECT_EQ(kDefaultPref1, actual);
701 }
702 };
703 TEST_F(ExtensionPrefsNotifyWhenNeeded,
704 ExtensionPrefsNotifyWhenNeeded) {}
705
706 // Tests disabling an extension
707 class ExtensionPrefsDisableExt
708 : public ExtensionPrefsPreferencesBase {
709 virtual void Initialize() {
710 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
711 std::string actual = prefs()->pref_service()->GetString(kPref1);
712 EXPECT_EQ("val1", actual);
713 prefs()->SetExtensionState(ext1_, Extension::DISABLED);
714 }
715 virtual void Verify() {
716 std::string actual = prefs()->pref_service()->GetString(kPref1);
717 EXPECT_EQ(kDefaultPref1, actual);
718 }
719 };
720 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsDisableExt) {}
721
722 // Tests disabling and reenabling an extension
723 class ExtensionPrefsReenableExt
724 : public ExtensionPrefsPreferencesBase {
725 virtual void Initialize() {
726 InstallExtControlledPref(ext1_, kPref1, Value::CreateStringValue("val1"));
727 prefs()->SetExtensionState(ext1_, Extension::DISABLED);
728 prefs()->SetExtensionState(ext1_, Extension::ENABLED);
729 }
730 virtual void Verify() {
731 std::string actual = prefs()->pref_service()->GetString(kPref1);
732 EXPECT_EQ("val1", actual);
733 }
734 };
735 TEST_F(ExtensionPrefsDisableExt, ExtensionPrefsReenableExt) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698