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

Side by Side Diff: components/leveldb_proto/proto_database_impl_unittest.cc

Issue 330833002: Extract protobuf database into a new 'leveldb_proto' component (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix test Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 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 "components/dom_distiller/core/dom_distiller_database.h" 5 #include "components/leveldb_proto/proto_database_impl.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/threading/thread.h" 13 #include "base/threading/thread.h"
14 #include "components/dom_distiller/core/article_entry.h" 14 #include "components/leveldb_proto/leveldb_database.h"
15 #include "components/leveldb_proto/testing/proto/test.pb.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 using base::MessageLoop; 19 using base::MessageLoop;
19 using base::ScopedTempDir; 20 using base::ScopedTempDir;
20 using testing::Invoke; 21 using testing::Invoke;
21 using testing::Return; 22 using testing::Return;
22 using testing::_; 23 using testing::_;
23 24
24 namespace dom_distiller { 25 namespace leveldb_proto {
25 26
26 namespace { 27 namespace {
27 28
28 typedef std::map<std::string, ArticleEntry> EntryMap; 29 typedef std::map<std::string, TestProto> EntryMap;
29 30
30 class MockDB : public DomDistillerDatabase::Database { 31 class MockDB : public LevelDB {
31 public: 32 public:
32 MOCK_METHOD1(Init, bool(const base::FilePath&)); 33 MOCK_METHOD1(Init, bool(const base::FilePath&));
33 MOCK_METHOD2(Save, bool(const EntryVector&, const EntryVector&)); 34 MOCK_METHOD2(Save, bool(const KeyValueVector&, const KeyVector&));
34 MOCK_METHOD1(Load, bool(EntryVector*)); 35 MOCK_METHOD1(Load, bool(std::vector<std::string>*));
35 36
36 MockDB() { 37 MockDB() {
37 ON_CALL(*this, Init(_)).WillByDefault(Return(true)); 38 ON_CALL(*this, Init(_)).WillByDefault(Return(true));
38 ON_CALL(*this, Save(_, _)).WillByDefault(Return(true)); 39 ON_CALL(*this, Save(_, _)).WillByDefault(Return(true));
39 ON_CALL(*this, Load(_)).WillByDefault(Return(true)); 40 ON_CALL(*this, Load(_)).WillByDefault(Return(true));
40 } 41 }
41
42 bool LoadEntries(EntryVector* entries);
43 }; 42 };
44 43
45 class MockDatabaseCaller { 44 class MockDatabaseCaller {
46 public: 45 public:
47 MOCK_METHOD1(InitCallback, void(bool)); 46 MOCK_METHOD1(InitCallback, void(bool));
48 MOCK_METHOD1(SaveCallback, void(bool)); 47 MOCK_METHOD1(SaveCallback, void(bool));
49 void LoadCallback(bool success, scoped_ptr<EntryVector> entries) { 48 void LoadCallback(bool success, scoped_ptr<std::vector<TestProto> > entries) {
50 LoadCallback1(success, entries.get()); 49 LoadCallback1(success, entries.get());
51 } 50 }
52 MOCK_METHOD2(LoadCallback1, void(bool, EntryVector*)); 51 MOCK_METHOD2(LoadCallback1, void(bool, std::vector<TestProto>*));
53 }; 52 };
54 53
55 } // namespace 54 } // namespace
56 55
57 EntryMap GetSmallModel() { 56 EntryMap GetSmallModel() {
58 EntryMap model; 57 EntryMap model;
59 58
60 model["key0"].set_entry_id("key0"); 59 model["0"].set_id("0");
61 model["key0"].add_pages()->set_url("http://foo.com/1"); 60 model["0"].set_data("http://foo.com/1");
62 model["key0"].add_pages()->set_url("http://foo.com/2");
63 model["key0"].add_pages()->set_url("http://foo.com/3");
64 61
65 model["key1"].set_entry_id("key1"); 62 model["1"].set_id("1");
66 model["key1"].add_pages()->set_url("http://bar.com/all"); 63 model["1"].set_data("http://bar.com/all");
67 64
68 model["key2"].set_entry_id("key2"); 65 model["2"].set_id("2");
69 model["key2"].add_pages()->set_url("http://baz.com/1"); 66 model["2"].set_data("http://baz.com/1");
70 67
71 return model; 68 return model;
72 } 69 }
73 70
74 void ExpectEntryPointersEquals(EntryMap expected, const EntryVector& actual) { 71 void ExpectEntryPointersEquals(EntryMap expected,
72 const std::vector<TestProto>& actual) {
75 EXPECT_EQ(expected.size(), actual.size()); 73 EXPECT_EQ(expected.size(), actual.size());
76 for (size_t i = 0; i < actual.size(); i++) { 74 for (size_t i = 0; i < actual.size(); i++) {
77 EntryMap::iterator expected_it = 75 EntryMap::iterator expected_it = expected.find(actual[i].id());
78 expected.find(std::string(actual[i].entry_id()));
79 EXPECT_TRUE(expected_it != expected.end()); 76 EXPECT_TRUE(expected_it != expected.end());
80 std::string serialized_expected = expected_it->second.SerializeAsString(); 77 std::string serialized_expected = expected_it->second.SerializeAsString();
81 std::string serialized_actual = actual[i].SerializeAsString(); 78 std::string serialized_actual = actual[i].SerializeAsString();
82 EXPECT_EQ(serialized_expected, serialized_actual); 79 EXPECT_EQ(serialized_expected, serialized_actual);
83 expected.erase(expected_it); 80 expected.erase(expected_it);
84 } 81 }
85 } 82 }
86 83
87 class DomDistillerDatabaseTest : public testing::Test { 84 class ProtoDatabaseImplTest : public testing::Test {
88 public: 85 public:
89 virtual void SetUp() { 86 virtual void SetUp() {
90 main_loop_.reset(new MessageLoop()); 87 main_loop_.reset(new MessageLoop());
91 db_.reset(new DomDistillerDatabase(main_loop_->message_loop_proxy())); 88 db_.reset(
89 new ProtoDatabaseImpl<TestProto>(main_loop_->message_loop_proxy()));
92 } 90 }
93 91
94 virtual void TearDown() { 92 virtual void TearDown() {
95 db_.reset(); 93 db_.reset();
96 base::RunLoop().RunUntilIdle(); 94 base::RunLoop().RunUntilIdle();
97 main_loop_.reset(); 95 main_loop_.reset();
98 } 96 }
99 97
100 scoped_ptr<DomDistillerDatabase> db_; 98 scoped_ptr<ProtoDatabaseImpl<TestProto> > db_;
101 scoped_ptr<MessageLoop> main_loop_; 99 scoped_ptr<MessageLoop> main_loop_;
102 }; 100 };
103 101
104 // Test that DomDistillerDatabase calls Init on the underlying database and that 102 // Test that ProtoDatabaseImpl calls Init on the underlying database and that
105 // the caller's InitCallback is called with the correct value. 103 // the caller's InitCallback is called with the correct value.
106 TEST_F(DomDistillerDatabaseTest, TestDBInitSuccess) { 104 TEST_F(ProtoDatabaseImplTest, TestDBInitSuccess) {
107 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 105 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
108 106
109 MockDB* mock_db = new MockDB(); 107 MockDB* mock_db = new MockDB();
110 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true)); 108 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(true));
111 109
112 MockDatabaseCaller caller; 110 MockDatabaseCaller caller;
113 EXPECT_CALL(caller, InitCallback(true)); 111 EXPECT_CALL(caller, InitCallback(true));
114 112
115 db_->InitWithDatabase( 113 db_->InitWithDatabase(
116 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 114 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
117 base::FilePath(path),
118 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 115 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
119 116
120 base::RunLoop().RunUntilIdle(); 117 base::RunLoop().RunUntilIdle();
121 } 118 }
122 119
123 TEST_F(DomDistillerDatabaseTest, TestDBInitFailure) { 120 TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) {
124 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 121 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
125 122
126 MockDB* mock_db = new MockDB(); 123 MockDB* mock_db = new MockDB();
127 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(false)); 124 EXPECT_CALL(*mock_db, Init(path)).WillOnce(Return(false));
128 125
129 MockDatabaseCaller caller; 126 MockDatabaseCaller caller;
130 EXPECT_CALL(caller, InitCallback(false)); 127 EXPECT_CALL(caller, InitCallback(false));
131 128
132 db_->InitWithDatabase( 129 db_->InitWithDatabase(
133 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 130 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
134 base::FilePath(path),
135 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 131 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
136 132
137 base::RunLoop().RunUntilIdle(); 133 base::RunLoop().RunUntilIdle();
138 } 134 }
139 135
140 ACTION_P(AppendLoadEntries, model) { 136 ACTION_P(AppendLoadEntries, model) {
141 EntryVector* output = arg0; 137 std::vector<std::string>* output = arg0;
142 for (EntryMap::const_iterator it = model.begin(); it != model.end(); ++it) { 138 for (EntryMap::const_iterator it = model.begin(); it != model.end(); ++it) {
143 output->push_back(it->second); 139 output->push_back(it->second.SerializeAsString());
144 } 140 }
145 return true; 141 return true;
146 } 142 }
147 143
148 ACTION_P(VerifyLoadEntries, expected) { 144 ACTION_P(VerifyLoadEntries, expected) {
149 EntryVector* actual = arg1; 145 std::vector<TestProto>* actual = arg1;
150 ExpectEntryPointersEquals(expected, *actual); 146 ExpectEntryPointersEquals(expected, *actual);
151 } 147 }
152 148
153 // Test that DomDistillerDatabase calls Load on the underlying database and that 149 // Test that ProtoDatabaseImpl calls Load on the underlying database and that
154 // the caller's LoadCallback is called with the correct success value. Also 150 // the caller's LoadCallback is called with the correct success value. Also
155 // confirms that on success, the expected entries are passed to the caller's 151 // confirms that on success, the expected entries are passed to the caller's
156 // LoadCallback. 152 // LoadCallback.
157 TEST_F(DomDistillerDatabaseTest, TestDBLoadSuccess) { 153 TEST_F(ProtoDatabaseImplTest, TestDBLoadSuccess) {
158 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 154 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
159 155
160 MockDB* mock_db = new MockDB(); 156 MockDB* mock_db = new MockDB();
161 MockDatabaseCaller caller; 157 MockDatabaseCaller caller;
162 EntryMap model = GetSmallModel(); 158 EntryMap model = GetSmallModel();
163 159
164 EXPECT_CALL(*mock_db, Init(_)); 160 EXPECT_CALL(*mock_db, Init(_));
165 EXPECT_CALL(caller, InitCallback(_)); 161 EXPECT_CALL(caller, InitCallback(_));
166 db_->InitWithDatabase( 162 db_->InitWithDatabase(
167 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 163 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
168 base::FilePath(path),
169 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 164 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
170 165
171 EXPECT_CALL(*mock_db, Load(_)).WillOnce(AppendLoadEntries(model)); 166 EXPECT_CALL(*mock_db, Load(_)).WillOnce(AppendLoadEntries(model));
172 EXPECT_CALL(caller, LoadCallback1(true, _)) 167 EXPECT_CALL(caller, LoadCallback1(true, _))
173 .WillOnce(VerifyLoadEntries(testing::ByRef(model))); 168 .WillOnce(VerifyLoadEntries(testing::ByRef(model)));
174 db_->LoadEntries( 169 db_->LoadEntries(
175 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); 170 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller)));
176 171
177 base::RunLoop().RunUntilIdle(); 172 base::RunLoop().RunUntilIdle();
178 } 173 }
179 174
180 TEST_F(DomDistillerDatabaseTest, TestDBLoadFailure) { 175 TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) {
181 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 176 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
182 177
183 MockDB* mock_db = new MockDB(); 178 MockDB* mock_db = new MockDB();
184 MockDatabaseCaller caller; 179 MockDatabaseCaller caller;
185 180
186 EXPECT_CALL(*mock_db, Init(_)); 181 EXPECT_CALL(*mock_db, Init(_));
187 EXPECT_CALL(caller, InitCallback(_)); 182 EXPECT_CALL(caller, InitCallback(_));
188 db_->InitWithDatabase( 183 db_->InitWithDatabase(
189 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 184 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
190 base::FilePath(path),
191 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 185 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
192 186
193 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false)); 187 EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false));
194 EXPECT_CALL(caller, LoadCallback1(false, _)); 188 EXPECT_CALL(caller, LoadCallback1(false, _));
195 db_->LoadEntries( 189 db_->LoadEntries(
196 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller))); 190 base::Bind(&MockDatabaseCaller::LoadCallback, base::Unretained(&caller)));
197 191
198 base::RunLoop().RunUntilIdle(); 192 base::RunLoop().RunUntilIdle();
199 } 193 }
200 194
201 ACTION_P(VerifyUpdateEntries, expected) { 195 ACTION_P(VerifyUpdateEntries, expected) {
202 const EntryVector& actual = arg0; 196 const KeyValueVector actual = arg0;
203 ExpectEntryPointersEquals(expected, actual); 197 // Create a vector of TestProto from |actual| to reuse the comparison
198 // function.
199 std::vector<TestProto> extracted_entries;
200 for (KeyValueVector::const_iterator it = actual.begin(); it != actual.end();
201 ++it) {
202 TestProto entry;
203 entry.ParseFromString(it->second);
204 extracted_entries.push_back(entry);
205 }
206 ExpectEntryPointersEquals(expected, extracted_entries);
204 return true; 207 return true;
205 } 208 }
206 209
207 // Test that DomDistillerDatabase calls Save on the underlying database with the 210 // Test that ProtoDatabaseImpl calls Save on the underlying database with the
208 // correct entries to save and that the caller's SaveCallback is called with the 211 // correct entries to save and that the caller's SaveCallback is called with the
209 // correct success value. 212 // correct success value.
210 TEST_F(DomDistillerDatabaseTest, TestDBSaveSuccess) { 213 TEST_F(ProtoDatabaseImplTest, TestDBSaveSuccess) {
211 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 214 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
212 215
213 MockDB* mock_db = new MockDB(); 216 MockDB* mock_db = new MockDB();
214 MockDatabaseCaller caller; 217 MockDatabaseCaller caller;
215 EntryMap model = GetSmallModel(); 218 EntryMap model = GetSmallModel();
216 219
217 EXPECT_CALL(*mock_db, Init(_)); 220 EXPECT_CALL(*mock_db, Init(_));
218 EXPECT_CALL(caller, InitCallback(_)); 221 EXPECT_CALL(caller, InitCallback(_));
219 db_->InitWithDatabase( 222 db_->InitWithDatabase(
220 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 223 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
221 base::FilePath(path),
222 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 224 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
223 225
224 scoped_ptr<EntryVector> entries(new EntryVector()); 226 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
227 new ProtoDatabase<TestProto>::KeyEntryVector());
225 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) { 228 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
226 entries->push_back(it->second); 229 entries->push_back(std::make_pair(it->second.id(), it->second));
227 } 230 }
228 scoped_ptr<EntryVector> entries_to_remove(new EntryVector()); 231 scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
229 232
230 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model)); 233 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model));
231 EXPECT_CALL(caller, SaveCallback(true)); 234 EXPECT_CALL(caller, SaveCallback(true));
232 db_->UpdateEntries( 235 db_->UpdateEntries(
233 entries.Pass(), 236 entries.Pass(), keys_to_remove.Pass(),
234 entries_to_remove.Pass(),
235 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); 237 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
236 238
237 base::RunLoop().RunUntilIdle(); 239 base::RunLoop().RunUntilIdle();
238 } 240 }
239 241
240 TEST_F(DomDistillerDatabaseTest, TestDBSaveFailure) { 242 TEST_F(ProtoDatabaseImplTest, TestDBSaveFailure) {
241 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 243 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
242 244
243 MockDB* mock_db = new MockDB(); 245 MockDB* mock_db = new MockDB();
244 MockDatabaseCaller caller; 246 MockDatabaseCaller caller;
245 scoped_ptr<EntryVector> entries(new EntryVector()); 247 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
246 scoped_ptr<EntryVector> entries_to_remove(new EntryVector()); 248 new ProtoDatabase<TestProto>::KeyEntryVector());
249 scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
247 250
248 EXPECT_CALL(*mock_db, Init(_)); 251 EXPECT_CALL(*mock_db, Init(_));
249 EXPECT_CALL(caller, InitCallback(_)); 252 EXPECT_CALL(caller, InitCallback(_));
250 db_->InitWithDatabase( 253 db_->InitWithDatabase(
251 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 254 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
252 base::FilePath(path),
253 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 255 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
254 256
255 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); 257 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
256 EXPECT_CALL(caller, SaveCallback(false)); 258 EXPECT_CALL(caller, SaveCallback(false));
257 db_->UpdateEntries( 259 db_->UpdateEntries(
258 entries.Pass(), 260 entries.Pass(), keys_to_remove.Pass(),
259 entries_to_remove.Pass(),
260 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); 261 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
261 262
262 base::RunLoop().RunUntilIdle(); 263 base::RunLoop().RunUntilIdle();
263 } 264 }
264 265
265 ACTION_P(VerifyRemoveEntries, expected) { 266 // Test that ProtoDatabaseImpl calls Save on the underlying database with the
266 const EntryVector& actual = arg1;
267 ExpectEntryPointersEquals(expected, actual);
268 return true;
269 }
270
271 // Test that DomDistillerDatabase calls Save on the underlying database with the
272 // correct entries to delete and that the caller's SaveCallback is called with 267 // correct entries to delete and that the caller's SaveCallback is called with
273 // the correct success value. 268 // the correct success value.
274 TEST_F(DomDistillerDatabaseTest, TestDBRemoveSuccess) { 269 TEST_F(ProtoDatabaseImplTest, TestDBRemoveSuccess) {
275 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 270 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
276 271
277 MockDB* mock_db = new MockDB(); 272 MockDB* mock_db = new MockDB();
278 MockDatabaseCaller caller; 273 MockDatabaseCaller caller;
279 EntryMap model = GetSmallModel(); 274 EntryMap model = GetSmallModel();
280 275
281 EXPECT_CALL(*mock_db, Init(_)); 276 EXPECT_CALL(*mock_db, Init(_));
282 EXPECT_CALL(caller, InitCallback(_)); 277 EXPECT_CALL(caller, InitCallback(_));
283 db_->InitWithDatabase( 278 db_->InitWithDatabase(
284 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 279 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
285 base::FilePath(path),
286 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 280 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
287 281
288 scoped_ptr<EntryVector> entries(new EntryVector()); 282 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
289 scoped_ptr<EntryVector> entries_to_remove(new EntryVector()); 283 new ProtoDatabase<TestProto>::KeyEntryVector());
284 scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
290 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) { 285 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
291 entries_to_remove->push_back(it->second); 286 keys_to_remove->push_back(it->second.id());
292 } 287 }
293 288
294 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyRemoveEntries(model)); 289 KeyVector keys_copy(*keys_to_remove.get());
290 EXPECT_CALL(*mock_db, Save(_, keys_copy)).WillOnce(Return(true));
295 EXPECT_CALL(caller, SaveCallback(true)); 291 EXPECT_CALL(caller, SaveCallback(true));
296 db_->UpdateEntries( 292 db_->UpdateEntries(
297 entries.Pass(), 293 entries.Pass(), keys_to_remove.Pass(),
298 entries_to_remove.Pass(),
299 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); 294 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
300 295
301 base::RunLoop().RunUntilIdle(); 296 base::RunLoop().RunUntilIdle();
302 } 297 }
303 298
304 TEST_F(DomDistillerDatabaseTest, TestDBRemoveFailure) { 299 TEST_F(ProtoDatabaseImplTest, TestDBRemoveFailure) {
305 base::FilePath path(FILE_PATH_LITERAL("/fake/path")); 300 base::FilePath path(FILE_PATH_LITERAL("/fake/path"));
306 301
307 MockDB* mock_db = new MockDB(); 302 MockDB* mock_db = new MockDB();
308 MockDatabaseCaller caller; 303 MockDatabaseCaller caller;
309 scoped_ptr<EntryVector> entries(new EntryVector()); 304 scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
310 scoped_ptr<EntryVector> entries_to_remove(new EntryVector()); 305 new ProtoDatabase<TestProto>::KeyEntryVector());
306 scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
311 307
312 EXPECT_CALL(*mock_db, Init(_)); 308 EXPECT_CALL(*mock_db, Init(_));
313 EXPECT_CALL(caller, InitCallback(_)); 309 EXPECT_CALL(caller, InitCallback(_));
314 db_->InitWithDatabase( 310 db_->InitWithDatabase(
315 scoped_ptr<DomDistillerDatabase::Database>(mock_db), 311 scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
316 base::FilePath(path),
317 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller))); 312 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
318 313
319 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false)); 314 EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
320 EXPECT_CALL(caller, SaveCallback(false)); 315 EXPECT_CALL(caller, SaveCallback(false));
321 db_->UpdateEntries( 316 db_->UpdateEntries(
322 entries.Pass(), 317 entries.Pass(), keys_to_remove.Pass(),
323 entries_to_remove.Pass(),
324 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller))); 318 base::Bind(&MockDatabaseCaller::SaveCallback, base::Unretained(&caller)));
325 319
326 base::RunLoop().RunUntilIdle(); 320 base::RunLoop().RunUntilIdle();
327 } 321 }
328 322
329
330 // This tests that normal usage of the real database does not cause any 323 // This tests that normal usage of the real database does not cause any
331 // threading violations. 324 // threading violations.
332 TEST(DomDistillerDatabaseThreadingTest, TestDBDestruction) { 325 TEST(ProtoDatabaseImplThreadingTest, TestDBDestruction) {
333 base::MessageLoop main_loop; 326 base::MessageLoop main_loop;
334 327
335 ScopedTempDir temp_dir; 328 ScopedTempDir temp_dir;
336 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 329 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
337 330
338 base::Thread db_thread("dbthread"); 331 base::Thread db_thread("dbthread");
339 ASSERT_TRUE(db_thread.Start()); 332 ASSERT_TRUE(db_thread.Start());
340 333
341 scoped_ptr<DomDistillerDatabase> db( 334 scoped_ptr<ProtoDatabaseImpl<TestProto> > db(
342 new DomDistillerDatabase(db_thread.message_loop_proxy())); 335 new ProtoDatabaseImpl<TestProto>(db_thread.message_loop_proxy()));
343 336
344 MockDatabaseCaller caller; 337 MockDatabaseCaller caller;
345 EXPECT_CALL(caller, InitCallback(_)); 338 EXPECT_CALL(caller, InitCallback(_));
346 db->Init( 339 db->Init(temp_dir.path(), base::Bind(&MockDatabaseCaller::InitCallback,
347 temp_dir.path(), 340 base::Unretained(&caller)));
348 base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
349 341
350 db.reset(); 342 db.reset();
351 343
352 base::RunLoop run_loop; 344 base::RunLoop run_loop;
353 db_thread.message_loop_proxy()->PostTaskAndReply( 345 db_thread.message_loop_proxy()->PostTaskAndReply(
354 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure()); 346 FROM_HERE, base::Bind(base::DoNothing), run_loop.QuitClosure());
355 run_loop.Run(); 347 run_loop.Run();
356 } 348 }
357 349
358 // Test that the LevelDB properly saves entries and that load returns the saved 350 // Test that the LevelDB properly saves entries and that load returns the saved
359 // entries. If |close_after_save| is true, the database will be closed after 351 // entries. If |close_after_save| is true, the database will be closed after
360 // saving and then re-opened to ensure that the data is properly persisted. 352 // saving and then re-opened to ensure that the data is properly persisted.
361 void TestLevelDBSaveAndLoad(bool close_after_save) { 353 void TestLevelDBSaveAndLoad(bool close_after_save) {
362 ScopedTempDir temp_dir; 354 ScopedTempDir temp_dir;
363 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); 355 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
364 356
365 EntryMap model = GetSmallModel(); 357 EntryMap model = GetSmallModel();
366 EntryVector save_entries; 358
367 EntryVector load_entries; 359 KeyValueVector save_entries;
368 EntryVector remove_entries; 360 std::vector<std::string> load_entries;
361 KeyVector remove_keys;
369 362
370 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) { 363 for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
371 save_entries.push_back(it->second); 364 save_entries.push_back(
365 std::make_pair(it->second.id(), it->second.SerializeAsString()));
372 } 366 }
373 367
374 scoped_ptr<DomDistillerDatabase::LevelDB> db( 368 scoped_ptr<LevelDB> db(new LevelDB());
375 new DomDistillerDatabase::LevelDB());
376 EXPECT_TRUE(db->Init(temp_dir.path())); 369 EXPECT_TRUE(db->Init(temp_dir.path()));
377 EXPECT_TRUE(db->Save(save_entries, remove_entries)); 370 EXPECT_TRUE(db->Save(save_entries, remove_keys));
378 371
379 if (close_after_save) { 372 if (close_after_save) {
380 db.reset(new DomDistillerDatabase::LevelDB()); 373 db.reset(new LevelDB());
381 EXPECT_TRUE(db->Init(temp_dir.path())); 374 EXPECT_TRUE(db->Init(temp_dir.path()));
382 } 375 }
383 376
384 EXPECT_TRUE(db->Load(&load_entries)); 377 EXPECT_TRUE(db->Load(&load_entries));
385 378 // Convert the strings back to TestProto.
386 ExpectEntryPointersEquals(model, load_entries); 379 std::vector<TestProto> loaded_protos;
380 for (std::vector<std::string>::iterator it = load_entries.begin();
381 it != load_entries.end(); ++it) {
382 TestProto entry;
383 entry.ParseFromString(*it);
384 loaded_protos.push_back(entry);
385 }
386 ExpectEntryPointersEquals(model, loaded_protos);
387 } 387 }
388 388
389 TEST(DomDistillerDatabaseLevelDBTest, TestDBSaveAndLoad) { 389 TEST(ProtoDatabaseImplLevelDBTest, TestDBSaveAndLoad) {
390 TestLevelDBSaveAndLoad(false); 390 TestLevelDBSaveAndLoad(false);
391 } 391 }
392 392
393 TEST(DomDistillerDatabaseLevelDBTest, TestDBCloseAndReopen) { 393 TEST(ProtoDatabaseImplLevelDBTest, TestDBCloseAndReopen) {
394 TestLevelDBSaveAndLoad(true); 394 TestLevelDBSaveAndLoad(true);
395 } 395 }
396 396
397 } // namespace dom_distiller 397 } // namespace leveldb_proto
OLDNEW
« no previous file with comments | « components/leveldb_proto/proto_database_impl.h ('k') | components/leveldb_proto/testing/fake_db.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698