Cleanup: Pass std::string as const reference from pdf/
[chromium-blink-merge.git] / components / offline_pages / offline_page_model_unittest.cc
blobf60e0f54d44baa3ff59bb21a4c40f19e0e900f11
1 // Copyright 2015 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 #include "components/offline_pages/offline_page_model.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/single_thread_task_runner.h"
15 #include "base/strings/string16.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/time/time.h"
18 #include "components/offline_pages/offline_page_item.h"
19 #include "components/offline_pages/offline_page_metadata_store.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
23 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult;
24 using DeletePageResult = offline_pages::OfflinePageModel::DeletePageResult;
26 namespace offline_pages {
28 namespace {
29 const GURL kTestUrl("http://example.com");
30 const int64 kTestPageBookmarkId1 = 1234LL;
31 const GURL kTestUrl2("http://other.page.com");
32 const int64 kTestPageBookmarkId2 = 5678LL;
33 const int64 kTestFileSize = 876543LL;
35 class OfflinePageTestStore : public OfflinePageMetadataStore {
36 public:
37 enum class TestScenario {
38 SUCCESSFUL,
39 WRITE_FAILED,
40 LOAD_FAILED,
41 REMOVE_FAILED,
44 explicit OfflinePageTestStore(
45 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
46 explicit OfflinePageTestStore(const OfflinePageTestStore& other_store);
47 ~OfflinePageTestStore() override;
49 // OfflinePageMetadataStore overrides:
50 void Load(const LoadCallback& callback) override;
51 void AddOfflinePage(const OfflinePageItem& offline_page,
52 const UpdateCallback& callback) override;
53 void RemoveOfflinePages(const std::vector<int64>& bookmark_ids,
54 const UpdateCallback& callback) override;
55 const OfflinePageItem& last_saved_page() const { return last_saved_page_; }
57 void set_test_scenario(TestScenario scenario) { scenario_ = scenario; };
59 const std::vector<OfflinePageItem>& offline_pages() const {
60 return offline_pages_;
63 private:
64 OfflinePageItem last_saved_page_;
65 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
66 TestScenario scenario_;
68 std::vector<OfflinePageItem> offline_pages_;
70 DISALLOW_ASSIGN(OfflinePageTestStore);
73 OfflinePageTestStore::OfflinePageTestStore(
74 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
75 : task_runner_(task_runner),
76 scenario_(TestScenario::SUCCESSFUL) {
79 OfflinePageTestStore::OfflinePageTestStore(
80 const OfflinePageTestStore& other_store)
81 : task_runner_(other_store.task_runner_),
82 scenario_(other_store.scenario_),
83 offline_pages_(other_store.offline_pages_) {}
85 OfflinePageTestStore::~OfflinePageTestStore() {
88 void OfflinePageTestStore::Load(const LoadCallback& callback) {
89 if (scenario_ != TestScenario::LOAD_FAILED) {
90 task_runner_->PostTask(
91 FROM_HERE, base::Bind(callback, true, offline_pages_));
92 } else {
93 task_runner_->PostTask(
94 FROM_HERE, base::Bind(callback, false, std::vector<OfflinePageItem>()));
98 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page,
99 const UpdateCallback& callback) {
100 last_saved_page_ = offline_page;
101 bool result = scenario_ != TestScenario::WRITE_FAILED;
102 if (result) {
103 offline_pages_.push_back(offline_page);
105 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
108 void OfflinePageTestStore::RemoveOfflinePages(
109 const std::vector<int64>& bookmark_ids,
110 const UpdateCallback& callback) {
111 ASSERT_FALSE(bookmark_ids.empty());
112 bool result = false;
113 if (scenario_ != TestScenario::REMOVE_FAILED) {
114 for (auto iter = offline_pages_.begin();
115 iter != offline_pages_.end(); ++iter) {
116 if (iter->bookmark_id == bookmark_ids[0]) {
117 offline_pages_.erase(iter);
118 result = true;
119 break;
124 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
127 } // namespace
129 class OfflinePageModelTest;
131 class OfflinePageTestArchiver : public OfflinePageArchiver {
132 public:
133 OfflinePageTestArchiver(
134 OfflinePageModelTest* test,
135 const GURL& url,
136 const base::FilePath& archiver_dir,
137 ArchiverResult result,
138 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
139 ~OfflinePageTestArchiver() override;
141 // OfflinePageArchiver implementation:
142 void CreateArchive(const CreateArchiveCallback& callback) override;
144 void CompleteCreateArchive();
146 void set_delayed(bool delayed) { delayed_ = delayed; }
148 bool create_archive_called() const { return create_archive_called_; }
150 private:
151 OfflinePageModelTest* test_; // Outlive OfflinePageTestArchiver.
152 GURL url_;
153 base::FilePath archiver_dir_;
154 ArchiverResult result_;
155 bool create_archive_called_;
156 bool delayed_;
157 CreateArchiveCallback callback_;
158 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
159 DISALLOW_COPY_AND_ASSIGN(OfflinePageTestArchiver);
162 class OfflinePageModelTest
163 : public testing::Test,
164 public OfflinePageModel::Observer,
165 public base::SupportsWeakPtr<OfflinePageModelTest> {
166 public:
167 OfflinePageModelTest();
168 ~OfflinePageModelTest() override;
170 void SetUp() override;
171 void TearDown() override;
173 // OfflinePageModel::Observer implementation.
174 void OfflinePageModelLoaded(OfflinePageModel* model) override;
176 // OfflinePageModel callbacks.
177 void OnSavePageDone(SavePageResult result);
178 void OnDeletePageDone(DeletePageResult result);
180 // OfflinePageMetadataStore callbacks.
181 void OnStoreUpdateDone(bool /* success */);
183 scoped_ptr<OfflinePageTestArchiver> BuildArchiver(
184 const GURL& url,
185 OfflinePageArchiver::ArchiverResult result);
186 scoped_ptr<OfflinePageMetadataStore> BuildStore();
187 scoped_ptr<OfflinePageModel> BuildModel(
188 scoped_ptr<OfflinePageMetadataStore> store);
189 void ResetModel();
191 // Utility methods.
192 void PumpLoop();
193 void ResetResults();
195 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
196 return message_loop_.task_runner();
199 OfflinePageModel* model() { return model_.get(); }
201 OfflinePageTestStore* GetStore();
203 SavePageResult last_save_result() const {
204 return last_save_result_;
207 DeletePageResult last_delete_result() const {
208 return last_delete_result_;
211 const base::FilePath& last_archiver_path() { return last_archiver_path_; }
212 void set_last_archiver_path(const base::FilePath& last_archiver_path) {
213 last_archiver_path_ = last_archiver_path;
216 private:
217 base::MessageLoop message_loop_;
218 scoped_ptr<base::RunLoop> run_loop_;
219 base::ScopedTempDir temp_dir_;
221 scoped_ptr<OfflinePageModel> model_;
222 SavePageResult last_save_result_;
223 DeletePageResult last_delete_result_;
224 base::FilePath last_archiver_path_;
227 OfflinePageTestArchiver::OfflinePageTestArchiver(
228 OfflinePageModelTest* test,
229 const GURL& url,
230 const base::FilePath& archiver_dir,
231 ArchiverResult result,
232 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
233 : test_(test),
234 url_(url),
235 archiver_dir_(archiver_dir),
236 result_(result),
237 create_archive_called_(false),
238 delayed_(false),
239 task_runner_(task_runner) {
242 OfflinePageTestArchiver::~OfflinePageTestArchiver() {
243 EXPECT_TRUE(create_archive_called_);
246 void OfflinePageTestArchiver::CreateArchive(
247 const CreateArchiveCallback& callback) {
248 create_archive_called_ = true;
249 callback_ = callback;
250 if (!delayed_)
251 CompleteCreateArchive();
254 void OfflinePageTestArchiver::CompleteCreateArchive() {
255 DCHECK(!callback_.is_null());
256 base::FilePath archiver_path;
257 ASSERT_TRUE(base::CreateTemporaryFileInDir(archiver_dir_, &archiver_path));
258 test_->set_last_archiver_path(archiver_path);
259 task_runner_->PostTask(FROM_HERE, base::Bind(callback_, this, result_, url_,
260 archiver_path, kTestFileSize));
263 OfflinePageModelTest::OfflinePageModelTest()
264 : last_save_result_(SavePageResult::CANCELLED),
265 last_delete_result_(DeletePageResult::CANCELLED) {
268 OfflinePageModelTest::~OfflinePageModelTest() {
271 void OfflinePageModelTest::SetUp() {
272 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
273 model_ = BuildModel(BuildStore().Pass()).Pass();
274 model_->AddObserver(this);
275 PumpLoop();
278 void OfflinePageModelTest::TearDown() {
279 model_->RemoveObserver(this);
282 void OfflinePageModelTest::OfflinePageModelLoaded(OfflinePageModel* model) {
283 ASSERT_EQ(model_.get(), model);
284 run_loop_->Quit();
287 void OfflinePageModelTest::OnSavePageDone(
288 OfflinePageModel::SavePageResult result) {
289 run_loop_->Quit();
290 last_save_result_ = result;
293 void OfflinePageModelTest::OnDeletePageDone(DeletePageResult result) {
294 run_loop_->Quit();
295 last_delete_result_ = result;
298 void OfflinePageModelTest::OnStoreUpdateDone(bool /* success - ignored */) {
299 run_loop_->Quit();
302 scoped_ptr<OfflinePageTestArchiver> OfflinePageModelTest::BuildArchiver(
303 const GURL& url,
304 OfflinePageArchiver::ArchiverResult result) {
305 return scoped_ptr<OfflinePageTestArchiver>(new OfflinePageTestArchiver(
306 this, url, temp_dir_.path(), result, task_runner()));
309 scoped_ptr<OfflinePageMetadataStore> OfflinePageModelTest::BuildStore() {
310 return scoped_ptr<OfflinePageMetadataStore>(
311 new OfflinePageTestStore(task_runner()));
314 scoped_ptr<OfflinePageModel> OfflinePageModelTest::BuildModel(
315 scoped_ptr<OfflinePageMetadataStore> store) {
316 return scoped_ptr<OfflinePageModel>(
317 new OfflinePageModel(store.Pass(), task_runner()));
320 void OfflinePageModelTest::ResetModel() {
321 model_->RemoveObserver(this);
322 OfflinePageTestStore* old_store = GetStore();
323 scoped_ptr<OfflinePageMetadataStore> new_store(
324 new OfflinePageTestStore(*old_store));
325 model_ = BuildModel(new_store.Pass()).Pass();
326 model_->AddObserver(this);
327 PumpLoop();
330 void OfflinePageModelTest::PumpLoop() {
331 run_loop_.reset(new base::RunLoop());
332 run_loop_->Run();
335 void OfflinePageModelTest::ResetResults() {
336 last_save_result_ = SavePageResult::CANCELLED;
337 last_delete_result_ = DeletePageResult::CANCELLED;
338 last_archiver_path_.clear();
341 OfflinePageTestStore* OfflinePageModelTest::GetStore() {
342 return static_cast<OfflinePageTestStore*>(model()->GetStoreForTesting());
345 TEST_F(OfflinePageModelTest, SavePageSuccessful) {
346 scoped_ptr<OfflinePageTestArchiver> archiver(
347 BuildArchiver(kTestUrl,
348 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
349 .Pass());
350 model()->SavePage(
351 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
352 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
353 PumpLoop();
355 OfflinePageTestStore* store = GetStore();
356 EXPECT_EQ(kTestUrl, store->last_saved_page().url);
357 EXPECT_EQ(kTestPageBookmarkId1, store->last_saved_page().bookmark_id);
358 // Save last_archiver_path since it will be referred to later.
359 base::FilePath archiver_path = last_archiver_path();
360 EXPECT_EQ(archiver_path, store->last_saved_page().file_path);
361 EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
362 EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
364 ResetResults();
366 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
368 EXPECT_EQ(1UL, offline_pages.size());
369 EXPECT_EQ(kTestUrl, offline_pages[0].url);
370 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
371 EXPECT_EQ(archiver_path, offline_pages[0].file_path);
372 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
375 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverCancelled) {
376 scoped_ptr<OfflinePageTestArchiver> archiver(
377 BuildArchiver(kTestUrl,
378 OfflinePageArchiver::ArchiverResult::ERROR_CANCELED)
379 .Pass());
380 model()->SavePage(
381 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
382 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
383 PumpLoop();
384 EXPECT_EQ(SavePageResult::CANCELLED, last_save_result());
387 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverDeviceFull) {
388 scoped_ptr<OfflinePageTestArchiver> archiver(
389 BuildArchiver(kTestUrl,
390 OfflinePageArchiver::ArchiverResult::ERROR_DEVICE_FULL)
391 .Pass());
392 model()->SavePage(
393 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
394 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
395 PumpLoop();
396 EXPECT_EQ(SavePageResult::DEVICE_FULL, last_save_result());
399 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverContentUnavailable) {
400 scoped_ptr<OfflinePageTestArchiver> archiver(
401 BuildArchiver(
402 kTestUrl,
403 OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE)
404 .Pass());
405 model()->SavePage(
406 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
407 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
408 PumpLoop();
409 EXPECT_EQ(SavePageResult::CONTENT_UNAVAILABLE, last_save_result());
412 TEST_F(OfflinePageModelTest, SavePageOfflineCreationFailed) {
413 scoped_ptr<OfflinePageTestArchiver> archiver(
414 BuildArchiver(
415 kTestUrl,
416 OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED)
417 .Pass());
418 model()->SavePage(
419 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
420 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
421 PumpLoop();
422 EXPECT_EQ(SavePageResult::ARCHIVE_CREATION_FAILED, last_save_result());
425 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverReturnedWrongUrl) {
426 scoped_ptr<OfflinePageTestArchiver> archiver(
427 BuildArchiver(GURL("http://other.random.url.com"),
428 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
429 .Pass());
430 model()->SavePage(
431 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
432 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
433 PumpLoop();
434 EXPECT_EQ(SavePageResult::ARCHIVE_CREATION_FAILED, last_save_result());
437 TEST_F(OfflinePageModelTest, SavePageOfflineCreationStoreWriteFailure) {
438 GetStore()->set_test_scenario(
439 OfflinePageTestStore::TestScenario::WRITE_FAILED);
440 scoped_ptr<OfflinePageTestArchiver> archiver(
441 BuildArchiver(kTestUrl,
442 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
443 .Pass());
444 model()->SavePage(
445 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
446 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
447 PumpLoop();
448 EXPECT_EQ(SavePageResult::STORE_FAILURE, last_save_result());
451 TEST_F(OfflinePageModelTest, SavePageOfflineArchiverTwoPages) {
452 scoped_ptr<OfflinePageTestArchiver> archiver(
453 BuildArchiver(kTestUrl,
454 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
455 .Pass());
456 // archiver_ptr will be valid until after first PumpLoop() call after
457 // CompleteCreateArchive() is called.
458 OfflinePageTestArchiver* archiver_ptr = archiver.get();
459 archiver_ptr->set_delayed(true);
460 model()->SavePage(
461 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
462 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
463 EXPECT_TRUE(archiver_ptr->create_archive_called());
465 // Request to save another page.
466 scoped_ptr<OfflinePageTestArchiver> archiver2(
467 BuildArchiver(kTestUrl2,
468 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
469 .Pass());
470 model()->SavePage(
471 kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
472 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
473 PumpLoop();
475 OfflinePageTestStore* store = GetStore();
477 EXPECT_EQ(kTestUrl2, store->last_saved_page().url);
478 EXPECT_EQ(kTestPageBookmarkId2, store->last_saved_page().bookmark_id);
479 base::FilePath archiver_path2 = last_archiver_path();
480 EXPECT_EQ(archiver_path2, store->last_saved_page().file_path);
481 EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
482 EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
484 ResetResults();
486 archiver_ptr->CompleteCreateArchive();
487 // After this pump loop archiver_ptr is invalid.
488 PumpLoop();
490 EXPECT_EQ(kTestUrl, store->last_saved_page().url);
491 EXPECT_EQ(kTestPageBookmarkId1, store->last_saved_page().bookmark_id);
492 base::FilePath archiver_path = last_archiver_path();
493 EXPECT_EQ(archiver_path, store->last_saved_page().file_path);
494 EXPECT_EQ(kTestFileSize, store->last_saved_page().file_size);
495 EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
497 ResetResults();
499 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
501 EXPECT_EQ(2UL, offline_pages.size());
502 EXPECT_EQ(kTestUrl, offline_pages[0].url);
503 EXPECT_EQ(kTestPageBookmarkId1, offline_pages[0].bookmark_id);
504 EXPECT_EQ(archiver_path, offline_pages[0].file_path);
505 EXPECT_EQ(kTestFileSize, offline_pages[0].file_size);
506 EXPECT_EQ(kTestUrl2, offline_pages[1].url);
507 EXPECT_EQ(kTestPageBookmarkId2, offline_pages[1].bookmark_id);
508 EXPECT_EQ(archiver_path2, offline_pages[1].file_path);
509 EXPECT_EQ(kTestFileSize, offline_pages[1].file_size);
512 TEST_F(OfflinePageModelTest, GetAllPagesStoreEmpty) {
513 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
515 EXPECT_EQ(0UL, offline_pages.size());
518 TEST_F(OfflinePageModelTest, GetAllPagesStoreFailure) {
519 GetStore()->set_test_scenario(
520 OfflinePageTestStore::TestScenario::LOAD_FAILED);
521 const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
523 EXPECT_EQ(0UL, offline_pages.size());
526 TEST_F(OfflinePageModelTest, DeletePageSuccessful) {
527 OfflinePageTestStore* store = GetStore();
529 // Save one page.
530 scoped_ptr<OfflinePageTestArchiver> archiver(
531 BuildArchiver(kTestUrl,
532 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
533 .Pass());
534 model()->SavePage(
535 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
536 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
537 PumpLoop();
539 EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
540 EXPECT_EQ(1u, store->offline_pages().size());
542 ResetResults();
544 // Save another page.
545 scoped_ptr<OfflinePageTestArchiver> archiver2(
546 BuildArchiver(kTestUrl2,
547 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
548 .Pass());
549 model()->SavePage(
550 kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
551 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
552 PumpLoop();
554 EXPECT_EQ(SavePageResult::SUCCESS, last_save_result());
555 EXPECT_EQ(2u, store->offline_pages().size());
557 ResetResults();
559 // Delete one page.
560 model()->DeletePageByBookmarkId(
561 kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
562 AsWeakPtr()));
564 PumpLoop();
566 EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result());
567 ASSERT_EQ(1u, store->offline_pages().size());
568 EXPECT_EQ(kTestUrl2, store->offline_pages()[0].url);
570 // Delete another page.
571 model()->DeletePageByBookmarkId(
572 kTestPageBookmarkId2, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
573 AsWeakPtr()));
575 ResetResults();
577 PumpLoop();
579 EXPECT_EQ(DeletePageResult::SUCCESS, last_delete_result());
580 EXPECT_EQ(0u, store->offline_pages().size());
583 TEST_F(OfflinePageModelTest, DeletePageNotFound) {
584 model()->DeletePageByBookmarkId(
585 kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
586 AsWeakPtr()));
587 EXPECT_EQ(DeletePageResult::NOT_FOUND, last_delete_result());
590 TEST_F(OfflinePageModelTest, DeletePageStoreFailureOnRemove) {
591 // Save a page.
592 scoped_ptr<OfflinePageTestArchiver> archiver(
593 BuildArchiver(kTestUrl,
594 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
595 .Pass());
596 model()->SavePage(
597 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
598 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
599 PumpLoop();
601 ResetResults();
603 // Try to delete this page.
604 GetStore()->set_test_scenario(
605 OfflinePageTestStore::TestScenario::REMOVE_FAILED);
606 model()->DeletePageByBookmarkId(
607 kTestPageBookmarkId1, base::Bind(&OfflinePageModelTest::OnDeletePageDone,
608 AsWeakPtr()));
609 PumpLoop();
610 EXPECT_EQ(DeletePageResult::STORE_FAILURE, last_delete_result());
613 TEST_F(OfflinePageModelTest, GetPageByBookmarkId) {
614 scoped_ptr<OfflinePageTestArchiver> archiver(
615 BuildArchiver(kTestUrl,
616 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
617 .Pass());
618 model()->SavePage(
619 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
620 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
621 PumpLoop();
623 scoped_ptr<OfflinePageTestArchiver> archiver2(
624 BuildArchiver(kTestUrl2,
625 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
626 .Pass());
627 model()->SavePage(
628 kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
629 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
630 PumpLoop();
632 const OfflinePageItem* page =
633 model()->GetPageByBookmarkId(kTestPageBookmarkId1);
634 ASSERT_TRUE(page);
635 EXPECT_EQ(kTestUrl, page->url);
636 EXPECT_EQ(kTestPageBookmarkId1, page->bookmark_id);
637 EXPECT_EQ(kTestFileSize, page->file_size);
639 page = model()->GetPageByBookmarkId(kTestPageBookmarkId2);
640 ASSERT_TRUE(page);
641 EXPECT_EQ(kTestUrl2, page->url);
642 EXPECT_EQ(kTestPageBookmarkId2, page->bookmark_id);
643 EXPECT_EQ(kTestFileSize, page->file_size);
645 page = model()->GetPageByBookmarkId(-42);
646 EXPECT_FALSE(page);
649 TEST_F(OfflinePageModelTest, GetPageByOfflineURL) {
650 scoped_ptr<OfflinePageTestArchiver> archiver(
651 BuildArchiver(kTestUrl,
652 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
653 .Pass());
654 model()->SavePage(
655 kTestUrl, kTestPageBookmarkId1, archiver.Pass(),
656 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
657 PumpLoop();
659 OfflinePageTestStore* store = GetStore();
660 GURL offline_url = store->last_saved_page().GetOfflineURL();
662 scoped_ptr<OfflinePageTestArchiver> archiver2(
663 BuildArchiver(kTestUrl2,
664 OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED)
665 .Pass());
666 model()->SavePage(
667 kTestUrl2, kTestPageBookmarkId2, archiver2.Pass(),
668 base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
669 PumpLoop();
671 GURL offline_url2 = store->last_saved_page().GetOfflineURL();
673 const OfflinePageItem* page = model()->GetPageByOfflineURL(offline_url2);
674 EXPECT_TRUE(page);
675 EXPECT_EQ(kTestUrl2, page->url);
676 EXPECT_EQ(kTestPageBookmarkId2, page->bookmark_id);
678 page = model()->GetPageByOfflineURL(offline_url);
679 EXPECT_TRUE(page);
680 EXPECT_EQ(kTestUrl, page->url);
681 EXPECT_EQ(kTestPageBookmarkId1, page->bookmark_id);
683 page = model()->GetPageByOfflineURL(GURL("http://foo"));
684 EXPECT_FALSE(page);
687 // Test that model returns pages that are older than 30 days as candidates for
688 // clean up, hence the numbers in time delta.
689 TEST_F(OfflinePageModelTest, GetPagesToCleanUp) {
690 base::Time now = base::Time::Now();
691 OfflinePageItem page_1(
692 GURL(kTestUrl), kTestPageBookmarkId1,
693 base::FilePath(FILE_PATH_LITERAL("/test/location/page1.mhtml")),
694 kTestFileSize, now - base::TimeDelta::FromDays(40));
695 GetStore()->AddOfflinePage(
696 page_1,
697 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
698 PumpLoop();
700 OfflinePageItem page_2(
701 GURL(kTestUrl2), kTestPageBookmarkId2,
702 base::FilePath(FILE_PATH_LITERAL("/test/location/page2.mhtml")),
703 kTestFileSize, now - base::TimeDelta::FromDays(31));
704 GetStore()->AddOfflinePage(
705 page_2,
706 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
707 PumpLoop();
709 OfflinePageItem page_3(
710 GURL("http://test.xyz"), 42,
711 base::FilePath(FILE_PATH_LITERAL("/test/location/page3.mhtml")),
712 kTestFileSize, now - base::TimeDelta::FromDays(29));
713 GetStore()->AddOfflinePage(
714 page_3,
715 base::Bind(&OfflinePageModelTest::OnStoreUpdateDone, AsWeakPtr()));
716 PumpLoop();
718 ResetModel();
720 // Only page_1 and page_2 are expected to be picked up by the model as page_3
721 // has not been in the store long enough.
722 std::vector<OfflinePageItem> pages_to_clean_up = model()->GetPagesToCleanUp();
723 EXPECT_EQ(2UL, pages_to_clean_up.size());
724 EXPECT_EQ(kTestUrl, pages_to_clean_up[0].url);
725 EXPECT_EQ(kTestPageBookmarkId1, pages_to_clean_up[0].bookmark_id);
726 EXPECT_EQ(kTestUrl2, pages_to_clean_up[1].url);
727 EXPECT_EQ(kTestPageBookmarkId2, pages_to_clean_up[1].bookmark_id);
730 } // namespace offline_pages