| 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 26 matching lines...) Expand all Loading... |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 class JsonPrefStoreTest : public testing::Test { | 40 class JsonPrefStoreTest : public testing::Test { |
| 41 protected: | 41 protected: |
| 42 virtual void SetUp() OVERRIDE { | 42 virtual void SetUp() OVERRIDE { |
| 43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 44 | 44 |
| 45 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 45 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
| 46 data_dir_ = data_dir_.AppendASCII("prefs"); | 46 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 47 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 47 ASSERT_TRUE(PathExists(data_dir_)); |
| 48 } | 48 } |
| 49 | 49 |
| 50 // The path to temporary directory used to contain the test operations. | 50 // The path to temporary directory used to contain the test operations. |
| 51 base::ScopedTempDir temp_dir_; | 51 base::ScopedTempDir temp_dir_; |
| 52 // The path to the directory where the test data is stored. | 52 // The path to the directory where the test data is stored. |
| 53 base::FilePath data_dir_; | 53 base::FilePath data_dir_; |
| 54 // A message loop that we can use as the file thread message loop. | 54 // A message loop that we can use as the file thread message loop. |
| 55 MessageLoop message_loop_; | 55 MessageLoop message_loop_; |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Test fallback behavior for a nonexistent file. | 58 // Test fallback behavior for a nonexistent file. |
| 59 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 59 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 60 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 60 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 61 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 61 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 62 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 62 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 63 bogus_input_file, message_loop_.message_loop_proxy().get()); | 63 bogus_input_file, message_loop_.message_loop_proxy().get()); |
| 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 64 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 65 pref_store->ReadPrefs()); | 65 pref_store->ReadPrefs()); |
| 66 EXPECT_FALSE(pref_store->ReadOnly()); | 66 EXPECT_FALSE(pref_store->ReadOnly()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 // Test fallback behavior for an invalid file. | 69 // Test fallback behavior for an invalid file. |
| 70 TEST_F(JsonPrefStoreTest, InvalidFile) { | 70 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 71 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 71 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 73 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 73 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
| 74 scoped_refptr<JsonPrefStore> pref_store = | 74 scoped_refptr<JsonPrefStore> pref_store = |
| 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); | 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); |
| 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 77 pref_store->ReadPrefs()); | 77 pref_store->ReadPrefs()); |
| 78 EXPECT_FALSE(pref_store->ReadOnly()); | 78 EXPECT_FALSE(pref_store->ReadOnly()); |
| 79 | 79 |
| 80 // The file should have been moved aside. | 80 // The file should have been moved aside. |
| 81 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 81 EXPECT_FALSE(PathExists(invalid_file)); |
| 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 83 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 83 EXPECT_TRUE(PathExists(moved_aside)); |
| 84 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 84 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
| 85 moved_aside)); | 85 moved_aside)); |
| 86 } | 86 } |
| 87 | 87 |
| 88 // This function is used to avoid code duplication while testing synchronous and | 88 // This function is used to avoid code duplication while testing synchronous and |
| 89 // asynchronous version of the JsonPrefStore loading. | 89 // asynchronous version of the JsonPrefStore loading. |
| 90 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, | 90 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, |
| 91 const base::FilePath& output_file, | 91 const base::FilePath& output_file, |
| 92 const base::FilePath& golden_output_file) { | 92 const base::FilePath& golden_output_file) { |
| 93 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; | 93 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 137 |
| 138 pref_store->SetValue(kLongIntPref, | 138 pref_store->SetValue(kLongIntPref, |
| 139 new StringValue(base::Int64ToString(214748364842LL))); | 139 new StringValue(base::Int64ToString(214748364842LL))); |
| 140 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 140 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
| 141 EXPECT_TRUE(actual->GetAsString(&string_value)); | 141 EXPECT_TRUE(actual->GetAsString(&string_value)); |
| 142 int64 value; | 142 int64 value; |
| 143 base::StringToInt64(string_value, &value); | 143 base::StringToInt64(string_value, &value); |
| 144 EXPECT_EQ(214748364842LL, value); | 144 EXPECT_EQ(214748364842LL, value); |
| 145 | 145 |
| 146 // Serialize and compare to expected output. | 146 // Serialize and compare to expected output. |
| 147 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 147 ASSERT_TRUE(PathExists(golden_output_file)); |
| 148 pref_store->CommitPendingWrite(); | 148 pref_store->CommitPendingWrite(); |
| 149 RunLoop().RunUntilIdle(); | 149 RunLoop().RunUntilIdle(); |
| 150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 151 ASSERT_TRUE(base::Delete(output_file, false)); | 151 ASSERT_TRUE(base::Delete(output_file, false)); |
| 152 } | 152 } |
| 153 | 153 |
| 154 TEST_F(JsonPrefStoreTest, Basic) { | 154 TEST_F(JsonPrefStoreTest, Basic) { |
| 155 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 155 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 156 temp_dir_.path().AppendASCII("write.json"))); | 156 temp_dir_.path().AppendASCII("write.json"))); |
| 157 | 157 |
| 158 // Test that the persistent value can be loaded. | 158 // Test that the persistent value can be loaded. |
| 159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 160 ASSERT_TRUE(file_util::PathExists(input_file)); | 160 ASSERT_TRUE(PathExists(input_file)); |
| 161 scoped_refptr<JsonPrefStore> pref_store = | 161 scoped_refptr<JsonPrefStore> pref_store = |
| 162 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); | 162 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
| 163 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 163 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 164 ASSERT_FALSE(pref_store->ReadOnly()); | 164 ASSERT_FALSE(pref_store->ReadOnly()); |
| 165 | 165 |
| 166 // The JSON file looks like this: | 166 // The JSON file looks like this: |
| 167 // { | 167 // { |
| 168 // "homepage": "http://www.cnn.com", | 168 // "homepage": "http://www.cnn.com", |
| 169 // "some_directory": "/usr/local/", | 169 // "some_directory": "/usr/local/", |
| 170 // "tabs": { | 170 // "tabs": { |
| 171 // "new_windows_in_tabs": true, | 171 // "new_windows_in_tabs": true, |
| 172 // "max_tabs": 20 | 172 // "max_tabs": 20 |
| 173 // } | 173 // } |
| 174 // } | 174 // } |
| 175 | 175 |
| 176 RunBasicJsonPrefStoreTest( | 176 RunBasicJsonPrefStoreTest( |
| 177 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 177 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST_F(JsonPrefStoreTest, BasicAsync) { | 180 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 181 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 181 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 182 temp_dir_.path().AppendASCII("write.json"))); | 182 temp_dir_.path().AppendASCII("write.json"))); |
| 183 | 183 |
| 184 // Test that the persistent value can be loaded. | 184 // Test that the persistent value can be loaded. |
| 185 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 185 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 186 ASSERT_TRUE(file_util::PathExists(input_file)); | 186 ASSERT_TRUE(PathExists(input_file)); |
| 187 scoped_refptr<JsonPrefStore> pref_store = | 187 scoped_refptr<JsonPrefStore> pref_store = |
| 188 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); | 188 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get()); |
| 189 | 189 |
| 190 { | 190 { |
| 191 MockPrefStoreObserver mock_observer; | 191 MockPrefStoreObserver mock_observer; |
| 192 pref_store->AddObserver(&mock_observer); | 192 pref_store->AddObserver(&mock_observer); |
| 193 | 193 |
| 194 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 194 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 195 pref_store->ReadPrefsAsync(mock_error_delegate); | 195 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 196 | 196 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 213 // } | 213 // } |
| 214 // } | 214 // } |
| 215 | 215 |
| 216 RunBasicJsonPrefStoreTest( | 216 RunBasicJsonPrefStoreTest( |
| 217 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 217 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 218 } | 218 } |
| 219 | 219 |
| 220 // Tests asynchronous reading of the file when there is no file. | 220 // Tests asynchronous reading of the file when there is no file. |
| 221 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 221 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 222 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 222 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 223 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 223 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 224 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 224 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 225 bogus_input_file, message_loop_.message_loop_proxy().get()); | 225 bogus_input_file, message_loop_.message_loop_proxy().get()); |
| 226 MockPrefStoreObserver mock_observer; | 226 MockPrefStoreObserver mock_observer; |
| 227 pref_store->AddObserver(&mock_observer); | 227 pref_store->AddObserver(&mock_observer); |
| 228 | 228 |
| 229 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 229 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 230 pref_store->ReadPrefsAsync(mock_error_delegate); | 230 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 231 | 231 |
| 232 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 232 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 233 EXPECT_CALL(*mock_error_delegate, | 233 EXPECT_CALL(*mock_error_delegate, |
| 234 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 234 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 235 RunLoop().RunUntilIdle(); | 235 RunLoop().RunUntilIdle(); |
| 236 pref_store->RemoveObserver(&mock_observer); | 236 pref_store->RemoveObserver(&mock_observer); |
| 237 | 237 |
| 238 EXPECT_FALSE(pref_store->ReadOnly()); | 238 EXPECT_FALSE(pref_store->ReadOnly()); |
| 239 } | 239 } |
| 240 | 240 |
| 241 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 241 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
| 242 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 242 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 243 | 243 |
| 244 ASSERT_TRUE(base::CopyFile( | 244 ASSERT_TRUE(base::CopyFile( |
| 245 data_dir_.AppendASCII("read.need_empty_value.json"), | 245 data_dir_.AppendASCII("read.need_empty_value.json"), |
| 246 pref_file)); | 246 pref_file)); |
| 247 | 247 |
| 248 // Test that the persistent value can be loaded. | 248 // Test that the persistent value can be loaded. |
| 249 ASSERT_TRUE(file_util::PathExists(pref_file)); | 249 ASSERT_TRUE(PathExists(pref_file)); |
| 250 scoped_refptr<JsonPrefStore> pref_store = | 250 scoped_refptr<JsonPrefStore> pref_store = |
| 251 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); | 251 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get()); |
| 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 252 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 253 ASSERT_FALSE(pref_store->ReadOnly()); | 253 ASSERT_FALSE(pref_store->ReadOnly()); |
| 254 | 254 |
| 255 // The JSON file looks like this: | 255 // The JSON file looks like this: |
| 256 // { | 256 // { |
| 257 // "list": [ 1 ], | 257 // "list": [ 1 ], |
| 258 // "list_needs_empty_value": [ 2 ], | 258 // "list_needs_empty_value": [ 2 ], |
| 259 // "dict": { | 259 // "dict": { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 274 pref_store->SetValue("dict", new base::DictionaryValue); | 274 pref_store->SetValue("dict", new base::DictionaryValue); |
| 275 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); | 275 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); |
| 276 | 276 |
| 277 // Write to file. | 277 // Write to file. |
| 278 pref_store->CommitPendingWrite(); | 278 pref_store->CommitPendingWrite(); |
| 279 RunLoop().RunUntilIdle(); | 279 RunLoop().RunUntilIdle(); |
| 280 | 280 |
| 281 // Compare to expected output. | 281 // Compare to expected output. |
| 282 base::FilePath golden_output_file = | 282 base::FilePath golden_output_file = |
| 283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 284 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 284 ASSERT_TRUE(PathExists(golden_output_file)); |
| 285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 286 } | 286 } |
| 287 | 287 |
| 288 } // namespace base | 288 } // namespace base |
| OLD | NEW |