[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 5 | #include "base/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 6 | #include "base/memory/ref_counted.h" |
| 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 8 | #include "base/message_loop.h" |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 9 | #include "base/message_loop_proxy.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 11 | #include "base/scoped_temp_dir.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 12 | #include "base/string_number_conversions.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 13 | #include "base/string_util.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 14 | #include "base/threading/thread.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 15 | #include "base/utf_string_conversions.h" |
| 16 | #include "base/values.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 17 | #include "chrome/common/chrome_paths.h" |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 18 | #include "chrome/common/json_pref_store.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 19 | #include "chrome/common/pref_names.h" |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 20 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | class MockPrefStoreObserver : public PrefStore::Observer { |
| 26 | public: |
| 27 | MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 28 | MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 29 | }; |
| 30 | |
| 31 | class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 32 | public: |
| 33 | MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 34 | }; |
| 35 | |
| 36 | } // namespace |
| 37 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 38 | class JsonPrefStoreTest : public testing::Test { |
| 39 | protected: |
| 40 | virtual void SetUp() { |
[email protected] | edd685f | 2011-08-15 20:33:46 | [diff] [blame] | 41 | message_loop_proxy_ = base::MessageLoopProxy::current(); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 42 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 43 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 44 | |
| 45 | ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 46 | data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 47 | ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 48 | } |
| 49 | |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 50 | // The path to temporary directory used to contain the test operations. |
| 51 | ScopedTempDir temp_dir_; |
| 52 | // The path to the directory where the test data is stored. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 53 | FilePath data_dir_; |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 54 | // A message loop that we can use as the file thread message loop. |
| 55 | MessageLoop message_loop_; |
| 56 | scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | // Test fallback behavior for a nonexistent file. |
| 60 | TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 61 | FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 62 | ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 63 | scoped_refptr<JsonPrefStore> pref_store = |
| 64 | new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 65 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 66 | pref_store->ReadPrefs()); |
| 67 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // Test fallback behavior for an invalid file. |
| 71 | TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 72 | FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 73 | FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 74 | ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 75 | scoped_refptr<JsonPrefStore> pref_store = |
| 76 | new JsonPrefStore(invalid_file, message_loop_proxy_.get()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 77 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 78 | pref_store->ReadPrefs()); |
| 79 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 80 | |
| 81 | // The file should have been moved aside. |
| 82 | EXPECT_FALSE(file_util::PathExists(invalid_file)); |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 83 | FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 84 | EXPECT_TRUE(file_util::PathExists(moved_aside)); |
| 85 | EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
| 86 | moved_aside)); |
| 87 | } |
| 88 | |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 89 | // This function is used to avoid code duplication while testing synchronous and |
| 90 | // asynchronous version of the JsonPrefStore loading. |
| 91 | void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store, |
| 92 | const FilePath& output_file, |
| 93 | const FilePath& golden_output_file) { |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 94 | const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| 95 | const char kMaxTabs[] = "tabs.max_tabs"; |
| 96 | const char kLongIntPref[] = "long_int.pref"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 97 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 98 | std::string cnn("http://www.cnn.com"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 99 | |
[email protected] | 68bf41a | 2011-03-25 16:38:31 | [diff] [blame] | 100 | const Value* actual; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 101 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 102 | pref_store->GetValue(prefs::kHomePage, &actual)); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 103 | std::string string_value; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 104 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 105 | EXPECT_EQ(cnn, string_value); |
| 106 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 107 | const char kSomeDirectory[] = "some_directory"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 108 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 109 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 110 | FilePath::StringType path; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 111 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 112 | EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 113 | FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 114 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 115 | pref_store->SetValue(kSomeDirectory, |
| 116 | Value::CreateStringValue(some_path.value())); |
| 117 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 118 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 119 | EXPECT_EQ(some_path.value(), path); |
| 120 | |
| 121 | // Test reading some other data types from sub-dictionaries. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 122 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 123 | pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 124 | bool boolean = false; |
| 125 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 126 | EXPECT_TRUE(boolean); |
| 127 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 128 | pref_store->SetValue(kNewWindowsInTabs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 129 | Value::CreateBooleanValue(false)); |
| 130 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 131 | pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 132 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 133 | EXPECT_FALSE(boolean); |
| 134 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 135 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 136 | int integer = 0; |
| 137 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 138 | EXPECT_EQ(20, integer); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 139 | pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); |
| 140 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 141 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 142 | EXPECT_EQ(10, integer); |
| 143 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 144 | pref_store->SetValue(kLongIntPref, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 145 | Value::CreateStringValue( |
| 146 | base::Int64ToString(214748364842LL))); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 147 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 148 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 149 | int64 value; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 150 | base::StringToInt64(string_value, &value); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 151 | EXPECT_EQ(214748364842LL, value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 152 | |
| 153 | // Serialize and compare to expected output. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 154 | ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
[email protected] | fbe17c8a | 2011-12-27 16:41:48 | [diff] [blame] | 155 | pref_store->CommitPendingWrite(); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 156 | MessageLoop::current()->RunAllPending(); |
| 157 | EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 158 | ASSERT_TRUE(file_util::Delete(output_file, false)); |
| 159 | } |
[email protected] | 845b43a8 | 2011-05-11 10:14:43 | [diff] [blame] | 160 | |
| 161 | TEST_F(JsonPrefStoreTest, Basic) { |
| 162 | ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 163 | temp_dir_.path().AppendASCII("write.json"))); |
| 164 | |
| 165 | // Test that the persistent value can be loaded. |
| 166 | FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 167 | ASSERT_TRUE(file_util::PathExists(input_file)); |
| 168 | scoped_refptr<JsonPrefStore> pref_store = |
| 169 | new JsonPrefStore(input_file, message_loop_proxy_.get()); |
| 170 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 171 | ASSERT_FALSE(pref_store->ReadOnly()); |
| 172 | |
| 173 | // The JSON file looks like this: |
| 174 | // { |
| 175 | // "homepage": "http://www.cnn.com", |
| 176 | // "some_directory": "/usr/local/", |
| 177 | // "tabs": { |
| 178 | // "new_windows_in_tabs": true, |
| 179 | // "max_tabs": 20 |
| 180 | // } |
| 181 | // } |
| 182 | |
| 183 | RunBasicJsonPrefStoreTest(pref_store, |
| 184 | input_file, |
| 185 | data_dir_.AppendASCII("write.golden.json")); |
| 186 | } |
| 187 | |
| 188 | TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 189 | ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 190 | temp_dir_.path().AppendASCII("write.json"))); |
| 191 | |
| 192 | // Test that the persistent value can be loaded. |
| 193 | FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 194 | ASSERT_TRUE(file_util::PathExists(input_file)); |
| 195 | scoped_refptr<JsonPrefStore> pref_store = |
| 196 | new JsonPrefStore(input_file, message_loop_proxy_.get()); |
| 197 | |
| 198 | MockPrefStoreObserver mock_observer; |
| 199 | pref_store->AddObserver(&mock_observer); |
| 200 | |
| 201 | MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 202 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 203 | |
| 204 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 205 | EXPECT_CALL(*mock_error_delegate, |
| 206 | OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 207 | message_loop_.RunAllPending(); |
| 208 | pref_store->RemoveObserver(&mock_observer); |
| 209 | |
| 210 | ASSERT_FALSE(pref_store->ReadOnly()); |
| 211 | |
| 212 | // The JSON file looks like this: |
| 213 | // { |
| 214 | // "homepage": "http://www.cnn.com", |
| 215 | // "some_directory": "/usr/local/", |
| 216 | // "tabs": { |
| 217 | // "new_windows_in_tabs": true, |
| 218 | // "max_tabs": 20 |
| 219 | // } |
| 220 | // } |
| 221 | |
| 222 | RunBasicJsonPrefStoreTest(pref_store, |
| 223 | input_file, |
| 224 | data_dir_.AppendASCII("write.golden.json")); |
| 225 | } |
| 226 | |
| 227 | // Tests asynchronous reading of the file when there is no file. |
| 228 | TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 229 | FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 230 | ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 231 | scoped_refptr<JsonPrefStore> pref_store = |
| 232 | new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
| 233 | MockPrefStoreObserver mock_observer; |
| 234 | pref_store->AddObserver(&mock_observer); |
| 235 | |
| 236 | MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 237 | pref_store->ReadPrefsAsync(mock_error_delegate); |
| 238 | |
| 239 | EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 240 | EXPECT_CALL(*mock_error_delegate, |
| 241 | OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 242 | message_loop_.RunAllPending(); |
| 243 | pref_store->RemoveObserver(&mock_observer); |
| 244 | |
| 245 | EXPECT_FALSE(pref_store->ReadOnly()); |
| 246 | } |
[email protected] | ea3e497 | 2012-04-12 03:41:37 | [diff] [blame^] | 247 | |
| 248 | TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
| 249 | FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 250 | |
| 251 | ASSERT_TRUE(file_util::CopyFile( |
| 252 | data_dir_.AppendASCII("read.need_empty_value.json"), |
| 253 | pref_file)); |
| 254 | |
| 255 | // Test that the persistent value can be loaded. |
| 256 | ASSERT_TRUE(file_util::PathExists(pref_file)); |
| 257 | scoped_refptr<JsonPrefStore> pref_store = |
| 258 | new JsonPrefStore(pref_file, message_loop_proxy_.get()); |
| 259 | ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 260 | ASSERT_FALSE(pref_store->ReadOnly()); |
| 261 | |
| 262 | // The JSON file looks like this: |
| 263 | // { |
| 264 | // "list": [ 1 ], |
| 265 | // "list_needs_empty_value": [ 2 ], |
| 266 | // "dict": { |
| 267 | // "dummy": true, |
| 268 | // }, |
| 269 | // "dict_needs_empty_value": { |
| 270 | // "dummy": true, |
| 271 | // }, |
| 272 | // } |
| 273 | |
| 274 | // Set flag to preserve empty values for the following keys. |
| 275 | pref_store->MarkNeedsEmptyValue("list_needs_empty_value"); |
| 276 | pref_store->MarkNeedsEmptyValue("dict_needs_empty_value"); |
| 277 | |
| 278 | // Set all keys to empty values. |
| 279 | pref_store->SetValue("list", new base::ListValue); |
| 280 | pref_store->SetValue("list_needs_empty_value", new base::ListValue); |
| 281 | pref_store->SetValue("dict", new base::DictionaryValue); |
| 282 | pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); |
| 283 | |
| 284 | // Write to file. |
| 285 | pref_store->CommitPendingWrite(); |
| 286 | MessageLoop::current()->RunAllPending(); |
| 287 | |
| 288 | // Compare to expected output. |
| 289 | FilePath golden_output_file = |
| 290 | data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 291 | ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 292 | EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 293 | } |