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

Side by Side Diff: chrome/browser/prefs/leveldb_pref_store_unittest.cc

Issue 1143343005: chrome/browser: Remove use of MessageLoopProxy and deprecated MessageLoop APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "chrome/browser/prefs/leveldb_pref_store.h" 5 #include "chrome/browser/prefs/leveldb_pref_store.h"
6 6
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h" 10 #include "base/path_service.h"
12 #include "base/run_loop.h" 11 #include "base/run_loop.h"
13 #include "base/values.h" 12 #include "base/values.h"
14 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/chrome_paths.h"
15 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
17 16
18 namespace { 17 namespace {
19 18
20 class MockPrefStoreObserver : public PrefStore::Observer { 19 class MockPrefStoreObserver : public PrefStore::Observer {
(...skipping 15 matching lines...) Expand all
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 35 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
37 36
38 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); 37 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
39 data_dir_ = data_dir_.AppendASCII("prefs"); 38 data_dir_ = data_dir_.AppendASCII("prefs");
40 ASSERT_TRUE(PathExists(data_dir_)); 39 ASSERT_TRUE(PathExists(data_dir_));
41 } 40 }
42 41
43 void TearDown() override { Close(); } 42 void TearDown() override { Close(); }
44 43
45 void Open() { 44 void Open() {
46 pref_store_ = new LevelDBPrefStore( 45 pref_store_ = new LevelDBPrefStore(temp_dir_.path(),
47 temp_dir_.path(), message_loop_.message_loop_proxy().get()); 46 message_loop_.task_runner().get());
48 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs()); 47 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs());
49 } 48 }
50 49
51 void Close() { 50 void Close() {
52 pref_store_ = NULL; 51 pref_store_ = NULL;
53 base::RunLoop().RunUntilIdle(); 52 base::RunLoop().RunUntilIdle();
54 } 53 }
55 54
56 void CloseAndReopen() { 55 void CloseAndReopen() {
57 Close(); 56 Close();
(...skipping 28 matching lines...) Expand all
86 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 85 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
87 86
88 CloseAndReopen(); 87 CloseAndReopen();
89 const base::Value* actual_value = NULL; 88 const base::Value* actual_value = NULL;
90 base::FundamentalValue orig_value(5); 89 base::FundamentalValue orig_value(5);
91 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value)); 90 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
92 EXPECT_TRUE(orig_value.Equals(actual_value)); 91 EXPECT_TRUE(orig_value.Equals(actual_value));
93 } 92 }
94 93
95 TEST_F(LevelDBPrefStoreTest, BasicObserver) { 94 TEST_F(LevelDBPrefStoreTest, BasicObserver) {
96 scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore( 95 scoped_refptr<LevelDBPrefStore> pref_store =
97 temp_dir_.path(), message_loop_.message_loop_proxy().get()); 96 new LevelDBPrefStore(temp_dir_.path(), message_loop_.task_runner().get());
98 MockPrefStoreObserver mock_observer; 97 MockPrefStoreObserver mock_observer;
99 pref_store->AddObserver(&mock_observer); 98 pref_store->AddObserver(&mock_observer);
100 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 99 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
101 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 100 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
102 testing::Mock::VerifyAndClearExpectations(&mock_observer); 101 testing::Mock::VerifyAndClearExpectations(&mock_observer);
103 102
104 const std::string key = "some.key"; 103 const std::string key = "some.key";
105 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1); 104 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
106 pref_store->SetValue(key, new base::FundamentalValue(5), 105 pref_store->SetValue(key, new base::FundamentalValue(5),
107 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 106 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 191
193 TEST_F(LevelDBPrefStoreTest, OpenAsync) { 192 TEST_F(LevelDBPrefStoreTest, OpenAsync) {
194 // First set a key/value with a synchronous connection. 193 // First set a key/value with a synchronous connection.
195 Open(); 194 Open();
196 const std::string key = "some.key"; 195 const std::string key = "some.key";
197 pref_store_->SetValue(key, new base::FundamentalValue(5), 196 pref_store_->SetValue(key, new base::FundamentalValue(5),
198 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 197 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
199 Close(); 198 Close();
200 199
201 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore( 200 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore(
202 temp_dir_.path(), message_loop_.message_loop_proxy().get())); 201 temp_dir_.path(), message_loop_.task_runner().get()));
203 MockReadErrorDelegate* delegate = new MockReadErrorDelegate; 202 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
204 pref_store->ReadPrefsAsync(delegate); 203 pref_store->ReadPrefsAsync(delegate);
205 204
206 MockPrefStoreObserver mock_observer; 205 MockPrefStoreObserver mock_observer;
207 pref_store->AddObserver(&mock_observer); 206 pref_store->AddObserver(&mock_observer);
208 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 207 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
209 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
210 pref_store->RemoveObserver(&mock_observer); 209 pref_store->RemoveObserver(&mock_observer);
211 210
212 const base::Value* result; 211 const base::Value* result;
213 EXPECT_TRUE(pref_store->GetValue("some.key", &result)); 212 EXPECT_TRUE(pref_store->GetValue("some.key", &result));
214 int int_value; 213 int int_value;
215 EXPECT_TRUE(result->GetAsInteger(&int_value)); 214 EXPECT_TRUE(result->GetAsInteger(&int_value));
216 EXPECT_EQ(5, int_value); 215 EXPECT_EQ(5, int_value);
217 216
218 pref_store = NULL; 217 pref_store = NULL;
219 } 218 }
220 219
221 TEST_F(LevelDBPrefStoreTest, OpenAsyncError) { 220 TEST_F(LevelDBPrefStoreTest, OpenAsyncError) {
222 // Open a connection that will lock the database. 221 // Open a connection that will lock the database.
223 Open(); 222 Open();
224 223
225 // Try to open an async connection to the same database. 224 // Try to open an async connection to the same database.
226 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore( 225 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore(
227 temp_dir_.path(), message_loop_.message_loop_proxy().get())); 226 temp_dir_.path(), message_loop_.task_runner().get()));
228 MockReadErrorDelegate* delegate = new MockReadErrorDelegate; 227 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
229 pref_store->ReadPrefsAsync(delegate); 228 pref_store->ReadPrefsAsync(delegate);
230 229
231 MockPrefStoreObserver mock_observer; 230 MockPrefStoreObserver mock_observer;
232 pref_store->AddObserver(&mock_observer); 231 pref_store->AddObserver(&mock_observer);
233 EXPECT_CALL(*delegate, 232 EXPECT_CALL(*delegate,
234 OnError(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO)) 233 OnError(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO))
235 .Times(1); 234 .Times(1);
236 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1); 235 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
237 base::RunLoop().RunUntilIdle(); 236 base::RunLoop().RunUntilIdle();
238 pref_store->RemoveObserver(&mock_observer); 237 pref_store->RemoveObserver(&mock_observer);
239 238
240 EXPECT_TRUE(pref_store->ReadOnly()); 239 EXPECT_TRUE(pref_store->ReadOnly());
241 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO, 240 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO,
242 pref_store->GetReadError()); 241 pref_store->GetReadError());
243 242
244 // Sync connection to the database will be closed by the destructor. 243 // Sync connection to the database will be closed by the destructor.
245 } 244 }
246 245
247 TEST_F(LevelDBPrefStoreTest, RepairCorrupt) { 246 TEST_F(LevelDBPrefStoreTest, RepairCorrupt) {
248 // Open a database where CURRENT has no newline. Ensure that repair is called 247 // Open a database where CURRENT has no newline. Ensure that repair is called
249 // and there is no error reading the database. 248 // and there is no error reading the database.
250 base::FilePath corrupted_dir = data_dir_.AppendASCII("corrupted_leveldb"); 249 base::FilePath corrupted_dir = data_dir_.AppendASCII("corrupted_leveldb");
251 base::FilePath dest = temp_dir_.path().AppendASCII("corrupted_leveldb"); 250 base::FilePath dest = temp_dir_.path().AppendASCII("corrupted_leveldb");
252 const bool kRecursive = true; 251 const bool kRecursive = true;
253 ASSERT_TRUE(CopyDirectory(corrupted_dir, dest, kRecursive)); 252 ASSERT_TRUE(CopyDirectory(corrupted_dir, dest, kRecursive));
254 pref_store_ = 253 pref_store_ = new LevelDBPrefStore(dest, message_loop_.task_runner().get());
255 new LevelDBPrefStore(dest, message_loop_.message_loop_proxy().get());
256 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_LEVELDB_CORRUPTION, 254 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_LEVELDB_CORRUPTION,
257 pref_store_->ReadPrefs()); 255 pref_store_->ReadPrefs());
258 } 256 }
259 257
260 TEST_F(LevelDBPrefStoreTest, Values) { 258 TEST_F(LevelDBPrefStoreTest, Values) {
261 Open(); 259 Open();
262 pref_store_->SetValue("boolean", new base::FundamentalValue(false), 260 pref_store_->SetValue("boolean", new base::FundamentalValue(false),
263 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 261 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
264 pref_store_->SetValue("integer", new base::FundamentalValue(10), 262 pref_store_->SetValue("integer", new base::FundamentalValue(10),
265 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); 263 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 307
310 EXPECT_TRUE(pref_store_->GetValue("dictionary", &value)); 308 EXPECT_TRUE(pref_store_->GetValue("dictionary", &value));
311 EXPECT_TRUE(base::Value::Equals(golden_dict_value.get(), value)); 309 EXPECT_TRUE(base::Value::Equals(golden_dict_value.get(), value));
312 310
313 EXPECT_TRUE(pref_store_->GetValue("list", &value)); 311 EXPECT_TRUE(pref_store_->GetValue("list", &value));
314 EXPECT_TRUE(base::Value::Equals(golden_list_value.get(), value)); 312 EXPECT_TRUE(base::Value::Equals(golden_list_value.get(), value));
315 313
316 EXPECT_TRUE(pref_store_->GetValue("compound_value", &value)); 314 EXPECT_TRUE(pref_store_->GetValue("compound_value", &value));
317 EXPECT_TRUE(base::Value::Equals(golden_compound_value.get(), value)); 315 EXPECT_TRUE(base::Value::Equals(golden_compound_value.get(), value));
318 } 316 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/leveldb_pref_store.h ('k') | chrome/browser/prefs/profile_pref_store_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698