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 virtual void Init(const base::FilePath
& database_dir
,
31 typename ProtoDatabase
<T
>::InitCallback callback
)
34 virtual void UpdateEntries(
35 scoped_ptr
<typename ProtoDatabase
<T
>::KeyEntryVector
> entries_to_save
,
36 scoped_ptr
<std::vector
<std::string
> > keys_to_remove
,
37 typename ProtoDatabase
<T
>::UpdateCallback callback
) override
;
39 virtual void LoadEntries(typename ProtoDatabase
<T
>::LoadCallback callback
)
41 base::FilePath
& GetDirectory();
43 void InitCallback(bool success
);
45 void LoadCallback(bool success
);
47 void UpdateCallback(bool success
);
49 static base::FilePath
DirectoryForTestDB();
52 static void RunLoadCallback(
53 typename ProtoDatabase
<T
>::LoadCallback callback
,
54 scoped_ptr
<typename
std::vector
<T
> > entries
,
60 Callback init_callback_
;
61 Callback load_callback_
;
62 Callback update_callback_
;
66 FakeDB
<T
>::FakeDB(EntryMap
* db
)
70 FakeDB
<T
>::~FakeDB() {}
73 void FakeDB
<T
>::Init(const base::FilePath
& database_dir
,
74 typename ProtoDatabase
<T
>::InitCallback callback
) {
76 init_callback_
= callback
;
80 void FakeDB
<T
>::UpdateEntries(
81 scoped_ptr
<typename ProtoDatabase
<T
>::KeyEntryVector
> entries_to_save
,
82 scoped_ptr
<std::vector
<std::string
> > keys_to_remove
,
83 typename ProtoDatabase
<T
>::UpdateCallback callback
) {
84 for (typename ProtoDatabase
<T
>::KeyEntryVector::iterator it
=
85 entries_to_save
->begin();
86 it
!= entries_to_save
->end(); ++it
) {
87 (*db_
)[it
->first
] = it
->second
;
89 for (std::vector
<std::string
>::iterator it
= keys_to_remove
->begin();
90 it
!= keys_to_remove
->end(); ++it
) {
93 update_callback_
= callback
;
97 void FakeDB
<T
>::LoadEntries(typename ProtoDatabase
<T
>::LoadCallback callback
) {
98 scoped_ptr
<std::vector
<T
> > entries(new std::vector
<T
>());
99 for (typename
EntryMap::iterator it
= db_
->begin(); it
!= db_
->end(); ++it
) {
100 entries
->push_back(it
->second
);
103 base::Bind(RunLoadCallback
, callback
, base::Passed(&entries
));
106 template <typename T
>
107 base::FilePath
& FakeDB
<T
>::GetDirectory() {
111 template <typename T
>
112 void FakeDB
<T
>::InitCallback(bool success
) {
113 init_callback_
.Run(success
);
114 init_callback_
.Reset();
117 template <typename T
>
118 void FakeDB
<T
>::LoadCallback(bool success
) {
119 load_callback_
.Run(success
);
120 load_callback_
.Reset();
123 template <typename T
>
124 void FakeDB
<T
>::UpdateCallback(bool success
) {
125 update_callback_
.Run(success
);
126 update_callback_
.Reset();
130 template <typename T
>
131 void FakeDB
<T
>::RunLoadCallback(
132 typename ProtoDatabase
<T
>::LoadCallback callback
,
133 scoped_ptr
<typename
std::vector
<T
> > entries
, bool success
) {
134 callback
.Run(success
, entries
.Pass());
138 template <typename T
>
139 base::FilePath FakeDB
<T
>::DirectoryForTestDB() {
140 return base::FilePath(FILE_PATH_LITERAL("/fake/path"));
144 } // namespace leveldb_proto
146 #endif // COMPONENTS_LEVELDB_PROTO_TESTING_FAKE_DB_H_