Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / extensions / test_extension_prefs.cc
blob6ea55b7cc3553c44bef3842f26eae2a5d1f17d70
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/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/user_prefs/pref_registry_syncable.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "extensions/browser/extension_pref_store.h"
25 #include "extensions/browser/extension_pref_value_map.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extensions_browser_client.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "sync/api/string_ordinal.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 using content::BrowserThread;
35 namespace extensions {
37 namespace {
39 // A TimeProvider which returns an incrementally later time each time
40 // GetCurrentTime is called.
41 class IncrementalTimeProvider : public ExtensionPrefs::TimeProvider {
42 public:
43 IncrementalTimeProvider() : current_time_(base::Time::Now()) {
46 virtual ~IncrementalTimeProvider() {
49 virtual base::Time GetCurrentTime() const OVERRIDE {
50 current_time_ += base::TimeDelta::FromSeconds(10);
51 return current_time_;
54 private:
55 DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider);
57 mutable base::Time current_time_;
60 } // namespace
62 TestExtensionPrefs::TestExtensionPrefs(base::SequencedTaskRunner* task_runner)
63 : task_runner_(task_runner), extensions_disabled_(false) {
64 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
65 preferences_file_ = temp_dir_.path().Append(chrome::kPreferencesFilename);
66 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
67 EXPECT_TRUE(base::CreateDirectory(extensions_dir_));
69 ResetPrefRegistry();
70 RecreateExtensionPrefs();
73 TestExtensionPrefs::~TestExtensionPrefs() {
76 PrefService* TestExtensionPrefs::pref_service() {
77 return pref_service_.get();
80 const scoped_refptr<user_prefs::PrefRegistrySyncable>&
81 TestExtensionPrefs::pref_registry() {
82 return pref_registry_;
85 void TestExtensionPrefs::ResetPrefRegistry() {
86 pref_registry_ = new user_prefs::PrefRegistrySyncable;
87 ExtensionPrefs::RegisterProfilePrefs(pref_registry_.get());
90 void TestExtensionPrefs::RecreateExtensionPrefs() {
91 // We persist and reload the PrefService's PrefStores because this process
92 // deletes all empty dictionaries. The ExtensionPrefs implementation
93 // needs to be able to handle this situation.
94 if (pref_service_) {
95 // Commit a pending write (which posts a task to task_runner_) and wait for
96 // it to finish.
97 pref_service_->CommitPendingWrite();
98 base::RunLoop run_loop;
99 ASSERT_TRUE(
100 task_runner_->PostTaskAndReply(
101 FROM_HERE,
102 base::Bind(&base::DoNothing),
103 run_loop.QuitClosure()));
104 run_loop.Run();
107 extension_pref_value_map_.reset(new ExtensionPrefValueMap);
108 PrefServiceMockFactory factory;
109 factory.SetUserPrefsFile(preferences_file_, task_runner_.get());
110 factory.set_extension_prefs(
111 new ExtensionPrefStore(extension_pref_value_map_.get(), false));
112 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass();
114 prefs_.reset(ExtensionPrefs::Create(
115 pref_service_.get(),
116 temp_dir_.path(),
117 extension_pref_value_map_.get(),
118 ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
119 extensions_disabled_,
120 std::vector<ExtensionPrefsObserver*>(),
121 // Guarantee that no two extensions get the same installation time
122 // stamp and we can reliably assert the installation order in the tests.
123 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider())));
126 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(
127 const std::string& name) {
128 base::DictionaryValue dictionary;
129 dictionary.SetString(manifest_keys::kName, name);
130 dictionary.SetString(manifest_keys::kVersion, "0.1");
131 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
134 scoped_refptr<Extension> TestExtensionPrefs::AddApp(const std::string& name) {
135 base::DictionaryValue dictionary;
136 dictionary.SetString(manifest_keys::kName, name);
137 dictionary.SetString(manifest_keys::kVersion, "0.1");
138 dictionary.SetString(manifest_keys::kApp, "true");
139 dictionary.SetString(manifest_keys::kLaunchWebURL, "http://example.com");
140 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
144 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest(
145 const base::DictionaryValue& manifest, Manifest::Location location) {
146 return AddExtensionWithManifestAndFlags(manifest, location,
147 Extension::NO_FLAGS);
150 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
151 const base::DictionaryValue& manifest,
152 Manifest::Location location,
153 int extra_flags) {
154 std::string name;
155 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name));
156 base::FilePath path = extensions_dir_.AppendASCII(name);
157 std::string errors;
158 scoped_refptr<Extension> extension = Extension::Create(
159 path, location, manifest, extra_flags, &errors);
160 EXPECT_TRUE(extension.get()) << errors;
161 if (!extension.get())
162 return NULL;
164 EXPECT_TRUE(Extension::IdIsValid(extension->id()));
165 prefs_->OnExtensionInstalled(extension.get(),
166 Extension::ENABLED,
167 false,
168 syncer::StringOrdinal::CreateInitialOrdinal(),
169 std::string());
170 return extension;
173 std::string TestExtensionPrefs::AddExtensionAndReturnId(
174 const std::string& name) {
175 scoped_refptr<Extension> extension(AddExtension(name));
176 return extension->id();
179 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const {
180 return pref_service_->CreateIncognitoPrefService(
181 new ExtensionPrefStore(extension_pref_value_map_.get(), true));
184 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
185 extensions_disabled_ = extensions_disabled;
188 } // namespace extensions