|
1 var BASE_URL = "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs"; |
|
2 |
|
3 function sleep(delay) |
|
4 { |
|
5 var start = Date.now(); |
|
6 while (Date.now() < start + delay); |
|
7 } |
|
8 |
|
9 function force_prompt(allow, callback) { |
|
10 SpecialPowers.pushPrefEnv({"set": [["geo.prompt.testing", true], ["geo.prompt.testing.allow", allow]]}, callback); |
|
11 } |
|
12 |
|
13 function start_sending_garbage(callback) |
|
14 { |
|
15 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=respond-garbage"]]}, function() { |
|
16 // we need to be sure that all location data has been purged/set. |
|
17 sleep(1000); |
|
18 callback.call(); |
|
19 }); |
|
20 } |
|
21 |
|
22 function stop_sending_garbage(callback) |
|
23 { |
|
24 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + ""]]}, function() { |
|
25 // we need to be sure that all location data has been purged/set. |
|
26 sleep(1000); |
|
27 callback.call(); |
|
28 }); |
|
29 } |
|
30 |
|
31 function stop_geolocationProvider(callback) |
|
32 { |
|
33 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=stop-responding"]]}, function() { |
|
34 // we need to be sure that all location data has been purged/set. |
|
35 sleep(1000); |
|
36 callback.call(); |
|
37 }); |
|
38 } |
|
39 |
|
40 function set_network_request_cache_enabled(enabled, callback) |
|
41 { |
|
42 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.debug.requestCache.enabled", enabled]]}, callback); |
|
43 } |
|
44 |
|
45 function worse_geolocationProvider(callback) |
|
46 { |
|
47 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=worse-accuracy"]]}, callback); |
|
48 } |
|
49 |
|
50 function resume_geolocationProvider(callback) |
|
51 { |
|
52 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + ""]]}, callback); |
|
53 } |
|
54 |
|
55 function delay_geolocationProvider(delay, callback) |
|
56 { |
|
57 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?delay=" + delay]]}, callback); |
|
58 } |
|
59 |
|
60 function send404_geolocationProvider(callback) |
|
61 { |
|
62 set_network_request_cache_enabled(false, function() { |
|
63 SpecialPowers.pushPrefEnv({"set": [["geo.wifi.uri", BASE_URL + "?action=send404"]]}, callback);}); |
|
64 } |
|
65 |
|
66 function check_geolocation(location) { |
|
67 |
|
68 ok(location, "Check to see if this location is non-null"); |
|
69 |
|
70 ok("timestamp" in location, "Check to see if there is a timestamp"); |
|
71 |
|
72 // eventually, coords may be optional (eg, when civic addresses are supported) |
|
73 ok("coords" in location, "Check to see if this location has a coords"); |
|
74 |
|
75 var coords = location.coords; |
|
76 |
|
77 ok("latitude" in coords, "Check to see if there is a latitude"); |
|
78 ok("longitude" in coords, "Check to see if there is a longitude"); |
|
79 ok("accuracy" in coords, "Check to see if there is a accuracy"); |
|
80 |
|
81 // optional ok("altitude" in coords, "Check to see if there is a altitude"); |
|
82 // optional ok("altitudeAccuracy" in coords, "Check to see if there is a alt accuracy"); |
|
83 // optional ok("heading" in coords, "Check to see if there is a heading"); |
|
84 // optional ok("speed" in coords, "Check to see if there is a speed"); |
|
85 |
|
86 ok (Math.abs(location.coords.latitude - 37.41857) < 0.001, "lat matches known value"); |
|
87 ok (Math.abs(location.coords.longitude + 122.08769) < 0.001, "lon matches known value"); |
|
88 // optional ok(location.coords.altitude == 42, "alt matches known value"); |
|
89 // optional ok(location.coords.altitudeAccuracy == 42, "alt acc matches known value"); |
|
90 } |
|
91 |
|
92 function toggleGeolocationSetting(value, callback) { |
|
93 var mozSettings = window.navigator.mozSettings; |
|
94 var lock = mozSettings.createLock(); |
|
95 |
|
96 var geoenabled = {"geolocation.enabled": value}; |
|
97 |
|
98 req = lock.set(geoenabled); |
|
99 req.onsuccess = function () { |
|
100 ok(true, "set done"); |
|
101 callback(); |
|
102 } |
|
103 } |