michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserver = null; michael@0: var geolocation = null; michael@0: var success = false; michael@0: var watchId = -1; michael@0: michael@0: function terminate(succ) { michael@0: success = succ; michael@0: geolocation.clearWatch(watchID); michael@0: } michael@0: michael@0: function successCallback(pos){ terminate(true); } michael@0: function errorCallback(pos) { terminate(false); } michael@0: michael@0: var observer = { michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsISupports) || michael@0: iid.equals(Ci.nsIObserver)) michael@0: return this; michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: michael@0: observe: function(subject, topic, data) { michael@0: if (data == "shutdown") { michael@0: do_check_true(1); michael@0: this._numProviders--; michael@0: if (!this._numProviders) { michael@0: httpserver.stop(function() { michael@0: do_check_true(success); michael@0: do_test_finished(); michael@0: }); michael@0: } michael@0: } michael@0: else if (data == "starting") { michael@0: do_check_true(1); michael@0: this._numProviders++; michael@0: } michael@0: }, michael@0: michael@0: _numProviders: 0, michael@0: }; michael@0: michael@0: function geoHandler(metadata, response) michael@0: { michael@0: var georesponse = { michael@0: status: "OK", michael@0: location: { michael@0: lat: 42, michael@0: lng: 42, michael@0: }, michael@0: accuracy: 42, michael@0: }; michael@0: var position = JSON.stringify(georesponse); michael@0: response.setStatusLine("1.0", 200, "OK"); michael@0: response.setHeader("Cache-Control", "no-cache", false); michael@0: response.setHeader("Content-Type", "aplication/x-javascript", false); michael@0: response.write(position); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: // XPCShell does not get a profile by default. The geolocation service michael@0: // depends on the settings service which uses IndexedDB and IndexedDB michael@0: // needs a place where it can store databases. michael@0: do_get_profile(); michael@0: michael@0: // only kill this test when shutdown is called on the provider. michael@0: do_test_pending(); michael@0: michael@0: httpserver = new HttpServer(); michael@0: httpserver.registerPathHandler("/geo", geoHandler); michael@0: httpserver.start(-1); michael@0: michael@0: var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); michael@0: prefs.setCharPref("geo.wifi.uri", "http://localhost:" + michael@0: httpserver.identity.primaryPort + "/geo"); michael@0: prefs.setBoolPref("dom.testing.ignore_ipc_principal", true); michael@0: prefs.setBoolPref("geo.wifi.scan", false); michael@0: michael@0: var obs = Cc["@mozilla.org/observer-service;1"].getService(); michael@0: obs = obs.QueryInterface(Ci.nsIObserverService); michael@0: obs.addObserver(observer, "geolocation-device-events", false); michael@0: michael@0: geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports); michael@0: watchID = geolocation.watchPosition(successCallback, errorCallback); michael@0: }