dom/tests/unit/test_geolocation_provider.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:09691973ec9d
1 const Cc = Components.classes;
2 const Ci = Components.interfaces;
3 const Cu = Components.utils;
4 const Cr = Components.results;
5
6 Cu.import("resource://testing-common/httpd.js");
7
8 var httpserver = null;
9 var geolocation = null;
10 var success = false;
11 var watchId = -1;
12
13 function terminate(succ) {
14 success = succ;
15 geolocation.clearWatch(watchID);
16 }
17
18 function successCallback(pos){ terminate(true); }
19 function errorCallback(pos) { terminate(false); }
20
21 var observer = {
22 QueryInterface: function(iid) {
23 if (iid.equals(Ci.nsISupports) ||
24 iid.equals(Ci.nsIObserver))
25 return this;
26 throw Cr.NS_ERROR_NO_INTERFACE;
27 },
28
29 observe: function(subject, topic, data) {
30 if (data == "shutdown") {
31 do_check_true(1);
32 this._numProviders--;
33 if (!this._numProviders) {
34 httpserver.stop(function() {
35 do_check_true(success);
36 do_test_finished();
37 });
38 }
39 }
40 else if (data == "starting") {
41 do_check_true(1);
42 this._numProviders++;
43 }
44 },
45
46 _numProviders: 0,
47 };
48
49 function geoHandler(metadata, response)
50 {
51 var georesponse = {
52 status: "OK",
53 location: {
54 lat: 42,
55 lng: 42,
56 },
57 accuracy: 42,
58 };
59 var position = JSON.stringify(georesponse);
60 response.setStatusLine("1.0", 200, "OK");
61 response.setHeader("Cache-Control", "no-cache", false);
62 response.setHeader("Content-Type", "aplication/x-javascript", false);
63 response.write(position);
64 }
65
66 function run_test()
67 {
68 // XPCShell does not get a profile by default. The geolocation service
69 // depends on the settings service which uses IndexedDB and IndexedDB
70 // needs a place where it can store databases.
71 do_get_profile();
72
73 // only kill this test when shutdown is called on the provider.
74 do_test_pending();
75
76 httpserver = new HttpServer();
77 httpserver.registerPathHandler("/geo", geoHandler);
78 httpserver.start(-1);
79
80 var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
81 prefs.setCharPref("geo.wifi.uri", "http://localhost:" +
82 httpserver.identity.primaryPort + "/geo");
83 prefs.setBoolPref("dom.testing.ignore_ipc_principal", true);
84 prefs.setBoolPref("geo.wifi.scan", false);
85
86 var obs = Cc["@mozilla.org/observer-service;1"].getService();
87 obs = obs.QueryInterface(Ci.nsIObserverService);
88 obs.addObserver(observer, "geolocation-device-events", false);
89
90 geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
91 watchID = geolocation.watchPosition(successCallback, errorCallback);
92 }

mercurial