Bug 1890706 - Implement default toJSON for GeolocationCoordinates and GeolocationPosition and add a web platform test; r=saschanaz,webidl,smaug

Differential Revision: https://phabricator.services.mozilla.com/D213741
This commit is contained in:
Thomas Wisniewski 2024-06-20 11:03:11 +00:00
parent f270de514e
commit 7ebc27abfc
4 changed files with 55 additions and 6 deletions

View File

@ -19,4 +19,6 @@ interface GeolocationCoordinates {
readonly attribute double? altitudeAccuracy;
readonly attribute double? heading;
readonly attribute double? speed;
[Default] object toJSON();
};

View File

@ -14,4 +14,6 @@
interface GeolocationPosition {
readonly attribute GeolocationCoordinates coords;
readonly attribute EpochTimeStamp timestamp;
[Default] object toJSON();
};

View File

@ -1,6 +0,0 @@
[idlharness.https.window.html]
[GeolocationPosition interface: operation toJSON()]
expected: FAIL
[GeolocationCoordinates interface: operation toJSON()]
expected: FAIL

View File

@ -0,0 +1,51 @@
// META: script=/resources/testdriver.js
// META: script=/resources/testdriver-vendor.js
'use strict';
function check_coords(original, json, prefix) {
for (const key of [
'accuracy',
'latitude',
'longitude',
'altitude',
'altitudeAccuracy',
'heading',
'speed',
'coords',
'timestamp',
]) {
assert_equals(original[key], json[key], `${prefix} ${key} entry does not match its toJSON value`);
}
}
promise_setup(async () => {
await test_driver.set_permission({ name: "geolocation" }, "granted");
if (document.readyState != 'complete') {
await new Promise(resolve => {
window.addEventListener('load', resolve, {once: true});
});
}
}, 'Grant permission and wait for the document to be fully active.');
promise_test(async (t) => {
const position = await new Promise((resolve, reject) => {
navigator.geolocation.getCurrentPosition(
t.step_func((position) => {
resolve(position);
}),
t.step_func((error) => {
reject(error.message);
}),
);
});
assert_equals(typeof(position.toJSON), 'function');
const json = position.toJSON();
assert_equals(position.timestamp, json.timestamp, 'GeolocationPosition timestamp entry does not match its toJSON value');
check_coords(position.coords, json.coords, 'GeolocationPosition coords');
assert_equals(typeof(position.coords.toJSON), 'function');
check_coords(position.coords, position.coords.toJSON(), 'GeolocationCoordinates');
}, 'Test toJSON() in GeolocationPosition and GeolocationCoordinates.');