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

Side by Side Diff: components/download/internal/controller_impl_unittest.cc

Issue 2895953004: Add initial Controller to DownloadService (Closed)
Patch Set: Rebased Created 3 years, 6 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 2017 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/download/internal/controller_impl.h"
6
7 #include <algorithm>
8 #include <memory>
9
10 #include "base/bind.h"
11 #include "base/guid.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h"
16 #include "components/download/internal/client_set.h"
17 #include "components/download/internal/config.h"
18 #include "components/download/internal/entry.h"
19 #include "components/download/internal/model_impl.h"
20 #include "components/download/internal/test/entry_utils.h"
21 #include "components/download/internal/test/mock_client.h"
22 #include "components/download/internal/test/test_download_driver.h"
23 #include "components/download/internal/test/test_store.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 using testing::_;
28
29 namespace download {
30
31 namespace {
32
33 class DownloadServiceControllerImplTest : public testing::Test {
34 public:
35 DownloadServiceControllerImplTest()
36 : task_runner_(new base::TestSimpleTaskRunner),
37 handle_(task_runner_),
38 client_(nullptr),
39 driver_(nullptr),
40 store_(nullptr) {
41 start_callback_ =
42 base::Bind(&DownloadServiceControllerImplTest::StartCallback,
43 base::Unretained(this));
44 }
45 ~DownloadServiceControllerImplTest() override = default;
46
47 void SetUp() override {
48 auto client = base::MakeUnique<test::MockClient>();
49 auto driver = base::MakeUnique<test::TestDownloadDriver>();
50 auto store = base::MakeUnique<test::TestStore>();
51 auto config = base::MakeUnique<Configuration>();
52
53 client_ = client.get();
54 driver_ = driver.get();
55 store_ = store.get();
56 config_ = config.get();
57
58 auto clients = base::MakeUnique<DownloadClientMap>();
59 clients->insert(std::make_pair(DownloadClient::TEST, std::move(client)));
60 auto client_set = base::MakeUnique<ClientSet>(std::move(clients));
61 auto model = base::MakeUnique<ModelImpl>(std::move(store));
62
63 controller_ = base::MakeUnique<ControllerImpl>(
64 std::move(client_set), std::move(config), std::move(driver),
65 std::move(model));
66 }
67
68 protected:
69 DownloadParams MakeDownloadParams() {
70 DownloadParams params;
71 params.client = DownloadClient::TEST;
72 params.guid = base::GenerateGUID();
73 params.callback = start_callback_;
74 return params;
75 }
76
77 MOCK_METHOD2(StartCallback,
78 void(const std::string&, DownloadParams::StartResult));
79
80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
81 base::ThreadTaskRunnerHandle handle_;
82
83 std::unique_ptr<ControllerImpl> controller_;
84 Configuration* config_;
85 test::MockClient* client_;
86 test::TestDownloadDriver* driver_;
87 test::TestStore* store_;
88
89 DownloadParams::StartCallback start_callback_;
90
91 private:
92 DISALLOW_COPY_AND_ASSIGN(DownloadServiceControllerImplTest);
93 };
94
95 } // namespace
96
97 TEST_F(DownloadServiceControllerImplTest, SuccessfulInitModelFirst) {
98 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(0);
99
100 controller_->Initialize();
101 EXPECT_TRUE(store_->init_called());
102 EXPECT_FALSE(controller_->GetStartupStatus().Complete());
103
104 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
105 EXPECT_FALSE(controller_->GetStartupStatus().Complete());
106 EXPECT_FALSE(controller_->GetStartupStatus().driver_ok.has_value());
107 EXPECT_TRUE(controller_->GetStartupStatus().model_ok.value());
108
109 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
110
111 driver_->MakeReady();
112 EXPECT_TRUE(controller_->GetStartupStatus().Complete());
113 EXPECT_TRUE(controller_->GetStartupStatus().driver_ok.value());
114 EXPECT_TRUE(controller_->GetStartupStatus().Ok());
115
116 task_runner_->RunUntilIdle();
117 }
118
119 TEST_F(DownloadServiceControllerImplTest, SuccessfulInitDriverFirst) {
120 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(0);
121
122 controller_->Initialize();
123 EXPECT_TRUE(store_->init_called());
124 EXPECT_FALSE(controller_->GetStartupStatus().Complete());
125
126 driver_->MakeReady();
127 EXPECT_FALSE(controller_->GetStartupStatus().Complete());
128 EXPECT_FALSE(controller_->GetStartupStatus().model_ok.has_value());
129 EXPECT_TRUE(controller_->GetStartupStatus().driver_ok.value());
130
131 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
132
133 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
134 EXPECT_TRUE(controller_->GetStartupStatus().Complete());
135 EXPECT_TRUE(controller_->GetStartupStatus().model_ok.value());
136 EXPECT_TRUE(controller_->GetStartupStatus().Ok());
137
138 task_runner_->RunUntilIdle();
139 }
140
141 TEST_F(DownloadServiceControllerImplTest, SuccessfulInitWithExistingDownload) {
142 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
143 Entry entry2 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
144 Entry entry3 =
145 test::BuildEntry(DownloadClient::INVALID, base::GenerateGUID());
146
147 std::vector<Entry> entries = {entry1, entry2, entry3};
148 std::vector<std::string> expected_guids = {entry1.guid, entry2.guid};
149
150 EXPECT_CALL(
151 *client_,
152 OnServiceInitialized(testing::UnorderedElementsAreArray(expected_guids)));
153
154 controller_->Initialize();
155 driver_->MakeReady();
156 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
157
158 task_runner_->RunUntilIdle();
159 }
160
161 TEST_F(DownloadServiceControllerImplTest, FailedInitWithBadModel) {
162 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(0);
163
164 controller_->Initialize();
165 store_->TriggerInit(false, base::MakeUnique<std::vector<Entry>>());
166 driver_->MakeReady();
167
168 task_runner_->RunUntilIdle();
169 }
170
171 TEST_F(DownloadServiceControllerImplTest, GetOwnerOfDownload) {
172 Entry entry = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
173 std::vector<Entry> entries = {entry};
174
175 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
176
177 controller_->Initialize();
178 driver_->MakeReady();
179 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
180
181 task_runner_->RunUntilIdle();
182
183 EXPECT_EQ(DownloadClient::TEST, controller_->GetOwnerOfDownload(entry.guid));
184 EXPECT_EQ(DownloadClient::INVALID,
185 controller_->GetOwnerOfDownload(base::GenerateGUID()));
186 }
187
188 TEST_F(DownloadServiceControllerImplTest, AddDownloadAccepted) {
189 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
190
191 // Set up the Controller.
192 controller_->Initialize();
193 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
194 driver_->MakeReady();
195 task_runner_->RunUntilIdle();
196
197 // Trigger the download.
198 DownloadParams params = MakeDownloadParams();
199 EXPECT_CALL(*this,
200 StartCallback(params.guid, DownloadParams::StartResult::ACCEPTED))
201 .Times(1);
202 controller_->StartDownload(params);
203
204 // TODO(dtrainor): Compare the full DownloadParams with the full Entry.
205 store_->TriggerUpdate(true);
206
207 task_runner_->RunUntilIdle();
208 }
209
210 TEST_F(DownloadServiceControllerImplTest, AddDownloadFailsWithBackoff) {
211 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
212
213 Entry entry = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
214 std::vector<Entry> entries = {entry};
215
216 // Set up the Controller.
217 controller_->Initialize();
218 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
219 driver_->MakeReady();
220 task_runner_->RunUntilIdle();
221
222 // Set the failure expectations.
223 config_->max_scheduled_downloads = 1U;
224
225 // Trigger the download.
226 DownloadParams params = MakeDownloadParams();
227 EXPECT_CALL(*this,
228 StartCallback(params.guid, DownloadParams::StartResult::BACKOFF))
229 .Times(1);
230 controller_->StartDownload(params);
231
232 EXPECT_TRUE(store_->updated_entries().empty());
233
234 task_runner_->RunUntilIdle();
235 }
236
237 TEST_F(DownloadServiceControllerImplTest,
238 AddDownloadFailsWithDuplicateGuidInModel) {
239 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
240
241 Entry entry = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
242 std::vector<Entry> entries = {entry};
243
244 // Set up the Controller.
245 controller_->Initialize();
246 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
247 driver_->MakeReady();
248 task_runner_->RunUntilIdle();
249
250 // Trigger the download.
251 DownloadParams params = MakeDownloadParams();
252 params.guid = entry.guid;
253 EXPECT_CALL(
254 *this,
255 StartCallback(params.guid, DownloadParams::StartResult::UNEXPECTED_GUID))
256 .Times(1);
257 controller_->StartDownload(params);
258
259 EXPECT_TRUE(store_->updated_entries().empty());
260
261 task_runner_->RunUntilIdle();
262 }
263
264 TEST_F(DownloadServiceControllerImplTest, AddDownloadFailsWithDuplicateCall) {
265 testing::InSequence sequence;
266
267 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
268
269 // Set up the Controller.
270 controller_->Initialize();
271 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
272 driver_->MakeReady();
273 task_runner_->RunUntilIdle();
274
275 // Trigger the download twice.
276 DownloadParams params = MakeDownloadParams();
277 EXPECT_CALL(
278 *this,
279 StartCallback(params.guid, DownloadParams::StartResult::UNEXPECTED_GUID))
280 .Times(1);
281 EXPECT_CALL(*this,
282 StartCallback(params.guid, DownloadParams::StartResult::ACCEPTED))
283 .Times(1);
284 controller_->StartDownload(params);
285 controller_->StartDownload(params);
286 store_->TriggerUpdate(true);
287
288 task_runner_->RunUntilIdle();
289 }
290
291 TEST_F(DownloadServiceControllerImplTest, AddDownloadFailsWithBadClient) {
292 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
293
294 // Set up the Controller.
295 controller_->Initialize();
296 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
297 driver_->MakeReady();
298 task_runner_->RunUntilIdle();
299
300 // Trigger the download.
301 DownloadParams params = MakeDownloadParams();
302 params.client = DownloadClient::INVALID;
303 EXPECT_CALL(*this,
304 StartCallback(params.guid,
305 DownloadParams::StartResult::UNEXPECTED_CLIENT))
306 .Times(1);
307 controller_->StartDownload(params);
308
309 task_runner_->RunUntilIdle();
310 }
311
312 TEST_F(DownloadServiceControllerImplTest, AddDownloadFailsWithClientCancel) {
313 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
314
315 // Set up the Controller.
316 controller_->Initialize();
317 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
318 driver_->MakeReady();
319 task_runner_->RunUntilIdle();
320
321 // Trigger the download.
322 DownloadParams params = MakeDownloadParams();
323 EXPECT_CALL(
324 *this,
325 StartCallback(params.guid, DownloadParams::StartResult::CLIENT_CANCELLED))
326 .Times(1);
327 controller_->StartDownload(params);
328
329 controller_->CancelDownload(params.guid);
330 store_->TriggerUpdate(true);
331
332 task_runner_->RunUntilIdle();
333 }
334
335 TEST_F(DownloadServiceControllerImplTest, AddDownloadFailsWithInternalError) {
336 EXPECT_CALL(*client_, OnServiceInitialized(_)).Times(1);
337
338 // Set up the Controller.
339 controller_->Initialize();
340 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
341 driver_->MakeReady();
342 task_runner_->RunUntilIdle();
343
344 // Trigger the download.
345 DownloadParams params = MakeDownloadParams();
346 EXPECT_CALL(*this, StartCallback(params.guid,
347 DownloadParams::StartResult::INTERNAL_ERROR))
348 .Times(1);
349 controller_->StartDownload(params);
350
351 store_->TriggerUpdate(false);
352
353 task_runner_->RunUntilIdle();
354 }
355
356 } // namespace download
OLDNEW
« no previous file with comments | « components/download/internal/controller_impl.cc ('k') | components/download/internal/download_driver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698