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

Side by Side Diff: base/prefs/json_pref_store_unittest.cc

Issue 257003007: Introduce a new framework for back-and-forth tracked/protected preferences migration. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix some tests -- more to come Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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 "base/prefs/json_pref_store.h" 5 #include "base/prefs/json_pref_store.h"
6 6
7 #include "base/bind.h"
7 #include "base/file_util.h" 8 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h" 12 #include "base/path_service.h"
12 #include "base/prefs/pref_filter.h" 13 #include "base/prefs/pref_filter.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/sequenced_worker_pool.h" 18 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace base { 24 namespace base {
24 namespace { 25 namespace {
25 26
26 const char kHomePage[] = "homepage"; 27 const char kHomePage[] = "homepage";
27 28
29 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on
30 // to the |prefs| until explicitly asked to release them.
31 class InterceptingPrefFilter : public PrefFilter {
32 public:
33 InterceptingPrefFilter();
34 ~InterceptingPrefFilter();
35
36 // PrefFilter implementation:
37 virtual void FilterOnLoad(
38 const PostFilterOnLoadCallback& post_filter_on_load_callback,
39 scoped_ptr<base::DictionaryValue> pref_store_contents) OVERRIDE;
40 virtual void FilterUpdate(const std::string& path) OVERRIDE {}
41 virtual void FilterSerializeData(
42 const base::DictionaryValue* pref_store_contents) OVERRIDE {}
43
44 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; }
45
46 // Finalize an intercepted read, handing |intercept_prefs_| back to its
47 // JsonPrefStore.
48 void ReleasePrefs();
49
50 private:
51 void OnFileRead(const PostFilterOnLoadCallback& finalize_prefs_read,
gab 2014/05/05 13:53:46 unused method.
gab 2014/05/05 17:30:36 Done.
52 scoped_ptr<base::DictionaryValue> prefs);
53
54 PostFilterOnLoadCallback post_filter_on_load_callback_;
55 scoped_ptr<base::DictionaryValue> intercepted_prefs_;
56
57 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter);
58 };
59
60 InterceptingPrefFilter::InterceptingPrefFilter() {}
61 InterceptingPrefFilter::~InterceptingPrefFilter() {}
62
63 void InterceptingPrefFilter::FilterOnLoad(
64 const PostFilterOnLoadCallback& post_filter_on_load_callback,
65 scoped_ptr<base::DictionaryValue> pref_store_contents) {
66 post_filter_on_load_callback_ = post_filter_on_load_callback;
67 intercepted_prefs_ = pref_store_contents.Pass();
68 }
69
70 void InterceptingPrefFilter::ReleasePrefs() {
71 EXPECT_FALSE(post_filter_on_load_callback_.is_null());
72 post_filter_on_load_callback_.Run(intercepted_prefs_.Pass(), false);
73 }
74
28 class MockPrefStoreObserver : public PrefStore::Observer { 75 class MockPrefStoreObserver : public PrefStore::Observer {
29 public: 76 public:
30 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); 77 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
31 MOCK_METHOD1(OnInitializationCompleted, void (bool)); 78 MOCK_METHOD1(OnInitializationCompleted, void (bool));
32 }; 79 };
33 80
34 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { 81 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
35 public: 82 public:
36 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); 83 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
37 }; 84 };
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 // Serialize and compare to expected output. 197 // Serialize and compare to expected output.
151 ASSERT_TRUE(PathExists(golden_output_file)); 198 ASSERT_TRUE(PathExists(golden_output_file));
152 pref_store->CommitPendingWrite(); 199 pref_store->CommitPendingWrite();
153 RunLoop().RunUntilIdle(); 200 RunLoop().RunUntilIdle();
154 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); 201 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file));
155 ASSERT_TRUE(base::DeleteFile(output_file, false)); 202 ASSERT_TRUE(base::DeleteFile(output_file, false));
156 } 203 }
157 204
158 TEST_F(JsonPrefStoreTest, Basic) { 205 TEST_F(JsonPrefStoreTest, Basic) {
159 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), 206 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
160 temp_dir_.path().AppendASCII("write.json"))); 207 temp_dir_.path().AppendASCII("write.json")));
161 208
162 // Test that the persistent value can be loaded. 209 // Test that the persistent value can be loaded.
163 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 210 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
164 ASSERT_TRUE(PathExists(input_file)); 211 ASSERT_TRUE(PathExists(input_file));
165 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 212 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
166 input_file, 213 input_file,
167 message_loop_.message_loop_proxy().get(), 214 message_loop_.message_loop_proxy().get(),
168 scoped_ptr<PrefFilter>()); 215 scoped_ptr<PrefFilter>());
169 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 216 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
170 ASSERT_FALSE(pref_store->ReadOnly()); 217 EXPECT_FALSE(pref_store->ReadOnly());
218 EXPECT_TRUE(pref_store->IsInitializationComplete());
171 219
172 // The JSON file looks like this: 220 // The JSON file looks like this:
173 // { 221 // {
174 // "homepage": "http://www.cnn.com", 222 // "homepage": "http://www.cnn.com",
175 // "some_directory": "/usr/local/", 223 // "some_directory": "/usr/local/",
176 // "tabs": { 224 // "tabs": {
177 // "new_windows_in_tabs": true, 225 // "new_windows_in_tabs": true,
178 // "max_tabs": 20 226 // "max_tabs": 20
179 // } 227 // }
180 // } 228 // }
181 229
182 RunBasicJsonPrefStoreTest( 230 RunBasicJsonPrefStoreTest(
183 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); 231 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
184 } 232 }
185 233
186 TEST_F(JsonPrefStoreTest, BasicAsync) { 234 TEST_F(JsonPrefStoreTest, BasicAsync) {
187 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), 235 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
188 temp_dir_.path().AppendASCII("write.json"))); 236 temp_dir_.path().AppendASCII("write.json")));
189 237
190 // Test that the persistent value can be loaded. 238 // Test that the persistent value can be loaded.
191 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 239 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
192 ASSERT_TRUE(PathExists(input_file)); 240 ASSERT_TRUE(PathExists(input_file));
193 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 241 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
194 input_file, 242 input_file,
195 message_loop_.message_loop_proxy().get(), 243 message_loop_.message_loop_proxy().get(),
196 scoped_ptr<PrefFilter>()); 244 scoped_ptr<PrefFilter>());
197 245
198 { 246 {
199 MockPrefStoreObserver mock_observer; 247 MockPrefStoreObserver mock_observer;
200 pref_store->AddObserver(&mock_observer); 248 pref_store->AddObserver(&mock_observer);
201 249
202 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; 250 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
203 pref_store->ReadPrefsAsync(mock_error_delegate); 251 pref_store->ReadPrefsAsync(mock_error_delegate);
204 252
205 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 253 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
206 EXPECT_CALL(*mock_error_delegate, 254 EXPECT_CALL(*mock_error_delegate,
207 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); 255 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
208 RunLoop().RunUntilIdle(); 256 RunLoop().RunUntilIdle();
209 pref_store->RemoveObserver(&mock_observer); 257 pref_store->RemoveObserver(&mock_observer);
210 258
211 ASSERT_FALSE(pref_store->ReadOnly()); 259 EXPECT_FALSE(pref_store->ReadOnly());
260 EXPECT_TRUE(pref_store->IsInitializationComplete());
212 } 261 }
213 262
214 // The JSON file looks like this: 263 // The JSON file looks like this:
215 // { 264 // {
216 // "homepage": "http://www.cnn.com", 265 // "homepage": "http://www.cnn.com",
217 // "some_directory": "/usr/local/", 266 // "some_directory": "/usr/local/",
218 // "tabs": { 267 // "tabs": {
219 // "new_windows_in_tabs": true, 268 // "new_windows_in_tabs": true,
220 // "max_tabs": 20 269 // "max_tabs": 20
221 // } 270 // }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 343
295 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 344 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
296 EXPECT_CALL(*mock_error_delegate, 345 EXPECT_CALL(*mock_error_delegate,
297 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); 346 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
298 RunLoop().RunUntilIdle(); 347 RunLoop().RunUntilIdle();
299 pref_store->RemoveObserver(&mock_observer); 348 pref_store->RemoveObserver(&mock_observer);
300 349
301 EXPECT_FALSE(pref_store->ReadOnly()); 350 EXPECT_FALSE(pref_store->ReadOnly());
302 } 351 }
303 352
353 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) {
354 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
355 temp_dir_.path().AppendASCII("write.json")));
356
357 // Test that the persistent value can be loaded.
358 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
359 ASSERT_TRUE(PathExists(input_file));
360
361 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter(
362 new InterceptingPrefFilter());
363 InterceptingPrefFilter* raw_intercepting_pref_filter_ =
364 intercepting_pref_filter.get();
365 scoped_refptr<JsonPrefStore> pref_store =
366 new JsonPrefStore(input_file,
367 message_loop_.message_loop_proxy().get(),
368 intercepting_pref_filter.PassAs<PrefFilter>());
369
370 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
371 EXPECT_FALSE(pref_store->ReadOnly());
372
373 // The store shouldn't be considered initialized until the interceptor
374 // returns.
375 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
376 EXPECT_FALSE(pref_store->IsInitializationComplete());
377 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
378
379 raw_intercepting_pref_filter_->ReleasePrefs();
380
381 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
382 EXPECT_TRUE(pref_store->IsInitializationComplete());
383 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
384
385 // The JSON file looks like this:
386 // {
387 // "homepage": "http://www.cnn.com",
388 // "some_directory": "/usr/local/",
389 // "tabs": {
390 // "new_windows_in_tabs": true,
391 // "max_tabs": 20
392 // }
393 // }
394
395 RunBasicJsonPrefStoreTest(
396 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
397 }
398
399 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) {
400 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
401 temp_dir_.path().AppendASCII("write.json")));
402
403 // Test that the persistent value can be loaded.
404 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
405 ASSERT_TRUE(PathExists(input_file));
406
407 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter(
408 new InterceptingPrefFilter());
409 InterceptingPrefFilter* raw_intercepting_pref_filter_ =
410 intercepting_pref_filter.get();
411 scoped_refptr<JsonPrefStore> pref_store =
412 new JsonPrefStore(input_file,
413 message_loop_.message_loop_proxy().get(),
414 intercepting_pref_filter.PassAs<PrefFilter>());
415
416 MockPrefStoreObserver mock_observer;
417 pref_store->AddObserver(&mock_observer);
418
419 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below.
420 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
421
422 {
423 pref_store->ReadPrefsAsync(mock_error_delegate);
424
425 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0);
426 // EXPECT_CALL(*mock_error_delegate,
gab 2014/05/05 13:53:46 Fix these; for some reason the mock gtest framewor
gab 2014/05/05 17:30:36 Done.
427 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
428 RunLoop().RunUntilIdle();
429
430 EXPECT_FALSE(pref_store->ReadOnly());
431 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
432 EXPECT_FALSE(pref_store->IsInitializationComplete());
433 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
434 }
435
436 {
437 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
438 // EXPECT_CALL(*mock_error_delegate,
439 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
440
441 raw_intercepting_pref_filter_->ReleasePrefs();
442
443 EXPECT_FALSE(pref_store->ReadOnly());
444 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
445 EXPECT_TRUE(pref_store->IsInitializationComplete());
446 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
447 }
448
449 pref_store->RemoveObserver(&mock_observer);
450
451 // The JSON file looks like this:
452 // {
453 // "homepage": "http://www.cnn.com",
454 // "some_directory": "/usr/local/",
455 // "tabs": {
456 // "new_windows_in_tabs": true,
457 // "max_tabs": 20
458 // }
459 // }
460
461 RunBasicJsonPrefStoreTest(
462 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
463 }
464
304 } // namespace base 465 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698