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 "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
| 9 #include <memory> |
9 #include <utility> | 10 #include <utility> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
14 #include "base/location.h" | 15 #include "base/location.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
19 #include "base/metrics/histogram_samples.h" | 20 #include "base/metrics/histogram_samples.h" |
20 #include "base/path_service.h" | 21 #include "base/path_service.h" |
21 #include "base/run_loop.h" | 22 #include "base/run_loop.h" |
22 #include "base/single_thread_task_runner.h" | 23 #include "base/single_thread_task_runner.h" |
23 #include "base/strings/string_number_conversions.h" | 24 #include "base/strings/string_number_conversions.h" |
24 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
25 #include "base/strings/utf_string_conversions.h" | 26 #include "base/strings/utf_string_conversions.h" |
26 #include "base/test/histogram_tester.h" | 27 #include "base/test/histogram_tester.h" |
27 #include "base/test/simple_test_clock.h" | 28 #include "base/test/simple_test_clock.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on | 66 // A PrefFilter that will intercept all calls to FilterOnLoad() and hold on |
66 // to the |prefs| until explicitly asked to release them. | 67 // to the |prefs| until explicitly asked to release them. |
67 class InterceptingPrefFilter : public PrefFilter { | 68 class InterceptingPrefFilter : public PrefFilter { |
68 public: | 69 public: |
69 InterceptingPrefFilter(); | 70 InterceptingPrefFilter(); |
70 ~InterceptingPrefFilter() override; | 71 ~InterceptingPrefFilter() override; |
71 | 72 |
72 // PrefFilter implementation: | 73 // PrefFilter implementation: |
73 void FilterOnLoad( | 74 void FilterOnLoad( |
74 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 75 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
75 scoped_ptr<base::DictionaryValue> pref_store_contents) override; | 76 std::unique_ptr<base::DictionaryValue> pref_store_contents) override; |
76 void FilterUpdate(const std::string& path) override {} | 77 void FilterUpdate(const std::string& path) override {} |
77 void FilterSerializeData( | 78 void FilterSerializeData( |
78 base::DictionaryValue* pref_store_contents) override {} | 79 base::DictionaryValue* pref_store_contents) override {} |
79 | 80 |
80 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } | 81 bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; } |
81 | 82 |
82 // Finalize an intercepted read, handing |intercepted_prefs_| back to its | 83 // Finalize an intercepted read, handing |intercepted_prefs_| back to its |
83 // JsonPrefStore. | 84 // JsonPrefStore. |
84 void ReleasePrefs(); | 85 void ReleasePrefs(); |
85 | 86 |
86 private: | 87 private: |
87 PostFilterOnLoadCallback post_filter_on_load_callback_; | 88 PostFilterOnLoadCallback post_filter_on_load_callback_; |
88 scoped_ptr<base::DictionaryValue> intercepted_prefs_; | 89 std::unique_ptr<base::DictionaryValue> intercepted_prefs_; |
89 | 90 |
90 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); | 91 DISALLOW_COPY_AND_ASSIGN(InterceptingPrefFilter); |
91 }; | 92 }; |
92 | 93 |
93 InterceptingPrefFilter::InterceptingPrefFilter() {} | 94 InterceptingPrefFilter::InterceptingPrefFilter() {} |
94 InterceptingPrefFilter::~InterceptingPrefFilter() {} | 95 InterceptingPrefFilter::~InterceptingPrefFilter() {} |
95 | 96 |
96 void InterceptingPrefFilter::FilterOnLoad( | 97 void InterceptingPrefFilter::FilterOnLoad( |
97 const PostFilterOnLoadCallback& post_filter_on_load_callback, | 98 const PostFilterOnLoadCallback& post_filter_on_load_callback, |
98 scoped_ptr<base::DictionaryValue> pref_store_contents) { | 99 std::unique_ptr<base::DictionaryValue> pref_store_contents) { |
99 post_filter_on_load_callback_ = post_filter_on_load_callback; | 100 post_filter_on_load_callback_ = post_filter_on_load_callback; |
100 intercepted_prefs_ = std::move(pref_store_contents); | 101 intercepted_prefs_ = std::move(pref_store_contents); |
101 } | 102 } |
102 | 103 |
103 void InterceptingPrefFilter::ReleasePrefs() { | 104 void InterceptingPrefFilter::ReleasePrefs() { |
104 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); | 105 EXPECT_FALSE(post_filter_on_load_callback_.is_null()); |
105 post_filter_on_load_callback_.Run(std::move(intercepted_prefs_), false); | 106 post_filter_on_load_callback_.Run(std::move(intercepted_prefs_), false); |
106 post_filter_on_load_callback_.Reset(); | 107 post_filter_on_load_callback_.Reset(); |
107 } | 108 } |
108 | 109 |
(...skipping 25 matching lines...) Expand all Loading... |
134 // The path to temporary directory used to contain the test operations. | 135 // The path to temporary directory used to contain the test operations. |
135 base::ScopedTempDir temp_dir_; | 136 base::ScopedTempDir temp_dir_; |
136 // A message loop that we can use as the file thread message loop. | 137 // A message loop that we can use as the file thread message loop. |
137 MessageLoop message_loop_; | 138 MessageLoop message_loop_; |
138 }; | 139 }; |
139 | 140 |
140 // Test fallback behavior for a nonexistent file. | 141 // Test fallback behavior for a nonexistent file. |
141 TEST_F(JsonPrefStoreTest, NonExistentFile) { | 142 TEST_F(JsonPrefStoreTest, NonExistentFile) { |
142 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 143 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
143 ASSERT_FALSE(PathExists(bogus_input_file)); | 144 ASSERT_FALSE(PathExists(bogus_input_file)); |
144 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 145 scoped_refptr<JsonPrefStore> pref_store = |
145 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 146 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
| 147 std::unique_ptr<PrefFilter>()); |
146 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 148 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
147 pref_store->ReadPrefs()); | 149 pref_store->ReadPrefs()); |
148 EXPECT_FALSE(pref_store->ReadOnly()); | 150 EXPECT_FALSE(pref_store->ReadOnly()); |
149 } | 151 } |
150 | 152 |
151 // Test fallback behavior for a nonexistent file and alternate file. | 153 // Test fallback behavior for a nonexistent file and alternate file. |
152 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { | 154 TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) { |
153 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 155 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
154 base::FilePath bogus_alternate_input_file = | 156 base::FilePath bogus_alternate_input_file = |
155 temp_dir_.path().AppendASCII("read_alternate.txt"); | 157 temp_dir_.path().AppendASCII("read_alternate.txt"); |
156 ASSERT_FALSE(PathExists(bogus_input_file)); | 158 ASSERT_FALSE(PathExists(bogus_input_file)); |
157 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); | 159 ASSERT_FALSE(PathExists(bogus_alternate_input_file)); |
158 scoped_refptr<JsonPrefStore> pref_store = | 160 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
159 new JsonPrefStore(bogus_input_file, bogus_alternate_input_file, | 161 bogus_input_file, bogus_alternate_input_file, message_loop_.task_runner(), |
160 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 162 std::unique_ptr<PrefFilter>()); |
161 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, | 163 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
162 pref_store->ReadPrefs()); | 164 pref_store->ReadPrefs()); |
163 EXPECT_FALSE(pref_store->ReadOnly()); | 165 EXPECT_FALSE(pref_store->ReadOnly()); |
164 } | 166 } |
165 | 167 |
166 // Test fallback behavior for an invalid file. | 168 // Test fallback behavior for an invalid file. |
167 TEST_F(JsonPrefStoreTest, InvalidFile) { | 169 TEST_F(JsonPrefStoreTest, InvalidFile) { |
168 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); | 170 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); |
169 ASSERT_LT(0, base::WriteFile(invalid_file, | 171 ASSERT_LT(0, base::WriteFile(invalid_file, |
170 kInvalidJson, arraysize(kInvalidJson) - 1)); | 172 kInvalidJson, arraysize(kInvalidJson) - 1)); |
171 | 173 |
172 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 174 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
173 invalid_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 175 invalid_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
174 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, | 176 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
175 pref_store->ReadPrefs()); | 177 pref_store->ReadPrefs()); |
176 EXPECT_FALSE(pref_store->ReadOnly()); | 178 EXPECT_FALSE(pref_store->ReadOnly()); |
177 | 179 |
178 // The file should have been moved aside. | 180 // The file should have been moved aside. |
179 EXPECT_FALSE(PathExists(invalid_file)); | 181 EXPECT_FALSE(PathExists(invalid_file)); |
180 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); | 182 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); |
181 EXPECT_TRUE(PathExists(moved_aside)); | 183 EXPECT_TRUE(PathExists(moved_aside)); |
182 | 184 |
183 std::string moved_aside_contents; | 185 std::string moved_aside_contents; |
(...skipping 20 matching lines...) Expand all Loading... |
204 | 206 |
205 const char kSomeDirectory[] = "some_directory"; | 207 const char kSomeDirectory[] = "some_directory"; |
206 | 208 |
207 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 209 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
208 base::FilePath::StringType path; | 210 base::FilePath::StringType path; |
209 EXPECT_TRUE(actual->GetAsString(&path)); | 211 EXPECT_TRUE(actual->GetAsString(&path)); |
210 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); | 212 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
211 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); | 213 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
212 | 214 |
213 pref_store->SetValue(kSomeDirectory, | 215 pref_store->SetValue(kSomeDirectory, |
214 make_scoped_ptr(new StringValue(some_path.value())), | 216 base::WrapUnique(new StringValue(some_path.value())), |
215 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 217 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
216 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); | 218 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual)); |
217 EXPECT_TRUE(actual->GetAsString(&path)); | 219 EXPECT_TRUE(actual->GetAsString(&path)); |
218 EXPECT_EQ(some_path.value(), path); | 220 EXPECT_EQ(some_path.value(), path); |
219 | 221 |
220 // Test reading some other data types from sub-dictionaries. | 222 // Test reading some other data types from sub-dictionaries. |
221 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 223 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
222 bool boolean = false; | 224 bool boolean = false; |
223 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 225 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
224 EXPECT_TRUE(boolean); | 226 EXPECT_TRUE(boolean); |
225 | 227 |
226 pref_store->SetValue(kNewWindowsInTabs, | 228 pref_store->SetValue(kNewWindowsInTabs, |
227 make_scoped_ptr(new FundamentalValue(false)), | 229 base::WrapUnique(new FundamentalValue(false)), |
228 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 230 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
229 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); | 231 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual)); |
230 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); | 232 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
231 EXPECT_FALSE(boolean); | 233 EXPECT_FALSE(boolean); |
232 | 234 |
233 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 235 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
234 int integer = 0; | 236 int integer = 0; |
235 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 237 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
236 EXPECT_EQ(20, integer); | 238 EXPECT_EQ(20, integer); |
237 pref_store->SetValue(kMaxTabs, make_scoped_ptr(new FundamentalValue(10)), | 239 pref_store->SetValue(kMaxTabs, base::WrapUnique(new FundamentalValue(10)), |
238 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 240 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
239 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); | 241 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual)); |
240 EXPECT_TRUE(actual->GetAsInteger(&integer)); | 242 EXPECT_TRUE(actual->GetAsInteger(&integer)); |
241 EXPECT_EQ(10, integer); | 243 EXPECT_EQ(10, integer); |
242 | 244 |
243 pref_store->SetValue( | 245 pref_store->SetValue( |
244 kLongIntPref, | 246 kLongIntPref, |
245 make_scoped_ptr(new StringValue(base::Int64ToString(214748364842LL))), | 247 base::WrapUnique(new StringValue(base::Int64ToString(214748364842LL))), |
246 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 248 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
247 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); | 249 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); |
248 EXPECT_TRUE(actual->GetAsString(&string_value)); | 250 EXPECT_TRUE(actual->GetAsString(&string_value)); |
249 int64_t value; | 251 int64_t value; |
250 base::StringToInt64(string_value, &value); | 252 base::StringToInt64(string_value, &value); |
251 EXPECT_EQ(214748364842LL, value); | 253 EXPECT_EQ(214748364842LL, value); |
252 | 254 |
253 // Serialize and compare to expected output. | 255 // Serialize and compare to expected output. |
254 pref_store->CommitPendingWrite(); | 256 pref_store->CommitPendingWrite(); |
255 RunLoop().RunUntilIdle(); | 257 RunLoop().RunUntilIdle(); |
256 | 258 |
257 std::string output_contents; | 259 std::string output_contents; |
258 ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); | 260 ASSERT_TRUE(base::ReadFileToString(output_file, &output_contents)); |
259 EXPECT_EQ(kWriteGolden, output_contents); | 261 EXPECT_EQ(kWriteGolden, output_contents); |
260 ASSERT_TRUE(base::DeleteFile(output_file, false)); | 262 ASSERT_TRUE(base::DeleteFile(output_file, false)); |
261 } | 263 } |
262 | 264 |
263 TEST_F(JsonPrefStoreTest, Basic) { | 265 TEST_F(JsonPrefStoreTest, Basic) { |
264 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 266 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
265 ASSERT_LT(0, base::WriteFile(input_file, | 267 ASSERT_LT(0, base::WriteFile(input_file, |
266 kReadJson, arraysize(kReadJson) - 1)); | 268 kReadJson, arraysize(kReadJson) - 1)); |
267 | 269 |
268 // Test that the persistent value can be loaded. | 270 // Test that the persistent value can be loaded. |
269 ASSERT_TRUE(PathExists(input_file)); | 271 ASSERT_TRUE(PathExists(input_file)); |
270 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 272 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
271 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 273 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
272 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 274 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
273 EXPECT_FALSE(pref_store->ReadOnly()); | 275 EXPECT_FALSE(pref_store->ReadOnly()); |
274 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 276 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
275 | 277 |
276 // The JSON file looks like this: | 278 // The JSON file looks like this: |
277 // { | 279 // { |
278 // "homepage": "http://www.cnn.com", | 280 // "homepage": "http://www.cnn.com", |
279 // "some_directory": "/usr/local/", | 281 // "some_directory": "/usr/local/", |
280 // "tabs": { | 282 // "tabs": { |
281 // "new_windows_in_tabs": true, | 283 // "new_windows_in_tabs": true, |
282 // "max_tabs": 20 | 284 // "max_tabs": 20 |
283 // } | 285 // } |
284 // } | 286 // } |
285 | 287 |
286 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 288 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
287 } | 289 } |
288 | 290 |
289 TEST_F(JsonPrefStoreTest, BasicAsync) { | 291 TEST_F(JsonPrefStoreTest, BasicAsync) { |
290 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 292 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
291 ASSERT_LT(0, base::WriteFile(input_file, | 293 ASSERT_LT(0, base::WriteFile(input_file, |
292 kReadJson, arraysize(kReadJson) - 1)); | 294 kReadJson, arraysize(kReadJson) - 1)); |
293 | 295 |
294 // Test that the persistent value can be loaded. | 296 // Test that the persistent value can be loaded. |
295 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 297 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
296 input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 298 input_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
297 | 299 |
298 { | 300 { |
299 MockPrefStoreObserver mock_observer; | 301 MockPrefStoreObserver mock_observer; |
300 pref_store->AddObserver(&mock_observer); | 302 pref_store->AddObserver(&mock_observer); |
301 | 303 |
302 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 304 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
303 pref_store->ReadPrefsAsync(mock_error_delegate); | 305 pref_store->ReadPrefsAsync(mock_error_delegate); |
304 | 306 |
305 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 307 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
306 EXPECT_CALL(*mock_error_delegate, | 308 EXPECT_CALL(*mock_error_delegate, |
(...skipping 15 matching lines...) Expand all Loading... |
322 // } | 324 // } |
323 // } | 325 // } |
324 | 326 |
325 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 327 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
326 } | 328 } |
327 | 329 |
328 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { | 330 TEST_F(JsonPrefStoreTest, PreserveEmptyValues) { |
329 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 331 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
330 | 332 |
331 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 333 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
332 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 334 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
333 | 335 |
334 // Set some keys with empty values. | 336 // Set some keys with empty values. |
335 pref_store->SetValue("list", make_scoped_ptr(new base::ListValue), | 337 pref_store->SetValue("list", base::WrapUnique(new base::ListValue), |
336 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 338 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
337 pref_store->SetValue("dict", make_scoped_ptr(new base::DictionaryValue), | 339 pref_store->SetValue("dict", base::WrapUnique(new base::DictionaryValue), |
338 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 340 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
339 | 341 |
340 // Write to file. | 342 // Write to file. |
341 pref_store->CommitPendingWrite(); | 343 pref_store->CommitPendingWrite(); |
342 RunLoop().RunUntilIdle(); | 344 RunLoop().RunUntilIdle(); |
343 | 345 |
344 // Reload. | 346 // Reload. |
345 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), | 347 pref_store = new JsonPrefStore(pref_file, message_loop_.task_runner(), |
346 scoped_ptr<PrefFilter>()); | 348 std::unique_ptr<PrefFilter>()); |
347 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 349 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
348 ASSERT_FALSE(pref_store->ReadOnly()); | 350 ASSERT_FALSE(pref_store->ReadOnly()); |
349 | 351 |
350 // Check values. | 352 // Check values. |
351 const Value* result = NULL; | 353 const Value* result = NULL; |
352 EXPECT_TRUE(pref_store->GetValue("list", &result)); | 354 EXPECT_TRUE(pref_store->GetValue("list", &result)); |
353 EXPECT_TRUE(ListValue().Equals(result)); | 355 EXPECT_TRUE(ListValue().Equals(result)); |
354 EXPECT_TRUE(pref_store->GetValue("dict", &result)); | 356 EXPECT_TRUE(pref_store->GetValue("dict", &result)); |
355 EXPECT_TRUE(DictionaryValue().Equals(result)); | 357 EXPECT_TRUE(DictionaryValue().Equals(result)); |
356 } | 358 } |
357 | 359 |
358 // This test is just documenting some potentially non-obvious behavior. It | 360 // This test is just documenting some potentially non-obvious behavior. It |
359 // shouldn't be taken as normative. | 361 // shouldn't be taken as normative. |
360 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { | 362 TEST_F(JsonPrefStoreTest, RemoveClearsEmptyParent) { |
361 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); | 363 FilePath pref_file = temp_dir_.path().AppendASCII("empty_values.json"); |
362 | 364 |
363 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 365 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
364 pref_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 366 pref_file, message_loop_.task_runner(), std::unique_ptr<PrefFilter>()); |
365 | 367 |
366 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); | 368 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
367 dict->SetString("key", "value"); | 369 dict->SetString("key", "value"); |
368 pref_store->SetValue("dict", std::move(dict), | 370 pref_store->SetValue("dict", std::move(dict), |
369 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 371 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
370 | 372 |
371 pref_store->RemoveValue("dict.key", | 373 pref_store->RemoveValue("dict.key", |
372 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 374 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
373 | 375 |
374 const base::Value* retrieved_dict = NULL; | 376 const base::Value* retrieved_dict = NULL; |
375 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); | 377 bool has_dict = pref_store->GetValue("dict", &retrieved_dict); |
376 EXPECT_FALSE(has_dict); | 378 EXPECT_FALSE(has_dict); |
377 } | 379 } |
378 | 380 |
379 // Tests asynchronous reading of the file when there is no file. | 381 // Tests asynchronous reading of the file when there is no file. |
380 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { | 382 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
381 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); | 383 base::FilePath bogus_input_file = temp_dir_.path().AppendASCII("read.txt"); |
382 ASSERT_FALSE(PathExists(bogus_input_file)); | 384 ASSERT_FALSE(PathExists(bogus_input_file)); |
383 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( | 385 scoped_refptr<JsonPrefStore> pref_store = |
384 bogus_input_file, message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 386 new JsonPrefStore(bogus_input_file, message_loop_.task_runner(), |
| 387 std::unique_ptr<PrefFilter>()); |
385 MockPrefStoreObserver mock_observer; | 388 MockPrefStoreObserver mock_observer; |
386 pref_store->AddObserver(&mock_observer); | 389 pref_store->AddObserver(&mock_observer); |
387 | 390 |
388 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; | 391 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; |
389 pref_store->ReadPrefsAsync(mock_error_delegate); | 392 pref_store->ReadPrefsAsync(mock_error_delegate); |
390 | 393 |
391 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); | 394 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); |
392 EXPECT_CALL(*mock_error_delegate, | 395 EXPECT_CALL(*mock_error_delegate, |
393 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); | 396 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); |
394 RunLoop().RunUntilIdle(); | 397 RunLoop().RunUntilIdle(); |
395 pref_store->RemoveObserver(&mock_observer); | 398 pref_store->RemoveObserver(&mock_observer); |
396 | 399 |
397 EXPECT_FALSE(pref_store->ReadOnly()); | 400 EXPECT_FALSE(pref_store->ReadOnly()); |
398 } | 401 } |
399 | 402 |
400 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { | 403 TEST_F(JsonPrefStoreTest, ReadWithInterceptor) { |
401 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 404 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
402 ASSERT_LT(0, base::WriteFile(input_file, | 405 ASSERT_LT(0, base::WriteFile(input_file, |
403 kReadJson, arraysize(kReadJson) - 1)); | 406 kReadJson, arraysize(kReadJson) - 1)); |
404 | 407 |
405 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 408 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
406 new InterceptingPrefFilter()); | 409 new InterceptingPrefFilter()); |
407 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 410 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
408 intercepting_pref_filter.get(); | 411 intercepting_pref_filter.get(); |
409 scoped_refptr<JsonPrefStore> pref_store = | 412 scoped_refptr<JsonPrefStore> pref_store = |
410 new JsonPrefStore(input_file, message_loop_.task_runner(), | 413 new JsonPrefStore(input_file, message_loop_.task_runner(), |
411 std::move(intercepting_pref_filter)); | 414 std::move(intercepting_pref_filter)); |
412 | 415 |
413 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, | 416 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE, |
414 pref_store->ReadPrefs()); | 417 pref_store->ReadPrefs()); |
415 EXPECT_FALSE(pref_store->ReadOnly()); | 418 EXPECT_FALSE(pref_store->ReadOnly()); |
(...skipping 21 matching lines...) Expand all Loading... |
437 // } | 440 // } |
438 | 441 |
439 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); | 442 RunBasicJsonPrefStoreTest(pref_store.get(), input_file); |
440 } | 443 } |
441 | 444 |
442 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { | 445 TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) { |
443 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 446 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
444 ASSERT_LT(0, base::WriteFile(input_file, | 447 ASSERT_LT(0, base::WriteFile(input_file, |
445 kReadJson, arraysize(kReadJson) - 1)); | 448 kReadJson, arraysize(kReadJson) - 1)); |
446 | 449 |
447 scoped_ptr<InterceptingPrefFilter> intercepting_pref_filter( | 450 std::unique_ptr<InterceptingPrefFilter> intercepting_pref_filter( |
448 new InterceptingPrefFilter()); | 451 new InterceptingPrefFilter()); |
449 InterceptingPrefFilter* raw_intercepting_pref_filter_ = | 452 InterceptingPrefFilter* raw_intercepting_pref_filter_ = |
450 intercepting_pref_filter.get(); | 453 intercepting_pref_filter.get(); |
451 scoped_refptr<JsonPrefStore> pref_store = | 454 scoped_refptr<JsonPrefStore> pref_store = |
452 new JsonPrefStore(input_file, message_loop_.task_runner(), | 455 new JsonPrefStore(input_file, message_loop_.task_runner(), |
453 std::move(intercepting_pref_filter)); | 456 std::move(intercepting_pref_filter)); |
454 | 457 |
455 MockPrefStoreObserver mock_observer; | 458 MockPrefStoreObserver mock_observer; |
456 pref_store->AddObserver(&mock_observer); | 459 pref_store->AddObserver(&mock_observer); |
457 | 460 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 base::FilePath alternate_input_file = | 507 base::FilePath alternate_input_file = |
505 temp_dir_.path().AppendASCII("alternate.json"); | 508 temp_dir_.path().AppendASCII("alternate.json"); |
506 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 509 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
507 kReadJson, arraysize(kReadJson) - 1)); | 510 kReadJson, arraysize(kReadJson) - 1)); |
508 | 511 |
509 // Test that the alternate file is moved to the main file and read as-is from | 512 // Test that the alternate file is moved to the main file and read as-is from |
510 // there. | 513 // there. |
511 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 514 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
512 ASSERT_FALSE(PathExists(input_file)); | 515 ASSERT_FALSE(PathExists(input_file)); |
513 ASSERT_TRUE(PathExists(alternate_input_file)); | 516 ASSERT_TRUE(PathExists(alternate_input_file)); |
514 scoped_refptr<JsonPrefStore> pref_store = | 517 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
515 new JsonPrefStore(input_file, alternate_input_file, | 518 input_file, alternate_input_file, message_loop_.task_runner(), |
516 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 519 std::unique_ptr<PrefFilter>()); |
517 | 520 |
518 ASSERT_FALSE(PathExists(input_file)); | 521 ASSERT_FALSE(PathExists(input_file)); |
519 ASSERT_TRUE(PathExists(alternate_input_file)); | 522 ASSERT_TRUE(PathExists(alternate_input_file)); |
520 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 523 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
521 | 524 |
522 ASSERT_TRUE(PathExists(input_file)); | 525 ASSERT_TRUE(PathExists(input_file)); |
523 ASSERT_FALSE(PathExists(alternate_input_file)); | 526 ASSERT_FALSE(PathExists(alternate_input_file)); |
524 | 527 |
525 EXPECT_FALSE(pref_store->ReadOnly()); | 528 EXPECT_FALSE(pref_store->ReadOnly()); |
526 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 529 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 17 matching lines...) Expand all Loading... |
544 kReadJson, arraysize(kReadJson) - 1)); | 547 kReadJson, arraysize(kReadJson) - 1)); |
545 | 548 |
546 base::FilePath alternate_input_file = | 549 base::FilePath alternate_input_file = |
547 temp_dir_.path().AppendASCII("alternate.json"); | 550 temp_dir_.path().AppendASCII("alternate.json"); |
548 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 551 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
549 kInvalidJson, arraysize(kInvalidJson) - 1)); | 552 kInvalidJson, arraysize(kInvalidJson) - 1)); |
550 | 553 |
551 // Test that the alternate file is ignored and that the read occurs from the | 554 // Test that the alternate file is ignored and that the read occurs from the |
552 // existing main file. There is no attempt at even deleting the alternate | 555 // existing main file. There is no attempt at even deleting the alternate |
553 // file as this scenario should never happen in normal user-data-dirs. | 556 // file as this scenario should never happen in normal user-data-dirs. |
554 scoped_refptr<JsonPrefStore> pref_store = | 557 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
555 new JsonPrefStore(input_file, alternate_input_file, | 558 input_file, alternate_input_file, message_loop_.task_runner(), |
556 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 559 std::unique_ptr<PrefFilter>()); |
557 | 560 |
558 ASSERT_TRUE(PathExists(input_file)); | 561 ASSERT_TRUE(PathExists(input_file)); |
559 ASSERT_TRUE(PathExists(alternate_input_file)); | 562 ASSERT_TRUE(PathExists(alternate_input_file)); |
560 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 563 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
561 | 564 |
562 ASSERT_TRUE(PathExists(input_file)); | 565 ASSERT_TRUE(PathExists(input_file)); |
563 ASSERT_TRUE(PathExists(alternate_input_file)); | 566 ASSERT_TRUE(PathExists(alternate_input_file)); |
564 | 567 |
565 EXPECT_FALSE(pref_store->ReadOnly()); | 568 EXPECT_FALSE(pref_store->ReadOnly()); |
566 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 569 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 15 matching lines...) Expand all Loading... |
582 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 585 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
583 ASSERT_LT(0, base::WriteFile(input_file, | 586 ASSERT_LT(0, base::WriteFile(input_file, |
584 kReadJson, arraysize(kReadJson) - 1)); | 587 kReadJson, arraysize(kReadJson) - 1)); |
585 | 588 |
586 // Test that the basic read works fine when an alternate file is specified but | 589 // Test that the basic read works fine when an alternate file is specified but |
587 // does not exist. | 590 // does not exist. |
588 base::FilePath alternate_input_file = | 591 base::FilePath alternate_input_file = |
589 temp_dir_.path().AppendASCII("alternate.json"); | 592 temp_dir_.path().AppendASCII("alternate.json"); |
590 ASSERT_TRUE(PathExists(input_file)); | 593 ASSERT_TRUE(PathExists(input_file)); |
591 ASSERT_FALSE(PathExists(alternate_input_file)); | 594 ASSERT_FALSE(PathExists(alternate_input_file)); |
592 scoped_refptr<JsonPrefStore> pref_store = | 595 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
593 new JsonPrefStore(input_file, alternate_input_file, | 596 input_file, alternate_input_file, message_loop_.task_runner(), |
594 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 597 std::unique_ptr<PrefFilter>()); |
595 | 598 |
596 ASSERT_TRUE(PathExists(input_file)); | 599 ASSERT_TRUE(PathExists(input_file)); |
597 ASSERT_FALSE(PathExists(alternate_input_file)); | 600 ASSERT_FALSE(PathExists(alternate_input_file)); |
598 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); | 601 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
599 | 602 |
600 ASSERT_TRUE(PathExists(input_file)); | 603 ASSERT_TRUE(PathExists(input_file)); |
601 ASSERT_FALSE(PathExists(alternate_input_file)); | 604 ASSERT_FALSE(PathExists(alternate_input_file)); |
602 | 605 |
603 EXPECT_FALSE(pref_store->ReadOnly()); | 606 EXPECT_FALSE(pref_store->ReadOnly()); |
604 EXPECT_TRUE(pref_store->IsInitializationComplete()); | 607 EXPECT_TRUE(pref_store->IsInitializationComplete()); |
(...skipping 13 matching lines...) Expand all Loading... |
618 | 621 |
619 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { | 622 TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) { |
620 base::FilePath alternate_input_file = | 623 base::FilePath alternate_input_file = |
621 temp_dir_.path().AppendASCII("alternate.json"); | 624 temp_dir_.path().AppendASCII("alternate.json"); |
622 ASSERT_LT(0, base::WriteFile(alternate_input_file, | 625 ASSERT_LT(0, base::WriteFile(alternate_input_file, |
623 kReadJson, arraysize(kReadJson) - 1)); | 626 kReadJson, arraysize(kReadJson) - 1)); |
624 | 627 |
625 // Test that the alternate file is moved to the main file and read as-is from | 628 // Test that the alternate file is moved to the main file and read as-is from |
626 // there even when the read is made asynchronously. | 629 // there even when the read is made asynchronously. |
627 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); | 630 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
628 scoped_refptr<JsonPrefStore> pref_store = | 631 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore( |
629 new JsonPrefStore(input_file, alternate_input_file, | 632 input_file, alternate_input_file, message_loop_.task_runner(), |
630 message_loop_.task_runner(), scoped_ptr<PrefFilter>()); | 633 std::unique_ptr<PrefFilter>()); |
631 | 634 |
632 ASSERT_FALSE(PathExists(input_file)); | 635 ASSERT_FALSE(PathExists(input_file)); |
633 ASSERT_TRUE(PathExists(alternate_input_file)); | 636 ASSERT_TRUE(PathExists(alternate_input_file)); |
634 | 637 |
635 { | 638 { |
636 MockPrefStoreObserver mock_observer; | 639 MockPrefStoreObserver mock_observer; |
637 pref_store->AddObserver(&mock_observer); | 640 pref_store->AddObserver(&mock_observer); |
638 | 641 |
639 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; | 642 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate; |
640 pref_store->ReadPrefsAsync(mock_error_delegate); | 643 pref_store->ReadPrefsAsync(mock_error_delegate); |
(...skipping 25 matching lines...) Expand all Loading... |
666 } | 669 } |
667 | 670 |
668 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { | 671 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestBasic) { |
669 base::HistogramTester histogram_tester; | 672 base::HistogramTester histogram_tester; |
670 | 673 |
671 SimpleTestClock* test_clock = new SimpleTestClock; | 674 SimpleTestClock* test_clock = new SimpleTestClock; |
672 SetCurrentTimeInMinutes(0, test_clock); | 675 SetCurrentTimeInMinutes(0, test_clock); |
673 JsonPrefStore::WriteCountHistogram histogram( | 676 JsonPrefStore::WriteCountHistogram histogram( |
674 base::TimeDelta::FromSeconds(10), | 677 base::TimeDelta::FromSeconds(10), |
675 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 678 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
676 scoped_ptr<base::Clock>(test_clock)); | 679 std::unique_ptr<base::Clock>(test_clock)); |
677 int32_t report_interval = | 680 int32_t report_interval = |
678 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 681 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
679 | 682 |
680 histogram.RecordWriteOccured(); | 683 histogram.RecordWriteOccured(); |
681 | 684 |
682 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); | 685 SetCurrentTimeInMinutes(1.5 * report_interval, test_clock); |
683 histogram.ReportOutstandingWrites(); | 686 histogram.ReportOutstandingWrites(); |
684 scoped_ptr<HistogramSamples> samples = | 687 std::unique_ptr<HistogramSamples> samples = |
685 histogram.GetHistogram()->SnapshotSamples(); | 688 histogram.GetHistogram()->SnapshotSamples(); |
686 | 689 |
687 std::string histogram_name = histogram.GetHistogram()->histogram_name(); | 690 std::string histogram_name = histogram.GetHistogram()->histogram_name(); |
688 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); | 691 histogram_tester.ExpectBucketCount(histogram_name, 1, 1); |
689 histogram_tester.ExpectTotalCount(histogram_name, 1); | 692 histogram_tester.ExpectTotalCount(histogram_name, 1); |
690 | 693 |
691 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", | 694 ASSERT_EQ("Settings.JsonDataWriteCount.Local_State", |
692 histogram.GetHistogram()->histogram_name()); | 695 histogram.GetHistogram()->histogram_name()); |
693 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); | 696 ASSERT_TRUE(histogram.GetHistogram()->HasConstructionArguments(1, 30, 31)); |
694 } | 697 } |
695 | 698 |
696 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { | 699 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod) { |
697 base::HistogramTester histogram_tester; | 700 base::HistogramTester histogram_tester; |
698 | 701 |
699 SimpleTestClock* test_clock = new SimpleTestClock; | 702 SimpleTestClock* test_clock = new SimpleTestClock; |
700 SetCurrentTimeInMinutes(0, test_clock); | 703 SetCurrentTimeInMinutes(0, test_clock); |
701 JsonPrefStore::WriteCountHistogram histogram( | 704 JsonPrefStore::WriteCountHistogram histogram( |
702 base::TimeDelta::FromSeconds(10), | 705 base::TimeDelta::FromSeconds(10), |
703 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 706 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
704 scoped_ptr<base::Clock>(test_clock)); | 707 std::unique_ptr<base::Clock>(test_clock)); |
705 int32_t report_interval = | 708 int32_t report_interval = |
706 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 709 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
707 | 710 |
708 histogram.RecordWriteOccured(); | 711 histogram.RecordWriteOccured(); |
709 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); | 712 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
710 histogram.RecordWriteOccured(); | 713 histogram.RecordWriteOccured(); |
711 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); | 714 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
712 histogram.RecordWriteOccured(); | 715 histogram.RecordWriteOccured(); |
713 | 716 |
714 // Nothing should be recorded until the report period has elapsed. | 717 // Nothing should be recorded until the report period has elapsed. |
(...skipping 17 matching lines...) Expand all Loading... |
732 } | 735 } |
733 | 736 |
734 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { | 737 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods) { |
735 base::HistogramTester histogram_tester; | 738 base::HistogramTester histogram_tester; |
736 | 739 |
737 SimpleTestClock* test_clock = new SimpleTestClock; | 740 SimpleTestClock* test_clock = new SimpleTestClock; |
738 SetCurrentTimeInMinutes(0, test_clock); | 741 SetCurrentTimeInMinutes(0, test_clock); |
739 JsonPrefStore::WriteCountHistogram histogram( | 742 JsonPrefStore::WriteCountHistogram histogram( |
740 base::TimeDelta::FromSeconds(10), | 743 base::TimeDelta::FromSeconds(10), |
741 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 744 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
742 scoped_ptr<base::Clock>(test_clock)); | 745 std::unique_ptr<base::Clock>(test_clock)); |
743 int32_t report_interval = | 746 int32_t report_interval = |
744 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 747 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
745 | 748 |
746 histogram.RecordWriteOccured(); | 749 histogram.RecordWriteOccured(); |
747 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); | 750 SetCurrentTimeInMinutes(0.5 * report_interval, test_clock); |
748 histogram.RecordWriteOccured(); | 751 histogram.RecordWriteOccured(); |
749 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); | 752 SetCurrentTimeInMinutes(0.7 * report_interval, test_clock); |
750 histogram.RecordWriteOccured(); | 753 histogram.RecordWriteOccured(); |
751 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); | 754 SetCurrentTimeInMinutes(1.3 * report_interval, test_clock); |
752 histogram.RecordWriteOccured(); | 755 histogram.RecordWriteOccured(); |
(...skipping 19 matching lines...) Expand all Loading... |
772 } | 775 } |
773 | 776 |
774 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { | 777 TEST_F(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps) { |
775 base::HistogramTester histogram_tester; | 778 base::HistogramTester histogram_tester; |
776 | 779 |
777 SimpleTestClock* test_clock = new SimpleTestClock; | 780 SimpleTestClock* test_clock = new SimpleTestClock; |
778 SetCurrentTimeInMinutes(0, test_clock); | 781 SetCurrentTimeInMinutes(0, test_clock); |
779 JsonPrefStore::WriteCountHistogram histogram( | 782 JsonPrefStore::WriteCountHistogram histogram( |
780 base::TimeDelta::FromSeconds(10), | 783 base::TimeDelta::FromSeconds(10), |
781 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), | 784 base::FilePath(FILE_PATH_LITERAL("/tmp/Local State")), |
782 scoped_ptr<base::Clock>(test_clock)); | 785 std::unique_ptr<base::Clock>(test_clock)); |
783 int32_t report_interval = | 786 int32_t report_interval = |
784 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; | 787 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins; |
785 | 788 |
786 // 1 write in the first period. | 789 // 1 write in the first period. |
787 histogram.RecordWriteOccured(); | 790 histogram.RecordWriteOccured(); |
788 | 791 |
789 // No writes in the second and third periods. | 792 // No writes in the second and third periods. |
790 | 793 |
791 // 2 writes in the fourth period. | 794 // 2 writes in the fourth period. |
792 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); | 795 SetCurrentTimeInMinutes(3.1 * report_interval, test_clock); |
(...skipping 24 matching lines...) Expand all Loading... |
817 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { | 820 class JsonPrefStoreLossyWriteTest : public JsonPrefStoreTest { |
818 protected: | 821 protected: |
819 void SetUp() override { | 822 void SetUp() override { |
820 JsonPrefStoreTest::SetUp(); | 823 JsonPrefStoreTest::SetUp(); |
821 test_file_ = temp_dir_.path().AppendASCII("test.json"); | 824 test_file_ = temp_dir_.path().AppendASCII("test.json"); |
822 } | 825 } |
823 | 826 |
824 // Creates a JsonPrefStore with the given |file_writer|. | 827 // Creates a JsonPrefStore with the given |file_writer|. |
825 scoped_refptr<JsonPrefStore> CreatePrefStore() { | 828 scoped_refptr<JsonPrefStore> CreatePrefStore() { |
826 return new JsonPrefStore(test_file_, message_loop_.task_runner(), | 829 return new JsonPrefStore(test_file_, message_loop_.task_runner(), |
827 scoped_ptr<PrefFilter>()); | 830 std::unique_ptr<PrefFilter>()); |
828 } | 831 } |
829 | 832 |
830 // Return the ImportantFileWriter for a given JsonPrefStore. | 833 // Return the ImportantFileWriter for a given JsonPrefStore. |
831 ImportantFileWriter* GetImportantFileWriter( | 834 ImportantFileWriter* GetImportantFileWriter( |
832 scoped_refptr<JsonPrefStore> pref_store) { | 835 scoped_refptr<JsonPrefStore> pref_store) { |
833 return &(pref_store->writer_); | 836 return &(pref_store->writer_); |
834 } | 837 } |
835 | 838 |
836 // Get the contents of kTestFile. Pumps the message loop before returning the | 839 // Get the contents of kTestFile. Pumps the message loop before returning the |
837 // result. | 840 // result. |
838 std::string GetTestFileContents() { | 841 std::string GetTestFileContents() { |
839 RunLoop().RunUntilIdle(); | 842 RunLoop().RunUntilIdle(); |
840 std::string file_contents; | 843 std::string file_contents; |
841 ReadFileToString(test_file_, &file_contents); | 844 ReadFileToString(test_file_, &file_contents); |
842 return file_contents; | 845 return file_contents; |
843 } | 846 } |
844 | 847 |
845 private: | 848 private: |
846 base::FilePath test_file_; | 849 base::FilePath test_file_; |
847 }; | 850 }; |
848 | 851 |
849 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { | 852 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteBasic) { |
850 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 853 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
851 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 854 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
852 | 855 |
853 // Set a normal pref and check that it gets scheduled to be written. | 856 // Set a normal pref and check that it gets scheduled to be written. |
854 ASSERT_FALSE(file_writer->HasPendingWrite()); | 857 ASSERT_FALSE(file_writer->HasPendingWrite()); |
855 pref_store->SetValue("normal", | 858 pref_store->SetValue("normal", |
856 make_scoped_ptr(new base::StringValue("normal")), | 859 base::WrapUnique(new base::StringValue("normal")), |
857 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 860 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
858 ASSERT_TRUE(file_writer->HasPendingWrite()); | 861 ASSERT_TRUE(file_writer->HasPendingWrite()); |
859 file_writer->DoScheduledWrite(); | 862 file_writer->DoScheduledWrite(); |
860 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); | 863 ASSERT_EQ("{\"normal\":\"normal\"}", GetTestFileContents()); |
861 ASSERT_FALSE(file_writer->HasPendingWrite()); | 864 ASSERT_FALSE(file_writer->HasPendingWrite()); |
862 | 865 |
863 // Set a lossy pref and check that it is not scheduled to be written. | 866 // Set a lossy pref and check that it is not scheduled to be written. |
864 // SetValue/RemoveValue. | 867 // SetValue/RemoveValue. |
865 pref_store->SetValue("lossy", make_scoped_ptr(new base::StringValue("lossy")), | 868 pref_store->SetValue("lossy", |
| 869 base::WrapUnique(new base::StringValue("lossy")), |
866 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 870 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
867 ASSERT_FALSE(file_writer->HasPendingWrite()); | 871 ASSERT_FALSE(file_writer->HasPendingWrite()); |
868 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 872 pref_store->RemoveValue("lossy", WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
869 ASSERT_FALSE(file_writer->HasPendingWrite()); | 873 ASSERT_FALSE(file_writer->HasPendingWrite()); |
870 | 874 |
871 // SetValueSilently/RemoveValueSilently. | 875 // SetValueSilently/RemoveValueSilently. |
872 pref_store->SetValueSilently("lossy", | 876 pref_store->SetValueSilently("lossy", |
873 make_scoped_ptr(new base::StringValue("lossy")), | 877 base::WrapUnique(new base::StringValue("lossy")), |
874 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 878 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
875 ASSERT_FALSE(file_writer->HasPendingWrite()); | 879 ASSERT_FALSE(file_writer->HasPendingWrite()); |
876 pref_store->RemoveValueSilently("lossy", | 880 pref_store->RemoveValueSilently("lossy", |
877 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 881 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
878 ASSERT_FALSE(file_writer->HasPendingWrite()); | 882 ASSERT_FALSE(file_writer->HasPendingWrite()); |
879 | 883 |
880 // ReportValueChanged. | 884 // ReportValueChanged. |
881 pref_store->SetValue("lossy", make_scoped_ptr(new base::StringValue("lossy")), | 885 pref_store->SetValue("lossy", |
| 886 base::WrapUnique(new base::StringValue("lossy")), |
882 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 887 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
883 ASSERT_FALSE(file_writer->HasPendingWrite()); | 888 ASSERT_FALSE(file_writer->HasPendingWrite()); |
884 pref_store->ReportValueChanged("lossy", | 889 pref_store->ReportValueChanged("lossy", |
885 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 890 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
886 ASSERT_FALSE(file_writer->HasPendingWrite()); | 891 ASSERT_FALSE(file_writer->HasPendingWrite()); |
887 | 892 |
888 // Call CommitPendingWrite and check that the lossy pref and the normal pref | 893 // Call CommitPendingWrite and check that the lossy pref and the normal pref |
889 // are there with the last values set above. | 894 // are there with the last values set above. |
890 pref_store->CommitPendingWrite(); | 895 pref_store->CommitPendingWrite(); |
891 ASSERT_FALSE(file_writer->HasPendingWrite()); | 896 ASSERT_FALSE(file_writer->HasPendingWrite()); |
892 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 897 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
893 GetTestFileContents()); | 898 GetTestFileContents()); |
894 } | 899 } |
895 | 900 |
896 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { | 901 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossyFirst) { |
897 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 902 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
898 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 903 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
899 | 904 |
900 // Set a lossy pref and check that it is not scheduled to be written. | 905 // Set a lossy pref and check that it is not scheduled to be written. |
901 ASSERT_FALSE(file_writer->HasPendingWrite()); | 906 ASSERT_FALSE(file_writer->HasPendingWrite()); |
902 pref_store->SetValue("lossy", make_scoped_ptr(new base::StringValue("lossy")), | 907 pref_store->SetValue("lossy", |
| 908 base::WrapUnique(new base::StringValue("lossy")), |
903 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 909 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
904 ASSERT_FALSE(file_writer->HasPendingWrite()); | 910 ASSERT_FALSE(file_writer->HasPendingWrite()); |
905 | 911 |
906 // Set a normal pref and check that it is scheduled to be written. | 912 // Set a normal pref and check that it is scheduled to be written. |
907 pref_store->SetValue("normal", | 913 pref_store->SetValue("normal", |
908 make_scoped_ptr(new base::StringValue("normal")), | 914 base::WrapUnique(new base::StringValue("normal")), |
909 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 915 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
910 ASSERT_TRUE(file_writer->HasPendingWrite()); | 916 ASSERT_TRUE(file_writer->HasPendingWrite()); |
911 | 917 |
912 // Call DoScheduledWrite and check both prefs get written. | 918 // Call DoScheduledWrite and check both prefs get written. |
913 file_writer->DoScheduledWrite(); | 919 file_writer->DoScheduledWrite(); |
914 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 920 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
915 GetTestFileContents()); | 921 GetTestFileContents()); |
916 ASSERT_FALSE(file_writer->HasPendingWrite()); | 922 ASSERT_FALSE(file_writer->HasPendingWrite()); |
917 } | 923 } |
918 | 924 |
919 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { | 925 TEST_F(JsonPrefStoreLossyWriteTest, LossyWriteMixedLossySecond) { |
920 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 926 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
921 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 927 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
922 | 928 |
923 // Set a normal pref and check that it is scheduled to be written. | 929 // Set a normal pref and check that it is scheduled to be written. |
924 ASSERT_FALSE(file_writer->HasPendingWrite()); | 930 ASSERT_FALSE(file_writer->HasPendingWrite()); |
925 pref_store->SetValue("normal", | 931 pref_store->SetValue("normal", |
926 make_scoped_ptr(new base::StringValue("normal")), | 932 base::WrapUnique(new base::StringValue("normal")), |
927 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 933 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
928 ASSERT_TRUE(file_writer->HasPendingWrite()); | 934 ASSERT_TRUE(file_writer->HasPendingWrite()); |
929 | 935 |
930 // Set a lossy pref and check that the write is still scheduled. | 936 // Set a lossy pref and check that the write is still scheduled. |
931 pref_store->SetValue("lossy", make_scoped_ptr(new base::StringValue("lossy")), | 937 pref_store->SetValue("lossy", |
| 938 base::WrapUnique(new base::StringValue("lossy")), |
932 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 939 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
933 ASSERT_TRUE(file_writer->HasPendingWrite()); | 940 ASSERT_TRUE(file_writer->HasPendingWrite()); |
934 | 941 |
935 // Call DoScheduledWrite and check both prefs get written. | 942 // Call DoScheduledWrite and check both prefs get written. |
936 file_writer->DoScheduledWrite(); | 943 file_writer->DoScheduledWrite(); |
937 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", | 944 ASSERT_EQ("{\"lossy\":\"lossy\",\"normal\":\"normal\"}", |
938 GetTestFileContents()); | 945 GetTestFileContents()); |
939 ASSERT_FALSE(file_writer->HasPendingWrite()); | 946 ASSERT_FALSE(file_writer->HasPendingWrite()); |
940 } | 947 } |
941 | 948 |
942 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { | 949 TEST_F(JsonPrefStoreLossyWriteTest, ScheduleLossyWrite) { |
943 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); | 950 scoped_refptr<JsonPrefStore> pref_store = CreatePrefStore(); |
944 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); | 951 ImportantFileWriter* file_writer = GetImportantFileWriter(pref_store); |
945 | 952 |
946 // Set a lossy pref and check that it is not scheduled to be written. | 953 // Set a lossy pref and check that it is not scheduled to be written. |
947 pref_store->SetValue("lossy", make_scoped_ptr(new base::StringValue("lossy")), | 954 pref_store->SetValue("lossy", |
| 955 base::WrapUnique(new base::StringValue("lossy")), |
948 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); | 956 WriteablePrefStore::LOSSY_PREF_WRITE_FLAG); |
949 ASSERT_FALSE(file_writer->HasPendingWrite()); | 957 ASSERT_FALSE(file_writer->HasPendingWrite()); |
950 | 958 |
951 // Schedule pending lossy writes and check that it is scheduled. | 959 // Schedule pending lossy writes and check that it is scheduled. |
952 pref_store->SchedulePendingLossyWrites(); | 960 pref_store->SchedulePendingLossyWrites(); |
953 ASSERT_TRUE(file_writer->HasPendingWrite()); | 961 ASSERT_TRUE(file_writer->HasPendingWrite()); |
954 | 962 |
955 // Call CommitPendingWrite and check that the lossy pref is there with the | 963 // Call CommitPendingWrite and check that the lossy pref is there with the |
956 // last value set above. | 964 // last value set above. |
957 pref_store->CommitPendingWrite(); | 965 pref_store->CommitPendingWrite(); |
958 ASSERT_FALSE(file_writer->HasPendingWrite()); | 966 ASSERT_FALSE(file_writer->HasPendingWrite()); |
959 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); | 967 ASSERT_EQ("{\"lossy\":\"lossy\"}", GetTestFileContents()); |
960 } | 968 } |
961 | 969 |
962 } // namespace base | 970 } // namespace base |
OLD | NEW |