Give names to all utility processes.
[chromium-blink-merge.git] / chrome / browser / prefs / leveldb_pref_store_unittest.cc
blobdbe29a5f813f9ba02bcc2e9807b0442801f31d3d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/prefs/leveldb_pref_store.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/values.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace {
20 class MockPrefStoreObserver : public PrefStore::Observer {
21 public:
22 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
23 MOCK_METHOD1(OnInitializationCompleted, void(bool));
26 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
27 public:
28 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
31 } // namespace
33 class LevelDBPrefStoreTest : public testing::Test {
34 protected:
35 void SetUp() override {
36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
38 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
39 data_dir_ = data_dir_.AppendASCII("prefs");
40 ASSERT_TRUE(PathExists(data_dir_));
43 void TearDown() override { Close(); }
45 void Open() {
46 pref_store_ = new LevelDBPrefStore(
47 temp_dir_.path(), message_loop_.message_loop_proxy().get());
48 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs());
51 void Close() {
52 pref_store_ = NULL;
53 base::RunLoop().RunUntilIdle();
56 void CloseAndReopen() {
57 Close();
58 Open();
61 // The path to temporary directory used to contain the test operations.
62 base::ScopedTempDir temp_dir_;
63 // The path to the directory where the test data is stored in the source tree.
64 base::FilePath data_dir_;
65 // A message loop that we can use as the file thread message loop.
66 base::MessageLoop message_loop_;
68 scoped_refptr<LevelDBPrefStore> pref_store_;
71 TEST_F(LevelDBPrefStoreTest, PutAndGet) {
72 Open();
73 const std::string key = "some.key";
74 pref_store_->SetValue(key, new base::FundamentalValue(5));
75 base::FundamentalValue orig_value(5);
76 const base::Value* actual_value;
77 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
78 EXPECT_TRUE(orig_value.Equals(actual_value));
81 TEST_F(LevelDBPrefStoreTest, PutAndGetPersistent) {
82 Open();
83 const std::string key = "some.key";
84 pref_store_->SetValue(key, new base::FundamentalValue(5));
86 CloseAndReopen();
87 const base::Value* actual_value = NULL;
88 base::FundamentalValue orig_value(5);
89 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
90 EXPECT_TRUE(orig_value.Equals(actual_value));
93 TEST_F(LevelDBPrefStoreTest, BasicObserver) {
94 scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
95 temp_dir_.path(), message_loop_.message_loop_proxy().get());
96 MockPrefStoreObserver mock_observer;
97 pref_store->AddObserver(&mock_observer);
98 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
99 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
100 testing::Mock::VerifyAndClearExpectations(&mock_observer);
102 const std::string key = "some.key";
103 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
104 pref_store->SetValue(key, new base::FundamentalValue(5));
106 pref_store->RemoveObserver(&mock_observer);
109 TEST_F(LevelDBPrefStoreTest, SetValueSilently) {
110 Open();
112 MockPrefStoreObserver mock_observer;
113 pref_store_->AddObserver(&mock_observer);
114 const std::string key = "some.key";
115 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(0);
116 pref_store_->SetValueSilently(key, new base::FundamentalValue(30));
117 pref_store_->RemoveObserver(&mock_observer);
119 CloseAndReopen();
120 base::FundamentalValue value(30);
121 const base::Value* actual_value = NULL;
122 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
123 EXPECT_TRUE(base::Value::Equals(&value, actual_value));
126 TEST_F(LevelDBPrefStoreTest, GetMutableValue) {
127 Open();
129 const std::string key = "some.key";
130 base::DictionaryValue* orig_value = new base::DictionaryValue;
131 orig_value->SetInteger("key2", 25);
132 pref_store_->SetValue(key, orig_value);
134 base::Value* actual_value;
135 EXPECT_TRUE(pref_store_->GetMutableValue(key, &actual_value));
136 EXPECT_TRUE(orig_value->Equals(actual_value));
137 base::DictionaryValue* dict_value;
138 ASSERT_TRUE(actual_value->GetAsDictionary(&dict_value));
139 dict_value->SetInteger("key2", 30);
140 pref_store_->ReportValueChanged(key);
142 // Ensure the new value is stored in memory.
143 const base::Value* retrieved_value;
144 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
145 scoped_ptr<base::DictionaryValue> golden_value(new base::DictionaryValue);
146 golden_value->SetInteger("key2", 30);
147 EXPECT_TRUE(base::Value::Equals(golden_value.get(), retrieved_value));
149 // Ensure the new value is persisted to disk.
150 CloseAndReopen();
151 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
152 EXPECT_TRUE(base::Value::Equals(golden_value.get(), retrieved_value));
155 TEST_F(LevelDBPrefStoreTest, RemoveFromMemory) {
156 Open();
157 const std::string key = "some.key";
158 pref_store_->SetValue(key, new base::FundamentalValue(5));
160 MockPrefStoreObserver mock_observer;
161 pref_store_->AddObserver(&mock_observer);
162 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
163 pref_store_->RemoveValue(key);
164 pref_store_->RemoveObserver(&mock_observer);
166 const base::Value* retrieved_value;
167 EXPECT_FALSE(pref_store_->GetValue(key, &retrieved_value));
170 TEST_F(LevelDBPrefStoreTest, RemoveFromDisk) {
171 Open();
172 const std::string key = "some.key";
173 pref_store_->SetValue(key, new base::FundamentalValue(5));
175 CloseAndReopen();
177 pref_store_->RemoveValue(key);
179 CloseAndReopen();
181 const base::Value* retrieved_value;
182 EXPECT_FALSE(pref_store_->GetValue(key, &retrieved_value));
185 TEST_F(LevelDBPrefStoreTest, OpenAsync) {
186 // First set a key/value with a synchronous connection.
187 Open();
188 const std::string key = "some.key";
189 pref_store_->SetValue(key, new base::FundamentalValue(5));
190 Close();
192 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore(
193 temp_dir_.path(), message_loop_.message_loop_proxy().get()));
194 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
195 pref_store->ReadPrefsAsync(delegate);
197 MockPrefStoreObserver mock_observer;
198 pref_store->AddObserver(&mock_observer);
199 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
200 base::RunLoop().RunUntilIdle();
201 pref_store->RemoveObserver(&mock_observer);
203 const base::Value* result;
204 EXPECT_TRUE(pref_store->GetValue("some.key", &result));
205 int int_value;
206 EXPECT_TRUE(result->GetAsInteger(&int_value));
207 EXPECT_EQ(5, int_value);
209 pref_store = NULL;
212 TEST_F(LevelDBPrefStoreTest, OpenAsyncError) {
213 // Open a connection that will lock the database.
214 Open();
216 // Try to open an async connection to the same database.
217 scoped_refptr<LevelDBPrefStore> pref_store(new LevelDBPrefStore(
218 temp_dir_.path(), message_loop_.message_loop_proxy().get()));
219 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
220 pref_store->ReadPrefsAsync(delegate);
222 MockPrefStoreObserver mock_observer;
223 pref_store->AddObserver(&mock_observer);
224 EXPECT_CALL(*delegate,
225 OnError(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO))
226 .Times(1);
227 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
228 base::RunLoop().RunUntilIdle();
229 pref_store->RemoveObserver(&mock_observer);
231 EXPECT_TRUE(pref_store->ReadOnly());
232 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_LEVELDB_IO,
233 pref_store->GetReadError());
235 // Sync connection to the database will be closed by the destructor.
238 TEST_F(LevelDBPrefStoreTest, RepairCorrupt) {
239 // Open a database where CURRENT has no newline. Ensure that repair is called
240 // and there is no error reading the database.
241 base::FilePath corrupted_dir = data_dir_.AppendASCII("corrupted_leveldb");
242 base::FilePath dest = temp_dir_.path().AppendASCII("corrupted_leveldb");
243 const bool kRecursive = true;
244 ASSERT_TRUE(CopyDirectory(corrupted_dir, dest, kRecursive));
245 pref_store_ =
246 new LevelDBPrefStore(dest, message_loop_.message_loop_proxy().get());
247 EXPECT_EQ(LevelDBPrefStore::PREF_READ_ERROR_LEVELDB_CORRUPTION,
248 pref_store_->ReadPrefs());
251 TEST_F(LevelDBPrefStoreTest, Values) {
252 Open();
253 pref_store_->SetValue("boolean", new base::FundamentalValue(false));
254 pref_store_->SetValue("integer", new base::FundamentalValue(10));
255 pref_store_->SetValue("double", new base::FundamentalValue(10.3));
256 pref_store_->SetValue("string", new base::StringValue("some string"));
258 base::DictionaryValue* dict_value = new base::DictionaryValue;
259 dict_value->Set("boolean", new base::FundamentalValue(true));
260 scoped_ptr<base::DictionaryValue> golden_dict_value(dict_value->DeepCopy());
261 pref_store_->SetValue("dictionary", dict_value);
263 base::ListValue* list_value = new base::ListValue;
264 list_value->Set(2, new base::StringValue("string in list"));
265 scoped_ptr<base::ListValue> golden_list_value(list_value->DeepCopy());
266 pref_store_->SetValue("list", list_value);
268 // Do something nontrivial as well.
269 base::DictionaryValue* compound_value = new base::DictionaryValue;
270 base::ListValue* outer_list = new base::ListValue;
271 base::ListValue* inner_list = new base::ListValue;
272 inner_list->Set(0, new base::FundamentalValue(5));
273 outer_list->Set(1, inner_list);
274 compound_value->Set("compound_lists", outer_list);
275 scoped_ptr<base::DictionaryValue> golden_compound_value(
276 compound_value->DeepCopy());
277 pref_store_->SetValue("compound_value", compound_value);
279 CloseAndReopen();
281 const base::Value* value;
282 EXPECT_TRUE(pref_store_->GetValue("boolean", &value));
283 EXPECT_TRUE(base::FundamentalValue(false).Equals(value));
285 EXPECT_TRUE(pref_store_->GetValue("integer", &value));
286 EXPECT_TRUE(base::FundamentalValue(10).Equals(value));
288 EXPECT_TRUE(pref_store_->GetValue("double", &value));
289 EXPECT_TRUE(base::FundamentalValue(10.3).Equals(value));
291 EXPECT_TRUE(pref_store_->GetValue("string", &value));
292 EXPECT_TRUE(base::StringValue("some string").Equals(value));
294 EXPECT_TRUE(pref_store_->GetValue("dictionary", &value));
295 EXPECT_TRUE(base::Value::Equals(golden_dict_value.get(), value));
297 EXPECT_TRUE(pref_store_->GetValue("list", &value));
298 EXPECT_TRUE(base::Value::Equals(golden_list_value.get(), value));
300 EXPECT_TRUE(pref_store_->GetValue("compound_value", &value));
301 EXPECT_TRUE(base::Value::Equals(golden_compound_value.get(), value));