1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/system/gonk/tests/marionette/test_geolocation.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +MARIONETTE_TIMEOUT = 10000; 1.8 + 1.9 +let geolocation = window.navigator.geolocation; 1.10 +ok(geolocation); 1.11 + 1.12 +var sample = []; 1.13 +var result = []; 1.14 +var wpid; 1.15 + 1.16 +/** 1.17 + * Grant special power to get the geolocation 1.18 + */ 1.19 +SpecialPowers.addPermission("geolocation", true, document); 1.20 + 1.21 +/** 1.22 + * Disable wifi geolocation provider 1.23 + */ 1.24 +wifiUri = SpecialPowers.getCharPref("geo.wifi.uri"); 1.25 +SpecialPowers.setCharPref("geo.wifi.uri", "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs?action=stop-responding"); 1.26 + 1.27 +/** 1.28 + * Helper that compares the geolocation against the web API. 1.29 + */ 1.30 +function verifyLocation() { 1.31 + 1.32 + log("Sample:" + sample.join(',')); 1.33 + log("Result:" + result.join(',')); 1.34 + 1.35 + for (i in sample) { 1.36 + is(sample.pop(), result.pop()); 1.37 + } 1.38 + 1.39 + window.setTimeout(cleanup, 0); 1.40 +} 1.41 + 1.42 +/** 1.43 + * Test story begins here. 1.44 + */ 1.45 +function setup() { 1.46 + log("Providing initial setup: set geographic position watcher."); 1.47 + 1.48 + 1.49 + wpid = geolocation.watchPosition(function(position) { 1.50 + log("Position changes: (" + position.coords.latitude + "/" + position.coords.longitude + ")"); 1.51 + result.push(""+position.coords.latitude + "/" + position.coords.longitude); 1.52 + }); 1.53 + 1.54 + lat = 0; 1.55 + lon = 0; 1.56 + 1.57 + cmd = "geo fix " + lon + " " + lat; 1.58 + sample.push(lat+"/"+lon); 1.59 + 1.60 + runEmulatorCmd(cmd, function(result) { 1.61 + window.setTimeout(movePosition_1, 0); 1.62 + }); 1.63 +} 1.64 + 1.65 +function movePosition_1() { 1.66 + log("Geolocation changes. Move to Position 1."); 1.67 + 1.68 + lat = 25; 1.69 + lon = 121.56499833333334; 1.70 + 1.71 + cmd = "geo fix " + lon + " " + lat; 1.72 + sample.push(lat+"/"+lon); 1.73 + 1.74 + runEmulatorCmd(cmd, function(result) { 1.75 + window.setTimeout(movePosition_2, 0); 1.76 + }); 1.77 +} 1.78 + 1.79 +function movePosition_2() { 1.80 + log("Geolocation changes to a negative longitude. Move to Position 2."); 1.81 + 1.82 + lat = 37.393; 1.83 + lon = -122.08199833333335; 1.84 + 1.85 + cmd = "geo fix " + lon + " " + lat; 1.86 + sample.push(lat+"/"+lon); 1.87 + 1.88 + runEmulatorCmd(cmd, function(result) { 1.89 + window.setTimeout(movePosition_3, 0); 1.90 + }); 1.91 +} 1.92 + 1.93 +function movePosition_3() { 1.94 + log("Geolocation changes with WatchPosition. Move to Position 3."); 1.95 + 1.96 + lat = -22; 1.97 + lon = -43; 1.98 + 1.99 + cmd = "geo fix " + lon + " " + lat; 1.100 + sample.push(lat+"/"+lon); 1.101 + 1.102 + geolocation.getCurrentPosition(function(position) { 1.103 + log("getCurrentPosition: Expected location: ("+lat+"/"+lon+"); Current location: (" + position.coords.latitude + "/" + position.coords.longitude + ")"); 1.104 + is(lat, position.coords.latitude); 1.105 + is(lon, position.coords.longitude); 1.106 + }); 1.107 + 1.108 + runEmulatorCmd(cmd, function(result) { 1.109 + window.setTimeout(verifyLocation, 0); 1.110 + }); 1.111 +} 1.112 + 1.113 +function cleanup() { 1.114 + geolocation.clearWatch(wpid); 1.115 + SpecialPowers.removePermission("geolocation", document); 1.116 + SpecialPowers.setCharPref("geo.wifi.uri", wifiUri); 1.117 + finish(); 1.118 +} 1.119 + 1.120 +setup();