Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/browser/prefs/profile_pref_store_manager_unittest.cc

Issue 2601873002: Add a mojo bridge for PersistentPrefStore. (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/prefs/profile_pref_store_manager.h" 5 #include "chrome/browser/prefs/profile_pref_store_manager.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/callback_helpers.h"
13 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
14 #include "base/files/file_enumerator.h" 15 #include "base/files/file_enumerator.h"
15 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h" 17 #include "base/files/scoped_temp_dir.h"
17 #include "base/macros.h" 18 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
19 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
20 #include "base/run_loop.h" 21 #include "base/run_loop.h"
21 #include "base/strings/string_util.h" 22 #include "base/strings/string_util.h"
22 #include "base/values.h" 23 #include "base/values.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 FirstEqualsPredicate(key))) 62 FirstEqualsPredicate(key)))
62 << "Unregistered key " << key << " was changed."; 63 << "Unregistered key " << key << " was changed.";
63 } 64 }
64 65
65 void OnInitializationCompleted(bool succeeded) override {} 66 void OnInitializationCompleted(bool succeeded) override {}
66 67
67 private: 68 private:
68 scoped_refptr<PrefRegistry> pref_registry_; 69 scoped_refptr<PrefRegistry> pref_registry_;
69 }; 70 };
70 71
72 class PrefStoreReadObserver : public PrefStore::Observer {
73 public:
74 explicit PrefStoreReadObserver(scoped_refptr<PersistentPrefStore> pref_store)
75 : pref_store_(std::move(pref_store)) {
76 pref_store_->AddObserver(this);
77 }
78
79 ~PrefStoreReadObserver() override { pref_store_->RemoveObserver(this); }
80
81 PersistentPrefStore::PrefReadError Read() {
82 base::RunLoop run_loop;
83 stop_waiting_ = run_loop.QuitClosure();
84 pref_store_->ReadPrefsAsync(nullptr);
85 run_loop.Run();
86 return pref_store_->GetReadError();
87 }
88
89 // PrefStore::Observer implementation
90 void OnPrefValueChanged(const std::string& key) override {}
91
92 void OnInitializationCompleted(bool succeeded) override {
93 if (!stop_waiting_.is_null()) {
94 base::ResetAndReturn(&stop_waiting_).Run();
95 }
96 }
97
98 private:
99 scoped_refptr<PersistentPrefStore> pref_store_;
100 base::Closure stop_waiting_;
101 };
102
71 const char kUnprotectedPref[] = "unprotected_pref"; 103 const char kUnprotectedPref[] = "unprotected_pref";
72 const char kTrackedAtomic[] = "tracked_atomic"; 104 const char kTrackedAtomic[] = "tracked_atomic";
73 const char kProtectedAtomic[] = "protected_atomic"; 105 const char kProtectedAtomic[] = "protected_atomic";
74 106
75 const char kFoobar[] = "FOOBAR"; 107 const char kFoobar[] = "FOOBAR";
76 const char kBarfoo[] = "BARFOO"; 108 const char kBarfoo[] = "BARFOO";
77 const char kHelloWorld[] = "HELLOWORLD"; 109 const char kHelloWorld[] = "HELLOWORLD";
78 const char kGoodbyeWorld[] = "GOODBYEWORLD"; 110 const char kGoodbyeWorld[] = "GOODBYEWORLD";
79 111
80 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { 112 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = {
(...skipping 12 matching lines...) Expand all
93 ProfilePrefStoreManagerTest() 125 ProfilePrefStoreManagerTest()
94 : configuration_(kConfiguration, 126 : configuration_(kConfiguration,
95 kConfiguration + arraysize(kConfiguration)), 127 kConfiguration + arraysize(kConfiguration)),
96 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), 128 profile_pref_registry_(new user_prefs::PrefRegistrySyncable),
97 registry_verifier_(profile_pref_registry_.get()), 129 registry_verifier_(profile_pref_registry_.get()),
98 seed_("seed"), 130 seed_("seed"),
99 reset_recorded_(false) {} 131 reset_recorded_(false) {}
100 132
101 void SetUp() override { 133 void SetUp() override {
102 mock_validation_delegate_record_ = new MockValidationDelegateRecord; 134 mock_validation_delegate_record_ = new MockValidationDelegateRecord;
103 mock_validation_delegate_ = base::MakeUnique<MockValidationDelegate>(
104 mock_validation_delegate_record_);
105 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); 135 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get());
106 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; 136 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration;
107 it != kConfiguration + arraysize(kConfiguration); 137 it != kConfiguration + arraysize(kConfiguration);
108 ++it) { 138 ++it) {
109 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { 139 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) {
110 profile_pref_registry_->RegisterStringPref(it->name, std::string()); 140 profile_pref_registry_->RegisterStringPref(it->name, std::string());
111 } else { 141 } else {
112 profile_pref_registry_->RegisterDictionaryPref(it->name); 142 profile_pref_registry_->RegisterDictionaryPref(it->name);
113 } 143 }
114 } 144 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 } 198 }
169 199
170 void InitializePrefs() { 200 void InitializePrefs() {
171 // According to the implementation of ProfilePrefStoreManager, this is 201 // According to the implementation of ProfilePrefStoreManager, this is
172 // actually a SegregatedPrefStore backed by two underlying pref stores. 202 // actually a SegregatedPrefStore backed by two underlying pref stores.
173 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate> 203 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate>
174 mock_validation_delegate = base::MakeUnique<MockValidationDelegate>( 204 mock_validation_delegate = base::MakeUnique<MockValidationDelegate>(
175 mock_validation_delegate_record_); 205 mock_validation_delegate_record_);
176 scoped_refptr<PersistentPrefStore> pref_store = 206 scoped_refptr<PersistentPrefStore> pref_store =
177 manager_->CreateProfilePrefStore( 207 manager_->CreateProfilePrefStore(
178 main_message_loop_.task_runner(), 208 main_message_loop_.task_runner(), main_message_loop_.task_runner(),
179 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, 209 base::Bind(&ProfilePrefStoreManagerTest::RecordReset,
180 base::Unretained(this)), 210 base::Unretained(this)),
181 mock_validation_delegate_.get()); 211 &mock_validation_delegate);
182 InitializePrefStore(pref_store.get()); 212 InitializePrefStore(pref_store.get());
183 pref_store = NULL; 213 pref_store = NULL;
184 base::RunLoop().RunUntilIdle(); 214 base::RunLoop().RunUntilIdle();
185 } 215 }
186 216
187 void DestroyPrefStore() { 217 void DestroyPrefStore() {
188 if (pref_store_.get()) { 218 if (pref_store_.get()) {
189 ClearResetRecorded(); 219 ClearResetRecorded();
190 // Force everything to be written to disk, triggering the PrefHashFilter 220 // Force everything to be written to disk, triggering the PrefHashFilter
191 // while our RegistryVerifier is watching. 221 // while our RegistryVerifier is watching.
192 pref_store_->CommitPendingWrite(); 222 pref_store_->CommitPendingWrite();
193 base::RunLoop().RunUntilIdle(); 223 base::RunLoop().RunUntilIdle();
194 224
195 pref_store_->RemoveObserver(&registry_verifier_); 225 pref_store_->RemoveObserver(&registry_verifier_);
196 pref_store_ = NULL; 226 pref_store_ = NULL;
197 // Nothing should have to happen on the background threads, but just in 227 // Nothing should have to happen on the background threads, but just in
198 // case... 228 // case...
199 base::RunLoop().RunUntilIdle(); 229 base::RunLoop().RunUntilIdle();
200 } 230 }
201 } 231 }
202 232
203 void InitializePrefStore(PersistentPrefStore* pref_store) { 233 void InitializePrefStore(PersistentPrefStore* pref_store) {
204 pref_store->AddObserver(&registry_verifier_); 234 pref_store->AddObserver(&registry_verifier_);
205 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); 235 PrefStoreReadObserver read_observer(pref_store);
236 PersistentPrefStore::PrefReadError error = read_observer.Read();
206 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); 237 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
207 pref_store->SetValue(kTrackedAtomic, 238 pref_store->SetValue(kTrackedAtomic,
208 base::MakeUnique<base::StringValue>(kFoobar), 239 base::MakeUnique<base::StringValue>(kFoobar),
209 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 240 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
210 pref_store->SetValue(kProtectedAtomic, 241 pref_store->SetValue(kProtectedAtomic,
211 base::MakeUnique<base::StringValue>(kHelloWorld), 242 base::MakeUnique<base::StringValue>(kHelloWorld),
212 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 243 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
213 pref_store->SetValue(kUnprotectedPref, 244 pref_store->SetValue(kUnprotectedPref,
214 base::MakeUnique<base::StringValue>(kFoobar), 245 base::MakeUnique<base::StringValue>(kFoobar),
215 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 246 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
216 pref_store->RemoveObserver(&registry_verifier_); 247 pref_store->RemoveObserver(&registry_verifier_);
217 pref_store->CommitPendingWrite(); 248 pref_store->CommitPendingWrite();
218 base::RunLoop().RunUntilIdle(); 249 base::RunLoop().RunUntilIdle();
219 } 250 }
220 251
221 void LoadExistingPrefs() { 252 void LoadExistingPrefs() {
222 DestroyPrefStore(); 253 DestroyPrefStore();
254 std::unique_ptr<prefs::mojom::TrackedPreferenceValidationDelegate>
255 validation_delegate;
223 pref_store_ = manager_->CreateProfilePrefStore( 256 pref_store_ = manager_->CreateProfilePrefStore(
224 main_message_loop_.task_runner(), 257 main_message_loop_.task_runner(), main_message_loop_.task_runner(),
225 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, 258 base::Bind(&ProfilePrefStoreManagerTest::RecordReset,
226 base::Unretained(this)), 259 base::Unretained(this)),
227 NULL); 260 &validation_delegate);
228 pref_store_->AddObserver(&registry_verifier_); 261 pref_store_->AddObserver(&registry_verifier_);
229 pref_store_->ReadPrefs(); 262 PrefStoreReadObserver read_observer(pref_store_);
263 read_observer.Read();
230 } 264 }
231 265
232 void ReplaceStringInPrefs(const std::string& find, 266 void ReplaceStringInPrefs(const std::string& find,
233 const std::string& replace) { 267 const std::string& replace) {
234 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, 268 base::FileEnumerator file_enum(profile_dir_.GetPath(), true,
235 base::FileEnumerator::FILES); 269 base::FileEnumerator::FILES);
236 270
237 for (base::FilePath path = file_enum.Next(); !path.empty(); 271 for (base::FilePath path = file_enum.Next(); !path.empty();
238 path = file_enum.Next()) { 272 path = file_enum.Next()) {
239 // Tamper with the file's contents 273 // Tamper with the file's contents
(...skipping 26 matching lines...) Expand all
266 ADD_FAILURE() << "No validation observed for preference: " << pref_path; 300 ADD_FAILURE() << "No validation observed for preference: " << pref_path;
267 } 301 }
268 302
269 base::MessageLoop main_message_loop_; 303 base::MessageLoop main_message_loop_;
270 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; 304 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_;
271 base::ScopedTempDir profile_dir_; 305 base::ScopedTempDir profile_dir_;
272 TestingPrefServiceSimple local_state_; 306 TestingPrefServiceSimple local_state_;
273 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; 307 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_;
274 RegistryVerifier registry_verifier_; 308 RegistryVerifier registry_verifier_;
275 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_; 309 scoped_refptr<MockValidationDelegateRecord> mock_validation_delegate_record_;
276 std::unique_ptr<MockValidationDelegate> mock_validation_delegate_;
277 std::unique_ptr<ProfilePrefStoreManager> manager_; 310 std::unique_ptr<ProfilePrefStoreManager> manager_;
278 scoped_refptr<PersistentPrefStore> pref_store_; 311 scoped_refptr<PersistentPrefStore> pref_store_;
279 312
280 std::string seed_; 313 std::string seed_;
281 314
282 private: 315 private:
283 void RecordReset() { 316 void RecordReset() {
284 // As-is |reset_recorded_| is only designed to remember a single reset, make 317 // As-is |reset_recorded_| is only designed to remember a single reset, make
285 // sure none was previously recorded (or that ClearResetRecorded() was 318 // sure none was previously recorded (or that ClearResetRecorded() was
286 // called). 319 // called).
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 522
490 // Trigger the logic that migrates it back to the unprotected preferences 523 // Trigger the logic that migrates it back to the unprotected preferences
491 // file. 524 // file.
492 pref_store_->SetValue(kProtectedAtomic, 525 pref_store_->SetValue(kProtectedAtomic,
493 base::WrapUnique(new base::StringValue(kGoodbyeWorld)), 526 base::WrapUnique(new base::StringValue(kGoodbyeWorld)),
494 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 527 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
495 LoadExistingPrefs(); 528 LoadExistingPrefs();
496 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); 529 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
497 VerifyResetRecorded(false); 530 VerifyResetRecorded(false);
498 } 531 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698