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

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

Issue 273243002: Revert 269415 "Introduce a new framework for back-and-forth trac..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: 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
« no previous file with comments | « trunk/src/base/prefs/json_pref_store.cc ('k') | trunk/src/base/prefs/persistent_pref_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
8 #include "base/file_util.h" 7 #include "base/file_util.h"
9 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h" 11 #include "base/path_service.h"
14 #include "base/prefs/pref_filter.h" 12 #include "base/prefs/pref_filter.h"
15 #include "base/run_loop.h" 13 #include "base/run_loop.h"
16 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/sequenced_worker_pool.h" 17 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
21 #include "base/values.h" 19 #include "base/values.h"
22 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
24 22
25 namespace base { 23 namespace base {
26 namespace { 24 namespace {
27 25
28 const char kHomePage[] = "homepage"; 26 const char kHomePage[] = "homepage";
29 27
30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on
31 // to the |prefs| until explicitly asked to release them.
32 class InterceptingPrefFilter : public PrefFilter {
33 public:
34 InterceptingPrefFilter();
35 virtual ~InterceptingPrefFilter();
36
37 // PrefFilter implementation:
38 virtual void FilterOnLoad(
39 const PostFilterOnLoadCallback& post_filter_on_load_callback,
40 scoped_ptr<base::DictionaryValue> pref_store_contents) OVERRIDE;
41 virtual void FilterUpdate(const std::string& path) OVERRIDE {}
42 virtual void FilterSerializeData(
43 const base::DictionaryValue* pref_store_contents) OVERRIDE {}
44
45 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; }
46
47 // Finalize an intercepted read, handing |intercept_prefs_| back to its
48 // JsonPrefStore.
49 void ReleasePrefs();
50
51 private:
52 PostFilterOnLoadCallback post_filter_on_load_callback_;
53 scoped_ptr<base::DictionaryValue> intercepted_prefs_;
54
55 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter);
56 };
57
58 InterceptingPrefFilter::InterceptingPrefFilter() {}
59 InterceptingPrefFilter::~InterceptingPrefFilter() {}
60
61 void InterceptingPrefFilter::FilterOnLoad(
62 const PostFilterOnLoadCallback& post_filter_on_load_callback,
63 scoped_ptr<base::DictionaryValue> pref_store_contents) {
64 post_filter_on_load_callback_ = post_filter_on_load_callback;
65 intercepted_prefs_ = pref_store_contents.Pass();
66 }
67
68 void InterceptingPrefFilter::ReleasePrefs() {
69 EXPECT_FALSE(post_filter_on_load_callback_.is_null());
70 post_filter_on_load_callback_.Run(intercepted_prefs_.Pass(), false);
71 post_filter_on_load_callback_.Reset();
72 }
73
74 class MockPrefStoreObserver : public PrefStore::Observer { 28 class MockPrefStoreObserver : public PrefStore::Observer {
75 public: 29 public:
76 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); 30 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
77 MOCK_METHOD1(OnInitializationCompleted, void (bool)); 31 MOCK_METHOD1(OnInitializationCompleted, void (bool));
78 }; 32 };
79 33
80 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { 34 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
81 public: 35 public:
82 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); 36 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
83 }; 37 };
84 38
85 } // namespace 39 } // namespace
86 40
87 class JsonPrefStoreTest : public testing::Test { 41 class JsonPrefStoreTest : public testing::Test {
88 protected: 42 protected:
89 virtual void SetUp() OVERRIDE { 43 virtual void SetUp() OVERRIDE {
90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
91 45
92 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); 46 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
93 data_dir_ = data_dir_.AppendASCII("prefs"); 47 data_dir_ = data_dir_.AppendASCII("prefs");
94 ASSERT_TRUE(PathExists(data_dir_)); 48 ASSERT_TRUE(PathExists(data_dir_));
95 } 49 }
96 50
97 virtual void TearDown() OVERRIDE {
98 // Make sure all pending tasks have been processed (e.g., deleting the
99 // JsonPrefStore may post write tasks).
100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
101 message_loop_.Run();
102 }
103
104 // The path to temporary directory used to contain the test operations. 51 // The path to temporary directory used to contain the test operations.
105 base::ScopedTempDir temp_dir_; 52 base::ScopedTempDir temp_dir_;
106 // The path to the directory where the test data is stored. 53 // The path to the directory where the test data is stored.
107 base::FilePath data_dir_; 54 base::FilePath data_dir_;
108 // A message loop that we can use as the file thread message loop. 55 // A message loop that we can use as the file thread message loop.
109 MessageLoop message_loop_; 56 MessageLoop message_loop_;
110 }; 57 };
111 58
112 // Test fallback behavior for a nonexistent file. 59 // Test fallback behavior for a nonexistent file.
113 TEST_F(JsonPrefStoreTest, NonExistentFile) { 60 TEST_F(JsonPrefStoreTest, NonExistentFile) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 // Serialize and compare to expected output. 150 // Serialize and compare to expected output.
204 ASSERT_TRUE(PathExists(golden_output_file)); 151 ASSERT_TRUE(PathExists(golden_output_file));
205 pref_store->CommitPendingWrite(); 152 pref_store->CommitPendingWrite();
206 RunLoop().RunUntilIdle(); 153 RunLoop().RunUntilIdle();
207 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); 154 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file));
208 ASSERT_TRUE(base::DeleteFile(output_file, false)); 155 ASSERT_TRUE(base::DeleteFile(output_file, false));
209 } 156 }
210 157
211 TEST_F(JsonPrefStoreTest, Basic) { 158 TEST_F(JsonPrefStoreTest, Basic) {
212 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), 159 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
213 temp_dir_.path().AppendASCII("write.json"))); 160 temp_dir_.path().AppendASCII("write.json")));
214 161
215 // Test that the persistent value can be loaded. 162 // Test that the persistent value can be loaded.
216 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 163 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
217 ASSERT_TRUE(PathExists(input_file)); 164 ASSERT_TRUE(PathExists(input_file));
218 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 165 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
219 input_file, 166 input_file,
220 message_loop_.message_loop_proxy().get(), 167 message_loop_.message_loop_proxy().get(),
221 scoped_ptr<PrefFilter>()); 168 scoped_ptr<PrefFilter>());
222 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 169 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
223 EXPECT_FALSE(pref_store->ReadOnly()); 170 ASSERT_FALSE(pref_store->ReadOnly());
224 EXPECT_TRUE(pref_store->IsInitializationComplete());
225 171
226 // The JSON file looks like this: 172 // The JSON file looks like this:
227 // { 173 // {
228 // "homepage": "http://www.cnn.com", 174 // "homepage": "http://www.cnn.com",
229 // "some_directory": "/usr/local/", 175 // "some_directory": "/usr/local/",
230 // "tabs": { 176 // "tabs": {
231 // "new_windows_in_tabs": true, 177 // "new_windows_in_tabs": true,
232 // "max_tabs": 20 178 // "max_tabs": 20
233 // } 179 // }
234 // } 180 // }
235 181
236 RunBasicJsonPrefStoreTest( 182 RunBasicJsonPrefStoreTest(
237 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); 183 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
238 } 184 }
239 185
240 TEST_F(JsonPrefStoreTest, BasicAsync) { 186 TEST_F(JsonPrefStoreTest, BasicAsync) {
241 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), 187 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
242 temp_dir_.path().AppendASCII("write.json"))); 188 temp_dir_.path().AppendASCII("write.json")));
243 189
244 // Test that the persistent value can be loaded. 190 // Test that the persistent value can be loaded.
245 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 191 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
246 ASSERT_TRUE(PathExists(input_file)); 192 ASSERT_TRUE(PathExists(input_file));
247 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( 193 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
248 input_file, 194 input_file,
249 message_loop_.message_loop_proxy().get(), 195 message_loop_.message_loop_proxy().get(),
250 scoped_ptr<PrefFilter>()); 196 scoped_ptr<PrefFilter>());
251 197
252 { 198 {
253 MockPrefStoreObserver mock_observer; 199 MockPrefStoreObserver mock_observer;
254 pref_store->AddObserver(&mock_observer); 200 pref_store->AddObserver(&mock_observer);
255 201
256 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; 202 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
257 pref_store->ReadPrefsAsync(mock_error_delegate); 203 pref_store->ReadPrefsAsync(mock_error_delegate);
258 204
259 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 205 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
260 EXPECT_CALL(*mock_error_delegate, 206 EXPECT_CALL(*mock_error_delegate,
261 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); 207 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
262 RunLoop().RunUntilIdle(); 208 RunLoop().RunUntilIdle();
263 pref_store->RemoveObserver(&mock_observer); 209 pref_store->RemoveObserver(&mock_observer);
264 210
265 EXPECT_FALSE(pref_store->ReadOnly()); 211 ASSERT_FALSE(pref_store->ReadOnly());
266 EXPECT_TRUE(pref_store->IsInitializationComplete());
267 } 212 }
268 213
269 // The JSON file looks like this: 214 // The JSON file looks like this:
270 // { 215 // {
271 // "homepage": "http://www.cnn.com", 216 // "homepage": "http://www.cnn.com",
272 // "some_directory": "/usr/local/", 217 // "some_directory": "/usr/local/",
273 // "tabs": { 218 // "tabs": {
274 // "new_windows_in_tabs": true, 219 // "new_windows_in_tabs": true,
275 // "max_tabs": 20 220 // "max_tabs": 20
276 // } 221 // }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 294
350 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 295 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
351 EXPECT_CALL(*mock_error_delegate, 296 EXPECT_CALL(*mock_error_delegate,
352 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); 297 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
353 RunLoop().RunUntilIdle(); 298 RunLoop().RunUntilIdle();
354 pref_store->RemoveObserver(&mock_observer); 299 pref_store->RemoveObserver(&mock_observer);
355 300
356 EXPECT_FALSE(pref_store->ReadOnly()); 301 EXPECT_FALSE(pref_store->ReadOnly());
357 } 302 }
358 303
359 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) {
360 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
361 temp_dir_.path().AppendASCII("write.json")));
362
363 // Test that the persistent value can be loaded.
364 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
365 ASSERT_TRUE(PathExists(input_file));
366
367 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter(
368 new InterceptingPrefFilter());
369 InterceptingPrefFilter* raw_intercepting_pref_filter_ =
370 intercepting_pref_filter.get();
371 scoped_refptr<JsonPrefStore> pref_store =
372 new JsonPrefStore(input_file,
373 message_loop_.message_loop_proxy().get(),
374 intercepting_pref_filter.PassAs<PrefFilter>());
375
376 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE,
377 pref_store->ReadPrefs());
378 EXPECT_FALSE(pref_store->ReadOnly());
379
380 // The store shouldn't be considered initialized until the interceptor
381 // returns.
382 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
383 EXPECT_FALSE(pref_store->IsInitializationComplete());
384 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
385
386 raw_intercepting_pref_filter_->ReleasePrefs();
387
388 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
389 EXPECT_TRUE(pref_store->IsInitializationComplete());
390 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
391
392 // The JSON file looks like this:
393 // {
394 // "homepage": "http://www.cnn.com",
395 // "some_directory": "/usr/local/",
396 // "tabs": {
397 // "new_windows_in_tabs": true,
398 // "max_tabs": 20
399 // }
400 // }
401
402 RunBasicJsonPrefStoreTest(
403 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
404 }
405
406 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) {
407 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
408 temp_dir_.path().AppendASCII("write.json")));
409
410 // Test that the persistent value can be loaded.
411 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
412 ASSERT_TRUE(PathExists(input_file));
413
414 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter(
415 new InterceptingPrefFilter());
416 InterceptingPrefFilter* raw_intercepting_pref_filter_ =
417 intercepting_pref_filter.get();
418 scoped_refptr<JsonPrefStore> pref_store =
419 new JsonPrefStore(input_file,
420 message_loop_.message_loop_proxy().get(),
421 intercepting_pref_filter.PassAs<PrefFilter>());
422
423 MockPrefStoreObserver mock_observer;
424 pref_store->AddObserver(&mock_observer);
425
426 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below.
427 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
428
429 {
430 pref_store->ReadPrefsAsync(mock_error_delegate);
431
432 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(0);
433 // EXPECT_CALL(*mock_error_delegate,
434 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
435 RunLoop().RunUntilIdle();
436
437 EXPECT_FALSE(pref_store->ReadOnly());
438 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs());
439 EXPECT_FALSE(pref_store->IsInitializationComplete());
440 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL));
441 }
442
443 {
444 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
445 // EXPECT_CALL(*mock_error_delegate,
446 // OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
447
448 raw_intercepting_pref_filter_->ReleasePrefs();
449
450 EXPECT_FALSE(pref_store->ReadOnly());
451 EXPECT_FALSE(raw_intercepting_pref_filter_->has_intercepted_prefs());
452 EXPECT_TRUE(pref_store->IsInitializationComplete());
453 EXPECT_TRUE(pref_store->GetValue(kHomePage, NULL));
454 }
455
456 pref_store->RemoveObserver(&mock_observer);
457
458 // The JSON file looks like this:
459 // {
460 // "homepage": "http://www.cnn.com",
461 // "some_directory": "/usr/local/",
462 // "tabs": {
463 // "new_windows_in_tabs": true,
464 // "max_tabs": 20
465 // }
466 // }
467
468 RunBasicJsonPrefStoreTest(
469 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
470 }
471
472 } // namespace base 304 } // namespace base
OLDNEW
« no previous file with comments | « trunk/src/base/prefs/json_pref_store.cc ('k') | trunk/src/base/prefs/persistent_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698