OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "base/memory/scoped_temp_dir.h" |
16 #include "chrome/common/json_pref_store.h" | 17 #include "chrome/common/json_pref_store.h" |
17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 class JsonPrefStoreTest : public testing::Test { | 22 class JsonPrefStoreTest : public testing::Test { |
22 protected: | 23 protected: |
23 virtual void SetUp() { | 24 virtual void SetUp() { |
24 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); | 25 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); |
25 // Name a subdirectory of the temp directory. | |
26 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | |
27 test_dir_ = test_dir_.AppendASCII("JsonPrefStoreTest"); | |
28 | 26 |
29 // Create a fresh, empty copy of this directory. | 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
30 file_util::Delete(test_dir_, true); | |
31 file_util::CreateDirectory(test_dir_); | |
32 | 28 |
33 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
34 data_dir_ = data_dir_.AppendASCII("pref_service"); | 30 data_dir_ = data_dir_.AppendASCII("pref_service"); |
35 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 31 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
36 } | 32 } |
37 | 33 |
38 virtual void TearDown() { | 34 // The path to temporary directory used to contain the test operations. |
39 // Clean up test directory | 35 ScopedTempDir temp_dir_; |
40 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | 36 // The path to the directory where the test data is stored. |
41 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
42 } | |
43 | |
44 // the path to temporary directory used to contain the test operations | |
45 FilePath test_dir_; | |
46 // the path to the directory where the test data is stored | |
47 FilePath data_dir_; | 37 FilePath data_dir_; |
48 // A message loop that we can use as the file thread message loop. | 38 // A message loop that we can use as the file thread message loop. |
49 MessageLoop message_loop_; | 39 MessageLoop message_loop_; |
50 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 40 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
51 }; | 41 }; |
52 | 42 |
53 // Test fallback behavior for a nonexistent file. | 43 // Test fallback behavior for a nonexistent file. |
54 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 44 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
55 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 45 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
56 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 46 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
57 scoped_refptr<JsonPrefStore> pref_store = | 47 scoped_refptr<JsonPrefStore> pref_store = |
58 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); | 48 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
59 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 49 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
60 pref_store->ReadPrefs()); | 50 pref_store->ReadPrefs()); |
61 EXPECT_FALSE(pref_store->ReadOnly()); | 51 EXPECT_FALSE(pref_store->ReadOnly()); |
62 } | 52 } |
63 | 53 |
64 // Test fallback behavior for an invalid file. | 54 // Test fallback behavior for an invalid file. |
65 TEST_F(JsonPrefStoreTest, InvalidFile) { | 55 TEST_F(JsonPrefStoreTest, InvalidFile) { |
66 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 56 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
67 FilePath invalid_file = test_dir_.AppendASCII("invalid.json"); | 57 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
68 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 58 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
69 scoped_refptr<JsonPrefStore> pref_store = | 59 scoped_refptr<JsonPrefStore> pref_store = |
70 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); | 60 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); |
71 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 61 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
72 pref_store->ReadPrefs()); | 62 pref_store->ReadPrefs()); |
73 EXPECT_FALSE(pref_store->ReadOnly()); | 63 EXPECT_FALSE(pref_store->ReadOnly()); |
74 | 64 |
75 // The file should have been moved aside. | 65 // The file should have been moved aside. |
76 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 66 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
77 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad"); | 67 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
78 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 68 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
79 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 69 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
80 moved_aside)); | 70 moved_aside)); |
81 } | 71 } |
82 | 72 |
83 TEST_F(JsonPrefStoreTest, Basic) { | 73 TEST_F(JsonPrefStoreTest, Basic) { |
84 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 74 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
85 test_dir_.AppendASCII("write.json"))); | 75 temp_dir_.path().AppendASCII("write.json"))); |
86 | 76 |
87 // Test that the persistent value can be loaded. | 77 // Test that the persistent value can be loaded. |
88 FilePath input_file = test_dir_.AppendASCII("write.json"); | 78 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
89 ASSERT_TRUE(file_util::PathExists(input_file)); | 79 ASSERT_TRUE(file_util::PathExists(input_file)); |
90 scoped_refptr<JsonPrefStore> pref_store = | 80 scoped_refptr<JsonPrefStore> pref_store = |
91 new JsonPrefStore(input_file, message_loop_proxy_.get()); | 81 new JsonPrefStore(input_file, message_loop_proxy_.get()); |
92 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 82 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
93 ASSERT_FALSE(pref_store->ReadOnly()); | 83 ASSERT_FALSE(pref_store->ReadOnly()); |
94 | 84 |
95 // The JSON file looks like this: | 85 // The JSON file looks like this: |
96 // { | 86 // { |
97 // "homepage": "http://www.cnn.com", | 87 // "homepage": "http://www.cnn.com", |
98 // "some_directory": "/usr/local/", | 88 // "some_directory": "/usr/local/", |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 153 |
164 // Serialize and compare to expected output. | 154 // Serialize and compare to expected output. |
165 FilePath output_file = input_file; | 155 FilePath output_file = input_file; |
166 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); | 156 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
167 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 157 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
168 ASSERT_TRUE(pref_store->WritePrefs()); | 158 ASSERT_TRUE(pref_store->WritePrefs()); |
169 MessageLoop::current()->RunAllPending(); | 159 MessageLoop::current()->RunAllPending(); |
170 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 160 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
171 ASSERT_TRUE(file_util::Delete(output_file, false)); | 161 ASSERT_TRUE(file_util::Delete(output_file, false)); |
172 } | 162 } |
OLD | NEW |