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

Side by Side Diff: base/prefs/leveldb_pref_store_unittest.cc

Issue 169323003: Implementation of leveldb-backed PrefStore (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Include most of a PersistentPrefStore implementation Created 6 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/prefs/leveldb_pref_store.h"
6
7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/prefs/pref_filter.h"
14 #include "base/run_loop.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19
20 namespace base {
21 namespace {
22
23 class MockPrefStoreObserver : public PrefStore::Observer {
24 public:
25 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
26 MOCK_METHOD1(OnInitializationCompleted, void(bool));
27 };
28
29 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
30 public:
31 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
32 };
33
34 } // namespace
35
36 class LevelDBPrefStoreTest : public testing::Test {
37 protected:
38 virtual void SetUp() OVERRIDE {
39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
40
41 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
42 data_dir_ = data_dir_.AppendASCII("prefs");
43 ASSERT_TRUE(PathExists(data_dir_));
44 }
45
46 void Open() {
47 pref_store_ = new LevelDBPrefStore(
48 temp_dir_.path(), message_loop_.message_loop_proxy().get());
49 DCHECK_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs());
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 This should probably be an EXPECT_EQ or ASSERT_EQ?
dgrogan 2014/03/05 03:06:58 Done.
50 }
51
52 void CloseAndReopen() {
53 pref_store_ = NULL;
54 Open();
55 }
56
57 // The path to temporary directory used to contain the test operations.
58 base::ScopedTempDir temp_dir_;
59 // The path to the directory where the test data is stored in the source tree.
60 base::FilePath data_dir_;
61 // A message loop that we can use as the file thread message loop.
62 MessageLoop message_loop_;
63
64 scoped_refptr<LevelDBPrefStore> pref_store_;
65 };
66
67 TEST_F(LevelDBPrefStoreTest, PutAndGet) {
68 Open();
69 const std::string key = "some.key";
70 base::Value* value = new FundamentalValue(5);
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 nit: no need for the |value| variable, just inline
dgrogan 2014/03/05 03:06:58 Done.
71 pref_store_->SetValue(key, value);
72 RunLoop().RunUntilIdle();
73 FundamentalValue orig_value(5);
74 const base::Value* actual_value;
75 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
76 EXPECT_TRUE(orig_value.Equals(actual_value));
77 }
78
79 TEST_F(LevelDBPrefStoreTest, PutAndGetPersistent) {
80 Open();
81 const std::string key = "some.key";
82 base::Value* value = new FundamentalValue(5);
83 pref_store_->SetValue(key, value);
84 RunLoop().RunUntilIdle();
85
86 CloseAndReopen();
87 const base::Value* actual_value = NULL;
88 FundamentalValue orig_value(5);
89 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
90 EXPECT_TRUE(orig_value.Equals(actual_value));
91 }
92
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
101 const std::string key = "some.key";
102 base::Value* value = new FundamentalValue(5);
103 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 The gmock docs say: Important note: Google Mock r
dgrogan 2014/03/05 03:06:58 Thanks, done.
104 pref_store->SetValue(key, value);
105
106 pref_store->RemoveObserver(&mock_observer);
107 }
108
109 TEST_F(LevelDBPrefStoreTest, SetValueSilently) {
110 Open();
111
112 MockPrefStoreObserver mock_observer;
113 pref_store_->AddObserver(&mock_observer);
114 const std::string key = "some.key";
115 base::Value* value = new FundamentalValue(30);
116 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(0);
117 pref_store_->SetValueSilently(key, value);
118 RunLoop().RunUntilIdle();
119 pref_store_->RemoveObserver(&mock_observer);
120
121 CloseAndReopen();
122 value = new FundamentalValue(30);
123 const base::Value* actual_value = NULL;
124 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
125 EXPECT_TRUE(base::Value::Equals(value, actual_value));
126 }
127
128 TEST_F(LevelDBPrefStoreTest, GetMutableValue) {
129 Open();
130
131 const std::string key = "some.key";
132 base::DictionaryValue* orig_value = new DictionaryValue;
133 orig_value->SetInteger("key2", 25);
134 pref_store_->SetValue(key, orig_value);
135 base::Value* actual_value;
136
137 EXPECT_TRUE(pref_store_->GetMutableValue(key, &actual_value));
138 EXPECT_TRUE(orig_value->Equals(actual_value));
139 base::DictionaryValue* dict_value =
140 static_cast<base::DictionaryValue*>(actual_value);
141 dict_value->SetInteger("key2", 30);
142 pref_store_->ReportValueChanged(key);
143 RunLoop().RunUntilIdle();
144
145 // Ensure the new value is stored in memory.
146 const base::Value* retrieved_value;
147 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
148 const base::Value* inner_value;
149 EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
150 ->Get("key2", &inner_value));
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 The canonical way to do this is to call GetAsDicti
dgrogan 2014/03/05 03:06:58 Done.
151 const base::FundamentalValue* inner_fundamental_value =
152 static_cast<const base::FundamentalValue*>(inner_value);
153 int inner_integer;
154 EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
155 EXPECT_EQ(30, inner_integer);
156
157 // Ensure the new value is persisted to disk.
158 CloseAndReopen();
159 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
160 EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
161 ->Get("key2", &inner_value));
162 inner_fundamental_value =
163 static_cast<const base::FundamentalValue*>(inner_value);
164 EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 same comments regarding reading the integer here.
dgrogan 2014/03/05 03:06:58 Done.
165 EXPECT_EQ(30, inner_integer);
166 }
167
168 TEST_F(LevelDBPrefStoreTest, Remove) {
169 Open();
170 const std::string key = "some.key";
171 base::Value* orig_value = new FundamentalValue(5);
172 pref_store_->SetValue(key, orig_value);
173 MockPrefStoreObserver mock_observer;
174 pref_store_->AddObserver(&mock_observer);
175 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
176 pref_store_->RemoveValue(key);
177 pref_store_->RemoveObserver(&mock_observer);
178 RunLoop().RunUntilIdle();
179
180 CloseAndReopen();
181 const base::Value* retrieved_value;
182 EXPECT_FALSE(pref_store_->GetValue(key, &retrieved_value));
183 }
184
185 TEST_F(LevelDBPrefStoreTest, OpenAsync) {
186 pref_store_ = new LevelDBPrefStore(
187 temp_dir_.path(), message_loop_.message_loop_proxy().get());
188 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
189 pref_store_->ReadPrefsAsync(delegate);
190 EXPECT_CALL(*delegate,
191 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(0);
192 MockPrefStoreObserver mock_observer;
193 pref_store_->AddObserver(&mock_observer);
194 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
195 RunLoop().RunUntilIdle();
196 pref_store_->RemoveObserver(&mock_observer);
197 }
198
Mattias Nissler (ping if slow) 2014/02/25 10:51:44 Another test that makes sure all different Value t
dgrogan 2014/03/20 00:25:37 Added.
199 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698