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/message_loop/message_loop_proxy.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/prefs/pref_value_store.h"
15 #include "base/run_loop.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/synchronization/waitable_event.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 virtual ~IncrementalTimeProvider() {
50 virtual base::Time
GetCurrentTime() const override
{
51 current_time_
+= base::TimeDelta::FromSeconds(10);
56 DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider
);
58 mutable base::Time current_time_
;
63 TestExtensionPrefs::TestExtensionPrefs(
64 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
)
65 : task_runner_(task_runner
), extensions_disabled_(false) {
66 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
67 preferences_file_
= temp_dir_
.path().Append(chrome::kPreferencesFilename
);
68 extensions_dir_
= temp_dir_
.path().AppendASCII("Extensions");
69 EXPECT_TRUE(base::CreateDirectory(extensions_dir_
));
72 RecreateExtensionPrefs();
75 TestExtensionPrefs::~TestExtensionPrefs() {
78 PrefService
* TestExtensionPrefs::pref_service() {
79 return pref_service_
.get();
82 const scoped_refptr
<user_prefs::PrefRegistrySyncable
>&
83 TestExtensionPrefs::pref_registry() {
84 return pref_registry_
;
87 void TestExtensionPrefs::ResetPrefRegistry() {
88 pref_registry_
= new user_prefs::PrefRegistrySyncable
;
89 ExtensionPrefs::RegisterProfilePrefs(pref_registry_
.get());
92 void TestExtensionPrefs::RecreateExtensionPrefs() {
93 // We persist and reload the PrefService's PrefStores because this process
94 // deletes all empty dictionaries. The ExtensionPrefs implementation
95 // needs to be able to handle this situation.
97 // Commit a pending write (which posts a task to task_runner_) and wait for
99 pref_service_
->CommitPendingWrite();
100 base::RunLoop run_loop
;
102 task_runner_
->PostTaskAndReply(
104 base::Bind(&base::DoNothing
),
105 run_loop
.QuitClosure()));
109 extension_pref_value_map_
.reset(new ExtensionPrefValueMap
);
110 PrefServiceMockFactory factory
;
111 factory
.SetUserPrefsFile(preferences_file_
, task_runner_
.get());
112 factory
.set_extension_prefs(
113 new ExtensionPrefStore(extension_pref_value_map_
.get(), false));
114 pref_service_
= factory
.CreateSyncable(pref_registry_
.get()).Pass();
116 prefs_
.reset(ExtensionPrefs::Create(
119 extension_pref_value_map_
.get(),
120 ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
121 extensions_disabled_
,
122 std::vector
<ExtensionPrefsObserver
*>(),
123 // Guarantee that no two extensions get the same installation time
124 // stamp and we can reliably assert the installation order in the tests.
125 scoped_ptr
<ExtensionPrefs::TimeProvider
>(new IncrementalTimeProvider())));
128 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtension(
129 const std::string
& name
) {
130 base::DictionaryValue dictionary
;
131 dictionary
.SetString(manifest_keys::kName
, name
);
132 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
133 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
136 scoped_refptr
<Extension
> TestExtensionPrefs::AddApp(const std::string
& name
) {
137 base::DictionaryValue dictionary
;
138 dictionary
.SetString(manifest_keys::kName
, name
);
139 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
140 dictionary
.SetString(manifest_keys::kApp
, "true");
141 dictionary
.SetString(manifest_keys::kLaunchWebURL
, "http://example.com");
142 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
146 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifest(
147 const base::DictionaryValue
& manifest
, Manifest::Location location
) {
148 return AddExtensionWithManifestAndFlags(manifest
, location
,
149 Extension::NO_FLAGS
);
152 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
153 const base::DictionaryValue
& manifest
,
154 Manifest::Location location
,
157 EXPECT_TRUE(manifest
.GetString(manifest_keys::kName
, &name
));
158 base::FilePath path
= extensions_dir_
.AppendASCII(name
);
160 scoped_refptr
<Extension
> extension
= Extension::Create(
161 path
, location
, manifest
, extra_flags
, &errors
);
162 EXPECT_TRUE(extension
.get()) << errors
;
163 if (!extension
.get())
166 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension
->id()));
167 prefs_
->OnExtensionInstalled(extension
.get(),
169 syncer::StringOrdinal::CreateInitialOrdinal(),
174 std::string
TestExtensionPrefs::AddExtensionAndReturnId(
175 const std::string
& name
) {
176 scoped_refptr
<Extension
> extension(AddExtension(name
));
177 return extension
->id();
180 PrefService
* TestExtensionPrefs::CreateIncognitoPrefService() const {
181 return pref_service_
->CreateIncognitoPrefService(
182 new ExtensionPrefStore(extension_pref_value_map_
.get(), true));
185 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled
) {
186 extensions_disabled_
= extensions_disabled
;
189 } // namespace extensions