| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/downloads/download_ui_adapter.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <map> |
| 10 #include <memory> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/bind.h" |
| 15 #include "base/callback.h" |
| 16 #include "base/files/file_path.h" |
| 17 #include "base/run_loop.h" |
| 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/test/test_mock_time_task_runner.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "base/time/time.h" |
| 22 #include "components/offline_pages/client_namespace_constants.h" |
| 23 #include "components/offline_pages/stub_offline_page_model.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 namespace offline_pages { |
| 27 |
| 28 namespace { |
| 29 // Constants for a test OfflinePageItem. |
| 30 static const int kTestOfflineId1 = 1; |
| 31 static const int kTestOfflineId2 = 2; |
| 32 static const int kTestOfflineId3 = 3; |
| 33 static const char kTestUrl[] = "http://foo.com/bar.mhtml"; |
| 34 static const char kTestGuid1[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc1"; |
| 35 static const char kTestGuid2[] = "cccccccc-cccc-4ccc-0ccc-ccccccccccc2"; |
| 36 static const char kTestBadGuid[] = "ccccccc-cccc-0ccc-0ccc-ccccccccccc0"; |
| 37 static const ClientId kTestClientIdOtherNamespace(kLastNNamespace, kTestGuid1); |
| 38 static const ClientId kTestClientIdOtherGuid(kLastNNamespace, kTestBadGuid); |
| 39 static const ClientId kTestClientId1(kAsyncNamespace, kTestGuid1); |
| 40 static const ClientId kTestClientId2(kAsyncNamespace, kTestGuid2); |
| 41 static const base::FilePath kTestFilePath = |
| 42 base::FilePath(FILE_PATH_LITERAL("foo/bar.mhtml")); |
| 43 static const int kFileSize = 1000; |
| 44 static const base::Time kTestCreationTime = base::Time::Now(); |
| 45 } // namespace |
| 46 |
| 47 // Mock OfflinePageModel for testing the SavePage calls. |
| 48 class MockOfflinePageModel : public StubOfflinePageModel { |
| 49 public: |
| 50 MockOfflinePageModel(base::TestMockTimeTaskRunner* task_runner) |
| 51 : observer_(nullptr), |
| 52 task_runner_(task_runner) { |
| 53 adapter.reset(new DownloadUIAdapter(this)); |
| 54 // Add one page. |
| 55 OfflinePageItem page(GURL(kTestUrl), |
| 56 kTestOfflineId1, |
| 57 kTestClientId1, |
| 58 kTestFilePath, |
| 59 kFileSize, |
| 60 kTestCreationTime); |
| 61 pages[kTestOfflineId1] = page; |
| 62 } |
| 63 |
| 64 ~MockOfflinePageModel() override {} |
| 65 |
| 66 // OfflinePageModel overrides. |
| 67 void AddObserver(Observer* observer) override { |
| 68 EXPECT_TRUE(observer != nullptr); |
| 69 observer_ = observer; |
| 70 } |
| 71 |
| 72 void RemoveObserver(Observer* observer) override { |
| 73 EXPECT_TRUE(observer != nullptr); |
| 74 EXPECT_EQ(observer, observer_); |
| 75 observer_ = nullptr; |
| 76 } |
| 77 |
| 78 // PostTask instead of just running callback to simpulate the real class. |
| 79 void GetAllPages(const MultipleOfflinePageItemCallback& callback) override { |
| 80 task_runner_->PostTask( |
| 81 FROM_HERE, base::Bind(&MockOfflinePageModel::GetAllPagesImpl, |
| 82 base::Unretained(this), callback)); |
| 83 } |
| 84 |
| 85 void GetAllPagesImpl(const MultipleOfflinePageItemCallback& callback) { |
| 86 std::vector<OfflinePageItem> result; |
| 87 for (const auto& page : pages) |
| 88 result.push_back(page.second); |
| 89 callback.Run(result); |
| 90 } |
| 91 |
| 92 void DeletePageAndNotifyAdapter(const std::string& guid) { |
| 93 for(const auto& page : pages) { |
| 94 if (page.second.client_id.id == guid) { |
| 95 observer_->OfflinePageDeleted(page.second.offline_id, |
| 96 page.second.client_id); |
| 97 pages.erase(page.first); |
| 98 return; |
| 99 } |
| 100 } |
| 101 } |
| 102 |
| 103 void AddPageAndNotifyAdapter(const OfflinePageItem& page) { |
| 104 EXPECT_EQ(pages.end(), pages.find(page.offline_id)); |
| 105 pages[page.offline_id] = page; |
| 106 observer_->OfflinePageModelChanged(this); |
| 107 } |
| 108 |
| 109 // Normally, OfflinePageModel owns this adapter, so lets test it this way. |
| 110 std::unique_ptr<DownloadUIAdapter> adapter; |
| 111 |
| 112 std::map<int64_t, OfflinePageItem> pages; |
| 113 |
| 114 private: |
| 115 OfflinePageModel::Observer* observer_; |
| 116 base::TestMockTimeTaskRunner* task_runner_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(MockOfflinePageModel); |
| 119 }; |
| 120 |
| 121 class DownloadUIAdapterTest |
| 122 : public testing::Test, |
| 123 public DownloadUIAdapter::Observer { |
| 124 public: |
| 125 DownloadUIAdapterTest(); |
| 126 ~DownloadUIAdapterTest() override; |
| 127 |
| 128 // testing::Test |
| 129 void SetUp() override; |
| 130 |
| 131 // DownloadUIAdapter::Observer |
| 132 void ItemsLoaded() override; |
| 133 void ItemAdded(const DownloadUIItem& item) override; |
| 134 void ItemUpdated(const DownloadUIItem& item) override; |
| 135 void ItemDeleted(const std::string& guid) override; |
| 136 |
| 137 // Runs until all of the tasks that are not delayed are gone from the task |
| 138 // queue. |
| 139 void PumpLoop(); |
| 140 |
| 141 bool items_loaded; |
| 142 std::vector<std::string> added_guids, updated_guids, deleted_guids; |
| 143 std::unique_ptr<MockOfflinePageModel> model; |
| 144 |
| 145 private: |
| 146 scoped_refptr<base::TestMockTimeTaskRunner> task_runner_; |
| 147 }; |
| 148 |
| 149 DownloadUIAdapterTest::DownloadUIAdapterTest() |
| 150 : items_loaded(false), |
| 151 task_runner_(new base::TestMockTimeTaskRunner) { |
| 152 } |
| 153 |
| 154 DownloadUIAdapterTest::~DownloadUIAdapterTest() { |
| 155 } |
| 156 |
| 157 void DownloadUIAdapterTest::SetUp() { |
| 158 model.reset(new MockOfflinePageModel(task_runner_.get())); |
| 159 model->adapter->AddObserver(this); |
| 160 } |
| 161 |
| 162 void DownloadUIAdapterTest::ItemsLoaded() { |
| 163 items_loaded = true; |
| 164 } |
| 165 |
| 166 void DownloadUIAdapterTest::ItemAdded(const DownloadUIItem& item) { |
| 167 added_guids.push_back(item.guid); |
| 168 } |
| 169 |
| 170 void DownloadUIAdapterTest::ItemUpdated(const DownloadUIItem& item) { |
| 171 updated_guids.push_back(item.guid); |
| 172 } |
| 173 |
| 174 void DownloadUIAdapterTest::ItemDeleted(const std::string& guid) { |
| 175 deleted_guids.push_back(guid); |
| 176 } |
| 177 |
| 178 void DownloadUIAdapterTest::PumpLoop() { |
| 179 task_runner_->RunUntilIdle(); |
| 180 } |
| 181 |
| 182 TEST_F(DownloadUIAdapterTest, InitialLoad) { |
| 183 EXPECT_NE(nullptr, model->adapter); |
| 184 EXPECT_FALSE(items_loaded); |
| 185 PumpLoop(); |
| 186 EXPECT_TRUE(items_loaded); |
| 187 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); |
| 188 EXPECT_NE(nullptr, item); |
| 189 } |
| 190 |
| 191 TEST_F(DownloadUIAdapterTest, InitialItemConversion) { |
| 192 EXPECT_EQ(1UL, model->pages.size()); |
| 193 EXPECT_EQ(kTestGuid1, model->pages[kTestOfflineId1].client_id.id); |
| 194 PumpLoop(); |
| 195 const DownloadUIItem* item = model->adapter->GetItem(kTestGuid1); |
| 196 EXPECT_EQ(kTestGuid1, item->guid); |
| 197 EXPECT_EQ(kTestUrl, item->url.spec()); |
| 198 EXPECT_EQ(kTestFilePath, item->target_path); |
| 199 EXPECT_EQ(kTestCreationTime, item->start_time); |
| 200 EXPECT_EQ(kFileSize, item->total_bytes); |
| 201 } |
| 202 |
| 203 TEST_F(DownloadUIAdapterTest, ItemDeletedAdded) { |
| 204 PumpLoop(); |
| 205 // Add page, notify adapter. |
| 206 OfflinePageItem page(GURL(kTestUrl), |
| 207 kTestOfflineId2, |
| 208 kTestClientId2, |
| 209 base::FilePath(kTestFilePath), |
| 210 kFileSize, |
| 211 kTestCreationTime); |
| 212 model->AddPageAndNotifyAdapter(page); |
| 213 PumpLoop(); |
| 214 EXPECT_EQ(1UL, added_guids.size()); |
| 215 EXPECT_EQ(kTestGuid2, added_guids[0]); |
| 216 // Remove pages, notify adapter. |
| 217 model->DeletePageAndNotifyAdapter(kTestGuid1); |
| 218 model->DeletePageAndNotifyAdapter(kTestGuid2); |
| 219 PumpLoop(); |
| 220 EXPECT_EQ(2UL, deleted_guids.size()); |
| 221 EXPECT_EQ(kTestGuid1, deleted_guids[0]); |
| 222 EXPECT_EQ(kTestGuid2, deleted_guids[1]); |
| 223 } |
| 224 |
| 225 TEST_F(DownloadUIAdapterTest, ItemWithWrongNamespace) { |
| 226 PumpLoop(); |
| 227 OfflinePageItem page1(GURL(kTestUrl), |
| 228 kTestOfflineId2, |
| 229 kTestClientIdOtherNamespace, |
| 230 base::FilePath(kTestFilePath), |
| 231 kFileSize, |
| 232 kTestCreationTime); |
| 233 model->AddPageAndNotifyAdapter(page1); |
| 234 PumpLoop(); |
| 235 // Should not add the page with wrong namespace. |
| 236 EXPECT_EQ(0UL, added_guids.size()); |
| 237 |
| 238 OfflinePageItem page2(GURL(kTestUrl), |
| 239 kTestOfflineId3, |
| 240 kTestClientIdOtherGuid, |
| 241 base::FilePath(kTestFilePath), |
| 242 kFileSize, |
| 243 kTestCreationTime); |
| 244 model->AddPageAndNotifyAdapter(page2); |
| 245 PumpLoop(); |
| 246 // Should not add the page with wrong guid. |
| 247 EXPECT_EQ(0UL, added_guids.size()); |
| 248 } |
| 249 |
| 250 TEST_F(DownloadUIAdapterTest, ItemUpdated) { |
| 251 PumpLoop(); |
| 252 // Clear the initial page and replace it with updated one. |
| 253 model->pages.clear(); |
| 254 // Add page with the same offline_id/guid, notify adapter. |
| 255 // This should generate 'updated' notification. |
| 256 OfflinePageItem page1(GURL(kTestUrl), |
| 257 kTestOfflineId1, |
| 258 kTestClientId1, |
| 259 base::FilePath(kTestFilePath), |
| 260 kFileSize, |
| 261 kTestCreationTime); |
| 262 // Add a new page which did not exist before. |
| 263 OfflinePageItem page2(GURL(kTestUrl), |
| 264 kTestOfflineId2, |
| 265 kTestClientId2, |
| 266 base::FilePath(kTestFilePath), |
| 267 kFileSize, |
| 268 kTestCreationTime); |
| 269 model->AddPageAndNotifyAdapter(page1); |
| 270 model->AddPageAndNotifyAdapter(page2); |
| 271 PumpLoop(); |
| 272 EXPECT_EQ(1UL, added_guids.size()); |
| 273 EXPECT_EQ(kTestGuid2, added_guids[0]); |
| 274 // TODO(dimich): we currently don't report updated items since OPM doesn't |
| 275 // have support for that. Add as needed, this will have to be updated when |
| 276 // support is added. |
| 277 EXPECT_EQ(0UL, updated_guids.size()); |
| 278 } |
| 279 |
| 280 } // namespace offline_pages |
| OLD | NEW |