Skip to content

Commit 0160ba8

Browse files
committed
GeolocationCoordinates should expose a Default .toJSON() method
https://bugs.webkit.org/show_bug.cgi?id=272434 rdar://126183686 Reviewed by Ryosuke Niwa. Implements and tests the toJSON() method for the GeolocationCoordinates interface, as proposed: w3c/geolocation#147 * LayoutTests/fast/dom/Geolocation/coordinates-interface-toJSON-expected.txt: Added. * LayoutTests/fast/dom/Geolocation/coordinates-interface-toJSON.html: Added. * Source/WebCore/Modules/geolocation/GeolocationCoordinates.idl: Canonical link: https://commits.webkit.org/277347@main
1 parent 629488f commit 0160ba8

File tree

3 files changed

+190
-1
lines changed

3 files changed

+190
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Test GeolocationCoordinates toJSON() method
2+
3+
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
4+
5+
6+
PASS typeof window.actual === 'object' is true
7+
PASS window.actual.latitude is window.expected.latitude
8+
PASS window.actual.longitude is window.expected.longitude
9+
PASS window.actual.altitude is window.expected.altitude
10+
PASS window.actual.accuracy is window.expected.accuracy
11+
PASS window.actual.altitudeAccuracy is window.expected.altitudeAccuracy
12+
PASS window.actual.heading is window.expected.heading
13+
PASS window.actual.speed is window.expected.speed
14+
PASS window.actual.floorLevel is window.expected.floorLevel
15+
PASS typeof window.actual === 'object' is true
16+
PASS window.actual.latitude is window.expected.latitude
17+
PASS window.actual.longitude is window.expected.longitude
18+
PASS window.actual.altitude is window.expected.altitude
19+
PASS window.actual.accuracy is window.expected.accuracy
20+
PASS window.actual.altitudeAccuracy is window.expected.altitudeAccuracy
21+
PASS window.actual.heading is window.expected.heading
22+
PASS window.actual.speed is window.expected.speed
23+
PASS window.actual.floorLevel is window.expected.floorLevel
24+
PASS typeof window.actual === 'object' is true
25+
PASS window.actual.latitude is window.expected.latitude
26+
PASS window.actual.longitude is window.expected.longitude
27+
PASS window.actual.altitude is window.expected.altitude
28+
PASS window.actual.accuracy is window.expected.accuracy
29+
PASS window.actual.altitudeAccuracy is window.expected.altitudeAccuracy
30+
PASS window.actual.heading is window.expected.heading
31+
PASS window.actual.speed is window.expected.speed
32+
PASS window.actual.floorLevel is window.expected.floorLevel
33+
PASS typeof window.actual === 'object' is true
34+
PASS window.actual.latitude is window.expected.latitude
35+
PASS window.actual.longitude is window.expected.longitude
36+
PASS window.actual.altitude is window.expected.altitude
37+
PASS window.actual.accuracy is window.expected.accuracy
38+
PASS window.actual.altitudeAccuracy is window.expected.altitudeAccuracy
39+
PASS window.actual.heading is window.expected.heading
40+
PASS window.actual.speed is window.expected.speed
41+
PASS window.actual.floorLevel is window.expected.floorLevel
42+
PASS typeof window.actual === 'object' is true
43+
PASS window.actual.latitude is window.expected.latitude
44+
PASS window.actual.longitude is window.expected.longitude
45+
PASS window.actual.altitude is window.expected.altitude
46+
PASS window.actual.accuracy is window.expected.accuracy
47+
PASS window.actual.altitudeAccuracy is window.expected.altitudeAccuracy
48+
PASS window.actual.heading is window.expected.heading
49+
PASS window.actual.speed is window.expected.speed
50+
PASS window.actual.floorLevel is window.expected.floorLevel
51+
PASS successfullyParsed is true
52+
53+
TEST COMPLETE
54+
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<!DOCTYPE html PUBLIC "-//IETF//DTD HTML//EN">
2+
<html>
3+
<head>
4+
<script src="../../../resources/js-test-pre.js"></script>
5+
</head>
6+
<body>
7+
<script>
8+
description("Test GeolocationCoordinates toJSON() method");
9+
10+
testRunner.setGeolocationPermission(true);
11+
12+
window.jsTestIsAsync = true;
13+
14+
const baseExpected = {
15+
altitude: null,
16+
altitudeAccuracy: null,
17+
heading: null,
18+
speed: null,
19+
floorLevel: null,
20+
};
21+
22+
const testData = [
23+
[
24+
{ latitude: 1, longitude: 2, accuracy: 3 },
25+
{ ...baseExpected, latitude: 1, longitude: 2, accuracy: 3 },
26+
],
27+
[
28+
{ latitude: 2, longitude: 3, accuracy: 4, speed: 5 },
29+
{ ...baseExpected, latitude: 2, longitude: 3, accuracy: 4, speed: 5 },
30+
],
31+
[
32+
{
33+
latitude: 3,
34+
longitude: 4,
35+
accuracy: 5,
36+
altitudeAccuracy: 6,
37+
speed: 7,
38+
},
39+
{
40+
...baseExpected,
41+
latitude: 3,
42+
longitude: 4,
43+
accuracy: 5,
44+
altitudeAccuracy: 6,
45+
speed: 7,
46+
},
47+
],
48+
[
49+
{
50+
latitude: 4,
51+
longitude: 5,
52+
accuracy: 6,
53+
altitudeAccuracy: 7,
54+
heading: 8,
55+
speed: 9,
56+
},
57+
{
58+
...baseExpected,
59+
latitude: 4,
60+
longitude: 5,
61+
accuracy: 6,
62+
altitudeAccuracy: 7,
63+
heading: 8,
64+
speed: 9,
65+
},
66+
],
67+
[
68+
{
69+
latitude: 5,
70+
longitude: 6,
71+
accuracy: 7,
72+
altitude: 8,
73+
altitudeAccuracy: 9,
74+
heading: 10,
75+
speed: 11,
76+
},
77+
{
78+
...baseExpected,
79+
latitude: 5,
80+
longitude: 6,
81+
accuracy: 7,
82+
altitude: 8,
83+
altitudeAccuracy: 9,
84+
heading: 10,
85+
speed: 11,
86+
},
87+
],
88+
];
89+
90+
async function runNextTest() {
91+
for (const [actual, expected] of testData) {
92+
window.expected = expected;
93+
const {
94+
latitude,
95+
longitude,
96+
accuracy,
97+
altitude,
98+
altitudeAccuracy,
99+
heading,
100+
speed,
101+
floorLevel,
102+
} = actual;
103+
104+
testRunner.setMockGeolocationPosition(
105+
latitude,
106+
longitude,
107+
accuracy,
108+
altitude,
109+
altitudeAccuracy,
110+
heading,
111+
speed,
112+
floorLevel
113+
);
114+
115+
const position = await new Promise((resolve, reject) =>
116+
navigator.geolocation.getCurrentPosition(resolve, reject)
117+
);
118+
119+
window.actual = position.coords.toJSON();
120+
121+
shouldBeTrue(`typeof window.actual === 'object'`);
122+
123+
// We use "actual" for future proofing on purpose, in case more properties get added to the interface.
124+
for (const key in window.actual) {
125+
shouldBe(`window.actual.${key}`, `window.expected.${key}`);
126+
}
127+
}
128+
}
129+
130+
runNextTest().finally(finishJSTest);
131+
</script>
132+
<script src="../../../resources/js-test-post.js"></script>
133+
</body>
134+
</html>

Source/WebCore/Modules/geolocation/GeolocationCoordinates.idl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
readonly attribute unrestricted double? altitudeAccuracy;
3737
readonly attribute unrestricted double? heading;
3838
readonly attribute unrestricted double? speed;
39-
39+
[Default] object toJSON();
40+
4041
// Non standard.
4142
[EnabledBySetting=GeolocationFloorLevelEnabled] readonly attribute unrestricted double? floorLevel;
4243
};

0 commit comments

Comments
 (0)