| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/offline_pages/offline_page_test_store.h" | 5 #include "components/offline_pages/offline_page_test_store.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 namespace offline_pages { | 13 namespace offline_pages { |
| 14 | 14 |
| 15 OfflinePageTestStore::OfflinePageTestStore( | 15 OfflinePageTestStore::OfflinePageTestStore( |
| 16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 17 : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) {} | 17 : task_runner_(task_runner), |
| 18 scenario_(TestScenario::SUCCESSFUL), |
| 19 store_state_(StoreState::NOT_LOADED) {} |
| 18 | 20 |
| 19 OfflinePageTestStore::OfflinePageTestStore( | 21 OfflinePageTestStore::OfflinePageTestStore( |
| 20 const OfflinePageTestStore& other_store) | 22 const OfflinePageTestStore& other_store) |
| 21 : task_runner_(other_store.task_runner_), | 23 : task_runner_(other_store.task_runner_), |
| 22 scenario_(other_store.scenario_), | 24 scenario_(other_store.scenario_), |
| 23 offline_pages_(other_store.offline_pages_) {} | 25 offline_pages_(other_store.offline_pages_) {} |
| 24 | 26 |
| 25 OfflinePageTestStore::~OfflinePageTestStore() {} | 27 OfflinePageTestStore::~OfflinePageTestStore() {} |
| 26 | 28 |
| 27 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) { | 29 void OfflinePageTestStore::Initialize(const InitializeCallback& callback) { |
| 28 OfflinePageMetadataStore::LoadStatus load_status; | 30 if (scenario_ == TestScenario::LOAD_FAILED_RESET_FAILED || |
| 29 if (scenario_ == TestScenario::LOAD_FAILED) { | 31 scenario_ == TestScenario::LOAD_FAILED_RESET_SUCCESS) { |
| 30 load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED; | 32 store_state_ = StoreState::FAILED_LOADING; |
| 31 offline_pages_.clear(); | 33 offline_pages_.clear(); |
| 32 } else { | 34 } else { |
| 33 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; | 35 store_state_ = StoreState::LOADED; |
| 34 } | 36 } |
| 35 task_runner_->PostTask(FROM_HERE, | 37 task_runner_->PostTask( |
| 36 base::Bind(callback, load_status, GetAllPages())); | 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())); |
| 37 } | 43 } |
| 38 | 44 |
| 39 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, | 45 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, |
| 40 const AddCallback& callback) { | 46 const AddCallback& callback) { |
| 41 // TODO(fgorski): Add and cover scenario with existing item. | 47 // TODO(fgorski): Add and cover scenario with existing item. |
| 42 ItemActionStatus result; | 48 ItemActionStatus result; |
| 43 if (scenario_ == TestScenario::SUCCESSFUL) { | 49 if (store_state_ == StoreState::LOADED && |
| 50 scenario_ != TestScenario::WRITE_FAILED) { |
| 44 offline_pages_[offline_page.offline_id] = offline_page; | 51 offline_pages_[offline_page.offline_id] = offline_page; |
| 45 last_saved_page_ = offline_page; | 52 last_saved_page_ = offline_page; |
| 46 result = ItemActionStatus::SUCCESS; | 53 result = ItemActionStatus::SUCCESS; |
| 47 } else if (scenario_ == TestScenario::WRITE_FAILED) { | 54 } else { |
| 48 result = ItemActionStatus::STORE_ERROR; | 55 result = ItemActionStatus::STORE_ERROR; |
| 49 } | 56 } |
| 50 if (!callback.is_null()) | 57 if (!callback.is_null()) |
| 51 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 58 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 52 } | 59 } |
| 53 | 60 |
| 54 void OfflinePageTestStore::UpdateOfflinePages( | 61 void OfflinePageTestStore::UpdateOfflinePages( |
| 55 const std::vector<OfflinePageItem>& pages, | 62 const std::vector<OfflinePageItem>& pages, |
| 56 const UpdateCallback& callback) { | 63 const UpdateCallback& callback) { |
| 57 // TODO(fgorski): Cover scenario where new items are being created while they | 64 // TODO(fgorski): Cover scenario where new items are being created while they |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 } | 112 } |
| 106 result->item_statuses.push_back(std::make_pair(offline_id, status)); | 113 result->item_statuses.push_back(std::make_pair(offline_id, status)); |
| 107 } | 114 } |
| 108 } | 115 } |
| 109 | 116 |
| 110 task_runner_->PostTask(FROM_HERE, | 117 task_runner_->PostTask(FROM_HERE, |
| 111 base::Bind(callback, base::Passed(&result))); | 118 base::Bind(callback, base::Passed(&result))); |
| 112 } | 119 } |
| 113 | 120 |
| 114 void OfflinePageTestStore::Reset(const ResetCallback& callback) { | 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 |
| 115 offline_pages_.clear(); | 131 offline_pages_.clear(); |
| 116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 132 task_runner_->PostTask( |
| 133 FROM_HERE, base::Bind(callback, store_state_ == StoreState::NOT_LOADED)); |
| 117 } | 134 } |
| 118 | 135 |
| 119 StoreState OfflinePageTestStore::state() const { | 136 StoreState OfflinePageTestStore::state() const { |
| 120 return StoreState::LOADED; | 137 return store_state_; |
| 121 } | 138 } |
| 122 | 139 |
| 123 void OfflinePageTestStore::UpdateLastAccessTime( | 140 void OfflinePageTestStore::UpdateLastAccessTime( |
| 124 int64_t offline_id, | 141 int64_t offline_id, |
| 125 const base::Time& last_access_time) { | 142 const base::Time& last_access_time) { |
| 126 auto iter = offline_pages_.find(offline_id); | 143 auto iter = offline_pages_.find(offline_id); |
| 127 if (iter == offline_pages_.end()) | 144 if (iter == offline_pages_.end()) |
| 128 return; | 145 return; |
| 129 iter->second.last_access_time = last_access_time; | 146 iter->second.last_access_time = last_access_time; |
| 130 } | 147 } |
| 131 | 148 |
| 132 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { | 149 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { |
| 133 std::vector<OfflinePageItem> offline_pages; | 150 std::vector<OfflinePageItem> offline_pages; |
| 134 for (const auto& id_page_pair : offline_pages_) | 151 for (const auto& id_page_pair : offline_pages_) |
| 135 offline_pages.push_back(id_page_pair.second); | 152 offline_pages.push_back(id_page_pair.second); |
| 136 return offline_pages; | 153 return offline_pages; |
| 137 } | 154 } |
| 138 | 155 |
| 139 void OfflinePageTestStore::ClearAllPages() { | 156 void OfflinePageTestStore::ClearAllPages() { |
| 140 offline_pages_.clear(); | 157 offline_pages_.clear(); |
| 141 } | 158 } |
| 142 | 159 |
| 143 } // namespace offline_pages | 160 } // namespace offline_pages |
| OLD | NEW |