| 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/metrics/histogram_samples.h" |
| 15 #include "base/metrics/statistics_recorder.h" |
| 13 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 14 #include "base/prefs/pref_filter.h" | 17 #include "base/prefs/pref_filter.h" |
| 15 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 19 #include "base/single_thread_task_runner.h" |
| 16 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 23 #include "base/test/simple_test_clock.h" |
| 19 #include "base/threading/sequenced_worker_pool.h" | 24 #include "base/threading/sequenced_worker_pool.h" |
| 20 #include "base/threading/thread.h" | 25 #include "base/threading/thread.h" |
| 21 #include "base/values.h" | 26 #include "base/values.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 29 |
| 25 namespace base { | 30 namespace base { |
| 26 namespace { | 31 namespace { |
| 27 | 32 |
| 28 const char kHomePage[] = "homepage"; | 33 const char kHomePage[] = "homepage"; |
| 29 | 34 |
| 35 // Set the time on the given SimpleTestClock to the given time in minutes. |
| 36 void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { |
| 37 const int32_t kBaseTimeMins = 100; |
| 38 clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); |
| 39 } |
| 40 |
| 30 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 41 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
| 31 // to the |prefs| until explicitly asked to release them. | 42 // to the |prefs| until explicitly asked to release them. |
| 32 class InterceptingPrefFilter : public PrefFilter { | 43 class InterceptingPrefFilter : public PrefFilter { |
| 33 public: | 44 public: |
| 34 InterceptingPrefFilter(); | 45 InterceptingPrefFilter(); |
| 35 ~InterceptingPrefFilter() override; | 46 ~InterceptingPrefFilter() override; |
| 36 | 47 |
| 37 // PrefFilter implementation: | 48 // PrefFilter implementation: |
| 38 void FilterOnLoad( | 49 void FilterOnLoad( |
| 39 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 50 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 91 | 102 |
| 92 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | 103 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); |
| 93 data_dir_ = data_dir_.AppendASCII("prefs"); | 104 data_dir_ = data_dir_.AppendASCII("prefs"); |
| 94 ASSERT_TRUE(PathExists(data_dir_)); | 105 ASSERT_TRUE(PathExists(data_dir_)); |
| 95 } | 106 } |
| 96 | 107 |
| 97 void TearDown() override { | 108 void TearDown() override { |
| 98 // Make sure all pending tasks have been processed (e.g., deleting the | 109 // Make sure all pending tasks have been processed (e.g., deleting the |
| 99 // JsonPrefStore may post write tasks). | 110 // JsonPrefStore may post write tasks). |
| 100 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); | 111 message_loop_.task_runner()->PostTask(FROM_HERE, |
| 112 MessageLoop::QuitWhenIdleClosure()); |
| 101 message_loop_.Run(); | 113 message_loop_.Run(); |
| 102 } | 114 } |
| 103 | 115 |
| 104 // The path to temporary directory used to contain the test operations. | 116 // The path to temporary directory used to contain the test operations. |
| 105 base::ScopedTempDir temp_dir_; | 117 base::ScopedTempDir temp_dir_; |
| 106 // The path to the directory where the test data is stored. | 118 // The path to the directory where the test data is stored. |
| 107 base::FilePath data_dir_; | 119 base::FilePath data_dir_; |
| 108 // A message loop that we can use as the file thread message loop. | 120 // A message loop that we can use as the file thread message loop. |
| 109 MessageLoop message_loop_; | 121 MessageLoop message_loop_; |
| 122 |
| 123 private: |
| 124 // Ensure histograms are reset for each test. |
| 125 StatisticsRecorder statistics_recorder_; |
| 110 }; | 126 }; |
| 111 | 127 |
| 112 // Test fallback behavior for a nonexistent file. | 128 // Test fallback behavior for a nonexistent file. |
| 113 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 129 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 114 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 130 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 115 ASSERT_FALSE(PathExists(bogus_input_file)); | 131 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 116 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 117 bogus_input_file, | 133 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 118 message_loop_.message_loop_proxy().get(), | |
| 119 scoped_ptr<PrefFilter>()); | |
| 120 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 134 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 121 pref_store->ReadPrefs()); | 135 pref_store->ReadPrefs()); |
| 122 EXPECT_FALSE(pref_store->ReadOnly()); | 136 EXPECT_FALSE(pref_store->ReadOnly()); |
| 123 } | 137 } |
| 124 | 138 |
| 125 // Test fallback behavior for a nonexistent file and alternate file. | 139 // Test fallback behavior for a nonexistent file and alternate file. |
| 126 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 140 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
| 127 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 141 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 128 base::FilePath bogus_alternate_input_file = | 142 base::FilePath bogus_alternate_input_file = |
| 129 data_dir_.AppendASCII("read_alternate.txt"); | 143 data_dir_.AppendASCII("read_alternate.txt"); |
| 130 ASSERT_FALSE(PathExists(bogus_input_file)); | 144 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 131 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 145 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
| 132 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 146 scoped_refptr<JsonPrefStore> pref_store = |
| 133 bogus_input_file, | 147 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, |
| 134 bogus_alternate_input_file, | 148 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 135 message_loop_.message_loop_proxy().get(), | |
| 136 scoped_ptr<PrefFilter>()); | |
| 137 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 149 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
| 138 pref_store->ReadPrefs()); | 150 pref_store->ReadPrefs()); |
| 139 EXPECT_FALSE(pref_store->ReadOnly()); | 151 EXPECT_FALSE(pref_store->ReadOnly()); |
| 140 } | 152 } |
| 141 | 153 |
| 142 // Test fallback behavior for an invalid file. | 154 // Test fallback behavior for an invalid file. |
| 143 TEST_F(JsonPrefStoreTest, InvalidFile) { | 155 TEST_F(JsonPrefStoreTest, InvalidFile) { |
| 144 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | 156 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); |
| 145 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 157 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
| 146 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 158 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); |
| 147 scoped_refptr<JsonPrefStore> pref_store = | 159 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 148 new JsonPrefStore(invalid_file, | 160 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 149 message_loop_.message_loop_proxy().get(), | |
| 150 scoped_ptr<PrefFilter>()); | |
| 151 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 161 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
| 152 pref_store->ReadPrefs()); | 162 pref_store->ReadPrefs()); |
| 153 EXPECT_FALSE(pref_store->ReadOnly()); | 163 EXPECT_FALSE(pref_store->ReadOnly()); |
| 154 | 164 |
| 155 // The file should have been moved aside. | 165 // The file should have been moved aside. |
| 156 EXPECT_FALSE(PathExists(invalid_file)); | 166 EXPECT_FALSE(PathExists(invalid_file)); |
| 157 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 167 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
| 158 EXPECT_TRUE(PathExists(moved_aside)); | 168 EXPECT_TRUE(PathExists(moved_aside)); |
| 159 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); | 169 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); |
| 160 } | 170 } |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 236 } |
| 227 | 237 |
| 228 TEST_F(JsonPrefStoreTest, Basic) { | 238 TEST_F(JsonPrefStoreTest, Basic) { |
| 229 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 239 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 230 temp_dir_.path().AppendASCII("write.json"))); | 240 temp_dir_.path().AppendASCII("write.json"))); |
| 231 | 241 |
| 232 // Test that the persistent value can be loaded. | 242 // Test that the persistent value can be loaded. |
| 233 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 243 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 234 ASSERT_TRUE(PathExists(input_file)); | 244 ASSERT_TRUE(PathExists(input_file)); |
| 235 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 245 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 236 input_file, | 246 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 237 message_loop_.message_loop_proxy().get(), | |
| 238 scoped_ptr<PrefFilter>()); | |
| 239 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 247 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 240 EXPECT_FALSE(pref_store->ReadOnly()); | 248 EXPECT_FALSE(pref_store->ReadOnly()); |
| 241 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 249 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| 242 | 250 |
| 243 // The JSON file looks like this: | 251 // The JSON file looks like this: |
| 244 // { | 252 // { |
| 245 // "homepage": "http://www.cnn.com", | 253 // "homepage": "http://www.cnn.com", |
| 246 // "some_directory": "/usr/local/", | 254 // "some_directory": "/usr/local/", |
| 247 // "tabs": { | 255 // "tabs": { |
| 248 // "new_windows_in_tabs": true, | 256 // "new_windows_in_tabs": true, |
| 249 // "max_tabs": 20 | 257 // "max_tabs": 20 |
| 250 // } | 258 // } |
| 251 // } | 259 // } |
| 252 | 260 |
| 253 RunBasicJsonPrefStoreTest( | 261 RunBasicJsonPrefStoreTest( |
| 254 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 262 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 255 } | 263 } |
| 256 | 264 |
| 257 TEST_F(JsonPrefStoreTest, BasicAsync) { | 265 TEST_F(JsonPrefStoreTest, BasicAsync) { |
| 258 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 266 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 259 temp_dir_.path().AppendASCII("write.json"))); | 267 temp_dir_.path().AppendASCII("write.json"))); |
| 260 | 268 |
| 261 // Test that the persistent value can be loaded. | 269 // Test that the persistent value can be loaded. |
| 262 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 270 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 263 ASSERT_TRUE(PathExists(input_file)); | 271 ASSERT_TRUE(PathExists(input_file)); |
| 264 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 272 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 265 input_file, | 273 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 266 message_loop_.message_loop_proxy().get(), | |
| 267 scoped_ptr<PrefFilter>()); | |
| 268 | 274 |
| 269 { | 275 { |
| 270 MockPrefStoreObserver mock_observer; | 276 MockPrefStoreObserver mock_observer; |
| 271 pref_store->AddObserver(&mock_observer); | 277 pref_store->AddObserver(&mock_observer); |
| 272 | 278 |
| 273 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 279 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 274 pref_store->ReadPrefsAsync(mock_error_delegate); | 280 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 275 | 281 |
| 276 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 282 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 277 EXPECT_CALL(*mock_error_delegate, | 283 EXPECT_CALL(*mock_error_delegate, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 294 // } | 300 // } |
| 295 | 301 |
| 296 RunBasicJsonPrefStoreTest( | 302 RunBasicJsonPrefStoreTest( |
| 297 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 303 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 298 } | 304 } |
| 299 | 305 |
| 300 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 306 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
| 301 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 307 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 302 | 308 |
| 303 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 309 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 304 pref_file, | 310 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 305 message_loop_.message_loop_proxy(), | |
| 306 scoped_ptr<PrefFilter>()); | |
| 307 | 311 |
| 308 // Set some keys with empty values. | 312 // Set some keys with empty values. |
| 309 pref_store->SetValue("list", new base::ListValue); | 313 pref_store->SetValue("list", new base::ListValue); |
| 310 pref_store->SetValue("dict", new base::DictionaryValue); | 314 pref_store->SetValue("dict", new base::DictionaryValue); |
| 311 | 315 |
| 312 // Write to file. | 316 // Write to file. |
| 313 pref_store->CommitPendingWrite(); | 317 pref_store->CommitPendingWrite(); |
| 314 MessageLoop::current()->RunUntilIdle(); | 318 MessageLoop::current()->RunUntilIdle(); |
| 315 | 319 |
| 316 // Reload. | 320 // Reload. |
| 317 pref_store = new JsonPrefStore( | 321 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
| 318 pref_file, | 322 scoped_ptr<PrefFilter>()); |
| 319 message_loop_.message_loop_proxy(), | |
| 320 scoped_ptr<PrefFilter>()); | |
| 321 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 323 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 322 ASSERT_FALSE(pref_store->ReadOnly()); | 324 ASSERT_FALSE(pref_store->ReadOnly()); |
| 323 | 325 |
| 324 // Check values. | 326 // Check values. |
| 325 const Value* result = NULL; | 327 const Value* result = NULL; |
| 326 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 328 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
| 327 EXPECT_TRUE(ListValue().Equals(result)); | 329 EXPECT_TRUE(ListValue().Equals(result)); |
| 328 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 330 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
| 329 EXPECT_TRUE(DictionaryValue().Equals(result)); | 331 EXPECT_TRUE(DictionaryValue().Equals(result)); |
| 330 } | 332 } |
| 331 | 333 |
| 332 // This test is just documenting some potentially non-obvious behavior. It | 334 // This test is just documenting some potentially non-obvious behavior. It |
| 333 // shouldn't be taken as normative. | 335 // shouldn't be taken as normative. |
| 334 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 336 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
| 335 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 337 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
| 336 | 338 |
| 337 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 339 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 338 pref_file, | 340 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 339 message_loop_.message_loop_proxy(), | |
| 340 scoped_ptr<PrefFilter>()); | |
| 341 | 341 |
| 342 base::DictionaryValue* dict = new base::DictionaryValue; | 342 base::DictionaryValue* dict = new base::DictionaryValue; |
| 343 dict->SetString("key", "value"); | 343 dict->SetString("key", "value"); |
| 344 pref_store->SetValue("dict", dict); | 344 pref_store->SetValue("dict", dict); |
| 345 | 345 |
| 346 pref_store->RemoveValue("dict.key"); | 346 pref_store->RemoveValue("dict.key"); |
| 347 | 347 |
| 348 const base::Value* retrieved_dict = NULL; | 348 const base::Value* retrieved_dict = NULL; |
| 349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 349 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
| 350 EXPECT_FALSE(has_dict); | 350 EXPECT_FALSE(has_dict); |
| 351 } | 351 } |
| 352 | 352 |
| 353 // Tests asynchronous reading of the file when there is no file. | 353 // Tests asynchronous reading of the file when there is no file. |
| 354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 354 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 355 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 356 ASSERT_FALSE(PathExists(bogus_input_file)); | 356 ASSERT_FALSE(PathExists(bogus_input_file)); |
| 357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 357 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 358 bogus_input_file, | 358 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 359 message_loop_.message_loop_proxy().get(), | |
| 360 scoped_ptr<PrefFilter>()); | |
| 361 MockPrefStoreObserver mock_observer; | 359 MockPrefStoreObserver mock_observer; |
| 362 pref_store->AddObserver(&mock_observer); | 360 pref_store->AddObserver(&mock_observer); |
| 363 | 361 |
| 364 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 362 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
| 365 pref_store->ReadPrefsAsync(mock_error_delegate); | 363 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 366 | 364 |
| 367 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 365 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
| 368 EXPECT_CALL(*mock_error_delegate, | 366 EXPECT_CALL(*mock_error_delegate, |
| 369 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 367 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
| 370 RunLoop().RunUntilIdle(); | 368 RunLoop().RunUntilIdle(); |
| 371 pref_store->RemoveObserver(&mock_observer); | 369 pref_store->RemoveObserver(&mock_observer); |
| 372 | 370 |
| 373 EXPECT_FALSE(pref_store->ReadOnly()); | 371 EXPECT_FALSE(pref_store->ReadOnly()); |
| 374 } | 372 } |
| 375 | 373 |
| 376 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 374 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
| 377 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 375 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 378 temp_dir_.path().AppendASCII("write.json"))); | 376 temp_dir_.path().AppendASCII("write.json"))); |
| 379 | 377 |
| 380 // Test that the persistent value can be loaded. | 378 // Test that the persistent value can be loaded. |
| 381 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 379 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 382 ASSERT_TRUE(PathExists(input_file)); | 380 ASSERT_TRUE(PathExists(input_file)); |
| 383 | 381 |
| 384 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 382 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 385 new InterceptingPrefFilter()); | 383 new InterceptingPrefFilter()); |
| 386 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 384 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 387 intercepting_pref_filter.get(); | 385 intercepting_pref_filter.get(); |
| 388 scoped_refptr<JsonPrefStore> pref_store = | 386 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 389 new JsonPrefStore(input_file, | 387 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); |
| 390 message_loop_.message_loop_proxy().get(), | |
| 391 intercepting_pref_filter.Pass()); | |
| 392 | 388 |
| 393 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 389 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
| 394 pref_store->ReadPrefs()); | 390 pref_store->ReadPrefs()); |
| 395 EXPECT_FALSE(pref_store->ReadOnly()); | 391 EXPECT_FALSE(pref_store->ReadOnly()); |
| 396 | 392 |
| 397 // The store shouldn't be considered initialized until the interceptor | 393 // The store shouldn't be considered initialized until the interceptor |
| 398 // returns. | 394 // returns. |
| 399 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); | 395 EXPECT_TRUE(raw_intercepting_pref_filter_->has_intercepted_prefs()); |
| 400 EXPECT_FALSE(pref_store->IsInitializationComplete()); | 396 EXPECT_FALSE(pref_store->IsInitializationComplete()); |
| 401 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); | 397 EXPECT_FALSE(pref_store->GetValue(kHomePage, NULL)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 425 temp_dir_.path().AppendASCII("write.json"))); | 421 temp_dir_.path().AppendASCII("write.json"))); |
| 426 | 422 |
| 427 // Test that the persistent value can be loaded. | 423 // Test that the persistent value can be loaded. |
| 428 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 424 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 429 ASSERT_TRUE(PathExists(input_file)); | 425 ASSERT_TRUE(PathExists(input_file)); |
| 430 | 426 |
| 431 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 427 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
| 432 new InterceptingPrefFilter()); | 428 new InterceptingPrefFilter()); |
| 433 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 429 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
| 434 intercepting_pref_filter.get(); | 430 intercepting_pref_filter.get(); |
| 435 scoped_refptr<JsonPrefStore> pref_store = | 431 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
| 436 new JsonPrefStore(input_file, | 432 input_file, message_loop_.task_runner(), intercepting_pref_filter.Pass()); |
| 437 message_loop_.message_loop_proxy().get(), | |
| 438 intercepting_pref_filter.Pass()); | |
| 439 | 433 |
| 440 MockPrefStoreObserver mock_observer; | 434 MockPrefStoreObserver mock_observer; |
| 441 pref_store->AddObserver(&mock_observer); | 435 pref_store->AddObserver(&mock_observer); |
| 442 | 436 |
| 443 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. | 437 // Ownership of the |mock_error_delegate| is handed to the |pref_store| below. |
| 444 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 438 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 445 | 439 |
| 446 { | 440 { |
| 447 pref_store->ReadPrefsAsync(mock_error_delegate); | 441 pref_store->ReadPrefsAsync(mock_error_delegate); |
| 448 | 442 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 base::CopyFile(data_dir_.AppendASCII("read.json"), | 485 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 492 temp_dir_.path().AppendASCII("alternate.json"))); | 486 temp_dir_.path().AppendASCII("alternate.json"))); |
| 493 | 487 |
| 494 // Test that the alternate file is moved to the main file and read as-is from | 488 // Test that the alternate file is moved to the main file and read as-is from |
| 495 // there. | 489 // there. |
| 496 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 490 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 497 base::FilePath alternate_input_file = | 491 base::FilePath alternate_input_file = |
| 498 temp_dir_.path().AppendASCII("alternate.json"); | 492 temp_dir_.path().AppendASCII("alternate.json"); |
| 499 ASSERT_FALSE(PathExists(input_file)); | 493 ASSERT_FALSE(PathExists(input_file)); |
| 500 ASSERT_TRUE(PathExists(alternate_input_file)); | 494 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 501 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 495 scoped_refptr<JsonPrefStore> pref_store = |
| 502 input_file, | 496 new JsonPrefStore(input_file, alternate_input_file, |
| 503 alternate_input_file, | 497 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 504 message_loop_.message_loop_proxy().get(), | |
| 505 scoped_ptr<PrefFilter>()); | |
| 506 | 498 |
| 507 ASSERT_FALSE(PathExists(input_file)); | 499 ASSERT_FALSE(PathExists(input_file)); |
| 508 ASSERT_TRUE(PathExists(alternate_input_file)); | 500 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 509 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 501 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 510 | 502 |
| 511 ASSERT_TRUE(PathExists(input_file)); | 503 ASSERT_TRUE(PathExists(input_file)); |
| 512 ASSERT_FALSE(PathExists(alternate_input_file)); | 504 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 513 | 505 |
| 514 EXPECT_FALSE(pref_store->ReadOnly()); | 506 EXPECT_FALSE(pref_store->ReadOnly()); |
| 515 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 507 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 537 temp_dir_.path().AppendASCII("alternate.json"))); | 529 temp_dir_.path().AppendASCII("alternate.json"))); |
| 538 | 530 |
| 539 // Test that the alternate file is ignored and that the read occurs from the | 531 // Test that the alternate file is ignored and that the read occurs from the |
| 540 // existing main file. There is no attempt at even deleting the alternate | 532 // existing main file. There is no attempt at even deleting the alternate |
| 541 // file as this scenario should never happen in normal user-data-dirs. | 533 // file as this scenario should never happen in normal user-data-dirs. |
| 542 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 534 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 543 base::FilePath alternate_input_file = | 535 base::FilePath alternate_input_file = |
| 544 temp_dir_.path().AppendASCII("alternate.json"); | 536 temp_dir_.path().AppendASCII("alternate.json"); |
| 545 ASSERT_TRUE(PathExists(input_file)); | 537 ASSERT_TRUE(PathExists(input_file)); |
| 546 ASSERT_TRUE(PathExists(alternate_input_file)); | 538 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 547 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 539 scoped_refptr<JsonPrefStore> pref_store = |
| 548 input_file, | 540 new JsonPrefStore(input_file, alternate_input_file, |
| 549 alternate_input_file, | 541 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 550 message_loop_.message_loop_proxy().get(), | |
| 551 scoped_ptr<PrefFilter>()); | |
| 552 | 542 |
| 553 ASSERT_TRUE(PathExists(input_file)); | 543 ASSERT_TRUE(PathExists(input_file)); |
| 554 ASSERT_TRUE(PathExists(alternate_input_file)); | 544 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 555 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 545 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 556 | 546 |
| 557 ASSERT_TRUE(PathExists(input_file)); | 547 ASSERT_TRUE(PathExists(input_file)); |
| 558 ASSERT_TRUE(PathExists(alternate_input_file)); | 548 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 559 | 549 |
| 560 EXPECT_FALSE(pref_store->ReadOnly()); | 550 EXPECT_FALSE(pref_store->ReadOnly()); |
| 561 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 551 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 579 base::CopyFile(data_dir_.AppendASCII("read.json"), | 569 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 580 temp_dir_.path().AppendASCII("write.json"))); | 570 temp_dir_.path().AppendASCII("write.json"))); |
| 581 | 571 |
| 582 // Test that the basic read works fine when an alternate file is specified but | 572 // Test that the basic read works fine when an alternate file is specified but |
| 583 // does not exist. | 573 // does not exist. |
| 584 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 574 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 585 base::FilePath alternate_input_file = | 575 base::FilePath alternate_input_file = |
| 586 temp_dir_.path().AppendASCII("alternate.json"); | 576 temp_dir_.path().AppendASCII("alternate.json"); |
| 587 ASSERT_TRUE(PathExists(input_file)); | 577 ASSERT_TRUE(PathExists(input_file)); |
| 588 ASSERT_FALSE(PathExists(alternate_input_file)); | 578 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 589 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 579 scoped_refptr<JsonPrefStore> pref_store = |
| 590 input_file, | 580 new JsonPrefStore(input_file, alternate_input_file, |
| 591 alternate_input_file, | 581 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 592 message_loop_.message_loop_proxy().get(), | |
| 593 scoped_ptr<PrefFilter>()); | |
| 594 | 582 |
| 595 ASSERT_TRUE(PathExists(input_file)); | 583 ASSERT_TRUE(PathExists(input_file)); |
| 596 ASSERT_FALSE(PathExists(alternate_input_file)); | 584 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 597 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 585 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
| 598 | 586 |
| 599 ASSERT_TRUE(PathExists(input_file)); | 587 ASSERT_TRUE(PathExists(input_file)); |
| 600 ASSERT_FALSE(PathExists(alternate_input_file)); | 588 ASSERT_FALSE(PathExists(alternate_input_file)); |
| 601 | 589 |
| 602 EXPECT_FALSE(pref_store->ReadOnly()); | 590 EXPECT_FALSE(pref_store->ReadOnly()); |
| 603 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 591 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 621 base::CopyFile(data_dir_.AppendASCII("read.json"), | 609 base::CopyFile(data_dir_.AppendASCII("read.json"), |
| 622 temp_dir_.path().AppendASCII("alternate.json"))); | 610 temp_dir_.path().AppendASCII("alternate.json"))); |
| 623 | 611 |
| 624 // Test that the alternate file is moved to the main file and read as-is from | 612 // Test that the alternate file is moved to the main file and read as-is from |
| 625 // there even when the read is made asynchronously. | 613 // there even when the read is made asynchronously. |
| 626 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 614 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
| 627 base::FilePath alternate_input_file = | 615 base::FilePath alternate_input_file = |
| 628 temp_dir_.path().AppendASCII("alternate.json"); | 616 temp_dir_.path().AppendASCII("alternate.json"); |
| 629 ASSERT_FALSE(PathExists(input_file)); | 617 ASSERT_FALSE(PathExists(input_file)); |
| 630 ASSERT_TRUE(PathExists(alternate_input_file)); | 618 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 631 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 619 scoped_refptr<JsonPrefStore> pref_store = |
| 632 input_file, | 620 new JsonPrefStore(input_file, alternate_input_file, |
| 633 alternate_input_file, | 621 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
| 634 message_loop_.message_loop_proxy().get(), | |
| 635 scoped_ptr<PrefFilter>()); | |
| 636 | 622 |
| 637 ASSERT_FALSE(PathExists(input_file)); | 623 ASSERT_FALSE(PathExists(input_file)); |
| 638 ASSERT_TRUE(PathExists(alternate_input_file)); | 624 ASSERT_TRUE(PathExists(alternate_input_file)); |
| 639 | 625 |
| 640 { | 626 { |
| 641 MockPrefStoreObserver mock_observer; | 627 MockPrefStoreObserver mock_observer; |
| 642 pref_store->AddObserver(&mock_observer); | 628 pref_store->AddObserver(&mock_observer); |
| 643 | 629 |
| 644 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 630 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
| 645 pref_store->ReadPrefsAsync(mock_error_delegate); | 631 pref_store->ReadPrefsAsync(mock_error_delegate); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 664 // "tabs": { | 650 // "tabs": { |
| 665 // "new_windows_in_tabs": true, | 651 // "new_windows_in_tabs": true, |
| 666 // "max_tabs": 20 | 652 // "max_tabs": 20 |
| 667 // } | 653 // } |
| 668 // } | 654 // } |
| 669 | 655 |
| 670 RunBasicJsonPrefStoreTest( | 656 RunBasicJsonPrefStoreTest( |
| 671 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | 657 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); |
| 672 } | 658 } |
| 673 | 659 |
| 660 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
| 661 SimpleTestClock* test_clock = new SimpleTestClock; |
| 662 SetCurrentTimeInMinutes(0, test_clock); |
| 663 JsonPrefStore::WriteCountHistogram histogram( |
| 664 base::TimeDelta::FromSeconds(10), |
| 665 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 666 scoped_ptr<base::Clock>(test_clock)); |
| 667 int32 report_interval = |
| 668 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 669 |
| 670 histogram.RecordWriteOccured(); |
| 671 |
| 672 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 673 histogram.ReportOutstandingWrites(); |
| 674 scoped_ptr<HistogramSamples> samples = |
| 675 histogram.GetHistogram()->SnapshotSamples(); |
| 676 ASSERT_EQ(1, samples->GetCount(1)); |
| 677 ASSERT_EQ(1, samples->TotalCount()); |
| 678 |
| 679 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
| 680 histogram.GetHistogram()->histogram_name()); |
| 681 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
| 682 } |
| 683 |
| 684 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
| 685 SimpleTestClock* test_clock = new SimpleTestClock; |
| 686 SetCurrentTimeInMinutes(0, test_clock); |
| 687 JsonPrefStore::WriteCountHistogram histogram( |
| 688 base::TimeDelta::FromSeconds(10), |
| 689 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 690 scoped_ptr<base::Clock>(test_clock)); |
| 691 int32 report_interval = |
| 692 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 693 |
| 694 histogram.RecordWriteOccured(); |
| 695 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 696 histogram.RecordWriteOccured(); |
| 697 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 698 histogram.RecordWriteOccured(); |
| 699 |
| 700 // Nothing should be recorded until the report period has elapsed. |
| 701 scoped_ptr<HistogramSamples> samples = |
| 702 histogram.GetHistogram()->SnapshotSamples(); |
| 703 ASSERT_EQ(0, samples->TotalCount()); |
| 704 |
| 705 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 706 histogram.RecordWriteOccured(); |
| 707 |
| 708 // Now the report period has elapsed. |
| 709 samples = histogram.GetHistogram()->SnapshotSamples(); |
| 710 ASSERT_EQ(1, samples->GetCount(3)); |
| 711 ASSERT_EQ(1, samples->TotalCount()); |
| 712 |
| 713 // The last write won't be recorded because the second count period hasn't |
| 714 // fully elapsed. |
| 715 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 716 histogram.ReportOutstandingWrites(); |
| 717 |
| 718 samples = histogram.GetHistogram()->SnapshotSamples(); |
| 719 ASSERT_EQ(1, samples->GetCount(3)); |
| 720 ASSERT_EQ(1, samples->TotalCount()); |
| 721 } |
| 722 |
| 723 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
| 724 SimpleTestClock* test_clock = new SimpleTestClock; |
| 725 SetCurrentTimeInMinutes(0, test_clock); |
| 726 JsonPrefStore::WriteCountHistogram histogram( |
| 727 base::TimeDelta::FromSeconds(10), |
| 728 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 729 scoped_ptr<base::Clock>(test_clock)); |
| 730 int32 report_interval = |
| 731 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 732 |
| 733 histogram.RecordWriteOccured(); |
| 734 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
| 735 histogram.RecordWriteOccured(); |
| 736 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
| 737 histogram.RecordWriteOccured(); |
| 738 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
| 739 histogram.RecordWriteOccured(); |
| 740 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
| 741 histogram.RecordWriteOccured(); |
| 742 SetCurrentTimeInMinutes(2.1 * report_interval, test_clock); |
| 743 histogram.RecordWriteOccured(); |
| 744 SetCurrentTimeInMinutes(2.5 * report_interval, test_clock); |
| 745 histogram.RecordWriteOccured(); |
| 746 SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
| 747 histogram.RecordWriteOccured(); |
| 748 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 749 histogram.RecordWriteOccured(); |
| 750 |
| 751 // The last write won't be recorded because the second count period hasn't |
| 752 // fully elapsed |
| 753 SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
| 754 histogram.ReportOutstandingWrites(); |
| 755 scoped_ptr<HistogramSamples> samples = |
| 756 histogram.GetHistogram()->SnapshotSamples(); |
| 757 ASSERT_EQ(2, samples->GetCount(3)); |
| 758 ASSERT_EQ(1, samples->GetCount(2)); |
| 759 ASSERT_EQ(3, samples->TotalCount()); |
| 760 } |
| 761 |
| 762 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
| 763 SimpleTestClock* test_clock = new SimpleTestClock; |
| 764 SetCurrentTimeInMinutes(0, test_clock); |
| 765 JsonPrefStore::WriteCountHistogram histogram( |
| 766 base::TimeDelta::FromSeconds(10), |
| 767 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
| 768 scoped_ptr<base::Clock>(test_clock)); |
| 769 int32 report_interval = |
| 770 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
| 771 |
| 772 // 1 write in the first period. |
| 773 histogram.RecordWriteOccured(); |
| 774 |
| 775 // No writes in the second and third periods. |
| 776 |
| 777 // 2 writes in the fourth period. |
| 778 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
| 779 histogram.RecordWriteOccured(); |
| 780 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
| 781 histogram.RecordWriteOccured(); |
| 782 |
| 783 // No writes in the fifth period. |
| 784 |
| 785 // 3 writes in the sixth period. |
| 786 SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
| 787 histogram.RecordWriteOccured(); |
| 788 SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
| 789 histogram.RecordWriteOccured(); |
| 790 SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
| 791 histogram.RecordWriteOccured(); |
| 792 |
| 793 SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
| 794 histogram.ReportOutstandingWrites(); |
| 795 scoped_ptr<HistogramSamples> samples = |
| 796 histogram.GetHistogram()->SnapshotSamples(); |
| 797 ASSERT_EQ(3, samples->GetCount(0)); |
| 798 ASSERT_EQ(1, samples->GetCount(1)); |
| 799 ASSERT_EQ(1, samples->GetCount(2)); |
| 800 ASSERT_EQ(1, samples->GetCount(3)); |
| 801 ASSERT_EQ(6, samples->TotalCount()); |
| 802 } |
| 803 |
| 674 } // namespace base | 804 } // namespace base |
| OLD | NEW |