OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/test_extension_prefs.h" | 5 #include "chrome/browser/extensions/test_extension_prefs.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/prefs/json_pref_store.h" | 12 #include "base/prefs/json_pref_store.h" |
13 #include "base/prefs/pref_value_store.h" | 13 #include "base/prefs/pref_value_store.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/browser/extensions/chrome_app_sorting.h" | |
20 #include "chrome/browser/extensions/test_extension_system.h" | |
19 #include "chrome/browser/prefs/pref_service_mock_factory.h" | 21 #include "chrome/browser/prefs/pref_service_mock_factory.h" |
20 #include "chrome/browser/prefs/pref_service_syncable.h" | 22 #include "chrome/browser/prefs/pref_service_syncable.h" |
21 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
22 #include "components/crx_file/id_util.h" | 24 #include "components/crx_file/id_util.h" |
23 #include "components/pref_registry/pref_registry_syncable.h" | 25 #include "components/pref_registry/pref_registry_syncable.h" |
24 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
25 #include "extensions/browser/extension_pref_store.h" | 27 #include "extensions/browser/extension_pref_store.h" |
26 #include "extensions/browser/extension_pref_value_map.h" | 28 #include "extensions/browser/extension_pref_value_map.h" |
27 #include "extensions/browser/extension_prefs.h" | 29 #include "extensions/browser/extension_prefs.h" |
30 #include "extensions/browser/extension_prefs_factory.h" | |
31 #include "extensions/browser/extension_system.h" | |
28 #include "extensions/browser/extensions_browser_client.h" | 32 #include "extensions/browser/extensions_browser_client.h" |
29 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
30 #include "extensions/common/manifest_constants.h" | 34 #include "extensions/common/manifest_constants.h" |
31 #include "sync/api/string_ordinal.h" | 35 #include "sync/api/string_ordinal.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
33 | 37 |
34 using content::BrowserThread; | 38 using content::BrowserThread; |
35 | 39 |
36 namespace extensions { | 40 namespace extensions { |
37 | 41 |
(...skipping 29 matching lines...) Expand all Loading... | |
67 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 71 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
68 EXPECT_TRUE(base::CreateDirectory(extensions_dir_)); | 72 EXPECT_TRUE(base::CreateDirectory(extensions_dir_)); |
69 | 73 |
70 ResetPrefRegistry(); | 74 ResetPrefRegistry(); |
71 RecreateExtensionPrefs(); | 75 RecreateExtensionPrefs(); |
72 } | 76 } |
73 | 77 |
74 TestExtensionPrefs::~TestExtensionPrefs() { | 78 TestExtensionPrefs::~TestExtensionPrefs() { |
75 } | 79 } |
76 | 80 |
81 ExtensionPrefs* TestExtensionPrefs::prefs() { | |
82 return ExtensionPrefs::Get(&profile_); | |
83 } | |
84 | |
85 const ExtensionPrefs& TestExtensionPrefs::const_prefs() const { | |
not at google - send to devlin
2015/08/03 21:12:08
Is there even any point in this const version? Whe
Marc Treib
2015/08/04 08:36:28
I hadn't checked. You're right, it's not used anyw
| |
86 return *ExtensionPrefs::Get(const_cast<TestingProfile*>(&profile_)); | |
87 } | |
88 | |
77 PrefService* TestExtensionPrefs::pref_service() { | 89 PrefService* TestExtensionPrefs::pref_service() { |
78 return pref_service_.get(); | 90 return pref_service_.get(); |
79 } | 91 } |
80 | 92 |
81 const scoped_refptr<user_prefs::PrefRegistrySyncable>& | 93 const scoped_refptr<user_prefs::PrefRegistrySyncable>& |
82 TestExtensionPrefs::pref_registry() { | 94 TestExtensionPrefs::pref_registry() { |
83 return pref_registry_; | 95 return pref_registry_; |
84 } | 96 } |
85 | 97 |
86 void TestExtensionPrefs::ResetPrefRegistry() { | 98 void TestExtensionPrefs::ResetPrefRegistry() { |
(...skipping 17 matching lines...) Expand all Loading... | |
104 run_loop.QuitClosure())); | 116 run_loop.QuitClosure())); |
105 run_loop.Run(); | 117 run_loop.Run(); |
106 } | 118 } |
107 | 119 |
108 extension_pref_value_map_.reset(new ExtensionPrefValueMap); | 120 extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
109 PrefServiceMockFactory factory; | 121 PrefServiceMockFactory factory; |
110 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); | 122 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); |
111 factory.set_extension_prefs( | 123 factory.set_extension_prefs( |
112 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); | 124 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
113 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); | 125 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); |
114 | 126 scoped_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create( |
115 prefs_.reset(ExtensionPrefs::Create( | 127 &profile_, |
116 pref_service_.get(), | 128 pref_service_.get(), |
117 temp_dir_.path(), | 129 temp_dir_.path(), |
118 extension_pref_value_map_.get(), | 130 extension_pref_value_map_.get(), |
119 ExtensionsBrowserClient::Get()->CreateAppSorting(nullptr).Pass(), | |
120 extensions_disabled_, | 131 extensions_disabled_, |
121 std::vector<ExtensionPrefsObserver*>(), | 132 std::vector<ExtensionPrefsObserver*>(), |
122 // Guarantee that no two extensions get the same installation time | 133 // Guarantee that no two extensions get the same installation time |
123 // stamp and we can reliably assert the installation order in the tests. | 134 // stamp and we can reliably assert the installation order in the tests. |
124 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); | 135 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); |
136 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_, | |
137 prefs.Pass()); | |
138 // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be | |
139 // recreated. (ExtensionPrefs is never recreated in non-test code.) | |
140 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)) | |
141 ->RecreateAppSorting(); | |
125 } | 142 } |
126 | 143 |
127 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( | 144 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( |
128 const std::string& name) { | 145 const std::string& name) { |
129 base::DictionaryValue dictionary; | 146 base::DictionaryValue dictionary; |
130 dictionary.SetString(manifest_keys::kName, name); | 147 dictionary.SetString(manifest_keys::kName, name); |
131 dictionary.SetString(manifest_keys::kVersion, "0.1"); | 148 dictionary.SetString(manifest_keys::kVersion, "0.1"); |
132 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); | 149 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); |
133 } | 150 } |
134 | 151 |
(...skipping 21 matching lines...) Expand all Loading... | |
156 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); | 173 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); |
157 base::FilePath path = extensions_dir_.AppendASCII(name); | 174 base::FilePath path = extensions_dir_.AppendASCII(name); |
158 std::string errors; | 175 std::string errors; |
159 scoped_refptr<Extension> extension = Extension::Create( | 176 scoped_refptr<Extension> extension = Extension::Create( |
160 path, location, manifest, extra_flags, &errors); | 177 path, location, manifest, extra_flags, &errors); |
161 EXPECT_TRUE(extension.get()) << errors; | 178 EXPECT_TRUE(extension.get()) << errors; |
162 if (!extension.get()) | 179 if (!extension.get()) |
163 return NULL; | 180 return NULL; |
164 | 181 |
165 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); | 182 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); |
166 prefs_->OnExtensionInstalled(extension.get(), | 183 prefs()->OnExtensionInstalled(extension.get(), |
167 Extension::ENABLED, | 184 Extension::ENABLED, |
168 syncer::StringOrdinal::CreateInitialOrdinal(), | 185 syncer::StringOrdinal::CreateInitialOrdinal(), |
169 std::string()); | 186 std::string()); |
170 return extension; | 187 return extension; |
171 } | 188 } |
172 | 189 |
173 std::string TestExtensionPrefs::AddExtensionAndReturnId( | 190 std::string TestExtensionPrefs::AddExtensionAndReturnId( |
174 const std::string& name) { | 191 const std::string& name) { |
175 scoped_refptr<Extension> extension(AddExtension(name)); | 192 scoped_refptr<Extension> extension(AddExtension(name)); |
176 return extension->id(); | 193 return extension->id(); |
177 } | 194 } |
178 | 195 |
179 void TestExtensionPrefs::AddExtension(Extension* extension) { | 196 void TestExtensionPrefs::AddExtension(Extension* extension) { |
180 prefs_->OnExtensionInstalled(extension, | 197 prefs()->OnExtensionInstalled(extension, |
181 Extension::ENABLED, | 198 Extension::ENABLED, |
182 syncer::StringOrdinal::CreateInitialOrdinal(), | 199 syncer::StringOrdinal::CreateInitialOrdinal(), |
183 std::string()); | 200 std::string()); |
184 } | 201 } |
185 | 202 |
186 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { | 203 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { |
187 return pref_service_->CreateIncognitoPrefService( | 204 return pref_service_->CreateIncognitoPrefService( |
188 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); | 205 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); |
189 } | 206 } |
190 | 207 |
191 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { | 208 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { |
192 extensions_disabled_ = extensions_disabled; | 209 extensions_disabled_ = extensions_disabled; |
193 } | 210 } |
194 | 211 |
212 ChromeAppSorting* TestExtensionPrefs::app_sorting() { | |
213 return static_cast<ChromeAppSorting*>( | |
214 ExtensionSystem::Get(&profile_)->app_sorting()); | |
215 } | |
216 | |
195 } // namespace extensions | 217 } // namespace extensions |
OLD | NEW |