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_
.message_loop_proxy().get()) {}
49 ExtensionPrefsTest::~ExtensionPrefsTest() {
52 void ExtensionPrefsTest::RegisterPreferences(
53 user_prefs::PrefRegistrySyncable
* registry
) {}
55 void ExtensionPrefsTest::SetUp() {
56 RegisterPreferences(prefs_
.pref_registry().get());
60 void ExtensionPrefsTest::TearDown() {
63 // Reset ExtensionPrefs, and re-verify.
64 prefs_
.ResetPrefRegistry();
65 RegisterPreferences(prefs_
.pref_registry().get());
66 prefs_
.RecreateExtensionPrefs();
68 prefs_
.pref_service()->CommitPendingWrite();
69 message_loop_
.RunUntilIdle();
72 // Tests the LastPingDay/SetLastPingDay functions.
73 class ExtensionPrefsLastPingDay
: public ExtensionPrefsTest
{
75 ExtensionPrefsLastPingDay()
76 : extension_time_(Time::Now() - TimeDelta::FromHours(4)),
77 blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {}
79 void Initialize() override
{
80 extension_id_
= prefs_
.AddExtensionAndReturnId("last_ping_day");
81 EXPECT_TRUE(prefs()->LastPingDay(extension_id_
).is_null());
82 prefs()->SetLastPingDay(extension_id_
, extension_time_
);
83 prefs()->SetBlacklistLastPingDay(blacklist_time_
);
86 void Verify() override
{
87 Time result
= prefs()->LastPingDay(extension_id_
);
88 EXPECT_FALSE(result
.is_null());
89 EXPECT_TRUE(result
== extension_time_
);
90 result
= prefs()->BlacklistLastPingDay();
91 EXPECT_FALSE(result
.is_null());
92 EXPECT_TRUE(result
== blacklist_time_
);
98 std::string extension_id_
;
100 TEST_F(ExtensionPrefsLastPingDay
, LastPingDay
) {}
102 // Tests the GetToolbarOrder/SetToolbarOrder functions.
103 class ExtensionPrefsToolbarOrder
: public ExtensionPrefsTest
{
105 void Initialize() override
{
106 list_
.push_back(prefs_
.AddExtensionAndReturnId("1"));
107 list_
.push_back(prefs_
.AddExtensionAndReturnId("2"));
108 list_
.push_back(prefs_
.AddExtensionAndReturnId("3"));
109 ExtensionIdList before_list
= prefs()->GetToolbarOrder();
110 EXPECT_TRUE(before_list
.empty());
111 prefs()->SetToolbarOrder(list_
);
114 void Verify() override
{
115 ExtensionIdList result
= prefs()->GetToolbarOrder();
116 ASSERT_EQ(list_
, result
);
120 ExtensionIdList list_
;
122 TEST_F(ExtensionPrefsToolbarOrder
, ToolbarOrder
) {}
124 // Tests the IsExtensionDisabled/SetExtensionState functions.
125 class ExtensionPrefsExtensionState
: public ExtensionPrefsTest
{
127 void Initialize() override
{
128 extension
= prefs_
.AddExtension("test");
129 prefs()->SetExtensionState(extension
->id(), Extension::DISABLED
);
132 void Verify() override
{
133 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension
->id()));
137 scoped_refptr
<Extension
> extension
;
139 TEST_F(ExtensionPrefsExtensionState
, ExtensionState
) {}
141 class ExtensionPrefsEscalatePermissions
: public ExtensionPrefsTest
{
143 void Initialize() override
{
144 extension
= prefs_
.AddExtension("test");
145 prefs()->SetDidExtensionEscalatePermissions(extension
.get(), true);
148 void Verify() override
{
149 EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension
->id()));
153 scoped_refptr
<Extension
> extension
;
155 TEST_F(ExtensionPrefsEscalatePermissions
, EscalatePermissions
) {}
157 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
158 class ExtensionPrefsGrantedPermissions
: public ExtensionPrefsTest
{
160 void Initialize() override
{
161 const APIPermissionInfo
* permission_info
=
162 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket
);
164 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
166 api_perm_set1_
.insert(APIPermission::kTab
);
167 api_perm_set1_
.insert(APIPermission::kBookmark
);
168 scoped_ptr
<APIPermission
> permission(
169 permission_info
->CreateAPIPermission());
171 scoped_ptr
<base::ListValue
> value(new base::ListValue());
172 value
->Append(new base::StringValue("tcp-connect:*.example.com:80"));
173 value
->Append(new base::StringValue("udp-bind::8080"));
174 value
->Append(new base::StringValue("udp-send-to::8888"));
175 ASSERT_TRUE(permission
->FromValue(value
.get(), NULL
, NULL
));
177 api_perm_set1_
.insert(permission
.release());
179 api_perm_set2_
.insert(APIPermission::kHistory
);
181 AddPattern(&ehost_perm_set1_
, "http://*.google.com/*");
182 AddPattern(&ehost_perm_set1_
, "http://example.com/*");
183 AddPattern(&ehost_perm_set1_
, "chrome://favicon/*");
185 AddPattern(&ehost_perm_set2_
, "https://*.google.com/*");
187 AddPattern(&ehost_perm_set2_
, "http://*.google.com/*");
189 AddPattern(&shost_perm_set1_
, "http://reddit.com/r/test/*");
190 AddPattern(&shost_perm_set2_
, "http://reddit.com/r/test/*");
191 AddPattern(&shost_perm_set2_
, "http://somesite.com/*");
192 AddPattern(&shost_perm_set2_
, "http://example.com/*");
194 APIPermissionSet expected_apis
= api_perm_set1_
;
196 AddPattern(&ehost_permissions_
, "http://*.google.com/*");
197 AddPattern(&ehost_permissions_
, "http://example.com/*");
198 AddPattern(&ehost_permissions_
, "chrome://favicon/*");
199 AddPattern(&ehost_permissions_
, "https://*.google.com/*");
201 AddPattern(&shost_permissions_
, "http://reddit.com/r/test/*");
202 AddPattern(&shost_permissions_
, "http://somesite.com/*");
203 AddPattern(&shost_permissions_
, "http://example.com/*");
205 APIPermissionSet empty_set
;
206 ManifestPermissionSet empty_manifest_permissions
;
207 URLPatternSet empty_extent
;
208 scoped_refptr
<PermissionSet
> permissions
;
209 scoped_refptr
<PermissionSet
> granted_permissions
;
211 // Make sure both granted api and host permissions start empty.
212 granted_permissions
=
213 prefs()->GetGrantedPermissions(extension_id_
);
214 EXPECT_TRUE(granted_permissions
->IsEmpty());
216 permissions
= new PermissionSet(
217 api_perm_set1_
, empty_manifest_permissions
, empty_extent
, empty_extent
);
219 // Add part of the api permissions.
220 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
221 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
222 EXPECT_TRUE(granted_permissions
.get());
223 EXPECT_FALSE(granted_permissions
->IsEmpty());
224 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
225 EXPECT_TRUE(granted_permissions
->effective_hosts().is_empty());
226 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
227 granted_permissions
= NULL
;
229 // Add part of the explicit host permissions.
230 permissions
= new PermissionSet(
231 empty_set
, empty_manifest_permissions
, ehost_perm_set1_
, empty_extent
);
232 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
233 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
234 EXPECT_FALSE(granted_permissions
->IsEmpty());
235 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
236 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
237 EXPECT_EQ(ehost_perm_set1_
,
238 granted_permissions
->explicit_hosts());
239 EXPECT_EQ(ehost_perm_set1_
,
240 granted_permissions
->effective_hosts());
242 // Add part of the scriptable host permissions.
243 permissions
= new PermissionSet(
244 empty_set
, empty_manifest_permissions
, empty_extent
, shost_perm_set1_
);
245 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
246 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
247 EXPECT_FALSE(granted_permissions
->IsEmpty());
248 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
249 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
250 EXPECT_EQ(ehost_perm_set1_
,
251 granted_permissions
->explicit_hosts());
252 EXPECT_EQ(shost_perm_set1_
,
253 granted_permissions
->scriptable_hosts());
255 URLPatternSet::CreateUnion(ehost_perm_set1_
, shost_perm_set1_
,
256 &effective_permissions_
);
257 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
259 // Add the rest of the permissions.
260 permissions
= new PermissionSet(
261 api_perm_set2_
, empty_manifest_permissions
,
262 ehost_perm_set2_
, shost_perm_set2_
);
264 APIPermissionSet::Union(expected_apis
, api_perm_set2_
, &api_permissions_
);
266 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
267 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
268 EXPECT_TRUE(granted_permissions
.get());
269 EXPECT_FALSE(granted_permissions
->IsEmpty());
270 EXPECT_EQ(api_permissions_
, granted_permissions
->apis());
271 EXPECT_EQ(ehost_permissions_
,
272 granted_permissions
->explicit_hosts());
273 EXPECT_EQ(shost_permissions_
,
274 granted_permissions
->scriptable_hosts());
275 effective_permissions_
.ClearPatterns();
276 URLPatternSet::CreateUnion(ehost_permissions_
, shost_permissions_
,
277 &effective_permissions_
);
278 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
281 void Verify() override
{
282 scoped_refptr
<PermissionSet
> permissions(
283 prefs()->GetGrantedPermissions(extension_id_
));
284 EXPECT_TRUE(permissions
.get());
285 EXPECT_FALSE(permissions
->HasEffectiveFullAccess());
286 EXPECT_EQ(api_permissions_
, permissions
->apis());
287 EXPECT_EQ(ehost_permissions_
,
288 permissions
->explicit_hosts());
289 EXPECT_EQ(shost_permissions_
,
290 permissions
->scriptable_hosts());
294 std::string extension_id_
;
295 APIPermissionSet api_perm_set1_
;
296 APIPermissionSet api_perm_set2_
;
297 URLPatternSet ehost_perm_set1_
;
298 URLPatternSet ehost_perm_set2_
;
299 URLPatternSet shost_perm_set1_
;
300 URLPatternSet shost_perm_set2_
;
302 APIPermissionSet api_permissions_
;
303 URLPatternSet ehost_permissions_
;
304 URLPatternSet shost_permissions_
;
305 URLPatternSet effective_permissions_
;
307 TEST_F(ExtensionPrefsGrantedPermissions
, GrantedPermissions
) {}
309 // Tests the SetActivePermissions / GetActivePermissions functions.
310 class ExtensionPrefsActivePermissions
: public ExtensionPrefsTest
{
312 void Initialize() override
{
313 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
315 APIPermissionSet api_perms
;
316 api_perms
.insert(APIPermission::kTab
);
317 api_perms
.insert(APIPermission::kBookmark
);
318 api_perms
.insert(APIPermission::kHistory
);
320 ManifestPermissionSet empty_manifest_permissions
;
322 URLPatternSet ehosts
;
323 AddPattern(&ehosts
, "http://*.google.com/*");
324 AddPattern(&ehosts
, "http://example.com/*");
325 AddPattern(&ehosts
, "chrome://favicon/*");
327 URLPatternSet shosts
;
328 AddPattern(&shosts
, "https://*.google.com/*");
329 AddPattern(&shosts
, "http://reddit.com/r/test/*");
331 active_perms_
= new PermissionSet(
332 api_perms
, empty_manifest_permissions
, ehosts
, shosts
);
334 // Make sure the active permissions start empty.
335 scoped_refptr
<PermissionSet
> active(
336 prefs()->GetActivePermissions(extension_id_
));
337 EXPECT_TRUE(active
->IsEmpty());
339 // Set the active permissions.
340 prefs()->SetActivePermissions(extension_id_
, active_perms_
.get());
341 active
= prefs()->GetActivePermissions(extension_id_
);
342 EXPECT_EQ(active_perms_
->apis(), active
->apis());
343 EXPECT_EQ(active_perms_
->explicit_hosts(), active
->explicit_hosts());
344 EXPECT_EQ(active_perms_
->scriptable_hosts(), active
->scriptable_hosts());
345 EXPECT_EQ(*active_perms_
.get(), *active
.get());
348 void Verify() override
{
349 scoped_refptr
<PermissionSet
> permissions(
350 prefs()->GetActivePermissions(extension_id_
));
351 EXPECT_EQ(*active_perms_
.get(), *permissions
.get());
355 std::string extension_id_
;
356 scoped_refptr
<PermissionSet
> active_perms_
;
358 TEST_F(ExtensionPrefsActivePermissions
, SetAndGetActivePermissions
) {}
360 // Tests the GetVersionString function.
361 class ExtensionPrefsVersionString
: public ExtensionPrefsTest
{
363 void Initialize() override
{
364 extension
= prefs_
.AddExtension("test");
365 EXPECT_EQ("0.1", prefs()->GetVersionString(extension
->id()));
366 prefs()->OnExtensionUninstalled(extension
->id(),
367 Manifest::INTERNAL
, false);
370 void Verify() override
{
371 EXPECT_EQ("", prefs()->GetVersionString(extension
->id()));
375 scoped_refptr
<Extension
> extension
;
377 TEST_F(ExtensionPrefsVersionString
, VersionString
) {}
379 class ExtensionPrefsAcknowledgment
: public ExtensionPrefsTest
{
381 void Initialize() override
{
382 not_installed_id_
= "pghjnghklobnfoidcldiidjjjhkeeaoi";
384 // Install some extensions.
385 for (int i
= 0; i
< 5; i
++) {
386 std::string name
= "test" + base::IntToString(i
);
387 extensions_
.push_back(prefs_
.AddExtension(name
));
390 prefs()->GetInstalledExtensionInfo(not_installed_id_
).get());
392 ExtensionList::const_iterator iter
;
393 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
394 std::string id
= (*iter
)->id();
395 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
396 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
397 if (external_id_
.empty()) {
401 if (blacklisted_id_
.empty()) {
402 blacklisted_id_
= id
;
406 // For each type of acknowledgment, acknowledge one installed and one
407 // not-installed extension id.
408 prefs()->AcknowledgeExternalExtension(external_id_
);
409 prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_
);
410 prefs()->AcknowledgeExternalExtension(not_installed_id_
);
411 prefs()->AcknowledgeBlacklistedExtension(not_installed_id_
);
414 void Verify() override
{
415 ExtensionList::const_iterator iter
;
416 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
417 std::string id
= (*iter
)->id();
418 if (id
== external_id_
) {
419 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id
));
421 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
423 if (id
== blacklisted_id_
) {
424 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
426 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
429 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_
));
430 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_
));
434 ExtensionList extensions_
;
436 std::string not_installed_id_
;
437 std::string external_id_
;
438 std::string blacklisted_id_
;
440 TEST_F(ExtensionPrefsAcknowledgment
, Acknowledgment
) {}
442 // Tests the idle install information functions.
443 class ExtensionPrefsDelayedInstallInfo
: public ExtensionPrefsTest
{
445 // Sets idle install information for one test extension.
446 void SetIdleInfo(const std::string
& id
, int num
) {
447 base::DictionaryValue manifest
;
448 manifest
.SetString(manifest_keys::kName
, "test");
449 manifest
.SetString(manifest_keys::kVersion
, "1." + base::IntToString(num
));
450 base::FilePath path
=
451 prefs_
.extensions_dir().AppendASCII(base::IntToString(num
));
453 scoped_refptr
<Extension
> extension
= Extension::Create(
454 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id
, &errors
);
455 ASSERT_TRUE(extension
.get()) << errors
;
456 ASSERT_EQ(id
, extension
->id());
457 prefs()->SetDelayedInstallInfo(extension
.get(),
460 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
461 syncer::StringOrdinal(),
465 // Verifies that we get back expected idle install information previously
466 // set by SetIdleInfo.
467 void VerifyIdleInfo(const std::string
& id
, int num
) {
468 scoped_ptr
<ExtensionInfo
> info(prefs()->GetDelayedInstallInfo(id
));
471 ASSERT_TRUE(info
->extension_manifest
->GetString("version", &version
));
472 ASSERT_EQ("1." + base::IntToString(num
), version
);
473 ASSERT_EQ(base::IntToString(num
),
474 info
->extension_path
.BaseName().MaybeAsASCII());
477 bool HasInfoForId(ExtensionPrefs::ExtensionsInfo
* info
,
478 const std::string
& id
) {
479 for (size_t i
= 0; i
< info
->size(); ++i
) {
480 if (info
->at(i
)->extension_id
== id
)
486 void Initialize() override
{
487 PathService::Get(chrome::DIR_TEST_DATA
, &basedir_
);
489 id1_
= prefs_
.AddExtensionAndReturnId("1");
490 id2_
= prefs_
.AddExtensionAndReturnId("2");
491 id3_
= prefs_
.AddExtensionAndReturnId("3");
492 id4_
= prefs_
.AddExtensionAndReturnId("4");
494 // Set info for two extensions, then remove it.
495 SetIdleInfo(id1_
, 1);
496 SetIdleInfo(id2_
, 2);
497 VerifyIdleInfo(id1_
, 1);
498 VerifyIdleInfo(id2_
, 2);
499 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
500 prefs()->GetAllDelayedInstallInfo());
501 EXPECT_EQ(2u, info
->size());
502 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
503 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
504 prefs()->RemoveDelayedInstallInfo(id1_
);
505 prefs()->RemoveDelayedInstallInfo(id2_
);
506 info
= prefs()->GetAllDelayedInstallInfo();
507 EXPECT_TRUE(info
->empty());
509 // Try getting/removing info for an id that used to have info set.
510 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_
));
511 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_
));
513 // Try getting/removing info for an id that has not yet had any info set.
514 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
515 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_
));
517 // Set info for 4 extensions, then remove for one of them.
518 SetIdleInfo(id1_
, 1);
519 SetIdleInfo(id2_
, 2);
520 SetIdleInfo(id3_
, 3);
521 SetIdleInfo(id4_
, 4);
522 VerifyIdleInfo(id1_
, 1);
523 VerifyIdleInfo(id2_
, 2);
524 VerifyIdleInfo(id3_
, 3);
525 VerifyIdleInfo(id4_
, 4);
526 prefs()->RemoveDelayedInstallInfo(id3_
);
529 void Verify() override
{
530 // Make sure the info for the 3 extensions we expect is present.
531 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
532 prefs()->GetAllDelayedInstallInfo());
533 EXPECT_EQ(3u, info
->size());
534 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
535 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
536 EXPECT_TRUE(HasInfoForId(info
.get(), id4_
));
537 VerifyIdleInfo(id1_
, 1);
538 VerifyIdleInfo(id2_
, 2);
539 VerifyIdleInfo(id4_
, 4);
541 // Make sure there isn't info the for the one extension id we removed.
542 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
547 base::FilePath basedir_
;
553 TEST_F(ExtensionPrefsDelayedInstallInfo
, DelayedInstallInfo
) {}
555 // Tests the FinishDelayedInstallInfo function.
556 class ExtensionPrefsFinishDelayedInstallInfo
: public ExtensionPrefsTest
{
558 void Initialize() override
{
559 base::DictionaryValue dictionary
;
560 dictionary
.SetString(manifest_keys::kName
, "test");
561 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
562 dictionary
.SetString(manifest_keys::kBackgroundPage
, "background.html");
563 scoped_refptr
<Extension
> extension
=
564 prefs_
.AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
565 id_
= extension
->id();
569 base::DictionaryValue manifest
;
570 manifest
.SetString(manifest_keys::kName
, "test");
571 manifest
.SetString(manifest_keys::kVersion
, "0.2");
572 scoped_ptr
<base::ListValue
> scripts(new base::ListValue
);
573 scripts
->AppendString("test.js");
574 manifest
.Set(manifest_keys::kBackgroundScripts
, scripts
.release());
575 base::FilePath path
=
576 prefs_
.extensions_dir().AppendASCII("test_0.2");
578 scoped_refptr
<Extension
> new_extension
= Extension::Create(
579 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id_
, &errors
);
580 ASSERT_TRUE(new_extension
.get()) << errors
;
581 ASSERT_EQ(id_
, new_extension
->id());
582 prefs()->SetDelayedInstallInfo(new_extension
.get(),
585 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
586 syncer::StringOrdinal(),
589 // Finish idle installation
590 ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_
));
593 void Verify() override
{
594 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_
));
595 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(id_
));
597 const base::DictionaryValue
* manifest
;
598 ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_
, "manifest", &manifest
));
599 ASSERT_TRUE(manifest
);
601 EXPECT_TRUE(manifest
->GetString(manifest_keys::kName
, &value
));
602 EXPECT_EQ("test", value
);
603 EXPECT_TRUE(manifest
->GetString(manifest_keys::kVersion
, &value
));
604 EXPECT_EQ("0.2", value
);
605 EXPECT_FALSE(manifest
->GetString(manifest_keys::kBackgroundPage
, &value
));
606 const base::ListValue
* scripts
;
607 ASSERT_TRUE(manifest
->GetList(manifest_keys::kBackgroundScripts
, &scripts
));
608 EXPECT_EQ(1u, scripts
->GetSize());
614 TEST_F(ExtensionPrefsFinishDelayedInstallInfo
, FinishDelayedInstallInfo
) {}
616 class ExtensionPrefsOnExtensionInstalled
: public ExtensionPrefsTest
{
618 void Initialize() override
{
619 extension_
= prefs_
.AddExtension("on_extension_installed");
620 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_
->id()));
621 prefs()->OnExtensionInstalled(extension_
.get(),
623 syncer::StringOrdinal(),
627 void Verify() override
{
628 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_
->id()));
629 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(extension_
->id()));
633 scoped_refptr
<Extension
> extension_
;
635 TEST_F(ExtensionPrefsOnExtensionInstalled
,
636 ExtensionPrefsOnExtensionInstalled
) {}
638 class ExtensionPrefsAppDraggedByUser
: public ExtensionPrefsTest
{
640 void Initialize() override
{
641 extension_
= prefs_
.AddExtension("on_extension_installed");
642 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_
->id()));
643 prefs()->OnExtensionInstalled(extension_
.get(),
645 syncer::StringOrdinal(),
649 void Verify() override
{
650 // Set the flag and see if it persisted.
651 prefs()->SetAppDraggedByUser(extension_
->id());
652 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
654 // Make sure it doesn't change on consecutive calls.
655 prefs()->SetAppDraggedByUser(extension_
->id());
656 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
660 scoped_refptr
<Extension
> extension_
;
662 TEST_F(ExtensionPrefsAppDraggedByUser
, ExtensionPrefsAppDraggedByUser
) {}
664 class ExtensionPrefsFlags
: public ExtensionPrefsTest
{
666 void Initialize() override
{
668 base::DictionaryValue dictionary
;
669 dictionary
.SetString(manifest_keys::kName
, "from_webstore");
670 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
671 webstore_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
672 dictionary
, Manifest::INTERNAL
, Extension::FROM_WEBSTORE
);
676 base::DictionaryValue dictionary
;
677 dictionary
.SetString(manifest_keys::kName
, "from_bookmark");
678 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
679 bookmark_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
680 dictionary
, Manifest::INTERNAL
, Extension::FROM_BOOKMARK
);
684 base::DictionaryValue dictionary
;
685 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_default");
686 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
687 default_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
690 Extension::WAS_INSTALLED_BY_DEFAULT
);
694 base::DictionaryValue dictionary
;
695 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_oem");
696 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
697 oem_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
698 dictionary
, Manifest::INTERNAL
, Extension::WAS_INSTALLED_BY_OEM
);
702 void Verify() override
{
703 EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_
->id()));
704 EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_
->id()));
706 EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_
->id()));
707 EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_
->id()));
709 EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_
->id()));
710 EXPECT_TRUE(prefs()->WasInstalledByOem(oem_extension_
->id()));
714 scoped_refptr
<Extension
> webstore_extension_
;
715 scoped_refptr
<Extension
> bookmark_extension_
;
716 scoped_refptr
<Extension
> default_extension_
;
717 scoped_refptr
<Extension
> oem_extension_
;
719 TEST_F(ExtensionPrefsFlags
, ExtensionPrefsFlags
) {}
721 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
722 : ExtensionPrefsTest() {
723 base::DictionaryValue simple_dict
;
726 simple_dict
.SetString(manifest_keys::kVersion
, "1.0.0.0");
727 simple_dict
.SetString(manifest_keys::kName
, "unused");
729 extension1_
= Extension::Create(
730 prefs_
.temp_dir().AppendASCII("ext1_"),
731 Manifest::EXTERNAL_PREF
,
735 extension2_
= Extension::Create(
736 prefs_
.temp_dir().AppendASCII("ext2_"),
737 Manifest::EXTERNAL_PREF
,
741 extension3_
= Extension::Create(
742 prefs_
.temp_dir().AppendASCII("ext3_"),
743 Manifest::EXTERNAL_PREF
,
747 extension4_
= Extension::Create(
748 prefs_
.temp_dir().AppendASCII("ext4_"),
749 Manifest::EXTERNAL_PREF
,
754 for (size_t i
= 0; i
< kNumInstalledExtensions
; ++i
)
755 installed_
[i
] = false;
758 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
761 // Tests that blacklist state can be queried.
762 class ExtensionPrefsBlacklistedExtensions
: public ExtensionPrefsTest
{
764 ~ExtensionPrefsBlacklistedExtensions() override
{}
766 void Initialize() override
{
767 extension_a_
= prefs_
.AddExtension("a");
768 extension_b_
= prefs_
.AddExtension("b");
769 extension_c_
= prefs_
.AddExtension("c");
772 void Verify() override
{
775 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
777 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
780 ids
.insert(extension_a_
->id());
781 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
783 prefs()->SetExtensionBlacklisted(extension_b_
->id(), true);
784 prefs()->SetExtensionBlacklisted(extension_c_
->id(), true);
787 ids
.insert(extension_a_
->id());
788 ids
.insert(extension_b_
->id());
789 ids
.insert(extension_c_
->id());
790 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
792 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
795 ids
.insert(extension_b_
->id());
796 ids
.insert(extension_c_
->id());
797 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
799 prefs()->SetExtensionBlacklisted(extension_b_
->id(), false);
800 prefs()->SetExtensionBlacklisted(extension_c_
->id(), false);
803 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
806 // The interesting part: make sure that we're cleaning up after ourselves
807 // when we're storing *just* the fact that the extension is blacklisted.
808 std::string arbitrary_id
= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
810 prefs()->SetExtensionBlacklisted(arbitrary_id
, true);
811 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
813 // (And make sure that the acknowledged bit is also cleared).
814 prefs()->AcknowledgeBlacklistedExtension(arbitrary_id
);
816 EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id
));
819 ids
.insert(arbitrary_id
);
820 ids
.insert(extension_a_
->id());
821 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
823 prefs()->SetExtensionBlacklisted(arbitrary_id
, false);
824 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
825 EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id
));
828 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
833 scoped_refptr
<const Extension
> extension_a_
;
834 scoped_refptr
<const Extension
> extension_b_
;
835 scoped_refptr
<const Extension
> extension_c_
;
837 TEST_F(ExtensionPrefsBlacklistedExtensions
,
838 ExtensionPrefsBlacklistedExtensions
) {}
840 // Tests the blacklist state. Old "blacklist" preference should take precedence
841 // over new "blacklist_state".
842 class ExtensionPrefsBlacklistState
: public ExtensionPrefsTest
{
844 ~ExtensionPrefsBlacklistState() override
{}
846 void Initialize() override
{ extension_a_
= prefs_
.AddExtension("a"); }
848 void Verify() override
{
849 ExtensionIdSet empty_ids
;
850 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
852 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
853 EXPECT_EQ(BLACKLISTED_MALWARE
,
854 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
856 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
857 BLACKLISTED_POTENTIALLY_UNWANTED
);
858 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED
,
859 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
860 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
861 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
863 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
864 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
865 EXPECT_EQ(BLACKLISTED_MALWARE
,
866 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
867 EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
869 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
871 EXPECT_EQ(NOT_BLACKLISTED
,
872 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
873 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
874 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
878 scoped_refptr
<const Extension
> extension_a_
;
880 TEST_F(ExtensionPrefsBlacklistState
, ExtensionPrefsBlacklistState
) {}
882 // Tests clearing the last launched preference.
883 class ExtensionPrefsClearLastLaunched
: public ExtensionPrefsTest
{
885 ~ExtensionPrefsClearLastLaunched() override
{}
887 void Initialize() override
{
888 extension_a_
= prefs_
.AddExtension("a");
889 extension_b_
= prefs_
.AddExtension("b");
892 void Verify() override
{
893 // Set last launched times for each extension.
894 prefs()->SetLastLaunchTime(extension_a_
->id(), base::Time::Now());
895 prefs()->SetLastLaunchTime(extension_b_
->id(), base::Time::Now());
897 // Also set some other preference for one of the extensions.
898 prefs()->SetAllowFileAccess(extension_a_
->id(), true);
900 // Now clear the launch times.
901 prefs()->ClearLastLaunchTimes();
903 // All launch times should be gone.
904 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_a_
->id()));
905 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_b_
->id()));
907 // Other preferences should be untouched.
908 EXPECT_TRUE(prefs()->AllowFileAccess(extension_a_
->id()));
912 scoped_refptr
<const Extension
> extension_a_
;
913 scoped_refptr
<const Extension
> extension_b_
;
915 TEST_F(ExtensionPrefsClearLastLaunched
, ExtensionPrefsClearLastLaunched
) {}
917 class ExtensionPrefsComponentExtension
: public ExtensionPrefsTest
{
919 ~ExtensionPrefsComponentExtension() override
{}
920 void Initialize() override
{
921 // Adding a component extension.
922 component_extension_
=
924 .SetManifest(DictionaryBuilder()
925 .Set(manifest_keys::kName
, "a")
926 .Set(manifest_keys::kVersion
, "0.1"))
927 .SetLocation(Manifest::COMPONENT
)
928 .SetPath(prefs_
.extensions_dir().AppendASCII("a"))
930 prefs_
.AddExtension(component_extension_
.get());
932 // Adding a non component extension.
933 no_component_extension_
=
935 .SetManifest(DictionaryBuilder()
936 .Set(manifest_keys::kName
, "b")
937 .Set(manifest_keys::kVersion
, "0.1"))
938 .SetLocation(Manifest::INTERNAL
)
939 .SetPath(prefs_
.extensions_dir().AppendASCII("b"))
941 prefs_
.AddExtension(no_component_extension_
.get());
943 APIPermissionSet api_perms
;
944 api_perms
.insert(APIPermission::kTab
);
945 api_perms
.insert(APIPermission::kBookmark
);
946 api_perms
.insert(APIPermission::kHistory
);
948 ManifestPermissionSet empty_manifest_permissions
;
950 URLPatternSet ehosts
, shosts
;
951 AddPattern(&shosts
, "chrome://print/*");
953 active_perms_
= new PermissionSet(api_perms
, empty_manifest_permissions
,
955 // Set the active permissions.
956 prefs()->SetActivePermissions(component_extension_
->id(),
957 active_perms_
.get());
958 prefs()->SetActivePermissions(no_component_extension_
->id(),
959 active_perms_
.get());
962 void Verify() override
{
963 // Component extension can access chrome://print/*.
964 scoped_refptr
<PermissionSet
> component_permissions(
965 prefs()->GetActivePermissions(component_extension_
->id()));
966 EXPECT_EQ(1u, component_permissions
->scriptable_hosts().size());
968 // Non Component extension can not access chrome://print/*.
969 scoped_refptr
<PermissionSet
> no_component_permissions(
970 prefs()->GetActivePermissions(no_component_extension_
->id()));
971 EXPECT_EQ(0u, no_component_permissions
->scriptable_hosts().size());
973 // |URLPattern::SCHEME_CHROMEUI| scheme will be added in valid_schemes for
974 // component extensions.
975 URLPatternSet scriptable_hosts
;
976 std::string pref_key
= "active_permissions.scriptable_host";
977 int valid_schemes
= URLPattern::SCHEME_ALL
& ~URLPattern::SCHEME_CHROMEUI
;
979 EXPECT_TRUE(prefs()->ReadPrefAsURLPatternSet(component_extension_
->id(),
980 pref_key
, &scriptable_hosts
,
983 EXPECT_FALSE(prefs()->ReadPrefAsURLPatternSet(no_component_extension_
->id(),
984 pref_key
, &scriptable_hosts
,
989 scoped_refptr
<PermissionSet
> active_perms_
;
990 scoped_refptr
<Extension
> component_extension_
;
991 scoped_refptr
<Extension
> no_component_extension_
;
993 TEST_F(ExtensionPrefsComponentExtension
, ExtensionPrefsComponentExtension
) {
996 } // namespace extensions