| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 <map> | 5 #include <map> |
| 6 #include <memory> | 6 #include <memory> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); | 470 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| 471 | 471 |
| 472 // Deliberate do *not* create a fetch for the |registration_id|. | 472 // Deliberate do *not* create a fetch for the |registration_id|. |
| 473 | 473 |
| 474 blink::mojom::BackgroundFetchError error; | 474 blink::mojom::BackgroundFetchError error; |
| 475 | 475 |
| 476 ASSERT_NO_FATAL_FAILURE(Abort(registration_id, &error)); | 476 ASSERT_NO_FATAL_FAILURE(Abort(registration_id, &error)); |
| 477 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_TAG); | 477 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 478 } | 478 } |
| 479 | 479 |
| 480 TEST_F(BackgroundFetchServiceTest, AbortEventDispatch) { |
| 481 // Tests that the `backgroundfetchabort` event will be fired when a Background |
| 482 // Fetch registration has been aborted by either the user or developer. |
| 483 |
| 484 BackgroundFetchRegistrationId registration_id; |
| 485 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| 486 |
| 487 // base::RunLoop that we'll run until the event has been dispatched. If this |
| 488 // test times out, it means that the event could not be dispatched. |
| 489 base::RunLoop event_dispatched_loop; |
| 490 embedded_worker_test_helper()->set_abort_event_closure( |
| 491 event_dispatched_loop.QuitClosure()); |
| 492 |
| 493 constexpr int kResponseCode = 200; |
| 494 |
| 495 std::vector<ServiceWorkerFetchRequest> requests; |
| 496 requests.push_back(CreateRequestWithProvidedResponse( |
| 497 "GET", "https://example.com/funny_cat.txt", |
| 498 TestResponseBuilder(kResponseCode) |
| 499 .SetResponseData("Random data about a funny cat.") |
| 500 .Build())); |
| 501 |
| 502 // Create the registration with the given |requests|. |
| 503 { |
| 504 BackgroundFetchOptions options; |
| 505 |
| 506 blink::mojom::BackgroundFetchError error; |
| 507 BackgroundFetchRegistration registration; |
| 508 |
| 509 // Create the first registration. This must succeed. |
| 510 ASSERT_NO_FATAL_FAILURE( |
| 511 Fetch(registration_id, requests, options, &error, ®istration)); |
| 512 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); |
| 513 } |
| 514 |
| 515 // Immediately abort the request created for the |registration_id|. Then wait |
| 516 // for the `backgroundfetchabort` event to have been invoked. |
| 517 { |
| 518 blink::mojom::BackgroundFetchError error; |
| 519 |
| 520 ASSERT_NO_FATAL_FAILURE(Abort(registration_id, &error)); |
| 521 ASSERT_EQ(error, blink::mojom::BackgroundFetchError::NONE); |
| 522 } |
| 523 |
| 524 event_dispatched_loop.Run(); |
| 525 |
| 526 ASSERT_TRUE(embedded_worker_test_helper()->last_tag().has_value()); |
| 527 EXPECT_EQ(kExampleTag, embedded_worker_test_helper()->last_tag().value()); |
| 528 } |
| 529 |
| 480 TEST_F(BackgroundFetchServiceTest, GetTags) { | 530 TEST_F(BackgroundFetchServiceTest, GetTags) { |
| 481 // This test verifies that the list of active tags can be retrieved from the | 531 // This test verifies that the list of active tags can be retrieved from the |
| 482 // service for a given Service Worker, as extracted from a registration. | 532 // service for a given Service Worker, as extracted from a registration. |
| 483 | 533 |
| 484 BackgroundFetchRegistrationId registration_id; | 534 BackgroundFetchRegistrationId registration_id; |
| 485 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); | 535 ASSERT_TRUE(CreateRegistrationId(kExampleTag, ®istration_id)); |
| 486 | 536 |
| 487 BackgroundFetchRegistrationId second_registration_id; | 537 BackgroundFetchRegistrationId second_registration_id; |
| 488 ASSERT_TRUE(CreateRegistrationId(kAlternativeTag, &second_registration_id)); | 538 ASSERT_TRUE(CreateRegistrationId(kAlternativeTag, &second_registration_id)); |
| 489 | 539 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 const bool has_alternative_tag = | 601 const bool has_alternative_tag = |
| 552 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; | 602 tags[0] == kAlternativeTag || tags[1] == kAlternativeTag; |
| 553 | 603 |
| 554 EXPECT_TRUE(has_example_tag); | 604 EXPECT_TRUE(has_example_tag); |
| 555 EXPECT_TRUE(has_alternative_tag); | 605 EXPECT_TRUE(has_alternative_tag); |
| 556 } | 606 } |
| 557 } | 607 } |
| 558 | 608 |
| 559 } // namespace | 609 } // namespace |
| 560 } // namespace content | 610 } // namespace content |
| OLD | NEW |