tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_geolocation_provider.js (1986B)


      1 const { HttpServer } = ChromeUtils.importESModule(
      2  "resource://testing-common/httpd.sys.mjs"
      3 );
      4 
      5 var httpserver = null;
      6 var geolocation = null;
      7 var success = false;
      8 var watchID = -1;
      9 
     10 function terminate(succ) {
     11  success = succ;
     12  geolocation.clearWatch(watchID);
     13 }
     14 
     15 function successCallback() {
     16  terminate(true);
     17 }
     18 function errorCallback() {
     19  terminate(false);
     20 }
     21 
     22 var observer = {
     23  QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
     24 
     25  observe(subject, topic, data) {
     26    if (data == "shutdown") {
     27      Assert.ok(1);
     28      this._numProviders--;
     29      if (!this._numProviders) {
     30        httpserver.stop(function () {
     31          Assert.ok(success);
     32          do_test_finished();
     33        });
     34      }
     35    } else if (data == "starting") {
     36      Assert.ok(1);
     37      this._numProviders++;
     38    }
     39  },
     40 
     41  _numProviders: 0,
     42 };
     43 
     44 function geoHandler(metadata, response) {
     45  var georesponse = {
     46    status: "OK",
     47    location: {
     48      lat: 42,
     49      lng: 42,
     50    },
     51    accuracy: 42,
     52  };
     53  var position = JSON.stringify(georesponse);
     54  response.setStatusLine("1.0", 200, "OK");
     55  response.setHeader("Cache-Control", "no-cache", false);
     56  response.setHeader("Content-Type", "aplication/x-javascript", false);
     57  response.write(position);
     58 }
     59 
     60 function run_test() {
     61  // only kill this test when shutdown is called on the provider.
     62  do_test_pending();
     63 
     64  httpserver = new HttpServer();
     65  httpserver.registerPathHandler("/geo", geoHandler);
     66  httpserver.start(-1);
     67 
     68  Services.prefs.setCharPref(
     69    "geo.provider.network.url",
     70    "http://localhost:" + httpserver.identity.primaryPort + "/geo"
     71  );
     72  Services.prefs.setBoolPref("geo.provider.network.scan", false);
     73 
     74  var obs = Cc["@mozilla.org/observer-service;1"].getService();
     75  obs = obs.QueryInterface(Ci.nsIObserverService);
     76  obs.addObserver(observer, "geolocation-device-events");
     77 
     78  geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
     79  watchID = geolocation.watchPosition(successCallback, errorCallback);
     80 }