geolocation_common.js (4016B)
1 "use strict"; 2 3 var harness = 4 SimpleTest.harnessParameters.testRoot == "chrome" ? "chrome" : "tests"; 5 var BASE_URL = 6 "http://mochi.test:8888/" + 7 harness + 8 "/dom/geolocation/test/mochitest/network_geolocation.sjs"; 9 10 function set_geo_wifi_uri(uri, callback) { 11 // Disable NetworkGeolocationProvider.js request cache because the cache 12 // does not remember from which location service it came from. We expect 13 // different results when we change the provider URL (geo.provider.network.url). 14 set_network_request_cache_enabled(false, () => { 15 SpecialPowers.pushPrefEnv( 16 { set: [["geo.provider.network.url", uri]] }, 17 callback 18 ); 19 }); 20 } 21 22 function force_prompt(allow, callback) { 23 SpecialPowers.pushPrefEnv( 24 { 25 set: [ 26 ["geo.prompt.testing", true], 27 ["geo.prompt.testing.allow", allow], 28 ], 29 }, 30 callback 31 ); 32 } 33 34 function start_sending_garbage(callback) { 35 set_geo_wifi_uri(BASE_URL + "?action=respond-garbage", () => { 36 // we need to be sure that all location data has been purged/set. 37 setTimeout(() => { 38 callback.call(); 39 }, 1000); 40 }); 41 } 42 43 function stop_sending_garbage(callback) { 44 set_geo_wifi_uri(BASE_URL + "", () => { 45 // we need to be sure that all location data has been purged/set. 46 setTimeout(() => { 47 callback.call(); 48 }, 1000); 49 }); 50 } 51 52 function stop_geolocationProvider(callback) { 53 set_geo_wifi_uri(BASE_URL + "?action=stop-responding", () => { 54 // we need to be sure that all location data has been purged/set. 55 setTimeout(() => { 56 callback.call(); 57 }, 1000); 58 }); 59 } 60 61 function set_network_request_cache_enabled(enabled, callback) { 62 SpecialPowers.pushPrefEnv( 63 { set: [["geo.provider.network.debug.requestCache.enabled", enabled]] }, 64 callback 65 ); 66 } 67 68 function worse_geolocationProvider(callback) { 69 set_geo_wifi_uri(BASE_URL + "?action=worse-accuracy", callback); 70 } 71 72 function resume_geolocationProvider(callback) { 73 set_geo_wifi_uri(BASE_URL + "", callback); 74 } 75 76 function delay_geolocationProvider(delay, callback) { 77 set_geo_wifi_uri(BASE_URL + "?delay=" + delay, callback); 78 } 79 80 function send404_geolocationProvider(callback) { 81 set_geo_wifi_uri(BASE_URL + "?action=send404", callback); 82 } 83 84 function check_geolocation(location) { 85 ok(location, "Check to see if this location is non-null"); 86 87 const timestamp = location.timestamp; 88 dump(`timestamp=${timestamp}\n`); 89 ok(IsNumber(timestamp), "check timestamp type"); 90 ok(timestamp > 0, "check timestamp range"); 91 92 // eventually, coords may be optional (eg, when civic addresses are supported) 93 ok("coords" in location, "Check to see if this location has a coords"); 94 95 const { 96 latitude, 97 longitude, 98 accuracy, 99 altitude, 100 altitudeAccuracy, 101 speed, 102 heading, 103 } = location.coords; 104 105 dump(`latitude=${latitude}\n`); 106 dump(`longitude=${longitude}\n`); 107 dump(`accuracy=${accuracy}\n`); 108 dump(`altitude=${altitude}\n`); 109 dump(`altitudeAccuracy=${altitudeAccuracy}\n`); 110 dump(`speed=${speed}\n`); 111 dump(`heading=${heading}\n`); 112 113 ok(IsNumber(latitude), "check latitude type"); 114 ok(IsNumber(longitude), "check longitude type"); 115 116 ok( 117 Math.abs(latitude - 37.41857) < 0.001, 118 "latitude matches hard-coded value" 119 ); 120 ok( 121 Math.abs(longitude + 122.08769) < 0.001, 122 "longitude matches hard-coded value" 123 ); 124 125 ok(IsNonNegativeNumber(accuracy), "check accuracy type and range"); 126 ok(IsNumber(altitude) || altitude === null, "check accuracy type"); 127 128 ok( 129 (IsNonNegativeNumber(altitudeAccuracy) && IsNumber(altitude)) || 130 altitudeAccuracy === null, 131 "check altitudeAccuracy type and range" 132 ); 133 134 ok( 135 IsNonNegativeNumber(speed) || speed === null, 136 "check speed type and range" 137 ); 138 139 ok( 140 (IsNonNegativeNumber(heading) && heading < 360 && speed > 0) || 141 heading === null, 142 "check heading type and range" 143 ); 144 } 145 146 function IsNumber(x) { 147 return typeof x === "number" && !Number.isNaN(x); 148 } 149 150 function IsNonNegativeNumber(x) { 151 return IsNumber(x) && x >= 0; 152 }