chromium/third_party/blink/web_tests/http/tests/push_messaging/get-subscription-in-document.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>
</head>
<body>
<script>
async_test(function(test) {
    var workerUrl = 'resources/empty_worker.js';
    var workerScope = 'resources/scope/' + location.pathname;
    var swRegistration = null;
    service_worker_unregister_and_register(test, workerUrl, workerScope)
        .then(function(serviceWorkerRegistration) {
            swRegistration = serviceWorkerRegistration;
            return wait_for_state(test, swRegistration.installing, 'activated');
        })
        .then(function() {
            // If running manually, grant permission when prompted.
            if (self.testRunner)
                testRunner.setPermission('push-messaging', 'granted', location.origin, location.origin);

            assert_inherits(swRegistration.pushManager, 'getSubscription',
                            'getSubscription() should be exposed on the PushManager object');
            assert_equals(typeof(swRegistration.pushManager.getSubscription), 'function',
                          'PushManager.getSubscription() is a function.');

            return swRegistration.pushManager.getSubscription();
        })
        .then(function(pushSubscription) {
            assert_equals(pushSubscription, null,
                          "pushSubscription should be null if there is no active push registration.")
            return swRegistration.pushManager.subscribe({ userVisibleOnly: true });
        })
        .then(function(pushSubscription) {
            previousPushSubscription = pushSubscription;
            return swRegistration.pushManager.getSubscription();
        })
        .then(function(pushSubscription) {
            assert_equals(previousPushSubscription.endpoint, pushSubscription.endpoint,
                          "Both subscription objects should have the same endpoint.");
            return pushSubscription.unsubscribe();
        })
        .then(function(unsubscribed) {
            assert_true(unsubscribed, "unsubscription was successful");
            return swRegistration.pushManager.getSubscription();
        })
        .then(function(pushSubscription) {
            assert_equals(pushSubscription, null,
                          "pushSubscription should be null after unsubscribing.")
            return swRegistration.pushManager.subscribe({ userVisibleOnly: true });
        })
        .then(function(pushSubscription) {
            assert_not_equals(pushSubscription, null,
                              "Subscription should have succeeded.");
            return service_worker_unregister(test, workerScope);
        })
        .then(function() {
            return swRegistration.pushManager.getSubscription();
        })
        .then(function(pushSubscription) {
            assert_equals(pushSubscription, null,
                          "After unregistration of SW, there should be no push subscription.");
            return service_worker_unregister_and_done(test, workerScope);
        })
        .catch(unreached_rejection(test));
}, 'getSubscription must return the details of the active subscription if there is one');
</script>
</body>
</html>