Chromium Code Reviews| 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> | |
|
dewittj
2016/09/23 17:21:36
no longer necessary
| |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/location.h" | 10 #include "base/location.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 12 |
| 11 namespace offline_pages { | 13 namespace offline_pages { |
| 12 | 14 |
| 13 OfflinePageTestStore::OfflinePageTestStore( | 15 OfflinePageTestStore::OfflinePageTestStore( |
| 14 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | 16 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) |
| 15 : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) {} | 17 : task_runner_(task_runner), scenario_(TestScenario::SUCCESSFUL) {} |
| 16 | 18 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 30 } else { | 32 } else { |
| 31 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; | 33 load_status = OfflinePageMetadataStore::LOAD_SUCCEEDED; |
| 32 } | 34 } |
| 33 task_runner_->PostTask(FROM_HERE, | 35 task_runner_->PostTask(FROM_HERE, |
| 34 base::Bind(callback, load_status, GetAllPages())); | 36 base::Bind(callback, load_status, GetAllPages())); |
| 35 } | 37 } |
| 36 | 38 |
| 37 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, | 39 void OfflinePageTestStore::AddOfflinePage(const OfflinePageItem& offline_page, |
| 38 const AddCallback& callback) { | 40 const AddCallback& callback) { |
| 39 // TODO(fgorski): Add and cover scenario with existing item. | 41 // TODO(fgorski): Add and cover scenario with existing item. |
| 40 OfflinePageMetadataStore::ItemActionStatus result; | 42 ItemActionStatus result; |
| 41 if (scenario_ == TestScenario::SUCCESSFUL) { | 43 if (scenario_ == TestScenario::SUCCESSFUL) { |
| 42 offline_pages_[offline_page.offline_id] = offline_page; | 44 offline_pages_[offline_page.offline_id] = offline_page; |
| 43 last_saved_page_ = offline_page; | 45 last_saved_page_ = offline_page; |
| 44 result = OfflinePageMetadataStore::SUCCESS; | 46 result = ItemActionStatus::SUCCESS; |
| 45 } else if (scenario_ == TestScenario::WRITE_FAILED) { | 47 } else if (scenario_ == TestScenario::WRITE_FAILED) { |
| 46 result = OfflinePageMetadataStore::STORE_ERROR; | 48 result = ItemActionStatus::STORE_ERROR; |
| 47 } | 49 } |
| 48 if (!callback.is_null()) | 50 if (!callback.is_null()) |
| 49 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 51 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void OfflinePageTestStore::UpdateOfflinePages( | 54 void OfflinePageTestStore::UpdateOfflinePages( |
| 53 const std::vector<OfflinePageItem>& pages, | 55 const std::vector<OfflinePageItem>& pages, |
| 54 const UpdateCallback& callback) { | 56 const UpdateCallback& callback) { |
| 55 bool result = scenario_ != TestScenario::WRITE_FAILED; | 57 // TODO(fgorski): Cover scenario where new items are being created while they |
| 56 if (result) { | 58 // shouldn't. |
| 57 // TODO(fgorski): Cover scenario, where new items are being created, while | 59 std::unique_ptr<StoreUpdateResult> result( |
| 58 // they shouldn't. | 60 new StoreUpdateResult(StoreState::LOADED)); |
| 59 for (auto& page : pages) { | 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) { | |
| 60 offline_pages_[page.offline_id] = page; | 68 offline_pages_[page.offline_id] = page; |
| 61 last_saved_page_ = page; | 69 last_saved_page_ = page; |
| 70 result->item_statuses.push_back( | |
| 71 std::make_pair(page.offline_id, ItemActionStatus::SUCCESS)); | |
| 62 } | 72 } |
| 73 result->updated_items.insert(result->updated_items.begin(), pages.begin(), | |
| 74 pages.end()); | |
| 63 } | 75 } |
| 64 if (!callback.is_null()) | 76 if (!callback.is_null()) |
| 65 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 77 task_runner_->PostTask(FROM_HERE, |
| 78 base::Bind(callback, base::Passed(&result))); | |
| 66 } | 79 } |
| 67 | 80 |
| 68 void OfflinePageTestStore::RemoveOfflinePages( | 81 void OfflinePageTestStore::RemoveOfflinePages( |
| 69 const std::vector<int64_t>& offline_ids, | 82 const std::vector<int64_t>& offline_ids, |
| 70 const UpdateCallback& callback) { | 83 const UpdateCallback& callback) { |
| 84 std::unique_ptr<StoreUpdateResult> result( | |
| 85 new StoreUpdateResult(StoreState::LOADED)); | |
| 86 | |
| 71 ASSERT_FALSE(offline_ids.empty()); | 87 ASSERT_FALSE(offline_ids.empty()); |
| 72 bool result = false; | 88 if (scenario_ == TestScenario::REMOVE_FAILED) { |
| 73 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 { | |
| 74 for (const auto& offline_id : offline_ids) { | 96 for (const auto& offline_id : offline_ids) { |
| 75 auto iter = offline_pages_.find(offline_id); | 97 auto iter = offline_pages_.find(offline_id); |
| 98 ItemActionStatus status; | |
| 76 if (iter != offline_pages_.end()) { | 99 if (iter != offline_pages_.end()) { |
| 100 result->updated_items.push_back(iter->second); | |
| 101 status = ItemActionStatus::SUCCESS; | |
| 77 offline_pages_.erase(iter); | 102 offline_pages_.erase(iter); |
| 78 result = true; | 103 } else { |
| 104 status = ItemActionStatus::NOT_FOUND; | |
| 79 } | 105 } |
| 106 result->item_statuses.push_back(std::make_pair(offline_id, status)); | |
| 80 } | 107 } |
| 81 } | 108 } |
| 82 | 109 |
| 83 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 110 task_runner_->PostTask(FROM_HERE, |
| 111 base::Bind(callback, base::Passed(&result))); | |
| 84 } | 112 } |
| 85 | 113 |
| 86 void OfflinePageTestStore::Reset(const ResetCallback& callback) { | 114 void OfflinePageTestStore::Reset(const ResetCallback& callback) { |
| 87 offline_pages_.clear(); | 115 offline_pages_.clear(); |
| 88 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 116 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
| 89 } | 117 } |
| 90 | 118 |
| 91 OfflinePageMetadataStore::StoreState OfflinePageTestStore::state() const { | 119 StoreState OfflinePageTestStore::state() const { |
| 92 return LOADED; | 120 return StoreState::LOADED; |
| 93 } | 121 } |
| 94 | 122 |
| 95 void OfflinePageTestStore::UpdateLastAccessTime( | 123 void OfflinePageTestStore::UpdateLastAccessTime( |
| 96 int64_t offline_id, | 124 int64_t offline_id, |
| 97 const base::Time& last_access_time) { | 125 const base::Time& last_access_time) { |
| 98 auto iter = offline_pages_.find(offline_id); | 126 auto iter = offline_pages_.find(offline_id); |
| 99 if (iter == offline_pages_.end()) | 127 if (iter == offline_pages_.end()) |
| 100 return; | 128 return; |
| 101 iter->second.last_access_time = last_access_time; | 129 iter->second.last_access_time = last_access_time; |
| 102 } | 130 } |
| 103 | 131 |
| 104 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { | 132 std::vector<OfflinePageItem> OfflinePageTestStore::GetAllPages() const { |
| 105 std::vector<OfflinePageItem> offline_pages; | 133 std::vector<OfflinePageItem> offline_pages; |
| 106 for (const auto& id_page_pair : offline_pages_) | 134 for (const auto& id_page_pair : offline_pages_) |
| 107 offline_pages.push_back(id_page_pair.second); | 135 offline_pages.push_back(id_page_pair.second); |
| 108 return offline_pages; | 136 return offline_pages; |
| 109 } | 137 } |
| 110 | 138 |
| 111 void OfflinePageTestStore::ClearAllPages() { | 139 void OfflinePageTestStore::ClearAllPages() { |
| 112 offline_pages_.clear(); | 140 offline_pages_.clear(); |
| 113 } | 141 } |
| 114 | 142 |
| 115 } // namespace offline_pages | 143 } // namespace offline_pages |
| OLD | NEW |