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 typedef base::Callback
<void(bool)> Callback
;
25 typedef typename
base::hash_map
<std::string
, T
> EntryMap
;
27 explicit FakeDB(EntryMap
* db
);
30 void Init(const base::FilePath
& database_dir
,
31 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 typename ProtoDatabase
<T
>::UpdateCallback callback
) override
;
38 void LoadEntries(typename ProtoDatabase
<T
>::LoadCallback callback
) override
;
39 base::FilePath
& GetDirectory();
41 void InitCallback(bool success
);
43 void LoadCallback(bool success
);
45 void UpdateCallback(bool success
);
47 static base::FilePath
DirectoryForTestDB();
50 static void RunLoadCallback(
51 typename ProtoDatabase
<T
>::LoadCallback callback
,
52 scoped_ptr
<typename
std::vector
<T
> > entries
,
58 Callback init_callback_
;
59 Callback load_callback_
;
60 Callback update_callback_
;
64 FakeDB
<T
>::FakeDB(EntryMap
* db
)
68 FakeDB
<T
>::~FakeDB() {}
71 void FakeDB
<T
>::Init(const base::FilePath
& database_dir
,
72 typename ProtoDatabase
<T
>::InitCallback callback
) {
74 init_callback_
= callback
;
78 void FakeDB
<T
>::UpdateEntries(
79 scoped_ptr
<typename ProtoDatabase
<T
>::KeyEntryVector
> entries_to_save
,
80 scoped_ptr
<std::vector
<std::string
> > keys_to_remove
,
81 typename ProtoDatabase
<T
>::UpdateCallback callback
) {
82 for (typename ProtoDatabase
<T
>::KeyEntryVector::iterator it
=
83 entries_to_save
->begin();
84 it
!= entries_to_save
->end(); ++it
) {
85 (*db_
)[it
->first
] = it
->second
;
87 for (std::vector
<std::string
>::iterator it
= keys_to_remove
->begin();
88 it
!= keys_to_remove
->end(); ++it
) {
91 update_callback_
= callback
;
95 void FakeDB
<T
>::LoadEntries(typename ProtoDatabase
<T
>::LoadCallback callback
) {
96 scoped_ptr
<std::vector
<T
> > entries(new std::vector
<T
>());
97 for (typename
EntryMap::iterator it
= db_
->begin(); it
!= db_
->end(); ++it
) {
98 entries
->push_back(it
->second
);
101 base::Bind(RunLoadCallback
, callback
, base::Passed(&entries
));
104 template <typename T
>
105 base::FilePath
& FakeDB
<T
>::GetDirectory() {
109 template <typename T
>
110 void FakeDB
<T
>::InitCallback(bool success
) {
111 init_callback_
.Run(success
);
112 init_callback_
.Reset();
115 template <typename T
>
116 void FakeDB
<T
>::LoadCallback(bool success
) {
117 load_callback_
.Run(success
);
118 load_callback_
.Reset();
121 template <typename T
>
122 void FakeDB
<T
>::UpdateCallback(bool success
) {
123 update_callback_
.Run(success
);
124 update_callback_
.Reset();
128 template <typename T
>
129 void FakeDB
<T
>::RunLoadCallback(
130 typename ProtoDatabase
<T
>::LoadCallback callback
,
131 scoped_ptr
<typename
std::vector
<T
> > entries
, bool success
) {
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_