wakelock-active-document.https.window.js (3664B)
1 function getWakeLockObject(iframe, url) { 2 return new Promise(resolve => { 3 iframe.addEventListener( 4 "load", 5 () => { 6 const { wakeLock } = iframe.contentWindow.navigator; 7 resolve(wakeLock); 8 }, 9 { once: true } 10 ); 11 iframe.src = url; 12 }); 13 } 14 15 promise_test(async t => { 16 const iframe = document.createElement("iframe"); 17 document.body.appendChild(iframe); 18 // We first got to page1.html, grab a WakeLock object. 19 const wakeLock1 = await getWakeLockObject( 20 iframe, 21 "/screen-wake-lock/resources/page1.html" 22 ); 23 // Save the DOMException of page1.html before navigating away. 24 const frameDOMException1 = iframe.contentWindow.DOMException; 25 // We navigate the iframe again, putting wakeLock1's document into an inactive state. 26 const wakeLock2 = await getWakeLockObject( 27 iframe, 28 "/screen-wake-lock/resources/page2.html" 29 ); 30 // Now, wakeLock1's relevant global object's document is no longer active. 31 // So, call .request(), and make sure it rejects appropriately. 32 await promise_rejects_dom( 33 t, 34 "NotAllowedError", 35 frameDOMException1, 36 wakeLock1.request('screen'), 37 "Inactive document, so must throw NotAllowedError" 38 ); 39 // We are done, so clean up. 40 iframe.remove(); 41 }, "navigator.wakeLock.request() aborts if the document becomes not active."); 42 43 promise_test(async t => { 44 const iframe = document.createElement("iframe"); 45 document.body.appendChild(iframe); 46 const wakeLock = await getWakeLockObject( 47 iframe, 48 "/screen-wake-lock/resources/page1.html" 49 ); 50 // Save the DOMException of page1.html before navigating away. 51 const frameDOMException = iframe.contentWindow.DOMException; 52 iframe.remove(); 53 await promise_rejects_dom( 54 t, 55 "NotAllowedError", 56 frameDOMException, 57 wakeLock.request('screen'), 58 "Inactive document, so must throw NotAllowedError" 59 ); 60 }, "navigator.wakeLock.request() aborts if the document is not fully active."); 61 62 promise_test(async t => { 63 // We nest two iframes and wait for them to load. 64 const outerIframe = document.createElement("iframe"); 65 document.body.appendChild(outerIframe); 66 // Load the outer iframe (we don't care about the awaited request) 67 await getWakeLockObject( 68 outerIframe, 69 "/screen-wake-lock/resources/page1.html" 70 ); 71 72 // Now we create the inner iframe 73 const innerIframe = outerIframe.contentDocument.createElement("iframe"); 74 75 // nest them 76 outerIframe.contentDocument.body.appendChild(innerIframe); 77 78 // load innerIframe, and get the WakeLock instance 79 const wakeLock = await getWakeLockObject( 80 innerIframe, 81 "/screen-wake-lock/resources/page2.html" 82 ); 83 // Save DOMException from innerIframe before navigating away. 84 const innerIframeDOMException = innerIframe.contentWindow.DOMException; 85 86 // Navigate the outer iframe to a new location. 87 // Wait for the load event to fire. 88 await new Promise(resolve => { 89 outerIframe.addEventListener("load", resolve); 90 outerIframe.src = "/screen-wake-lock/resources/page2.html"; 91 }); 92 93 // Now, request's relevant global object's document is still active 94 // (it is the active document of the inner iframe), but is not fully active 95 // (since the parent of the inner iframe is itself no longer active). 96 // So, call request.show() and make sure it rejects appropriately. 97 await promise_rejects_dom( 98 t, 99 "NotAllowedError", 100 innerIframeDOMException, 101 wakeLock.request('screen'), 102 "Active, but not fully active, so must throw NotAllowedError" 103 ); 104 // We are done, so clean up. 105 outerIframe.remove(); 106 }, "navigator.wakeLock.request() aborts if the document is active, but not fully active.");