mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-27 14:52:16 +00:00
Bug 1653602 - Add a geolocation monitor r=baku
Differential Revision: https://phabricator.services.mozilla.com/D85938
This commit is contained in:
parent
aed60cdf61
commit
19745e4e98
@ -413,6 +413,10 @@ void nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition) {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
||||
obs->NotifyObservers(wrapped, "geolocation-position-events",
|
||||
u"location-updated");
|
||||
|
||||
nsAutoMicroTask mt;
|
||||
if (mCallback.HasWebIDLCallback()) {
|
||||
RefPtr<PositionCallback> callback = mCallback.GetWebIDLCallback();
|
||||
|
100
dom/tests/unit/test_geolocation_monitor.js
Normal file
100
dom/tests/unit/test_geolocation_monitor.js
Normal file
@ -0,0 +1,100 @@
|
||||
const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
|
||||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
|
||||
let geolocation = null;
|
||||
let numRequests = 0;
|
||||
let locations = [
|
||||
[1, 2],
|
||||
[3, 4],
|
||||
[5, 6],
|
||||
];
|
||||
|
||||
function geoHandler(metadata, response) {
|
||||
numRequests++;
|
||||
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));
|
||||
});
|
@ -5,6 +5,7 @@ head =
|
||||
[test_bug465752.js]
|
||||
[test_Fetch.js]
|
||||
[test_geolocation_provider.js]
|
||||
[test_geolocation_monitor.js]
|
||||
[test_geolocation_timeout.js]
|
||||
[test_geolocation_timeout_wrap.js]
|
||||
skip-if = os == "mac" || os == "android"
|
||||
|
Loading…
Reference in New Issue
Block a user