Bug 1498415 [wpt PR 13473] - [Background Fetch] Add WPT tests for BackgroundFetchRegistration::abort, a=testonly
authorRayan Kanso <rayankans@chromium.org>
Mon, 15 Oct 2018 17:16:21 +0000
changeset 489976 9445f12f346989d2cbe39946305df72667c72e3a
parent 489975 483aee01b7d1ba1b68e3f04e8700a7950ab6caec
child 489977 e00a419d38d9f0f627059ff93b75239aee35acb1
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewerstestonly
bugs1498415, 13473, 1261477, 1276557, 598848
milestone64.0a1
Bug 1498415 [wpt PR 13473] - [Background Fetch] Add WPT tests for BackgroundFetchRegistration::abort, a=testonly Automatic update from web-platform-tests[Background Fetch] Add WPT tests for BackgroundFetchRegistration::abort Re-resubmit cr/1261477 The issue was that there is a race condition between the abort going through, and the downloaded file being persisted on disk. I added an extra check in the WPT test. Change-Id: I98aa005b4637c08f60a126a6c7c896c1c0edfddf Reviewed-on: https://chromium-review.googlesource.com/c/1276557 Commit-Queue: Rayan Kanso <rayankans@chromium.org> Reviewed-by: Mugdha Lakhani <nator@chromium.org> Reviewed-by: Peter Beverloo <peter@chromium.org> Cr-Commit-Position: refs/heads/master@{#598848} -- wpt-commits: c54a084daf22377e9c9ac3b50f9220ad0df27858 wpt-pr: 13473
testing/web-platform/tests/background-fetch/abort.https.window.js
testing/web-platform/tests/background-fetch/service_workers/sw.js
new file mode 100644
--- /dev/null
+++ b/testing/web-platform/tests/background-fetch/abort.https.window.js
@@ -0,0 +1,59 @@
+// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
+// META: script=resources/utils.js
+'use strict';
+
+// Covers basic functionality provided by BackgroundFetchManager.abort().
+// https://wicg.github.io/background-fetch/#background-fetch-registration-abort
+
+backgroundFetchTest(async (test, backgroundFetch) => {
+  const registration = await backgroundFetch.fetch(
+      uniqueId(),
+      ['resources/feature-name.txt', '/serviceworker/resources/slow-response.php']);
+
+  assert_true(await registration.abort());
+  assert_false(await registration.abort());
+
+}, 'Aborting the same registration twice fails');
+
+backgroundFetchTest(async (test, backgroundFetch) => {
+  const registration = await backgroundFetch.fetch(
+      uniqueId(),
+      ['resources/feature-name.txt', '/serviceworker/resources/slow-response.php']);
+
+  await new Promise(resolve => {
+    let aborted = false;
+    const expectedResultText = 'Background Fetch';
+
+    registration.onprogress = async event => {
+      if (event.target.downloaded < expectedResultText.length)
+        return;
+
+      if (aborted)
+        return;
+
+      // Abort after the first file has been downloaded and check the results.
+
+      aborted = true;
+      assert_true(await registration.abort());
+
+      const {type, eventRegistration, results} = await getMessageFromServiceWorker();
+
+      assert_equals(eventRegistration.result, 'failure');
+      assert_equals(eventRegistration.failureReason, 'aborted');
+      assert_equals(registration.result, 'failure');
+      assert_equals(registration.failureReason, 'aborted');
+
+      assert_equals(type, 'backgroundfetchabort');
+
+      // The abort might have gone through before the first result was persisted.
+      if (results.length === 1) {
+        assert_true(results[0].url.includes('resources/feature-name.txt'));
+        assert_equals(results[0].status, 200);
+        assert_equals(results[0].text, expectedResultText);
+      }
+
+      resolve();
+    };
+  });
+
+}, 'Calling BackgroundFetchRegistration.abort sets the correct fields and responses are still available');
\ No newline at end of file
--- a/testing/web-platform/tests/background-fetch/service_workers/sw.js
+++ b/testing/web-platform/tests/background-fetch/service_workers/sw.js
@@ -22,8 +22,9 @@ function handleBackgroundFetchUpdateEven
         const registrationCopy = cloneRegistration(event.registration);
         sendMessageToDocument(
           { type: event.type, eventRegistration: registrationCopy, results })
       }));
 }
 
 self.addEventListener('backgroundfetchsuccess', handleBackgroundFetchUpdateEvent);
 self.addEventListener('backgroundfetchfail', handleBackgroundFetchUpdateEvent);
+self.addEventListener('backgroundfetchabort', handleBackgroundFetchUpdateEvent);