testing/web-platform/mozilla/tests/service-workers/service-worker/multiple-register.https.html
author James Graham <james@hoppipolla.co.uk>
Thu, 25 Jun 2015 16:10:50 -0700
changeset 250330 a03152790896f9304cf6b79e48de91cc0388cc64
child 260749 89d49d9d01be6f6be979aae8f89ddc932a2a77e4
permissions -rw-r--r--
Bug 1120505 - Add Blink service-worker tests to local wpt tests, rs=Ms2ger

<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>
var worker_url = 'resources/empty-worker.js';

async_test(function(t) {
  var scope = 'resources/scope/subsequent-register-from-same-window';
  var registration;

  service_worker_unregister_and_register(t, worker_url, scope)
    .then(function(r) {
        registration = r;
        return wait_for_state(t, r.installing, 'activated');
      })
    .then(function() {
        return navigator.serviceWorker.register(worker_url, { scope: scope });
      })
    .then(function(new_registration) {
        assert_equals(new_registration, registration,
                      'register should resolve to the same registration');
        assert_equals(new_registration.active, registration.active,
                      'register should resolve to the same worker');
        assert_equals(new_registration.active.state, 'activated',
                      'the worker should be in state "activated"');
        return registration.unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Subsequent registrations resolve to the same registration object');

async_test(function(t) {
  var scope = 'resources/scope/subsequent-register-from-different-iframe';
  var frame;
  var registration;

  service_worker_unregister_and_register(t, worker_url, scope)
    .then(function(r) {
        registration = r;
        return wait_for_state(t, r.installing, 'activated');
      })
    .then(function() { return with_iframe('out-of-scope'); })
    .then(function(f) {
        frame = f;
        return frame.contentWindow.navigator.serviceWorker.register(
            worker_url, { scope: scope });
      })
    .then(function(new_registration) {
        assert_not_equals(
          registration, new_registration,
          'register should resolve to the different registration');
        assert_equals(
          registration.scope, new_registration.scope,
          'registrations should have the same scope');

        assert_equals(
          registration.installing, null,
          'installing worker should be null');
        assert_equals(
          new_registration.installing, null,
          'installing worker should be null');
        assert_equals(
          registration.waiting, null,
          'waiting worker should be null')
        assert_equals(
          new_registration.waiting, null,
          'waiting worker should be null')

        assert_not_equals(
          registration.active, new_registration.active,
          'registration should have the different active worker');
        assert_equals(
          registration.active.scriptURL,
          new_registration.active.scriptURL,
          'active workers should have the same script URL');
        assert_equals(
          registration.active.state,
          new_registration.active.state,
          'active workers should be in the same state');

        frame.remove();
        return registration.unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Subsequent registrations from a different iframe resolve to the ' +
       'different registration object but they refer to the same ' +
       'registration and workers');

async_test(function(t) {
  var scope = 'resources/scope/concurrent-register';

  service_worker_unregister(t, scope)
    .then(function() {
        var promises = [];
        for (var i = 0; i < 10; ++i) {
          promises.push(navigator.serviceWorker.register(worker_url,
                                                         { scope: scope }));
        }
        return Promise.all(promises);
      })
    .then(function(registrations) {
        registrations.forEach(function(registration) {
            assert_equals(registration, registrations[0],
                          'register should resolve to the same registration');
          });
        return registrations[0].unregister();
      })
    .then(function() { t.done(); })
    .catch(unreached_rejection(t));
}, 'Concurrent registrations resolve to the same registration object');

</script>