Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(208)

Side by Side Diff: components/offline_pages/downloads/download_ui_adapter_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698