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/test_extension_prefs.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/json_pref_store.h"
13 #include "base/prefs/pref_value_store.h"
14 #include "base/run_loop.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/values.h"
19 #include "chrome/browser/prefs/pref_service_mock_factory.h"
20 #include "chrome/browser/prefs/pref_service_syncable.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "components/crx_file/id_util.h"
23 #include "components/pref_registry/pref_registry_syncable.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "extensions/browser/extension_pref_store.h"
26 #include "extensions/browser/extension_pref_value_map.h"
27 #include "extensions/browser/extension_prefs.h"
28 #include "extensions/browser/extensions_browser_client.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/manifest_constants.h"
31 #include "sync/api/string_ordinal.h"
32 #include "testing/gtest/include/gtest/gtest.h"
34 using content::BrowserThread
;
36 namespace extensions
{
40 // A TimeProvider which returns an incrementally later time each time
41 // GetCurrentTime is called.
42 class IncrementalTimeProvider
: public ExtensionPrefs::TimeProvider
{
44 IncrementalTimeProvider() : current_time_(base::Time::Now()) {
47 ~IncrementalTimeProvider() override
{}
49 base::Time
GetCurrentTime() const override
{
50 current_time_
+= base::TimeDelta::FromSeconds(10);
55 DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider
);
57 mutable base::Time current_time_
;
62 TestExtensionPrefs::TestExtensionPrefs(
63 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
)
64 : task_runner_(task_runner
), extensions_disabled_(false) {
65 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
66 preferences_file_
= temp_dir_
.path().Append(chrome::kPreferencesFilename
);
67 extensions_dir_
= temp_dir_
.path().AppendASCII("Extensions");
68 EXPECT_TRUE(base::CreateDirectory(extensions_dir_
));
71 RecreateExtensionPrefs();
74 TestExtensionPrefs::~TestExtensionPrefs() {
77 PrefService
* TestExtensionPrefs::pref_service() {
78 return pref_service_
.get();
81 const scoped_refptr
<user_prefs::PrefRegistrySyncable
>&
82 TestExtensionPrefs::pref_registry() {
83 return pref_registry_
;
86 void TestExtensionPrefs::ResetPrefRegistry() {
87 pref_registry_
= new user_prefs::PrefRegistrySyncable
;
88 ExtensionPrefs::RegisterProfilePrefs(pref_registry_
.get());
91 void TestExtensionPrefs::RecreateExtensionPrefs() {
92 // We persist and reload the PrefService's PrefStores because this process
93 // deletes all empty dictionaries. The ExtensionPrefs implementation
94 // needs to be able to handle this situation.
96 // Commit a pending write (which posts a task to task_runner_) and wait for
98 pref_service_
->CommitPendingWrite();
99 base::RunLoop run_loop
;
101 task_runner_
->PostTaskAndReply(
103 base::Bind(&base::DoNothing
),
104 run_loop
.QuitClosure()));
108 extension_pref_value_map_
.reset(new ExtensionPrefValueMap
);
109 PrefServiceMockFactory factory
;
110 factory
.SetUserPrefsFile(preferences_file_
, task_runner_
.get());
111 factory
.set_extension_prefs(
112 new ExtensionPrefStore(extension_pref_value_map_
.get(), false));
113 pref_service_
= factory
.CreateSyncable(pref_registry_
.get()).Pass();
115 prefs_
.reset(ExtensionPrefs::Create(
118 extension_pref_value_map_
.get(),
119 ExtensionsBrowserClient::Get()->CreateAppSorting(nullptr).Pass(),
120 extensions_disabled_
,
121 std::vector
<ExtensionPrefsObserver
*>(),
122 // Guarantee that no two extensions get the same installation time
123 // stamp and we can reliably assert the installation order in the tests.
124 scoped_ptr
<ExtensionPrefs::TimeProvider
>(new IncrementalTimeProvider())));
127 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtension(
128 const std::string
& name
) {
129 base::DictionaryValue dictionary
;
130 dictionary
.SetString(manifest_keys::kName
, name
);
131 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
132 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
135 scoped_refptr
<Extension
> TestExtensionPrefs::AddApp(const std::string
& name
) {
136 base::DictionaryValue dictionary
;
137 dictionary
.SetString(manifest_keys::kName
, name
);
138 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
139 dictionary
.SetString(manifest_keys::kApp
, "true");
140 dictionary
.SetString(manifest_keys::kLaunchWebURL
, "http://example.com");
141 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
145 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifest(
146 const base::DictionaryValue
& manifest
, Manifest::Location location
) {
147 return AddExtensionWithManifestAndFlags(manifest
, location
,
148 Extension::NO_FLAGS
);
151 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
152 const base::DictionaryValue
& manifest
,
153 Manifest::Location location
,
156 EXPECT_TRUE(manifest
.GetString(manifest_keys::kName
, &name
));
157 base::FilePath path
= extensions_dir_
.AppendASCII(name
);
159 scoped_refptr
<Extension
> extension
= Extension::Create(
160 path
, location
, manifest
, extra_flags
, &errors
);
161 EXPECT_TRUE(extension
.get()) << errors
;
162 if (!extension
.get())
165 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension
->id()));
166 prefs_
->OnExtensionInstalled(extension
.get(),
168 syncer::StringOrdinal::CreateInitialOrdinal(),
173 std::string
TestExtensionPrefs::AddExtensionAndReturnId(
174 const std::string
& name
) {
175 scoped_refptr
<Extension
> extension(AddExtension(name
));
176 return extension
->id();
179 void TestExtensionPrefs::AddExtension(Extension
* extension
) {
180 prefs_
->OnExtensionInstalled(extension
,
182 syncer::StringOrdinal::CreateInitialOrdinal(),
186 PrefService
* TestExtensionPrefs::CreateIncognitoPrefService() const {
187 return pref_service_
->CreateIncognitoPrefService(
188 new ExtensionPrefStore(extension_pref_value_map_
.get(), true));
191 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled
) {
192 extensions_disabled_
= extensions_disabled
;
195 } // namespace extensions