OLD | NEW |
1 // Copyright (c) 2010 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/message_loop.h" | 6 #include "base/message_loop.h" |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/ref_counted.h" |
9 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
10 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/common/json_pref_store.h" | 16 #include "chrome/common/json_pref_store.h" |
16 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 27 matching lines...) Expand all Loading... |
46 FilePath data_dir_; | 47 FilePath data_dir_; |
47 // A message loop that we can use as the file thread message loop. | 48 // A message loop that we can use as the file thread message loop. |
48 MessageLoop message_loop_; | 49 MessageLoop message_loop_; |
49 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 50 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
50 }; | 51 }; |
51 | 52 |
52 // Test fallback behavior for a nonexistent file. | 53 // Test fallback behavior for a nonexistent file. |
53 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 54 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
54 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 55 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
55 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 56 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
56 JsonPrefStore pref_store(bogus_input_file, message_loop_proxy_.get()); | 57 scoped_refptr<JsonPrefStore> pref_store = |
| 58 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
57 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 59 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
58 pref_store.ReadPrefs()); | 60 pref_store->ReadPrefs()); |
59 EXPECT_FALSE(pref_store.ReadOnly()); | 61 EXPECT_FALSE(pref_store->ReadOnly()); |
60 } | 62 } |
61 | 63 |
62 // Test fallback behavior for an invalid file. | 64 // Test fallback behavior for an invalid file. |
63 TEST_F(JsonPrefStoreTest, InvalidFile) { | 65 TEST_F(JsonPrefStoreTest, InvalidFile) { |
64 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 66 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
65 FilePath invalid_file = test_dir_.AppendASCII("invalid.json"); | 67 FilePath invalid_file = test_dir_.AppendASCII("invalid.json"); |
66 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 68 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
67 JsonPrefStore pref_store(invalid_file, message_loop_proxy_.get()); | 69 scoped_refptr<JsonPrefStore> pref_store = |
| 70 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); |
68 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 71 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
69 pref_store.ReadPrefs()); | 72 pref_store->ReadPrefs()); |
70 EXPECT_FALSE(pref_store.ReadOnly()); | 73 EXPECT_FALSE(pref_store->ReadOnly()); |
71 | 74 |
72 // The file should have been moved aside. | 75 // The file should have been moved aside. |
73 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 76 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
74 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad"); | 77 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad"); |
75 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 78 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
76 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 79 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
77 moved_aside)); | 80 moved_aside)); |
78 } | 81 } |
79 | 82 |
80 TEST_F(JsonPrefStoreTest, Basic) { | 83 TEST_F(JsonPrefStoreTest, Basic) { |
81 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 84 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
82 test_dir_.AppendASCII("write.json"))); | 85 test_dir_.AppendASCII("write.json"))); |
83 | 86 |
84 // Test that the persistent value can be loaded. | 87 // Test that the persistent value can be loaded. |
85 FilePath input_file = test_dir_.AppendASCII("write.json"); | 88 FilePath input_file = test_dir_.AppendASCII("write.json"); |
86 ASSERT_TRUE(file_util::PathExists(input_file)); | 89 ASSERT_TRUE(file_util::PathExists(input_file)); |
87 JsonPrefStore pref_store(input_file, message_loop_proxy_.get()); | 90 scoped_refptr<JsonPrefStore> pref_store = |
88 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store.ReadPrefs()); | 91 new JsonPrefStore(input_file, message_loop_proxy_.get()); |
89 ASSERT_FALSE(pref_store.ReadOnly()); | 92 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 93 ASSERT_FALSE(pref_store->ReadOnly()); |
90 | 94 |
91 // The JSON file looks like this: | 95 // The JSON file looks like this: |
92 // { | 96 // { |
93 // "homepage": "http://www.cnn.com", | 97 // "homepage": "http://www.cnn.com", |
94 // "some_directory": "/usr/local/", | 98 // "some_directory": "/usr/local/", |
95 // "tabs": { | 99 // "tabs": { |
96 // "new_windows_in_tabs": true, | 100 // "new_windows_in_tabs": true, |
97 // "max_tabs": 20 | 101 // "max_tabs": 20 |
98 // } | 102 // } |
99 // } | 103 // } |
100 | 104 |
101 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; | 105 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
102 const char kMaxTabs[] = "tabs.max_tabs"; | 106 const char kMaxTabs[] = "tabs.max_tabs"; |
103 const char kLongIntPref[] = "long_int.pref"; | 107 const char kLongIntPref[] = "long_int.pref"; |
104 | 108 |
105 std::string cnn("http://www.cnn.com"); | 109 std::string cnn("http://www.cnn.com"); |
106 | 110 |
107 Value* actual; | 111 Value* actual; |
108 EXPECT_EQ(PrefStore::READ_OK, | 112 EXPECT_EQ(PrefStore::READ_OK, |
109 pref_store.GetValue(prefs::kHomePage, &actual)); | 113 pref_store->GetValue(prefs::kHomePage, &actual)); |
110 std::string string_value; | 114 std::string string_value; |
111 EXPECT_TRUE(actual->GetAsString(&string_value)); | 115 EXPECT_TRUE(actual->GetAsString(&string_value)); |
112 EXPECT_EQ(cnn, string_value); | 116 EXPECT_EQ(cnn, string_value); |
113 | 117 |
114 const char kSomeDirectory[] = "some_directory"; | 118 const char kSomeDirectory[] = "some_directory"; |
115 | 119 |
116 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kSomeDirectory, &actual)); | 120 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
117 FilePath::StringType path; | 121 FilePath::StringType path; |
118 EXPECT_TRUE(actual->GetAsString(&path)); | 122 EXPECT_TRUE(actual->GetAsString(&path)); |
119 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 123 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
120 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 124 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
121 | 125 |
122 pref_store.SetValue(kSomeDirectory, | 126 pref_store->SetValue(kSomeDirectory, |
123 Value::CreateStringValue(some_path.value())); | 127 Value::CreateStringValue(some_path.value())); |
124 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kSomeDirectory, &actual)); | 128 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
125 EXPECT_TRUE(actual->GetAsString(&path)); | 129 EXPECT_TRUE(actual->GetAsString(&path)); |
126 EXPECT_EQ(some_path.value(), path); | 130 EXPECT_EQ(some_path.value(), path); |
127 | 131 |
128 // Test reading some other data types from sub-dictionaries. | 132 // Test reading some other data types from sub-dictionaries. |
129 EXPECT_EQ(PrefStore::READ_OK, | 133 EXPECT_EQ(PrefStore::READ_OK, |
130 pref_store.GetValue(kNewWindowsInTabs, &actual)); | 134 pref_store->GetValue(kNewWindowsInTabs, &actual)); |
131 bool boolean = false; | 135 bool boolean = false; |
132 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 136 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
133 EXPECT_TRUE(boolean); | 137 EXPECT_TRUE(boolean); |
134 | 138 |
135 pref_store.SetValue(kNewWindowsInTabs, | 139 pref_store->SetValue(kNewWindowsInTabs, |
136 Value::CreateBooleanValue(false)); | 140 Value::CreateBooleanValue(false)); |
137 EXPECT_EQ(PrefStore::READ_OK, | 141 EXPECT_EQ(PrefStore::READ_OK, |
138 pref_store.GetValue(kNewWindowsInTabs, &actual)); | 142 pref_store->GetValue(kNewWindowsInTabs, &actual)); |
139 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 143 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
140 EXPECT_FALSE(boolean); | 144 EXPECT_FALSE(boolean); |
141 | 145 |
142 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kMaxTabs, &actual)); | 146 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
143 int integer = 0; | 147 int integer = 0; |
144 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 148 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
145 EXPECT_EQ(20, integer); | 149 EXPECT_EQ(20, integer); |
146 pref_store.SetValue(kMaxTabs, Value::CreateIntegerValue(10)); | 150 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); |
147 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kMaxTabs, &actual)); | 151 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
148 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 152 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
149 EXPECT_EQ(10, integer); | 153 EXPECT_EQ(10, integer); |
150 | 154 |
151 pref_store.SetValue(kLongIntPref, | 155 pref_store->SetValue(kLongIntPref, |
152 Value::CreateStringValue( | 156 Value::CreateStringValue( |
153 base::Int64ToString(214748364842LL))); | 157 base::Int64ToString(214748364842LL))); |
154 EXPECT_EQ(PrefStore::READ_OK, pref_store.GetValue(kLongIntPref, &actual)); | 158 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); |
155 EXPECT_TRUE(actual->GetAsString(&string_value)); | 159 EXPECT_TRUE(actual->GetAsString(&string_value)); |
156 int64 value; | 160 int64 value; |
157 base::StringToInt64(string_value, &value); | 161 base::StringToInt64(string_value, &value); |
158 EXPECT_EQ(214748364842LL, value); | 162 EXPECT_EQ(214748364842LL, value); |
159 | 163 |
160 // Serialize and compare to expected output. | 164 // Serialize and compare to expected output. |
161 FilePath output_file = input_file; | 165 FilePath output_file = input_file; |
162 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); | 166 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
163 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 167 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
164 ASSERT_TRUE(pref_store.WritePrefs()); | 168 ASSERT_TRUE(pref_store->WritePrefs()); |
165 MessageLoop::current()->RunAllPending(); | 169 MessageLoop::current()->RunAllPending(); |
166 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 170 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
167 ASSERT_TRUE(file_util::Delete(output_file, false)); | 171 ASSERT_TRUE(file_util::Delete(output_file, false)); |
168 } | 172 } |
OLD | NEW |