1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/offline_pages/offline_page_metadata_store_impl.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "components/leveldb_proto/proto_database_impl.h"
#include "components/offline_pages/offline_page_item.h"
#include "components/offline_pages/proto/offline_pages.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
using leveldb_proto::ProtoDatabaseImpl;
namespace offline_pages {
namespace {
const char kTestURL[] = "https://example.com";
const int64 kTestBookmarkId = 1234LL;
const base::FilePath::CharType kFilePath[] =
FILE_PATH_LITERAL("/offline_pages/example_com.mhtml");
int64 kFileSize = 234567;
class OfflinePageMetadataStoreImplTest : public testing::Test {
public:
enum CalledCallback { NONE, LOAD, ADD, REMOVE, DESTROY };
enum Status { STATUS_NONE, STATUS_TRUE, STATUS_FALSE };
OfflinePageMetadataStoreImplTest();
~OfflinePageMetadataStoreImplTest() override;
void TearDown() override { message_loop_.RunUntilIdle(); }
scoped_ptr<OfflinePageMetadataStoreImpl> BuildStore();
void PumpLoop();
void LoadCallback(bool success,
const std::vector<OfflinePageItem>& offline_pages);
void UpdateCallback(CalledCallback called_callback, bool success);
void ClearResults();
protected:
CalledCallback last_called_callback_;
Status last_status_;
std::vector<OfflinePageItem> offline_pages_;
base::ScopedTempDir temp_directory_;
base::MessageLoop message_loop_;
scoped_ptr<base::RunLoop> run_loop_;
};
OfflinePageMetadataStoreImplTest::OfflinePageMetadataStoreImplTest()
: last_called_callback_(NONE), last_status_(STATUS_NONE) {
EXPECT_TRUE(temp_directory_.CreateUniqueTempDir());
}
OfflinePageMetadataStoreImplTest::~OfflinePageMetadataStoreImplTest() {
}
void OfflinePageMetadataStoreImplTest::PumpLoop() {
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
}
scoped_ptr<OfflinePageMetadataStoreImpl>
OfflinePageMetadataStoreImplTest::BuildStore() {
scoped_ptr<ProtoDatabaseImpl<offline_pages::OfflinePageEntry>> db(
new ProtoDatabaseImpl<offline_pages::OfflinePageEntry>(
message_loop_.task_runner()));
return scoped_ptr<OfflinePageMetadataStoreImpl>(
new OfflinePageMetadataStoreImpl(db.Pass(), temp_directory_.path()));
}
void OfflinePageMetadataStoreImplTest::LoadCallback(
bool status,
const std::vector<OfflinePageItem>& offline_pages) {
last_called_callback_ = LOAD;
last_status_ = status ? STATUS_TRUE : STATUS_FALSE;
offline_pages_.swap(const_cast<std::vector<OfflinePageItem>&>(offline_pages));
run_loop_->Quit();
}
void OfflinePageMetadataStoreImplTest::UpdateCallback(
CalledCallback called_callback,
bool status) {
last_called_callback_ = called_callback;
last_status_ = status ? STATUS_TRUE : STATUS_FALSE;
run_loop_->Quit();
}
void OfflinePageMetadataStoreImplTest::ClearResults() {
last_called_callback_ = NONE;
last_status_ = STATUS_NONE;
offline_pages_.clear();
}
// Loads empty store and makes sure that there are no offline pages stored in
// it.
TEST_F(OfflinePageMetadataStoreImplTest, LoadEmptyStore) {
scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore());
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(0U, offline_pages_.size());
}
// Adds metadata of an offline page into a store and then loads from the
// store to make sure the metadata is preserved.
TEST_F(OfflinePageMetadataStoreImplTest, AddOfflinePageThenLoad) {
scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore());
OfflinePageItem offline_page(GURL(kTestURL), kTestBookmarkId,
base::FilePath(kFilePath), kFileSize);
store->AddOfflinePage(
offline_page,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), ADD));
PumpLoop();
EXPECT_EQ(ADD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
ClearResults();
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(1U, offline_pages_.size());
EXPECT_EQ(offline_page.url, offline_pages_[0].url);
EXPECT_EQ(offline_page.bookmark_id, offline_pages_[0].bookmark_id);
EXPECT_EQ(offline_page.version, offline_pages_[0].version);
EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path);
EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size);
EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time);
EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time);
}
// Adds metadata of an offline page into a store and then opens the store
// again to make sure that stored metadata survives store restarts.
TEST_F(OfflinePageMetadataStoreImplTest, AddOfflinePageRestartLoad) {
scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore());
OfflinePageItem offline_page(GURL(kTestURL), kTestBookmarkId,
base::FilePath(kFilePath), kFileSize);
store->AddOfflinePage(
offline_page,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), ADD));
PumpLoop();
EXPECT_EQ(ADD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
// Reset the store first to ensure file lock is removed.
store.reset();
store = BuildStore().Pass();
ClearResults();
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(1U, offline_pages_.size());
EXPECT_EQ(offline_page.url, offline_pages_[0].url);
EXPECT_EQ(offline_page.bookmark_id, offline_pages_[0].bookmark_id);
EXPECT_EQ(offline_page.version, offline_pages_[0].version);
EXPECT_EQ(offline_page.file_path, offline_pages_[0].file_path);
EXPECT_EQ(offline_page.file_size, offline_pages_[0].file_size);
EXPECT_EQ(offline_page.creation_time, offline_pages_[0].creation_time);
EXPECT_EQ(offline_page.last_access_time, offline_pages_[0].last_access_time);
}
// Tests removing offline page metadata from the store, for which it first adds
// metadata of an offline page.
TEST_F(OfflinePageMetadataStoreImplTest, RemoveOfflinePage) {
scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore());
OfflinePageItem offline_page(GURL(kTestURL), kTestBookmarkId,
base::FilePath(kFilePath), kFileSize);
store->AddOfflinePage(
offline_page,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), ADD));
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
store->RemoveOfflinePage(
offline_page.url,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), REMOVE));
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
store.reset();
store = BuildStore().Pass();
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
// Add offline page is exectued:
PumpLoop();
EXPECT_EQ(ADD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
// Load is exectued:
ClearResults();
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(1U, offline_pages_.size());
// Remove offline page is exectued:
ClearResults();
PumpLoop();
EXPECT_EQ(REMOVE, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
// Load is exectued:
ClearResults();
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(0U, offline_pages_.size());
// Checking the value after reseting the store.
ClearResults();
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(0U, offline_pages_.size());
}
// Adds metadata of multiple offline pages into a store and removes some.
TEST_F(OfflinePageMetadataStoreImplTest, AddRemoveMultipleOfflinePages) {
scoped_ptr<OfflinePageMetadataStoreImpl> store(BuildStore());
OfflinePageItem offline_page_1(GURL(kTestURL), kTestBookmarkId,
base::FilePath(kFilePath), kFileSize);
base::FilePath file_path_2 =
base::FilePath(FILE_PATH_LITERAL("//other.page.com.mhtml"));
OfflinePageItem offline_page_2(GURL("https://other.page.com"), 5678LL,
file_path_2, 12345, base::Time::Now());
store->AddOfflinePage(
offline_page_1,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), ADD));
PumpLoop();
EXPECT_EQ(ADD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
ClearResults();
store->AddOfflinePage(
offline_page_2,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), ADD));
PumpLoop();
EXPECT_EQ(ADD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
ClearResults();
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(2U, offline_pages_.size());
store->RemoveOfflinePage(
offline_page_1.url,
base::Bind(&OfflinePageMetadataStoreImplTest::UpdateCallback,
base::Unretained(this), REMOVE));
PumpLoop();
EXPECT_EQ(REMOVE, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
ClearResults();
store.reset();
store = BuildStore().Pass();
store->Load(base::Bind(&OfflinePageMetadataStoreImplTest::LoadCallback,
base::Unretained(this)));
PumpLoop();
EXPECT_EQ(LOAD, last_called_callback_);
EXPECT_EQ(STATUS_TRUE, last_status_);
EXPECT_EQ(1U, offline_pages_.size());
EXPECT_EQ(offline_page_2.url, offline_pages_[0].url);
EXPECT_EQ(offline_page_2.bookmark_id, offline_pages_[0].bookmark_id);
EXPECT_EQ(offline_page_2.version, offline_pages_[0].version);
EXPECT_EQ(offline_page_2.file_path, offline_pages_[0].file_path);
EXPECT_EQ(offline_page_2.file_size, offline_pages_[0].file_size);
EXPECT_EQ(offline_page_2.creation_time, offline_pages_[0].creation_time);
EXPECT_EQ(offline_page_2.last_access_time,
offline_pages_[0].last_access_time);
}
} // namespace
} // namespace offline_pages
|