| 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/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" | |
| 9 #include "base/message_loop_proxy.h" | |
| 10 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 11 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 12 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 15 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 16 #include "base/values.h" | 15 #include "base/values.h" |
| 17 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 18 #include "chrome/common/json_pref_store.h" | 17 #include "chrome/common/json_pref_store.h" |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 namespace { | 22 namespace { |
| 24 | 23 |
| 25 class MockPrefStoreObserver : public PrefStore::Observer { | 24 class MockPrefStoreObserver : public PrefStore::Observer { |
| 26 public: | 25 public: |
| 27 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); | 26 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
| 28 MOCK_METHOD1(OnInitializationCompleted, void (bool)); | 27 MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
| 29 }; | 28 }; |
| 30 | 29 |
| 31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 30 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 32 public: | 31 public: |
| 33 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); | 32 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
| 34 }; | 33 }; |
| 35 | 34 |
| 36 } // namespace | 35 } // namespace |
| 37 | 36 |
| 38 class JsonPrefStoreTest : public testing::Test { | 37 class JsonPrefStoreTest : public testing::Test { |
| 39 protected: | 38 protected: |
| 40 virtual void SetUp() { | 39 virtual void SetUp() { |
| 41 message_loop_proxy_ = base::MessageLoopProxy::current(); | 40 blocking_pool_ = new base::SequencedWorkerPool(1, "TestBlocking"); |
| 42 | 41 |
| 43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 44 | 43 |
| 45 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 44 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 46 data_dir_ = data_dir_.AppendASCII("pref_service"); | 45 data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 47 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 46 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 48 } | 47 } |
| 49 | 48 |
| 50 // The path to temporary directory used to contain the test operations. | 49 // The path to temporary directory used to contain the test operations. |
| 51 ScopedTempDir temp_dir_; | 50 ScopedTempDir temp_dir_; |
| 52 // The path to the directory where the test data is stored. | 51 // The path to the directory where the test data is stored. |
| 53 FilePath data_dir_; | 52 FilePath data_dir_; |
| 54 // A message loop that we can use as the file thread message loop. | 53 // A blocking pool that we can use for file operations. |
| 55 MessageLoop message_loop_; | 54 scoped_refptr<base::SequencedWorkerPool> blocking_pool_; |
| 56 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 57 }; | 55 }; |
| 58 | 56 |
| 59 // Test fallback behavior for a nonexistent file. | 57 // Test fallback behavior for a nonexistent file. |
| 60 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 58 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 61 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 59 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 62 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 60 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 63 scoped_refptr<JsonPrefStore> pref_store = | 61 scoped_refptr<JsonPrefStore> pref_store = |
| 64 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); | 62 JsonPrefStore::Create(bogus_input_file, blocking_pool_.get()); |
| 65 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 63 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 66 pref_store->ReadPrefs()); | 64 pref_store->ReadPrefs()); |
| 67 EXPECT_FALSE(pref_store->ReadOnly()); | 65 EXPECT_FALSE(pref_store->ReadOnly()); |
| 68 } | 66 } |
| 69 | 67 |
| 70 // Test fallback behavior for an invalid file. | 68 // Test fallback behavior for an invalid file. |
| 71 TEST_F(JsonPrefStoreTest, InvalidFile) { | 69 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 72 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 70 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 73 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 71 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 74 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); | 72 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); |
| 75 scoped_refptr<JsonPrefStore> pref_store = | 73 scoped_refptr<JsonPrefStore> pref_store = |
| 76 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); | 74 JsonPrefStore::Create(invalid_file, blocking_pool_.get()); |
| 77 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 75 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 78 pref_store->ReadPrefs()); | 76 pref_store->ReadPrefs()); |
| 79 EXPECT_FALSE(pref_store->ReadOnly()); | 77 EXPECT_FALSE(pref_store->ReadOnly()); |
| 80 | 78 |
| 81 // The file should have been moved aside. | 79 // The file should have been moved aside. |
| 82 EXPECT_FALSE(file_util::PathExists(invalid_file)); | 80 EXPECT_FALSE(file_util::PathExists(invalid_file)); |
| 83 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 81 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 84 EXPECT_TRUE(file_util::PathExists(moved_aside)); | 82 EXPECT_TRUE(file_util::PathExists(moved_aside)); |
| 85 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, | 83 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, |
| 86 moved_aside)); | 84 moved_aside)); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 } | 157 } |
| 160 | 158 |
| 161 TEST_F(JsonPrefStoreTest, Basic) { | 159 TEST_F(JsonPrefStoreTest, Basic) { |
| 162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 160 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 163 temp_dir_.path().AppendASCII("write.json"))); | 161 temp_dir_.path().AppendASCII("write.json"))); |
| 164 | 162 |
| 165 // Test that the persistent value can be loaded. | 163 // Test that the persistent value can be loaded. |
| 166 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 164 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 167 ASSERT_TRUE(file_util::PathExists(input_file)); | 165 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 168 scoped_refptr<JsonPrefStore> pref_store = | 166 scoped_refptr<JsonPrefStore> pref_store = |
| 169 new JsonPrefStore(input_file, message_loop_proxy_.get()); | 167 JsonPrefStore::Create(input_file, blocking_pool_.get()); |
| 170 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 168 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 171 ASSERT_FALSE(pref_store->ReadOnly()); | 169 ASSERT_FALSE(pref_store->ReadOnly()); |
| 172 | 170 |
| 173 // The JSON file looks like this: | 171 // The JSON file looks like this: |
| 174 // { | 172 // { |
| 175 // "homepage": "http://www.cnn.com", | 173 // "homepage": "http://www.cnn.com", |
| 176 // "some_directory": "/usr/local/", | 174 // "some_directory": "/usr/local/", |
| 177 // "tabs": { | 175 // "tabs": { |
| 178 // "new_windows_in_tabs": true, | 176 // "new_windows_in_tabs": true, |
| 179 // "max_tabs": 20 | 177 // "max_tabs": 20 |
| 180 // } | 178 // } |
| 181 // } | 179 // } |
| 182 | 180 |
| 183 RunBasicJsonPrefStoreTest(pref_store, | 181 RunBasicJsonPrefStoreTest(pref_store, |
| 184 input_file, | 182 input_file, |
| 185 data_dir_.AppendASCII("write.golden.json")); | 183 data_dir_.AppendASCII("write.golden.json")); |
| 186 } | 184 } |
| 187 | 185 |
| 188 TEST_F(JsonPrefStoreTest, BasicAsync) { | 186 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 189 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 187 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 190 temp_dir_.path().AppendASCII("write.json"))); | 188 temp_dir_.path().AppendASCII("write.json"))); |
| 191 | 189 |
| 192 // Test that the persistent value can be loaded. | 190 // Test that the persistent value can be loaded. |
| 193 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 191 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 194 ASSERT_TRUE(file_util::PathExists(input_file)); | 192 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 195 scoped_refptr<JsonPrefStore> pref_store = | 193 scoped_refptr<JsonPrefStore> pref_store = |
| 196 new JsonPrefStore(input_file, message_loop_proxy_.get()); | 194 JsonPrefStore::Create(input_file, blocking_pool_.get()); |
| 197 | 195 |
| 198 MockPrefStoreObserver mock_observer; | 196 MockPrefStoreObserver mock_observer; |
| 199 pref_store->AddObserver(&mock_observer); | 197 pref_store->AddObserver(&mock_observer); |
| 200 | 198 |
| 201 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 199 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 202 pref_store->ReadPrefsAsync(mock_error_delegate); | 200 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 203 | 201 |
| 204 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 202 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 205 EXPECT_CALL(*mock_error_delegate, | 203 EXPECT_CALL(*mock_error_delegate, |
| 206 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); | 204 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); |
| 207 message_loop_.RunAllPending(); | 205 blocking_pool_->FlushForTesting(); |
| 208 pref_store->RemoveObserver(&mock_observer); | 206 pref_store->RemoveObserver(&mock_observer); |
| 209 | 207 |
| 210 ASSERT_FALSE(pref_store->ReadOnly()); | 208 ASSERT_FALSE(pref_store->ReadOnly()); |
| 211 | 209 |
| 212 // The JSON file looks like this: | 210 // The JSON file looks like this: |
| 213 // { | 211 // { |
| 214 // "homepage": "http://www.cnn.com", | 212 // "homepage": "http://www.cnn.com", |
| 215 // "some_directory": "/usr/local/", | 213 // "some_directory": "/usr/local/", |
| 216 // "tabs": { | 214 // "tabs": { |
| 217 // "new_windows_in_tabs": true, | 215 // "new_windows_in_tabs": true, |
| 218 // "max_tabs": 20 | 216 // "max_tabs": 20 |
| 219 // } | 217 // } |
| 220 // } | 218 // } |
| 221 | 219 |
| 222 RunBasicJsonPrefStoreTest(pref_store, | 220 RunBasicJsonPrefStoreTest(pref_store, |
| 223 input_file, | 221 input_file, |
| 224 data_dir_.AppendASCII("write.golden.json")); | 222 data_dir_.AppendASCII("write.golden.json")); |
| 225 } | 223 } |
| 226 | 224 |
| 227 // Tests asynchronous reading of the file when there is no file. | 225 // Tests asynchronous reading of the file when there is no file. |
| 228 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 226 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 229 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 227 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 230 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); | 228 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 231 scoped_refptr<JsonPrefStore> pref_store = | 229 scoped_refptr<JsonPrefStore> pref_store = |
| 232 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); | 230 JsonPrefStore::Create(bogus_input_file, blocking_pool_.get()); |
| 233 MockPrefStoreObserver mock_observer; | 231 MockPrefStoreObserver mock_observer; |
| 234 pref_store->AddObserver(&mock_observer); | 232 pref_store->AddObserver(&mock_observer); |
| 235 | 233 |
| 236 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 234 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 237 pref_store->ReadPrefsAsync(mock_error_delegate); | 235 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 238 | 236 |
| 239 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 237 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 240 EXPECT_CALL(*mock_error_delegate, | 238 EXPECT_CALL(*mock_error_delegate, |
| 241 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 239 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 242 message_loop_.RunAllPending(); | 240 blocking_pool_->Shutdown(); |
| 243 pref_store->RemoveObserver(&mock_observer); | 241 pref_store->RemoveObserver(&mock_observer); |
| 244 | 242 |
| 245 EXPECT_FALSE(pref_store->ReadOnly()); | 243 EXPECT_FALSE(pref_store->ReadOnly()); |
| 246 } | 244 } |
| 247 | 245 |
| 248 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { | 246 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { |
| 249 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 247 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 250 | 248 |
| 251 ASSERT_TRUE(file_util::CopyFile( | 249 ASSERT_TRUE(file_util::CopyFile( |
| 252 data_dir_.AppendASCII("read.need_empty_value.json"), | 250 data_dir_.AppendASCII("read.need_empty_value.json"), |
| 253 pref_file)); | 251 pref_file)); |
| 254 | 252 |
| 255 // Test that the persistent value can be loaded. | 253 // Test that the persistent value can be loaded. |
| 256 ASSERT_TRUE(file_util::PathExists(pref_file)); | 254 ASSERT_TRUE(file_util::PathExists(pref_file)); |
| 257 scoped_refptr<JsonPrefStore> pref_store = | 255 scoped_refptr<JsonPrefStore> pref_store = |
| 258 new JsonPrefStore(pref_file, message_loop_proxy_.get()); | 256 JsonPrefStore::Create(pref_file, blocking_pool_.get()); |
| 259 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 257 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 260 ASSERT_FALSE(pref_store->ReadOnly()); | 258 ASSERT_FALSE(pref_store->ReadOnly()); |
| 261 | 259 |
| 262 // The JSON file looks like this: | 260 // The JSON file looks like this: |
| 263 // { | 261 // { |
| 264 // "list": [ 1 ], | 262 // "list": [ 1 ], |
| 265 // "list_needs_empty_value": [ 2 ], | 263 // "list_needs_empty_value": [ 2 ], |
| 266 // "dict": { | 264 // "dict": { |
| 267 // "dummy": true, | 265 // "dummy": true, |
| 268 // }, | 266 // }, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 284 // Write to file. | 282 // Write to file. |
| 285 pref_store->CommitPendingWrite(); | 283 pref_store->CommitPendingWrite(); |
| 286 MessageLoop::current()->RunAllPending(); | 284 MessageLoop::current()->RunAllPending(); |
| 287 | 285 |
| 288 // Compare to expected output. | 286 // Compare to expected output. |
| 289 FilePath golden_output_file = | 287 FilePath golden_output_file = |
| 290 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 288 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 291 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 289 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 292 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 290 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 293 } | 291 } |
| OLD | NEW |