Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / extensions / test_extension_prefs.cc
blobbdd5dab8c1c855f0f45c42a2ea70735c1ea030b0
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"
7 #include "base/bind.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/extensions/chrome_app_sorting.h"
20 #include "chrome/browser/extensions/test_extension_system.h"
21 #include "chrome/browser/prefs/pref_service_syncable_util.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "components/crx_file/id_util.h"
24 #include "components/pref_registry/pref_registry_syncable.h"
25 #include "components/syncable_prefs/pref_service_mock_factory.h"
26 #include "components/syncable_prefs/pref_service_syncable.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "extensions/browser/extension_pref_store.h"
29 #include "extensions/browser/extension_pref_value_map.h"
30 #include "extensions/browser/extension_prefs.h"
31 #include "extensions/browser/extension_prefs_factory.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/extensions_browser_client.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/manifest_constants.h"
36 #include "sync/api/string_ordinal.h"
37 #include "testing/gtest/include/gtest/gtest.h"
39 using content::BrowserThread;
41 namespace extensions {
43 namespace {
45 // A TimeProvider which returns an incrementally later time each time
46 // GetCurrentTime is called.
47 class IncrementalTimeProvider : public ExtensionPrefs::TimeProvider {
48 public:
49 IncrementalTimeProvider() : current_time_(base::Time::Now()) {
52 ~IncrementalTimeProvider() override {}
54 base::Time GetCurrentTime() const override {
55 current_time_ += base::TimeDelta::FromSeconds(10);
56 return current_time_;
59 private:
60 DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider);
62 mutable base::Time current_time_;
65 } // namespace
67 TestExtensionPrefs::TestExtensionPrefs(
68 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
69 : task_runner_(task_runner), extensions_disabled_(false) {
70 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
71 preferences_file_ = temp_dir_.path().Append(chrome::kPreferencesFilename);
72 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
73 EXPECT_TRUE(base::CreateDirectory(extensions_dir_));
75 ResetPrefRegistry();
76 RecreateExtensionPrefs();
79 TestExtensionPrefs::~TestExtensionPrefs() {
82 ExtensionPrefs* TestExtensionPrefs::prefs() {
83 return ExtensionPrefs::Get(&profile_);
86 PrefService* TestExtensionPrefs::pref_service() {
87 return pref_service_.get();
90 const scoped_refptr<user_prefs::PrefRegistrySyncable>&
91 TestExtensionPrefs::pref_registry() {
92 return pref_registry_;
95 void TestExtensionPrefs::ResetPrefRegistry() {
96 pref_registry_ = new user_prefs::PrefRegistrySyncable;
97 ExtensionPrefs::RegisterProfilePrefs(pref_registry_.get());
100 void TestExtensionPrefs::RecreateExtensionPrefs() {
101 // We persist and reload the PrefService's PrefStores because this process
102 // deletes all empty dictionaries. The ExtensionPrefs implementation
103 // needs to be able to handle this situation.
104 if (pref_service_) {
105 // Commit a pending write (which posts a task to task_runner_) and wait for
106 // it to finish.
107 pref_service_->CommitPendingWrite();
108 base::RunLoop run_loop;
109 ASSERT_TRUE(
110 task_runner_->PostTaskAndReply(
111 FROM_HERE,
112 base::Bind(&base::DoNothing),
113 run_loop.QuitClosure()));
114 run_loop.Run();
117 extension_pref_value_map_.reset(new ExtensionPrefValueMap);
118 syncable_prefs::PrefServiceMockFactory factory;
119 factory.SetUserPrefsFile(preferences_file_, task_runner_.get());
120 factory.set_extension_prefs(
121 new ExtensionPrefStore(extension_pref_value_map_.get(), false));
122 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass();
123 scoped_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create(
124 &profile_,
125 pref_service_.get(),
126 temp_dir_.path(),
127 extension_pref_value_map_.get(),
128 extensions_disabled_,
129 std::vector<ExtensionPrefsObserver*>(),
130 // Guarantee that no two extensions get the same installation time
131 // stamp and we can reliably assert the installation order in the tests.
132 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider())));
133 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_,
134 prefs.Pass());
135 // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be
136 // recreated. (ExtensionPrefs is never recreated in non-test code.)
137 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_))
138 ->RecreateAppSorting();
141 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(
142 const std::string& name) {
143 base::DictionaryValue dictionary;
144 dictionary.SetString(manifest_keys::kName, name);
145 dictionary.SetString(manifest_keys::kVersion, "0.1");
146 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
149 scoped_refptr<Extension> TestExtensionPrefs::AddApp(const std::string& name) {
150 base::DictionaryValue dictionary;
151 dictionary.SetString(manifest_keys::kName, name);
152 dictionary.SetString(manifest_keys::kVersion, "0.1");
153 dictionary.SetString(manifest_keys::kApp, "true");
154 dictionary.SetString(manifest_keys::kLaunchWebURL, "http://example.com");
155 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
159 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest(
160 const base::DictionaryValue& manifest, Manifest::Location location) {
161 return AddExtensionWithManifestAndFlags(manifest, location,
162 Extension::NO_FLAGS);
165 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
166 const base::DictionaryValue& manifest,
167 Manifest::Location location,
168 int extra_flags) {
169 std::string name;
170 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name));
171 base::FilePath path = extensions_dir_.AppendASCII(name);
172 std::string errors;
173 scoped_refptr<Extension> extension = Extension::Create(
174 path, location, manifest, extra_flags, &errors);
175 EXPECT_TRUE(extension.get()) << errors;
176 if (!extension.get())
177 return NULL;
179 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id()));
180 prefs()->OnExtensionInstalled(extension.get(),
181 Extension::ENABLED,
182 syncer::StringOrdinal::CreateInitialOrdinal(),
183 std::string());
184 return extension;
187 std::string TestExtensionPrefs::AddExtensionAndReturnId(
188 const std::string& name) {
189 scoped_refptr<Extension> extension(AddExtension(name));
190 return extension->id();
193 void TestExtensionPrefs::AddExtension(Extension* extension) {
194 prefs()->OnExtensionInstalled(extension,
195 Extension::ENABLED,
196 syncer::StringOrdinal::CreateInitialOrdinal(),
197 std::string());
200 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const {
201 return CreateIncognitoPrefServiceSyncable(
202 pref_service_.get(),
203 new ExtensionPrefStore(extension_pref_value_map_.get(), true));
206 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
207 extensions_disabled_ = extensions_disabled;
210 ChromeAppSorting* TestExtensionPrefs::app_sorting() {
211 return static_cast<ChromeAppSorting*>(
212 ExtensionSystem::Get(&profile_)->app_sorting());
215 } // namespace extensions