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> | |
| 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 28 matching lines...) Expand all Loading... | |
| 45 } else if (scenario_ == TestScenario::WRITE_FAILED) { | 47 } else if (scenario_ == TestScenario::WRITE_FAILED) { |
| 46 result = OfflinePageMetadataStore::STORE_ERROR; | 48 result = OfflinePageMetadataStore::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 |
|
Pete Williamson
2016/09/15 18:05:43
nit - this sentence would read better with no comm
fgorski
2016/09/19 23:24:30
Done.
| |
| 56 if (result) { | 58 // they shouldn't. |
| 57 // TODO(fgorski): Cover scenario, where new items are being created, while | 59 std::map<int64_t, ItemActionStatus> failed_results; |
| 58 // they shouldn't. | 60 std::vector<OfflinePageItem> updated_items; |
| 59 for (auto& page : pages) { | 61 if (scenario_ == TestScenario::WRITE_FAILED) { |
| 62 for (const auto& page : pages) | |
| 63 failed_results[page.offline_id] = STORE_ERROR; | |
| 64 } else { | |
| 65 for (const auto& page : pages) { | |
| 60 offline_pages_[page.offline_id] = page; | 66 offline_pages_[page.offline_id] = page; |
| 61 last_saved_page_ = page; | 67 last_saved_page_ = page; |
| 62 } | 68 } |
| 69 updated_items.insert(updated_items.begin(), pages.begin(), pages.end()); | |
| 63 } | 70 } |
| 64 if (!callback.is_null()) | 71 if (!callback.is_null()) |
| 65 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 72 task_runner_->PostTask(FROM_HERE, |
| 73 base::Bind(callback, failed_results, updated_items)); | |
| 66 } | 74 } |
| 67 | 75 |
| 68 void OfflinePageTestStore::RemoveOfflinePages( | 76 void OfflinePageTestStore::RemoveOfflinePages( |
| 69 const std::vector<int64_t>& offline_ids, | 77 const std::vector<int64_t>& offline_ids, |
| 70 const UpdateCallback& callback) { | 78 const UpdateCallback& callback) { |
| 79 std::map<int64_t, ItemActionStatus> failed_results; | |
| 80 std::vector<OfflinePageItem> deleted_items; | |
| 71 ASSERT_FALSE(offline_ids.empty()); | 81 ASSERT_FALSE(offline_ids.empty()); |
| 72 bool result = false; | 82 if (scenario_ == TestScenario::REMOVE_FAILED) { |
| 73 if (scenario_ != TestScenario::REMOVE_FAILED) { | 83 for (const auto& offline_id : offline_ids) |
| 84 failed_results[offline_id] = STORE_ERROR; | |
| 85 } else { | |
| 74 for (const auto& offline_id : offline_ids) { | 86 for (const auto& offline_id : offline_ids) { |
| 75 auto iter = offline_pages_.find(offline_id); | 87 auto iter = offline_pages_.find(offline_id); |
| 76 if (iter != offline_pages_.end()) { | 88 if (iter != offline_pages_.end()) { |
| 89 deleted_items.push_back(iter->second); | |
| 77 offline_pages_.erase(iter); | 90 offline_pages_.erase(iter); |
| 78 result = true; | 91 } else { |
| 92 failed_results[offline_id] = DOESNT_EXIST; | |
| 79 } | 93 } |
| 80 } | 94 } |
| 81 } | 95 } |
| 82 | 96 |
| 83 task_runner_->PostTask(FROM_HERE, base::Bind(callback, result)); | 97 task_runner_->PostTask(FROM_HERE, |
| 98 base::Bind(callback, failed_results, deleted_items)); | |
| 84 } | 99 } |
| 85 | 100 |
| 86 void OfflinePageTestStore::Reset(const ResetCallback& callback) { | 101 void OfflinePageTestStore::Reset(const ResetCallback& callback) { |
| 87 offline_pages_.clear(); | 102 offline_pages_.clear(); |
| 88 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); | 103 task_runner_->PostTask(FROM_HERE, base::Bind(callback, true)); |
| 89 } | 104 } |
| 90 | 105 |
| 91 OfflinePageMetadataStore::StoreState OfflinePageTestStore::state() const { | 106 OfflinePageMetadataStore::StoreState OfflinePageTestStore::state() const { |
| 92 return LOADED; | 107 return LOADED; |
| 93 } | 108 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 106 for (const auto& id_page_pair : offline_pages_) | 121 for (const auto& id_page_pair : offline_pages_) |
| 107 offline_pages.push_back(id_page_pair.second); | 122 offline_pages.push_back(id_page_pair.second); |
| 108 return offline_pages; | 123 return offline_pages; |
| 109 } | 124 } |
| 110 | 125 |
| 111 void OfflinePageTestStore::ClearAllPages() { | 126 void OfflinePageTestStore::ClearAllPages() { |
| 112 offline_pages_.clear(); | 127 offline_pages_.clear(); |
| 113 } | 128 } |
| 114 | 129 |
| 115 } // namespace offline_pages | 130 } // namespace offline_pages |
| OLD | NEW |