1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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));
});
|