chromium/third_party/blink/web_tests/http/tests/push_messaging/get-subscription-in-service-worker.html

<!DOCTYPE html>
<html>
<head>
<title>getSubscription must return the details of the active subscription if there is one</title>
<link rel="manifest" href="resources/push_manifest.json">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="../serviceworker/resources/test-helpers.js"></script>
<script src="../notifications/resources/test-helpers.js"></script>
<script src="resources/test-helpers.js"></script>
</head>
<body>
<script>
// This test uses the test runner. If running manually, grant permission when prompted.
async_test(function(test) {
  if (window.testRunner)
    testRunner.setPermission('push-messaging', 'granted', location.origin, location.origin);

  var script = 'resources/instrumentation-service-worker.js';
  var scope = 'resources/scope/' + location.pathname;
  var port;
  var registration;
  var previousEndpoint;

  getActiveServiceWorkerWithMessagePort(test, script, scope)
      .then(function(workerInfo) {
        port = workerInfo.port;
        registration = workerInfo.registration;
        return subscribeAndUnsubscribePush(registration);
      })
      .then(function() {
        return runCommandInServiceWorker(port, 'getSubscription');
      })
      .then(function(data) {
        assert_equals(data.endpoint, null,
            'There must be no subscription after unsubscribing in the document.');
        return runCommandInServiceWorker(port, 'subscribe');
      })
      .then(function(data) {
        previousEndpoint = data.endpoint;
        return runCommandInServiceWorker(port, 'getSubscription');
      })
      .then(function(data) {
        assert_equals(data.endpoint, previousEndpoint,
            'Both PushSubscription instances should have the same endpoint.');
        return registration.pushManager.getSubscription();
      })
      .then(function(subscription) {
        assert_equals(subscription.endpoint, previousEndpoint,
            'The endpoints for the PushSubscription instances from document and worker must match.');
        return runCommandInServiceWorker(port, 'unsubscribe');
      })
      .then(function(data) {
        assert_true(data.unsubscribeResult,
            'Calling unsubscribe on an active subscription must resolve with true.');
        return runCommandInServiceWorker(port, 'getSubscription');
      })
      .then(function(data) {
        assert_equals(data.endpoint, null,
            'There must be no subscription after unsubscribing in the service worker.');
        return runCommandInServiceWorker(port, 'subscribe');
      })
      .then(function(data) {
        assert_equals(typeof data.endpoint, 'string', 'Subscribing again should succeed.');
        return registration.unregister();
      })
      .then(function(unregisterResult) {
        assert_true(unregisterResult,
            'Calling unregister on an active registration must resolve with true.');
        return registration.pushManager.getSubscription();
      })
      .then(function(subscription) {
        assert_equals(subscription, null,
            'Unregistering the service worker should clear the push subscription.');
        test.done();
      })
      .catch(unreached_rejection(test));
}, 'getSubscription must return the details of the active subscription if there is one');
</script>
</body>
</html>