Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/./extension_prefs_unittest.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/files/scoped_temp_dir.h"
      9 #include "base/path_service.h"
     10 #include "base/prefs/mock_pref_change_callback.h"
     11 #include "base/prefs/pref_change_registrar.h"
     12 #include "base/prefs/scoped_user_pref_update.h"
     13 #include "base/stl_util.h"
     14 #include "base/strings/string_number_conversions.h"
     15 #include "base/strings/stringprintf.h"
     16 #include "base/values.h"
     17 #include "chrome/browser/prefs/pref_service_syncable.h"
     18 #include "chrome/common/chrome_paths.h"
     19 #include "components/pref_registry/pref_registry_syncable.h"
     20 #include "content/public/browser/notification_details.h"
     21 #include "content/public/browser/notification_source.h"
     22 #include "content/public/test/mock_notification_observer.h"
     23 #include "extensions/browser/extension_pref_value_map.h"
     24 #include "extensions/browser/extension_prefs.h"
     25 #include "extensions/browser/install_flag.h"
     26 #include "extensions/common/extension.h"
     27 #include "extensions/common/manifest_constants.h"
     28 #include "extensions/common/permissions/permission_set.h"
     29 #include "extensions/common/permissions/permissions_info.h"
     30 #include "sync/api/string_ordinal.h"
     31 
     32 using base::Time;
     33 using base::TimeDelta;
     34 using content::BrowserThread;
     35 
     36 namespace extensions {
     37 
     38 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
     39   int schemes = URLPattern::SCHEME_ALL;
     40   extent->AddPattern(URLPattern(schemes, pattern));
     41 }
     42 
     43 ExtensionPrefsTest::ExtensionPrefsTest()
     44     : ui_thread_(BrowserThread::UI, &message_loop_),
     45       prefs_(message_loop_.message_loop_proxy().get()) {}
     46 
     47 ExtensionPrefsTest::~ExtensionPrefsTest() {
     48 }
     49 
     50 void ExtensionPrefsTest::RegisterPreferences(
     51     user_prefs::PrefRegistrySyncable* registry) {}
     52 
     53 void ExtensionPrefsTest::SetUp() {
     54   RegisterPreferences(prefs_.pref_registry().get());
     55   Initialize();
     56 }
     57 
     58 void ExtensionPrefsTest::TearDown() {
     59   Verify();
     60 
     61   // Reset ExtensionPrefs, and re-verify.
     62   prefs_.ResetPrefRegistry();
     63   RegisterPreferences(prefs_.pref_registry().get());
     64   prefs_.RecreateExtensionPrefs();
     65   Verify();
     66   prefs_.pref_service()->CommitPendingWrite();
     67   message_loop_.RunUntilIdle();
     68 }
     69 
     70 // Tests the LastPingDay/SetLastPingDay functions.
     71 class ExtensionPrefsLastPingDay : public ExtensionPrefsTest {
     72  public:
     73   ExtensionPrefsLastPingDay()
     74       : extension_time_(Time::Now() - TimeDelta::FromHours(4)),
     75         blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {}
     76 
     77   virtual void Initialize() OVERRIDE {
     78     extension_id_ = prefs_.AddExtensionAndReturnId("last_ping_day");
     79     EXPECT_TRUE(prefs()->LastPingDay(extension_id_).is_null());
     80     prefs()->SetLastPingDay(extension_id_, extension_time_);
     81     prefs()->SetBlacklistLastPingDay(blacklist_time_);
     82   }
     83 
     84   virtual void Verify() OVERRIDE {
     85     Time result = prefs()->LastPingDay(extension_id_);
     86     EXPECT_FALSE(result.is_null());
     87     EXPECT_TRUE(result == extension_time_);
     88     result = prefs()->BlacklistLastPingDay();
     89     EXPECT_FALSE(result.is_null());
     90     EXPECT_TRUE(result == blacklist_time_);
     91   }
     92 
     93  private:
     94   Time extension_time_;
     95   Time blacklist_time_;
     96   std::string extension_id_;
     97 };
     98 TEST_F(ExtensionPrefsLastPingDay, LastPingDay) {}
     99 
    100 // Tests the GetToolbarOrder/SetToolbarOrder functions.
    101 class ExtensionPrefsToolbarOrder : public ExtensionPrefsTest {
    102  public:
    103   virtual void Initialize() OVERRIDE {
    104     list_.push_back(prefs_.AddExtensionAndReturnId("1"));
    105     list_.push_back(prefs_.AddExtensionAndReturnId("2"));
    106     list_.push_back(prefs_.AddExtensionAndReturnId("3"));
    107     ExtensionIdList before_list = prefs()->GetToolbarOrder();
    108     EXPECT_TRUE(before_list.empty());
    109     prefs()->SetToolbarOrder(list_);
    110   }
    111 
    112   virtual void Verify() OVERRIDE {
    113     ExtensionIdList result = prefs()->GetToolbarOrder();
    114     ASSERT_EQ(list_, result);
    115   }
    116 
    117  private:
    118   ExtensionIdList list_;
    119 };
    120 TEST_F(ExtensionPrefsToolbarOrder, ToolbarOrder) {}
    121 
    122 // Tests the GetKnownDisabled/SetKnownDisabled functions.
    123 class ExtensionPrefsKnownDisabled : public ExtensionPrefsTest {
    124  public:
    125   virtual void Initialize() OVERRIDE {
    126     ExtensionIdSet before_set;
    127     EXPECT_FALSE(prefs()->GetKnownDisabled(&before_set));
    128     EXPECT_TRUE(before_set.empty());
    129 
    130     // Initialize to an empty list and confirm that GetKnownDisabled() returns
    131     // true and an empty list.
    132     prefs()->SetKnownDisabled(before_set);
    133     EXPECT_TRUE(prefs()->GetKnownDisabled(&before_set));
    134     EXPECT_TRUE(before_set.empty());
    135 
    136     set_.insert(prefs_.AddExtensionAndReturnId("1"));
    137     set_.insert(prefs_.AddExtensionAndReturnId("2"));
    138     set_.insert(prefs_.AddExtensionAndReturnId("3"));
    139     prefs()->SetKnownDisabled(set_);
    140   }
    141 
    142   virtual void Verify() OVERRIDE {
    143     ExtensionIdSet result;
    144     EXPECT_TRUE(prefs()->GetKnownDisabled(&result));
    145     ASSERT_EQ(set_, result);
    146   }
    147 
    148  private:
    149   ExtensionIdSet set_;
    150 };
    151 TEST_F(ExtensionPrefsKnownDisabled, KnownDisabled) {}
    152 
    153 // Tests the IsExtensionDisabled/SetExtensionState functions.
    154 class ExtensionPrefsExtensionState : public ExtensionPrefsTest {
    155  public:
    156   virtual void Initialize() OVERRIDE {
    157     extension = prefs_.AddExtension("test");
    158     prefs()->SetExtensionState(extension->id(), Extension::DISABLED);
    159   }
    160 
    161   virtual void Verify() OVERRIDE {
    162     EXPECT_TRUE(prefs()->IsExtensionDisabled(extension->id()));
    163   }
    164 
    165  private:
    166   scoped_refptr<Extension> extension;
    167 };
    168 TEST_F(ExtensionPrefsExtensionState, ExtensionState) {}
    169 
    170 class ExtensionPrefsEscalatePermissions : public ExtensionPrefsTest {
    171  public:
    172   virtual void Initialize() OVERRIDE {
    173     extension = prefs_.AddExtension("test");
    174     prefs()->SetDidExtensionEscalatePermissions(extension.get(), true);
    175   }
    176 
    177   virtual void Verify() OVERRIDE {
    178     EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension->id()));
    179   }
    180 
    181  private:
    182   scoped_refptr<Extension> extension;
    183 };
    184 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {}
    185 
    186 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
    187 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest {
    188  public:
    189   virtual void Initialize() OVERRIDE {
    190     const APIPermissionInfo* permission_info =
    191       PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
    192 
    193     extension_id_ = prefs_.AddExtensionAndReturnId("test");
    194 
    195     api_perm_set1_.insert(APIPermission::kTab);
    196     api_perm_set1_.insert(APIPermission::kBookmark);
    197     scoped_ptr<APIPermission> permission(
    198         permission_info->CreateAPIPermission());
    199     {
    200       scoped_ptr<base::ListValue> value(new base::ListValue());
    201       value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
    202       value->Append(new base::StringValue("udp-bind::8080"));
    203       value->Append(new base::StringValue("udp-send-to::8888"));
    204       ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
    205     }
    206     api_perm_set1_.insert(permission.release());
    207 
    208     api_perm_set2_.insert(APIPermission::kHistory);
    209 
    210     AddPattern(&ehost_perm_set1_, "http://*.google.com/*");
    211     AddPattern(&ehost_perm_set1_, "http://example.com/*");
    212     AddPattern(&ehost_perm_set1_, "chrome://favicon/*");
    213 
    214     AddPattern(&ehost_perm_set2_, "https://*.google.com/*");
    215     // with duplicate:
    216     AddPattern(&ehost_perm_set2_, "http://*.google.com/*");
    217 
    218     AddPattern(&shost_perm_set1_, "http://reddit.com/r/test/*");
    219     AddPattern(&shost_perm_set2_, "http://reddit.com/r/test/*");
    220     AddPattern(&shost_perm_set2_, "http://somesite.com/*");
    221     AddPattern(&shost_perm_set2_, "http://example.com/*");
    222 
    223     APIPermissionSet expected_apis = api_perm_set1_;
    224 
    225     AddPattern(&ehost_permissions_, "http://*.google.com/*");
    226     AddPattern(&ehost_permissions_, "http://example.com/*");
    227     AddPattern(&ehost_permissions_, "chrome://favicon/*");
    228     AddPattern(&ehost_permissions_, "https://*.google.com/*");
    229 
    230     AddPattern(&shost_permissions_, "http://reddit.com/r/test/*");
    231     AddPattern(&shost_permissions_, "http://somesite.com/*");
    232     AddPattern(&shost_permissions_, "http://example.com/*");
    233 
    234     APIPermissionSet empty_set;
    235     ManifestPermissionSet empty_manifest_permissions;
    236     URLPatternSet empty_extent;
    237     scoped_refptr<PermissionSet> permissions;
    238     scoped_refptr<PermissionSet> granted_permissions;
    239 
    240     // Make sure both granted api and host permissions start empty.
    241     granted_permissions =
    242         prefs()->GetGrantedPermissions(extension_id_);
    243     EXPECT_TRUE(granted_permissions->IsEmpty());
    244 
    245     permissions = new PermissionSet(
    246         api_perm_set1_, empty_manifest_permissions, empty_extent, empty_extent);
    247 
    248     // Add part of the api permissions.
    249     prefs()->AddGrantedPermissions(extension_id_, permissions.get());
    250     granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
    251     EXPECT_TRUE(granted_permissions.get());
    252     EXPECT_FALSE(granted_permissions->IsEmpty());
    253     EXPECT_EQ(expected_apis, granted_permissions->apis());
    254     EXPECT_TRUE(granted_permissions->effective_hosts().is_empty());
    255     EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
    256     granted_permissions = NULL;
    257 
    258     // Add part of the explicit host permissions.
    259     permissions = new PermissionSet(
    260         empty_set, empty_manifest_permissions, ehost_perm_set1_, empty_extent);
    261     prefs()->AddGrantedPermissions(extension_id_, permissions.get());
    262     granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
    263     EXPECT_FALSE(granted_permissions->IsEmpty());
    264     EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
    265     EXPECT_EQ(expected_apis, granted_permissions->apis());
    266     EXPECT_EQ(ehost_perm_set1_,
    267               granted_permissions->explicit_hosts());
    268     EXPECT_EQ(ehost_perm_set1_,
    269               granted_permissions->effective_hosts());
    270 
    271     // Add part of the scriptable host permissions.
    272     permissions = new PermissionSet(
    273         empty_set, empty_manifest_permissions, empty_extent, shost_perm_set1_);
    274     prefs()->AddGrantedPermissions(extension_id_, permissions.get());
    275     granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
    276     EXPECT_FALSE(granted_permissions->IsEmpty());
    277     EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
    278     EXPECT_EQ(expected_apis, granted_permissions->apis());
    279     EXPECT_EQ(ehost_perm_set1_,
    280               granted_permissions->explicit_hosts());
    281     EXPECT_EQ(shost_perm_set1_,
    282               granted_permissions->scriptable_hosts());
    283 
    284     URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_,
    285                                &effective_permissions_);
    286     EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
    287 
    288     // Add the rest of the permissions.
    289     permissions = new PermissionSet(
    290         api_perm_set2_, empty_manifest_permissions,
    291         ehost_perm_set2_, shost_perm_set2_);
    292 
    293     APIPermissionSet::Union(expected_apis, api_perm_set2_, &api_permissions_);
    294 
    295     prefs()->AddGrantedPermissions(extension_id_, permissions.get());
    296     granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
    297     EXPECT_TRUE(granted_permissions.get());
    298     EXPECT_FALSE(granted_permissions->IsEmpty());
    299     EXPECT_EQ(api_permissions_, granted_permissions->apis());
    300     EXPECT_EQ(ehost_permissions_,
    301               granted_permissions->explicit_hosts());
    302     EXPECT_EQ(shost_permissions_,
    303               granted_permissions->scriptable_hosts());
    304     effective_permissions_.ClearPatterns();
    305     URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_,
    306                                &effective_permissions_);
    307     EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
    308   }
    309 
    310   virtual void Verify() OVERRIDE {
    311     scoped_refptr<PermissionSet> permissions(
    312         prefs()->GetGrantedPermissions(extension_id_));
    313     EXPECT_TRUE(permissions.get());
    314     EXPECT_FALSE(permissions->HasEffectiveFullAccess());
    315     EXPECT_EQ(api_permissions_, permissions->apis());
    316     EXPECT_EQ(ehost_permissions_,
    317               permissions->explicit_hosts());
    318     EXPECT_EQ(shost_permissions_,
    319               permissions->scriptable_hosts());
    320   }
    321 
    322  private:
    323   std::string extension_id_;
    324   APIPermissionSet api_perm_set1_;
    325   APIPermissionSet api_perm_set2_;
    326   URLPatternSet ehost_perm_set1_;
    327   URLPatternSet ehost_perm_set2_;
    328   URLPatternSet shost_perm_set1_;
    329   URLPatternSet shost_perm_set2_;
    330 
    331   APIPermissionSet api_permissions_;
    332   URLPatternSet ehost_permissions_;
    333   URLPatternSet shost_permissions_;
    334   URLPatternSet effective_permissions_;
    335 };
    336 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
    337 
    338 // Tests the SetActivePermissions / GetActivePermissions functions.
    339 class ExtensionPrefsActivePermissions : public ExtensionPrefsTest {
    340  public:
    341   virtual void Initialize() OVERRIDE {
    342     extension_id_ = prefs_.AddExtensionAndReturnId("test");
    343 
    344     APIPermissionSet api_perms;
    345     api_perms.insert(APIPermission::kTab);
    346     api_perms.insert(APIPermission::kBookmark);
    347     api_perms.insert(APIPermission::kHistory);
    348 
    349     ManifestPermissionSet empty_manifest_permissions;
    350 
    351     URLPatternSet ehosts;
    352     AddPattern(&ehosts, "http://*.google.com/*");
    353     AddPattern(&ehosts, "http://example.com/*");
    354     AddPattern(&ehosts, "chrome://favicon/*");
    355 
    356     URLPatternSet shosts;
    357     AddPattern(&shosts, "https://*.google.com/*");
    358     AddPattern(&shosts, "http://reddit.com/r/test/*");
    359 
    360     active_perms_ = new PermissionSet(
    361         api_perms, empty_manifest_permissions, ehosts, shosts);
    362 
    363     // Make sure the active permissions start empty.
    364     scoped_refptr<PermissionSet> active(
    365         prefs()->GetActivePermissions(extension_id_));
    366     EXPECT_TRUE(active->IsEmpty());
    367 
    368     // Set the active permissions.
    369     prefs()->SetActivePermissions(extension_id_, active_perms_.get());
    370     active = prefs()->GetActivePermissions(extension_id_);
    371     EXPECT_EQ(active_perms_->apis(), active->apis());
    372     EXPECT_EQ(active_perms_->explicit_hosts(), active->explicit_hosts());
    373     EXPECT_EQ(active_perms_->scriptable_hosts(), active->scriptable_hosts());
    374     EXPECT_EQ(*active_perms_.get(), *active.get());
    375   }
    376 
    377   virtual void Verify() OVERRIDE {
    378     scoped_refptr<PermissionSet> permissions(
    379         prefs()->GetActivePermissions(extension_id_));
    380     EXPECT_EQ(*active_perms_.get(), *permissions.get());
    381   }
    382 
    383  private:
    384   std::string extension_id_;
    385   scoped_refptr<PermissionSet> active_perms_;
    386 };
    387 TEST_F(ExtensionPrefsActivePermissions, SetAndGetActivePermissions) {}
    388 
    389 // Tests the GetVersionString function.
    390 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
    391  public:
    392   virtual void Initialize() OVERRIDE {
    393     extension = prefs_.AddExtension("test");
    394     EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
    395     prefs()->OnExtensionUninstalled(extension->id(),
    396                                     Manifest::INTERNAL, false);
    397   }
    398 
    399   virtual void Verify() OVERRIDE {
    400     EXPECT_EQ("", prefs()->GetVersionString(extension->id()));
    401   }
    402 
    403  private:
    404   scoped_refptr<Extension> extension;
    405 };
    406 TEST_F(ExtensionPrefsVersionString, VersionString) {}
    407 
    408 class ExtensionPrefsAcknowledgment : public ExtensionPrefsTest {
    409  public:
    410   virtual void Initialize() OVERRIDE {
    411     not_installed_id_ = "pghjnghklobnfoidcldiidjjjhkeeaoi";
    412 
    413     // Install some extensions.
    414     for (int i = 0; i < 5; i++) {
    415       std::string name = "test" + base::IntToString(i);
    416       extensions_.push_back(prefs_.AddExtension(name));
    417     }
    418     EXPECT_EQ(NULL,
    419               prefs()->GetInstalledExtensionInfo(not_installed_id_).get());
    420 
    421     ExtensionList::const_iterator iter;
    422     for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
    423       std::string id = (*iter)->id();
    424       EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
    425       EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
    426       if (external_id_.empty()) {
    427         external_id_ = id;
    428         continue;
    429       }
    430       if (blacklisted_id_.empty()) {
    431         blacklisted_id_ = id;
    432         continue;
    433       }
    434     }
    435     // For each type of acknowledgment, acknowledge one installed and one
    436     // not-installed extension id.
    437     prefs()->AcknowledgeExternalExtension(external_id_);
    438     prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_);
    439     prefs()->AcknowledgeExternalExtension(not_installed_id_);
    440     prefs()->AcknowledgeBlacklistedExtension(not_installed_id_);
    441   }
    442 
    443   virtual void Verify() OVERRIDE {
    444     ExtensionList::const_iterator iter;
    445     for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
    446       std::string id = (*iter)->id();
    447       if (id == external_id_) {
    448         EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id));
    449       } else {
    450         EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
    451       }
    452       if (id == blacklisted_id_) {
    453         EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id));
    454       } else {
    455         EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
    456       }
    457     }
    458     EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_));
    459     EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_));
    460   }
    461 
    462  private:
    463   ExtensionList extensions_;
    464 
    465   std::string not_installed_id_;
    466   std::string external_id_;
    467   std::string blacklisted_id_;
    468 };
    469 TEST_F(ExtensionPrefsAcknowledgment, Acknowledgment) {}
    470 
    471 // Tests the idle install information functions.
    472 class ExtensionPrefsDelayedInstallInfo : public ExtensionPrefsTest {
    473  public:
    474   // Sets idle install information for one test extension.
    475   void SetIdleInfo(const std::string& id, int num) {
    476     base::DictionaryValue manifest;
    477     manifest.SetString(manifest_keys::kName, "test");
    478     manifest.SetString(manifest_keys::kVersion, "1." + base::IntToString(num));
    479     base::FilePath path =
    480         prefs_.extensions_dir().AppendASCII(base::IntToString(num));
    481     std::string errors;
    482     scoped_refptr<Extension> extension = Extension::Create(
    483         path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id, &errors);
    484     ASSERT_TRUE(extension.get()) << errors;
    485     ASSERT_EQ(id, extension->id());
    486     prefs()->SetDelayedInstallInfo(extension.get(),
    487                                    Extension::ENABLED,
    488                                    kInstallFlagNone,
    489                                    ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
    490                                    syncer::StringOrdinal(),
    491                                    std::string());
    492   }
    493 
    494   // Verifies that we get back expected idle install information previously
    495   // set by SetIdleInfo.
    496   void VerifyIdleInfo(const std::string& id, int num) {
    497     scoped_ptr<ExtensionInfo> info(prefs()->GetDelayedInstallInfo(id));
    498     ASSERT_TRUE(info);
    499     std::string version;
    500     ASSERT_TRUE(info->extension_manifest->GetString("version", &version));
    501     ASSERT_EQ("1." + base::IntToString(num), version);
    502     ASSERT_EQ(base::IntToString(num),
    503               info->extension_path.BaseName().MaybeAsASCII());
    504   }
    505 
    506   bool HasInfoForId(ExtensionPrefs::ExtensionsInfo* info,
    507                     const std::string& id) {
    508     for (size_t i = 0; i < info->size(); ++i) {
    509       if (info->at(i)->extension_id == id)
    510         return true;
    511     }
    512     return false;
    513   }
    514 
    515   virtual void Initialize() OVERRIDE {
    516     PathService::Get(chrome::DIR_TEST_DATA, &basedir_);
    517     now_ = Time::Now();
    518     id1_ = prefs_.AddExtensionAndReturnId("1");
    519     id2_ = prefs_.AddExtensionAndReturnId("2");
    520     id3_ = prefs_.AddExtensionAndReturnId("3");
    521     id4_ = prefs_.AddExtensionAndReturnId("4");
    522 
    523     // Set info for two extensions, then remove it.
    524     SetIdleInfo(id1_, 1);
    525     SetIdleInfo(id2_, 2);
    526     VerifyIdleInfo(id1_, 1);
    527     VerifyIdleInfo(id2_, 2);
    528     scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
    529         prefs()->GetAllDelayedInstallInfo());
    530     EXPECT_EQ(2u, info->size());
    531     EXPECT_TRUE(HasInfoForId(info.get(), id1_));
    532     EXPECT_TRUE(HasInfoForId(info.get(), id2_));
    533     prefs()->RemoveDelayedInstallInfo(id1_);
    534     prefs()->RemoveDelayedInstallInfo(id2_);
    535     info = prefs()->GetAllDelayedInstallInfo();
    536     EXPECT_TRUE(info->empty());
    537 
    538     // Try getting/removing info for an id that used to have info set.
    539     EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_));
    540     EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_));
    541 
    542     // Try getting/removing info for an id that has not yet had any info set.
    543     EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
    544     EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_));
    545 
    546     // Set info for 4 extensions, then remove for one of them.
    547     SetIdleInfo(id1_, 1);
    548     SetIdleInfo(id2_, 2);
    549     SetIdleInfo(id3_, 3);
    550     SetIdleInfo(id4_, 4);
    551     VerifyIdleInfo(id1_, 1);
    552     VerifyIdleInfo(id2_, 2);
    553     VerifyIdleInfo(id3_, 3);
    554     VerifyIdleInfo(id4_, 4);
    555     prefs()->RemoveDelayedInstallInfo(id3_);
    556   }
    557 
    558   virtual void Verify() OVERRIDE {
    559     // Make sure the info for the 3 extensions we expect is present.
    560     scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
    561         prefs()->GetAllDelayedInstallInfo());
    562     EXPECT_EQ(3u, info->size());
    563     EXPECT_TRUE(HasInfoForId(info.get(), id1_));
    564     EXPECT_TRUE(HasInfoForId(info.get(), id2_));
    565     EXPECT_TRUE(HasInfoForId(info.get(), id4_));
    566     VerifyIdleInfo(id1_, 1);
    567     VerifyIdleInfo(id2_, 2);
    568     VerifyIdleInfo(id4_, 4);
    569 
    570     // Make sure there isn't info the for the one extension id we removed.
    571     EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
    572   }
    573 
    574  protected:
    575   Time now_;
    576   base::FilePath basedir_;
    577   std::string id1_;
    578   std::string id2_;
    579   std::string id3_;
    580   std::string id4_;
    581 };
    582 TEST_F(ExtensionPrefsDelayedInstallInfo, DelayedInstallInfo) {}
    583 
    584 // Tests the FinishDelayedInstallInfo function.
    585 class ExtensionPrefsFinishDelayedInstallInfo : public ExtensionPrefsTest {
    586  public:
    587   virtual void Initialize() OVERRIDE {
    588     base::DictionaryValue dictionary;
    589     dictionary.SetString(manifest_keys::kName, "test");
    590     dictionary.SetString(manifest_keys::kVersion, "0.1");
    591     dictionary.SetString(manifest_keys::kBackgroundPage, "background.html");
    592     scoped_refptr<Extension> extension =
    593         prefs_.AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
    594     id_ = extension->id();
    595 
    596 
    597     // Set idle info
    598     base::DictionaryValue manifest;
    599     manifest.SetString(manifest_keys::kName, "test");
    600     manifest.SetString(manifest_keys::kVersion, "0.2");
    601     scoped_ptr<base::ListValue> scripts(new base::ListValue);
    602     scripts->AppendString("test.js");
    603     manifest.Set(manifest_keys::kBackgroundScripts, scripts.release());
    604     base::FilePath path =
    605         prefs_.extensions_dir().AppendASCII("test_0.2");
    606     std::string errors;
    607     scoped_refptr<Extension> new_extension = Extension::Create(
    608         path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id_, &errors);
    609     ASSERT_TRUE(new_extension.get()) << errors;
    610     ASSERT_EQ(id_, new_extension->id());
    611     prefs()->SetDelayedInstallInfo(new_extension.get(),
    612                                    Extension::ENABLED,
    613                                    kInstallFlagNone,
    614                                    ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
    615                                    syncer::StringOrdinal(),
    616                                    "Param");
    617 
    618     // Finish idle installation
    619     ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_));
    620   }
    621 
    622   virtual void Verify() OVERRIDE {
    623     EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_));
    624     EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(id_));
    625 
    626     const base::DictionaryValue* manifest;
    627     ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_, "manifest", &manifest));
    628     ASSERT_TRUE(manifest);
    629     std::string value;
    630     EXPECT_TRUE(manifest->GetString(manifest_keys::kName, &value));
    631     EXPECT_EQ("test", value);
    632     EXPECT_TRUE(manifest->GetString(manifest_keys::kVersion, &value));
    633     EXPECT_EQ("0.2", value);
    634     EXPECT_FALSE(manifest->GetString(manifest_keys::kBackgroundPage, &value));
    635     const base::ListValue* scripts;
    636     ASSERT_TRUE(manifest->GetList(manifest_keys::kBackgroundScripts, &scripts));
    637     EXPECT_EQ(1u, scripts->GetSize());
    638   }
    639 
    640  protected:
    641   std::string id_;
    642 };
    643 TEST_F(ExtensionPrefsFinishDelayedInstallInfo, FinishDelayedInstallInfo) {}
    644 
    645 class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest {
    646  public:
    647   virtual void Initialize() OVERRIDE {
    648     extension_ = prefs_.AddExtension("on_extension_installed");
    649     EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id()));
    650     prefs()->OnExtensionInstalled(extension_.get(),
    651                                   Extension::DISABLED,
    652                                   syncer::StringOrdinal(),
    653                                   "Param");
    654   }
    655 
    656   virtual void Verify() OVERRIDE {
    657     EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_->id()));
    658     EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(extension_->id()));
    659   }
    660 
    661  private:
    662   scoped_refptr<Extension> extension_;
    663 };
    664 TEST_F(ExtensionPrefsOnExtensionInstalled,
    665        ExtensionPrefsOnExtensionInstalled) {}
    666 
    667 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest {
    668  public:
    669   virtual void Initialize() OVERRIDE {
    670     extension_ = prefs_.AddExtension("on_extension_installed");
    671     EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id()));
    672     prefs()->OnExtensionInstalled(extension_.get(),
    673                                   Extension::ENABLED,
    674                                   syncer::StringOrdinal(),
    675                                   std::string());
    676   }
    677 
    678   virtual void Verify() OVERRIDE {
    679     // Set the flag and see if it persisted.
    680     prefs()->SetAppDraggedByUser(extension_->id());
    681     EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
    682 
    683     // Make sure it doesn't change on consecutive calls.
    684     prefs()->SetAppDraggedByUser(extension_->id());
    685     EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
    686   }
    687 
    688  private:
    689   scoped_refptr<Extension> extension_;
    690 };
    691 TEST_F(ExtensionPrefsAppDraggedByUser, ExtensionPrefsAppDraggedByUser) {}
    692 
    693 class ExtensionPrefsFlags : public ExtensionPrefsTest {
    694  public:
    695   virtual void Initialize() OVERRIDE {
    696     {
    697       base::DictionaryValue dictionary;
    698       dictionary.SetString(manifest_keys::kName, "from_webstore");
    699       dictionary.SetString(manifest_keys::kVersion, "0.1");
    700       webstore_extension_ = prefs_.AddExtensionWithManifestAndFlags(
    701           dictionary, Manifest::INTERNAL, Extension::FROM_WEBSTORE);
    702     }
    703 
    704     {
    705       base::DictionaryValue dictionary;
    706       dictionary.SetString(manifest_keys::kName, "from_bookmark");
    707       dictionary.SetString(manifest_keys::kVersion, "0.1");
    708       bookmark_extension_ = prefs_.AddExtensionWithManifestAndFlags(
    709           dictionary, Manifest::INTERNAL, Extension::FROM_BOOKMARK);
    710     }
    711 
    712     {
    713       base::DictionaryValue dictionary;
    714       dictionary.SetString(manifest_keys::kName, "was_installed_by_default");
    715       dictionary.SetString(manifest_keys::kVersion, "0.1");
    716       default_extension_ = prefs_.AddExtensionWithManifestAndFlags(
    717           dictionary,
    718           Manifest::INTERNAL,
    719           Extension::WAS_INSTALLED_BY_DEFAULT);
    720     }
    721 
    722     {
    723       base::DictionaryValue dictionary;
    724       dictionary.SetString(manifest_keys::kName, "was_installed_by_oem");
    725       dictionary.SetString(manifest_keys::kVersion, "0.1");
    726       oem_extension_ = prefs_.AddExtensionWithManifestAndFlags(
    727           dictionary, Manifest::INTERNAL, Extension::WAS_INSTALLED_BY_OEM);
    728     }
    729   }
    730 
    731   virtual void Verify() OVERRIDE {
    732     EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_->id()));
    733     EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_->id()));
    734 
    735     EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_->id()));
    736     EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_->id()));
    737 
    738     EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_->id()));
    739     EXPECT_TRUE(prefs()->WasInstalledByOem(oem_extension_->id()));
    740   }
    741 
    742  private:
    743   scoped_refptr<Extension> webstore_extension_;
    744   scoped_refptr<Extension> bookmark_extension_;
    745   scoped_refptr<Extension> default_extension_;
    746   scoped_refptr<Extension> oem_extension_;
    747 };
    748 TEST_F(ExtensionPrefsFlags, ExtensionPrefsFlags) {}
    749 
    750 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
    751     : ExtensionPrefsTest() {
    752   base::DictionaryValue simple_dict;
    753   std::string error;
    754 
    755   simple_dict.SetString(manifest_keys::kVersion, "1.0.0.0");
    756   simple_dict.SetString(manifest_keys::kName, "unused");
    757 
    758   extension1_ = Extension::Create(
    759       prefs_.temp_dir().AppendASCII("ext1_"),
    760       Manifest::EXTERNAL_PREF,
    761       simple_dict,
    762       Extension::NO_FLAGS,
    763       &error);
    764   extension2_ = Extension::Create(
    765       prefs_.temp_dir().AppendASCII("ext2_"),
    766       Manifest::EXTERNAL_PREF,
    767       simple_dict,
    768       Extension::NO_FLAGS,
    769       &error);
    770   extension3_ = Extension::Create(
    771       prefs_.temp_dir().AppendASCII("ext3_"),
    772       Manifest::EXTERNAL_PREF,
    773       simple_dict,
    774       Extension::NO_FLAGS,
    775       &error);
    776   extension4_ = Extension::Create(
    777       prefs_.temp_dir().AppendASCII("ext4_"),
    778       Manifest::EXTERNAL_PREF,
    779       simple_dict,
    780       Extension::NO_FLAGS,
    781       &error);
    782 
    783   for (size_t i = 0; i < kNumInstalledExtensions; ++i)
    784     installed_[i] = false;
    785 }
    786 
    787 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
    788 }
    789 
    790 // Tests that blacklist state can be queried.
    791 class ExtensionPrefsBlacklistedExtensions : public ExtensionPrefsTest {
    792  public:
    793   virtual ~ExtensionPrefsBlacklistedExtensions() {}
    794 
    795   virtual void Initialize() OVERRIDE {
    796     extension_a_ = prefs_.AddExtension("a");
    797     extension_b_ = prefs_.AddExtension("b");
    798     extension_c_ = prefs_.AddExtension("c");
    799   }
    800 
    801   virtual void Verify() OVERRIDE {
    802     {
    803       ExtensionIdSet ids;
    804       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    805     }
    806     prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
    807     {
    808       ExtensionIdSet ids;
    809       ids.insert(extension_a_->id());
    810       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    811     }
    812     prefs()->SetExtensionBlacklisted(extension_b_->id(), true);
    813     prefs()->SetExtensionBlacklisted(extension_c_->id(), true);
    814     {
    815       ExtensionIdSet ids;
    816       ids.insert(extension_a_->id());
    817       ids.insert(extension_b_->id());
    818       ids.insert(extension_c_->id());
    819       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    820     }
    821     prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
    822     {
    823       ExtensionIdSet ids;
    824       ids.insert(extension_b_->id());
    825       ids.insert(extension_c_->id());
    826       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    827     }
    828     prefs()->SetExtensionBlacklisted(extension_b_->id(), false);
    829     prefs()->SetExtensionBlacklisted(extension_c_->id(), false);
    830     {
    831       ExtensionIdSet ids;
    832       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    833     }
    834 
    835     // The interesting part: make sure that we're cleaning up after ourselves
    836     // when we're storing *just* the fact that the extension is blacklisted.
    837     std::string arbitrary_id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    838 
    839     prefs()->SetExtensionBlacklisted(arbitrary_id, true);
    840     prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
    841 
    842     // (And make sure that the acknowledged bit is also cleared).
    843     prefs()->AcknowledgeBlacklistedExtension(arbitrary_id);
    844 
    845     EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id));
    846     {
    847       ExtensionIdSet ids;
    848       ids.insert(arbitrary_id);
    849       ids.insert(extension_a_->id());
    850       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    851     }
    852     prefs()->SetExtensionBlacklisted(arbitrary_id, false);
    853     prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
    854     EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id));
    855     {
    856       ExtensionIdSet ids;
    857       EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
    858     }
    859   }
    860 
    861  private:
    862   scoped_refptr<const Extension> extension_a_;
    863   scoped_refptr<const Extension> extension_b_;
    864   scoped_refptr<const Extension> extension_c_;
    865 };
    866 TEST_F(ExtensionPrefsBlacklistedExtensions,
    867        ExtensionPrefsBlacklistedExtensions) {}
    868 
    869 // Tests the blacklist state. Old "blacklist" preference should take precedence
    870 // over new "blacklist_state".
    871 class ExtensionPrefsBlacklistState : public ExtensionPrefsTest {
    872  public:
    873   virtual ~ExtensionPrefsBlacklistState() {}
    874 
    875   virtual void Initialize() OVERRIDE {
    876     extension_a_ = prefs_.AddExtension("a");
    877   }
    878 
    879   virtual void Verify() OVERRIDE {
    880     ExtensionIdSet empty_ids;
    881     EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
    882 
    883     prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
    884     EXPECT_EQ(BLACKLISTED_MALWARE,
    885               prefs()->GetExtensionBlacklistState(extension_a_->id()));
    886 
    887     prefs()->SetExtensionBlacklistState(extension_a_->id(),
    888                                         BLACKLISTED_POTENTIALLY_UNWANTED);
    889     EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED,
    890               prefs()->GetExtensionBlacklistState(extension_a_->id()));
    891     EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
    892     EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
    893 
    894     prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
    895     EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
    896     EXPECT_EQ(BLACKLISTED_MALWARE,
    897               prefs()->GetExtensionBlacklistState(extension_a_->id()));
    898     EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
    899 
    900     prefs()->SetExtensionBlacklistState(extension_a_->id(),
    901                                         NOT_BLACKLISTED);
    902     EXPECT_EQ(NOT_BLACKLISTED,
    903               prefs()->GetExtensionBlacklistState(extension_a_->id()));
    904     EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
    905     EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
    906   }
    907 
    908  private:
    909   scoped_refptr<const Extension> extension_a_;
    910 };
    911 TEST_F(ExtensionPrefsBlacklistState, ExtensionPrefsBlacklistState) {}
    912 
    913 }  // namespace extensions
    914