tor-browser

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

callback-suspended.html (2597B)


      1 <!doctype html>
      2 <meta charset=utf-8>
      3 <title>Dispatching idle callbacks should be able to be suspended and then resumed</title>
      4 <meta name="timeout" content="long">
      5 <script src=/resources/testharness.js></script>
      6 <script src=/resources/testharnessreport.js></script>
      7 <div id="log"></div>
      8 <script>
      9  function withEventListener(target, event, handler) {
     10    handler = handler || (e => e);
     11    return new Promise(resolve => {
     12      let wrapper = function(e) {
     13        let result = handler(e);
     14        if (!result) {
     15          return;
     16        }
     17 
     18        resolve(result);
     19      }
     20      target.addEventListener(event, wrapper, { once: true });
     21    });
     22  }
     23 
     24  function makePostBackUrl(name) {
     25    return new URL('resources/post_name_on_load.html?name=' + name,
     26                   window.location).href;
     27  }
     28 
     29  function waitForMessage(message, handler) {
     30    return withEventListener(window, 'message', e => (e.data === message) && handler(e));;
     31  }
     32 
     33  function withWindow(name) {
     34    let win = window.open(makePostBackUrl(name))
     35    return waitForMessage(name, _ => win);
     36  }
     37 
     38  function navigateWindow(win, name) {
     39    win.location = makePostBackUrl(name);
     40    return waitForMessage(name, _ => win);
     41  }
     42 
     43  function waitDuration(delay) {
     44    return new Promise(resolve => {
     45      step_timeout(resolve, delay);
     46    })
     47  }
     48 
     49  function goBack(win) {
     50    var p = withEventListener(win, 'pagehide');
     51    win.history.back();
     52    return p;
     53  }
     54 
     55  promise_test(t => {
     56    let idleCalled = false;
     57    let running = true;
     58    return withWindow('foo')
     59      .then(win => {
     60        let callback = function(d) {
     61          idleCalled = true;
     62          if (running) {
     63            win.requestIdleCallback(callback);
     64          }
     65        };
     66 
     67        win.requestIdleCallback(callback);
     68 
     69        return navigateWindow(win, 'bar')
     70          .then(_ => idleCalled = false)
     71          .then(_ => waitDuration(2000))
     72          .then(_ => {
     73            assert_false(idleCalled, "idle callback shouldn't have been called yet");
     74            return goBack(win);
     75          })
     76          .then(_ => Promise.race([
     77            // At this point it's a matter of having bfcache ...
     78            waitDuration(2000)
     79              .then(_ => {
     80                assert_true(idleCalled, "idle callback should've been called by now");
     81                running = false;
     82              }),
     83            // ... or not. If not, we expect a load event.
     84            waitForMessage("foo", _ => win)
     85          ]))
     86          .then(_ => win.close())
     87          .catch(e => {
     88            win.close();
     89            throw e;
     90          })
     91      });
     92  });
     93 
     94 </script>