tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

synced-state.https.html (3765B)


      1 <!doctype html>
      2 <title>ServiceWorker: worker objects have synced state</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="resources/test-helpers.sub.js"></script>
      6 <script>
      7 // Tests that ServiceWorker objects representing the same Service Worker
      8 // entity have the same state. JS-level equality is now required according to
      9 // the spec.
     10 'use strict';
     11 
     12 function nextChange(worker) {
     13  return new Promise(function(resolve, reject) {
     14      worker.addEventListener('statechange', function handler(event) {
     15          try {
     16            worker.removeEventListener('statechange', handler);
     17            resolve(event.currentTarget.state);
     18          } catch (err) {
     19            reject(err);
     20          }
     21        });
     22    });
     23 }
     24 
     25 promise_test(function(t) {
     26    var scope = 'resources/synced-state';
     27    var script = 'resources/empty-worker.js';
     28    var registration, worker;
     29 
     30    return service_worker_unregister_and_register(t, script, scope)
     31      .then(function(r) {
     32          registration = r;
     33          worker = registration.installing;
     34 
     35          t.add_cleanup(function() {
     36              return r.unregister();
     37            });
     38 
     39          return nextChange(worker);
     40        })
     41      .then(function(state) {
     42          assert_equals(state, 'installed',
     43                        'original SW should be installed');
     44          assert_equals(registration.installing, null,
     45                        'in installed, .installing should be null');
     46          assert_equals(registration.waiting, worker,
     47                        'in installed, .waiting should be equal to the ' +
     48                          'original worker');
     49          assert_equals(registration.waiting.state, 'installed',
     50                        'in installed, .waiting should be installed');
     51          assert_equals(registration.active, null,
     52                        'in installed, .active should be null');
     53 
     54          return nextChange(worker);
     55        })
     56      .then(function(state) {
     57          assert_equals(state, 'activating',
     58                        'original SW should be activating');
     59          assert_equals(registration.installing, null,
     60                        'in activating, .installing should be null');
     61          assert_equals(registration.waiting, null,
     62                        'in activating, .waiting should be null');
     63          assert_equals(registration.active, worker,
     64                        'in activating, .active should be equal to the ' +
     65                          'original worker');
     66          assert_equals(
     67              registration.active.state, 'activating',
     68              'in activating, .active should be activating');
     69 
     70          return nextChange(worker);
     71        })
     72      .then(function(state) {
     73          assert_equals(state, 'activated',
     74                        'original SW should be activated');
     75          assert_equals(registration.installing, null,
     76                        'in activated, .installing should be null');
     77          assert_equals(registration.waiting, null,
     78                        'in activated, .waiting should be null');
     79          assert_equals(registration.active, worker,
     80                        'in activated, .active should be equal to the ' +
     81                          'original worker');
     82          assert_equals(registration.active.state, 'activated',
     83                        'in activated .active should be activated');
     84        })
     85      .then(function() {
     86          return navigator.serviceWorker.getRegistration(scope);
     87        })
     88      .then(function(r) {
     89          assert_equals(r, registration, 'getRegistration should return the ' +
     90                                         'same object');
     91        });
     92  }, 'worker objects for the same entity have the same state');
     93 </script>