set_geolocation_override.https.html (3472B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"/> 3 <title>TestDriver bidi.emulation.set_geolocation_override method</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/resources/testdriver.js?feature=bidi"></script> 7 <script src="/resources/testdriver-vendor.js"></script> 8 9 <script> 10 promise_setup(async () => { 11 // Ensure permission is granted before proceeding. 12 await test_driver.bidi.permissions.set_permission({ 13 descriptor: {name: "geolocation"}, 14 state: "granted", 15 }); 16 }); 17 18 /** Get the current geolocation or error */ 19 function get_current_geolocation() { 20 return new Promise( 21 resolve => window.navigator.geolocation.getCurrentPosition( 22 position => resolve(position.coords.toJSON()), 23 error => resolve({code: error.code}), 24 // Fail fast if geolocation is not available. 25 {timeout: 500} 26 )) 27 } 28 29 /** Asserts that coordinate or error matches the expected value */ 30 function assert_coordinates_match(actual, expected) { 31 for (const key in expected) { 32 assert_equals(actual[key], expected[key], 33 `"${key}" should match the expected value ${expected[key]}`); 34 } 35 } 36 37 const SOME_COORDINATES = { 38 latitude: 52.51, 39 longitude: 13.39, 40 accuracy: 0.5, 41 altitude: 34, 42 altitudeAccuracy: 0.75, 43 heading: 180, 44 speed: 2.77 45 }; 46 47 promise_test(async (t) => { 48 t.add_cleanup(async () => { 49 await test_driver.bidi.emulation.set_geolocation_override( 50 {coordinates: null}); 51 }); 52 53 // Get the initial geolocation (might be error). 54 const initial_coords = await get_current_geolocation(); 55 56 // Set the geolocation override 57 await test_driver.bidi.emulation.set_geolocation_override({ 58 coordinates: SOME_COORDINATES 59 }); 60 61 // Get the geolocation after setting the override. 62 const coords = await get_current_geolocation(); 63 // Assert the coordinates match the override. 64 assert_coordinates_match(coords, SOME_COORDINATES); 65 66 // Clear the geolocation override. 67 await test_driver.bidi.emulation.set_geolocation_override( 68 {coordinates: null}); 69 // Assert coordinates are set to the original value. 70 assert_coordinates_match(await get_current_geolocation(), 71 initial_coords); 72 }, "emulate geolocation and clear override"); 73 74 promise_test(async (t) => { 75 // Emulate position unavailable. 76 await test_driver.bidi.emulation.set_geolocation_override({ 77 error: {type: 'positionUnavailable'}, 78 }); 79 80 // Get the geolocation after setting the override. 81 const error = await get_current_geolocation(); 82 83 assert_equals(error.code, 2, 84 `Geolocation should be unavailable with code POSITION_UNAVAILABLE = 2`); 85 }, "emulate geolocation position unavailable"); 86 87 promise_test(async (t) => { 88 assert_throws_js(Error, 89 () => test_driver.bidi.emulation.set_geolocation_override({ 90 error: {type: 'positionUnavailable'}, 91 coordinates: SOME_COORDINATES 92 }), "Setting both `coordinates` and `error` should fail"); 93 }, "cannot set `coordinates` and `error` simultaneously"); 94 </script>