tor-browser

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

get-state.https.html (8460B)


      1 <!DOCTYPE html>
      2 <meta charset="utf-8">
      3 <title>NavigationPreloadManager.getState</title>
      4 <script src="/resources/testharness.js"></script>
      5 <script src="/resources/testharnessreport.js"></script>
      6 <script src="../resources/test-helpers.sub.js"></script>
      7 <script src="resources/helpers.js"></script>
      8 <body>
      9 <script>
     10 function post_and_wait_for_reply(worker, message) {
     11  return new Promise(resolve => {
     12      navigator.serviceWorker.onmessage = e => { resolve(e.data); };
     13      worker.postMessage(message);
     14    });
     15 }
     16 
     17 promise_test(t => {
     18  const scope = '../resources/get-state';
     19  const script = '../resources/empty-worker.js';
     20  var np;
     21 
     22  return service_worker_unregister_and_register(t, script, scope)
     23    .then(r => {
     24        np = r.navigationPreload;
     25        add_completion_callback(() => r.unregister());
     26        return wait_for_state(t, r.installing, 'activated');
     27      })
     28    .then(() => np.getState())
     29    .then(state => {
     30        expect_navigation_preload_state(state, false, 'true', 'default state');
     31        return np.enable();
     32      })
     33    .then(result => {
     34        assert_equals(result, undefined,
     35                      'enable() should resolve to undefined');
     36        return np.getState();
     37      })
     38    .then(state => {
     39        expect_navigation_preload_state(state, true, 'true',
     40                                        'state after enable()');
     41        return np.disable();
     42      })
     43    .then(result => {
     44        assert_equals(result, undefined,
     45                      'disable() should resolve to undefined');
     46        return np.getState();
     47      })
     48    .then(state => {
     49        expect_navigation_preload_state(state, false, 'true',
     50                                        'state after disable()');
     51        return np.setHeaderValue('dreams that cannot be');
     52      })
     53    .then(result => {
     54        assert_equals(result, undefined,
     55                      'setHeaderValue() should resolve to undefined');
     56        return np.getState();
     57      })
     58    .then(state => {
     59        expect_navigation_preload_state(state, false, 'dreams that cannot be',
     60                                        'state after setHeaderValue()');
     61        return np.setHeaderValue('').then(() => np.getState());
     62      })
     63    .then(state => {
     64        expect_navigation_preload_state(state, false, '',
     65                                        'after setHeaderValue to empty string');
     66        return np.setHeaderValue(null).then(() => np.getState());
     67      })
     68    .then(state => {
     69        expect_navigation_preload_state(state, false, 'null',
     70                                        'after setHeaderValue to null');
     71        return promise_rejects_js(t,
     72            TypeError,
     73            np.setHeaderValue('what\uDC00\uD800this'),
     74            'setHeaderValue() should throw if passed surrogates');
     75      })
     76    .then(() => {
     77        return promise_rejects_js(t,
     78            TypeError,
     79            np.setHeaderValue('zer\0o'),
     80            'setHeaderValue() should throw if passed \\0');
     81      })
     82    .then(() => {
     83        return promise_rejects_js(t,
     84            TypeError,
     85            np.setHeaderValue('\rcarriage'),
     86            'setHeaderValue() should throw if passed \\r');
     87      })
     88    .then(() => {
     89        return promise_rejects_js(t,
     90            TypeError,
     91            np.setHeaderValue('newline\n'),
     92            'setHeaderValue() should throw if passed \\n');
     93      })
     94    .then(() => {
     95        return promise_rejects_js(t,
     96            TypeError,
     97            np.setHeaderValue(),
     98            'setHeaderValue() should throw if passed undefined');
     99      })
    100    .then(() => np.enable().then(() => np.getState()))
    101    .then(state => {
    102        expect_navigation_preload_state(state, true, 'null',
    103                                        'enable() should not change header');
    104      });
    105  }, 'getState');
    106 
    107 // This test sends commands to a worker to call enable()/disable()/getState().
    108 // It checks the results from the worker and verifies that they match the
    109 // navigation preload state accessible from the page.
    110 promise_test(t => {
    111  const scope = 'resources/get-state-worker';
    112  const script = 'resources/get-state-worker.js';
    113  var worker;
    114  var registration;
    115 
    116  return service_worker_unregister_and_register(t, script, scope)
    117    .then(r => {
    118        registration = r;
    119        add_completion_callback(() => registration.unregister());
    120        worker = registration.installing;
    121        return wait_for_state(t, worker, 'activated');
    122      })
    123    .then(() => {
    124        // Call getState().
    125        return post_and_wait_for_reply(worker, 'getState');
    126      })
    127    .then(data => {
    128        return Promise.all([data, registration.navigationPreload.getState()]);
    129      })
    130    .then(states => {
    131        expect_navigation_preload_state(states[0], false, 'true',
    132                                        'default state (from worker)');
    133        expect_navigation_preload_state(states[1], false, 'true',
    134                                        'default state (from page)');
    135        // Call enable() and then getState().
    136        return post_and_wait_for_reply(worker, 'enable');
    137      })
    138    .then(data => {
    139        assert_equals(data, undefined, 'enable() should resolve to undefined');
    140        return Promise.all([
    141            post_and_wait_for_reply(worker, 'getState'),
    142            registration.navigationPreload.getState()
    143          ]);
    144      })
    145    .then(states => {
    146        expect_navigation_preload_state(states[0], true, 'true',
    147                                        'state after enable() (from worker)');
    148        expect_navigation_preload_state(states[1], true, 'true',
    149                                        'state after enable() (from page)');
    150        // Call disable() and then getState().
    151        return post_and_wait_for_reply(worker, 'disable');
    152      })
    153    .then(data => {
    154        assert_equals(data, undefined,
    155                      '.disable() should resolve to undefined');
    156        return Promise.all([
    157            post_and_wait_for_reply(worker, 'getState'),
    158            registration.navigationPreload.getState()
    159          ]);
    160      })
    161    .then(states => {
    162        expect_navigation_preload_state(states[0], false, 'true',
    163                                        'state after disable() (from worker)');
    164        expect_navigation_preload_state(states[1], false, 'true',
    165                                        'state after disable() (from page)');
    166        return post_and_wait_for_reply(worker, 'setHeaderValue');
    167      })
    168    .then(data => {
    169        assert_equals(data, undefined,
    170                      '.setHeaderValue() should resolve to undefined');
    171        return Promise.all([
    172            post_and_wait_for_reply(worker, 'getState'),
    173            registration.navigationPreload.getState()]);
    174      })
    175    .then(states => {
    176        expect_navigation_preload_state(
    177            states[0], false, 'insightful',
    178            'state after setHeaderValue() (from worker)');
    179        expect_navigation_preload_state(
    180            states[1], false, 'insightful',
    181            'state after setHeaderValue() (from page)');
    182      });
    183  }, 'getState from a worker');
    184 
    185 // This tests navigation preload API when there is no active worker. It calls
    186 // the API from the main page and then from the worker itself.
    187 promise_test(t => {
    188  const scope = 'resources/wait-for-activate-worker';
    189  const script = 'resources/wait-for-activate-worker.js';
    190  var registration;
    191  var np;
    192  return service_worker_unregister_and_register(t, script, scope)
    193    .then(r => {
    194        registration = r;
    195        np = registration.navigationPreload;
    196        add_completion_callback(() => registration.unregister());
    197        return Promise.all([
    198            promise_rejects_dom(
    199                t, 'InvalidStateError', np.enable(),
    200                'enable should reject if there is no active worker'),
    201            promise_rejects_dom(
    202                t, 'InvalidStateError', np.disable(),
    203                'disable should reject if there is no active worker'),
    204            promise_rejects_dom(
    205                t, 'InvalidStateError', np.setHeaderValue('umm'),
    206                'setHeaderValue should reject if there is no active worker')]);
    207      })
    208    .then(() => np.getState())
    209    .then(state => {
    210        expect_navigation_preload_state(state, false, 'true',
    211                                        'state before activation');
    212        return post_and_wait_for_reply(registration.installing, 'ping');
    213      })
    214    .then(result => assert_equals(result, 'PASS'));
    215  }, 'no active worker');
    216 </script>
    217 </body>