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 |
77 PrefService* TestExtensionPrefs::pref_service() { | 85 PrefService* TestExtensionPrefs::pref_service() { |
78 return pref_service_.get(); | 86 return pref_service_.get(); |
79 } | 87 } |
80 | 88 |
81 const scoped_refptr<user_prefs::PrefRegistrySyncable>& | 89 const scoped_refptr<user_prefs::PrefRegistrySyncable>& |
82 TestExtensionPrefs::pref_registry() { | 90 TestExtensionPrefs::pref_registry() { |
83 return pref_registry_; | 91 return pref_registry_; |
84 } | 92 } |
85 | 93 |
86 void TestExtensionPrefs::ResetPrefRegistry() { | 94 void TestExtensionPrefs::ResetPrefRegistry() { |
(...skipping 17 matching lines...) Expand all Loading... |
104 run_loop.QuitClosure())); | 112 run_loop.QuitClosure())); |
105 run_loop.Run(); | 113 run_loop.Run(); |
106 } | 114 } |
107 | 115 |
108 extension_pref_value_map_.reset(new ExtensionPrefValueMap); | 116 extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
109 PrefServiceMockFactory factory; | 117 PrefServiceMockFactory factory; |
110 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); | 118 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); |
111 factory.set_extension_prefs( | 119 factory.set_extension_prefs( |
112 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); | 120 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
113 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); | 121 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); |
114 | 122 scoped_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create( |
115 prefs_.reset(ExtensionPrefs::Create( | 123 &profile_, |
116 pref_service_.get(), | 124 pref_service_.get(), |
117 temp_dir_.path(), | 125 temp_dir_.path(), |
118 extension_pref_value_map_.get(), | 126 extension_pref_value_map_.get(), |
119 ExtensionsBrowserClient::Get()->CreateAppSorting(nullptr).Pass(), | |
120 extensions_disabled_, | 127 extensions_disabled_, |
121 std::vector<ExtensionPrefsObserver*>(), | 128 std::vector<ExtensionPrefsObserver*>(), |
122 // Guarantee that no two extensions get the same installation time | 129 // Guarantee that no two extensions get the same installation time |
123 // stamp and we can reliably assert the installation order in the tests. | 130 // stamp and we can reliably assert the installation order in the tests. |
124 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); | 131 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); |
| 132 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_, |
| 133 prefs.Pass()); |
| 134 // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be |
| 135 // recreated. (ExtensionPrefs is never recreated in non-test code.) |
| 136 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)) |
| 137 ->RecreateAppSorting(); |
125 } | 138 } |
126 | 139 |
127 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( | 140 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( |
128 const std::string& name) { | 141 const std::string& name) { |
129 base::DictionaryValue dictionary; | 142 base::DictionaryValue dictionary; |
130 dictionary.SetString(manifest_keys::kName, name); | 143 dictionary.SetString(manifest_keys::kName, name); |
131 dictionary.SetString(manifest_keys::kVersion, "0.1"); | 144 dictionary.SetString(manifest_keys::kVersion, "0.1"); |
132 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); | 145 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); |
133 } | 146 } |
134 | 147 |
(...skipping 21 matching lines...) Expand all Loading... |
156 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); | 169 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); |
157 base::FilePath path = extensions_dir_.AppendASCII(name); | 170 base::FilePath path = extensions_dir_.AppendASCII(name); |
158 std::string errors; | 171 std::string errors; |
159 scoped_refptr<Extension> extension = Extension::Create( | 172 scoped_refptr<Extension> extension = Extension::Create( |
160 path, location, manifest, extra_flags, &errors); | 173 path, location, manifest, extra_flags, &errors); |
161 EXPECT_TRUE(extension.get()) << errors; | 174 EXPECT_TRUE(extension.get()) << errors; |
162 if (!extension.get()) | 175 if (!extension.get()) |
163 return NULL; | 176 return NULL; |
164 | 177 |
165 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); | 178 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); |
166 prefs_->OnExtensionInstalled(extension.get(), | 179 prefs()->OnExtensionInstalled(extension.get(), |
167 Extension::ENABLED, | 180 Extension::ENABLED, |
168 syncer::StringOrdinal::CreateInitialOrdinal(), | 181 syncer::StringOrdinal::CreateInitialOrdinal(), |
169 std::string()); | 182 std::string()); |
170 return extension; | 183 return extension; |
171 } | 184 } |
172 | 185 |
173 std::string TestExtensionPrefs::AddExtensionAndReturnId( | 186 std::string TestExtensionPrefs::AddExtensionAndReturnId( |
174 const std::string& name) { | 187 const std::string& name) { |
175 scoped_refptr<Extension> extension(AddExtension(name)); | 188 scoped_refptr<Extension> extension(AddExtension(name)); |
176 return extension->id(); | 189 return extension->id(); |
177 } | 190 } |
178 | 191 |
179 void TestExtensionPrefs::AddExtension(Extension* extension) { | 192 void TestExtensionPrefs::AddExtension(Extension* extension) { |
180 prefs_->OnExtensionInstalled(extension, | 193 prefs()->OnExtensionInstalled(extension, |
181 Extension::ENABLED, | 194 Extension::ENABLED, |
182 syncer::StringOrdinal::CreateInitialOrdinal(), | 195 syncer::StringOrdinal::CreateInitialOrdinal(), |
183 std::string()); | 196 std::string()); |
184 } | 197 } |
185 | 198 |
186 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { | 199 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { |
187 return pref_service_->CreateIncognitoPrefService( | 200 return pref_service_->CreateIncognitoPrefService( |
188 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); | 201 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); |
189 } | 202 } |
190 | 203 |
191 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { | 204 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { |
192 extensions_disabled_ = extensions_disabled; | 205 extensions_disabled_ = extensions_disabled; |
193 } | 206 } |
194 | 207 |
| 208 ChromeAppSorting* TestExtensionPrefs::app_sorting() { |
| 209 return static_cast<ChromeAppSorting*>( |
| 210 ExtensionSystem::Get(&profile_)->app_sorting()); |
| 211 } |
| 212 |
195 } // namespace extensions | 213 } // namespace extensions |
OLD | NEW |