test_hidden.html (3285B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1653549 5 --> 6 <meta charset="utf-8"> 7 <title>Test that geolocation position can't be gotten when document is hidden</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <script src="geolocation_common.js"></script> 10 <link rel="stylesheet" href="/tests/SimpleTest/test.css" /> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1653549">Mozilla Bug 1653549</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"> 15 </div> 16 <pre id="test"> 17 18 <script class="testbody"> 19 SimpleTest.waitForExplicitFinish(); 20 SimpleTest.requestFlakyTimeout("we need to be sure that all location data has been purged/set"); 21 22 // Little promise wrapper helper. 23 function p(f) { 24 return new Promise((r) => f(r)); 25 } 26 27 resume_geolocationProvider(async () => { 28 // Initialize 29 await new Promise((r) => force_prompt(true, r)); 30 const popupWindow = window.open("popup.html"); 31 popupWindow.opener = window; 32 await new Promise((r) => 33 window.addEventListener("message", r, { once: true }) 34 ); 35 36 // Confirm everything is working ok... 37 const geo = popupWindow.navigator.geolocation; 38 await new Promise((resolve, reject) => { 39 geo.getCurrentPosition(resolve, reject); 40 }); 41 42 // Hide the document... 43 const hiddenPromise = new Promise( 44 (r) => (popupWindow.document.onvisibilitychange = r) 45 ); 46 await SimpleTest.promiseFocus(window); 47 await hiddenPromise; 48 49 // Send data to be ignored... 50 await p(start_sending_garbage); 51 await p(stop_sending_garbage); 52 await p(resume_geolocationProvider); 53 54 // The following promises only resolve successfully when document is visible, 55 // meaning that position updates are ignored when the document is hidden. 56 let success = false; 57 let watchId = null; 58 const watchPositionPromise = new Promise((resolve) => { 59 watchId = geo.watchPosition( 60 () => { 61 ok(success, "watchPosition was called."); 62 if (!success) { 63 throw new Error("watchPosition was called too early"); 64 } 65 resolve(); 66 }, 67 () => { 68 // It's still allowed to get error when document is hidden 69 ok(true, "Error callback of watchPosition was called."); 70 } 71 ); 72 }); 73 74 const currentPositionPromise = new Promise((resolve, reject) => { 75 geo.getCurrentPosition( 76 () => { 77 ok(success, "getCurrentPosition was called."); 78 if (!success){ 79 throw new Error("getCurrentPosition was called too early"); 80 } 81 resolve(); 82 }, 83 () => { 84 reject(new Error("Error callback of getCurrentPosition must not be called.")) 85 } 86 ); 87 }); 88 89 // Refocus popup window... 90 const visiblePopupPromise = new Promise( 91 (r) => (popupWindow.document.onvisibilitychange = r) 92 ); 93 await SimpleTest.promiseFocus(popupWindow); 94 await visiblePopupPromise; 95 96 // Resuming the geolocation events must now cause the promises to resolve correctly (with success = true). 97 success = true; 98 await p(resume_geolocationProvider); 99 await Promise.all([currentPositionPromise, watchPositionPromise]); 100 101 // Cleanup and finish! 102 geo.clearWatch(watchId); 103 await SimpleTest.promiseFocus(window); 104 popupWindow.close(); 105 SimpleTest.finish(); 106 }); 107 108 </script> 109 </pre> 110 </body> 111 </html>