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"
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());
61 void ExtensionPrefsTest::TearDown() {
64 // Reset ExtensionPrefs, and re-verify.
65 prefs_
.ResetPrefRegistry();
66 RegisterPreferences(prefs_
.pref_registry().get());
67 prefs_
.RecreateExtensionPrefs();
69 prefs_
.pref_service()->CommitPendingWrite();
70 message_loop_
.RunUntilIdle();
73 // Tests the LastPingDay/SetLastPingDay functions.
74 class ExtensionPrefsLastPingDay
: public ExtensionPrefsTest
{
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_
);
99 std::string extension_id_
;
101 TEST_F(ExtensionPrefsLastPingDay
, LastPingDay
) {}
103 // Tests the GetToolbarOrder/SetToolbarOrder functions.
104 class ExtensionPrefsToolbarOrder
: public ExtensionPrefsTest
{
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
);
121 ExtensionIdList list_
;
123 TEST_F(ExtensionPrefsToolbarOrder
, ToolbarOrder
) {}
125 // Tests the IsExtensionDisabled/SetExtensionState functions.
126 class ExtensionPrefsExtensionState
: public ExtensionPrefsTest
{
128 void Initialize() override
{
129 extension
= prefs_
.AddExtension("test");
130 prefs()->SetExtensionDisabled(extension
->id(),
131 Extension::DISABLE_USER_ACTION
);
134 void Verify() override
{
135 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension
->id()));
139 scoped_refptr
<Extension
> extension
;
141 TEST_F(ExtensionPrefsExtensionState
, ExtensionState
) {}
143 class ExtensionPrefsEscalatePermissions
: public ExtensionPrefsTest
{
145 void Initialize() override
{
146 extension
= prefs_
.AddExtension("test");
147 prefs()->SetExtensionDisabled(extension
->id(),
148 Extension::DISABLE_PERMISSIONS_INCREASE
);
151 void Verify() override
{
152 EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension
->id()));
156 scoped_refptr
<Extension
> extension
;
158 TEST_F(ExtensionPrefsEscalatePermissions
, EscalatePermissions
) {}
160 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
161 class ExtensionPrefsGrantedPermissions
: public ExtensionPrefsTest
{
163 void Initialize() override
{
164 const APIPermissionInfo
* permission_info
=
165 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket
);
167 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
169 api_perm_set1_
.insert(APIPermission::kTab
);
170 api_perm_set1_
.insert(APIPermission::kBookmark
);
171 scoped_ptr
<APIPermission
> permission(
172 permission_info
->CreateAPIPermission());
174 scoped_ptr
<base::ListValue
> value(new base::ListValue());
175 value
->Append(new base::StringValue("tcp-connect:*.example.com:80"));
176 value
->Append(new base::StringValue("udp-bind::8080"));
177 value
->Append(new base::StringValue("udp-send-to::8888"));
178 ASSERT_TRUE(permission
->FromValue(value
.get(), NULL
, NULL
));
180 api_perm_set1_
.insert(permission
.release());
182 api_perm_set2_
.insert(APIPermission::kHistory
);
184 AddPattern(&ehost_perm_set1_
, "http://*.google.com/*");
185 AddPattern(&ehost_perm_set1_
, "http://example.com/*");
186 AddPattern(&ehost_perm_set1_
, "chrome://favicon/*");
188 AddPattern(&ehost_perm_set2_
, "https://*.google.com/*");
190 AddPattern(&ehost_perm_set2_
, "http://*.google.com/*");
192 AddPattern(&shost_perm_set1_
, "http://reddit.com/r/test/*");
193 AddPattern(&shost_perm_set2_
, "http://reddit.com/r/test/*");
194 AddPattern(&shost_perm_set2_
, "http://somesite.com/*");
195 AddPattern(&shost_perm_set2_
, "http://example.com/*");
197 APIPermissionSet expected_apis
= api_perm_set1_
;
199 AddPattern(&ehost_permissions_
, "http://*.google.com/*");
200 AddPattern(&ehost_permissions_
, "http://example.com/*");
201 AddPattern(&ehost_permissions_
, "chrome://favicon/*");
202 AddPattern(&ehost_permissions_
, "https://*.google.com/*");
204 AddPattern(&shost_permissions_
, "http://reddit.com/r/test/*");
205 AddPattern(&shost_permissions_
, "http://somesite.com/*");
206 AddPattern(&shost_permissions_
, "http://example.com/*");
208 APIPermissionSet empty_set
;
209 ManifestPermissionSet empty_manifest_permissions
;
210 URLPatternSet empty_extent
;
211 scoped_refptr
<PermissionSet
> permissions
;
212 scoped_refptr
<PermissionSet
> granted_permissions
;
214 // Make sure both granted api and host permissions start empty.
215 granted_permissions
=
216 prefs()->GetGrantedPermissions(extension_id_
);
217 EXPECT_TRUE(granted_permissions
->IsEmpty());
219 permissions
= new PermissionSet(
220 api_perm_set1_
, empty_manifest_permissions
, empty_extent
, empty_extent
);
222 // Add part of the api permissions.
223 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
224 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
225 EXPECT_TRUE(granted_permissions
.get());
226 EXPECT_FALSE(granted_permissions
->IsEmpty());
227 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
228 EXPECT_TRUE(granted_permissions
->effective_hosts().is_empty());
229 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
230 granted_permissions
= NULL
;
232 // Add part of the explicit host permissions.
233 permissions
= new PermissionSet(
234 empty_set
, empty_manifest_permissions
, ehost_perm_set1_
, empty_extent
);
235 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
236 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
237 EXPECT_FALSE(granted_permissions
->IsEmpty());
238 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
239 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
240 EXPECT_EQ(ehost_perm_set1_
,
241 granted_permissions
->explicit_hosts());
242 EXPECT_EQ(ehost_perm_set1_
,
243 granted_permissions
->effective_hosts());
245 // Add part of the scriptable host permissions.
246 permissions
= new PermissionSet(
247 empty_set
, empty_manifest_permissions
, empty_extent
, shost_perm_set1_
);
248 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
249 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
250 EXPECT_FALSE(granted_permissions
->IsEmpty());
251 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
252 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
253 EXPECT_EQ(ehost_perm_set1_
,
254 granted_permissions
->explicit_hosts());
255 EXPECT_EQ(shost_perm_set1_
,
256 granted_permissions
->scriptable_hosts());
258 URLPatternSet::CreateUnion(ehost_perm_set1_
, shost_perm_set1_
,
259 &effective_permissions_
);
260 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
262 // Add the rest of the permissions.
263 permissions
= new PermissionSet(
264 api_perm_set2_
, empty_manifest_permissions
,
265 ehost_perm_set2_
, shost_perm_set2_
);
267 APIPermissionSet::Union(expected_apis
, api_perm_set2_
, &api_permissions_
);
269 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
270 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
271 EXPECT_TRUE(granted_permissions
.get());
272 EXPECT_FALSE(granted_permissions
->IsEmpty());
273 EXPECT_EQ(api_permissions_
, granted_permissions
->apis());
274 EXPECT_EQ(ehost_permissions_
,
275 granted_permissions
->explicit_hosts());
276 EXPECT_EQ(shost_permissions_
,
277 granted_permissions
->scriptable_hosts());
278 effective_permissions_
.ClearPatterns();
279 URLPatternSet::CreateUnion(ehost_permissions_
, shost_permissions_
,
280 &effective_permissions_
);
281 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
284 void Verify() override
{
285 scoped_refptr
<PermissionSet
> permissions(
286 prefs()->GetGrantedPermissions(extension_id_
));
287 EXPECT_TRUE(permissions
.get());
288 EXPECT_FALSE(permissions
->HasEffectiveFullAccess());
289 EXPECT_EQ(api_permissions_
, permissions
->apis());
290 EXPECT_EQ(ehost_permissions_
,
291 permissions
->explicit_hosts());
292 EXPECT_EQ(shost_permissions_
,
293 permissions
->scriptable_hosts());
297 std::string extension_id_
;
298 APIPermissionSet api_perm_set1_
;
299 APIPermissionSet api_perm_set2_
;
300 URLPatternSet ehost_perm_set1_
;
301 URLPatternSet ehost_perm_set2_
;
302 URLPatternSet shost_perm_set1_
;
303 URLPatternSet shost_perm_set2_
;
305 APIPermissionSet api_permissions_
;
306 URLPatternSet ehost_permissions_
;
307 URLPatternSet shost_permissions_
;
308 URLPatternSet effective_permissions_
;
310 TEST_F(ExtensionPrefsGrantedPermissions
, GrantedPermissions
) {}
312 // Tests the SetActivePermissions / GetActivePermissions functions.
313 class ExtensionPrefsActivePermissions
: public ExtensionPrefsTest
{
315 void Initialize() override
{
316 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
318 APIPermissionSet api_perms
;
319 api_perms
.insert(APIPermission::kTab
);
320 api_perms
.insert(APIPermission::kBookmark
);
321 api_perms
.insert(APIPermission::kHistory
);
323 ManifestPermissionSet empty_manifest_permissions
;
325 URLPatternSet ehosts
;
326 AddPattern(&ehosts
, "http://*.google.com/*");
327 AddPattern(&ehosts
, "http://example.com/*");
328 AddPattern(&ehosts
, "chrome://favicon/*");
330 URLPatternSet shosts
;
331 AddPattern(&shosts
, "https://*.google.com/*");
332 AddPattern(&shosts
, "http://reddit.com/r/test/*");
334 active_perms_
= new PermissionSet(
335 api_perms
, empty_manifest_permissions
, ehosts
, shosts
);
337 // Make sure the active permissions start empty.
338 scoped_refptr
<PermissionSet
> active(
339 prefs()->GetActivePermissions(extension_id_
));
340 EXPECT_TRUE(active
->IsEmpty());
342 // Set the active permissions.
343 prefs()->SetActivePermissions(extension_id_
, active_perms_
.get());
344 active
= prefs()->GetActivePermissions(extension_id_
);
345 EXPECT_EQ(active_perms_
->apis(), active
->apis());
346 EXPECT_EQ(active_perms_
->explicit_hosts(), active
->explicit_hosts());
347 EXPECT_EQ(active_perms_
->scriptable_hosts(), active
->scriptable_hosts());
348 EXPECT_EQ(*active_perms_
.get(), *active
.get());
351 void Verify() override
{
352 scoped_refptr
<PermissionSet
> permissions(
353 prefs()->GetActivePermissions(extension_id_
));
354 EXPECT_EQ(*active_perms_
.get(), *permissions
.get());
358 std::string extension_id_
;
359 scoped_refptr
<PermissionSet
> active_perms_
;
361 TEST_F(ExtensionPrefsActivePermissions
, SetAndGetActivePermissions
) {}
363 // Tests the GetVersionString function.
364 class ExtensionPrefsVersionString
: public ExtensionPrefsTest
{
366 void Initialize() override
{
367 extension
= prefs_
.AddExtension("test");
368 EXPECT_EQ("0.1", prefs()->GetVersionString(extension
->id()));
369 prefs()->OnExtensionUninstalled(extension
->id(),
370 Manifest::INTERNAL
, false);
373 void Verify() override
{
374 EXPECT_EQ("", prefs()->GetVersionString(extension
->id()));
378 scoped_refptr
<Extension
> extension
;
380 TEST_F(ExtensionPrefsVersionString
, VersionString
) {}
382 class ExtensionPrefsAcknowledgment
: public ExtensionPrefsTest
{
384 void Initialize() override
{
385 not_installed_id_
= "pghjnghklobnfoidcldiidjjjhkeeaoi";
387 // Install some extensions.
388 for (int i
= 0; i
< 5; i
++) {
389 std::string name
= "test" + base::IntToString(i
);
390 extensions_
.push_back(prefs_
.AddExtension(name
));
393 prefs()->GetInstalledExtensionInfo(not_installed_id_
).get());
395 ExtensionList::const_iterator iter
;
396 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
397 std::string id
= (*iter
)->id();
398 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
399 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
400 if (external_id_
.empty()) {
404 if (blacklisted_id_
.empty()) {
405 blacklisted_id_
= id
;
409 // For each type of acknowledgment, acknowledge one installed and one
410 // not-installed extension id.
411 prefs()->AcknowledgeExternalExtension(external_id_
);
412 prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_
);
413 prefs()->AcknowledgeExternalExtension(not_installed_id_
);
414 prefs()->AcknowledgeBlacklistedExtension(not_installed_id_
);
417 void Verify() override
{
418 ExtensionList::const_iterator iter
;
419 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
420 std::string id
= (*iter
)->id();
421 if (id
== external_id_
) {
422 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id
));
424 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
426 if (id
== blacklisted_id_
) {
427 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
429 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
432 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_
));
433 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_
));
437 ExtensionList extensions_
;
439 std::string not_installed_id_
;
440 std::string external_id_
;
441 std::string blacklisted_id_
;
443 TEST_F(ExtensionPrefsAcknowledgment
, Acknowledgment
) {}
445 // Tests the idle install information functions.
446 class ExtensionPrefsDelayedInstallInfo
: public ExtensionPrefsTest
{
448 // Sets idle install information for one test extension.
449 void SetIdleInfo(const std::string
& id
, int num
) {
450 base::DictionaryValue manifest
;
451 manifest
.SetString(manifest_keys::kName
, "test");
452 manifest
.SetString(manifest_keys::kVersion
, "1." + base::IntToString(num
));
453 base::FilePath path
=
454 prefs_
.extensions_dir().AppendASCII(base::IntToString(num
));
456 scoped_refptr
<Extension
> extension
= Extension::Create(
457 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id
, &errors
);
458 ASSERT_TRUE(extension
.get()) << errors
;
459 ASSERT_EQ(id
, extension
->id());
460 prefs()->SetDelayedInstallInfo(extension
.get(),
463 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
464 syncer::StringOrdinal(),
468 // Verifies that we get back expected idle install information previously
469 // set by SetIdleInfo.
470 void VerifyIdleInfo(const std::string
& id
, int num
) {
471 scoped_ptr
<ExtensionInfo
> info(prefs()->GetDelayedInstallInfo(id
));
474 ASSERT_TRUE(info
->extension_manifest
->GetString("version", &version
));
475 ASSERT_EQ("1." + base::IntToString(num
), version
);
476 ASSERT_EQ(base::IntToString(num
),
477 info
->extension_path
.BaseName().MaybeAsASCII());
480 bool HasInfoForId(ExtensionPrefs::ExtensionsInfo
* info
,
481 const std::string
& id
) {
482 for (size_t i
= 0; i
< info
->size(); ++i
) {
483 if (info
->at(i
)->extension_id
== id
)
489 void Initialize() override
{
490 PathService::Get(chrome::DIR_TEST_DATA
, &basedir_
);
492 id1_
= prefs_
.AddExtensionAndReturnId("1");
493 id2_
= prefs_
.AddExtensionAndReturnId("2");
494 id3_
= prefs_
.AddExtensionAndReturnId("3");
495 id4_
= prefs_
.AddExtensionAndReturnId("4");
497 // Set info for two extensions, then remove it.
498 SetIdleInfo(id1_
, 1);
499 SetIdleInfo(id2_
, 2);
500 VerifyIdleInfo(id1_
, 1);
501 VerifyIdleInfo(id2_
, 2);
502 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
503 prefs()->GetAllDelayedInstallInfo());
504 EXPECT_EQ(2u, info
->size());
505 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
506 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
507 prefs()->RemoveDelayedInstallInfo(id1_
);
508 prefs()->RemoveDelayedInstallInfo(id2_
);
509 info
= prefs()->GetAllDelayedInstallInfo();
510 EXPECT_TRUE(info
->empty());
512 // Try getting/removing info for an id that used to have info set.
513 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_
));
514 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_
));
516 // Try getting/removing info for an id that has not yet had any info set.
517 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
518 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_
));
520 // Set info for 4 extensions, then remove for one of them.
521 SetIdleInfo(id1_
, 1);
522 SetIdleInfo(id2_
, 2);
523 SetIdleInfo(id3_
, 3);
524 SetIdleInfo(id4_
, 4);
525 VerifyIdleInfo(id1_
, 1);
526 VerifyIdleInfo(id2_
, 2);
527 VerifyIdleInfo(id3_
, 3);
528 VerifyIdleInfo(id4_
, 4);
529 prefs()->RemoveDelayedInstallInfo(id3_
);
532 void Verify() override
{
533 // Make sure the info for the 3 extensions we expect is present.
534 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
535 prefs()->GetAllDelayedInstallInfo());
536 EXPECT_EQ(3u, info
->size());
537 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
538 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
539 EXPECT_TRUE(HasInfoForId(info
.get(), id4_
));
540 VerifyIdleInfo(id1_
, 1);
541 VerifyIdleInfo(id2_
, 2);
542 VerifyIdleInfo(id4_
, 4);
544 // Make sure there isn't info the for the one extension id we removed.
545 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
550 base::FilePath basedir_
;
556 TEST_F(ExtensionPrefsDelayedInstallInfo
, DelayedInstallInfo
) {}
558 // Tests the FinishDelayedInstallInfo function.
559 class ExtensionPrefsFinishDelayedInstallInfo
: public ExtensionPrefsTest
{
561 void Initialize() override
{
562 base::DictionaryValue dictionary
;
563 dictionary
.SetString(manifest_keys::kName
, "test");
564 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
565 dictionary
.SetString(manifest_keys::kBackgroundPage
, "background.html");
566 scoped_refptr
<Extension
> extension
=
567 prefs_
.AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
568 id_
= extension
->id();
572 base::DictionaryValue manifest
;
573 manifest
.SetString(manifest_keys::kName
, "test");
574 manifest
.SetString(manifest_keys::kVersion
, "0.2");
575 scoped_ptr
<base::ListValue
> scripts(new base::ListValue
);
576 scripts
->AppendString("test.js");
577 manifest
.Set(manifest_keys::kBackgroundScripts
, scripts
.release());
578 base::FilePath path
=
579 prefs_
.extensions_dir().AppendASCII("test_0.2");
581 scoped_refptr
<Extension
> new_extension
= Extension::Create(
582 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id_
, &errors
);
583 ASSERT_TRUE(new_extension
.get()) << errors
;
584 ASSERT_EQ(id_
, new_extension
->id());
585 prefs()->SetDelayedInstallInfo(new_extension
.get(),
588 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
589 syncer::StringOrdinal(),
592 // Finish idle installation
593 ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_
));
596 void Verify() override
{
597 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_
));
598 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(id_
));
600 const base::DictionaryValue
* manifest
;
601 ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_
, "manifest", &manifest
));
602 ASSERT_TRUE(manifest
);
604 EXPECT_TRUE(manifest
->GetString(manifest_keys::kName
, &value
));
605 EXPECT_EQ("test", value
);
606 EXPECT_TRUE(manifest
->GetString(manifest_keys::kVersion
, &value
));
607 EXPECT_EQ("0.2", value
);
608 EXPECT_FALSE(manifest
->GetString(manifest_keys::kBackgroundPage
, &value
));
609 const base::ListValue
* scripts
;
610 ASSERT_TRUE(manifest
->GetList(manifest_keys::kBackgroundScripts
, &scripts
));
611 EXPECT_EQ(1u, scripts
->GetSize());
617 TEST_F(ExtensionPrefsFinishDelayedInstallInfo
, FinishDelayedInstallInfo
) {}
619 class ExtensionPrefsOnExtensionInstalled
: public ExtensionPrefsTest
{
621 void Initialize() override
{
622 extension_
= prefs_
.AddExtension("on_extension_installed");
623 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_
->id()));
624 prefs()->OnExtensionInstalled(extension_
.get(),
626 syncer::StringOrdinal(),
630 void Verify() override
{
631 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_
->id()));
632 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(extension_
->id()));
636 scoped_refptr
<Extension
> extension_
;
638 TEST_F(ExtensionPrefsOnExtensionInstalled
,
639 ExtensionPrefsOnExtensionInstalled
) {}
641 class ExtensionPrefsAppDraggedByUser
: public ExtensionPrefsTest
{
643 void Initialize() override
{
644 extension_
= prefs_
.AddExtension("on_extension_installed");
645 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_
->id()));
646 prefs()->OnExtensionInstalled(extension_
.get(),
648 syncer::StringOrdinal(),
652 void Verify() override
{
653 // Set the flag and see if it persisted.
654 prefs()->SetAppDraggedByUser(extension_
->id());
655 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
657 // Make sure it doesn't change on consecutive calls.
658 prefs()->SetAppDraggedByUser(extension_
->id());
659 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
663 scoped_refptr
<Extension
> extension_
;
665 TEST_F(ExtensionPrefsAppDraggedByUser
, ExtensionPrefsAppDraggedByUser
) {}
667 class ExtensionPrefsFlags
: public ExtensionPrefsTest
{
669 void Initialize() override
{
671 base::DictionaryValue dictionary
;
672 dictionary
.SetString(manifest_keys::kName
, "from_webstore");
673 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
674 webstore_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
675 dictionary
, Manifest::INTERNAL
, Extension::FROM_WEBSTORE
);
679 base::DictionaryValue dictionary
;
680 dictionary
.SetString(manifest_keys::kName
, "from_bookmark");
681 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
682 bookmark_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
683 dictionary
, Manifest::INTERNAL
, Extension::FROM_BOOKMARK
);
687 base::DictionaryValue dictionary
;
688 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_default");
689 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
690 default_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
693 Extension::WAS_INSTALLED_BY_DEFAULT
);
697 base::DictionaryValue dictionary
;
698 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_oem");
699 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
700 oem_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
701 dictionary
, Manifest::INTERNAL
, Extension::WAS_INSTALLED_BY_OEM
);
705 void Verify() override
{
706 EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_
->id()));
707 EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_
->id()));
709 EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_
->id()));
710 EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_
->id()));
712 EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_
->id()));
713 EXPECT_TRUE(prefs()->WasInstalledByOem(oem_extension_
->id()));
717 scoped_refptr
<Extension
> webstore_extension_
;
718 scoped_refptr
<Extension
> bookmark_extension_
;
719 scoped_refptr
<Extension
> default_extension_
;
720 scoped_refptr
<Extension
> oem_extension_
;
722 TEST_F(ExtensionPrefsFlags
, ExtensionPrefsFlags
) {}
724 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
725 : ExtensionPrefsTest() {
726 base::DictionaryValue simple_dict
;
729 simple_dict
.SetString(manifest_keys::kVersion
, "1.0.0.0");
730 simple_dict
.SetString(manifest_keys::kName
, "unused");
732 extension1_
= Extension::Create(
733 prefs_
.temp_dir().AppendASCII("ext1_"),
734 Manifest::EXTERNAL_PREF
,
738 extension2_
= Extension::Create(
739 prefs_
.temp_dir().AppendASCII("ext2_"),
740 Manifest::EXTERNAL_PREF
,
744 extension3_
= Extension::Create(
745 prefs_
.temp_dir().AppendASCII("ext3_"),
746 Manifest::EXTERNAL_PREF
,
750 extension4_
= Extension::Create(
751 prefs_
.temp_dir().AppendASCII("ext4_"),
752 Manifest::EXTERNAL_PREF
,
757 for (size_t i
= 0; i
< kNumInstalledExtensions
; ++i
)
758 installed_
[i
] = false;
761 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
764 // Tests that blacklist state can be queried.
765 class ExtensionPrefsBlacklistedExtensions
: public ExtensionPrefsTest
{
767 ~ExtensionPrefsBlacklistedExtensions() override
{}
769 void Initialize() override
{
770 extension_a_
= prefs_
.AddExtension("a");
771 extension_b_
= prefs_
.AddExtension("b");
772 extension_c_
= prefs_
.AddExtension("c");
775 void Verify() override
{
778 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
780 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
783 ids
.insert(extension_a_
->id());
784 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
786 prefs()->SetExtensionBlacklisted(extension_b_
->id(), true);
787 prefs()->SetExtensionBlacklisted(extension_c_
->id(), true);
790 ids
.insert(extension_a_
->id());
791 ids
.insert(extension_b_
->id());
792 ids
.insert(extension_c_
->id());
793 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
795 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
798 ids
.insert(extension_b_
->id());
799 ids
.insert(extension_c_
->id());
800 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
802 prefs()->SetExtensionBlacklisted(extension_b_
->id(), false);
803 prefs()->SetExtensionBlacklisted(extension_c_
->id(), false);
806 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
809 // The interesting part: make sure that we're cleaning up after ourselves
810 // when we're storing *just* the fact that the extension is blacklisted.
811 std::string arbitrary_id
= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
813 prefs()->SetExtensionBlacklisted(arbitrary_id
, true);
814 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
816 // (And make sure that the acknowledged bit is also cleared).
817 prefs()->AcknowledgeBlacklistedExtension(arbitrary_id
);
819 EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id
));
822 ids
.insert(arbitrary_id
);
823 ids
.insert(extension_a_
->id());
824 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
826 prefs()->SetExtensionBlacklisted(arbitrary_id
, false);
827 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
828 EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id
));
831 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
836 scoped_refptr
<const Extension
> extension_a_
;
837 scoped_refptr
<const Extension
> extension_b_
;
838 scoped_refptr
<const Extension
> extension_c_
;
840 TEST_F(ExtensionPrefsBlacklistedExtensions
,
841 ExtensionPrefsBlacklistedExtensions
) {}
843 // Tests the blacklist state. Old "blacklist" preference should take precedence
844 // over new "blacklist_state".
845 class ExtensionPrefsBlacklistState
: public ExtensionPrefsTest
{
847 ~ExtensionPrefsBlacklistState() override
{}
849 void Initialize() override
{ extension_a_
= prefs_
.AddExtension("a"); }
851 void Verify() override
{
852 ExtensionIdSet empty_ids
;
853 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
855 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
856 EXPECT_EQ(BLACKLISTED_MALWARE
,
857 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
859 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
860 BLACKLISTED_POTENTIALLY_UNWANTED
);
861 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED
,
862 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
863 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
864 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
866 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
867 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
868 EXPECT_EQ(BLACKLISTED_MALWARE
,
869 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
870 EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
872 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
874 EXPECT_EQ(NOT_BLACKLISTED
,
875 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
876 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
877 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
881 scoped_refptr
<const Extension
> extension_a_
;
883 TEST_F(ExtensionPrefsBlacklistState
, ExtensionPrefsBlacklistState
) {}
885 // Tests clearing the last launched preference.
886 class ExtensionPrefsClearLastLaunched
: public ExtensionPrefsTest
{
888 ~ExtensionPrefsClearLastLaunched() override
{}
890 void Initialize() override
{
891 extension_a_
= prefs_
.AddExtension("a");
892 extension_b_
= prefs_
.AddExtension("b");
895 void Verify() override
{
896 // Set last launched times for each extension.
897 prefs()->SetLastLaunchTime(extension_a_
->id(), base::Time::Now());
898 prefs()->SetLastLaunchTime(extension_b_
->id(), base::Time::Now());
900 // Also set some other preference for one of the extensions.
901 prefs()->SetAllowFileAccess(extension_a_
->id(), true);
903 // Now clear the launch times.
904 prefs()->ClearLastLaunchTimes();
906 // All launch times should be gone.
907 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_a_
->id()));
908 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_b_
->id()));
910 // Other preferences should be untouched.
911 EXPECT_TRUE(prefs()->AllowFileAccess(extension_a_
->id()));
915 scoped_refptr
<const Extension
> extension_a_
;
916 scoped_refptr
<const Extension
> extension_b_
;
918 TEST_F(ExtensionPrefsClearLastLaunched
, ExtensionPrefsClearLastLaunched
) {}
920 class ExtensionPrefsComponentExtension
: public ExtensionPrefsTest
{
922 ~ExtensionPrefsComponentExtension() override
{}
923 void Initialize() override
{
924 // Adding a component extension.
925 component_extension_
=
927 .SetManifest(DictionaryBuilder()
928 .Set(manifest_keys::kName
, "a")
929 .Set(manifest_keys::kVersion
, "0.1"))
930 .SetLocation(Manifest::COMPONENT
)
931 .SetPath(prefs_
.extensions_dir().AppendASCII("a"))
933 prefs_
.AddExtension(component_extension_
.get());
935 // Adding a non component extension.
936 no_component_extension_
=
938 .SetManifest(DictionaryBuilder()
939 .Set(manifest_keys::kName
, "b")
940 .Set(manifest_keys::kVersion
, "0.1"))
941 .SetLocation(Manifest::INTERNAL
)
942 .SetPath(prefs_
.extensions_dir().AppendASCII("b"))
944 prefs_
.AddExtension(no_component_extension_
.get());
946 APIPermissionSet api_perms
;
947 api_perms
.insert(APIPermission::kTab
);
948 api_perms
.insert(APIPermission::kBookmark
);
949 api_perms
.insert(APIPermission::kHistory
);
951 ManifestPermissionSet empty_manifest_permissions
;
953 URLPatternSet ehosts
, shosts
;
954 AddPattern(&shosts
, "chrome://print/*");
956 active_perms_
= new PermissionSet(api_perms
, empty_manifest_permissions
,
958 // Set the active permissions.
959 prefs()->SetActivePermissions(component_extension_
->id(),
960 active_perms_
.get());
961 prefs()->SetActivePermissions(no_component_extension_
->id(),
962 active_perms_
.get());
965 void Verify() override
{
966 // Component extension can access chrome://print/*.
967 scoped_refptr
<PermissionSet
> component_permissions(
968 prefs()->GetActivePermissions(component_extension_
->id()));
969 EXPECT_EQ(1u, component_permissions
->scriptable_hosts().size());
971 // Non Component extension can not access chrome://print/*.
972 scoped_refptr
<PermissionSet
> no_component_permissions(
973 prefs()->GetActivePermissions(no_component_extension_
->id()));
974 EXPECT_EQ(0u, no_component_permissions
->scriptable_hosts().size());
976 // |URLPattern::SCHEME_CHROMEUI| scheme will be added in valid_schemes for
977 // component extensions.
978 URLPatternSet scriptable_hosts
;
979 std::string pref_key
= "active_permissions.scriptable_host";
980 int valid_schemes
= URLPattern::SCHEME_ALL
& ~URLPattern::SCHEME_CHROMEUI
;
982 EXPECT_TRUE(prefs()->ReadPrefAsURLPatternSet(component_extension_
->id(),
983 pref_key
, &scriptable_hosts
,
986 EXPECT_FALSE(prefs()->ReadPrefAsURLPatternSet(no_component_extension_
->id(),
987 pref_key
, &scriptable_hosts
,
992 scoped_refptr
<PermissionSet
> active_perms_
;
993 scoped_refptr
<Extension
> component_extension_
;
994 scoped_refptr
<Extension
> no_component_extension_
;
996 TEST_F(ExtensionPrefsComponentExtension
, ExtensionPrefsComponentExtension
) {
999 } // namespace extensions