tor-browser

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

navigate-window.https.html (5705B)


      1 <!DOCTYPE html>
      2 <title>Service Worker: Navigate a Window</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="/common/get-host-info.sub.js"></script>
      6 <script src="resources/test-helpers.sub.js"></script>
      7 <body>
      8 <script>
      9 var host_info = get_host_info();
     10 var BASE_URL = host_info['HTTPS_ORIGIN'] + base_path();
     11 
     12 function wait_for_message(msg) {
     13  return new Promise(function(resolve, reject) {
     14    window.addEventListener('message', function onMsg(evt) {
     15      if (evt.data.type === msg) {
     16        resolve();
     17      }
     18    });
     19  });
     20 }
     21 
     22 function with_window(url) {
     23  var win = window.open(url);
     24  return wait_for_message('LOADED').then(_ => win);
     25 }
     26 
     27 function navigate_window(win, url) {
     28  win.location = url;
     29  return wait_for_message('LOADED').then(_ => win);
     30 }
     31 
     32 function reload_window(win) {
     33  win.location.reload();
     34  return wait_for_message('LOADED').then(_ => win);
     35 }
     36 
     37 function go_back(win) {
     38  win.history.back();
     39  return wait_for_message('PAGESHOW').then(_ => win);
     40 }
     41 
     42 function go_forward(win) {
     43  win.history.forward();
     44  return wait_for_message('PAGESHOW').then(_ => win);
     45 }
     46 
     47 function get_clients(win, sw, opts) {
     48  return new Promise((resolve, reject) => {
     49    win.navigator.serviceWorker.addEventListener('message', function onMsg(evt) {
     50      win.navigator.serviceWorker.removeEventListener('message', onMsg);
     51      if (evt.data.type === 'success') {
     52        resolve(evt.data.detail);
     53      } else {
     54        reject(evt.data.detail);
     55      }
     56    });
     57    sw.postMessage({ type: 'GET_CLIENTS', opts: (opts || {}) });
     58  });
     59 }
     60 
     61 function compare_urls(a, b) {
     62  return a.url < b.url ? -1 : b.url < a.url ? 1 : 0;
     63 }
     64 
     65 function validate_window(win, url, opts) {
     66  return win.navigator.serviceWorker.getRegistration(url)
     67    .then(reg => {
     68        // In order to compare service worker instances we need to
     69        // make sure the DOM object is owned by the same global; the
     70        // opened window in this case.
     71        assert_equals(win.navigator.serviceWorker.controller, reg.active,
     72                      'window should be controlled by service worker');
     73        return get_clients(win, reg.active, opts);
     74      })
     75    .then(resultList => {
     76        // We should always see our controlled window.
     77        var expected = [
     78          { url: url, frameType: 'auxiliary' }
     79        ];
     80        // If we are including uncontrolled windows, then we might see the
     81        // test window itself and the test harness.
     82        if (opts.includeUncontrolled) {
     83          expected.push({ url: BASE_URL + 'navigate-window.https.html',
     84                          frameType: 'auxiliary' });
     85          expected.push({
     86            url: host_info['HTTPS_ORIGIN'] + '/testharness_runner.html',
     87            frameType: 'top-level' });
     88        }
     89 
     90        assert_equals(resultList.length, expected.length,
     91                      'expected number of clients');
     92 
     93        expected.sort(compare_urls);
     94        resultList.sort(compare_urls);
     95 
     96        for (var i = 0; i < resultList.length; ++i) {
     97          assert_equals(resultList[i].url, expected[i].url,
     98                        'client should have expected url');
     99          assert_equals(resultList[i].frameType, expected[i].frameType,
    100                        'client should have expected frame type');
    101        }
    102        return win;
    103      })
    104 }
    105 
    106 promise_test(function(t) {
    107    var worker = BASE_URL + 'resources/navigate-window-worker.js';
    108    var scope = BASE_URL + 'resources/loaded.html?navigate-window-controlled';
    109    var url1 = scope + '&q=1';
    110    var url2 = scope + '&q=2';
    111    return service_worker_unregister_and_register(t, worker, scope)
    112      .then(reg => wait_for_state(t, reg.installing, 'activated') )
    113      .then(___ => with_window(url1))
    114      .then(win => validate_window(win, url1, { includeUncontrolled: false }))
    115      .then(win => navigate_window(win, url2))
    116      .then(win => validate_window(win, url2, { includeUncontrolled: false }))
    117      .then(win => go_back(win))
    118      .then(win => validate_window(win, url1, { includeUncontrolled: false }))
    119      .then(win => go_forward(win))
    120      .then(win => validate_window(win, url2, { includeUncontrolled: false }))
    121      .then(win => reload_window(win))
    122      .then(win => validate_window(win, url2, { includeUncontrolled: false }))
    123      .then(win => win.close())
    124      .catch(unreached_rejection(t))
    125      .then(___ => service_worker_unregister(t, scope))
    126  }, 'Clients.matchAll() should not show an old window as controlled after ' +
    127     'it navigates.');
    128 
    129 promise_test(function(t) {
    130    var worker = BASE_URL + 'resources/navigate-window-worker.js';
    131    var scope = BASE_URL + 'resources/loaded.html?navigate-window-uncontrolled';
    132    var url1 = scope + '&q=1';
    133    var url2 = scope + '&q=2';
    134    return service_worker_unregister_and_register(t, worker, scope)
    135      .then(reg => wait_for_state(t, reg.installing, 'activated') )
    136      .then(___ => with_window(url1))
    137      .then(win => validate_window(win, url1, { includeUncontrolled: true }))
    138      .then(win => navigate_window(win, url2))
    139      .then(win => validate_window(win, url2, { includeUncontrolled: true }))
    140      .then(win => go_back(win))
    141      .then(win => validate_window(win, url1, { includeUncontrolled: true }))
    142      .then(win => go_forward(win))
    143      .then(win => validate_window(win, url2, { includeUncontrolled: true }))
    144      .then(win => reload_window(win))
    145      .then(win => validate_window(win, url2, { includeUncontrolled: true }))
    146      .then(win => win.close())
    147      .catch(unreached_rejection(t))
    148      .then(___ => service_worker_unregister(t, scope))
    149  }, 'Clients.matchAll() should not show an old window after it navigates.');
    150 </script>
    151 </body>