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