| OLD | NEW |
| 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 data_dir_ = data_dir_.AppendASCII("test"); | 48 data_dir_ = data_dir_.AppendASCII("test"); |
| 49 data_dir_ = data_dir_.AppendASCII("data"); | 49 data_dir_ = data_dir_.AppendASCII("data"); |
| 50 data_dir_ = data_dir_.AppendASCII("pref_service"); | 50 data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 51 LOG(WARNING) << data_dir_.value().c_str(); | 51 LOG(WARNING) << data_dir_.value().c_str(); |
| 52 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 52 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // The path to temporary directory used to contain the test operations. | 55 // The path to temporary directory used to contain the test operations. |
| 56 base::ScopedTempDir temp_dir_; | 56 base::ScopedTempDir temp_dir_; |
| 57 // The path to the directory where the test data is stored. | 57 // The path to the directory where the test data is stored. |
| 58 FilePath data_dir_; | 58 base::FilePath data_dir_; |
| 59 // A message loop that we can use as the file thread message loop. | 59 // A message loop that we can use as the file thread message loop. |
| 60 MessageLoop message_loop_; | 60 MessageLoop message_loop_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // Test fallback behavior for a nonexistent file. | 63 // Test fallback behavior for a nonexistent file. |
| 64 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 64 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 65 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 65 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 66 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 66 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 67 scoped_refptr<JsonPrefStore> pref_store = | 67 scoped_refptr<JsonPrefStore> pref_store = |
| 68 new JsonPrefStore( | 68 new JsonPrefStore( |
| 69 bogus_input_file, message_loop_.message_loop_proxy()); | 69 bogus_input_file, message_loop_.message_loop_proxy()); |
| 70 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 70 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 71 pref_store->ReadPrefs()); | 71 pref_store->ReadPrefs()); |
| 72 EXPECT_FALSE(pref_store->ReadOnly()); | 72 EXPECT_FALSE(pref_store->ReadOnly()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 // Test fallback behavior for an invalid file. | 75 // Test fallback behavior for an invalid file. |
| 76 TEST_F(JsonPrefStoreTest, InvalidFile) { | 76 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 77 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 77 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 78 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 78 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 79 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 79 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
| 80 scoped_refptr<JsonPrefStore> pref_store = | 80 scoped_refptr<JsonPrefStore> pref_store = |
| 81 new JsonPrefStore( | 81 new JsonPrefStore( |
| 82 invalid_file, message_loop_.message_loop_proxy()); | 82 invalid_file, message_loop_.message_loop_proxy()); |
| 83 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 83 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 84 pref_store->ReadPrefs()); | 84 pref_store->ReadPrefs()); |
| 85 EXPECT_FALSE(pref_store->ReadOnly()); | 85 EXPECT_FALSE(pref_store->ReadOnly()); |
| 86 | 86 |
| 87 // The file should have been moved aside. | 87 // The file should have been moved aside. |
| 88 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 88 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
| 89 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 89 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 90 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 90 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
| 91 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 91 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
| 92 moved_aside)); | 92 moved_aside)); |
| 93 } | 93 } |
| 94 | 94 |
| 95 // This function is used to avoid code duplication while testing synchronous and | 95 // This function is used to avoid code duplication while testing synchronous and |
| 96 // asynchronous version of the JsonPrefStore loading. | 96 // asynchronous version of the JsonPrefStore loading. |
| 97 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, | 97 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, |
| 98 const FilePath& output_file, | 98 const base::FilePath& output_file, |
| 99 const FilePath& golden_output_file) { | 99 const base::FilePath& golden_output_file) { |
| 100 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; | 100 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| 101 const char kMaxTabs[] = "tabs.max_tabs"; | 101 const char kMaxTabs[] = "tabs.max_tabs"; |
| 102 const char kLongIntPref[] = "long_int.pref"; | 102 const char kLongIntPref[] = "long_int.pref"; |
| 103 | 103 |
| 104 std::string cnn("http://www.cnn.com"); | 104 std::string cnn("http://www.cnn.com"); |
| 105 | 105 |
| 106 const Value* actual; | 106 const Value* actual; |
| 107 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); | 107 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); |
| 108 std::string string_value; | 108 std::string string_value; |
| 109 EXPECT_TRUE(actual->GetAsString(&string_value)); | 109 EXPECT_TRUE(actual->GetAsString(&string_value)); |
| 110 EXPECT_EQ(cnn, string_value); | 110 EXPECT_EQ(cnn, string_value); |
| 111 | 111 |
| 112 const char kSomeDirectory[] = "some_directory"; | 112 const char kSomeDirectory[] = "some_directory"; |
| 113 | 113 |
| 114 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 114 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 115 FilePath::StringType path; | 115 base::FilePath::StringType path; |
| 116 EXPECT_TRUE(actual->GetAsString(&path)); | 116 EXPECT_TRUE(actual->GetAsString(&path)); |
| 117 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 117 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 118 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 118 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
| 119 | 119 |
| 120 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value())); | 120 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value())); |
| 121 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 121 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
| 122 EXPECT_TRUE(actual->GetAsString(&path)); | 122 EXPECT_TRUE(actual->GetAsString(&path)); |
| 123 EXPECT_EQ(some_path.value(), path); | 123 EXPECT_EQ(some_path.value(), path); |
| 124 | 124 |
| 125 // Test reading some other data types from sub-dictionaries. | 125 // Test reading some other data types from sub-dictionaries. |
| 126 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 126 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
| 127 bool boolean = false; | 127 bool boolean = false; |
| 128 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 128 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 156 RunLoop().RunUntilIdle(); | 156 RunLoop().RunUntilIdle(); |
| 157 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 157 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 158 ASSERT_TRUE(file_util::Delete(output_file, false)); | 158 ASSERT_TRUE(file_util::Delete(output_file, false)); |
| 159 } | 159 } |
| 160 | 160 |
| 161 TEST_F(JsonPrefStoreTest, Basic) { | 161 TEST_F(JsonPrefStoreTest, Basic) { |
| 162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 163 temp_dir_.path().AppendASCII("write.json"))); | 163 temp_dir_.path().AppendASCII("write.json"))); |
| 164 | 164 |
| 165 // Test that the persistent value can be loaded. | 165 // Test that the persistent value can be loaded. |
| 166 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 166 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 167 ASSERT_TRUE(file_util::PathExists(input_file)); | 167 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 168 scoped_refptr<JsonPrefStore> pref_store = | 168 scoped_refptr<JsonPrefStore> pref_store = |
| 169 new JsonPrefStore( | 169 new JsonPrefStore( |
| 170 input_file, message_loop_.message_loop_proxy()); | 170 input_file, message_loop_.message_loop_proxy()); |
| 171 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 171 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 172 ASSERT_FALSE(pref_store->ReadOnly()); | 172 ASSERT_FALSE(pref_store->ReadOnly()); |
| 173 | 173 |
| 174 // The JSON file looks like this: | 174 // The JSON file looks like this: |
| 175 // { | 175 // { |
| 176 // "homepage": "http://www.cnn.com", | 176 // "homepage": "http://www.cnn.com", |
| 177 // "some_directory": "/usr/local/", | 177 // "some_directory": "/usr/local/", |
| 178 // "tabs": { | 178 // "tabs": { |
| 179 // "new_windows_in_tabs": true, | 179 // "new_windows_in_tabs": true, |
| 180 // "max_tabs": 20 | 180 // "max_tabs": 20 |
| 181 // } | 181 // } |
| 182 // } | 182 // } |
| 183 | 183 |
| 184 RunBasicJsonPrefStoreTest(pref_store, | 184 RunBasicJsonPrefStoreTest(pref_store, |
| 185 input_file, | 185 input_file, |
| 186 data_dir_.AppendASCII("write.golden.json")); | 186 data_dir_.AppendASCII("write.golden.json")); |
| 187 } | 187 } |
| 188 | 188 |
| 189 TEST_F(JsonPrefStoreTest, BasicAsync) { | 189 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 190 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 190 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 191 temp_dir_.path().AppendASCII("write.json"))); | 191 temp_dir_.path().AppendASCII("write.json"))); |
| 192 | 192 |
| 193 // Test that the persistent value can be loaded. | 193 // Test that the persistent value can be loaded. |
| 194 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 194 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 195 ASSERT_TRUE(file_util::PathExists(input_file)); | 195 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 196 scoped_refptr<JsonPrefStore> pref_store = | 196 scoped_refptr<JsonPrefStore> pref_store = |
| 197 new JsonPrefStore( | 197 new JsonPrefStore( |
| 198 input_file, message_loop_.message_loop_proxy()); | 198 input_file, message_loop_.message_loop_proxy()); |
| 199 | 199 |
| 200 { | 200 { |
| 201 MockPrefStoreObserver mock_observer; | 201 MockPrefStoreObserver mock_observer; |
| 202 pref_store->AddObserver(&mock_observer); | 202 pref_store->AddObserver(&mock_observer); |
| 203 | 203 |
| 204 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 204 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 223 // } | 223 // } |
| 224 // } | 224 // } |
| 225 | 225 |
| 226 RunBasicJsonPrefStoreTest(pref_store, | 226 RunBasicJsonPrefStoreTest(pref_store, |
| 227 input_file, | 227 input_file, |
| 228 data_dir_.AppendASCII("write.golden.json")); | 228 data_dir_.AppendASCII("write.golden.json")); |
| 229 } | 229 } |
| 230 | 230 |
| 231 // Tests asynchronous reading of the file when there is no file. | 231 // Tests asynchronous reading of the file when there is no file. |
| 232 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 232 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 233 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 233 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 234 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 234 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 235 scoped_refptr<JsonPrefStore> pref_store = | 235 scoped_refptr<JsonPrefStore> pref_store = |
| 236 new JsonPrefStore( | 236 new JsonPrefStore( |
| 237 bogus_input_file, message_loop_.message_loop_proxy()); | 237 bogus_input_file, message_loop_.message_loop_proxy()); |
| 238 MockPrefStoreObserver mock_observer; | 238 MockPrefStoreObserver mock_observer; |
| 239 pref_store->AddObserver(&mock_observer); | 239 pref_store->AddObserver(&mock_observer); |
| 240 | 240 |
| 241 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 241 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 242 pref_store->ReadPrefsAsync(mock_error_delegate); | 242 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 243 | 243 |
| 244 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 244 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 245 EXPECT_CALL(*mock_error_delegate, | 245 EXPECT_CALL(*mock_error_delegate, |
| 246 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 246 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 247 RunLoop().RunUntilIdle(); | 247 RunLoop().RunUntilIdle(); |
| 248 pref_store->RemoveObserver(&mock_observer); | 248 pref_store->RemoveObserver(&mock_observer); |
| 249 | 249 |
| 250 EXPECT_FALSE(pref_store->ReadOnly()); | 250 EXPECT_FALSE(pref_store->ReadOnly()); |
| 251 } | 251 } |
| 252 | 252 |
| 253 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 253 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
| 254 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 254 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 255 | 255 |
| 256 ASSERT_TRUE(file_util::CopyFile( | 256 ASSERT_TRUE(file_util::CopyFile( |
| 257 data_dir_.AppendASCII("read.need_empty_value.json"), | 257 data_dir_.AppendASCII("read.need_empty_value.json"), |
| 258 pref_file)); | 258 pref_file)); |
| 259 | 259 |
| 260 // Test that the persistent value can be loaded. | 260 // Test that the persistent value can be loaded. |
| 261 ASSERT_TRUE(file_util::PathExists(pref_file)); | 261 ASSERT_TRUE(file_util::PathExists(pref_file)); |
| 262 scoped_refptr<JsonPrefStore> pref_store = | 262 scoped_refptr<JsonPrefStore> pref_store = |
| 263 new JsonPrefStore( | 263 new JsonPrefStore( |
| 264 pref_file, message_loop_.message_loop_proxy()); | 264 pref_file, message_loop_.message_loop_proxy()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 285 pref_store->SetValue("list", new base::ListValue); | 285 pref_store->SetValue("list", new base::ListValue); |
| 286 pref_store->SetValue("list_needs_empty_value", new base::ListValue); | 286 pref_store->SetValue("list_needs_empty_value", new base::ListValue); |
| 287 pref_store->SetValue("dict", new base::DictionaryValue); | 287 pref_store->SetValue("dict", new base::DictionaryValue); |
| 288 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); | 288 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); |
| 289 | 289 |
| 290 // Write to file. | 290 // Write to file. |
| 291 pref_store->CommitPendingWrite(); | 291 pref_store->CommitPendingWrite(); |
| 292 RunLoop().RunUntilIdle(); | 292 RunLoop().RunUntilIdle(); |
| 293 | 293 |
| 294 // Compare to expected output. | 294 // Compare to expected output. |
| 295 FilePath golden_output_file = | 295 base::FilePath golden_output_file = |
| 296 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 296 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 297 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 297 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 298 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 298 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 299 } | 299 } |
| 300 | 300 |
| 301 } // namespace base | 301 } // namespace base |
| OLD | NEW |