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

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

Issue 25428002: LevelDBPrefStore Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: latest 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
« no previous file with comments | « base/prefs/leveldb_pref_store.cc ('k') | base/prefs/migration_pref_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/path_service.h"
12 #include "base/prefs/pref_filter.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/sequenced_worker_pool.h"
18 #include "base/threading/thread.h"
19 #include "base/values.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace base {
24 namespace {
25
26 class MockPrefStoreObserver : public PrefStore::Observer {
27 public:
28 MOCK_METHOD1(OnPrefValueChanged, void(const std::string&));
29 MOCK_METHOD1(OnInitializationCompleted, void(bool));
30 };
31
32 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
33 public:
34 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
35 };
36
37 } // namespace
38
39 class LevelDBPrefStoreTest : public testing::Test {
40 protected:
41 virtual void SetUp() OVERRIDE {
42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
43
44 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
45 data_dir_ = data_dir_.AppendASCII("prefs");
46 ASSERT_TRUE(PathExists(data_dir_));
47 }
48
49 void Open() {
50 pref_store_ = new LevelDBPrefStore(
51 temp_dir_.path(), message_loop_.message_loop_proxy().get(),
52 scoped_ptr<PrefFilter>());
53 DCHECK_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs());
54 }
55
56 void CloseAndReopen() {
57 pref_store_ = NULL;
58 Open();
59 }
60
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 MessageLoop message_loop_;
67
68 scoped_refptr<LevelDBPrefStore> pref_store_;
69 };
70
71 TEST_F(LevelDBPrefStoreTest, PutAndGet) {
72 Open();
73 const std::string key = "some.key";
74 base::Value* value = new FundamentalValue(5);
75 pref_store_->SetValue(key, value);
76 RunLoop().RunUntilIdle();
77 const base::Value* actual_value;
78 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
79 EXPECT_TRUE(value->Equals(actual_value));
80 }
81
82 TEST_F(LevelDBPrefStoreTest, PutAndGetPersistent) {
83 Open();
84 const std::string key = "some.key";
85 base::Value* value = new FundamentalValue(5);
86 pref_store_->SetValue(key, value);
87 RunLoop().RunUntilIdle();
88
89 CloseAndReopen();
90 const base::Value* actual_value = NULL;
91 value = new FundamentalValue(5);
92 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
93 EXPECT_TRUE(base::Value::Equals(value, actual_value));
94 }
95
96 TEST_F(LevelDBPrefStoreTest, BasicObserver) {
97 scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
98 temp_dir_.path(), message_loop_.message_loop_proxy().get(),
99 scoped_ptr<PrefFilter>());
100 MockPrefStoreObserver mock_observer;
101 pref_store->AddObserver(&mock_observer);
102 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
103 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
104
105 const std::string key = "some.key";
106 base::Value* value = new FundamentalValue(5);
107 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
108 pref_store->SetValue(key, value);
109
110 pref_store->RemoveObserver(&mock_observer);
111 }
112
113 TEST_F(LevelDBPrefStoreTest, SetValueSilently) {
114 Open();
115
116 MockPrefStoreObserver mock_observer;
117 pref_store_->AddObserver(&mock_observer);
118 const std::string key = "some.key";
119 base::Value* value = new FundamentalValue(30);
120 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(0);
121 pref_store_->SetValueSilently(key, value);
122 RunLoop().RunUntilIdle();
123 pref_store_->RemoveObserver(&mock_observer);
124
125 CloseAndReopen();
126 value = new FundamentalValue(30);
127 const base::Value* actual_value = NULL;
128 EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
129 EXPECT_TRUE(base::Value::Equals(value, actual_value));
130 }
131
132 TEST_F(LevelDBPrefStoreTest, GetMutableValue) {
133 Open();
134
135 const std::string key = "some.key";
136 base::DictionaryValue* orig_value = new DictionaryValue;
137 orig_value->SetInteger("key2", 25);
138 pref_store_->SetValue(key, orig_value);
139 base::Value* actual_value;
140
141 EXPECT_TRUE(pref_store_->GetMutableValue(key, &actual_value));
142 EXPECT_TRUE(orig_value->Equals(actual_value));
143 base::DictionaryValue* dict_value =
144 static_cast<base::DictionaryValue*>(actual_value);
145 dict_value->SetInteger("key2", 30);
146 pref_store_->ReportValueChanged(key);
147 RunLoop().RunUntilIdle();
148
149 // Ensure the new value is stored in memory.
150 const base::Value* retrieved_value;
151 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
152 const base::Value* inner_value;
153 EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
154 ->Get("key2", &inner_value));
155 const base::FundamentalValue* inner_fundamental_value =
156 static_cast<const base::FundamentalValue*>(inner_value);
157 int inner_integer;
158 EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
159 EXPECT_EQ(30, inner_integer);
160
161 // Ensure the new value is persisted to disk.
162 CloseAndReopen();
163 EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
164 EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
165 ->Get("key2", &inner_value));
166 inner_fundamental_value =
167 static_cast<const base::FundamentalValue*>(inner_value);
168 EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
169 EXPECT_EQ(30, inner_integer);
170 }
171
172 TEST_F(LevelDBPrefStoreTest, Remove) {
173 Open();
174 const std::string key = "some.key";
175 base::Value* orig_value = new FundamentalValue(5);
176 pref_store_->SetValue(key, orig_value);
177 MockPrefStoreObserver mock_observer;
178 pref_store_->AddObserver(&mock_observer);
179 EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
180 pref_store_->RemoveValue(key);
181 pref_store_->RemoveObserver(&mock_observer);
182 RunLoop().RunUntilIdle();
183
184 CloseAndReopen();
185 const base::Value* retrieved_value;
186 EXPECT_FALSE(pref_store_->GetValue(key, &retrieved_value));
187 }
188
189 TEST_F(LevelDBPrefStoreTest, OpenAsync) {
190 pref_store_ = new LevelDBPrefStore(
191 temp_dir_.path(), message_loop_.message_loop_proxy().get(),
192 scoped_ptr<PrefFilter>());
193 MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
194 pref_store_->ReadPrefsAsync(delegate);
195 EXPECT_CALL(*delegate,
196 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(0);
197 MockPrefStoreObserver mock_observer;
198 pref_store_->AddObserver(&mock_observer);
199 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
200 RunLoop().RunUntilIdle();
201 pref_store_->RemoveObserver(&mock_observer);
202 }
203
204 // Test fallback behavior for an invalid file.
205 //TEST_F(LevelDBPrefStoreTest, InvalidFile) {
206 // base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json") ;
207 // base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
208 // ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file));
209 // scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
210 // invalid_file, message_loop_.message_loop_proxy().get());
211 // EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
212 // pref_store->ReadPrefs());
213 // EXPECT_FALSE(pref_store->ReadOnly());
214 //
215 // // The file should have been moved aside.
216 // EXPECT_FALSE(PathExists(invalid_file));
217 // base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
218 // EXPECT_TRUE(PathExists(moved_aside));
219 // EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside));
220 //}
221 //
222 //// Tests asynchronous reading of the file when there is no file.
223 //TEST_F(LevelDBPrefStoreTest, AsyncNonExistingFile) {
224 // base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
225 // ASSERT_FALSE(PathExists(bogus_input_file));
226 // scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
227 // bogus_input_file, message_loop_.message_loop_proxy().get());
228 // MockPrefStoreObserver mock_observer;
229 // pref_store->AddObserver(&mock_observer);
230 //
231 // MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
232 // pref_store->ReadPrefsAsync(mock_error_delegate);
233 //
234 // EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
235 // EXPECT_CALL(*mock_error_delegate,
236 // OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
237 // RunLoop().RunUntilIdle();
238 // pref_store->RemoveObserver(&mock_observer);
239 //
240 // EXPECT_FALSE(pref_store->ReadOnly());
241 //}
242
243 } // namespace base
OLDNEW
« no previous file with comments | « base/prefs/leveldb_pref_store.cc ('k') | base/prefs/migration_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698