tor-browser

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

extendable-event-async-waituntil.https.html (5602B)


      1 <!DOCTYPE html>
      2 <meta name="timeout" content="long">
      3 <script src="/resources/testharness.js"></script>
      4 <script src="resources/testharness-helpers.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="resources/test-helpers.sub.js"></script>
      7 <script>
      8 
      9 function sync_message(worker, message, transfer) {
     10  let wait = new Promise((res, rej) => {
     11    navigator.serviceWorker.addEventListener('message', function(e) {
     12        if (e.data === 'ACK') {
     13          res();
     14        } else {
     15          rej();
     16        }
     17      });
     18    });
     19  worker.postMessage(message, transfer);
     20  return wait;
     21 }
     22 
     23 function runTest(test, step, testBody) {
     24  var scope = './resources/' + step;
     25  var script = 'resources/extendable-event-async-waituntil.js?' + scope;
     26  return service_worker_unregister_and_register(test, script, scope)
     27    .then(function(registration) {
     28        test.add_cleanup(function() {
     29            return service_worker_unregister(test, scope);
     30          });
     31 
     32        let worker = registration.installing;
     33        var channel = new MessageChannel();
     34        var saw_message = new Promise(function(resolve) {
     35          channel.port1.onmessage = function(e) { resolve(e.data); }
     36        });
     37 
     38        return wait_for_state(test, worker, 'activated')
     39          .then(function() {
     40              return sync_message(worker, { step: 'init', port: channel.port2 },
     41                [channel.port2]);
     42            })
     43          .then(function() { return testBody(worker); })
     44          .then(function() { return saw_message; })
     45          .then(function(output) {
     46              assert_equals(output.result, output.expected);
     47            })
     48          .then(function() { return sync_message(worker, { step: 'done' }); });
     49      });
     50 }
     51 
     52 function msg_event_test(scope, test) {
     53  var testBody = function(worker) {
     54    return sync_message(worker, { step: scope });
     55  };
     56  return runTest(test, scope, testBody);
     57 }
     58 
     59 promise_test(msg_event_test.bind(this, 'no-current-extension-different-task'),
     60  'Test calling waitUntil in a task at the end of the event handler without an existing extension throws');
     61 
     62 promise_test(msg_event_test.bind(this, 'no-current-extension-different-microtask'),
     63  'Test calling waitUntil in a microtask at the end of the event handler without an existing extension suceeds');
     64 
     65 promise_test(msg_event_test.bind(this, 'current-extension-different-task'),
     66  'Test calling waitUntil in a different task an existing extension succeeds');
     67 
     68 promise_test(msg_event_test.bind(this, 'during-event-dispatch-current-extension-expired-same-microtask-turn'),
     69  'Test calling waitUntil at the end of an existing extension promise handler succeeds (event is still being dispatched)');
     70 
     71 promise_test(msg_event_test.bind(this, 'during-event-dispatch-current-extension-expired-same-microtask-turn-extra'),
     72  'Test calling waitUntil in a microtask at the end of an existing extension promise handler succeeds (event is still being dispatched)');
     73 
     74 promise_test(msg_event_test.bind(this, 'after-event-dispatch-current-extension-expired-same-microtask-turn'),
     75  'Test calling waitUntil in an existing extension promise handler succeeds (event is not being dispatched)');
     76 
     77 promise_test(msg_event_test.bind(this, 'after-event-dispatch-current-extension-expired-same-microtask-turn-extra'),
     78  'Test calling waitUntil in a microtask at the end of an existing extension promise handler throws (event is not being dispatched)');
     79 
     80 promise_test(msg_event_test.bind(this, 'current-extension-expired-different-task'),
     81  'Test calling waitUntil after the current extension expired in a different task fails');
     82 
     83 promise_test(msg_event_test.bind(this, 'script-extendable-event'),
     84  'Test calling waitUntil on a script constructed ExtendableEvent throws exception');
     85 
     86 promise_test(function(t) {
     87    var testBody = function(worker) {
     88      return with_iframe('./resources/pending-respondwith-async-waituntil');
     89    }
     90    return runTest(t, 'pending-respondwith-async-waituntil', testBody);
     91  }, 'Test calling waitUntil asynchronously with pending respondWith promise.');
     92 
     93 promise_test(function(t) {
     94    var testBody = function(worker) {
     95      return with_iframe('./resources/during-event-dispatch-respondwith-microtask-sync-waituntil');
     96    }
     97    return runTest(t, 'during-event-dispatch-respondwith-microtask-sync-waituntil', testBody);
     98  }, 'Test calling waitUntil synchronously inside microtask of respondWith promise (event is being dispatched).');
     99 
    100 promise_test(function(t) {
    101    var testBody = function(worker) {
    102      return with_iframe('./resources/during-event-dispatch-respondwith-microtask-async-waituntil');
    103    }
    104    return runTest(t, 'during-event-dispatch-respondwith-microtask-async-waituntil', testBody);
    105  }, 'Test calling waitUntil asynchronously inside microtask of respondWith promise (event is being dispatched).');
    106 
    107 promise_test(function(t) {
    108    var testBody = function(worker) {
    109      return with_iframe('./resources/after-event-dispatch-respondwith-microtask-sync-waituntil');
    110    }
    111    return runTest(t, 'after-event-dispatch-respondwith-microtask-sync-waituntil', testBody);
    112  }, 'Test calling waitUntil synchronously inside microtask of respondWith promise (event is not being dispatched).');
    113 
    114 promise_test(function(t) {
    115    var testBody = function(worker) {
    116      return with_iframe('./resources/after-event-dispatch-respondwith-microtask-async-waituntil');
    117    }
    118    return runTest(t, 'after-event-dispatch-respondwith-microtask-async-waituntil', testBody);
    119  }, 'Test calling waitUntil asynchronously inside microtask of respondWith promise (event is not being dispatched).');
    120 </script>