Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: chrome/common/json_pref_store_unittest.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop.h"
9 #include "base/message_loop_proxy.h"
10 #include "base/path_service.h" 8 #include "base/path_service.h"
11 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
12 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
13 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/threading/sequenced_worker_pool.h"
14 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
15 #include "base/utf_string_conversions.h" 14 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 15 #include "base/values.h"
17 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/json_pref_store.h" 17 #include "chrome/common/json_pref_store.h"
19 #include "chrome/common/pref_names.h" 18 #include "chrome/common/pref_names.h"
20 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
22 21
23 namespace { 22 namespace {
24 23
25 class MockPrefStoreObserver : public PrefStore::Observer { 24 class MockPrefStoreObserver : public PrefStore::Observer {
26 public: 25 public:
27 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); 26 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
28 MOCK_METHOD1(OnInitializationCompleted, void (bool)); 27 MOCK_METHOD1(OnInitializationCompleted, void (bool));
29 }; 28 };
30 29
31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { 30 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
32 public: 31 public:
33 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); 32 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
34 }; 33 };
35 34
36 } // namespace 35 } // namespace
37 36
38 class JsonPrefStoreTest : public testing::Test { 37 class JsonPrefStoreTest : public testing::Test {
39 protected: 38 protected:
40 virtual void SetUp() { 39 virtual void SetUp() OVERRIDE {
41 message_loop_proxy_ = base::MessageLoopProxy::current(); 40 blocking_pool_ = new base::SequencedWorkerPool(3, "TestBlocking");
42 41
43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
44 43
45 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); 44 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
46 data_dir_ = data_dir_.AppendASCII("pref_service"); 45 data_dir_ = data_dir_.AppendASCII("pref_service");
47 ASSERT_TRUE(file_util::PathExists(data_dir_)); 46 ASSERT_TRUE(file_util::PathExists(data_dir_));
48 } 47 }
49 48
49 virtual void TearDown() OVERRIDE {
50 blocking_pool_->Shutdown();
51 }
52
50 // The path to temporary directory used to contain the test operations. 53 // The path to temporary directory used to contain the test operations.
51 ScopedTempDir temp_dir_; 54 ScopedTempDir temp_dir_;
52 // The path to the directory where the test data is stored. 55 // The path to the directory where the test data is stored.
53 FilePath data_dir_; 56 FilePath data_dir_;
54 // A message loop that we can use as the file thread message loop. 57 // A message loop that we need for timers.
55 MessageLoop message_loop_; 58 MessageLoop message_loop_;
56 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 59 // A blocking pool that we can use for file operations.
60 scoped_refptr<base::SequencedWorkerPool> blocking_pool_;
57 }; 61 };
58 62
59 // Test fallback behavior for a nonexistent file. 63 // Test fallback behavior for a nonexistent file.
60 TEST_F(JsonPrefStoreTest, NonExistentFile) { 64 TEST_F(JsonPrefStoreTest, NonExistentFile) {
61 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); 65 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
62 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); 66 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
63 scoped_refptr<JsonPrefStore> pref_store = 67 scoped_refptr<JsonPrefStore> pref_store =
64 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); 68 JsonPrefStore::Create(
69 bogus_input_file,
70 JsonPrefStore::GetTaskRunnerForFile(bogus_input_file,
71 blocking_pool_));
65 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, 72 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
66 pref_store->ReadPrefs()); 73 pref_store->ReadPrefs());
67 EXPECT_FALSE(pref_store->ReadOnly()); 74 EXPECT_FALSE(pref_store->ReadOnly());
68 } 75 }
69 76
70 // Test fallback behavior for an invalid file. 77 // Test fallback behavior for an invalid file.
71 TEST_F(JsonPrefStoreTest, InvalidFile) { 78 TEST_F(JsonPrefStoreTest, InvalidFile) {
72 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); 79 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
73 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); 80 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
74 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); 81 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file));
75 scoped_refptr<JsonPrefStore> pref_store = 82 scoped_refptr<JsonPrefStore> pref_store =
76 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); 83 JsonPrefStore::Create(
84 invalid_file,
85 JsonPrefStore::GetTaskRunnerForFile(invalid_file, blocking_pool_));
77 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, 86 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
78 pref_store->ReadPrefs()); 87 pref_store->ReadPrefs());
79 EXPECT_FALSE(pref_store->ReadOnly()); 88 EXPECT_FALSE(pref_store->ReadOnly());
80 89
81 // The file should have been moved aside. 90 // The file should have been moved aside.
82 EXPECT_FALSE(file_util::PathExists(invalid_file)); 91 EXPECT_FALSE(file_util::PathExists(invalid_file));
83 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); 92 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
84 EXPECT_TRUE(file_util::PathExists(moved_aside)); 93 EXPECT_TRUE(file_util::PathExists(moved_aside));
85 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, 94 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
86 moved_aside)); 95 moved_aside));
87 } 96 }
88 97
89 // This function is used to avoid code duplication while testing synchronous and 98 // This function is used to avoid code duplication while testing synchronous and
90 // asynchronous version of the JsonPrefStore loading. 99 // asynchronous version of the JsonPrefStore loading.
91 void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store, 100 void RunBasicJsonPrefStoreTest(base::SequencedWorkerPool* blocking_pool,
akalin 2012/10/19 02:00:51 looks like you don't actually use the blocking poo
101 JsonPrefStore *pref_store,
akalin 2012/10/19 02:00:51 ' *' -> '* '
92 const FilePath& output_file, 102 const FilePath& output_file,
93 const FilePath& golden_output_file) { 103 const FilePath& golden_output_file) {
94 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; 104 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
95 const char kMaxTabs[] = "tabs.max_tabs"; 105 const char kMaxTabs[] = "tabs.max_tabs";
96 const char kLongIntPref[] = "long_int.pref"; 106 const char kLongIntPref[] = "long_int.pref";
97 107
98 std::string cnn("http://www.cnn.com"); 108 std::string cnn("http://www.cnn.com");
99 109
100 const Value* actual; 110 const Value* actual;
101 EXPECT_EQ(PrefStore::READ_OK, 111 EXPECT_EQ(PrefStore::READ_OK,
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 base::Int64ToString(214748364842LL))); 156 base::Int64ToString(214748364842LL)));
147 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); 157 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual));
148 EXPECT_TRUE(actual->GetAsString(&string_value)); 158 EXPECT_TRUE(actual->GetAsString(&string_value));
149 int64 value; 159 int64 value;
150 base::StringToInt64(string_value, &value); 160 base::StringToInt64(string_value, &value);
151 EXPECT_EQ(214748364842LL, value); 161 EXPECT_EQ(214748364842LL, value);
152 162
153 // Serialize and compare to expected output. 163 // Serialize and compare to expected output.
154 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 164 ASSERT_TRUE(file_util::PathExists(golden_output_file));
155 pref_store->CommitPendingWrite(); 165 pref_store->CommitPendingWrite();
156 MessageLoop::current()->RunAllPending(); 166 blocking_pool->FlushForTesting();
157 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 167 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
158 ASSERT_TRUE(file_util::Delete(output_file, false)); 168 ASSERT_TRUE(file_util::Delete(output_file, false));
159 } 169 }
160 170
161 TEST_F(JsonPrefStoreTest, Basic) { 171 TEST_F(JsonPrefStoreTest, Basic) {
162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), 172 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
163 temp_dir_.path().AppendASCII("write.json"))); 173 temp_dir_.path().AppendASCII("write.json")));
164 174
165 // Test that the persistent value can be loaded. 175 // Test that the persistent value can be loaded.
166 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 176 FilePath input_file = temp_dir_.path().AppendASCII("write.json");
167 ASSERT_TRUE(file_util::PathExists(input_file)); 177 ASSERT_TRUE(file_util::PathExists(input_file));
168 scoped_refptr<JsonPrefStore> pref_store = 178 scoped_refptr<JsonPrefStore> pref_store =
169 new JsonPrefStore(input_file, message_loop_proxy_.get()); 179 JsonPrefStore::Create(
180 input_file,
181 JsonPrefStore::GetTaskRunnerForFile(input_file, blocking_pool_));
170 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 182 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
171 ASSERT_FALSE(pref_store->ReadOnly()); 183 ASSERT_FALSE(pref_store->ReadOnly());
172 184
173 // The JSON file looks like this: 185 // The JSON file looks like this:
174 // { 186 // {
175 // "homepage": "http://www.cnn.com", 187 // "homepage": "http://www.cnn.com",
176 // "some_directory": "/usr/local/", 188 // "some_directory": "/usr/local/",
177 // "tabs": { 189 // "tabs": {
178 // "new_windows_in_tabs": true, 190 // "new_windows_in_tabs": true,
179 // "max_tabs": 20 191 // "max_tabs": 20
180 // } 192 // }
181 // } 193 // }
182 194
183 RunBasicJsonPrefStoreTest(pref_store, 195 RunBasicJsonPrefStoreTest(blocking_pool_.get(),
196 pref_store,
184 input_file, 197 input_file,
185 data_dir_.AppendASCII("write.golden.json")); 198 data_dir_.AppendASCII("write.golden.json"));
186 } 199 }
187 200
188 TEST_F(JsonPrefStoreTest, BasicAsync) { 201 TEST_F(JsonPrefStoreTest, BasicAsync) {
189 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), 202 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
190 temp_dir_.path().AppendASCII("write.json"))); 203 temp_dir_.path().AppendASCII("write.json")));
191 204
192 // Test that the persistent value can be loaded. 205 // Test that the persistent value can be loaded.
193 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 206 FilePath input_file = temp_dir_.path().AppendASCII("write.json");
194 ASSERT_TRUE(file_util::PathExists(input_file)); 207 ASSERT_TRUE(file_util::PathExists(input_file));
195 scoped_refptr<JsonPrefStore> pref_store = 208 scoped_refptr<JsonPrefStore> pref_store =
196 new JsonPrefStore(input_file, message_loop_proxy_.get()); 209 JsonPrefStore::Create(
210 input_file,
211 JsonPrefStore::GetTaskRunnerForFile(input_file, blocking_pool_));
197 212
198 MockPrefStoreObserver mock_observer; 213 {
199 pref_store->AddObserver(&mock_observer); 214 MockPrefStoreObserver mock_observer;
215 pref_store->AddObserver(&mock_observer);
200 216
201 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; 217 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
akalin 2012/10/19 02:00:51 ' *' -> '* '
zel 2012/10/19 18:45:07 Done.
202 pref_store->ReadPrefsAsync(mock_error_delegate); 218 pref_store->ReadPrefsAsync(mock_error_delegate);
203 219
204 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 220 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
205 EXPECT_CALL(*mock_error_delegate, 221 EXPECT_CALL(*mock_error_delegate,
206 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0); 222 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
207 message_loop_.RunAllPending(); 223 blocking_pool_->FlushForTesting();
208 pref_store->RemoveObserver(&mock_observer); 224 message_loop_.RunAllPending();
225 pref_store->RemoveObserver(&mock_observer);
209 226
210 ASSERT_FALSE(pref_store->ReadOnly()); 227 ASSERT_FALSE(pref_store->ReadOnly());
228 }
211 229
212 // The JSON file looks like this: 230 // The JSON file looks like this:
213 // { 231 // {
214 // "homepage": "http://www.cnn.com", 232 // "homepage": "http://www.cnn.com",
215 // "some_directory": "/usr/local/", 233 // "some_directory": "/usr/local/",
216 // "tabs": { 234 // "tabs": {
217 // "new_windows_in_tabs": true, 235 // "new_windows_in_tabs": true,
218 // "max_tabs": 20 236 // "max_tabs": 20
219 // } 237 // }
220 // } 238 // }
221 239
222 RunBasicJsonPrefStoreTest(pref_store, 240 RunBasicJsonPrefStoreTest(blocking_pool_.get(),
241 pref_store,
223 input_file, 242 input_file,
224 data_dir_.AppendASCII("write.golden.json")); 243 data_dir_.AppendASCII("write.golden.json"));
225 } 244 }
226 245
227 // Tests asynchronous reading of the file when there is no file. 246 // Tests asynchronous reading of the file when there is no file.
228 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { 247 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
229 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); 248 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
230 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); 249 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
231 scoped_refptr<JsonPrefStore> pref_store = 250 scoped_refptr<JsonPrefStore> pref_store =
232 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); 251 JsonPrefStore::Create(
252 bogus_input_file,
253 JsonPrefStore::GetTaskRunnerForFile(bogus_input_file,
254 blocking_pool_));
233 MockPrefStoreObserver mock_observer; 255 MockPrefStoreObserver mock_observer;
234 pref_store->AddObserver(&mock_observer); 256 pref_store->AddObserver(&mock_observer);
235 257
236 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate; 258 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
237 pref_store->ReadPrefsAsync(mock_error_delegate); 259 pref_store->ReadPrefsAsync(mock_error_delegate);
238 260
239 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 261 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
240 EXPECT_CALL(*mock_error_delegate, 262 EXPECT_CALL(*mock_error_delegate,
241 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1); 263 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
264 blocking_pool_->FlushForTesting();
242 message_loop_.RunAllPending(); 265 message_loop_.RunAllPending();
243 pref_store->RemoveObserver(&mock_observer); 266 pref_store->RemoveObserver(&mock_observer);
244 267
245 EXPECT_FALSE(pref_store->ReadOnly()); 268 EXPECT_FALSE(pref_store->ReadOnly());
246 } 269 }
247 270
248 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) { 271 TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
249 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); 272 FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
250 273
251 ASSERT_TRUE(file_util::CopyFile( 274 ASSERT_TRUE(file_util::CopyFile(
252 data_dir_.AppendASCII("read.need_empty_value.json"), 275 data_dir_.AppendASCII("read.need_empty_value.json"),
253 pref_file)); 276 pref_file));
254 277
255 // Test that the persistent value can be loaded. 278 // Test that the persistent value can be loaded.
256 ASSERT_TRUE(file_util::PathExists(pref_file)); 279 ASSERT_TRUE(file_util::PathExists(pref_file));
257 scoped_refptr<JsonPrefStore> pref_store = 280 scoped_refptr<JsonPrefStore> pref_store =
258 new JsonPrefStore(pref_file, message_loop_proxy_.get()); 281 JsonPrefStore::Create(
282 pref_file,
283 JsonPrefStore::GetTaskRunnerForFile(pref_file, blocking_pool_));
259 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 284 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
260 ASSERT_FALSE(pref_store->ReadOnly()); 285 ASSERT_FALSE(pref_store->ReadOnly());
261 286
262 // The JSON file looks like this: 287 // The JSON file looks like this:
263 // { 288 // {
264 // "list": [ 1 ], 289 // "list": [ 1 ],
265 // "list_needs_empty_value": [ 2 ], 290 // "list_needs_empty_value": [ 2 ],
266 // "dict": { 291 // "dict": {
267 // "dummy": true, 292 // "dummy": true,
268 // }, 293 // },
269 // "dict_needs_empty_value": { 294 // "dict_needs_empty_value": {
270 // "dummy": true, 295 // "dummy": true,
271 // }, 296 // },
272 // } 297 // }
273 298
274 // Set flag to preserve empty values for the following keys. 299 // Set flag to preserve empty values for the following keys.
275 pref_store->MarkNeedsEmptyValue("list_needs_empty_value"); 300 pref_store->MarkNeedsEmptyValue("list_needs_empty_value");
276 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value"); 301 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value");
277 302
278 // Set all keys to empty values. 303 // Set all keys to empty values.
279 pref_store->SetValue("list", new base::ListValue); 304 pref_store->SetValue("list", new base::ListValue);
280 pref_store->SetValue("list_needs_empty_value", new base::ListValue); 305 pref_store->SetValue("list_needs_empty_value", new base::ListValue);
281 pref_store->SetValue("dict", new base::DictionaryValue); 306 pref_store->SetValue("dict", new base::DictionaryValue);
282 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); 307 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue);
283 308
284 // Write to file. 309 // Write to file.
285 pref_store->CommitPendingWrite(); 310 pref_store->CommitPendingWrite();
286 MessageLoop::current()->RunAllPending(); 311 blocking_pool_->FlushForTesting();
287 312
288 // Compare to expected output. 313 // Compare to expected output.
289 FilePath golden_output_file = 314 FilePath golden_output_file =
290 data_dir_.AppendASCII("write.golden.need_empty_value.json"); 315 data_dir_.AppendASCII("write.golden.need_empty_value.json");
291 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 316 ASSERT_TRUE(file_util::PathExists(golden_output_file));
292 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); 317 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
293 } 318 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698