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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
// 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_model.h"
#include <algorithm>
#include "base/bind.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/offline_pages/offline_page_item.h"
#include "components/offline_pages/offline_page_metadata_store.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
using DeletePageResult = offline_pages::OfflinePageModel::DeletePageResult;
namespace offline_pages {
namespace {
const GURL kTestUrl("http://example.com");
const int64 kTestPageBookmarkId1 = 1234LL;
const GURL kTestUrl2("http://other.page.com");
const int64 kTestPageBookmarkId2 = 5678LL;
const int64 kTestFileSize = 876543LL;
class OfflinePageTestStore : public OfflinePageMetadataStore {
public:
enum class TestScenario {
SUCCESSFUL,
WRITE_FAILED,
LOAD_FAILED,
REMOVE_FAILED,
};
explicit OfflinePageTestStore(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
~OfflinePageTestStore() override;
// OfflinePageMetadataStore overrides:
void Load(const LoadCallback& callback) override;
void AddOfflinePage(const OfflinePageItem& offline_page,
const UpdateCallback& callback) override;
void RemoveOfflinePage(const GURL& page_url,
const UpdateCallback& callback) override;
const OfflinePageItem& last_saved_page() const { return last_saved_page_; }
void set_test_scenario(TestScenario scenario) { scenario_ = scenario; };
const std::vector<OfflinePageItem>& offline_pages() const {
return offline_pages_;
}
private:
OfflinePageItem last_saved_page_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
TestScenario scenario_;
std::vector<OfflinePageItem> offline_pages_;
DISALLOW_COPY_AND_ASSIGN(OfflinePageTestStore);
};
OfflinePageTestStore::OfflinePageTestStore(
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
: task_runner_(task_runner),
scenario_(TestScenario::SUCCESSFUL) {
}
OfflinePageTestStore::~OfflinePageTestStore() {
}
void OfflinePageTestStore::Load(const LoadCallback& callback) {
if (scenario_ != TestScenario::LOAD_FAILED) {
task_runner_->PostTask(
FROM_HERE, base::Bind(callback, true, offline_pages_));
} else {
task_runner_->PostTask(
FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
}
}
void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page,
const UpdateCallback& callback) {
last_saved_page_ = offline_page;
bool result = scenario_ != TestScenario::WRITE_FAILED;
if (result) {
offline_pages_.push_back(offline_page);
}
task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
}
void OfflinePageTestStore::RemoveOfflinePage(const GURL& page_url,
const UpdateCallback& callback) {
bool result = false;
if (scenario_ != TestScenario::REMOVE_FAILED) {
for (auto iter = offline_pages_.begin();
iter != offline_pages_.end(); ++iter) {
if (iter->url == page_url) {
offline_pages_.erase(iter);
result = true;
break;
}
}
}
task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
}
} // namespace
class OfflinePageModelTest;
class OfflinePageTestArchiver : public OfflinePageArchiver {
public:
OfflinePageTestArchiver(
OfflinePageModelTest* test,
const GURL& url,
const base::FilePath& archiver_dir,
ArchiverResult result,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
~OfflinePageTestArchiver() override;
// OfflinePageArchiver implementation:
void CreateArchive(const CreateArchiveCallback& callback) override;
void CompleteCreateArchive();
void set_delayed(bool delayed) { delayed_ = delayed; }
bool create_archive_called() const { return create_archive_called_; }
private:
OfflinePageModelTest* test_; // Outlive OfflinePageTestArchiver.
GURL url_;
base::FilePath archiver_dir_;
ArchiverResult result_;
bool create_archive_called_;
bool delayed_;
CreateArchiveCallback callback_;
scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
DISALLOW_COPY_AND_ASSIGN(OfflinePageTestArchiver);
};
class OfflinePageModelTest
: public testing::Test,
public OfflinePageModel::Observer,
public base::SupportsWeakPtr<OfflinePageModelTest> {
public:
OfflinePageModelTest();
~OfflinePageModelTest() override;
void SetUp() override;
void TearDown() override;
// OfflinePageModel::Observer implementation.
void OfflinePageModelLoaded(OfflinePageModel* model) override;
// OfflinePageModel callbacks.
void OnSavePageDone(SavePageResult result);
void OnDeletePageDone(DeletePageResult result);
scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
const GURL& url,
OfflinePageArchiver::ArchiverResult result);
scoped_ptr<OfflinePageMetadataStore> BuildStore();
scoped_ptr<OfflinePageModel> BuildModel();
// Utility methods.
void PumpLoop();
void ResetResults();
scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
return message_loop_.task_runner();
}
OfflinePageModel* model() { return model_.get(); }
OfflinePageTestStore* GetStore();
SavePageResult last_save_result() const {
return last_save_result_;
}
DeletePageResult last_delete_result() const {
return last_delete_result_;
}
const base::FilePath& last_archiver_path() { return last_archiver_path_; }
void set_last_archiver_path(const base::FilePath& last_archiver_path) {
last_archiver_path_ = last_archiver_path;
}
private:
base::MessageLoop message_loop_;
scoped_ptr<base::RunLoop> run_loop_;
base::ScopedTempDir temp_dir_;
scoped_ptr<OfflinePageModel> model_;
SavePageResult last_save_result_;
DeletePageResult last_delete_result_;
base::FilePath last_archiver_path_;
};
OfflinePageTestArchiver::OfflinePageTestArchiver(
OfflinePageModelTest* test,
const GURL& url,
const base::FilePath& archiver_dir,
ArchiverResult result,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
: test_(test),
url_(url),
archiver_dir_(archiver_dir),
result_(result),
create_archive_called_(false),
delayed_(false),
task_runner_(task_runner) {
}
OfflinePageTestArchiver::~OfflinePageTestArchiver() {
EXPECT_TRUE(create_archive_called_);
}
void OfflinePageTestArchiver::CreateArchive(
const CreateArchiveCallback& callback) {
create_archive_called_ = true;
callback_ = callback;
if (!delayed_)
CompleteCreateArchive();
}
void OfflinePageTestArchiver::CompleteCreateArchive() {
DCHECK(!callback_.is_null());
base::FilePath archiver_path;
ASSERT_TRUE(base::CreateTemporaryFileInDir(archiver_dir_, &archiver_path));
test_->set_last_archiver_path(archiver_path);
task_runner_->PostTask(FROM_HERE, base::Bind(callback_, this, result_, url_,
archiver_path, kTestFileSize));
}
OfflinePageModelTest::OfflinePageModelTest()
: last_save_result_(SavePageResult::CANCELLED),
last_delete_result_(DeletePageResult::CANCELLED) {
}
OfflinePageModelTest::~OfflinePageModelTest() {
}
void OfflinePageModelTest::SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
model_ = BuildModel().Pass();
model_->AddObserver(this);
PumpLoop();
}
void OfflinePageModelTest::TearDown() {
model_->RemoveObserver(this);
}
void OfflinePageModelTest::OfflinePageModelLoaded(OfflinePageModel* model) {
ASSERT_EQ(model_.get(), model);
run_loop_->Quit();
}
void OfflinePageModelTest::OnSavePageDone(
OfflinePageModel::SavePageResult result) {
run_loop_->Quit();
last_save_result_ = result;
}
void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) {
run_loop_->Quit();
last_delete_result_ = result;
}
scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver(
const GURL& url,
OfflinePageArchiver::ArchiverResult result) {
return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver(
this, url, temp_dir_.path(), result, task_runner()));
}
scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() {
return scoped_ptr<OfflinePageMetadataStore>(
new OfflinePageTestStore(task_runner()));
}
scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel() {
return scoped_ptr<OfflinePageModel>(
new OfflinePageModel(BuildStore().Pass(), task_runner()));
}
void OfflinePageModelTest::PumpLoop() {
run_loop_.reset(new base::RunLoop());
run_loop_->Run();
}
void OfflinePageModelTest::ResetResults() {
last_save_result_ = SavePageResult::CANCELLED;
last_delete_result_ = DeletePageResult::CANCELLED;
last_archiver_path_.clear();
}
OfflinePageTestStore* OfflinePageModelTest::GetStore() {
return static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting());
}
TEST_F(OfflinePageModelTest, SavePageSuccessful) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
OfflinePageTestStore* store = GetStore();
EXPECT_EQ(kTestUrl, store->last_saved_page().url);
EXPECT_EQ(kTestPageBookmarkId1, store->last_saved_page().bookmark_id);
// Save last_archiver_path since it will be referred to later.
base::FilePath archiver_path = last_archiver_path();
EXPECT_EQ(archiver_path, store->last_saved_page().file_path);
EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
ResetResults();
const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
EXPECT_EQ(1UL, offline_pages.size());
EXPECT_EQ(kTestUrl, offline_pages[0].url);
EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
EXPECT_EQ(archiver_path, offline_pages[0].file_path);
EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
}
TEST_F(OfflinePageModelTest, SavePageOfflineArchiverCancelled) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::ERROR_CANCELED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::CANCELLED, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineArchiverDeviceFull) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::ERROR_DEVICE_FULL)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::DEVICE_FULL, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineArchiverContentUnavailable) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(
kTestUrl,
OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::CONTENT_UNAVAILABLE, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineCreationFailed) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(
kTestUrl,
OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::ARCHIVE_CREATION_FAILED, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineArchiverReturnedWrongUrl) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(GURL("http://other.random.url.com"),
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::ARCHIVE_CREATION_FAILED, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineCreationStoreWriteFailure) {
GetStore()->set_test_scenario(
OfflinePageTestStore::TestScenario::WRITE_FAILED);
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::STORE_FAILURE, last_save_result());
}
TEST_F(OfflinePageModelTest, SavePageOfflineArchiverTwoPages) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
// archiver_ptr will be valid until after first PumpLoop() call after
// CompleteCreateArchive() is called.
OfflinePageTestArchiver* archiver_ptr = archiver.get();
archiver_ptr->set_delayed(true);
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
EXPECT_TRUE(archiver_ptr->create_archive_called());
// Request to save another page.
scoped_ptr<OfflinePageTestArchiver> archiver2(
BuildArchiver(kTestUrl2,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
OfflinePageTestStore* store = GetStore();
EXPECT_EQ(kTestUrl2, store->last_saved_page().url);
EXPECT_EQ(kTestPageBookmarkId2, store->last_saved_page().bookmark_id);
base::FilePath archiver_path2 = last_archiver_path();
EXPECT_EQ(archiver_path2, store->last_saved_page().file_path);
EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
ResetResults();
archiver_ptr->CompleteCreateArchive();
// After this pump loop archiver_ptr is invalid.
PumpLoop();
EXPECT_EQ(kTestUrl, store->last_saved_page().url);
EXPECT_EQ(kTestPageBookmarkId1, store->last_saved_page().bookmark_id);
base::FilePath archiver_path = last_archiver_path();
EXPECT_EQ(archiver_path, store->last_saved_page().file_path);
EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
ResetResults();
const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
EXPECT_EQ(2UL, offline_pages.size());
EXPECT_EQ(kTestUrl2, offline_pages[0].url);
EXPECT_EQ(kTestPageBookmarkId2, offline_pages[0].bookmark_id);
EXPECT_EQ(archiver_path2, offline_pages[0].file_path);
EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
EXPECT_EQ(kTestUrl, offline_pages[1].url);
EXPECT_EQ(kTestPageBookmarkId1, offline_pages[1].bookmark_id);
EXPECT_EQ(archiver_path, offline_pages[1].file_path);
EXPECT_EQ(kTestFileSize, offline_pages[1].file_size);
}
TEST_F(OfflinePageModelTest, GetAllPagesStoreEmpty) {
const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
EXPECT_EQ(0UL, offline_pages.size());
}
TEST_F(OfflinePageModelTest, GetAllPagesStoreFailure) {
GetStore()->set_test_scenario(
OfflinePageTestStore::TestScenario::LOAD_FAILED);
const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
EXPECT_EQ(0UL, offline_pages.size());
}
TEST_F(OfflinePageModelTest, DeletePageSuccessful) {
OfflinePageTestStore* store = GetStore();
// Save one page.
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
EXPECT_EQ(1u, store->offline_pages().size());
ResetResults();
// Save another page.
scoped_ptr<OfflinePageTestArchiver> archiver2(
BuildArchiver(kTestUrl2,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
EXPECT_EQ(2u, store->offline_pages().size());
ResetResults();
// Delete one page.
model()->DeletePageByBookmarkId(
kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
AsWeakPtr()));
PumpLoop();
EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result());
ASSERT_EQ(1u, store->offline_pages().size());
EXPECT_EQ(kTestUrl2, store->offline_pages()[0].url);
// Delete another page.
model()->DeletePageByBookmarkId(
kTestPageBookmarkId2, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
AsWeakPtr()));
ResetResults();
PumpLoop();
EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result());
EXPECT_EQ(0u, store->offline_pages().size());
}
TEST_F(OfflinePageModelTest, DeletePageNotFound) {
model()->DeletePageByBookmarkId(
kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
AsWeakPtr()));
EXPECT_EQ(DeletePageResult::NOT_FOUND, last_delete_result());
}
TEST_F(OfflinePageModelTest, DeletePageStoreFailureOnRemove) {
// Save a page.
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
ResetResults();
// Try to delete this page.
GetStore()->set_test_scenario(
OfflinePageTestStore::TestScenario::REMOVE_FAILED);
model()->DeletePageByBookmarkId(
kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
AsWeakPtr()));
PumpLoop();
EXPECT_EQ(DeletePageResult::STORE_FAILURE, last_delete_result());
}
TEST_F(OfflinePageModelTest, GetPageByBookmarkId) {
scoped_ptr<OfflinePageTestArchiver> archiver(
BuildArchiver(kTestUrl,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
scoped_ptr<OfflinePageTestArchiver> archiver2(
BuildArchiver(kTestUrl2,
OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
.Pass());
model()->SavePage(
kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();
OfflinePageItem page;
EXPECT_TRUE(model()->GetPageByBookmarkId(kTestPageBookmarkId1, &page));
EXPECT_EQ(kTestUrl, page.url);
EXPECT_EQ(kTestPageBookmarkId1, page.bookmark_id);
EXPECT_EQ(kTestFileSize, page.file_size);
EXPECT_TRUE(model()->GetPageByBookmarkId(kTestPageBookmarkId2, &page));
EXPECT_EQ(kTestUrl2, page.url);
EXPECT_EQ(kTestPageBookmarkId2, page.bookmark_id);
EXPECT_EQ(kTestFileSize, page.file_size);
EXPECT_FALSE(model()->GetPageByBookmarkId(-42, &page));
}
} // namespace offline_pages
|