tojson.https.window.js (1528B)
1 // META: script=/resources/testdriver.js?feature=bidi 2 // META: script=/resources/testdriver-vendor.js 3 "use strict"; 4 5 function check_equals(original, json) { 6 const proto = Object.getPrototypeOf(original); 7 const keys = Object.keys(proto).filter( 8 (k) => typeof original[k] !== "function", 9 ); 10 for (const key of keys) { 11 assert_equals( 12 original[key], 13 json[key], 14 `${original.constructor.name} ${key} entry does not match its toJSON value`, 15 ); 16 } 17 } 18 19 promise_setup(async () => { 20 // Ensure permission is granted before proceeding. 21 await test_driver.bidi.permissions.set_permission({ 22 descriptor: {name: "geolocation"}, 23 state: "granted", 24 }); 25 }); 26 27 promise_test(async (t) => { 28 t.add_cleanup(async () => { 29 await test_driver.bidi.emulation.set_geolocation_override( 30 {coordinates: null}); 31 }); 32 33 const latitude = 51.478; 34 const longitude = -0.166; 35 const accuracy = 100; 36 await test_driver.bidi.emulation.set_geolocation_override({ 37 coordinates: {latitude, longitude, accuracy} 38 }); 39 40 const position = await new Promise((resolve, reject) => { 41 navigator.geolocation.getCurrentPosition(resolve, reject); 42 }); 43 44 const json = position.toJSON(); 45 assert_equals( 46 position.timestamp, 47 json.timestamp, 48 "GeolocationPosition timestamp entry does not match its toJSON value", 49 ); 50 51 check_equals(position.coords, json.coords); 52 check_equals(position.coords, position.coords.toJSON()); 53 }, "Test toJSON() in GeolocationPosition and GeolocationCoordinates.");