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

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

Issue 2601873002: Add a mojo bridge for PersistentPrefStore. (Closed)
Patch Set: Created 3 years, 10 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 <vector> 10 #include <vector>
11 11
12 #include "base/callback_helpers.h"
12 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
13 #include "base/files/file_enumerator.h" 14 #include "base/files/file_enumerator.h"
14 #include "base/files/file_util.h" 15 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h" 16 #include "base/files/scoped_temp_dir.h"
16 #include "base/macros.h" 17 #include "base/macros.h"
17 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
18 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
19 #include "base/run_loop.h" 20 #include "base/run_loop.h"
20 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
21 #include "base/values.h" 22 #include "base/values.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 FirstEqualsPredicate(key))) 61 FirstEqualsPredicate(key)))
61 << "Unregistered key " << key << " was changed."; 62 << "Unregistered key " << key << " was changed.";
62 } 63 }
63 64
64 void OnInitializationCompleted(bool succeeded) override {} 65 void OnInitializationCompleted(bool succeeded) override {}
65 66
66 private: 67 private:
67 scoped_refptr<PrefRegistry> pref_registry_; 68 scoped_refptr<PrefRegistry> pref_registry_;
68 }; 69 };
69 70
71 class PrefStoreReadObserver : public PrefStore::Observer {
72 public:
73 explicit PrefStoreReadObserver(scoped_refptr<PersistentPrefStore> pref_store)
74 : pref_store_(std::move(pref_store)) {
75 pref_store_->AddObserver(this);
76 }
77
78 ~PrefStoreReadObserver() override { pref_store_->RemoveObserver(this); }
79
80 PersistentPrefStore::PrefReadError Read() {
81 base::RunLoop run_loop;
82 stop_waiting_ = run_loop.QuitClosure();
83 pref_store_->ReadPrefsAsync(nullptr);
84 run_loop.Run();
85 return pref_store_->GetReadError();
86 }
87
88 // PrefStore::Observer implementation
89 void OnPrefValueChanged(const std::string& key) override {}
90
91 void OnInitializationCompleted(bool succeeded) override {
92 if (!stop_waiting_.is_null()) {
93 base::ResetAndReturn(&stop_waiting_).Run();
94 }
95 }
96
97 private:
98 scoped_refptr<PersistentPrefStore> pref_store_;
99 base::Closure stop_waiting_;
100 };
101
70 const char kUnprotectedPref[] = "unprotected_pref"; 102 const char kUnprotectedPref[] = "unprotected_pref";
71 const char kTrackedAtomic[] = "tracked_atomic"; 103 const char kTrackedAtomic[] = "tracked_atomic";
72 const char kProtectedAtomic[] = "protected_atomic"; 104 const char kProtectedAtomic[] = "protected_atomic";
73 105
74 const char kFoobar[] = "FOOBAR"; 106 const char kFoobar[] = "FOOBAR";
75 const char kBarfoo[] = "BARFOO"; 107 const char kBarfoo[] = "BARFOO";
76 const char kHelloWorld[] = "HELLOWORLD"; 108 const char kHelloWorld[] = "HELLOWORLD";
77 const char kGoodbyeWorld[] = "GOODBYEWORLD"; 109 const char kGoodbyeWorld[] = "GOODBYEWORLD";
78 110
79 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = { 111 const PrefHashFilter::TrackedPreferenceMetadata kConfiguration[] = {
80 {0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT, 112 {0u, kTrackedAtomic, PrefHashFilter::NO_ENFORCEMENT,
81 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}, 113 PrefHashFilter::TRACKING_STRATEGY_ATOMIC},
82 {1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD, 114 {1u, kProtectedAtomic, PrefHashFilter::ENFORCE_ON_LOAD,
83 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}}; 115 PrefHashFilter::TRACKING_STRATEGY_ATOMIC}};
84 116
85 const size_t kExtraReportingId = 2u; 117 const size_t kExtraReportingId = 2u;
86 const size_t kReportingIdCount = 3u; 118 const size_t kReportingIdCount = 3u;
87 119
88 } // namespace 120 } // namespace
89 121
90 class ProfilePrefStoreManagerTest : public testing::Test { 122 class ProfilePrefStoreManagerTest : public testing::Test {
91 public: 123 public:
92 ProfilePrefStoreManagerTest() 124 ProfilePrefStoreManagerTest()
93 : configuration_(kConfiguration, 125 : configuration_(kConfiguration,
94 kConfiguration + arraysize(kConfiguration)), 126 kConfiguration + arraysize(kConfiguration)),
95 profile_pref_registry_(new user_prefs::PrefRegistrySyncable), 127 profile_pref_registry_(new user_prefs::PrefRegistrySyncable),
96 registry_verifier_(profile_pref_registry_.get()), 128 registry_verifier_(profile_pref_registry_.get()),
129 mock_validation_delegate_weak_factory_(&mock_validation_delegate_),
97 seed_("seed"), 130 seed_("seed"),
98 reset_recorded_(false) {} 131 reset_recorded_(false) {}
99 132
100 void SetUp() override { 133 void SetUp() override {
101 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get()); 134 ProfilePrefStoreManager::RegisterProfilePrefs(profile_pref_registry_.get());
102 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration; 135 for (const PrefHashFilter::TrackedPreferenceMetadata* it = kConfiguration;
103 it != kConfiguration + arraysize(kConfiguration); 136 it != kConfiguration + arraysize(kConfiguration);
104 ++it) { 137 ++it) {
105 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) { 138 if (it->strategy == PrefHashFilter::TRACKING_STRATEGY_ATOMIC) {
106 profile_pref_registry_->RegisterStringPref(it->name, std::string()); 139 profile_pref_registry_->RegisterStringPref(it->name, std::string());
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 pref_service_factory.Create(profile_pref_registry_.get())); 194 pref_service_factory.Create(profile_pref_registry_.get()));
162 195
163 ProfilePrefStoreManager::ClearResetTime(pref_service.get()); 196 ProfilePrefStoreManager::ClearResetTime(pref_service.get());
164 } 197 }
165 198
166 void InitializePrefs() { 199 void InitializePrefs() {
167 // According to the implementation of ProfilePrefStoreManager, this is 200 // According to the implementation of ProfilePrefStoreManager, this is
168 // actually a SegregatedPrefStore backed by two underlying pref stores. 201 // actually a SegregatedPrefStore backed by two underlying pref stores.
169 scoped_refptr<PersistentPrefStore> pref_store = 202 scoped_refptr<PersistentPrefStore> pref_store =
170 manager_->CreateProfilePrefStore( 203 manager_->CreateProfilePrefStore(
171 main_message_loop_.task_runner(), 204 main_message_loop_.task_runner(), main_message_loop_.task_runner(),
172 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, 205 base::Bind(&ProfilePrefStoreManagerTest::RecordReset,
173 base::Unretained(this)), 206 base::Unretained(this)),
174 &mock_validation_delegate_); 207 mock_validation_delegate_weak_factory_.GetWeakPtr());
175 InitializePrefStore(pref_store.get()); 208 InitializePrefStore(pref_store.get());
176 pref_store = NULL; 209 pref_store = NULL;
177 base::RunLoop().RunUntilIdle(); 210 base::RunLoop().RunUntilIdle();
178 } 211 }
179 212
180 void DestroyPrefStore() { 213 void DestroyPrefStore() {
181 if (pref_store_.get()) { 214 if (pref_store_.get()) {
182 ClearResetRecorded(); 215 ClearResetRecorded();
183 // Force everything to be written to disk, triggering the PrefHashFilter 216 // Force everything to be written to disk, triggering the PrefHashFilter
184 // while our RegistryVerifier is watching. 217 // while our RegistryVerifier is watching.
185 pref_store_->CommitPendingWrite(); 218 pref_store_->CommitPendingWrite();
186 base::RunLoop().RunUntilIdle(); 219 base::RunLoop().RunUntilIdle();
187 220
188 pref_store_->RemoveObserver(&registry_verifier_); 221 pref_store_->RemoveObserver(&registry_verifier_);
189 pref_store_ = NULL; 222 pref_store_ = NULL;
190 // Nothing should have to happen on the background threads, but just in 223 // Nothing should have to happen on the background threads, but just in
191 // case... 224 // case...
192 base::RunLoop().RunUntilIdle(); 225 base::RunLoop().RunUntilIdle();
193 } 226 }
194 } 227 }
195 228
196 void InitializePrefStore(PersistentPrefStore* pref_store) { 229 void InitializePrefStore(PersistentPrefStore* pref_store) {
197 pref_store->AddObserver(&registry_verifier_); 230 pref_store->AddObserver(&registry_verifier_);
198 PersistentPrefStore::PrefReadError error = pref_store->ReadPrefs(); 231 PrefStoreReadObserver read_observer(pref_store);
232 PersistentPrefStore::PrefReadError error = read_observer.Read();
199 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); 233 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
200 pref_store->SetValue(kTrackedAtomic, 234 pref_store->SetValue(kTrackedAtomic,
201 base::MakeUnique<base::StringValue>(kFoobar), 235 base::MakeUnique<base::StringValue>(kFoobar),
202 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 236 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
203 pref_store->SetValue(kProtectedAtomic, 237 pref_store->SetValue(kProtectedAtomic,
204 base::MakeUnique<base::StringValue>(kHelloWorld), 238 base::MakeUnique<base::StringValue>(kHelloWorld),
205 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 239 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
206 pref_store->SetValue(kUnprotectedPref, 240 pref_store->SetValue(kUnprotectedPref,
207 base::MakeUnique<base::StringValue>(kFoobar), 241 base::MakeUnique<base::StringValue>(kFoobar),
208 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 242 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
209 pref_store->RemoveObserver(&registry_verifier_); 243 pref_store->RemoveObserver(&registry_verifier_);
210 pref_store->CommitPendingWrite(); 244 pref_store->CommitPendingWrite();
211 base::RunLoop().RunUntilIdle(); 245 base::RunLoop().RunUntilIdle();
212 } 246 }
213 247
214 void LoadExistingPrefs() { 248 void LoadExistingPrefs() {
215 DestroyPrefStore(); 249 DestroyPrefStore();
216 pref_store_ = manager_->CreateProfilePrefStore( 250 pref_store_ = manager_->CreateProfilePrefStore(
217 main_message_loop_.task_runner(), 251 main_message_loop_.task_runner(), main_message_loop_.task_runner(),
218 base::Bind(&ProfilePrefStoreManagerTest::RecordReset, 252 base::Bind(&ProfilePrefStoreManagerTest::RecordReset,
219 base::Unretained(this)), 253 base::Unretained(this)),
220 NULL); 254 NULL);
221 pref_store_->AddObserver(&registry_verifier_); 255 pref_store_->AddObserver(&registry_verifier_);
222 pref_store_->ReadPrefs(); 256 PrefStoreReadObserver read_observer(pref_store_);
257 read_observer.Read();
223 } 258 }
224 259
225 void ReplaceStringInPrefs(const std::string& find, 260 void ReplaceStringInPrefs(const std::string& find,
226 const std::string& replace) { 261 const std::string& replace) {
227 base::FileEnumerator file_enum(profile_dir_.GetPath(), true, 262 base::FileEnumerator file_enum(profile_dir_.GetPath(), true,
228 base::FileEnumerator::FILES); 263 base::FileEnumerator::FILES);
229 264
230 for (base::FilePath path = file_enum.Next(); !path.empty(); 265 for (base::FilePath path = file_enum.Next(); !path.empty();
231 path = file_enum.Next()) { 266 path = file_enum.Next()) {
232 // Tamper with the file's contents 267 // Tamper with the file's contents
(...skipping 26 matching lines...) Expand all
259 ADD_FAILURE() << "No validation observed for preference: " << pref_path; 294 ADD_FAILURE() << "No validation observed for preference: " << pref_path;
260 } 295 }
261 296
262 base::MessageLoop main_message_loop_; 297 base::MessageLoop main_message_loop_;
263 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_; 298 std::vector<PrefHashFilter::TrackedPreferenceMetadata> configuration_;
264 base::ScopedTempDir profile_dir_; 299 base::ScopedTempDir profile_dir_;
265 TestingPrefServiceSimple local_state_; 300 TestingPrefServiceSimple local_state_;
266 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_; 301 scoped_refptr<user_prefs::PrefRegistrySyncable> profile_pref_registry_;
267 RegistryVerifier registry_verifier_; 302 RegistryVerifier registry_verifier_;
268 MockValidationDelegate mock_validation_delegate_; 303 MockValidationDelegate mock_validation_delegate_;
304 base::WeakPtrFactory<TrackedPreferenceValidationDelegate>
305 mock_validation_delegate_weak_factory_;
269 std::unique_ptr<ProfilePrefStoreManager> manager_; 306 std::unique_ptr<ProfilePrefStoreManager> manager_;
270 scoped_refptr<PersistentPrefStore> pref_store_; 307 scoped_refptr<PersistentPrefStore> pref_store_;
271 308
272 std::string seed_; 309 std::string seed_;
273 310
274 private: 311 private:
275 void RecordReset() { 312 void RecordReset() {
276 // As-is |reset_recorded_| is only designed to remember a single reset, make 313 // As-is |reset_recorded_| is only designed to remember a single reset, make
277 // sure none was previously recorded (or that ClearResetRecorded() was 314 // sure none was previously recorded (or that ClearResetRecorded() was
278 // called). 315 // called).
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 517
481 // Trigger the logic that migrates it back to the unprotected preferences 518 // Trigger the logic that migrates it back to the unprotected preferences
482 // file. 519 // file.
483 pref_store_->SetValue(kProtectedAtomic, 520 pref_store_->SetValue(kProtectedAtomic,
484 base::WrapUnique(new base::StringValue(kGoodbyeWorld)), 521 base::WrapUnique(new base::StringValue(kGoodbyeWorld)),
485 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 522 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
486 LoadExistingPrefs(); 523 LoadExistingPrefs();
487 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld); 524 ExpectStringValueEquals(kProtectedAtomic, kGoodbyeWorld);
488 VerifyResetRecorded(false); 525 VerifyResetRecorded(false);
489 } 526 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698