test_not_fully_active.html (3267B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title> 5 Test for when geolocation is used on non fully active documents 6 </title> 7 <script src="/tests/SimpleTest/SimpleTest.js"></script> 8 <script src="geolocation_common.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <script> 13 /* global GeolocationPositionError */ 14 SimpleTest.waitForExplicitFinish(); 15 16 async function runTest() { 17 // Create the iframe, wait for it to load... 18 const iframe = document.createElement("iframe"); 19 20 // We rely on this popup.html to acquire prompt privileges. 21 iframe.src = "popup.html"; 22 document.body.appendChild(iframe); 23 iframe.contentWindow.opener = window; 24 await new Promise(r => window.addEventListener("message", r, { once: true })); 25 26 // Steal geolocation. 27 const geo = iframe.contentWindow.navigator.geolocation; 28 29 // No longer fully active. 30 iframe.remove(); 31 32 // Try to watch a position while not fully active... 33 const watchError = await new Promise((resolve, reject) => { 34 const result = geo.watchPosition( 35 reject, // We don't want a position 36 resolve // We want an error! 37 ); 38 is(result, 0, "watchPosition returns 0 on non-fully-active document"); 39 }); 40 is( 41 watchError.code, 42 GeolocationPositionError.POSITION_UNAVAILABLE, 43 "watchPosition returns an error on non-fully-active document" 44 ); 45 46 // Now try to get current position while not fully active... 47 const positionError = await new Promise((resolve, reject) => { 48 geo.getCurrentPosition( 49 reject, // We don't want a position 50 resolve // We want an error! 51 ); 52 }); 53 is( 54 positionError.code, 55 GeolocationPositionError.POSITION_UNAVAILABLE, 56 "getCurrentPosition returns an error on non-fully-active document" 57 ); 58 59 // Re-attach, and go back to fully active. 60 document.body.appendChild(iframe); 61 iframe.contentWindow.opener = window; 62 await new Promise(r => window.addEventListener("message", r, { once: true })); 63 64 // And we are back to fully active. 65 let watchId; 66 let position = await new Promise((resolve, reject) => { 67 watchId = iframe.contentWindow.navigator.geolocation.watchPosition( 68 resolve, 69 reject 70 ); 71 }); 72 ok(watchId > 0, "Expected anything greater than 0"); 73 ok(position, "Expected a position"); 74 75 // Finally, let's get the position from the reattached document. 76 position = await new Promise((resolve, reject) => { 77 iframe.contentWindow.navigator.geolocation.getCurrentPosition( 78 resolve, 79 reject 80 ); 81 }); 82 ok(position, "Expected a position"); 83 iframe.contentWindow.navigator.geolocation.clearWatch(watchId); 84 iframe.remove(); 85 } 86 87 resume_geolocationProvider(async () => { 88 await new Promise(r => force_prompt(true, r)); 89 await runTest(); 90 SimpleTest.finish(); 91 }); 92 </script> 93 </body> 94 </html>