dom/tests/unit/test_geolocation_provider.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/unit/test_geolocation_provider.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,92 @@
     1.4 +const Cc = Components.classes;
     1.5 +const Ci = Components.interfaces;
     1.6 +const Cu = Components.utils;
     1.7 +const Cr = Components.results;
     1.8 +
     1.9 +Cu.import("resource://testing-common/httpd.js");
    1.10 +
    1.11 +var httpserver = null;
    1.12 +var geolocation = null;
    1.13 +var success = false;
    1.14 +var watchId = -1;
    1.15 +
    1.16 +function terminate(succ) {
    1.17 +      success = succ;
    1.18 +      geolocation.clearWatch(watchID);
    1.19 +    }
    1.20 +
    1.21 +function successCallback(pos){ terminate(true); }
    1.22 +function errorCallback(pos) { terminate(false); }
    1.23 +
    1.24 +var observer = {
    1.25 +    QueryInterface: function(iid) {
    1.26 +    if (iid.equals(Ci.nsISupports) ||
    1.27 +        iid.equals(Ci.nsIObserver))
    1.28 +        return this;
    1.29 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.30 +    },
    1.31 +
    1.32 +    observe: function(subject, topic, data) {
    1.33 +        if (data == "shutdown") {
    1.34 +            do_check_true(1);
    1.35 +            this._numProviders--;
    1.36 +            if (!this._numProviders) {
    1.37 +                httpserver.stop(function() {
    1.38 +                        do_check_true(success);
    1.39 +                        do_test_finished();
    1.40 +                    });
    1.41 +            }
    1.42 +        }
    1.43 +        else if (data == "starting") {
    1.44 +            do_check_true(1);
    1.45 +            this._numProviders++;
    1.46 +        }
    1.47 +    },
    1.48 +
    1.49 +    _numProviders: 0,
    1.50 +};
    1.51 +
    1.52 +function geoHandler(metadata, response)
    1.53 +{
    1.54 +    var georesponse = {
    1.55 +        status: "OK",
    1.56 +        location: {
    1.57 +            lat: 42,
    1.58 +            lng: 42,
    1.59 +        },
    1.60 +        accuracy: 42,
    1.61 +    };
    1.62 +  var position = JSON.stringify(georesponse);
    1.63 +  response.setStatusLine("1.0", 200, "OK");
    1.64 +  response.setHeader("Cache-Control", "no-cache", false);
    1.65 +  response.setHeader("Content-Type", "aplication/x-javascript", false);
    1.66 +  response.write(position);
    1.67 +}
    1.68 +
    1.69 +function run_test()
    1.70 +{
    1.71 +    // XPCShell does not get a profile by default. The geolocation service
    1.72 +    // depends on the settings service which uses IndexedDB and IndexedDB
    1.73 +    // needs a place where it can store databases.
    1.74 +    do_get_profile();
    1.75 +
    1.76 +    // only kill this test when shutdown is called on the provider.
    1.77 +    do_test_pending();
    1.78 +
    1.79 +    httpserver = new HttpServer();
    1.80 +    httpserver.registerPathHandler("/geo", geoHandler);
    1.81 +    httpserver.start(-1);
    1.82 +
    1.83 +    var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
    1.84 +    prefs.setCharPref("geo.wifi.uri", "http://localhost:" +
    1.85 +                      httpserver.identity.primaryPort + "/geo");
    1.86 +    prefs.setBoolPref("dom.testing.ignore_ipc_principal", true);
    1.87 +    prefs.setBoolPref("geo.wifi.scan", false);
    1.88 +
    1.89 +    var obs = Cc["@mozilla.org/observer-service;1"].getService();
    1.90 +    obs = obs.QueryInterface(Ci.nsIObserverService);
    1.91 +    obs.addObserver(observer, "geolocation-device-events", false);
    1.92 +
    1.93 +    geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
    1.94 +    watchID = geolocation.watchPosition(successCallback, errorCallback);
    1.95 +}

mercurial