summaryrefslogtreecommitdiffstats
path: root/components/offline_pages/offline_page_test_store.cc
blob: 755191ce2b70565b5c773cdc999454b0e5833b36 (plain)
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
// 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_test_store.h"

#include "base/bind.h"
#include "base/location.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace offline_pages {

OfflinePageTestStore::OfflinePageTestStore(
    const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
    : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) {}

OfflinePageTestStore::OfflinePageTestStore(
    const OfflinePageTestStore& other_store)
    : task_runner_(other_store.task_runner_),
      scenario_(other_store.scenario_),
      offline_pages_(other_store.offline_pages_) {}

OfflinePageTestStore::~OfflinePageTestStore() {}

void OfflinePageTestStore::Load(const LoadCallback& callback) {
  OfflinePageMetadataStore::LoadStatus load_status;
  if (scenario_ == TestScenario::LOAD_FAILED) {
    load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED;
    offline_pages_.clear();
  } else {
    load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED;
  }
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(callback, load_status, GetAllPages()));
}

void OfflinePageTestStore::AddOrUpdateOfflinePage(
    const OfflinePageItem& offline_page,
    const UpdateCallback& callback) {
  last_saved_page_ = offline_page;
  bool result = scenario_ != TestScenario::WRITE_FAILED;
  if (result)
    offline_pages_[offline_page.bookmark_id] = offline_page;
  task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
}

void OfflinePageTestStore::RemoveOfflinePages(
    const std::vector<int64>& bookmark_ids,
    const UpdateCallback& callback) {
  ASSERT_FALSE(bookmark_ids.empty());
  bool result = false;
  if (scenario_ != TestScenario::REMOVE_FAILED) {
    for (const auto& bookmark_id : bookmark_ids) {
      auto iter = offline_pages_.find(bookmark_id);
      if (iter != offline_pages_.end()) {
        offline_pages_.erase(iter);
        result = true;
      }
    }
  }

  task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
}

void OfflinePageTestStore::Reset(const ResetCallback& callback) {
  offline_pages_.clear();
  task_runner_->PostTask(FROM_HERE, base::Bind(callback, true));
}

void OfflinePageTestStore::UpdateLastAccessTime(
    int64 bookmark_id,
    const base::Time& last_access_time) {
  auto iter = offline_pages_.find(bookmark_id);
  if (iter == offline_pages_.end())
    return;
  iter->second.last_access_time = last_access_time;
}

std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const {
  std::vector<OfflinePageItem> offline_pages;
  for (const auto& id_page_pair : offline_pages_)
    offline_pages.push_back(id_page_pair.second);
  return offline_pages;
}

}  // namespace offline_pages