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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
19 #include "base/metrics/histogram_samples.h" | 19 #include "base/metrics/histogram_samples.h" |
20 #include "base/metrics/statistics_recorder.h" | |
21 #include "base/path_service.h" | 20 #include "base/path_service.h" |
22 #include "base/prefs/pref_filter.h" | 21 #include "base/prefs/pref_filter.h" |
23 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
24 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
25 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
26 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
27 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
| 27 #include "base/test/histogram_tester.h" |
28 #include "base/test/simple_test_clock.h" | 28 #include "base/test/simple_test_clock.h" |
29 #include "base/threading/sequenced_worker_pool.h" | 29 #include "base/threading/sequenced_worker_pool.h" |
30 #include "base/threading/thread.h" | 30 #include "base/threading/thread.h" |
31 #include "base/values.h" | 31 #include "base/values.h" |
32 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
34 | 34 |
35 namespace base { | 35 namespace base { |
36 namespace { | 36 namespace { |
37 | 37 |
38 const char kHomePage[] = "homepage"; | 38 const char kHomePage[] = "homepage"; |
39 | 39 |
| 40 const char kReadJson[] = |
| 41 "{\n" |
| 42 " \"homepage\": \"http://www.cnn.com\",\n" |
| 43 " \"some_directory\": \"/usr/local/\",\n" |
| 44 " \"tabs\": {\n" |
| 45 " \"new_windows_in_tabs\": true,\n" |
| 46 " \"max_tabs\": 20\n" |
| 47 " }\n" |
| 48 "}"; |
| 49 |
| 50 const char kInvalidJson[] = "!@#$%^&"; |
| 51 |
| 52 // Expected output for tests using RunBasicJsonPrefStoreTest(). |
| 53 const char kWriteGolden[] = |
| 54 "{\"homepage\":\"http://www.cnn.com\"," |
| 55 "\"long_int\":{\"pref\":\"214748364842\"}," |
| 56 "\"some_directory\":\"/usr/sbin/\"," |
| 57 "\"tabs\":{\"max_tabs\":10,\"new_windows_in_tabs\":false}}"; |
| 58 |
40 // Set the time on the given SimpleTestClock to the given time in minutes. | 59 // Set the time on the given SimpleTestClock to the given time in minutes. |
41 void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { | 60 void SetCurrentTimeInMinutes(double minutes, base::SimpleTestClock* clock) { |
42 const int32_t kBaseTimeMins = 100; | 61 const int32_t kBaseTimeMins = 100; |
43 clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); | 62 clock->SetNow(base::Time::FromDoubleT((kBaseTimeMins + minutes) * 60)); |
44 } | 63 } |
45 | 64 |
46 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 65 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
47 // to the |prefs| until explicitly asked to release them. | 66 // to the |prefs| until explicitly asked to release them. |
48 class InterceptingPrefFilter : public PrefFilter { | 67 class InterceptingPrefFilter : public PrefFilter { |
49 public: | 68 public: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 public: | 116 public: |
98 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); | 117 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
99 }; | 118 }; |
100 | 119 |
101 } // namespace | 120 } // namespace |
102 | 121 |
103 class JsonPrefStoreTest : public testing::Test { | 122 class JsonPrefStoreTest : public testing::Test { |
104 protected: | 123 protected: |
105 void SetUp() override { | 124 void SetUp() override { |
106 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 125 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
107 | |
108 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_)); | |
109 data_dir_ = data_dir_.AppendASCII("prefs"); | |
110 ASSERT_TRUE(PathExists(data_dir_)); | |
111 } | 126 } |
112 | 127 |
113 void TearDown() override { | 128 void TearDown() override { |
114 // Make sure all pending tasks have been processed (e.g., deleting the | 129 // Make sure all pending tasks have been processed (e.g., deleting the |
115 // JsonPrefStore may post write tasks). | 130 // JsonPrefStore may post write tasks). |
116 RunLoop().RunUntilIdle(); | 131 RunLoop().RunUntilIdle(); |
117 } | 132 } |
118 | 133 |
119 // The path to temporary directory used to contain the test operations. | 134 // The path to temporary directory used to contain the test operations. |
120 base::ScopedTempDir temp_dir_; | 135 base::ScopedTempDir temp_dir_; |
121 // The path to the directory where the test data is stored. | |
122 base::FilePath data_dir_; | |
123 // A message loop that we can use as the file thread message loop. | 136 // A message loop that we can use as the file thread message loop. |
124 MessageLoop message_loop_; | 137 MessageLoop message_loop_; |
125 | |
126 private: | |
127 // Ensure histograms are reset for each test. | |
128 StatisticsRecorder statistics_recorder_; | |
129 }; | 138 }; |
130 | 139 |
131 // Test fallback behavior for a nonexistent file. | 140 // Test fallback behavior for a nonexistent file. |
132 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 141 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
133 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 142 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
134 ASSERT_FALSE(PathExists(bogus_input_file)); | 143 ASSERT_FALSE(PathExists(bogus_input_file)); |
135 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 144 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
136 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 145 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
137 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 146 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
138 pref_store->ReadPrefs()); | 147 pref_store->ReadPrefs()); |
(...skipping 10 matching lines...) Expand all Loading... |
149 scoped_refptr<JsonPrefStore> pref_store = | 158 scoped_refptr<JsonPrefStore> pref_store = |
150 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, | 159 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, |
151 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 160 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
152 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 161 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
153 pref_store->ReadPrefs()); | 162 pref_store->ReadPrefs()); |
154 EXPECT_FALSE(pref_store->ReadOnly()); | 163 EXPECT_FALSE(pref_store->ReadOnly()); |
155 } | 164 } |
156 | 165 |
157 // Test fallback behavior for an invalid file. | 166 // Test fallback behavior for an invalid file. |
158 TEST_F(JsonPrefStoreTest, InvalidFile) { | 167 TEST_F(JsonPrefStoreTest, InvalidFile) { |
159 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); | |
160 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 168 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
161 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file)); | 169 ASSERT_LT(0, base::WriteFile(invalid_file, |
| 170 kInvalidJson, arraysize(kInvalidJson) - 1)); |
| 171 |
162 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 172 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
163 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 173 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 174 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
165 pref_store->ReadPrefs()); | 175 pref_store->ReadPrefs()); |
166 EXPECT_FALSE(pref_store->ReadOnly()); | 176 EXPECT_FALSE(pref_store->ReadOnly()); |
167 | 177 |
168 // The file should have been moved aside. | 178 // The file should have been moved aside. |
169 EXPECT_FALSE(PathExists(invalid_file)); | 179 EXPECT_FALSE(PathExists(invalid_file)); |
170 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 180 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
171 EXPECT_TRUE(PathExists(moved_aside)); | 181 EXPECT_TRUE(PathExists(moved_aside)); |
172 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside)); | 182 |
| 183 std::string moved_aside_contents; |
| 184 ASSERT_TRUE(base::ReadFileToString(moved_aside, &moved_aside_contents)); |
| 185 EXPECT_EQ(kInvalidJson, moved_aside_contents); |
173 } | 186 } |
174 | 187 |
175 // This function is used to avoid code duplication while testing synchronous and | 188 // This function is used to avoid code duplication while testing synchronous |
176 // asynchronous version of the JsonPrefStore loading. | 189 // and asynchronous version of the JsonPrefStore loading. It validates that the |
| 190 // given output file's contents matches kWriteGolden. |
177 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, | 191 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, |
178 const base::FilePath& output_file, | 192 const base::FilePath& output_file) { |
179 const base::FilePath& golden_output_file) { | |
180 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; | 193 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
181 const char kMaxTabs[] = "tabs.max_tabs"; | 194 const char kMaxTabs[] = "tabs.max_tabs"; |
182 const char kLongIntPref[] = "long_int.pref"; | 195 const char kLongIntPref[] = "long_int.pref"; |
183 | 196 |
184 std::string cnn("http://www.cnn.com"); | 197 std::string cnn("http://www.cnn.com"); |
185 | 198 |
186 const Value* actual; | 199 const Value* actual; |
187 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); | 200 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual)); |
188 std::string string_value; | 201 std::string string_value; |
189 EXPECT_TRUE(actual->GetAsString(&string_value)); | 202 EXPECT_TRUE(actual->GetAsString(&string_value)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 kLongIntPref, | 244 kLongIntPref, |
232 make_scoped_ptr(new StringValue(base::Int64ToString(214748364842LL))), | 245 make_scoped_ptr(new StringValue(base::Int64ToString(214748364842LL))), |
233 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 246 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
234 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 247 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
235 EXPECT_TRUE(actual->GetAsString(&string_value)); | 248 EXPECT_TRUE(actual->GetAsString(&string_value)); |
236 int64_t value; | 249 int64_t value; |
237 base::StringToInt64(string_value, &value); | 250 base::StringToInt64(string_value, &value); |
238 EXPECT_EQ(214748364842LL, value); | 251 EXPECT_EQ(214748364842LL, value); |
239 | 252 |
240 // Serialize and compare to expected output. | 253 // Serialize and compare to expected output. |
241 ASSERT_TRUE(PathExists(golden_output_file)); | |
242 pref_store->CommitPendingWrite(); | 254 pref_store->CommitPendingWrite(); |
243 RunLoop().RunUntilIdle(); | 255 RunLoop().RunUntilIdle(); |
244 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file)); | 256 |
| 257 std::string output_contents; |
| 258 ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); |
| 259 EXPECT_EQ(kWriteGolden, output_contents); |
245 ASSERT_TRUE(base::DeleteFile(output_file, false)); | 260 ASSERT_TRUE(base::DeleteFile(output_file, false)); |
246 } | 261 } |
247 | 262 |
248 TEST_F(JsonPrefStoreTest, Basic) { | 263 TEST_F(JsonPrefStoreTest, Basic) { |
249 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 264 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
250 temp_dir_.path().AppendASCII("write.json"))); | 265 ASSERT_LT(0, base::WriteFile(input_file, |
| 266 kReadJson, arraysize(kReadJson) - 1)); |
251 | 267 |
252 // Test that the persistent value can be loaded. | 268 // Test that the persistent value can be loaded. |
253 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | |
254 ASSERT_TRUE(PathExists(input_file)); | 269 ASSERT_TRUE(PathExists(input_file)); |
255 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 270 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
256 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 271 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
257 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 272 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
258 EXPECT_FALSE(pref_store->ReadOnly()); | 273 EXPECT_FALSE(pref_store->ReadOnly()); |
259 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 274 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
260 | 275 |
261 // The JSON file looks like this: | 276 // The JSON file looks like this: |
262 // { | 277 // { |
263 // "homepage": "http://www.cnn.com", | 278 // "homepage": "http://www.cnn.com", |
264 // "some_directory": "/usr/local/", | 279 // "some_directory": "/usr/local/", |
265 // "tabs": { | 280 // "tabs": { |
266 // "new_windows_in_tabs": true, | 281 // "new_windows_in_tabs": true, |
267 // "max_tabs": 20 | 282 // "max_tabs": 20 |
268 // } | 283 // } |
269 // } | 284 // } |
270 | 285 |
271 RunBasicJsonPrefStoreTest( | 286 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
272 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
273 } | 287 } |
274 | 288 |
275 TEST_F(JsonPrefStoreTest, BasicAsync) { | 289 TEST_F(JsonPrefStoreTest, BasicAsync) { |
276 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | 290 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
277 temp_dir_.path().AppendASCII("write.json"))); | 291 ASSERT_LT(0, base::WriteFile(input_file, |
| 292 kReadJson, arraysize(kReadJson) - 1)); |
278 | 293 |
279 // Test that the persistent value can be loaded. | 294 // Test that the persistent value can be loaded. |
280 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | |
281 ASSERT_TRUE(PathExists(input_file)); | |
282 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 295 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
283 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 296 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
284 | 297 |
285 { | 298 { |
286 MockPrefStoreObserver mock_observer; | 299 MockPrefStoreObserver mock_observer; |
287 pref_store->AddObserver(&mock_observer); | 300 pref_store->AddObserver(&mock_observer); |
288 | 301 |
289 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 302 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
290 pref_store->ReadPrefsAsync(mock_error_delegate); | 303 pref_store->ReadPrefsAsync(mock_error_delegate); |
291 | 304 |
(...skipping 10 matching lines...) Expand all Loading... |
302 // The JSON file looks like this: | 315 // The JSON file looks like this: |
303 // { | 316 // { |
304 // "homepage": "http://www.cnn.com", | 317 // "homepage": "http://www.cnn.com", |
305 // "some_directory": "/usr/local/", | 318 // "some_directory": "/usr/local/", |
306 // "tabs": { | 319 // "tabs": { |
307 // "new_windows_in_tabs": true, | 320 // "new_windows_in_tabs": true, |
308 // "max_tabs": 20 | 321 // "max_tabs": 20 |
309 // } | 322 // } |
310 // } | 323 // } |
311 | 324 |
312 RunBasicJsonPrefStoreTest( | 325 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
313 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
314 } | 326 } |
315 | 327 |
316 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 328 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
317 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 329 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
318 | 330 |
319 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 331 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
320 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 332 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
321 | 333 |
322 // Set some keys with empty values. | 334 // Set some keys with empty values. |
323 pref_store->SetValue("list", make_scoped_ptr(new base::ListValue), | 335 pref_store->SetValue("list", make_scoped_ptr(new base::ListValue), |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 391 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
380 EXPECT_CALL(*mock_error_delegate, | 392 EXPECT_CALL(*mock_error_delegate, |
381 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 393 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
382 RunLoop().RunUntilIdle(); | 394 RunLoop().RunUntilIdle(); |
383 pref_store->RemoveObserver(&mock_observer); | 395 pref_store->RemoveObserver(&mock_observer); |
384 | 396 |
385 EXPECT_FALSE(pref_store->ReadOnly()); | 397 EXPECT_FALSE(pref_store->ReadOnly()); |
386 } | 398 } |
387 | 399 |
388 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 400 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
389 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | |
390 temp_dir_.path().AppendASCII("write.json"))); | |
391 | |
392 // Test that the persistent value can be loaded. | |
393 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 401 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
394 ASSERT_TRUE(PathExists(input_file)); | 402 ASSERT_LT(0, base::WriteFile(input_file, |
| 403 kReadJson, arraysize(kReadJson) - 1)); |
395 | 404 |
396 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 405 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
397 new InterceptingPrefFilter()); | 406 new InterceptingPrefFilter()); |
398 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 407 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
399 intercepting_pref_filter.get(); | 408 intercepting_pref_filter.get(); |
400 scoped_refptr<JsonPrefStore> pref_store = | 409 scoped_refptr<JsonPrefStore> pref_store = |
401 new JsonPrefStore(input_file, message_loop_.task_runner(), | 410 new JsonPrefStore(input_file, message_loop_.task_runner(), |
402 std::move(intercepting_pref_filter)); | 411 std::move(intercepting_pref_filter)); |
403 | 412 |
404 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 413 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
(...skipping 15 matching lines...) Expand all Loading... |
420 // The JSON file looks like this: | 429 // The JSON file looks like this: |
421 // { | 430 // { |
422 // "homepage": "http://www.cnn.com", | 431 // "homepage": "http://www.cnn.com", |
423 // "some_directory": "/usr/local/", | 432 // "some_directory": "/usr/local/", |
424 // "tabs": { | 433 // "tabs": { |
425 // "new_windows_in_tabs": true, | 434 // "new_windows_in_tabs": true, |
426 // "max_tabs": 20 | 435 // "max_tabs": 20 |
427 // } | 436 // } |
428 // } | 437 // } |
429 | 438 |
430 RunBasicJsonPrefStoreTest( | 439 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
431 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
432 } | 440 } |
433 | 441 |
434 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { | 442 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
435 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), | |
436 temp_dir_.path().AppendASCII("write.json"))); | |
437 | |
438 // Test that the persistent value can be loaded. | |
439 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 443 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
440 ASSERT_TRUE(PathExists(input_file)); | 444 ASSERT_LT(0, base::WriteFile(input_file, |
| 445 kReadJson, arraysize(kReadJson) - 1)); |
441 | 446 |
442 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 447 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
443 new InterceptingPrefFilter()); | 448 new InterceptingPrefFilter()); |
444 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 449 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
445 intercepting_pref_filter.get(); | 450 intercepting_pref_filter.get(); |
446 scoped_refptr<JsonPrefStore> pref_store = | 451 scoped_refptr<JsonPrefStore> pref_store = |
447 new JsonPrefStore(input_file, message_loop_.task_runner(), | 452 new JsonPrefStore(input_file, message_loop_.task_runner(), |
448 std::move(intercepting_pref_filter)); | 453 std::move(intercepting_pref_filter)); |
449 | 454 |
450 MockPrefStoreObserver mock_observer; | 455 MockPrefStoreObserver mock_observer; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // The JSON file looks like this: | 490 // The JSON file looks like this: |
486 // { | 491 // { |
487 // "homepage": "http://www.cnn.com", | 492 // "homepage": "http://www.cnn.com", |
488 // "some_directory": "/usr/local/", | 493 // "some_directory": "/usr/local/", |
489 // "tabs": { | 494 // "tabs": { |
490 // "new_windows_in_tabs": true, | 495 // "new_windows_in_tabs": true, |
491 // "max_tabs": 20 | 496 // "max_tabs": 20 |
492 // } | 497 // } |
493 // } | 498 // } |
494 | 499 |
495 RunBasicJsonPrefStoreTest( | 500 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
496 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
497 } | 501 } |
498 | 502 |
499 TEST_F(JsonPrefStoreTest, AlternateFile) { | 503 TEST_F(JsonPrefStoreTest, AlternateFile) { |
500 ASSERT_TRUE( | 504 base::FilePath alternate_input_file = |
501 base::CopyFile(data_dir_.AppendASCII("read.json"), | 505 temp_dir_.path().AppendASCII("alternate.json"); |
502 temp_dir_.path().AppendASCII("alternate.json"))); | 506 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 507 kReadJson, arraysize(kReadJson) - 1)); |
503 | 508 |
504 // Test that the alternate file is moved to the main file and read as-is from | 509 // Test that the alternate file is moved to the main file and read as-is from |
505 // there. | 510 // there. |
506 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 511 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
507 base::FilePath alternate_input_file = | |
508 temp_dir_.path().AppendASCII("alternate.json"); | |
509 ASSERT_FALSE(PathExists(input_file)); | 512 ASSERT_FALSE(PathExists(input_file)); |
510 ASSERT_TRUE(PathExists(alternate_input_file)); | 513 ASSERT_TRUE(PathExists(alternate_input_file)); |
511 scoped_refptr<JsonPrefStore> pref_store = | 514 scoped_refptr<JsonPrefStore> pref_store = |
512 new JsonPrefStore(input_file, alternate_input_file, | 515 new JsonPrefStore(input_file, alternate_input_file, |
513 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 516 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
514 | 517 |
515 ASSERT_FALSE(PathExists(input_file)); | 518 ASSERT_FALSE(PathExists(input_file)); |
516 ASSERT_TRUE(PathExists(alternate_input_file)); | 519 ASSERT_TRUE(PathExists(alternate_input_file)); |
517 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 520 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
518 | 521 |
519 ASSERT_TRUE(PathExists(input_file)); | 522 ASSERT_TRUE(PathExists(input_file)); |
520 ASSERT_FALSE(PathExists(alternate_input_file)); | 523 ASSERT_FALSE(PathExists(alternate_input_file)); |
521 | 524 |
522 EXPECT_FALSE(pref_store->ReadOnly()); | 525 EXPECT_FALSE(pref_store->ReadOnly()); |
523 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 526 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
524 | 527 |
525 // The JSON file looks like this: | 528 // The JSON file looks like this: |
526 // { | 529 // { |
527 // "homepage": "http://www.cnn.com", | 530 // "homepage": "http://www.cnn.com", |
528 // "some_directory": "/usr/local/", | 531 // "some_directory": "/usr/local/", |
529 // "tabs": { | 532 // "tabs": { |
530 // "new_windows_in_tabs": true, | 533 // "new_windows_in_tabs": true, |
531 // "max_tabs": 20 | 534 // "max_tabs": 20 |
532 // } | 535 // } |
533 // } | 536 // } |
534 | 537 |
535 RunBasicJsonPrefStoreTest( | 538 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
536 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
537 } | 539 } |
538 | 540 |
539 TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { | 541 TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) { |
540 ASSERT_TRUE( | 542 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
541 base::CopyFile(data_dir_.AppendASCII("read.json"), | 543 ASSERT_LT(0, base::WriteFile(input_file, |
542 temp_dir_.path().AppendASCII("write.json"))); | 544 kReadJson, arraysize(kReadJson) - 1)); |
543 ASSERT_TRUE( | 545 |
544 base::CopyFile(data_dir_.AppendASCII("invalid.json"), | 546 base::FilePath alternate_input_file = |
545 temp_dir_.path().AppendASCII("alternate.json"))); | 547 temp_dir_.path().AppendASCII("alternate.json"); |
| 548 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 549 kInvalidJson, arraysize(kInvalidJson) - 1)); |
546 | 550 |
547 // Test that the alternate file is ignored and that the read occurs from the | 551 // Test that the alternate file is ignored and that the read occurs from the |
548 // existing main file. There is no attempt at even deleting the alternate | 552 // existing main file. There is no attempt at even deleting the alternate |
549 // file as this scenario should never happen in normal user-data-dirs. | 553 // file as this scenario should never happen in normal user-data-dirs. |
550 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | |
551 base::FilePath alternate_input_file = | |
552 temp_dir_.path().AppendASCII("alternate.json"); | |
553 ASSERT_TRUE(PathExists(input_file)); | |
554 ASSERT_TRUE(PathExists(alternate_input_file)); | |
555 scoped_refptr<JsonPrefStore> pref_store = | 554 scoped_refptr<JsonPrefStore> pref_store = |
556 new JsonPrefStore(input_file, alternate_input_file, | 555 new JsonPrefStore(input_file, alternate_input_file, |
557 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 556 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
558 | 557 |
559 ASSERT_TRUE(PathExists(input_file)); | 558 ASSERT_TRUE(PathExists(input_file)); |
560 ASSERT_TRUE(PathExists(alternate_input_file)); | 559 ASSERT_TRUE(PathExists(alternate_input_file)); |
561 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 560 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
562 | 561 |
563 ASSERT_TRUE(PathExists(input_file)); | 562 ASSERT_TRUE(PathExists(input_file)); |
564 ASSERT_TRUE(PathExists(alternate_input_file)); | 563 ASSERT_TRUE(PathExists(alternate_input_file)); |
565 | 564 |
566 EXPECT_FALSE(pref_store->ReadOnly()); | 565 EXPECT_FALSE(pref_store->ReadOnly()); |
567 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 566 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
568 | 567 |
569 // The JSON file looks like this: | 568 // The JSON file looks like this: |
570 // { | 569 // { |
571 // "homepage": "http://www.cnn.com", | 570 // "homepage": "http://www.cnn.com", |
572 // "some_directory": "/usr/local/", | 571 // "some_directory": "/usr/local/", |
573 // "tabs": { | 572 // "tabs": { |
574 // "new_windows_in_tabs": true, | 573 // "new_windows_in_tabs": true, |
575 // "max_tabs": 20 | 574 // "max_tabs": 20 |
576 // } | 575 // } |
577 // } | 576 // } |
578 | 577 |
579 RunBasicJsonPrefStoreTest( | 578 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
580 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
581 } | 579 } |
582 | 580 |
583 TEST_F(JsonPrefStoreTest, AlternateFileDNE) { | 581 TEST_F(JsonPrefStoreTest, AlternateFileDNE) { |
584 ASSERT_TRUE( | 582 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
585 base::CopyFile(data_dir_.AppendASCII("read.json"), | 583 ASSERT_LT(0, base::WriteFile(input_file, |
586 temp_dir_.path().AppendASCII("write.json"))); | 584 kReadJson, arraysize(kReadJson) - 1)); |
587 | 585 |
588 // Test that the basic read works fine when an alternate file is specified but | 586 // Test that the basic read works fine when an alternate file is specified but |
589 // does not exist. | 587 // does not exist. |
590 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | |
591 base::FilePath alternate_input_file = | 588 base::FilePath alternate_input_file = |
592 temp_dir_.path().AppendASCII("alternate.json"); | 589 temp_dir_.path().AppendASCII("alternate.json"); |
593 ASSERT_TRUE(PathExists(input_file)); | 590 ASSERT_TRUE(PathExists(input_file)); |
594 ASSERT_FALSE(PathExists(alternate_input_file)); | 591 ASSERT_FALSE(PathExists(alternate_input_file)); |
595 scoped_refptr<JsonPrefStore> pref_store = | 592 scoped_refptr<JsonPrefStore> pref_store = |
596 new JsonPrefStore(input_file, alternate_input_file, | 593 new JsonPrefStore(input_file, alternate_input_file, |
597 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 594 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
598 | 595 |
599 ASSERT_TRUE(PathExists(input_file)); | 596 ASSERT_TRUE(PathExists(input_file)); |
600 ASSERT_FALSE(PathExists(alternate_input_file)); | 597 ASSERT_FALSE(PathExists(alternate_input_file)); |
601 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 598 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
602 | 599 |
603 ASSERT_TRUE(PathExists(input_file)); | 600 ASSERT_TRUE(PathExists(input_file)); |
604 ASSERT_FALSE(PathExists(alternate_input_file)); | 601 ASSERT_FALSE(PathExists(alternate_input_file)); |
605 | 602 |
606 EXPECT_FALSE(pref_store->ReadOnly()); | 603 EXPECT_FALSE(pref_store->ReadOnly()); |
607 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 604 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
608 | 605 |
609 // The JSON file looks like this: | 606 // The JSON file looks like this: |
610 // { | 607 // { |
611 // "homepage": "http://www.cnn.com", | 608 // "homepage": "http://www.cnn.com", |
612 // "some_directory": "/usr/local/", | 609 // "some_directory": "/usr/local/", |
613 // "tabs": { | 610 // "tabs": { |
614 // "new_windows_in_tabs": true, | 611 // "new_windows_in_tabs": true, |
615 // "max_tabs": 20 | 612 // "max_tabs": 20 |
616 // } | 613 // } |
617 // } | 614 // } |
618 | 615 |
619 RunBasicJsonPrefStoreTest( | 616 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
620 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
621 } | 617 } |
622 | 618 |
623 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { | 619 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { |
624 ASSERT_TRUE( | 620 base::FilePath alternate_input_file = |
625 base::CopyFile(data_dir_.AppendASCII("read.json"), | 621 temp_dir_.path().AppendASCII("alternate.json"); |
626 temp_dir_.path().AppendASCII("alternate.json"))); | 622 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
| 623 kReadJson, arraysize(kReadJson) - 1)); |
627 | 624 |
628 // Test that the alternate file is moved to the main file and read as-is from | 625 // Test that the alternate file is moved to the main file and read as-is from |
629 // there even when the read is made asynchronously. | 626 // there even when the read is made asynchronously. |
630 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 627 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
631 base::FilePath alternate_input_file = | |
632 temp_dir_.path().AppendASCII("alternate.json"); | |
633 ASSERT_FALSE(PathExists(input_file)); | |
634 ASSERT_TRUE(PathExists(alternate_input_file)); | |
635 scoped_refptr<JsonPrefStore> pref_store = | 628 scoped_refptr<JsonPrefStore> pref_store = |
636 new JsonPrefStore(input_file, alternate_input_file, | 629 new JsonPrefStore(input_file, alternate_input_file, |
637 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 630 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); |
638 | 631 |
639 ASSERT_FALSE(PathExists(input_file)); | 632 ASSERT_FALSE(PathExists(input_file)); |
640 ASSERT_TRUE(PathExists(alternate_input_file)); | 633 ASSERT_TRUE(PathExists(alternate_input_file)); |
641 | 634 |
642 { | 635 { |
643 MockPrefStoreObserver mock_observer; | 636 MockPrefStoreObserver mock_observer; |
644 pref_store->AddObserver(&mock_observer); | 637 pref_store->AddObserver(&mock_observer); |
(...skipping 17 matching lines...) Expand all Loading... |
662 // The JSON file looks like this: | 655 // The JSON file looks like this: |
663 // { | 656 // { |
664 // "homepage": "http://www.cnn.com", | 657 // "homepage": "http://www.cnn.com", |
665 // "some_directory": "/usr/local/", | 658 // "some_directory": "/usr/local/", |
666 // "tabs": { | 659 // "tabs": { |
667 // "new_windows_in_tabs": true, | 660 // "new_windows_in_tabs": true, |
668 // "max_tabs": 20 | 661 // "max_tabs": 20 |
669 // } | 662 // } |
670 // } | 663 // } |
671 | 664 |
672 RunBasicJsonPrefStoreTest( | 665 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
673 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json")); | |
674 } | 666 } |
675 | 667 |
676 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { | 668 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
| 669 base::HistogramTester histogram_tester; |
| 670 |
677 SimpleTestClock* test_clock = new SimpleTestClock; | 671 SimpleTestClock* test_clock = new SimpleTestClock; |
678 SetCurrentTimeInMinutes(0, test_clock); | 672 SetCurrentTimeInMinutes(0, test_clock); |
679 JsonPrefStore::WriteCountHistogram histogram( | 673 JsonPrefStore::WriteCountHistogram histogram( |
680 base::TimeDelta::FromSeconds(10), | 674 base::TimeDelta::FromSeconds(10), |
681 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 675 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
682 scoped_ptr<base::Clock>(test_clock)); | 676 scoped_ptr<base::Clock>(test_clock)); |
683 int32_t report_interval = | 677 int32_t report_interval = |
684 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 678 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
685 | 679 |
686 histogram.RecordWriteOccured(); | 680 histogram.RecordWriteOccured(); |
687 | 681 |
688 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); | 682 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
689 histogram.ReportOutstandingWrites(); | 683 histogram.ReportOutstandingWrites(); |
690 scoped_ptr<HistogramSamples> samples = | 684 scoped_ptr<HistogramSamples> samples = |
691 histogram.GetHistogram()->SnapshotSamples(); | 685 histogram.GetHistogram()->SnapshotSamples(); |
692 ASSERT_EQ(1, samples->GetCount(1)); | 686 |
693 ASSERT_EQ(1, samples->TotalCount()); | 687 std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
| 688 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
| 689 histogram_tester.ExpectTotalCount(histogram_name, 1); |
694 | 690 |
695 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", | 691 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
696 histogram.GetHistogram()->histogram_name()); | 692 histogram.GetHistogram()->histogram_name()); |
697 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); | 693 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
698 } | 694 } |
699 | 695 |
700 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { | 696 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
| 697 base::HistogramTester histogram_tester; |
| 698 |
701 SimpleTestClock* test_clock = new SimpleTestClock; | 699 SimpleTestClock* test_clock = new SimpleTestClock; |
702 SetCurrentTimeInMinutes(0, test_clock); | 700 SetCurrentTimeInMinutes(0, test_clock); |
703 JsonPrefStore::WriteCountHistogram histogram( | 701 JsonPrefStore::WriteCountHistogram histogram( |
704 base::TimeDelta::FromSeconds(10), | 702 base::TimeDelta::FromSeconds(10), |
705 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 703 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
706 scoped_ptr<base::Clock>(test_clock)); | 704 scoped_ptr<base::Clock>(test_clock)); |
707 int32_t report_interval = | 705 int32_t report_interval = |
708 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 706 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
709 | 707 |
710 histogram.RecordWriteOccured(); | 708 histogram.RecordWriteOccured(); |
711 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); | 709 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
712 histogram.RecordWriteOccured(); | 710 histogram.RecordWriteOccured(); |
713 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); | 711 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
714 histogram.RecordWriteOccured(); | 712 histogram.RecordWriteOccured(); |
715 | 713 |
716 // Nothing should be recorded until the report period has elapsed. | 714 // Nothing should be recorded until the report period has elapsed. |
717 scoped_ptr<HistogramSamples> samples = | 715 std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
718 histogram.GetHistogram()->SnapshotSamples(); | 716 histogram_tester.ExpectTotalCount(histogram_name, 0); |
719 ASSERT_EQ(0, samples->TotalCount()); | |
720 | 717 |
721 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); | 718 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
722 histogram.RecordWriteOccured(); | 719 histogram.RecordWriteOccured(); |
723 | 720 |
724 // Now the report period has elapsed. | 721 // Now the report period has elapsed. |
725 samples = histogram.GetHistogram()->SnapshotSamples(); | 722 histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
726 ASSERT_EQ(1, samples->GetCount(3)); | 723 histogram_tester.ExpectTotalCount(histogram_name, 1); |
727 ASSERT_EQ(1, samples->TotalCount()); | |
728 | 724 |
729 // The last write won't be recorded because the second count period hasn't | 725 // The last write won't be recorded because the second count period hasn't |
730 // fully elapsed. | 726 // fully elapsed. |
731 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); | 727 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
732 histogram.ReportOutstandingWrites(); | 728 histogram.ReportOutstandingWrites(); |
733 | 729 |
734 samples = histogram.GetHistogram()->SnapshotSamples(); | 730 histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
735 ASSERT_EQ(1, samples->GetCount(3)); | 731 histogram_tester.ExpectTotalCount(histogram_name, 1); |
736 ASSERT_EQ(1, samples->TotalCount()); | |
737 } | 732 } |
738 | 733 |
739 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { | 734 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
| 735 base::HistogramTester histogram_tester; |
| 736 |
740 SimpleTestClock* test_clock = new SimpleTestClock; | 737 SimpleTestClock* test_clock = new SimpleTestClock; |
741 SetCurrentTimeInMinutes(0, test_clock); | 738 SetCurrentTimeInMinutes(0, test_clock); |
742 JsonPrefStore::WriteCountHistogram histogram( | 739 JsonPrefStore::WriteCountHistogram histogram( |
743 base::TimeDelta::FromSeconds(10), | 740 base::TimeDelta::FromSeconds(10), |
744 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 741 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
745 scoped_ptr<base::Clock>(test_clock)); | 742 scoped_ptr<base::Clock>(test_clock)); |
746 int32_t report_interval = | 743 int32_t report_interval = |
747 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 744 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
748 | 745 |
749 histogram.RecordWriteOccured(); | 746 histogram.RecordWriteOccured(); |
(...skipping 11 matching lines...) Expand all Loading... |
761 histogram.RecordWriteOccured(); | 758 histogram.RecordWriteOccured(); |
762 SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); | 759 SetCurrentTimeInMinutes(2.7 * report_interval, test_clock); |
763 histogram.RecordWriteOccured(); | 760 histogram.RecordWriteOccured(); |
764 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); | 761 SetCurrentTimeInMinutes(3.3 * report_interval, test_clock); |
765 histogram.RecordWriteOccured(); | 762 histogram.RecordWriteOccured(); |
766 | 763 |
767 // The last write won't be recorded because the second count period hasn't | 764 // The last write won't be recorded because the second count period hasn't |
768 // fully elapsed | 765 // fully elapsed |
769 SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); | 766 SetCurrentTimeInMinutes(3.5 * report_interval, test_clock); |
770 histogram.ReportOutstandingWrites(); | 767 histogram.ReportOutstandingWrites(); |
771 scoped_ptr<HistogramSamples> samples = | 768 std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
772 histogram.GetHistogram()->SnapshotSamples(); | 769 histogram_tester.ExpectBucketCount(histogram_name, 3, 2); |
773 ASSERT_EQ(2, samples->GetCount(3)); | 770 histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
774 ASSERT_EQ(1, samples->GetCount(2)); | 771 histogram_tester.ExpectTotalCount(histogram_name, 3); |
775 ASSERT_EQ(3, samples->TotalCount()); | |
776 } | 772 } |
777 | 773 |
778 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { | 774 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
| 775 base::HistogramTester histogram_tester; |
| 776 |
779 SimpleTestClock* test_clock = new SimpleTestClock; | 777 SimpleTestClock* test_clock = new SimpleTestClock; |
780 SetCurrentTimeInMinutes(0, test_clock); | 778 SetCurrentTimeInMinutes(0, test_clock); |
781 JsonPrefStore::WriteCountHistogram histogram( | 779 JsonPrefStore::WriteCountHistogram histogram( |
782 base::TimeDelta::FromSeconds(10), | 780 base::TimeDelta::FromSeconds(10), |
783 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 781 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
784 scoped_ptr<base::Clock>(test_clock)); | 782 scoped_ptr<base::Clock>(test_clock)); |
785 int32_t report_interval = | 783 int32_t report_interval = |
786 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 784 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
787 | 785 |
788 // 1 write in the first period. | 786 // 1 write in the first period. |
(...skipping 12 matching lines...) Expand all Loading... |
801 // 3 writes in the sixth period. | 799 // 3 writes in the sixth period. |
802 SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); | 800 SetCurrentTimeInMinutes(5.1 * report_interval, test_clock); |
803 histogram.RecordWriteOccured(); | 801 histogram.RecordWriteOccured(); |
804 SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); | 802 SetCurrentTimeInMinutes(5.3 * report_interval, test_clock); |
805 histogram.RecordWriteOccured(); | 803 histogram.RecordWriteOccured(); |
806 SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); | 804 SetCurrentTimeInMinutes(5.5 * report_interval, test_clock); |
807 histogram.RecordWriteOccured(); | 805 histogram.RecordWriteOccured(); |
808 | 806 |
809 SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); | 807 SetCurrentTimeInMinutes(6.1 * report_interval, test_clock); |
810 histogram.ReportOutstandingWrites(); | 808 histogram.ReportOutstandingWrites(); |
811 scoped_ptr<HistogramSamples> samples = | 809 std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
812 histogram.GetHistogram()->SnapshotSamples(); | 810 histogram_tester.ExpectBucketCount(histogram_name, 0, 3); |
813 ASSERT_EQ(3, samples->GetCount(0)); | 811 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
814 ASSERT_EQ(1, samples->GetCount(1)); | 812 histogram_tester.ExpectBucketCount(histogram_name, 2, 1); |
815 ASSERT_EQ(1, samples->GetCount(2)); | 813 histogram_tester.ExpectBucketCount(histogram_name, 3, 1); |
816 ASSERT_EQ(1, samples->GetCount(3)); | 814 histogram_tester.ExpectTotalCount(histogram_name, 6); |
817 ASSERT_EQ(6, samples->TotalCount()); | |
818 } | 815 } |
819 | 816 |
820 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | 817 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
821 protected: | 818 protected: |
822 void SetUp() override { | 819 void SetUp() override { |
823 JsonPrefStoreTest::SetUp(); | 820 JsonPrefStoreTest::SetUp(); |
824 test_file_ = temp_dir_.path().AppendASCII("test.json"); | 821 test_file_ = temp_dir_.path().AppendASCII("test.json"); |
825 } | 822 } |
826 | 823 |
827 // Creates a JsonPrefStore with the given |file_writer|. | 824 // Creates a JsonPrefStore with the given |file_writer|. |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
956 ASSERT_TRUE(file_writer->HasPendingWrite()); | 953 ASSERT_TRUE(file_writer->HasPendingWrite()); |
957 | 954 |
958 // Call CommitPendingWrite and check that the lossy pref is there with the | 955 // Call CommitPendingWrite and check that the lossy pref is there with the |
959 // last value set above. | 956 // last value set above. |
960 pref_store->CommitPendingWrite(); | 957 pref_store->CommitPendingWrite(); |
961 ASSERT_FALSE(file_writer->HasPendingWrite()); | 958 ASSERT_FALSE(file_writer->HasPendingWrite()); |
962 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 959 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
963 } | 960 } |
964 | 961 |
965 } // namespace base | 962 } // namespace base |
OLD | NEW |