OLD | NEW |
| (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), scenario_(TestScenario::SUCCESSFUL) {} | |
18 | |
19 OfflinePageTestStore::OfflinePageTestStore( | |
20 const OfflinePageTestStore& other_store) | |
21 : task_runner_(other_store.task_runner_), | |
22 scenario_(other_store.scenario_), | |
23 offline_pages_(other_store.offline_pages_) {} | |
24 | |
25 OfflinePageTestStore::~OfflinePageTestStore() {} | |
26 | |
27 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) { | |
28 OfflinePageMetadataStore::LoadStatus load_status; | |
29 if (scenario_ == TestScenario::LOAD_FAILED) { | |
30 load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED; | |
31 offline_pages_.clear(); | |
32 } else { | |
33 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; | |
34 } | |
35 task_runner_->PostTask(FROM_HERE, | |
36 base::Bind(callback, load_status, GetAllPages())); | |
37 } | |
38 | |
39 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, | |
40 const AddCallback& callback) { | |
41 // TODO(fgorski): Add and cover scenario with existing item. | |
42 ItemActionStatus result; | |
43 if (scenario_ == TestScenario::SUCCESSFUL) { | |
44 offline_pages_[offline_page.offline_id] = offline_page; | |
45 last_saved_page_ = offline_page; | |
46 result = ItemActionStatus::SUCCESS; | |
47 } else if (scenario_ == TestScenario::WRITE_FAILED) { | |
48 result = ItemActionStatus::STORE_ERROR; | |
49 } | |
50 if (!callback.is_null()) | |
51 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | |
52 } | |
53 | |
54 void OfflinePageTestStore::UpdateOfflinePages( | |
55 const std::vector<OfflinePageItem>& pages, | |
56 const UpdateCallback& callback) { | |
57 // TODO(fgorski): Cover scenario where new items are being created while they | |
58 // shouldn't. | |
59 std::unique_ptr<OfflinePagesUpdateResult> result( | |
60 new OfflinePagesUpdateResult(StoreState::LOADED)); | |
61 if (scenario_ == TestScenario::WRITE_FAILED) { | |
62 for (const auto& page : pages) { | |
63 result->item_statuses.push_back( | |
64 std::make_pair(page.offline_id, ItemActionStatus::STORE_ERROR)); | |
65 } | |
66 } else { | |
67 for (const auto& page : pages) { | |
68 offline_pages_[page.offline_id] = page; | |
69 last_saved_page_ = page; | |
70 result->item_statuses.push_back( | |
71 std::make_pair(page.offline_id, ItemActionStatus::SUCCESS)); | |
72 } | |
73 result->updated_items.insert(result->updated_items.begin(), pages.begin(), | |
74 pages.end()); | |
75 } | |
76 if (!callback.is_null()) | |
77 task_runner_->PostTask(FROM_HERE, | |
78 base::Bind(callback, base::Passed(&result))); | |
79 } | |
80 | |
81 void OfflinePageTestStore::RemoveOfflinePages( | |
82 const std::vector<int64_t>& offline_ids, | |
83 const UpdateCallback& callback) { | |
84 std::unique_ptr<OfflinePagesUpdateResult> result( | |
85 new OfflinePagesUpdateResult(StoreState::LOADED)); | |
86 | |
87 ASSERT_FALSE(offline_ids.empty()); | |
88 if (scenario_ == TestScenario::REMOVE_FAILED) { | |
89 for (const auto& offline_id : offline_ids) { | |
90 result->item_statuses.push_back( | |
91 std::make_pair(offline_id, ItemActionStatus::STORE_ERROR)); | |
92 } | |
93 // Anything different that LOADED is good here. | |
94 result->store_state = StoreState::FAILED_LOADING; | |
95 } else { | |
96 for (const auto& offline_id : offline_ids) { | |
97 auto iter = offline_pages_.find(offline_id); | |
98 ItemActionStatus status; | |
99 if (iter != offline_pages_.end()) { | |
100 result->updated_items.push_back(iter->second); | |
101 status = ItemActionStatus::SUCCESS; | |
102 offline_pages_.erase(iter); | |
103 } else { | |
104 status = ItemActionStatus::NOT_FOUND; | |
105 } | |
106 result->item_statuses.push_back(std::make_pair(offline_id, status)); | |
107 } | |
108 } | |
109 | |
110 task_runner_->PostTask(FROM_HERE, | |
111 base::Bind(callback, base::Passed(&result))); | |
112 } | |
113 | |
114 void OfflinePageTestStore::Reset(const ResetCallback& callback) { | |
115 offline_pages_.clear(); | |
116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | |
117 } | |
118 | |
119 StoreState OfflinePageTestStore::state() const { | |
120 return StoreState::LOADED; | |
121 } | |
122 | |
123 void OfflinePageTestStore::UpdateLastAccessTime( | |
124 int64_t offline_id, | |
125 const base::Time& last_access_time) { | |
126 auto iter = offline_pages_.find(offline_id); | |
127 if (iter == offline_pages_.end()) | |
128 return; | |
129 iter->second.last_access_time = last_access_time; | |
130 } | |
131 | |
132 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { | |
133 std::vector<OfflinePageItem> offline_pages; | |
134 for (const auto& id_page_pair : offline_pages_) | |
135 offline_pages.push_back(id_page_pair.second); | |
136 return offline_pages; | |
137 } | |
138 | |
139 void OfflinePageTestStore::ClearAllPages() { | |
140 offline_pages_.clear(); | |
141 } | |
142 | |
143 } // namespace offline_pages | |
OLD | NEW |