Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / extensions / extension_prefs_unittest.cc
blob65ed8c53e095a697e62c6d1a094552c5fb8c474e
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/time/time.h"
17 #include "base/values.h"
18 #include "chrome/browser/prefs/pref_service_syncable.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "components/pref_registry/pref_registry_syncable.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_source.h"
23 #include "content/public/test/mock_notification_observer.h"
24 #include "extensions/browser/extension_pref_value_map.h"
25 #include "extensions/browser/extension_prefs.h"
26 #include "extensions/browser/install_flag.h"
27 #include "extensions/common/extension.h"
28 #include "extensions/common/extension_builder.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "extensions/common/permissions/permission_set.h"
31 #include "extensions/common/permissions/permissions_info.h"
32 #include "sync/api/string_ordinal.h"
34 using base::Time;
35 using base::TimeDelta;
36 using content::BrowserThread;
38 namespace extensions {
40 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
41 int schemes = URLPattern::SCHEME_ALL;
42 extent->AddPattern(URLPattern(schemes, pattern));
45 ExtensionPrefsTest::ExtensionPrefsTest()
46 : ui_thread_(BrowserThread::UI, &message_loop_),
47 prefs_(message_loop_.task_runner().get()) {
50 ExtensionPrefsTest::~ExtensionPrefsTest() {
53 void ExtensionPrefsTest::RegisterPreferences(
54 user_prefs::PrefRegistrySyncable* registry) {}
56 void ExtensionPrefsTest::SetUp() {
57 RegisterPreferences(prefs_.pref_registry().get());
58 Initialize();
61 void ExtensionPrefsTest::TearDown() {
62 Verify();
64 // Reset ExtensionPrefs, and re-verify.
65 prefs_.ResetPrefRegistry();
66 RegisterPreferences(prefs_.pref_registry().get());
67 prefs_.RecreateExtensionPrefs();
68 Verify();
69 prefs_.pref_service()->CommitPendingWrite();
70 message_loop_.RunUntilIdle();
73 // Tests the LastPingDay/SetLastPingDay functions.
74 class ExtensionPrefsLastPingDay : public ExtensionPrefsTest {
75 public:
76 ExtensionPrefsLastPingDay()
77 : extension_time_(Time::Now() - TimeDelta::FromHours(4)),
78 blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {}
80 void Initialize() override {
81 extension_id_ = prefs_.AddExtensionAndReturnId("last_ping_day");
82 EXPECT_TRUE(prefs()->LastPingDay(extension_id_).is_null());
83 prefs()->SetLastPingDay(extension_id_, extension_time_);
84 prefs()->SetBlacklistLastPingDay(blacklist_time_);
87 void Verify() override {
88 Time result = prefs()->LastPingDay(extension_id_);
89 EXPECT_FALSE(result.is_null());
90 EXPECT_TRUE(result == extension_time_);
91 result = prefs()->BlacklistLastPingDay();
92 EXPECT_FALSE(result.is_null());
93 EXPECT_TRUE(result == blacklist_time_);
96 private:
97 Time extension_time_;
98 Time blacklist_time_;
99 std::string extension_id_;
101 TEST_F(ExtensionPrefsLastPingDay, LastPingDay) {}
103 // Tests the GetToolbarOrder/SetToolbarOrder functions.
104 class ExtensionPrefsToolbarOrder : public ExtensionPrefsTest {
105 public:
106 void Initialize() override {
107 list_.push_back(prefs_.AddExtensionAndReturnId("1"));
108 list_.push_back(prefs_.AddExtensionAndReturnId("2"));
109 list_.push_back(prefs_.AddExtensionAndReturnId("3"));
110 ExtensionIdList before_list = prefs()->GetToolbarOrder();
111 EXPECT_TRUE(before_list.empty());
112 prefs()->SetToolbarOrder(list_);
115 void Verify() override {
116 ExtensionIdList result = prefs()->GetToolbarOrder();
117 ASSERT_EQ(list_, result);
120 private:
121 ExtensionIdList list_;
123 TEST_F(ExtensionPrefsToolbarOrder, ToolbarOrder) {}
125 // Tests the IsExtensionDisabled/SetExtensionState functions.
126 class ExtensionPrefsExtensionState : public ExtensionPrefsTest {
127 public:
128 void Initialize() override {
129 extension = prefs_.AddExtension("test");
130 prefs()->SetExtensionState(extension->id(), Extension::DISABLED);
133 void Verify() override {
134 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension->id()));
137 private:
138 scoped_refptr<Extension> extension;
140 TEST_F(ExtensionPrefsExtensionState, ExtensionState) {}
142 class ExtensionPrefsEscalatePermissions : public ExtensionPrefsTest {
143 public:
144 void Initialize() override {
145 extension = prefs_.AddExtension("test");
146 prefs()->AddDisableReason(extension->id(),
147 Extension::DISABLE_PERMISSIONS_INCREASE);
150 void Verify() override {
151 EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension->id()));
154 private:
155 scoped_refptr<Extension> extension;
157 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {}
159 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
160 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest {
161 public:
162 void Initialize() override {
163 const APIPermissionInfo* permission_info =
164 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
166 extension_id_ = prefs_.AddExtensionAndReturnId("test");
168 api_perm_set1_.insert(APIPermission::kTab);
169 api_perm_set1_.insert(APIPermission::kBookmark);
170 scoped_ptr<APIPermission> permission(
171 permission_info->CreateAPIPermission());
173 scoped_ptr<base::ListValue> value(new base::ListValue());
174 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
175 value->Append(new base::StringValue("udp-bind::8080"));
176 value->Append(new base::StringValue("udp-send-to::8888"));
177 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
179 api_perm_set1_.insert(permission.release());
181 api_perm_set2_.insert(APIPermission::kHistory);
183 AddPattern(&ehost_perm_set1_, "http://*.google.com/*");
184 AddPattern(&ehost_perm_set1_, "http://example.com/*");
185 AddPattern(&ehost_perm_set1_, "chrome://favicon/*");
187 AddPattern(&ehost_perm_set2_, "https://*.google.com/*");
188 // with duplicate:
189 AddPattern(&ehost_perm_set2_, "http://*.google.com/*");
191 AddPattern(&shost_perm_set1_, "http://reddit.com/r/test/*");
192 AddPattern(&shost_perm_set2_, "http://reddit.com/r/test/*");
193 AddPattern(&shost_perm_set2_, "http://somesite.com/*");
194 AddPattern(&shost_perm_set2_, "http://example.com/*");
196 APIPermissionSet expected_apis = api_perm_set1_;
198 AddPattern(&ehost_permissions_, "http://*.google.com/*");
199 AddPattern(&ehost_permissions_, "http://example.com/*");
200 AddPattern(&ehost_permissions_, "chrome://favicon/*");
201 AddPattern(&ehost_permissions_, "https://*.google.com/*");
203 AddPattern(&shost_permissions_, "http://reddit.com/r/test/*");
204 AddPattern(&shost_permissions_, "http://somesite.com/*");
205 AddPattern(&shost_permissions_, "http://example.com/*");
207 APIPermissionSet empty_set;
208 ManifestPermissionSet empty_manifest_permissions;
209 URLPatternSet empty_extent;
210 scoped_refptr<PermissionSet> permissions;
211 scoped_refptr<PermissionSet> granted_permissions;
213 // Make sure both granted api and host permissions start empty.
214 granted_permissions =
215 prefs()->GetGrantedPermissions(extension_id_);
216 EXPECT_TRUE(granted_permissions->IsEmpty());
218 permissions = new PermissionSet(
219 api_perm_set1_, empty_manifest_permissions, empty_extent, empty_extent);
221 // Add part of the api permissions.
222 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
223 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
224 EXPECT_TRUE(granted_permissions.get());
225 EXPECT_FALSE(granted_permissions->IsEmpty());
226 EXPECT_EQ(expected_apis, granted_permissions->apis());
227 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty());
228 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
229 granted_permissions = NULL;
231 // Add part of the explicit host permissions.
232 permissions = new PermissionSet(
233 empty_set, empty_manifest_permissions, ehost_perm_set1_, empty_extent);
234 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
235 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
236 EXPECT_FALSE(granted_permissions->IsEmpty());
237 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
238 EXPECT_EQ(expected_apis, granted_permissions->apis());
239 EXPECT_EQ(ehost_perm_set1_,
240 granted_permissions->explicit_hosts());
241 EXPECT_EQ(ehost_perm_set1_,
242 granted_permissions->effective_hosts());
244 // Add part of the scriptable host permissions.
245 permissions = new PermissionSet(
246 empty_set, empty_manifest_permissions, empty_extent, shost_perm_set1_);
247 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
248 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
249 EXPECT_FALSE(granted_permissions->IsEmpty());
250 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
251 EXPECT_EQ(expected_apis, granted_permissions->apis());
252 EXPECT_EQ(ehost_perm_set1_,
253 granted_permissions->explicit_hosts());
254 EXPECT_EQ(shost_perm_set1_,
255 granted_permissions->scriptable_hosts());
257 URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_,
258 &effective_permissions_);
259 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
261 // Add the rest of the permissions.
262 permissions = new PermissionSet(
263 api_perm_set2_, empty_manifest_permissions,
264 ehost_perm_set2_, shost_perm_set2_);
266 APIPermissionSet::Union(expected_apis, api_perm_set2_, &api_permissions_);
268 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
269 granted_permissions = prefs()->GetGrantedPermissions(extension_id_);
270 EXPECT_TRUE(granted_permissions.get());
271 EXPECT_FALSE(granted_permissions->IsEmpty());
272 EXPECT_EQ(api_permissions_, granted_permissions->apis());
273 EXPECT_EQ(ehost_permissions_,
274 granted_permissions->explicit_hosts());
275 EXPECT_EQ(shost_permissions_,
276 granted_permissions->scriptable_hosts());
277 effective_permissions_.ClearPatterns();
278 URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_,
279 &effective_permissions_);
280 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts());
283 void Verify() override {
284 scoped_refptr<PermissionSet> permissions(
285 prefs()->GetGrantedPermissions(extension_id_));
286 EXPECT_TRUE(permissions.get());
287 EXPECT_FALSE(permissions->HasEffectiveFullAccess());
288 EXPECT_EQ(api_permissions_, permissions->apis());
289 EXPECT_EQ(ehost_permissions_,
290 permissions->explicit_hosts());
291 EXPECT_EQ(shost_permissions_,
292 permissions->scriptable_hosts());
295 private:
296 std::string extension_id_;
297 APIPermissionSet api_perm_set1_;
298 APIPermissionSet api_perm_set2_;
299 URLPatternSet ehost_perm_set1_;
300 URLPatternSet ehost_perm_set2_;
301 URLPatternSet shost_perm_set1_;
302 URLPatternSet shost_perm_set2_;
304 APIPermissionSet api_permissions_;
305 URLPatternSet ehost_permissions_;
306 URLPatternSet shost_permissions_;
307 URLPatternSet effective_permissions_;
309 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
311 // Tests the SetActivePermissions / GetActivePermissions functions.
312 class ExtensionPrefsActivePermissions : public ExtensionPrefsTest {
313 public:
314 void Initialize() override {
315 extension_id_ = prefs_.AddExtensionAndReturnId("test");
317 APIPermissionSet api_perms;
318 api_perms.insert(APIPermission::kTab);
319 api_perms.insert(APIPermission::kBookmark);
320 api_perms.insert(APIPermission::kHistory);
322 ManifestPermissionSet empty_manifest_permissions;
324 URLPatternSet ehosts;
325 AddPattern(&ehosts, "http://*.google.com/*");
326 AddPattern(&ehosts, "http://example.com/*");
327 AddPattern(&ehosts, "chrome://favicon/*");
329 URLPatternSet shosts;
330 AddPattern(&shosts, "https://*.google.com/*");
331 AddPattern(&shosts, "http://reddit.com/r/test/*");
333 active_perms_ = new PermissionSet(
334 api_perms, empty_manifest_permissions, ehosts, shosts);
336 // Make sure the active permissions start empty.
337 scoped_refptr<PermissionSet> active(
338 prefs()->GetActivePermissions(extension_id_));
339 EXPECT_TRUE(active->IsEmpty());
341 // Set the active permissions.
342 prefs()->SetActivePermissions(extension_id_, active_perms_.get());
343 active = prefs()->GetActivePermissions(extension_id_);
344 EXPECT_EQ(active_perms_->apis(), active->apis());
345 EXPECT_EQ(active_perms_->explicit_hosts(), active->explicit_hosts());
346 EXPECT_EQ(active_perms_->scriptable_hosts(), active->scriptable_hosts());
347 EXPECT_EQ(*active_perms_.get(), *active.get());
350 void Verify() override {
351 scoped_refptr<PermissionSet> permissions(
352 prefs()->GetActivePermissions(extension_id_));
353 EXPECT_EQ(*active_perms_.get(), *permissions.get());
356 private:
357 std::string extension_id_;
358 scoped_refptr<PermissionSet> active_perms_;
360 TEST_F(ExtensionPrefsActivePermissions, SetAndGetActivePermissions) {}
362 // Tests the GetVersionString function.
363 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
364 public:
365 void Initialize() override {
366 extension = prefs_.AddExtension("test");
367 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
368 prefs()->OnExtensionUninstalled(extension->id(),
369 Manifest::INTERNAL, false);
372 void Verify() override {
373 EXPECT_EQ("", prefs()->GetVersionString(extension->id()));
376 private:
377 scoped_refptr<Extension> extension;
379 TEST_F(ExtensionPrefsVersionString, VersionString) {}
381 class ExtensionPrefsAcknowledgment : public ExtensionPrefsTest {
382 public:
383 void Initialize() override {
384 not_installed_id_ = "pghjnghklobnfoidcldiidjjjhkeeaoi";
386 // Install some extensions.
387 for (int i = 0; i < 5; i++) {
388 std::string name = "test" + base::IntToString(i);
389 extensions_.push_back(prefs_.AddExtension(name));
391 EXPECT_EQ(NULL,
392 prefs()->GetInstalledExtensionInfo(not_installed_id_).get());
394 ExtensionList::const_iterator iter;
395 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
396 std::string id = (*iter)->id();
397 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
398 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
399 if (external_id_.empty()) {
400 external_id_ = id;
401 continue;
403 if (blacklisted_id_.empty()) {
404 blacklisted_id_ = id;
405 continue;
408 // For each type of acknowledgment, acknowledge one installed and one
409 // not-installed extension id.
410 prefs()->AcknowledgeExternalExtension(external_id_);
411 prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_);
412 prefs()->AcknowledgeExternalExtension(not_installed_id_);
413 prefs()->AcknowledgeBlacklistedExtension(not_installed_id_);
416 void Verify() override {
417 ExtensionList::const_iterator iter;
418 for (iter = extensions_.begin(); iter != extensions_.end(); ++iter) {
419 std::string id = (*iter)->id();
420 if (id == external_id_) {
421 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id));
422 } else {
423 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id));
425 if (id == blacklisted_id_) {
426 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id));
427 } else {
428 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id));
431 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_));
432 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_));
435 private:
436 ExtensionList extensions_;
438 std::string not_installed_id_;
439 std::string external_id_;
440 std::string blacklisted_id_;
442 TEST_F(ExtensionPrefsAcknowledgment, Acknowledgment) {}
444 // Tests the idle install information functions.
445 class ExtensionPrefsDelayedInstallInfo : public ExtensionPrefsTest {
446 public:
447 // Sets idle install information for one test extension.
448 void SetIdleInfo(const std::string& id, int num) {
449 base::DictionaryValue manifest;
450 manifest.SetString(manifest_keys::kName, "test");
451 manifest.SetString(manifest_keys::kVersion, "1." + base::IntToString(num));
452 base::FilePath path =
453 prefs_.extensions_dir().AppendASCII(base::IntToString(num));
454 std::string errors;
455 scoped_refptr<Extension> extension = Extension::Create(
456 path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id, &errors);
457 ASSERT_TRUE(extension.get()) << errors;
458 ASSERT_EQ(id, extension->id());
459 prefs()->SetDelayedInstallInfo(extension.get(),
460 Extension::ENABLED,
461 kInstallFlagNone,
462 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
463 syncer::StringOrdinal(),
464 std::string());
467 // Verifies that we get back expected idle install information previously
468 // set by SetIdleInfo.
469 void VerifyIdleInfo(const std::string& id, int num) {
470 scoped_ptr<ExtensionInfo> info(prefs()->GetDelayedInstallInfo(id));
471 ASSERT_TRUE(info);
472 std::string version;
473 ASSERT_TRUE(info->extension_manifest->GetString("version", &version));
474 ASSERT_EQ("1." + base::IntToString(num), version);
475 ASSERT_EQ(base::IntToString(num),
476 info->extension_path.BaseName().MaybeAsASCII());
479 bool HasInfoForId(ExtensionPrefs::ExtensionsInfo* info,
480 const std::string& id) {
481 for (size_t i = 0; i < info->size(); ++i) {
482 if (info->at(i)->extension_id == id)
483 return true;
485 return false;
488 void Initialize() override {
489 PathService::Get(chrome::DIR_TEST_DATA, &basedir_);
490 now_ = Time::Now();
491 id1_ = prefs_.AddExtensionAndReturnId("1");
492 id2_ = prefs_.AddExtensionAndReturnId("2");
493 id3_ = prefs_.AddExtensionAndReturnId("3");
494 id4_ = prefs_.AddExtensionAndReturnId("4");
496 // Set info for two extensions, then remove it.
497 SetIdleInfo(id1_, 1);
498 SetIdleInfo(id2_, 2);
499 VerifyIdleInfo(id1_, 1);
500 VerifyIdleInfo(id2_, 2);
501 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
502 prefs()->GetAllDelayedInstallInfo());
503 EXPECT_EQ(2u, info->size());
504 EXPECT_TRUE(HasInfoForId(info.get(), id1_));
505 EXPECT_TRUE(HasInfoForId(info.get(), id2_));
506 prefs()->RemoveDelayedInstallInfo(id1_);
507 prefs()->RemoveDelayedInstallInfo(id2_);
508 info = prefs()->GetAllDelayedInstallInfo();
509 EXPECT_TRUE(info->empty());
511 // Try getting/removing info for an id that used to have info set.
512 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_));
513 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_));
515 // Try getting/removing info for an id that has not yet had any info set.
516 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
517 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_));
519 // Set info for 4 extensions, then remove for one of them.
520 SetIdleInfo(id1_, 1);
521 SetIdleInfo(id2_, 2);
522 SetIdleInfo(id3_, 3);
523 SetIdleInfo(id4_, 4);
524 VerifyIdleInfo(id1_, 1);
525 VerifyIdleInfo(id2_, 2);
526 VerifyIdleInfo(id3_, 3);
527 VerifyIdleInfo(id4_, 4);
528 prefs()->RemoveDelayedInstallInfo(id3_);
531 void Verify() override {
532 // Make sure the info for the 3 extensions we expect is present.
533 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
534 prefs()->GetAllDelayedInstallInfo());
535 EXPECT_EQ(3u, info->size());
536 EXPECT_TRUE(HasInfoForId(info.get(), id1_));
537 EXPECT_TRUE(HasInfoForId(info.get(), id2_));
538 EXPECT_TRUE(HasInfoForId(info.get(), id4_));
539 VerifyIdleInfo(id1_, 1);
540 VerifyIdleInfo(id2_, 2);
541 VerifyIdleInfo(id4_, 4);
543 // Make sure there isn't info the for the one extension id we removed.
544 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_));
547 protected:
548 Time now_;
549 base::FilePath basedir_;
550 std::string id1_;
551 std::string id2_;
552 std::string id3_;
553 std::string id4_;
555 TEST_F(ExtensionPrefsDelayedInstallInfo, DelayedInstallInfo) {}
557 // Tests the FinishDelayedInstallInfo function.
558 class ExtensionPrefsFinishDelayedInstallInfo : public ExtensionPrefsTest {
559 public:
560 void Initialize() override {
561 base::DictionaryValue dictionary;
562 dictionary.SetString(manifest_keys::kName, "test");
563 dictionary.SetString(manifest_keys::kVersion, "0.1");
564 dictionary.SetString(manifest_keys::kBackgroundPage, "background.html");
565 scoped_refptr<Extension> extension =
566 prefs_.AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
567 id_ = extension->id();
570 // Set idle info
571 base::DictionaryValue manifest;
572 manifest.SetString(manifest_keys::kName, "test");
573 manifest.SetString(manifest_keys::kVersion, "0.2");
574 scoped_ptr<base::ListValue> scripts(new base::ListValue);
575 scripts->AppendString("test.js");
576 manifest.Set(manifest_keys::kBackgroundScripts, scripts.release());
577 base::FilePath path =
578 prefs_.extensions_dir().AppendASCII("test_0.2");
579 std::string errors;
580 scoped_refptr<Extension> new_extension = Extension::Create(
581 path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id_, &errors);
582 ASSERT_TRUE(new_extension.get()) << errors;
583 ASSERT_EQ(id_, new_extension->id());
584 prefs()->SetDelayedInstallInfo(new_extension.get(),
585 Extension::ENABLED,
586 kInstallFlagNone,
587 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE,
588 syncer::StringOrdinal(),
589 "Param");
591 // Finish idle installation
592 ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_));
595 void Verify() override {
596 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_));
597 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(id_));
599 const base::DictionaryValue* manifest;
600 ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_, "manifest", &manifest));
601 ASSERT_TRUE(manifest);
602 std::string value;
603 EXPECT_TRUE(manifest->GetString(manifest_keys::kName, &value));
604 EXPECT_EQ("test", value);
605 EXPECT_TRUE(manifest->GetString(manifest_keys::kVersion, &value));
606 EXPECT_EQ("0.2", value);
607 EXPECT_FALSE(manifest->GetString(manifest_keys::kBackgroundPage, &value));
608 const base::ListValue* scripts;
609 ASSERT_TRUE(manifest->GetList(manifest_keys::kBackgroundScripts, &scripts));
610 EXPECT_EQ(1u, scripts->GetSize());
613 protected:
614 std::string id_;
616 TEST_F(ExtensionPrefsFinishDelayedInstallInfo, FinishDelayedInstallInfo) {}
618 class ExtensionPrefsOnExtensionInstalled : public ExtensionPrefsTest {
619 public:
620 void Initialize() override {
621 extension_ = prefs_.AddExtension("on_extension_installed");
622 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_->id()));
623 prefs()->OnExtensionInstalled(extension_.get(),
624 Extension::DISABLED,
625 syncer::StringOrdinal(),
626 "Param");
629 void Verify() override {
630 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_->id()));
631 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(extension_->id()));
634 private:
635 scoped_refptr<Extension> extension_;
637 TEST_F(ExtensionPrefsOnExtensionInstalled,
638 ExtensionPrefsOnExtensionInstalled) {}
640 class ExtensionPrefsAppDraggedByUser : public ExtensionPrefsTest {
641 public:
642 void Initialize() override {
643 extension_ = prefs_.AddExtension("on_extension_installed");
644 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_->id()));
645 prefs()->OnExtensionInstalled(extension_.get(),
646 Extension::ENABLED,
647 syncer::StringOrdinal(),
648 std::string());
651 void Verify() override {
652 // Set the flag and see if it persisted.
653 prefs()->SetAppDraggedByUser(extension_->id());
654 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
656 // Make sure it doesn't change on consecutive calls.
657 prefs()->SetAppDraggedByUser(extension_->id());
658 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_->id()));
661 private:
662 scoped_refptr<Extension> extension_;
664 TEST_F(ExtensionPrefsAppDraggedByUser, ExtensionPrefsAppDraggedByUser) {}
666 class ExtensionPrefsFlags : public ExtensionPrefsTest {
667 public:
668 void Initialize() override {
670 base::DictionaryValue dictionary;
671 dictionary.SetString(manifest_keys::kName, "from_webstore");
672 dictionary.SetString(manifest_keys::kVersion, "0.1");
673 webstore_extension_ = prefs_.AddExtensionWithManifestAndFlags(
674 dictionary, Manifest::INTERNAL, Extension::FROM_WEBSTORE);
678 base::DictionaryValue dictionary;
679 dictionary.SetString(manifest_keys::kName, "from_bookmark");
680 dictionary.SetString(manifest_keys::kVersion, "0.1");
681 bookmark_extension_ = prefs_.AddExtensionWithManifestAndFlags(
682 dictionary, Manifest::INTERNAL, Extension::FROM_BOOKMARK);
686 base::DictionaryValue dictionary;
687 dictionary.SetString(manifest_keys::kName, "was_installed_by_default");
688 dictionary.SetString(manifest_keys::kVersion, "0.1");
689 default_extension_ = prefs_.AddExtensionWithManifestAndFlags(
690 dictionary,
691 Manifest::INTERNAL,
692 Extension::WAS_INSTALLED_BY_DEFAULT);
696 base::DictionaryValue dictionary;
697 dictionary.SetString(manifest_keys::kName, "was_installed_by_oem");
698 dictionary.SetString(manifest_keys::kVersion, "0.1");
699 oem_extension_ = prefs_.AddExtensionWithManifestAndFlags(
700 dictionary, Manifest::INTERNAL, Extension::WAS_INSTALLED_BY_OEM);
704 void Verify() override {
705 EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_->id()));
706 EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_->id()));
708 EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_->id()));
709 EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_->id()));
711 EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_->id()));
712 EXPECT_TRUE(prefs()->WasInstalledByOem(oem_extension_->id()));
715 private:
716 scoped_refptr<Extension> webstore_extension_;
717 scoped_refptr<Extension> bookmark_extension_;
718 scoped_refptr<Extension> default_extension_;
719 scoped_refptr<Extension> oem_extension_;
721 TEST_F(ExtensionPrefsFlags, ExtensionPrefsFlags) {}
723 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
724 : ExtensionPrefsTest() {
725 base::DictionaryValue simple_dict;
726 std::string error;
728 simple_dict.SetString(manifest_keys::kVersion, "1.0.0.0");
729 simple_dict.SetString(manifest_keys::kName, "unused");
731 extension1_ = Extension::Create(
732 prefs_.temp_dir().AppendASCII("ext1_"),
733 Manifest::EXTERNAL_PREF,
734 simple_dict,
735 Extension::NO_FLAGS,
736 &error);
737 extension2_ = Extension::Create(
738 prefs_.temp_dir().AppendASCII("ext2_"),
739 Manifest::EXTERNAL_PREF,
740 simple_dict,
741 Extension::NO_FLAGS,
742 &error);
743 extension3_ = Extension::Create(
744 prefs_.temp_dir().AppendASCII("ext3_"),
745 Manifest::EXTERNAL_PREF,
746 simple_dict,
747 Extension::NO_FLAGS,
748 &error);
749 extension4_ = Extension::Create(
750 prefs_.temp_dir().AppendASCII("ext4_"),
751 Manifest::EXTERNAL_PREF,
752 simple_dict,
753 Extension::NO_FLAGS,
754 &error);
756 for (size_t i = 0; i < kNumInstalledExtensions; ++i)
757 installed_[i] = false;
760 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
763 // Tests that blacklist state can be queried.
764 class ExtensionPrefsBlacklistedExtensions : public ExtensionPrefsTest {
765 public:
766 ~ExtensionPrefsBlacklistedExtensions() override {}
768 void Initialize() override {
769 extension_a_ = prefs_.AddExtension("a");
770 extension_b_ = prefs_.AddExtension("b");
771 extension_c_ = prefs_.AddExtension("c");
774 void Verify() override {
776 ExtensionIdSet ids;
777 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
779 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
781 ExtensionIdSet ids;
782 ids.insert(extension_a_->id());
783 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
785 prefs()->SetExtensionBlacklisted(extension_b_->id(), true);
786 prefs()->SetExtensionBlacklisted(extension_c_->id(), true);
788 ExtensionIdSet ids;
789 ids.insert(extension_a_->id());
790 ids.insert(extension_b_->id());
791 ids.insert(extension_c_->id());
792 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
794 prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
796 ExtensionIdSet ids;
797 ids.insert(extension_b_->id());
798 ids.insert(extension_c_->id());
799 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
801 prefs()->SetExtensionBlacklisted(extension_b_->id(), false);
802 prefs()->SetExtensionBlacklisted(extension_c_->id(), false);
804 ExtensionIdSet ids;
805 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
808 // The interesting part: make sure that we're cleaning up after ourselves
809 // when we're storing *just* the fact that the extension is blacklisted.
810 std::string arbitrary_id = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
812 prefs()->SetExtensionBlacklisted(arbitrary_id, true);
813 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
815 // (And make sure that the acknowledged bit is also cleared).
816 prefs()->AcknowledgeBlacklistedExtension(arbitrary_id);
818 EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id));
820 ExtensionIdSet ids;
821 ids.insert(arbitrary_id);
822 ids.insert(extension_a_->id());
823 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
825 prefs()->SetExtensionBlacklisted(arbitrary_id, false);
826 prefs()->SetExtensionBlacklisted(extension_a_->id(), false);
827 EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id));
829 ExtensionIdSet ids;
830 EXPECT_EQ(ids, prefs()->GetBlacklistedExtensions());
834 private:
835 scoped_refptr<const Extension> extension_a_;
836 scoped_refptr<const Extension> extension_b_;
837 scoped_refptr<const Extension> extension_c_;
839 TEST_F(ExtensionPrefsBlacklistedExtensions,
840 ExtensionPrefsBlacklistedExtensions) {}
842 // Tests the blacklist state. Old "blacklist" preference should take precedence
843 // over new "blacklist_state".
844 class ExtensionPrefsBlacklistState : public ExtensionPrefsTest {
845 public:
846 ~ExtensionPrefsBlacklistState() override {}
848 void Initialize() override { extension_a_ = prefs_.AddExtension("a"); }
850 void Verify() override {
851 ExtensionIdSet empty_ids;
852 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
854 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
855 EXPECT_EQ(BLACKLISTED_MALWARE,
856 prefs()->GetExtensionBlacklistState(extension_a_->id()));
858 prefs()->SetExtensionBlacklistState(extension_a_->id(),
859 BLACKLISTED_POTENTIALLY_UNWANTED);
860 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED,
861 prefs()->GetExtensionBlacklistState(extension_a_->id()));
862 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
863 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
865 prefs()->SetExtensionBlacklisted(extension_a_->id(), true);
866 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
867 EXPECT_EQ(BLACKLISTED_MALWARE,
868 prefs()->GetExtensionBlacklistState(extension_a_->id()));
869 EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
871 prefs()->SetExtensionBlacklistState(extension_a_->id(),
872 NOT_BLACKLISTED);
873 EXPECT_EQ(NOT_BLACKLISTED,
874 prefs()->GetExtensionBlacklistState(extension_a_->id()));
875 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_->id()));
876 EXPECT_EQ(empty_ids, prefs()->GetBlacklistedExtensions());
879 private:
880 scoped_refptr<const Extension> extension_a_;
882 TEST_F(ExtensionPrefsBlacklistState, ExtensionPrefsBlacklistState) {}
884 // Tests clearing the last launched preference.
885 class ExtensionPrefsClearLastLaunched : public ExtensionPrefsTest {
886 public:
887 ~ExtensionPrefsClearLastLaunched() override {}
889 void Initialize() override {
890 extension_a_ = prefs_.AddExtension("a");
891 extension_b_ = prefs_.AddExtension("b");
894 void Verify() override {
895 // Set last launched times for each extension.
896 prefs()->SetLastLaunchTime(extension_a_->id(), base::Time::Now());
897 prefs()->SetLastLaunchTime(extension_b_->id(), base::Time::Now());
899 // Also set some other preference for one of the extensions.
900 prefs()->SetAllowFileAccess(extension_a_->id(), true);
902 // Now clear the launch times.
903 prefs()->ClearLastLaunchTimes();
905 // All launch times should be gone.
906 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_a_->id()));
907 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_b_->id()));
909 // Other preferences should be untouched.
910 EXPECT_TRUE(prefs()->AllowFileAccess(extension_a_->id()));
913 private:
914 scoped_refptr<const Extension> extension_a_;
915 scoped_refptr<const Extension> extension_b_;
917 TEST_F(ExtensionPrefsClearLastLaunched, ExtensionPrefsClearLastLaunched) {}
919 class ExtensionPrefsComponentExtension : public ExtensionPrefsTest {
920 public:
921 ~ExtensionPrefsComponentExtension() override {}
922 void Initialize() override {
923 // Adding a component extension.
924 component_extension_ =
925 ExtensionBuilder()
926 .SetManifest(DictionaryBuilder()
927 .Set(manifest_keys::kName, "a")
928 .Set(manifest_keys::kVersion, "0.1"))
929 .SetLocation(Manifest::COMPONENT)
930 .SetPath(prefs_.extensions_dir().AppendASCII("a"))
931 .Build();
932 prefs_.AddExtension(component_extension_.get());
934 // Adding a non component extension.
935 no_component_extension_ =
936 ExtensionBuilder()
937 .SetManifest(DictionaryBuilder()
938 .Set(manifest_keys::kName, "b")
939 .Set(manifest_keys::kVersion, "0.1"))
940 .SetLocation(Manifest::INTERNAL)
941 .SetPath(prefs_.extensions_dir().AppendASCII("b"))
942 .Build();
943 prefs_.AddExtension(no_component_extension_.get());
945 APIPermissionSet api_perms;
946 api_perms.insert(APIPermission::kTab);
947 api_perms.insert(APIPermission::kBookmark);
948 api_perms.insert(APIPermission::kHistory);
950 ManifestPermissionSet empty_manifest_permissions;
952 URLPatternSet ehosts, shosts;
953 AddPattern(&shosts, "chrome://print/*");
955 active_perms_ = new PermissionSet(api_perms, empty_manifest_permissions,
956 ehosts, shosts);
957 // Set the active permissions.
958 prefs()->SetActivePermissions(component_extension_->id(),
959 active_perms_.get());
960 prefs()->SetActivePermissions(no_component_extension_->id(),
961 active_perms_.get());
964 void Verify() override {
965 // Component extension can access chrome://print/*.
966 scoped_refptr<PermissionSet> component_permissions(
967 prefs()->GetActivePermissions(component_extension_->id()));
968 EXPECT_EQ(1u, component_permissions->scriptable_hosts().size());
970 // Non Component extension can not access chrome://print/*.
971 scoped_refptr<PermissionSet> no_component_permissions(
972 prefs()->GetActivePermissions(no_component_extension_->id()));
973 EXPECT_EQ(0u, no_component_permissions->scriptable_hosts().size());
975 // |URLPattern::SCHEME_CHROMEUI| scheme will be added in valid_schemes for
976 // component extensions.
977 URLPatternSet scriptable_hosts;
978 std::string pref_key = "active_permissions.scriptable_host";
979 int valid_schemes = URLPattern::SCHEME_ALL & ~URLPattern::SCHEME_CHROMEUI;
981 EXPECT_TRUE(prefs()->ReadPrefAsURLPatternSet(component_extension_->id(),
982 pref_key, &scriptable_hosts,
983 valid_schemes));
985 EXPECT_FALSE(prefs()->ReadPrefAsURLPatternSet(no_component_extension_->id(),
986 pref_key, &scriptable_hosts,
987 valid_schemes));
990 private:
991 scoped_refptr<PermissionSet> active_perms_;
992 scoped_refptr<Extension> component_extension_;
993 scoped_refptr<Extension> no_component_extension_;
995 TEST_F(ExtensionPrefsComponentExtension, ExtensionPrefsComponentExtension) {
998 } // namespace extensions