test_enumerateDevices_navigation.html (1624B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <script src="mediaStreamPlayback.js"></script> 5 </head> 6 <body> 7 <iframe id="iframe" srcdoc="<script> 8 window.enumerateDevices = () => 9 navigator.mediaDevices.enumerateDevices(); 10 </script>" 11 width="100%" height="50%" frameborder="1"> 12 </iframe> 13 <pre id="test"> 14 <script type="application/javascript"> 15 createHTML({ title: "Suspend enumerateDevices code ", bug: "1479840" }); 16 /** 17 This test covers the case that the enumerateDevices method is suspended by 18 navigating away the current window. In order to implement that the enumeration 19 is executed in an iframe which is cleared before the enumeration has been resolved 20 */ 21 22 runTest(async () => { 23 // Run enumerate devices and mesure the time it will take. 24 const start = new Date().getTime(); 25 try { 26 await iframe.contentWindow.enumerateDevices(); 27 } catch (e) { 28 info("Failed to enumerate devices, error: " + e); 29 } 30 const elapsed = new Date().getTime() - start; 31 32 // Run again and navigate away. Expected to remain pending. 33 let p = iframe.contentWindow.enumerateDevices() 34 p.then( devices => { 35 ok(false, "Enumerate devices promise resolved unexpectedly, found " + devices.length + " devices."); 36 }) 37 .catch ( error => { 38 ok(false, "Enumerate devices promise rejected unexpectedly: " + error); 39 }); 40 iframe.srcdoc = ""; 41 42 // Wait enough time. 43 try { 44 await timeout(p, 5 * elapsed, "timeout"); 45 ok(false, "Enumerate devices promise resolved unexpectedly"); 46 } catch (e) { 47 is(e.message, "timeout", "We should time out without enumerateDevices rejecting"); 48 } 49 }); 50 51 </script> 52 </pre> 53 </body> 54 </html>