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 virtual void TearDown() { |
Paweł Hajdan Jr.
2011/04/08 15:58:17
Empty TearDown can be removed.
Mike West
2011/04/11 14:47:04
Done.
| |
39 // Clean up test directory | |
40 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | |
41 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
42 } | 35 } |
43 | 36 |
44 // the path to temporary directory used to contain the test operations | 37 // the path to temporary directory used to contain the test operations |
45 FilePath test_dir_; | 38 ScopedTempDir temp_dir_; |
46 // the path to the directory where the test data is stored | 39 // the path to the directory where the test data is stored |
47 FilePath data_dir_; | 40 FilePath data_dir_; |
48 // A message loop that we can use as the file thread message loop. | 41 // A message loop that we can use as the file thread message loop. |
49 MessageLoop message_loop_; | 42 MessageLoop message_loop_; |
50 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 43 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
51 }; | 44 }; |
52 | 45 |
53 // Test fallback behavior for a nonexistent file. | 46 // Test fallback behavior for a nonexistent file. |
54 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 47 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
55 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 48 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
56 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 49 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
57 scoped_refptr<JsonPrefStore> pref_store = | 50 scoped_refptr<JsonPrefStore> pref_store = |
58 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); | 51 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
59 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 52 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
60 pref_store->ReadPrefs()); | 53 pref_store->ReadPrefs()); |
61 EXPECT_FALSE(pref_store->ReadOnly()); | 54 EXPECT_FALSE(pref_store->ReadOnly()); |
62 } | 55 } |
63 | 56 |
64 // Test fallback behavior for an invalid file. | 57 // Test fallback behavior for an invalid file. |
65 TEST_F(JsonPrefStoreTest, InvalidFile) { | 58 TEST_F(JsonPrefStoreTest, InvalidFile) { |
66 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 59 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
67 FilePath invalid_file = test_dir_.AppendASCII("invalid.json"); | 60 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
68 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 61 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
69 scoped_refptr<JsonPrefStore> pref_store = | 62 scoped_refptr<JsonPrefStore> pref_store = |
70 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); | 63 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); |
71 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
72 pref_store->ReadPrefs()); | 65 pref_store->ReadPrefs()); |
73 EXPECT_FALSE(pref_store->ReadOnly()); | 66 EXPECT_FALSE(pref_store->ReadOnly()); |
74 | 67 |
75 // The file should have been moved aside. | 68 // The file should have been moved aside. |
76 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 69 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
77 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad"); | 70 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
78 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 71 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
79 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 72 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
80 moved_aside)); | 73 moved_aside)); |
81 } | 74 } |
82 | 75 |
83 TEST_F(JsonPrefStoreTest, Basic) { | 76 TEST_F(JsonPrefStoreTest, Basic) { |
84 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 77 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
85 test_dir_.AppendASCII("write.json"))); | 78 temp_dir_.path().AppendASCII("write.json"))); |
86 | 79 |
87 // Test that the persistent value can be loaded. | 80 // Test that the persistent value can be loaded. |
88 FilePath input_file = test_dir_.AppendASCII("write.json"); | 81 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
89 ASSERT_TRUE(file_util::PathExists(input_file)); | 82 ASSERT_TRUE(file_util::PathExists(input_file)); |
90 scoped_refptr<JsonPrefStore> pref_store = | 83 scoped_refptr<JsonPrefStore> pref_store = |
91 new JsonPrefStore(input_file, message_loop_proxy_.get()); | 84 new JsonPrefStore(input_file, message_loop_proxy_.get()); |
92 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 85 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
93 ASSERT_FALSE(pref_store->ReadOnly()); | 86 ASSERT_FALSE(pref_store->ReadOnly()); |
94 | 87 |
95 // The JSON file looks like this: | 88 // The JSON file looks like this: |
96 // { | 89 // { |
97 // "homepage": "http://www.cnn.com", | 90 // "homepage": "http://www.cnn.com", |
98 // "some_directory": "/usr/local/", | 91 // "some_directory": "/usr/local/", |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 | 156 |
164 // Serialize and compare to expected output. | 157 // Serialize and compare to expected output. |
165 FilePath output_file = input_file; | 158 FilePath output_file = input_file; |
166 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); | 159 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
167 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 160 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
168 ASSERT_TRUE(pref_store->WritePrefs()); | 161 ASSERT_TRUE(pref_store->WritePrefs()); |
169 MessageLoop::current()->RunAllPending(); | 162 MessageLoop::current()->RunAllPending(); |
170 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 163 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
171 ASSERT_TRUE(file_util::Delete(output_file, false)); | 164 ASSERT_TRUE(file_util::Delete(output_file, false)); |
172 } | 165 } |
OLD | NEW |