dom/tests/unit/test_geolocation_provider.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial