Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Tests for watchPosition and getCurrentPosition with setHighAccuracy</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
/* global sendAsyncMessage, addMessageListener */
function mockChromeScript() {
let highAccuracy = false;
let hasSetHighAccuracy = false;
function enableHighAccuracy(subject, topic, data) {
highAccuracy = data == "true";
hasSetHighAccuracy |= highAccuracy;
};
Services.obs.addObserver(enableHighAccuracy, "testing-geolocation-high-accuracy");
addMessageListener("cleanup", () => {
Services.obs.removeObserver(enableHighAccuracy, "testing-geolocation-high-accuracy");
});
addMessageListener("getHighAccuracy", () => highAccuracy);
addMessageListener("hasSetHighAccuracy", () => hasSetHighAccuracy);
sendAsyncMessage("ready");
}
add_setup(async () => {
await SpecialPowers.pushPrefEnv({
set: [["geo.prompt.testing", true], ["geo.prompt.testing.allow", true]],
});
})
add_task(async function test_watchPosition() {
let chromeScript = SpecialPowers.loadChromeScript(mockChromeScript);
await chromeScript.promiseOneMessage("ready");
let { promise, resolve, reject } = Promise.withResolvers();
let id = navigator.geolocation.watchPosition(resolve, reject, { enableHighAccuracy: true });
await promise;
let highAccuracy = await chromeScript.sendQuery("getHighAccuracy");
ok(highAccuracy, "enableHighAccuracy option should be enabled");
navigator.geolocation.clearWatch(id);
({ promise, resolve, reject } = Promise.withResolvers());
id = navigator.geolocation.watchPosition(resolve, reject, { enableHighAccuracy: false });
await promise;
highAccuracy = await chromeScript.sendQuery("getHighAccuracy");
ok(!highAccuracy, "enableHighAccuracy option should be disabled");
navigator.geolocation.clearWatch(id);
await chromeScript.sendQuery("cleanup");
chromeScript.destroy();
});
add_task(async function test_getCurrentPosition() {
let chromeScript = SpecialPowers.loadChromeScript(mockChromeScript);
await chromeScript.promiseOneMessage("ready");
let { promise, resolve, reject } = Promise.withResolvers();
navigator.geolocation.getCurrentPosition(resolve, reject, { enableHighAccuracy: false });
await promise;
let hasSetHighAccuracy = await chromeScript.sendQuery("hasSetHighAccuracy");
ok(!hasSetHighAccuracy, "enableHighAccuracy option should have never been enabled");
let highAccuracy = await chromeScript.sendQuery("getHighAccuracy");
ok(!highAccuracy, "enableHighAccuracy option should be disabled");
({ promise, resolve, reject } = Promise.withResolvers());
navigator.geolocation.getCurrentPosition(resolve, reject, { enableHighAccuracy: true });
await promise;
hasSetHighAccuracy = await chromeScript.sendQuery("hasSetHighAccuracy");
ok(hasSetHighAccuracy, "enableHighAccuracy option should have been enabled");
highAccuracy = await chromeScript.sendQuery("getHighAccuracy");
ok(!highAccuracy, "enableHighAccuracy option should be disabled");
await chromeScript.sendQuery("cleanup");
chromeScript.destroy();
});
</script>
</body>
</html>