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 #ifndef COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
6 #define COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/leveldb_proto/proto_database.h"
17 namespace leveldb_proto
{
21 class FakeDB
: public ProtoDatabase
<T
> {
22 using Callback
= base::Callback
<void(bool)>;
25 using EntryMap
= typename
base::hash_map
<std::string
, T
>;
27 explicit FakeDB(EntryMap
* db
);
30 // ProtoDatabase implementation.
31 void Init(const base::FilePath
& database_dir
,
32 const typename ProtoDatabase
<T
>::InitCallback
& callback
) override
;
34 scoped_ptr
<typename ProtoDatabase
<T
>::KeyEntryVector
> entries_to_save
,
35 scoped_ptr
<std::vector
<std::string
>> keys_to_remove
,
36 const typename ProtoDatabase
<T
>::UpdateCallback
& callback
) override
;
38 const typename ProtoDatabase
<T
>::LoadCallback
& callback
) override
;
40 base::FilePath
& GetDirectory();
42 void InitCallback(bool success
);
44 void LoadCallback(bool success
);
46 void UpdateCallback(bool success
);
48 static base::FilePath
DirectoryForTestDB();
51 static void RunLoadCallback(
52 const typename ProtoDatabase
<T
>::LoadCallback
& callback
,
53 scoped_ptr
<typename
std::vector
<T
>> entries
,
59 Callback init_callback_
;
60 Callback load_callback_
;
61 Callback update_callback_
;
65 FakeDB
<T
>::FakeDB(EntryMap
* db
)
69 FakeDB
<T
>::~FakeDB() {}
72 void FakeDB
<T
>::Init(const base::FilePath
& database_dir
,
73 const typename ProtoDatabase
<T
>::InitCallback
& callback
) {
75 init_callback_
= callback
;
79 void FakeDB
<T
>::UpdateEntries(
80 scoped_ptr
<typename ProtoDatabase
<T
>::KeyEntryVector
> entries_to_save
,
81 scoped_ptr
<std::vector
<std::string
>> keys_to_remove
,
82 const typename ProtoDatabase
<T
>::UpdateCallback
& callback
) {
83 for (const auto& pair
: *entries_to_save
)
84 (*db_
)[pair
.first
] = pair
.second
;
86 for (const auto& key
: *keys_to_remove
)
89 update_callback_
= callback
;
93 void FakeDB
<T
>::LoadEntries(
94 const typename ProtoDatabase
<T
>::LoadCallback
& callback
) {
95 scoped_ptr
<std::vector
<T
>> entries(new std::vector
<T
>());
96 for (const auto& pair
: *db_
)
97 entries
->push_back(pair
.second
);
100 base::Bind(RunLoadCallback
, callback
, base::Passed(&entries
));
103 template <typename T
>
104 base::FilePath
& FakeDB
<T
>::GetDirectory() {
108 template <typename T
>
109 void FakeDB
<T
>::InitCallback(bool success
) {
110 init_callback_
.Run(success
);
111 init_callback_
.Reset();
114 template <typename T
>
115 void FakeDB
<T
>::LoadCallback(bool success
) {
116 load_callback_
.Run(success
);
117 load_callback_
.Reset();
120 template <typename T
>
121 void FakeDB
<T
>::UpdateCallback(bool success
) {
122 update_callback_
.Run(success
);
123 update_callback_
.Reset();
127 template <typename T
>
128 void FakeDB
<T
>::RunLoadCallback(
129 const typename ProtoDatabase
<T
>::LoadCallback
& callback
,
130 scoped_ptr
<typename
std::vector
<T
>> entries
,
132 callback
.Run(success
, entries
.Pass());
136 template <typename T
>
137 base::FilePath FakeDB
<T
>::DirectoryForTestDB() {
138 return base::FilePath(FILE_PATH_LITERAL("/fake/path"));
142 } // namespace leveldb_proto
144 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_