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/manifest_constants.h"
29 #include "extensions/common/permissions/permission_set.h"
30 #include "extensions/common/permissions/permissions_info.h"
31 #include "sync/api/string_ordinal.h"
34 using base::TimeDelta
;
35 using content::BrowserThread
;
37 namespace extensions
{
39 static void AddPattern(URLPatternSet
* extent
, const std::string
& pattern
) {
40 int schemes
= URLPattern::SCHEME_ALL
;
41 extent
->AddPattern(URLPattern(schemes
, pattern
));
44 ExtensionPrefsTest::ExtensionPrefsTest()
45 : ui_thread_(BrowserThread::UI
, &message_loop_
),
46 prefs_(message_loop_
.message_loop_proxy().get()) {}
48 ExtensionPrefsTest::~ExtensionPrefsTest() {
51 void ExtensionPrefsTest::RegisterPreferences(
52 user_prefs::PrefRegistrySyncable
* registry
) {}
54 void ExtensionPrefsTest::SetUp() {
55 RegisterPreferences(prefs_
.pref_registry().get());
59 void ExtensionPrefsTest::TearDown() {
62 // Reset ExtensionPrefs, and re-verify.
63 prefs_
.ResetPrefRegistry();
64 RegisterPreferences(prefs_
.pref_registry().get());
65 prefs_
.RecreateExtensionPrefs();
67 prefs_
.pref_service()->CommitPendingWrite();
68 message_loop_
.RunUntilIdle();
71 // Tests the LastPingDay/SetLastPingDay functions.
72 class ExtensionPrefsLastPingDay
: public ExtensionPrefsTest
{
74 ExtensionPrefsLastPingDay()
75 : extension_time_(Time::Now() - TimeDelta::FromHours(4)),
76 blacklist_time_(Time::Now() - TimeDelta::FromHours(2)) {}
78 void Initialize() override
{
79 extension_id_
= prefs_
.AddExtensionAndReturnId("last_ping_day");
80 EXPECT_TRUE(prefs()->LastPingDay(extension_id_
).is_null());
81 prefs()->SetLastPingDay(extension_id_
, extension_time_
);
82 prefs()->SetBlacklistLastPingDay(blacklist_time_
);
85 void Verify() override
{
86 Time result
= prefs()->LastPingDay(extension_id_
);
87 EXPECT_FALSE(result
.is_null());
88 EXPECT_TRUE(result
== extension_time_
);
89 result
= prefs()->BlacklistLastPingDay();
90 EXPECT_FALSE(result
.is_null());
91 EXPECT_TRUE(result
== blacklist_time_
);
97 std::string extension_id_
;
99 TEST_F(ExtensionPrefsLastPingDay
, LastPingDay
) {}
101 // Tests the GetToolbarOrder/SetToolbarOrder functions.
102 class ExtensionPrefsToolbarOrder
: public ExtensionPrefsTest
{
104 void Initialize() override
{
105 list_
.push_back(prefs_
.AddExtensionAndReturnId("1"));
106 list_
.push_back(prefs_
.AddExtensionAndReturnId("2"));
107 list_
.push_back(prefs_
.AddExtensionAndReturnId("3"));
108 ExtensionIdList before_list
= prefs()->GetToolbarOrder();
109 EXPECT_TRUE(before_list
.empty());
110 prefs()->SetToolbarOrder(list_
);
113 void Verify() override
{
114 ExtensionIdList result
= prefs()->GetToolbarOrder();
115 ASSERT_EQ(list_
, result
);
119 ExtensionIdList list_
;
121 TEST_F(ExtensionPrefsToolbarOrder
, ToolbarOrder
) {}
123 // Tests the IsExtensionDisabled/SetExtensionState functions.
124 class ExtensionPrefsExtensionState
: public ExtensionPrefsTest
{
126 void Initialize() override
{
127 extension
= prefs_
.AddExtension("test");
128 prefs()->SetExtensionState(extension
->id(), Extension::DISABLED
);
131 void Verify() override
{
132 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension
->id()));
136 scoped_refptr
<Extension
> extension
;
138 TEST_F(ExtensionPrefsExtensionState
, ExtensionState
) {}
140 class ExtensionPrefsEscalatePermissions
: public ExtensionPrefsTest
{
142 void Initialize() override
{
143 extension
= prefs_
.AddExtension("test");
144 prefs()->SetDidExtensionEscalatePermissions(extension
.get(), true);
147 void Verify() override
{
148 EXPECT_TRUE(prefs()->DidExtensionEscalatePermissions(extension
->id()));
152 scoped_refptr
<Extension
> extension
;
154 TEST_F(ExtensionPrefsEscalatePermissions
, EscalatePermissions
) {}
156 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
157 class ExtensionPrefsGrantedPermissions
: public ExtensionPrefsTest
{
159 void Initialize() override
{
160 const APIPermissionInfo
* permission_info
=
161 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket
);
163 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
165 api_perm_set1_
.insert(APIPermission::kTab
);
166 api_perm_set1_
.insert(APIPermission::kBookmark
);
167 scoped_ptr
<APIPermission
> permission(
168 permission_info
->CreateAPIPermission());
170 scoped_ptr
<base::ListValue
> value(new base::ListValue());
171 value
->Append(new base::StringValue("tcp-connect:*.example.com:80"));
172 value
->Append(new base::StringValue("udp-bind::8080"));
173 value
->Append(new base::StringValue("udp-send-to::8888"));
174 ASSERT_TRUE(permission
->FromValue(value
.get(), NULL
, NULL
));
176 api_perm_set1_
.insert(permission
.release());
178 api_perm_set2_
.insert(APIPermission::kHistory
);
180 AddPattern(&ehost_perm_set1_
, "http://*.google.com/*");
181 AddPattern(&ehost_perm_set1_
, "http://example.com/*");
182 AddPattern(&ehost_perm_set1_
, "chrome://favicon/*");
184 AddPattern(&ehost_perm_set2_
, "https://*.google.com/*");
186 AddPattern(&ehost_perm_set2_
, "http://*.google.com/*");
188 AddPattern(&shost_perm_set1_
, "http://reddit.com/r/test/*");
189 AddPattern(&shost_perm_set2_
, "http://reddit.com/r/test/*");
190 AddPattern(&shost_perm_set2_
, "http://somesite.com/*");
191 AddPattern(&shost_perm_set2_
, "http://example.com/*");
193 APIPermissionSet expected_apis
= api_perm_set1_
;
195 AddPattern(&ehost_permissions_
, "http://*.google.com/*");
196 AddPattern(&ehost_permissions_
, "http://example.com/*");
197 AddPattern(&ehost_permissions_
, "chrome://favicon/*");
198 AddPattern(&ehost_permissions_
, "https://*.google.com/*");
200 AddPattern(&shost_permissions_
, "http://reddit.com/r/test/*");
201 AddPattern(&shost_permissions_
, "http://somesite.com/*");
202 AddPattern(&shost_permissions_
, "http://example.com/*");
204 APIPermissionSet empty_set
;
205 ManifestPermissionSet empty_manifest_permissions
;
206 URLPatternSet empty_extent
;
207 scoped_refptr
<PermissionSet
> permissions
;
208 scoped_refptr
<PermissionSet
> granted_permissions
;
210 // Make sure both granted api and host permissions start empty.
211 granted_permissions
=
212 prefs()->GetGrantedPermissions(extension_id_
);
213 EXPECT_TRUE(granted_permissions
->IsEmpty());
215 permissions
= new PermissionSet(
216 api_perm_set1_
, empty_manifest_permissions
, empty_extent
, empty_extent
);
218 // Add part of the api permissions.
219 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
220 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
221 EXPECT_TRUE(granted_permissions
.get());
222 EXPECT_FALSE(granted_permissions
->IsEmpty());
223 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
224 EXPECT_TRUE(granted_permissions
->effective_hosts().is_empty());
225 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
226 granted_permissions
= NULL
;
228 // Add part of the explicit host permissions.
229 permissions
= new PermissionSet(
230 empty_set
, empty_manifest_permissions
, ehost_perm_set1_
, empty_extent
);
231 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
232 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
233 EXPECT_FALSE(granted_permissions
->IsEmpty());
234 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
235 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
236 EXPECT_EQ(ehost_perm_set1_
,
237 granted_permissions
->explicit_hosts());
238 EXPECT_EQ(ehost_perm_set1_
,
239 granted_permissions
->effective_hosts());
241 // Add part of the scriptable host permissions.
242 permissions
= new PermissionSet(
243 empty_set
, empty_manifest_permissions
, empty_extent
, shost_perm_set1_
);
244 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
245 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
246 EXPECT_FALSE(granted_permissions
->IsEmpty());
247 EXPECT_FALSE(granted_permissions
->HasEffectiveFullAccess());
248 EXPECT_EQ(expected_apis
, granted_permissions
->apis());
249 EXPECT_EQ(ehost_perm_set1_
,
250 granted_permissions
->explicit_hosts());
251 EXPECT_EQ(shost_perm_set1_
,
252 granted_permissions
->scriptable_hosts());
254 URLPatternSet::CreateUnion(ehost_perm_set1_
, shost_perm_set1_
,
255 &effective_permissions_
);
256 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
258 // Add the rest of the permissions.
259 permissions
= new PermissionSet(
260 api_perm_set2_
, empty_manifest_permissions
,
261 ehost_perm_set2_
, shost_perm_set2_
);
263 APIPermissionSet::Union(expected_apis
, api_perm_set2_
, &api_permissions_
);
265 prefs()->AddGrantedPermissions(extension_id_
, permissions
.get());
266 granted_permissions
= prefs()->GetGrantedPermissions(extension_id_
);
267 EXPECT_TRUE(granted_permissions
.get());
268 EXPECT_FALSE(granted_permissions
->IsEmpty());
269 EXPECT_EQ(api_permissions_
, granted_permissions
->apis());
270 EXPECT_EQ(ehost_permissions_
,
271 granted_permissions
->explicit_hosts());
272 EXPECT_EQ(shost_permissions_
,
273 granted_permissions
->scriptable_hosts());
274 effective_permissions_
.ClearPatterns();
275 URLPatternSet::CreateUnion(ehost_permissions_
, shost_permissions_
,
276 &effective_permissions_
);
277 EXPECT_EQ(effective_permissions_
, granted_permissions
->effective_hosts());
280 void Verify() override
{
281 scoped_refptr
<PermissionSet
> permissions(
282 prefs()->GetGrantedPermissions(extension_id_
));
283 EXPECT_TRUE(permissions
.get());
284 EXPECT_FALSE(permissions
->HasEffectiveFullAccess());
285 EXPECT_EQ(api_permissions_
, permissions
->apis());
286 EXPECT_EQ(ehost_permissions_
,
287 permissions
->explicit_hosts());
288 EXPECT_EQ(shost_permissions_
,
289 permissions
->scriptable_hosts());
293 std::string extension_id_
;
294 APIPermissionSet api_perm_set1_
;
295 APIPermissionSet api_perm_set2_
;
296 URLPatternSet ehost_perm_set1_
;
297 URLPatternSet ehost_perm_set2_
;
298 URLPatternSet shost_perm_set1_
;
299 URLPatternSet shost_perm_set2_
;
301 APIPermissionSet api_permissions_
;
302 URLPatternSet ehost_permissions_
;
303 URLPatternSet shost_permissions_
;
304 URLPatternSet effective_permissions_
;
306 TEST_F(ExtensionPrefsGrantedPermissions
, GrantedPermissions
) {}
308 // Tests the SetActivePermissions / GetActivePermissions functions.
309 class ExtensionPrefsActivePermissions
: public ExtensionPrefsTest
{
311 void Initialize() override
{
312 extension_id_
= prefs_
.AddExtensionAndReturnId("test");
314 APIPermissionSet api_perms
;
315 api_perms
.insert(APIPermission::kTab
);
316 api_perms
.insert(APIPermission::kBookmark
);
317 api_perms
.insert(APIPermission::kHistory
);
319 ManifestPermissionSet empty_manifest_permissions
;
321 URLPatternSet ehosts
;
322 AddPattern(&ehosts
, "http://*.google.com/*");
323 AddPattern(&ehosts
, "http://example.com/*");
324 AddPattern(&ehosts
, "chrome://favicon/*");
326 URLPatternSet shosts
;
327 AddPattern(&shosts
, "https://*.google.com/*");
328 AddPattern(&shosts
, "http://reddit.com/r/test/*");
330 active_perms_
= new PermissionSet(
331 api_perms
, empty_manifest_permissions
, ehosts
, shosts
);
333 // Make sure the active permissions start empty.
334 scoped_refptr
<PermissionSet
> active(
335 prefs()->GetActivePermissions(extension_id_
));
336 EXPECT_TRUE(active
->IsEmpty());
338 // Set the active permissions.
339 prefs()->SetActivePermissions(extension_id_
, active_perms_
.get());
340 active
= prefs()->GetActivePermissions(extension_id_
);
341 EXPECT_EQ(active_perms_
->apis(), active
->apis());
342 EXPECT_EQ(active_perms_
->explicit_hosts(), active
->explicit_hosts());
343 EXPECT_EQ(active_perms_
->scriptable_hosts(), active
->scriptable_hosts());
344 EXPECT_EQ(*active_perms_
.get(), *active
.get());
347 void Verify() override
{
348 scoped_refptr
<PermissionSet
> permissions(
349 prefs()->GetActivePermissions(extension_id_
));
350 EXPECT_EQ(*active_perms_
.get(), *permissions
.get());
354 std::string extension_id_
;
355 scoped_refptr
<PermissionSet
> active_perms_
;
357 TEST_F(ExtensionPrefsActivePermissions
, SetAndGetActivePermissions
) {}
359 // Tests the GetVersionString function.
360 class ExtensionPrefsVersionString
: public ExtensionPrefsTest
{
362 void Initialize() override
{
363 extension
= prefs_
.AddExtension("test");
364 EXPECT_EQ("0.1", prefs()->GetVersionString(extension
->id()));
365 prefs()->OnExtensionUninstalled(extension
->id(),
366 Manifest::INTERNAL
, false);
369 void Verify() override
{
370 EXPECT_EQ("", prefs()->GetVersionString(extension
->id()));
374 scoped_refptr
<Extension
> extension
;
376 TEST_F(ExtensionPrefsVersionString
, VersionString
) {}
378 class ExtensionPrefsAcknowledgment
: public ExtensionPrefsTest
{
380 void Initialize() override
{
381 not_installed_id_
= "pghjnghklobnfoidcldiidjjjhkeeaoi";
383 // Install some extensions.
384 for (int i
= 0; i
< 5; i
++) {
385 std::string name
= "test" + base::IntToString(i
);
386 extensions_
.push_back(prefs_
.AddExtension(name
));
389 prefs()->GetInstalledExtensionInfo(not_installed_id_
).get());
391 ExtensionList::const_iterator iter
;
392 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
393 std::string id
= (*iter
)->id();
394 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
395 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
396 if (external_id_
.empty()) {
400 if (blacklisted_id_
.empty()) {
401 blacklisted_id_
= id
;
405 // For each type of acknowledgment, acknowledge one installed and one
406 // not-installed extension id.
407 prefs()->AcknowledgeExternalExtension(external_id_
);
408 prefs()->AcknowledgeBlacklistedExtension(blacklisted_id_
);
409 prefs()->AcknowledgeExternalExtension(not_installed_id_
);
410 prefs()->AcknowledgeBlacklistedExtension(not_installed_id_
);
413 void Verify() override
{
414 ExtensionList::const_iterator iter
;
415 for (iter
= extensions_
.begin(); iter
!= extensions_
.end(); ++iter
) {
416 std::string id
= (*iter
)->id();
417 if (id
== external_id_
) {
418 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(id
));
420 EXPECT_FALSE(prefs()->IsExternalExtensionAcknowledged(id
));
422 if (id
== blacklisted_id_
) {
423 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
425 EXPECT_FALSE(prefs()->IsBlacklistedExtensionAcknowledged(id
));
428 EXPECT_TRUE(prefs()->IsExternalExtensionAcknowledged(not_installed_id_
));
429 EXPECT_TRUE(prefs()->IsBlacklistedExtensionAcknowledged(not_installed_id_
));
433 ExtensionList extensions_
;
435 std::string not_installed_id_
;
436 std::string external_id_
;
437 std::string blacklisted_id_
;
439 TEST_F(ExtensionPrefsAcknowledgment
, Acknowledgment
) {}
441 // Tests the idle install information functions.
442 class ExtensionPrefsDelayedInstallInfo
: public ExtensionPrefsTest
{
444 // Sets idle install information for one test extension.
445 void SetIdleInfo(const std::string
& id
, int num
) {
446 base::DictionaryValue manifest
;
447 manifest
.SetString(manifest_keys::kName
, "test");
448 manifest
.SetString(manifest_keys::kVersion
, "1." + base::IntToString(num
));
449 base::FilePath path
=
450 prefs_
.extensions_dir().AppendASCII(base::IntToString(num
));
452 scoped_refptr
<Extension
> extension
= Extension::Create(
453 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id
, &errors
);
454 ASSERT_TRUE(extension
.get()) << errors
;
455 ASSERT_EQ(id
, extension
->id());
456 prefs()->SetDelayedInstallInfo(extension
.get(),
459 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
460 syncer::StringOrdinal(),
464 // Verifies that we get back expected idle install information previously
465 // set by SetIdleInfo.
466 void VerifyIdleInfo(const std::string
& id
, int num
) {
467 scoped_ptr
<ExtensionInfo
> info(prefs()->GetDelayedInstallInfo(id
));
470 ASSERT_TRUE(info
->extension_manifest
->GetString("version", &version
));
471 ASSERT_EQ("1." + base::IntToString(num
), version
);
472 ASSERT_EQ(base::IntToString(num
),
473 info
->extension_path
.BaseName().MaybeAsASCII());
476 bool HasInfoForId(ExtensionPrefs::ExtensionsInfo
* info
,
477 const std::string
& id
) {
478 for (size_t i
= 0; i
< info
->size(); ++i
) {
479 if (info
->at(i
)->extension_id
== id
)
485 void Initialize() override
{
486 PathService::Get(chrome::DIR_TEST_DATA
, &basedir_
);
488 id1_
= prefs_
.AddExtensionAndReturnId("1");
489 id2_
= prefs_
.AddExtensionAndReturnId("2");
490 id3_
= prefs_
.AddExtensionAndReturnId("3");
491 id4_
= prefs_
.AddExtensionAndReturnId("4");
493 // Set info for two extensions, then remove it.
494 SetIdleInfo(id1_
, 1);
495 SetIdleInfo(id2_
, 2);
496 VerifyIdleInfo(id1_
, 1);
497 VerifyIdleInfo(id2_
, 2);
498 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
499 prefs()->GetAllDelayedInstallInfo());
500 EXPECT_EQ(2u, info
->size());
501 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
502 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
503 prefs()->RemoveDelayedInstallInfo(id1_
);
504 prefs()->RemoveDelayedInstallInfo(id2_
);
505 info
= prefs()->GetAllDelayedInstallInfo();
506 EXPECT_TRUE(info
->empty());
508 // Try getting/removing info for an id that used to have info set.
509 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id1_
));
510 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id1_
));
512 // Try getting/removing info for an id that has not yet had any info set.
513 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
514 EXPECT_FALSE(prefs()->RemoveDelayedInstallInfo(id3_
));
516 // Set info for 4 extensions, then remove for one of them.
517 SetIdleInfo(id1_
, 1);
518 SetIdleInfo(id2_
, 2);
519 SetIdleInfo(id3_
, 3);
520 SetIdleInfo(id4_
, 4);
521 VerifyIdleInfo(id1_
, 1);
522 VerifyIdleInfo(id2_
, 2);
523 VerifyIdleInfo(id3_
, 3);
524 VerifyIdleInfo(id4_
, 4);
525 prefs()->RemoveDelayedInstallInfo(id3_
);
528 void Verify() override
{
529 // Make sure the info for the 3 extensions we expect is present.
530 scoped_ptr
<ExtensionPrefs::ExtensionsInfo
> info(
531 prefs()->GetAllDelayedInstallInfo());
532 EXPECT_EQ(3u, info
->size());
533 EXPECT_TRUE(HasInfoForId(info
.get(), id1_
));
534 EXPECT_TRUE(HasInfoForId(info
.get(), id2_
));
535 EXPECT_TRUE(HasInfoForId(info
.get(), id4_
));
536 VerifyIdleInfo(id1_
, 1);
537 VerifyIdleInfo(id2_
, 2);
538 VerifyIdleInfo(id4_
, 4);
540 // Make sure there isn't info the for the one extension id we removed.
541 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id3_
));
546 base::FilePath basedir_
;
552 TEST_F(ExtensionPrefsDelayedInstallInfo
, DelayedInstallInfo
) {}
554 // Tests the FinishDelayedInstallInfo function.
555 class ExtensionPrefsFinishDelayedInstallInfo
: public ExtensionPrefsTest
{
557 void Initialize() override
{
558 base::DictionaryValue dictionary
;
559 dictionary
.SetString(manifest_keys::kName
, "test");
560 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
561 dictionary
.SetString(manifest_keys::kBackgroundPage
, "background.html");
562 scoped_refptr
<Extension
> extension
=
563 prefs_
.AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
564 id_
= extension
->id();
568 base::DictionaryValue manifest
;
569 manifest
.SetString(manifest_keys::kName
, "test");
570 manifest
.SetString(manifest_keys::kVersion
, "0.2");
571 scoped_ptr
<base::ListValue
> scripts(new base::ListValue
);
572 scripts
->AppendString("test.js");
573 manifest
.Set(manifest_keys::kBackgroundScripts
, scripts
.release());
574 base::FilePath path
=
575 prefs_
.extensions_dir().AppendASCII("test_0.2");
577 scoped_refptr
<Extension
> new_extension
= Extension::Create(
578 path
, Manifest::INTERNAL
, manifest
, Extension::NO_FLAGS
, id_
, &errors
);
579 ASSERT_TRUE(new_extension
.get()) << errors
;
580 ASSERT_EQ(id_
, new_extension
->id());
581 prefs()->SetDelayedInstallInfo(new_extension
.get(),
584 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE
,
585 syncer::StringOrdinal(),
588 // Finish idle installation
589 ASSERT_TRUE(prefs()->FinishDelayedInstallInfo(id_
));
592 void Verify() override
{
593 EXPECT_FALSE(prefs()->GetDelayedInstallInfo(id_
));
594 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(id_
));
596 const base::DictionaryValue
* manifest
;
597 ASSERT_TRUE(prefs()->ReadPrefAsDictionary(id_
, "manifest", &manifest
));
598 ASSERT_TRUE(manifest
);
600 EXPECT_TRUE(manifest
->GetString(manifest_keys::kName
, &value
));
601 EXPECT_EQ("test", value
);
602 EXPECT_TRUE(manifest
->GetString(manifest_keys::kVersion
, &value
));
603 EXPECT_EQ("0.2", value
);
604 EXPECT_FALSE(manifest
->GetString(manifest_keys::kBackgroundPage
, &value
));
605 const base::ListValue
* scripts
;
606 ASSERT_TRUE(manifest
->GetList(manifest_keys::kBackgroundScripts
, &scripts
));
607 EXPECT_EQ(1u, scripts
->GetSize());
613 TEST_F(ExtensionPrefsFinishDelayedInstallInfo
, FinishDelayedInstallInfo
) {}
615 class ExtensionPrefsOnExtensionInstalled
: public ExtensionPrefsTest
{
617 void Initialize() override
{
618 extension_
= prefs_
.AddExtension("on_extension_installed");
619 EXPECT_FALSE(prefs()->IsExtensionDisabled(extension_
->id()));
620 prefs()->OnExtensionInstalled(extension_
.get(),
622 syncer::StringOrdinal(),
626 void Verify() override
{
627 EXPECT_TRUE(prefs()->IsExtensionDisabled(extension_
->id()));
628 EXPECT_EQ(std::string("Param"), prefs()->GetInstallParam(extension_
->id()));
632 scoped_refptr
<Extension
> extension_
;
634 TEST_F(ExtensionPrefsOnExtensionInstalled
,
635 ExtensionPrefsOnExtensionInstalled
) {}
637 class ExtensionPrefsAppDraggedByUser
: public ExtensionPrefsTest
{
639 void Initialize() override
{
640 extension_
= prefs_
.AddExtension("on_extension_installed");
641 EXPECT_FALSE(prefs()->WasAppDraggedByUser(extension_
->id()));
642 prefs()->OnExtensionInstalled(extension_
.get(),
644 syncer::StringOrdinal(),
648 void Verify() override
{
649 // Set the flag and see if it persisted.
650 prefs()->SetAppDraggedByUser(extension_
->id());
651 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
653 // Make sure it doesn't change on consecutive calls.
654 prefs()->SetAppDraggedByUser(extension_
->id());
655 EXPECT_TRUE(prefs()->WasAppDraggedByUser(extension_
->id()));
659 scoped_refptr
<Extension
> extension_
;
661 TEST_F(ExtensionPrefsAppDraggedByUser
, ExtensionPrefsAppDraggedByUser
) {}
663 class ExtensionPrefsFlags
: public ExtensionPrefsTest
{
665 void Initialize() override
{
667 base::DictionaryValue dictionary
;
668 dictionary
.SetString(manifest_keys::kName
, "from_webstore");
669 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
670 webstore_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
671 dictionary
, Manifest::INTERNAL
, Extension::FROM_WEBSTORE
);
675 base::DictionaryValue dictionary
;
676 dictionary
.SetString(manifest_keys::kName
, "from_bookmark");
677 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
678 bookmark_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
679 dictionary
, Manifest::INTERNAL
, Extension::FROM_BOOKMARK
);
683 base::DictionaryValue dictionary
;
684 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_default");
685 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
686 default_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
689 Extension::WAS_INSTALLED_BY_DEFAULT
);
693 base::DictionaryValue dictionary
;
694 dictionary
.SetString(manifest_keys::kName
, "was_installed_by_oem");
695 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
696 oem_extension_
= prefs_
.AddExtensionWithManifestAndFlags(
697 dictionary
, Manifest::INTERNAL
, Extension::WAS_INSTALLED_BY_OEM
);
701 void Verify() override
{
702 EXPECT_TRUE(prefs()->IsFromWebStore(webstore_extension_
->id()));
703 EXPECT_FALSE(prefs()->IsFromBookmark(webstore_extension_
->id()));
705 EXPECT_TRUE(prefs()->IsFromBookmark(bookmark_extension_
->id()));
706 EXPECT_FALSE(prefs()->IsFromWebStore(bookmark_extension_
->id()));
708 EXPECT_TRUE(prefs()->WasInstalledByDefault(default_extension_
->id()));
709 EXPECT_TRUE(prefs()->WasInstalledByOem(oem_extension_
->id()));
713 scoped_refptr
<Extension
> webstore_extension_
;
714 scoped_refptr
<Extension
> bookmark_extension_
;
715 scoped_refptr
<Extension
> default_extension_
;
716 scoped_refptr
<Extension
> oem_extension_
;
718 TEST_F(ExtensionPrefsFlags
, ExtensionPrefsFlags
) {}
720 PrefsPrepopulatedTestBase::PrefsPrepopulatedTestBase()
721 : ExtensionPrefsTest() {
722 base::DictionaryValue simple_dict
;
725 simple_dict
.SetString(manifest_keys::kVersion
, "1.0.0.0");
726 simple_dict
.SetString(manifest_keys::kName
, "unused");
728 extension1_
= Extension::Create(
729 prefs_
.temp_dir().AppendASCII("ext1_"),
730 Manifest::EXTERNAL_PREF
,
734 extension2_
= Extension::Create(
735 prefs_
.temp_dir().AppendASCII("ext2_"),
736 Manifest::EXTERNAL_PREF
,
740 extension3_
= Extension::Create(
741 prefs_
.temp_dir().AppendASCII("ext3_"),
742 Manifest::EXTERNAL_PREF
,
746 extension4_
= Extension::Create(
747 prefs_
.temp_dir().AppendASCII("ext4_"),
748 Manifest::EXTERNAL_PREF
,
753 for (size_t i
= 0; i
< kNumInstalledExtensions
; ++i
)
754 installed_
[i
] = false;
757 PrefsPrepopulatedTestBase::~PrefsPrepopulatedTestBase() {
760 // Tests that blacklist state can be queried.
761 class ExtensionPrefsBlacklistedExtensions
: public ExtensionPrefsTest
{
763 ~ExtensionPrefsBlacklistedExtensions() override
{}
765 void Initialize() override
{
766 extension_a_
= prefs_
.AddExtension("a");
767 extension_b_
= prefs_
.AddExtension("b");
768 extension_c_
= prefs_
.AddExtension("c");
771 void Verify() override
{
774 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
776 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
779 ids
.insert(extension_a_
->id());
780 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
782 prefs()->SetExtensionBlacklisted(extension_b_
->id(), true);
783 prefs()->SetExtensionBlacklisted(extension_c_
->id(), true);
786 ids
.insert(extension_a_
->id());
787 ids
.insert(extension_b_
->id());
788 ids
.insert(extension_c_
->id());
789 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
791 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
794 ids
.insert(extension_b_
->id());
795 ids
.insert(extension_c_
->id());
796 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
798 prefs()->SetExtensionBlacklisted(extension_b_
->id(), false);
799 prefs()->SetExtensionBlacklisted(extension_c_
->id(), false);
802 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
805 // The interesting part: make sure that we're cleaning up after ourselves
806 // when we're storing *just* the fact that the extension is blacklisted.
807 std::string arbitrary_id
= "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
809 prefs()->SetExtensionBlacklisted(arbitrary_id
, true);
810 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
812 // (And make sure that the acknowledged bit is also cleared).
813 prefs()->AcknowledgeBlacklistedExtension(arbitrary_id
);
815 EXPECT_TRUE(prefs()->GetExtensionPref(arbitrary_id
));
818 ids
.insert(arbitrary_id
);
819 ids
.insert(extension_a_
->id());
820 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
822 prefs()->SetExtensionBlacklisted(arbitrary_id
, false);
823 prefs()->SetExtensionBlacklisted(extension_a_
->id(), false);
824 EXPECT_FALSE(prefs()->GetExtensionPref(arbitrary_id
));
827 EXPECT_EQ(ids
, prefs()->GetBlacklistedExtensions());
832 scoped_refptr
<const Extension
> extension_a_
;
833 scoped_refptr
<const Extension
> extension_b_
;
834 scoped_refptr
<const Extension
> extension_c_
;
836 TEST_F(ExtensionPrefsBlacklistedExtensions
,
837 ExtensionPrefsBlacklistedExtensions
) {}
839 // Tests the blacklist state. Old "blacklist" preference should take precedence
840 // over new "blacklist_state".
841 class ExtensionPrefsBlacklistState
: public ExtensionPrefsTest
{
843 ~ExtensionPrefsBlacklistState() override
{}
845 void Initialize() override
{ extension_a_
= prefs_
.AddExtension("a"); }
847 void Verify() override
{
848 ExtensionIdSet empty_ids
;
849 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
851 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
852 EXPECT_EQ(BLACKLISTED_MALWARE
,
853 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
855 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
856 BLACKLISTED_POTENTIALLY_UNWANTED
);
857 EXPECT_EQ(BLACKLISTED_POTENTIALLY_UNWANTED
,
858 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
859 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
860 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
862 prefs()->SetExtensionBlacklisted(extension_a_
->id(), true);
863 EXPECT_TRUE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
864 EXPECT_EQ(BLACKLISTED_MALWARE
,
865 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
866 EXPECT_EQ(1u, prefs()->GetBlacklistedExtensions().size());
868 prefs()->SetExtensionBlacklistState(extension_a_
->id(),
870 EXPECT_EQ(NOT_BLACKLISTED
,
871 prefs()->GetExtensionBlacklistState(extension_a_
->id()));
872 EXPECT_FALSE(prefs()->IsExtensionBlacklisted(extension_a_
->id()));
873 EXPECT_EQ(empty_ids
, prefs()->GetBlacklistedExtensions());
877 scoped_refptr
<const Extension
> extension_a_
;
879 TEST_F(ExtensionPrefsBlacklistState
, ExtensionPrefsBlacklistState
) {}
881 // Tests clearing the last launched preference.
882 class ExtensionPrefsClearLastLaunched
: public ExtensionPrefsTest
{
884 ~ExtensionPrefsClearLastLaunched() override
{}
886 void Initialize() override
{
887 extension_a_
= prefs_
.AddExtension("a");
888 extension_b_
= prefs_
.AddExtension("b");
891 void Verify() override
{
892 // Set last launched times for each extension.
893 prefs()->SetLastLaunchTime(extension_a_
->id(), base::Time::Now());
894 prefs()->SetLastLaunchTime(extension_b_
->id(), base::Time::Now());
896 // Also set some other preference for one of the extensions.
897 prefs()->SetAllowFileAccess(extension_a_
->id(), true);
899 // Now clear the launch times.
900 prefs()->ClearLastLaunchTimes();
902 // All launch times should be gone.
903 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_a_
->id()));
904 EXPECT_EQ(base::Time(), prefs()->GetLastLaunchTime(extension_b_
->id()));
906 // Other preferences should be untouched.
907 EXPECT_TRUE(prefs()->AllowFileAccess(extension_a_
->id()));
911 scoped_refptr
<const Extension
> extension_a_
;
912 scoped_refptr
<const Extension
> extension_b_
;
914 TEST_F(ExtensionPrefsClearLastLaunched
, ExtensionPrefsClearLastLaunched
) {}
916 } // namespace extensions