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

Side by Side Diff: content/browser/background_fetch/background_fetch_service_unittest.cc

Issue 2786783002: Dispatch a bare Service Worker event for a finished Background Fetch (Closed)
Patch Set: Dispatch a bare Service Worker event for a finished Background Fetch Created 3 years, 8 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
« no previous file with comments | « content/browser/background_fetch/background_fetch_service_impl.h ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <memory>
6
7 #include "base/macros.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/run_loop.h"
10 #include "content/browser/background_fetch/background_fetch_context.h"
11 #include "content/browser/background_fetch/background_fetch_embedded_worker_test _helper.h"
12 #include "content/browser/background_fetch/background_fetch_registration_id.h"
13 #include "content/browser/background_fetch/background_fetch_service_impl.h"
14 #include "content/browser/background_fetch/background_fetch_test_base.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h"
16 #include "content/browser/storage_partition_impl.h"
17
18 namespace content {
19 namespace {
20
21 const char kExampleTag[] = "my-background-fetch";
22 const char kAlternativeTag[] = "my-alternative-fetch";
23
24 IconDefinition CreateIcon(std::string src,
25 std::string sizes,
26 std::string type) {
27 IconDefinition icon;
28 icon.src = std::move(src);
29 icon.sizes = std::move(sizes);
30 icon.type = std::move(type);
31
32 return icon;
33 }
34
35 class BackgroundFetchServiceTest : public BackgroundFetchTestBase {
36 public:
37 BackgroundFetchServiceTest() = default;
38 ~BackgroundFetchServiceTest() override = default;
39
40 // Synchronous wrapper for BackgroundFetchServiceImpl::Fetch().
41 // Should be wrapped in ASSERT_NO_FATAL_FAILURE().
42 void Fetch(const BackgroundFetchRegistrationId& registration_id,
43 const std::vector<ServiceWorkerFetchRequest>& requests,
44 const BackgroundFetchOptions& options,
45 blink::mojom::BackgroundFetchError* out_error,
46 BackgroundFetchRegistration* out_registration) {
47 DCHECK(out_error);
48 DCHECK(out_registration);
49
50 base::RunLoop run_loop;
51 service_->Fetch(registration_id.service_worker_registration_id(),
52 registration_id.origin(), registration_id.tag(), requests,
53 options,
54 base::Bind(&BackgroundFetchServiceTest::DidGetRegistration,
55 base::Unretained(this), run_loop.QuitClosure(),
56 out_error, out_registration));
57
58 run_loop.Run();
59 }
60
61 // TODO(harkness): Add tests for UpdateUI() when its functionality has been
62 // implemented.
63
64 // Synchronous wrapper for BackgroundFetchServiceImpl::Abort().
65 // Should be wrapped in ASSERT_NO_FATAL_FAILURE().
66 void Abort(const BackgroundFetchRegistrationId& registration_id,
67 blink::mojom::BackgroundFetchError* out_error) {
68 DCHECK(out_error);
69
70 base::RunLoop run_loop;
71 service_->Abort(
72 registration_id.service_worker_registration_id(),
73 registration_id.origin(), registration_id.tag(),
74 base::Bind(&BackgroundFetchServiceTest::DidAbort,
75 base::Unretained(this), run_loop.QuitClosure(), out_error));
76
77 run_loop.Run();
78 }
79
80 // Synchronous wrapper for BackgroundFetchServiceImpl::GetRegistration().
81 // Should be wrapped in ASSERT_NO_FATAL_FAILURE().
82 void GetRegistration(const BackgroundFetchRegistrationId& registration_id,
83 blink::mojom::BackgroundFetchError* out_error,
84 BackgroundFetchRegistration* out_registration) {
85 DCHECK(out_error);
86 DCHECK(out_registration);
87
88 base::RunLoop run_loop;
89 service_->GetRegistration(
90 registration_id.service_worker_registration_id(),
91 registration_id.origin(), registration_id.tag(),
92 base::Bind(&BackgroundFetchServiceTest::DidGetRegistration,
93 base::Unretained(this), run_loop.QuitClosure(), out_error,
94 out_registration));
95
96 run_loop.Run();
97 }
98
99 // Synchronous wrapper for BackgroundFetchServiceImpl::GetTags().
100 void GetTags(const BackgroundFetchRegistrationId& registration_id,
101 blink::mojom::BackgroundFetchError* out_error,
102 std::vector<std::string>* out_tags) {
103 DCHECK(out_error);
104 DCHECK(out_tags);
105
106 base::RunLoop run_loop;
107 service_->GetTags(registration_id.service_worker_registration_id(),
108 registration_id.origin(),
109 base::Bind(&BackgroundFetchServiceTest::DidGetTags,
110 base::Unretained(this), run_loop.QuitClosure(),
111 out_error, out_tags));
112
113 run_loop.Run();
114 }
115
116 // BackgroundFetchTestBase overrides:
117 void SetUp() override {
118 BackgroundFetchTestBase::SetUp();
119 StoragePartitionImpl* storage_partition =
120 static_cast<StoragePartitionImpl*>(
121 BrowserContext::GetDefaultStoragePartition(browser_context()));
122
123 context_ = new BackgroundFetchContext(
124 browser_context(), storage_partition,
125 make_scoped_refptr(embedded_worker_test_helper()->context_wrapper()));
126 service_ = base::MakeUnique<BackgroundFetchServiceImpl>(
127 0 /* render_process_id */, context_);
128 }
129
130 void TearDown() override {
131 service_.reset();
132
133 context_->Shutdown();
134 context_ = nullptr;
135
136 // Give pending shutdown operations a chance to finish.
137 base::RunLoop().RunUntilIdle();
138
139 BackgroundFetchTestBase::TearDown();
140 }
141
142 private:
143 void DidGetRegistration(
144 base::Closure quit_closure,
145 blink::mojom::BackgroundFetchError* out_error,
146 BackgroundFetchRegistration* out_registration,
147 blink::mojom::BackgroundFetchError error,
148 const base::Optional<content::BackgroundFetchRegistration>&
149 registration) {
150 *out_error = error;
151 if (registration)
152 *out_registration = registration.value();
153
154 quit_closure.Run();
155 }
156
157 void DidAbort(base::Closure quit_closure,
158 blink::mojom::BackgroundFetchError* out_error,
159 blink::mojom::BackgroundFetchError error) {
160 *out_error = error;
161
162 quit_closure.Run();
163 }
164
165 void DidGetTags(base::Closure quit_closure,
166 blink::mojom::BackgroundFetchError* out_error,
167 std::vector<std::string>* out_tags,
168 blink::mojom::BackgroundFetchError error,
169 const std::vector<std::string>& tags) {
170 *out_error = error;
171 *out_tags = tags;
172
173 quit_closure.Run();
174 }
175
176 scoped_refptr<BackgroundFetchContext> context_;
177 std::unique_ptr<BackgroundFetchServiceImpl> service_;
178
179 DISALLOW_COPY_AND_ASSIGN(BackgroundFetchServiceTest);
180 };
181
182 TEST_F(BackgroundFetchServiceTest, FetchInvalidArguments) {
183 // This test verifies that the Fetch() function will kill the renderer and
184 // return INVALID_ARGUMENT when invalid data is send over the Mojo channel.
185
186 BackgroundFetchOptions options;
187
188 // The `tag` must be a non-empty string.
189 {
190 BackgroundFetchRegistrationId registration_id(
191 42 /* service_worker_registration_id */, origin(), "" /* tag */);
192
193 std::vector<ServiceWorkerFetchRequest> requests;
194 requests.emplace_back(); // empty, but valid
195
196 blink::mojom::BackgroundFetchError error;
197 BackgroundFetchRegistration registration;
198
199 ASSERT_NO_FATAL_FAILURE(
200 Fetch(registration_id, requests, options, &error, &registration));
201 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
202 }
203
204 // At least a single ServiceWorkerFetchRequest must be given.
205 {
206 BackgroundFetchRegistrationId registration_id(
207 42 /* service_worker_registration_id */, origin(), kExampleTag);
208
209 std::vector<ServiceWorkerFetchRequest> requests;
210 // |requests| has deliberately been left empty.
211
212 blink::mojom::BackgroundFetchError error;
213 BackgroundFetchRegistration registration;
214
215 ASSERT_NO_FATAL_FAILURE(
216 Fetch(registration_id, requests, options, &error, &registration));
217 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
218 }
219 }
220
221 TEST_F(BackgroundFetchServiceTest, FetchRegistrationProperties) {
222 // This test starts a new Background Fetch and verifies that the returned
223 // BackgroundFetchRegistration object matches the given options. Then gets
224 // the active Background Fetch with the same tag, and verifies it again.
225
226 BackgroundFetchRegistrationId registration_id;
227 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
228
229 std::vector<ServiceWorkerFetchRequest> requests;
230 requests.emplace_back(); // empty, but valid
231
232 BackgroundFetchOptions options;
233 options.icons.push_back(CreateIcon("funny_cat.png", "256x256", "image/png"));
234 options.icons.push_back(CreateIcon("silly_cat.gif", "512x512", "image/gif"));
235 options.title = "My Background Fetch!";
236 options.total_download_size = 9001;
237
238 blink::mojom::BackgroundFetchError error;
239 BackgroundFetchRegistration registration;
240
241 ASSERT_NO_FATAL_FAILURE(
242 Fetch(registration_id, requests, options, &error, &registration));
243 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
244
245 // The |registration| should reflect the options given in |options|.
246 EXPECT_EQ(registration.tag, kExampleTag);
247 ASSERT_EQ(registration.icons.size(), options.icons.size());
248
249 for (size_t i = 0; i < registration.icons.size(); ++i) {
250 EXPECT_EQ(registration.icons[i].src, options.icons[i].src);
251 EXPECT_EQ(registration.icons[i].sizes, options.icons[i].sizes);
252 EXPECT_EQ(registration.icons[i].type, options.icons[i].type);
253 }
254
255 EXPECT_EQ(registration.title, options.title);
256 EXPECT_EQ(registration.total_download_size, options.total_download_size);
257
258 blink::mojom::BackgroundFetchError second_error;
259 BackgroundFetchRegistration second_registration;
260
261 ASSERT_NO_FATAL_FAILURE(
262 GetRegistration(registration_id, &second_error, &second_registration));
263 ASSERT_EQ(second_error, blink::mojom::BackgroundFetchError::NONE);
264
265 // The |second_registration| should reflect the options given in |options|.
266 EXPECT_EQ(second_registration.tag, kExampleTag);
267 ASSERT_EQ(second_registration.icons.size(), options.icons.size());
268
269 for (size_t i = 0; i < second_registration.icons.size(); ++i) {
270 EXPECT_EQ(second_registration.icons[i].src, options.icons[i].src);
271 EXPECT_EQ(second_registration.icons[i].sizes, options.icons[i].sizes);
272 EXPECT_EQ(second_registration.icons[i].type, options.icons[i].type);
273 }
274
275 EXPECT_EQ(second_registration.title, options.title);
276 EXPECT_EQ(second_registration.total_download_size,
277 options.total_download_size);
278 }
279
280 TEST_F(BackgroundFetchServiceTest, FetchDuplicatedRegistrationFailure) {
281 // This tests starts a new Background Fetch, verifies that a registration was
282 // successfully created, and then tries to start a second fetch for the same
283 // registration. This should fail with a DUPLICATED_TAG error.
284
285 BackgroundFetchRegistrationId registration_id;
286 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
287
288 std::vector<ServiceWorkerFetchRequest> requests;
289 requests.emplace_back(); // empty, but valid
290
291 BackgroundFetchOptions options;
292
293 blink::mojom::BackgroundFetchError error;
294 BackgroundFetchRegistration registration;
295
296 // Create the first registration. This must succeed.
297 ASSERT_NO_FATAL_FAILURE(
298 Fetch(registration_id, requests, options, &error, &registration));
299 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
300
301 blink::mojom::BackgroundFetchError second_error;
302 BackgroundFetchRegistration second_registration;
303
304 // Create the second registration with the same data. This must fail.
305 ASSERT_NO_FATAL_FAILURE(Fetch(registration_id, requests, options,
306 &second_error, &second_registration));
307 ASSERT_EQ(second_error, blink::mojom::BackgroundFetchError::DUPLICATED_TAG);
308 }
309
310 TEST_F(BackgroundFetchServiceTest, Abort) {
311 // This test starts a new Background Fetch, completes the registration, and
312 // then aborts the Background Fetch mid-process. Tests all of StartFetch(),
313 // GetActiveFetches() and GetActiveTagsForServiceWorkerRegistration().
314
315 BackgroundFetchRegistrationId registration_id;
316 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
317
318 std::vector<ServiceWorkerFetchRequest> requests;
319 requests.emplace_back(); // empty, but valid
320
321 BackgroundFetchOptions options;
322
323 blink::mojom::BackgroundFetchError error;
324 BackgroundFetchRegistration registration;
325
326 // Create the registration. This must succeed.
327 ASSERT_NO_FATAL_FAILURE(
328 Fetch(registration_id, requests, options, &error, &registration));
329 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
330
331 blink::mojom::BackgroundFetchError abort_error;
332
333 // Immediately abort the registration. This also is expected to succeed.
334 ASSERT_NO_FATAL_FAILURE(Abort(registration_id, &abort_error));
335 ASSERT_EQ(abort_error, blink::mojom::BackgroundFetchError::NONE);
336
337 blink::mojom::BackgroundFetchError second_error;
338 BackgroundFetchRegistration second_registration;
339
340 // Now try to get the created registration, which is expected to fail.
341 ASSERT_NO_FATAL_FAILURE(
342 GetRegistration(registration_id, &second_error, &second_registration));
343 ASSERT_EQ(second_error, blink::mojom::BackgroundFetchError::INVALID_TAG);
344 }
345
346 TEST_F(BackgroundFetchServiceTest, AbortInvalidArguments) {
347 // This test verifies that the Abort() function will kill the renderer and
348 // return INVALID_ARGUMENT when invalid data is send over the Mojo channel.
349
350 BackgroundFetchOptions options;
351
352 // The `tag` must be a non-empty string.
353 {
354 BackgroundFetchRegistrationId registration_id(
355 42 /* service_worker_registration_id */, origin(), "" /* tag */);
356
357 blink::mojom::BackgroundFetchError error;
358
359 ASSERT_NO_FATAL_FAILURE(Abort(registration_id, &error));
360 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_ARGUMENT);
361 }
362 }
363
364 TEST_F(BackgroundFetchServiceTest, GetTags) {
365 // This test verifies that the list of active tags can be retrieved from the
366 // service for a given Service Worker, as extracted from a registration.
367
368 BackgroundFetchRegistrationId registration_id;
369 ASSERT_TRUE(CreateRegistrationId(kExampleTag, &registration_id));
370
371 BackgroundFetchRegistrationId second_registration_id;
372 ASSERT_TRUE(CreateRegistrationId(kAlternativeTag, &second_registration_id));
373
374 std::vector<ServiceWorkerFetchRequest> requests;
375 requests.emplace_back(); // empty, but valid
376
377 BackgroundFetchOptions options;
378
379 // Verify that there are no active tags yet.
380 {
381 blink::mojom::BackgroundFetchError error;
382 std::vector<std::string> tags;
383
384 ASSERT_NO_FATAL_FAILURE(GetTags(registration_id, &error, &tags));
385 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
386
387 ASSERT_EQ(tags.size(), 0u);
388 }
389
390 // Start the Background Fetch for the |registration_id|.
391 {
392 blink::mojom::BackgroundFetchError error;
393 BackgroundFetchRegistration registration;
394
395 ASSERT_NO_FATAL_FAILURE(
396 Fetch(registration_id, requests, options, &error, &registration));
397 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
398 }
399
400 // Verify that there is a single active fetch (the one we just started).
401 {
402 blink::mojom::BackgroundFetchError error;
403 std::vector<std::string> tags;
404
405 ASSERT_NO_FATAL_FAILURE(GetTags(registration_id, &error, &tags));
406 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
407
408 ASSERT_EQ(tags.size(), 1u);
409 EXPECT_EQ(tags[0], kExampleTag);
410 }
411
412 // Start the Background Fetch for the |second_registration_id|.
413 {
414 blink::mojom::BackgroundFetchError error;
415 BackgroundFetchRegistration registration;
416
417 ASSERT_NO_FATAL_FAILURE(Fetch(second_registration_id, requests, options,
418 &error, &registration));
419 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
420 }
421
422 // Verify that there are two active fetches.
423 {
424 blink::mojom::BackgroundFetchError error;
425 std::vector<std::string> tags;
426
427 ASSERT_NO_FATAL_FAILURE(GetTags(registration_id, &error, &tags));
428 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE);
429
430 ASSERT_EQ(tags.size(), 2u);
431
432 // We make no guarantees about ordering of the tags.
433 const bool has_example_tag =
434 tags[0] == kExampleTag || tags[1] == kExampleTag;
435 const bool has_alternative_tag =
436 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag;
437
438 EXPECT_TRUE(has_example_tag);
439 EXPECT_TRUE(has_alternative_tag);
440 }
441 }
442
443 } // namespace
444 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/background_fetch/background_fetch_service_impl.h ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698