Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: components/offline_pages/offline_page_test_store.cc

Issue 2489443002: Move all components/offline_pages/ files into component/offline_pages/core (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/offline_pages/offline_page_test_store.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace offline_pages {
14
15 OfflinePageTestStore::OfflinePageTestStore(
16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
17 : task_runner_(task_runner),
18 scenario_(TestScenario::SUCCESSFUL),
19 store_state_(StoreState::NOT_LOADED) {}
20
21 OfflinePageTestStore::OfflinePageTestStore(
22 const OfflinePageTestStore& other_store)
23 : task_runner_(other_store.task_runner_),
24 scenario_(other_store.scenario_),
25 offline_pages_(other_store.offline_pages_) {}
26
27 OfflinePageTestStore::~OfflinePageTestStore() {}
28
29 void OfflinePageTestStore::Initialize(const InitializeCallback& callback) {
30 if (scenario_ == TestScenario::LOAD_FAILED_RESET_FAILED ||
31 scenario_ == TestScenario::LOAD_FAILED_RESET_SUCCESS) {
32 store_state_ = StoreState::FAILED_LOADING;
33 offline_pages_.clear();
34 } else {
35 store_state_ = StoreState::LOADED;
36 }
37 task_runner_->PostTask(
38 FROM_HERE, base::Bind(callback, store_state_ == StoreState::LOADED));
39 }
40
41 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) {
42 task_runner_->PostTask(FROM_HERE, base::Bind(callback, GetAllPages()));
43 }
44
45 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page,
46 const AddCallback& callback) {
47 // TODO(fgorski): Add and cover scenario with existing item.
48 ItemActionStatus result;
49 if (store_state_ == StoreState::LOADED &&
50 scenario_ != TestScenario::WRITE_FAILED) {
51 offline_pages_[offline_page.offline_id] = offline_page;
52 last_saved_page_ = offline_page;
53 result = ItemActionStatus::SUCCESS;
54 } else {
55 result = ItemActionStatus::STORE_ERROR;
56 }
57 if (!callback.is_null())
58 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result));
59 }
60
61 void OfflinePageTestStore::UpdateOfflinePages(
62 const std::vector<OfflinePageItem>& pages,
63 const UpdateCallback& callback) {
64 // TODO(fgorski): Cover scenario where new items are being created while they
65 // shouldn't.
66 std::unique_ptr<OfflinePagesUpdateResult> result(
67 new OfflinePagesUpdateResult(StoreState::LOADED));
68 if (scenario_ == TestScenario::WRITE_FAILED) {
69 for (const auto& page : pages) {
70 result->item_statuses.push_back(
71 std::make_pair(page.offline_id, ItemActionStatus::STORE_ERROR));
72 }
73 } else {
74 for (const auto& page : pages) {
75 offline_pages_[page.offline_id] = page;
76 last_saved_page_ = page;
77 result->item_statuses.push_back(
78 std::make_pair(page.offline_id, ItemActionStatus::SUCCESS));
79 }
80 result->updated_items.insert(result->updated_items.begin(), pages.begin(),
81 pages.end());
82 }
83 if (!callback.is_null())
84 task_runner_->PostTask(FROM_HERE,
85 base::Bind(callback, base::Passed(&result)));
86 }
87
88 void OfflinePageTestStore::RemoveOfflinePages(
89 const std::vector<int64_t>& offline_ids,
90 const UpdateCallback& callback) {
91 std::unique_ptr<OfflinePagesUpdateResult> result(
92 new OfflinePagesUpdateResult(StoreState::LOADED));
93
94 ASSERT_FALSE(offline_ids.empty());
95 if (scenario_ == TestScenario::REMOVE_FAILED) {
96 for (const auto& offline_id : offline_ids) {
97 result->item_statuses.push_back(
98 std::make_pair(offline_id, ItemActionStatus::STORE_ERROR));
99 }
100 // Anything different that LOADED is good here.
101 result->store_state = StoreState::FAILED_LOADING;
102 } else {
103 for (const auto& offline_id : offline_ids) {
104 auto iter = offline_pages_.find(offline_id);
105 ItemActionStatus status;
106 if (iter != offline_pages_.end()) {
107 result->updated_items.push_back(iter->second);
108 status = ItemActionStatus::SUCCESS;
109 offline_pages_.erase(iter);
110 } else {
111 status = ItemActionStatus::NOT_FOUND;
112 }
113 result->item_statuses.push_back(std::make_pair(offline_id, status));
114 }
115 }
116
117 task_runner_->PostTask(FROM_HERE,
118 base::Bind(callback, base::Passed(&result)));
119 }
120
121 void OfflinePageTestStore::Reset(const ResetCallback& callback) {
122 if (scenario_ == TestScenario::LOAD_FAILED_RESET_FAILED) {
123 store_state_ = StoreState::FAILED_RESET;
124 } else {
125 store_state_ = StoreState::NOT_LOADED;
126 // Scenario is flipped to successful here, as the reset succeeds.
127 if (scenario_ == TestScenario::LOAD_FAILED_RESET_SUCCESS)
128 scenario_ = TestScenario::SUCCESSFUL;
129 }
130
131 offline_pages_.clear();
132 task_runner_->PostTask(
133 FROM_HERE, base::Bind(callback, store_state_ == StoreState::NOT_LOADED));
134 }
135
136 StoreState OfflinePageTestStore::state() const {
137 return store_state_;
138 }
139
140 void OfflinePageTestStore::UpdateLastAccessTime(
141 int64_t offline_id,
142 const base::Time& last_access_time) {
143 auto iter = offline_pages_.find(offline_id);
144 if (iter == offline_pages_.end())
145 return;
146 iter->second.last_access_time = last_access_time;
147 }
148
149 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const {
150 std::vector<OfflinePageItem> offline_pages;
151 for (const auto& id_page_pair : offline_pages_)
152 offline_pages.push_back(id_page_pair.second);
153 return offline_pages;
154 }
155
156 void OfflinePageTestStore::ClearAllPages() {
157 offline_pages_.clear();
158 }
159
160 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/offline_page_test_store.h ('k') | components/offline_pages/offline_page_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698