NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / extensions / extension_prefs_unittest.cc
blob7d81c9f0f35a28f85304b0b3a56bd4e2a4980912
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.
5 #include "chrome/browser/extensions/./extension_prefs_unittest.h"
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/user_prefs/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/common/extension.h"
26 #include "extensions/common/manifest_constants.h"
27 #include "extensions/common/permissions/permission_set.h"
28 #include "extensions/common/permissions/permissions_info.h"
29 #include "sync/api/string_ordinal.h"
31 using base::Time;
32 using base::TimeDelta;
33 using content::BrowserThread;
35 namespace extensions {
37 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
38 int schemes = URLPattern::SCHEME_ALL;
39 extent->AddPattern(URLPattern(schemes, pattern));
42 ExtensionPrefsTest::ExtensionPrefsTest()
43 : ui_thread_(BrowserThread::UI, &message_loop_),
44 prefs_(message_loop_.message_loop_proxy().get()) {}
46 ExtensionPrefsTest::~ExtensionPrefsTest() {
49 void ExtensionPrefsTest::RegisterPreferences(
50 user_prefs::PrefRegistrySyncable* registry) {}
52 void ExtensionPrefsTest::SetUp() {
53 RegisterPreferences(prefs_.pref_registry().get());
54 Initialize();
57 void ExtensionPrefsTest::TearDown() {
58 Verify();
60 // Reset ExtensionPrefs, and re-verify.
61 prefs_.ResetPrefRegistry();
62 RegisterPreferences(prefs_.pref_registry().get());
63 prefs_.RecreateExtensionPrefs();
64 Verify();
65 prefs_.pref_service()->CommitPendingWrite();
66 message_loop_.RunUntilIdle();
69 // Tests the LastPingDay/SetLastPingDay functions.
70 class ExtensionPrefsLastPingDay : public ExtensionPrefsTest {
71 public:
72 ExtensionPrefsLastPingDay()
73 : extension_time_(Time::Now() - TimeDelta::FromHours(4)),
74 blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {}
76 virtual void Initialize() OVERRIDE {
77 extension_id_ = prefs_.AddExtensionAndReturnId("last_ping_day");
78 EXPECT_TRUE(prefs()->LastPingDay(extension_id_).is_null());
79 prefs()->SetLastPingDay(extension_id_, extension_time_);
80 prefs()->SetBlacklistLastPingDay(blacklist_time_);
83 virtual void Verify() OVERRIDE {
84 Time result = prefs()->LastPingDay(extension_id_);
85 EXPECT_FALSE(result.is_null());
86 EXPECT_TRUE(result == extension_time_);
87 result = prefs()->BlacklistLastPingDay();
88 EXPECT_FALSE(result.is_null());
89 EXPECT_TRUE(result == blacklist_time_);
92 private:
93 Time extension_time_;
94 Time blacklist_time_;
95 std::string extension_id_;
97 TEST_F(ExtensionPrefsLastPingDay, LastPingDay) {}
99 // Tests the GetToolbarOrder/SetToolbarOrder functions.
100 class ExtensionPrefsToolbarOrder : public ExtensionPrefsTest {
101 public:
102 virtual void Initialize() OVERRIDE {
103 list_.push_back(prefs_.AddExtensionAndReturnId("1"));
104 list_.push_back(prefs_.AddExtensionAndReturnId("2"));
105 list_.push_back(prefs_.AddExtensionAndReturnId("3"));
106 ExtensionIdList before_list = prefs()->GetToolbarOrder();
107 EXPECT_TRUE(before_list.empty());
108 prefs()->SetToolbarOrder(list_);
111 virtual void Verify() OVERRIDE {
112 ExtensionIdList result = prefs()->GetToolbarOrder();
113 ASSERT_EQ(list_, result);
116 private:
117 ExtensionIdList list_;
119 TEST_F(ExtensionPrefsToolbarOrder, ToolbarOrder) {}
121 // Tests the GetKnownDisabled/SetKnownDisabled functions.
122 class ExtensionPrefsKnownDisabled : public ExtensionPrefsTest {
123 public:
124 virtual void Initialize() OVERRIDE {
125 ExtensionIdSet before_set;
126 EXPECT_FALSE(prefs()->GetKnownDisabled(&before_set));
127 EXPECT_TRUE(before_set.empty());
129 // Initialize to an empty list and confirm that GetKnownDisabled() returns
130 // true and an empty list.
131 prefs()->SetKnownDisabled(before_set);
132 EXPECT_TRUE(prefs()->GetKnownDisabled(&before_set));
133 EXPECT_TRUE(before_set.empty());
135 set_.insert(prefs_.AddExtensionAndReturnId("1"));
136 set_.insert(prefs_.AddExtensionAndReturnId("2"));
137 set_.insert(prefs_.AddExtensionAndReturnId("3"));
138 prefs()->SetKnownDisabled(set_);
141 virtual void Verify() OVERRIDE {
142 ExtensionIdSet result;
143 EXPECT_TRUE(prefs()->GetKnownDisabled(&result));
144 ASSERT_EQ(set_, result);
147 private:
148 ExtensionIdSet set_;
150 TEST_F(ExtensionPrefsKnownDisabled, KnownDisabled) {}
152 // Tests the IsExtensionDisabled/SetExtensionState functions.
153 class ExtensionPrefsExtensionState : public ExtensionPrefsTest {
154 public:
155 virtual void Initialize() OVERRIDE {
156 extension = prefs_.AddExtension("test");
157 prefs()->SetExtensionState(extension->id(), Extension::DISABLED);
160 virtual void Verify() OVERRIDE {
161 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension->id()));
164 private:
165 scoped_refptr<Extension> extension;
167 TEST_F(ExtensionPrefsExtensionState, ExtensionState) {}
169 class ExtensionPrefsEscalatePermissions : public ExtensionPrefsTest {
170 public:
171 virtual void Initialize() OVERRIDE {
172 extension = prefs_.AddExtension("test");
173 prefs()->SetDidExtensionEscalatePermissions(extension.get(), true);
176 virtual void Verify() OVERRIDE {
177 EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension->id()));
180 private:
181 scoped_refptr<Extension> extension;
183 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {}
185 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
186 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest {
187 public:
188 virtual void Initialize() OVERRIDE {
189 const APIPermissionInfo* permission_info =
190 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
192 extension_id_ = prefs_.AddExtensionAndReturnId("test");
194 api_perm_set1_.insert(APIPermission::kTab);
195 api_perm_set1_.insert(APIPermission::kBookmark);
196 scoped_ptr<APIPermission> permission(
197 permission_info->CreateAPIPermission());
199 scoped_ptr<base::ListValue> value(new base::ListValue());
200 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
201 value->Append(new base::StringValue("udp-bind::8080"));
202 value->Append(new base::StringValue("udp-send-to::8888"));
203 ASSERT_TRUE(permission->FromValue(value.get(), NULL));
205 api_perm_set1_.insert(permission.release());
207 api_perm_set2_.insert(APIPermission::kHistory);
209 AddPattern(&ehost_perm_set1_, "http://*.google.com/*");
210 AddPattern(&ehost_perm_set1_, "http://example.com/*");
211 AddPattern(&ehost_perm_set1_, "chrome://favicon/*");
213 AddPattern(&ehost_perm_set2_, "https://*.google.com/*");
214 // with duplicate:
215 AddPattern(&ehost_perm_set2_, "http://*.google.com/*");
217 AddPattern(&shost_perm_set1_, "http://reddit.com/r/test/*");
218 AddPattern(&shost_perm_set2_, "http://reddit.com/r/test/*");
219 AddPattern(&shost_perm_set2_, "http://somesite.com/*");
220 AddPattern(&shost_perm_set2_, "http://example.com/*");
222 APIPermissionSet expected_apis = api_perm_set1_;
224 AddPattern(&ehost_permissions_, "http://*.google.com/*");
225 AddPattern(&ehost_permissions_, "http://example.com/*");
226 AddPattern(&ehost_permissions_, "chrome://favicon/*");
227 AddPattern(&ehost_permissions_, "https://*.google.com/*");
229 AddPattern(&shost_permissions_, "http://reddit.com/r/test/*");
230 AddPattern(&shost_permissions_, "http://somesite.com/*");
231 AddPattern(&shost_permissions_, "http://example.com/*");
233 APIPermissionSet empty_set;
234 ManifestPermissionSet empty_manifest_permissions;
235 URLPatternSet empty_extent;
236 scoped_refptr<PermissionSet> permissions;
237 scoped_refptr<PermissionSet> granted_permissions;
239 // Make sure both granted api and host permissions start empty.
240 granted_permissions =
241 prefs()->GetGrantedPermissions(extension_id_);
242 EXPECT_TRUE(granted_permissions->IsEmpty());
244 permissions = new PermissionSet(
245 api_perm_set1_, empty_manifest_permissions, empty_extent, empty_extent);
247 // Add part of the api permissions.
248 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
249 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
250 EXPECT_TRUE(granted_permissions.get());
251 EXPECT_FALSE(granted_permissions->IsEmpty());
252 EXPECT_EQ(expected_apis, granted_permissions->apis());
253 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty());
254 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
255 granted_permissions = NULL;
257 // Add part of the explicit host permissions.
258 permissions = new PermissionSet(
259 empty_set, empty_manifest_permissions, ehost_perm_set1_, empty_extent);
260 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
261 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
262 EXPECT_FALSE(granted_permissions->IsEmpty());
263 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
264 EXPECT_EQ(expected_apis, granted_permissions->apis());
265 EXPECT_EQ(ehost_perm_set1_,
266 granted_permissions->explicit_hosts());
267 EXPECT_EQ(ehost_perm_set1_,
268 granted_permissions->effective_hosts());
270 // Add part of the scriptable host permissions.
271 permissions = new PermissionSet(
272 empty_set, empty_manifest_permissions, empty_extent, shost_perm_set1_);
273 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
274 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
275 EXPECT_FALSE(granted_permissions->IsEmpty());
276 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
277 EXPECT_EQ(expected_apis, granted_permissions->apis());
278 EXPECT_EQ(ehost_perm_set1_,
279 granted_permissions->explicit_hosts());
280 EXPECT_EQ(shost_perm_set1_,
281 granted_permissions->scriptable_hosts());
283 URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_,
284 &effective_permissions_);
285 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
287 // Add the rest of the permissions.
288 permissions = new PermissionSet(
289 api_perm_set2_, empty_manifest_permissions,
290 ehost_perm_set2_, shost_perm_set2_);
292 APIPermissionSet::Union(expected_apis, api_perm_set2_, &api_permissions_);
294 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
295 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
296 EXPECT_TRUE(granted_permissions.get());
297 EXPECT_FALSE(granted_permissions->IsEmpty());
298 EXPECT_EQ(api_permissions_, granted_permissions->apis());
299 EXPECT_EQ(ehost_permissions_,
300 granted_permissions->explicit_hosts());
301 EXPECT_EQ(shost_permissions_,
302 granted_permissions->scriptable_hosts());
303 effective_permissions_.ClearPatterns();
304 URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_,
305 &effective_permissions_);
306 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
309 virtual void Verify() OVERRIDE {
310 scoped_refptr<PermissionSet> permissions(
311 prefs()->GetGrantedPermissions(extension_id_));
312 EXPECT_TRUE(permissions.get());
313 EXPECT_FALSE(permissions->HasEffectiveFullAccess());
314 EXPECT_EQ(api_permissions_, permissions->apis());
315 EXPECT_EQ(ehost_permissions_,
316 permissions->explicit_hosts());
317 EXPECT_EQ(shost_permissions_,
318 permissions->scriptable_hosts());
321 private:
322 std::string extension_id_;
323 APIPermissionSet api_perm_set1_;
324 APIPermissionSet api_perm_set2_;
325 URLPatternSet ehost_perm_set1_;
326 URLPatternSet ehost_perm_set2_;
327 URLPatternSet shost_perm_set1_;
328 URLPatternSet shost_perm_set2_;
330 APIPermissionSet api_permissions_;
331 URLPatternSet ehost_permissions_;
332 URLPatternSet shost_permissions_;
333 URLPatternSet effective_permissions_;
335 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
337 // Tests the SetActivePermissions / GetActivePermissions functions.
338 class ExtensionPrefsActivePermissions : public ExtensionPrefsTest {
339 public:
340 virtual void Initialize() OVERRIDE {
341 extension_id_ = prefs_.AddExtensionAndReturnId("test");
343 APIPermissionSet api_perms;
344 api_perms.insert(APIPermission::kTab);
345 api_perms.insert(APIPermission::kBookmark);
346 api_perms.insert(APIPermission::kHistory);
348 ManifestPermissionSet empty_manifest_permissions;
350 URLPatternSet ehosts;
351 AddPattern(&ehosts, "http://*.google.com/*");
352 AddPattern(&ehosts, "http://example.com/*");
353 AddPattern(&ehosts, "chrome://favicon/*");
355 URLPatternSet shosts;
356 AddPattern(&shosts, "https://*.google.com/*");
357 AddPattern(&shosts, "http://reddit.com/r/test/*");
359 active_perms_ = new PermissionSet(
360 api_perms, empty_manifest_permissions, ehosts, shosts);
362 // Make sure the active permissions start empty.
363 scoped_refptr<PermissionSet> active(
364 prefs()->GetActivePermissions(extension_id_));
365 EXPECT_TRUE(active->IsEmpty());
367 // Set the active permissions.
368 prefs()->SetActivePermissions(extension_id_, active_perms_.get());
369 active = prefs()->GetActivePermissions(extension_id_);
370 EXPECT_EQ(active_perms_->apis(), active->apis());
371 EXPECT_EQ(active_perms_->explicit_hosts(), active->explicit_hosts());
372 EXPECT_EQ(active_perms_->scriptable_hosts(), active->scriptable_hosts());
373 EXPECT_EQ(*active_perms_.get(), *active.get());
376 virtual void Verify() OVERRIDE {
377 scoped_refptr<PermissionSet> permissions(
378 prefs()->GetActivePermissions(extension_id_));
379 EXPECT_EQ(*active_perms_.get(), *permissions.get());
382 private:
383 std::string extension_id_;
384 scoped_refptr<PermissionSet> active_perms_;
386 TEST_F(ExtensionPrefsActivePermissions, SetAndGetActivePermissions) {}
388 // Tests the GetVersionString function.
389 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
390 public:
391 virtual void Initialize() OVERRIDE {
392 extension = prefs_.AddExtension("test");
393 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
394 prefs()->OnExtensionUninstalled(extension->id(),
395 Manifest::INTERNAL, false);
398 virtual void Verify() OVERRIDE {
399 EXPECT_EQ("", prefs()->GetVersionString(extension->id()));
402 private:
403 scoped_refptr<Extension> extension;
405 TEST_F(ExtensionPrefsVersionString, VersionString) {}
407 class ExtensionPrefsAcknowledgment : public ExtensionPrefsTest {
408 public:
409 virtual void Initialize() OVERRIDE {
410 not_installed_id_ = "pghjnghklobnfoidcldiidjjjhkeeaoi";
412 // Install some extensions.
413 for (int i = 0; i < 5; i++) {
414 std::string name = "test" + base::IntToString(i);
415 extensions_.push_back(prefs_.AddExtension(name));
417 EXPECT_EQ(NULL,
418 prefs()->GetInstalledExtensionInfo(not_installed_id_).get());
420 ExtensionList::const_iterator iter;
421 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
422 std::string id = (*iter)->id();
423 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
424 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
425 if (external_id_.empty()) {
426 external_id_ = id;
427 continue;
429 if (blacklisted_id_.empty()) {
430 blacklisted_id_ = id;
431 continue;
434 // For each type of acknowledgment, acknowledge one installed and one
435 // not-installed extension id.
436 prefs()->AcknowledgeExternalExtension(external_id_);
437 prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_);
438 prefs()->AcknowledgeExternalExtension(not_installed_id_);
439 prefs()->AcknowledgeBlacklistedExtension(not_installed_id_);
442 virtual void Verify() OVERRIDE {
443 ExtensionList::const_iterator iter;
444 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
445 std::string id = (*iter)->id();
446 if (id == external_id_) {
447 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id));
448 } else {
449 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
451 if (id == blacklisted_id_) {
452 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id));
453 } else {
454 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
457 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_));
458 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_));
461 private:
462 ExtensionList extensions_;
464 std::string not_installed_id_;
465 std::string external_id_;
466 std::string blacklisted_id_;
468 TEST_F(ExtensionPrefsAcknowledgment, Acknowledgment) {}
470 // Tests the idle install information functions.
471 class ExtensionPrefsDelayedInstallInfo : public ExtensionPrefsTest {
472 public:
473 // Sets idle install information for one test extension.
474 void SetIdleInfo(std::string id, int num) {
475 base::DictionaryValue manifest;
476 manifest.SetString(manifest_keys::kName, "test");
477 manifest.SetString(manifest_keys::kVersion, "1." + base::IntToString(num));
478 base::FilePath path =
479 prefs_.extensions_dir().AppendASCII(base::IntToString(num));
480 std::string errors;
481 scoped_refptr<Extension> extension = Extension::Create(
482 path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id, &errors);
483 ASSERT_TRUE(extension.get()) << errors;
484 ASSERT_EQ(id, extension->id());
485 prefs()->SetDelayedInstallInfo(extension.get(),
486 Extension::ENABLED,
487 false,
488 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
489 syncer::StringOrdinal());
492 // Verifies that we get back expected idle install information previously
493 // set by SetIdleInfo.
494 void VerifyIdleInfo(std::string id, int num) {
495 scoped_ptr<ExtensionInfo> info(prefs()->GetDelayedInstallInfo(id));
496 ASSERT_TRUE(info);
497 std::string version;
498 ASSERT_TRUE(info->extension_manifest->GetString("version", &version));
499 ASSERT_EQ("1." + base::IntToString(num), version);
500 ASSERT_EQ(base::IntToString(num),
501 info->extension_path.BaseName().MaybeAsASCII());
504 bool HasInfoForId(ExtensionPrefs::ExtensionsInfo* info,
505 const std::string& id) {
506 for (size_t i = 0; i < info->size(); ++i) {
507 if (info->at(i)->extension_id == id)
508 return true;
510 return false;
513 virtual void Initialize() OVERRIDE {
514 PathService::Get(chrome::DIR_TEST_DATA, &basedir_);
515 now_ = Time::Now();
516 id1_ = prefs_.AddExtensionAndReturnId("1");
517 id2_ = prefs_.AddExtensionAndReturnId("2");
518 id3_ = prefs_.AddExtensionAndReturnId("3");
519 id4_ = prefs_.AddExtensionAndReturnId("4");
521 // Set info for two extensions, then remove it.
522 SetIdleInfo(id1_, 1);
523 SetIdleInfo(id2_, 2);
524 VerifyIdleInfo(id1_, 1);
525 VerifyIdleInfo(id2_, 2);
526 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
527 prefs()->GetAllDelayedInstallInfo());
528 EXPECT_EQ(2u, info->size());
529 EXPECT_TRUE(HasInfoForId(info.get(), id1_));
530 EXPECT_TRUE(HasInfoForId(info.get(), id2_));
531 prefs()->RemoveDelayedInstallInfo(id1_);
532 prefs()->RemoveDelayedInstallInfo(id2_);
533 info = prefs()->GetAllDelayedInstallInfo();
534 EXPECT_TRUE(info->empty());
536 // Try getting/removing info for an id that used to have info set.
537 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_));
538 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_));
540 // Try getting/removing info for an id that has not yet had any info set.
541 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
542 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_));
544 // Set info for 4 extensions, then remove for one of them.
545 SetIdleInfo(id1_, 1);
546 SetIdleInfo(id2_, 2);
547 SetIdleInfo(id3_, 3);
548 SetIdleInfo(id4_, 4);
549 VerifyIdleInfo(id1_, 1);
550 VerifyIdleInfo(id2_, 2);
551 VerifyIdleInfo(id3_, 3);
552 VerifyIdleInfo(id4_, 4);
553 prefs()->RemoveDelayedInstallInfo(id3_);
556 virtual void Verify() OVERRIDE {
557 // Make sure the info for the 3 extensions we expect is present.
558 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
559 prefs()->GetAllDelayedInstallInfo());
560 EXPECT_EQ(3u, info->size());
561 EXPECT_TRUE(HasInfoForId(info.get(), id1_));
562 EXPECT_TRUE(HasInfoForId(info.get(), id2_));
563 EXPECT_TRUE(HasInfoForId(info.get(), id4_));
564 VerifyIdleInfo(id1_, 1);
565 VerifyIdleInfo(id2_, 2);
566 VerifyIdleInfo(id4_, 4);
568 // Make sure there isn't info the for the one extension id we removed.
569 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
572 protected:
573 Time now_;
574 base::FilePath basedir_;
575 std::string id1_;
576 std::string id2_;
577 std::string id3_;
578 std::string id4_;
580 TEST_F(ExtensionPrefsDelayedInstallInfo, DelayedInstallInfo) {}
582 // Tests the FinishDelayedInstallInfo function.
583 class ExtensionPrefsFinishDelayedInstallInfo : public ExtensionPrefsTest {
584 public:
585 virtual void Initialize() OVERRIDE {
586 base::DictionaryValue dictionary;
587 dictionary.SetString(manifest_keys::kName, "test");
588 dictionary.SetString(manifest_keys::kVersion, "0.1");
589 dictionary.SetString(manifest_keys::kBackgroundPage, "background.html");
590 scoped_refptr<Extension> extension =
591 prefs_.AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
592 id_ = extension->id();
595 // Set idle info
596 base::DictionaryValue manifest;
597 manifest.SetString(manifest_keys::kName, "test");
598 manifest.SetString(manifest_keys::kVersion, "0.2");
599 scoped_ptr<base::ListValue> scripts(new base::ListValue);
600 scripts->AppendString("test.js");
601 manifest.Set(manifest_keys::kBackgroundScripts, scripts.release());
602 base::FilePath path =
603 prefs_.extensions_dir().AppendASCII("test_0.2");
604 std::string errors;
605 scoped_refptr<Extension> new_extension = Extension::Create(
606 path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id_, &errors);
607 ASSERT_TRUE(new_extension.get()) << errors;
608 ASSERT_EQ(id_, new_extension->id());
609 prefs()->SetDelayedInstallInfo(new_extension.get(),
610 Extension::ENABLED,
611 false,
612 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
613 syncer::StringOrdinal());
615 // Finish idle installation
616 ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_));
619 virtual void Verify() OVERRIDE {
620 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_));
622 const base::DictionaryValue* manifest;
623 ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_, "manifest", &manifest));
624 ASSERT_TRUE(manifest);
625 std::string value;
626 EXPECT_TRUE(manifest->GetString(manifest_keys::kName, &value));
627 EXPECT_EQ("test", value);
628 EXPECT_TRUE(manifest->GetString(manifest_keys::kVersion, &value));
629 EXPECT_EQ("0.2", value);
630 EXPECT_FALSE(manifest->GetString(manifest_keys::kBackgroundPage, &value));
631 const base::ListValue* scripts;
632 ASSERT_TRUE(manifest->GetList(manifest_keys::kBackgroundScripts, &scripts));
633 EXPECT_EQ(1u, scripts->GetSize());
636 protected:
637 std::string id_;
639 TEST_F(ExtensionPrefsFinishDelayedInstallInfo, FinishDelayedInstallInfo) {}
641 class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest {
642 public:
643 virtual void Initialize() OVERRIDE {
644 extension_ = prefs_.AddExtension("on_extension_installed");
645 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id()));
646 prefs()->OnExtensionInstalled(extension_.get(),
647 Extension::DISABLED,
648 false,
649 syncer::StringOrdinal());
652 virtual void Verify() OVERRIDE {
653 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_->id()));
656 private:
657 scoped_refptr<Extension> extension_;
659 TEST_F(ExtensionPrefsOnExtensionInstalled,
660 ExtensionPrefsOnExtensionInstalled) {}
662 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest {
663 public:
664 virtual void Initialize() OVERRIDE {
665 extension_ = prefs_.AddExtension("on_extension_installed");
666 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id()));
667 prefs()->OnExtensionInstalled(extension_.get(),
668 Extension::ENABLED,
669 false,
670 syncer::StringOrdinal());
673 virtual void Verify() OVERRIDE {
674 // Set the flag and see if it persisted.
675 prefs()->SetAppDraggedByUser(extension_->id());
676 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
678 // Make sure it doesn't change on consecutive calls.
679 prefs()->SetAppDraggedByUser(extension_->id());
680 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
683 private:
684 scoped_refptr<Extension> extension_;
686 TEST_F(ExtensionPrefsAppDraggedByUser, ExtensionPrefsAppDraggedByUser) {}
688 class ExtensionPrefsFlags : public ExtensionPrefsTest {
689 public:
690 virtual void Initialize() OVERRIDE {
692 base::DictionaryValue dictionary;
693 dictionary.SetString(manifest_keys::kName, "from_webstore");
694 dictionary.SetString(manifest_keys::kVersion, "0.1");
695 webstore_extension_ = prefs_.AddExtensionWithManifestAndFlags(
696 dictionary, Manifest::INTERNAL, Extension::FROM_WEBSTORE);
700 base::DictionaryValue dictionary;
701 dictionary.SetString(manifest_keys::kName, "from_bookmark");
702 dictionary.SetString(manifest_keys::kVersion, "0.1");
703 bookmark_extension_ = prefs_.AddExtensionWithManifestAndFlags(
704 dictionary, Manifest::INTERNAL, Extension::FROM_BOOKMARK);
708 base::DictionaryValue dictionary;
709 dictionary.SetString(manifest_keys::kName, "was_installed_by_default");
710 dictionary.SetString(manifest_keys::kVersion, "0.1");
711 default_extension_ = prefs_.AddExtensionWithManifestAndFlags(
712 dictionary,
713 Manifest::INTERNAL,
714 Extension::WAS_INSTALLED_BY_DEFAULT);
718 virtual void Verify() OVERRIDE {
719 EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_->id()));
720 EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_->id()));
722 EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_->id()));
723 EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_->id()));
725 EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_->id()));
728 private:
729 scoped_refptr<Extension> webstore_extension_;
730 scoped_refptr<Extension> bookmark_extension_;
731 scoped_refptr<Extension> default_extension_;
733 TEST_F(ExtensionPrefsFlags, ExtensionPrefsFlags) {}
735 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
736 : ExtensionPrefsTest() {
737 base::DictionaryValue simple_dict;
738 std::string error;
740 simple_dict.SetString(manifest_keys::kVersion, "1.0.0.0");
741 simple_dict.SetString(manifest_keys::kName, "unused");
743 extension1_ = Extension::Create(
744 prefs_.temp_dir().AppendASCII("ext1_"),
745 Manifest::EXTERNAL_PREF,
746 simple_dict,
747 Extension::NO_FLAGS,
748 &error);
749 extension2_ = Extension::Create(
750 prefs_.temp_dir().AppendASCII("ext2_"),
751 Manifest::EXTERNAL_PREF,
752 simple_dict,
753 Extension::NO_FLAGS,
754 &error);
755 extension3_ = Extension::Create(
756 prefs_.temp_dir().AppendASCII("ext3_"),
757 Manifest::EXTERNAL_PREF,
758 simple_dict,
759 Extension::NO_FLAGS,
760 &error);
761 extension4_ = Extension::Create(
762 prefs_.temp_dir().AppendASCII("ext4_"),
763 Manifest::EXTERNAL_PREF,
764 simple_dict,
765 Extension::NO_FLAGS,
766 &error);
768 for (size_t i = 0; i < kNumInstalledExtensions; ++i)
769 installed_[i] = false;
772 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
775 // Tests that blacklist state can be queried.
776 class ExtensionPrefsBlacklistedExtensions : public ExtensionPrefsTest {
777 public:
778 virtual ~ExtensionPrefsBlacklistedExtensions() {}
780 virtual void Initialize() OVERRIDE {
781 extension_a_ = prefs_.AddExtension("a");
782 extension_b_ = prefs_.AddExtension("b");
783 extension_c_ = prefs_.AddExtension("c");
786 virtual void Verify() OVERRIDE {
788 ExtensionIdSet ids;
789 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
791 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
793 ExtensionIdSet ids;
794 ids.insert(extension_a_->id());
795 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
797 prefs()->SetExtensionBlacklisted(extension_b_->id(), true);
798 prefs()->SetExtensionBlacklisted(extension_c_->id(), true);
800 ExtensionIdSet ids;
801 ids.insert(extension_a_->id());
802 ids.insert(extension_b_->id());
803 ids.insert(extension_c_->id());
804 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
806 prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
808 ExtensionIdSet ids;
809 ids.insert(extension_b_->id());
810 ids.insert(extension_c_->id());
811 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
813 prefs()->SetExtensionBlacklisted(extension_b_->id(), false);
814 prefs()->SetExtensionBlacklisted(extension_c_->id(), false);
816 ExtensionIdSet ids;
817 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
820 // The interesting part: make sure that we're cleaning up after ourselves
821 // when we're storing *just* the fact that the extension is blacklisted.
822 std::string arbitrary_id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
824 prefs()->SetExtensionBlacklisted(arbitrary_id, true);
825 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
827 // (And make sure that the acknowledged bit is also cleared).
828 prefs()->AcknowledgeBlacklistedExtension(arbitrary_id);
830 EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id));
832 ExtensionIdSet ids;
833 ids.insert(arbitrary_id);
834 ids.insert(extension_a_->id());
835 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
837 prefs()->SetExtensionBlacklisted(arbitrary_id, false);
838 prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
839 EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id));
841 ExtensionIdSet ids;
842 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
846 private:
847 scoped_refptr<const Extension> extension_a_;
848 scoped_refptr<const Extension> extension_b_;
849 scoped_refptr<const Extension> extension_c_;
851 TEST_F(ExtensionPrefsBlacklistedExtensions,
852 ExtensionPrefsBlacklistedExtensions) {}
854 // Tests the blacklist state. Old "blacklist" preference should take precedence
855 // over new "blacklist_state".
856 class ExtensionPrefsBlacklistState : public ExtensionPrefsTest {
857 public:
858 virtual ~ExtensionPrefsBlacklistState() {}
860 virtual void Initialize() OVERRIDE {
861 extension_a_ = prefs_.AddExtension("a");
864 virtual void Verify() OVERRIDE {
865 ExtensionIdSet empty_ids;
866 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
868 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
869 EXPECT_EQ(BLACKLISTED_MALWARE,
870 prefs()->GetExtensionBlacklistState(extension_a_->id()));
872 prefs()->SetExtensionBlacklistState(extension_a_->id(),
873 BLACKLISTED_POTENTIALLY_UNWANTED);
874 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED,
875 prefs()->GetExtensionBlacklistState(extension_a_->id()));
876 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
877 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
879 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
880 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
881 EXPECT_EQ(BLACKLISTED_MALWARE,
882 prefs()->GetExtensionBlacklistState(extension_a_->id()));
883 EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
885 prefs()->SetExtensionBlacklistState(extension_a_->id(),
886 NOT_BLACKLISTED);
887 EXPECT_EQ(NOT_BLACKLISTED,
888 prefs()->GetExtensionBlacklistState(extension_a_->id()));
889 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
890 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
893 private:
894 scoped_refptr<const Extension> extension_a_;
896 TEST_F(ExtensionPrefsBlacklistState, ExtensionPrefsBlacklistState) {}
898 } // namespace extensions