summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/requestidlecallback/callback-suspended.html
blob: 511ec128d6624a7cb8ff930db5763c8632e86def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!doctype html>
<meta charset=utf-8>
<title>Dispatching idle callbacks should be able to be suspended and then resumed</title>
<meta name="timeout" content="long">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id="log"></div>
<script>
  function withEventListener(target, event, handler) {
    handler = handler || (e => e);
    return new Promise(resolve => {
      let wrapper = function(e) {
        let result = handler(e);
        if (!result) {
          return;
        }

        resolve(result);
      }
      target.addEventListener(event, wrapper, { once: true });
    });
  }

  function makePostBackUrl(name) {
    return new URL('resources/post_name_on_load.html?name=' + name,
                   window.location).href;
  }

  function waitForMessage(message, handler) {
    return withEventListener(window, 'message', e => (e.data === message) && handler(e));;
  }

  function withWindow(name) {
    let win = window.open(makePostBackUrl(name))
    return waitForMessage(name, _ => win);
  }

  function navigateWindow(win, name) {
    win.location = makePostBackUrl(name);
    return waitForMessage(name, _ => win);
  }

  function waitDuration(delay) {
    return new Promise(resolve => {
      step_timeout(resolve, delay);
    })
  }

  function goBack(win) {
    var p = withEventListener(win, 'pagehide');
    win.history.back();
    return p;
  }

  promise_test(t => {
    let idleCalled = false;
    let running = true;
    return withWindow('foo')
      .then(win => {
        let callback = function(d) {
          idleCalled = true;
          if (running) {
            win.requestIdleCallback(callback);
          }
        };

        win.requestIdleCallback(callback);

        return navigateWindow(win, 'bar')
          .then(_ => idleCalled = false)
          .then(_ => waitDuration(2000))
          .then(_ => {
            assert_false(idleCalled, "idle callback shouldn't have been called yet");
            return goBack(win);
          })
          .then(_ => Promise.race([
            // At this point it's a matter of having bfcache ...
            waitDuration(2000)
              .then(_ => {
                assert_true(idleCalled, "idle callback should've been called by now");
                running = false;
              }),
            // ... or not. If not, we expect a load event.
            waitForMessage("foo", _ => win)
          ]))
          .then(_ => win.close())
          .catch(e => {
            win.close();
            throw e;
          })
      });
  });

</script>