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" |
19 #include "chrome/browser/prefs/pref_service_mock_factory.h" | 20 #include "chrome/browser/prefs/pref_service_mock_factory.h" |
20 #include "chrome/browser/prefs/pref_service_syncable.h" | 21 #include "chrome/browser/prefs/pref_service_syncable.h" |
21 #include "chrome/common/chrome_constants.h" | 22 #include "chrome/common/chrome_constants.h" |
22 #include "components/crx_file/id_util.h" | 23 #include "components/crx_file/id_util.h" |
23 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
25 #include "extensions/browser/extension_pref_store.h" | 26 #include "extensions/browser/extension_pref_store.h" |
26 #include "extensions/browser/extension_pref_value_map.h" | 27 #include "extensions/browser/extension_pref_value_map.h" |
27 #include "extensions/browser/extension_prefs.h" | 28 #include "extensions/browser/extension_prefs.h" |
| 29 #include "extensions/browser/extension_prefs_factory.h" |
28 #include "extensions/browser/extensions_browser_client.h" | 30 #include "extensions/browser/extensions_browser_client.h" |
29 #include "extensions/common/extension.h" | 31 #include "extensions/common/extension.h" |
30 #include "extensions/common/manifest_constants.h" | 32 #include "extensions/common/manifest_constants.h" |
31 #include "sync/api/string_ordinal.h" | 33 #include "sync/api/string_ordinal.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
33 | 35 |
34 using content::BrowserThread; | 36 using content::BrowserThread; |
35 | 37 |
36 namespace extensions { | 38 namespace extensions { |
37 | 39 |
(...skipping 29 matching lines...) Expand all Loading... |
67 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); | 69 extensions_dir_ = temp_dir_.path().AppendASCII("Extensions"); |
68 EXPECT_TRUE(base::CreateDirectory(extensions_dir_)); | 70 EXPECT_TRUE(base::CreateDirectory(extensions_dir_)); |
69 | 71 |
70 ResetPrefRegistry(); | 72 ResetPrefRegistry(); |
71 RecreateExtensionPrefs(); | 73 RecreateExtensionPrefs(); |
72 } | 74 } |
73 | 75 |
74 TestExtensionPrefs::~TestExtensionPrefs() { | 76 TestExtensionPrefs::~TestExtensionPrefs() { |
75 } | 77 } |
76 | 78 |
| 79 ExtensionPrefs* TestExtensionPrefs::prefs() { |
| 80 return ExtensionPrefs::Get(&profile_); |
| 81 } |
| 82 |
| 83 const ExtensionPrefs& TestExtensionPrefs::const_prefs() const { |
| 84 return *ExtensionPrefs::Get(const_cast<TestingProfile*>(&profile_)); |
| 85 } |
| 86 |
77 PrefService* TestExtensionPrefs::pref_service() { | 87 PrefService* TestExtensionPrefs::pref_service() { |
78 return pref_service_.get(); | 88 return pref_service_.get(); |
79 } | 89 } |
80 | 90 |
81 const scoped_refptr<user_prefs::PrefRegistrySyncable>& | 91 const scoped_refptr<user_prefs::PrefRegistrySyncable>& |
82 TestExtensionPrefs::pref_registry() { | 92 TestExtensionPrefs::pref_registry() { |
83 return pref_registry_; | 93 return pref_registry_; |
84 } | 94 } |
85 | 95 |
86 void TestExtensionPrefs::ResetPrefRegistry() { | 96 void TestExtensionPrefs::ResetPrefRegistry() { |
(...skipping 17 matching lines...) Expand all Loading... |
104 run_loop.QuitClosure())); | 114 run_loop.QuitClosure())); |
105 run_loop.Run(); | 115 run_loop.Run(); |
106 } | 116 } |
107 | 117 |
108 extension_pref_value_map_.reset(new ExtensionPrefValueMap); | 118 extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
109 PrefServiceMockFactory factory; | 119 PrefServiceMockFactory factory; |
110 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); | 120 factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); |
111 factory.set_extension_prefs( | 121 factory.set_extension_prefs( |
112 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); | 122 new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
113 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); | 123 pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass(); |
114 | 124 scoped_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create( |
115 prefs_.reset(ExtensionPrefs::Create( | |
116 pref_service_.get(), | 125 pref_service_.get(), |
117 temp_dir_.path(), | 126 temp_dir_.path(), |
118 extension_pref_value_map_.get(), | 127 extension_pref_value_map_.get(), |
119 ExtensionsBrowserClient::Get()->CreateAppSorting(nullptr).Pass(), | |
120 extensions_disabled_, | 128 extensions_disabled_, |
121 std::vector<ExtensionPrefsObserver*>(), | 129 std::vector<ExtensionPrefsObserver*>(), |
122 // Guarantee that no two extensions get the same installation time | 130 // Guarantee that no two extensions get the same installation time |
123 // stamp and we can reliably assert the installation order in the tests. | 131 // stamp and we can reliably assert the installation order in the tests. |
124 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); | 132 scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider()))); |
| 133 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_, |
| 134 prefs.Pass()); |
| 135 app_sorting_.reset(new ChromeAppSorting(&profile_)); |
| 136 ExtensionPrefs::Get(&profile_)->set_app_sorting(app_sorting_.get()); |
125 } | 137 } |
126 | 138 |
127 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( | 139 scoped_refptr<Extension> TestExtensionPrefs::AddExtension( |
128 const std::string& name) { | 140 const std::string& name) { |
129 base::DictionaryValue dictionary; | 141 base::DictionaryValue dictionary; |
130 dictionary.SetString(manifest_keys::kName, name); | 142 dictionary.SetString(manifest_keys::kName, name); |
131 dictionary.SetString(manifest_keys::kVersion, "0.1"); | 143 dictionary.SetString(manifest_keys::kVersion, "0.1"); |
132 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); | 144 return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); |
133 } | 145 } |
134 | 146 |
(...skipping 21 matching lines...) Expand all Loading... |
156 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); | 168 EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); |
157 base::FilePath path = extensions_dir_.AppendASCII(name); | 169 base::FilePath path = extensions_dir_.AppendASCII(name); |
158 std::string errors; | 170 std::string errors; |
159 scoped_refptr<Extension> extension = Extension::Create( | 171 scoped_refptr<Extension> extension = Extension::Create( |
160 path, location, manifest, extra_flags, &errors); | 172 path, location, manifest, extra_flags, &errors); |
161 EXPECT_TRUE(extension.get()) << errors; | 173 EXPECT_TRUE(extension.get()) << errors; |
162 if (!extension.get()) | 174 if (!extension.get()) |
163 return NULL; | 175 return NULL; |
164 | 176 |
165 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); | 177 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); |
166 prefs_->OnExtensionInstalled(extension.get(), | 178 prefs()->OnExtensionInstalled(extension.get(), |
167 Extension::ENABLED, | 179 Extension::ENABLED, |
168 syncer::StringOrdinal::CreateInitialOrdinal(), | 180 syncer::StringOrdinal::CreateInitialOrdinal(), |
169 std::string()); | 181 std::string()); |
170 return extension; | 182 return extension; |
171 } | 183 } |
172 | 184 |
173 std::string TestExtensionPrefs::AddExtensionAndReturnId( | 185 std::string TestExtensionPrefs::AddExtensionAndReturnId( |
174 const std::string& name) { | 186 const std::string& name) { |
175 scoped_refptr<Extension> extension(AddExtension(name)); | 187 scoped_refptr<Extension> extension(AddExtension(name)); |
176 return extension->id(); | 188 return extension->id(); |
177 } | 189 } |
178 | 190 |
179 void TestExtensionPrefs::AddExtension(Extension* extension) { | 191 void TestExtensionPrefs::AddExtension(Extension* extension) { |
180 prefs_->OnExtensionInstalled(extension, | 192 prefs()->OnExtensionInstalled(extension, |
181 Extension::ENABLED, | 193 Extension::ENABLED, |
182 syncer::StringOrdinal::CreateInitialOrdinal(), | 194 syncer::StringOrdinal::CreateInitialOrdinal(), |
183 std::string()); | 195 std::string()); |
184 } | 196 } |
185 | 197 |
186 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { | 198 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const { |
187 return pref_service_->CreateIncognitoPrefService( | 199 return pref_service_->CreateIncognitoPrefService( |
188 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); | 200 new ExtensionPrefStore(extension_pref_value_map_.get(), true)); |
189 } | 201 } |
190 | 202 |
191 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { | 203 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { |
192 extensions_disabled_ = extensions_disabled; | 204 extensions_disabled_ = extensions_disabled; |
193 } | 205 } |
194 | 206 |
195 } // namespace extensions | 207 } // namespace extensions |
OLD | NEW |