| 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 reset_called_(false), |
| 20 store_state_(StoreState::NOT_LOADED) {} |
| 18 | 21 |
| 19 OfflinePageTestStore::OfflinePageTestStore( | 22 OfflinePageTestStore::OfflinePageTestStore( |
| 20 const OfflinePageTestStore& other_store) | 23 const OfflinePageTestStore& other_store) |
| 21 : task_runner_(other_store.task_runner_), | 24 : task_runner_(other_store.task_runner_), |
| 22 scenario_(other_store.scenario_), | 25 scenario_(other_store.scenario_), |
| 26 reset_called_(false), |
| 27 store_state_(StoreState::NOT_LOADED), |
| 23 offline_pages_(other_store.offline_pages_) {} | 28 offline_pages_(other_store.offline_pages_) {} |
| 24 | 29 |
| 25 OfflinePageTestStore::~OfflinePageTestStore() {} | 30 OfflinePageTestStore::~OfflinePageTestStore() {} |
| 26 | 31 |
| 27 void OfflinePageTestStore::GetOfflinePages(const LoadCallback& callback) { | 32 void OfflinePageTestStore::GetOfflinePages( |
| 28 OfflinePageMetadataStore::LoadStatus load_status; | 33 const GetOfflinePagesCallback& callback) { |
| 29 if (scenario_ == TestScenario::LOAD_FAILED) { | 34 if (scenario_ == TestScenario::RESET_SUCCESS) { |
| 30 load_status = OfflinePageMetadataStore::STORE_LOAD_FAILED; | 35 store_state_ = StoreState::FAILED_LOADING; |
| 36 offline_pages_.clear(); |
| 37 } else if (scenario_ == TestScenario::RESET_FAILED) { |
| 38 // Load has to fail first, then reset will fail. |
| 39 store_state_ = StoreState::FAILED_LOADING; |
| 31 offline_pages_.clear(); | 40 offline_pages_.clear(); |
| 32 } else { | 41 } else { |
| 33 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; | 42 store_state_ = StoreState::LOADED; |
| 34 } | 43 } |
| 35 task_runner_->PostTask(FROM_HERE, | 44 task_runner_->PostTask(FROM_HERE, |
| 36 base::Bind(callback, load_status, GetAllPages())); | 45 base::Bind(callback, store_state_, GetAllPages())); |
| 37 } | 46 } |
| 38 | 47 |
| 39 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, | 48 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, |
| 40 const AddCallback& callback) { | 49 const AddCallback& callback) { |
| 41 // TODO(fgorski): Add and cover scenario with existing item. | 50 // TODO(fgorski): Add and cover scenario with existing item. |
| 42 ItemActionStatus result; | 51 ItemActionStatus result; |
| 43 if (scenario_ == TestScenario::SUCCESSFUL) { | 52 if (scenario_ == TestScenario::SUCCESSFUL) { |
| 44 offline_pages_[offline_page.offline_id] = offline_page; | 53 offline_pages_[offline_page.offline_id] = offline_page; |
| 45 last_saved_page_ = offline_page; | 54 last_saved_page_ = offline_page; |
| 46 result = ItemActionStatus::SUCCESS; | 55 result = ItemActionStatus::SUCCESS; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 std::unique_ptr<OfflinePagesUpdateResult> result( | 93 std::unique_ptr<OfflinePagesUpdateResult> result( |
| 85 new OfflinePagesUpdateResult(StoreState::LOADED)); | 94 new OfflinePagesUpdateResult(StoreState::LOADED)); |
| 86 | 95 |
| 87 ASSERT_FALSE(offline_ids.empty()); | 96 ASSERT_FALSE(offline_ids.empty()); |
| 88 if (scenario_ == TestScenario::REMOVE_FAILED) { | 97 if (scenario_ == TestScenario::REMOVE_FAILED) { |
| 89 for (const auto& offline_id : offline_ids) { | 98 for (const auto& offline_id : offline_ids) { |
| 90 result->item_statuses.push_back( | 99 result->item_statuses.push_back( |
| 91 std::make_pair(offline_id, ItemActionStatus::STORE_ERROR)); | 100 std::make_pair(offline_id, ItemActionStatus::STORE_ERROR)); |
| 92 } | 101 } |
| 93 // Anything different that LOADED is good here. | 102 // Anything different that LOADED is good here. |
| 94 result->store_state = StoreState::FAILED_LOADING; | 103 result->store_state = StoreState::FAILED_OPERATION; |
| 95 } else { | 104 } else { |
| 96 for (const auto& offline_id : offline_ids) { | 105 for (const auto& offline_id : offline_ids) { |
| 97 auto iter = offline_pages_.find(offline_id); | 106 auto iter = offline_pages_.find(offline_id); |
| 98 ItemActionStatus status; | 107 ItemActionStatus status; |
| 99 if (iter != offline_pages_.end()) { | 108 if (iter != offline_pages_.end()) { |
| 100 result->updated_items.push_back(iter->second); | 109 result->updated_items.push_back(iter->second); |
| 101 status = ItemActionStatus::SUCCESS; | 110 status = ItemActionStatus::SUCCESS; |
| 102 offline_pages_.erase(iter); | 111 offline_pages_.erase(iter); |
| 103 } else { | 112 } else { |
| 104 status = ItemActionStatus::NOT_FOUND; | 113 status = ItemActionStatus::NOT_FOUND; |
| 105 } | 114 } |
| 106 result->item_statuses.push_back(std::make_pair(offline_id, status)); | 115 result->item_statuses.push_back(std::make_pair(offline_id, status)); |
| 107 } | 116 } |
| 108 } | 117 } |
| 109 | 118 |
| 110 task_runner_->PostTask(FROM_HERE, | 119 task_runner_->PostTask(FROM_HERE, |
| 111 base::Bind(callback, base::Passed(&result))); | 120 base::Bind(callback, base::Passed(&result))); |
| 112 } | 121 } |
| 113 | 122 |
| 114 void OfflinePageTestStore::Reset(const ResetCallback& callback) { | 123 void OfflinePageTestStore::Reset(const ResetCallback& callback) { |
| 115 offline_pages_.clear(); | 124 offline_pages_.clear(); |
| 116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 125 reset_called_ = true; |
| 126 if (scenario_ == TestScenario::RESET_FAILED) { |
| 127 store_state_ = StoreState::FAILED_RESET; |
| 128 } else { |
| 129 // Flip scenario to successful after this, so that next read works. |
| 130 store_state_ = StoreState::LOADED; |
| 131 scenario_ = TestScenario::SUCCESSFUL; |
| 132 } |
| 133 |
| 134 task_runner_->PostTask( |
| 135 FROM_HERE, base::Bind(callback, store_state_ == StoreState::LOADED)); |
| 117 } | 136 } |
| 118 | 137 |
| 119 StoreState OfflinePageTestStore::state() const { | 138 StoreState OfflinePageTestStore::state() const { |
| 120 return StoreState::LOADED; | 139 return store_state_; |
| 121 } | 140 } |
| 122 | 141 |
| 123 void OfflinePageTestStore::UpdateLastAccessTime( | 142 void OfflinePageTestStore::UpdateLastAccessTime( |
| 124 int64_t offline_id, | 143 int64_t offline_id, |
| 125 const base::Time& last_access_time) { | 144 const base::Time& last_access_time) { |
| 126 auto iter = offline_pages_.find(offline_id); | 145 auto iter = offline_pages_.find(offline_id); |
| 127 if (iter == offline_pages_.end()) | 146 if (iter == offline_pages_.end()) |
| 128 return; | 147 return; |
| 129 iter->second.last_access_time = last_access_time; | 148 iter->second.last_access_time = last_access_time; |
| 130 } | 149 } |
| 131 | 150 |
| 132 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { | 151 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { |
| 133 std::vector<OfflinePageItem> offline_pages; | 152 std::vector<OfflinePageItem> offline_pages; |
| 134 for (const auto& id_page_pair : offline_pages_) | 153 for (const auto& id_page_pair : offline_pages_) |
| 135 offline_pages.push_back(id_page_pair.second); | 154 offline_pages.push_back(id_page_pair.second); |
| 136 return offline_pages; | 155 return offline_pages; |
| 137 } | 156 } |
| 138 | 157 |
| 139 void OfflinePageTestStore::ClearAllPages() { | 158 void OfflinePageTestStore::ClearAllPages() { |
| 140 offline_pages_.clear(); | 159 offline_pages_.clear(); |
| 141 } | 160 } |
| 142 | 161 |
| 143 } // namespace offline_pages | 162 } // namespace offline_pages |
| OLD | NEW |