diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /dom/tests/unit/test_geolocation_monitor.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/tests/unit/test_geolocation_monitor.js')
-rw-r--r-- | dom/tests/unit/test_geolocation_monitor.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/dom/tests/unit/test_geolocation_monitor.js b/dom/tests/unit/test_geolocation_monitor.js new file mode 100644 index 0000000000..161a017b35 --- /dev/null +++ b/dom/tests/unit/test_geolocation_monitor.js @@ -0,0 +1,97 @@ +const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js"); + +let geolocation = null; +let locations = [ + [1, 2], + [3, 4], + [5, 6], +]; + +function geoHandler(metadata, response) { + let [lat, lng] = locations.shift(); + response.setStatusLine("1.0", 200, "OK"); + response.setHeader("Cache-Control", "no-cache", false); + response.setHeader("Content-Type", "application/x-javascript", false); + response.write( + JSON.stringify({ + status: "OK", + location: { lat, lng }, + accuracy: 42, + }) + ); +} + +function toJSON(pos) { + return { lat: pos.coords.latitude, lng: pos.coords.longitude }; +} + +function getPosition() { + return new Promise(function (resolve, reject) { + geolocation.getCurrentPosition(resolve, reject); + }); +} + +function watchPosition() { + let seen = 0; + return new Promise(function (resolve, reject) { + let id = geolocation.watchPosition(position => { + seen++; + if (seen === 1) { + Assert.deepEqual(toJSON(position), { lat: 3, lng: 4 }); + Assert.deepEqual(observer._lastData, { lat: 3, lng: 4 }); + Assert.equal(observer._countEvents, 2); + } else if (seen === 2) { + Assert.deepEqual(toJSON(position), { lat: 5, lng: 6 }); + Assert.deepEqual(observer._lastData, { lat: 5, lng: 6 }); + Assert.equal(observer._countEvents, 3); + geolocation.clearWatch(id); + resolve(); + } + }, reject); + }); +} + +let observer = { + QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), + + observe(subject, topic, data) { + Assert.equal(topic, "geolocation-position-events"); + observer._countEvents++; + observer._lastData = toJSON(subject); + }, + + _lastData: null, + _countEvents: 0, +}; + +add_task(async function test_resetClient() { + do_get_profile(); + geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports); + + let server = new HttpServer(); + server.registerPathHandler("/geo", geoHandler); + server.start(-1); + + Services.prefs.setCharPref( + "geo.provider.network.url", + "http://localhost:" + server.identity.primaryPort + "/geo" + ); + Services.prefs.setBoolPref( + "geo.provider.network.debug.requestCache.enabled", + false + ); + Services.prefs.setBoolPref("geo.provider.network.scan", false); + + var obs = Cc["@mozilla.org/observer-service;1"].getService(); + obs = obs.QueryInterface(Ci.nsIObserverService); + obs.addObserver(observer, "geolocation-position-events"); + + let position = await getPosition(); + Assert.deepEqual(toJSON(position), { lat: 1, lng: 2 }); + Assert.equal(observer._countEvents, 1); + Assert.deepEqual(observer._lastData, { lat: 1, lng: 2 }); + + await watchPosition(); + + await new Promise(resolve => server.stop(resolve)); +}); |