dom/tests/mochitest/geolocation/geolocation_common.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/mochitest/geolocation/geolocation_common.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +var BASE_URL = "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs";
     1.5 +
     1.6 +function sleep(delay)
     1.7 +{
     1.8 +    var start = Date.now();
     1.9 +    while (Date.now() < start + delay);
    1.10 +}
    1.11 +
    1.12 +function force_prompt(allow, callback) {
    1.13 +  SpecialPowers.pushPrefEnv({"set": [["geo.prompt.testing", true], ["geo.prompt.testing.allow", allow]]}, callback);
    1.14 +}
    1.15 +
    1.16 +function start_sending_garbage(callback)
    1.17 +{
    1.18 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=respond-garbage"]]}, function() {
    1.19 +    // we need to be sure that all location data has been purged/set.
    1.20 +    sleep(1000);
    1.21 +    callback.call();
    1.22 +  });
    1.23 +}
    1.24 +
    1.25 +function stop_sending_garbage(callback)
    1.26 +{
    1.27 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + ""]]}, function() {
    1.28 +    // we need to be sure that all location data has been purged/set.
    1.29 +    sleep(1000);
    1.30 +    callback.call();
    1.31 +  });
    1.32 +}
    1.33 +
    1.34 +function stop_geolocationProvider(callback)
    1.35 +{
    1.36 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=stop-responding"]]}, function() {
    1.37 +    // we need to be sure that all location data has been purged/set.
    1.38 +    sleep(1000);
    1.39 +    callback.call();
    1.40 +  });
    1.41 +}
    1.42 +
    1.43 +function set_network_request_cache_enabled(enabled, callback)
    1.44 +{
    1.45 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.debug.requestCache.enabled", enabled]]}, callback);
    1.46 +}
    1.47 +
    1.48 +function worse_geolocationProvider(callback)
    1.49 +{
    1.50 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=worse-accuracy"]]}, callback);
    1.51 +}
    1.52 +
    1.53 +function resume_geolocationProvider(callback)
    1.54 +{
    1.55 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + ""]]}, callback);
    1.56 +}
    1.57 +
    1.58 +function delay_geolocationProvider(delay, callback)
    1.59 +{
    1.60 +  SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?delay=" + delay]]}, callback);
    1.61 +}
    1.62 +
    1.63 +function send404_geolocationProvider(callback)
    1.64 +{
    1.65 +  set_network_request_cache_enabled(false, function() {
    1.66 +    SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=send404"]]}, callback);});
    1.67 +}
    1.68 +
    1.69 +function check_geolocation(location) {
    1.70 +
    1.71 +  ok(location, "Check to see if this location is non-null");
    1.72 +
    1.73 +  ok("timestamp" in location, "Check to see if there is a timestamp");
    1.74 +
    1.75 +  // eventually, coords may be optional (eg, when civic addresses are supported)
    1.76 +  ok("coords" in location, "Check to see if this location has a coords");
    1.77 +
    1.78 +  var coords = location.coords;
    1.79 +
    1.80 +  ok("latitude" in coords, "Check to see if there is a latitude");
    1.81 +  ok("longitude" in coords, "Check to see if there is a longitude");
    1.82 +  ok("accuracy" in coords, "Check to see if there is a accuracy");
    1.83 +  
    1.84 +  // optional ok("altitude" in coords, "Check to see if there is a altitude");
    1.85 +  // optional ok("altitudeAccuracy" in coords, "Check to see if there is a alt accuracy");
    1.86 +  // optional ok("heading" in coords, "Check to see if there is a heading");
    1.87 +  // optional ok("speed" in coords, "Check to see if there is a speed");
    1.88 +
    1.89 +  ok (Math.abs(location.coords.latitude - 37.41857) < 0.001, "lat matches known value");
    1.90 +  ok (Math.abs(location.coords.longitude + 122.08769) < 0.001, "lon matches known value");
    1.91 +  // optional  ok(location.coords.altitude == 42, "alt matches known value");
    1.92 +  // optional  ok(location.coords.altitudeAccuracy == 42, "alt acc matches known value");
    1.93 +}
    1.94 +
    1.95 +function toggleGeolocationSetting(value, callback) {
    1.96 +  var mozSettings = window.navigator.mozSettings;
    1.97 +  var lock = mozSettings.createLock();
    1.98 +
    1.99 +  var geoenabled = {"geolocation.enabled": value};
   1.100 +
   1.101 +  req = lock.set(geoenabled);
   1.102 +  req.onsuccess = function () {
   1.103 +    ok(true, "set done");
   1.104 +    callback();
   1.105 +  }
   1.106 +}

mercurial