summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/geolocation
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/tests/mochitest/geolocation
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/tests/mochitest/geolocation')
-rw-r--r--dom/tests/mochitest/geolocation/chrome.toml7
-rw-r--r--dom/tests/mochitest/geolocation/crossorigin_iframe.html33
-rw-r--r--dom/tests/mochitest/geolocation/file_featurePolicy.html47
-rw-r--r--dom/tests/mochitest/geolocation/geolocation.html13
-rw-r--r--dom/tests/mochitest/geolocation/geolocation_common.js154
-rw-r--r--dom/tests/mochitest/geolocation/mochitest.toml100
-rw-r--r--dom/tests/mochitest/geolocation/network_geolocation.sjs79
-rw-r--r--dom/tests/mochitest/geolocation/popup.html19
-rw-r--r--dom/tests/mochitest/geolocation/test_allowCurrent.html41
-rw-r--r--dom/tests/mochitest/geolocation/test_allowWatch.html44
-rw-r--r--dom/tests/mochitest/geolocation/test_cachedPosition.html83
-rw-r--r--dom/tests/mochitest/geolocation/test_cancelCurrent.html45
-rw-r--r--dom/tests/mochitest/geolocation/test_cancelWatch.html49
-rw-r--r--dom/tests/mochitest/geolocation/test_clearWatch.html76
-rw-r--r--dom/tests/mochitest/geolocation/test_clearWatchBeforeAllowing.html62
-rw-r--r--dom/tests/mochitest/geolocation/test_clearWatch_invalid.html47
-rw-r--r--dom/tests/mochitest/geolocation/test_crossorigin_iframe.html61
-rw-r--r--dom/tests/mochitest/geolocation/test_enableHighAccuracy.html73
-rw-r--r--dom/tests/mochitest/geolocation/test_errorcheck.html52
-rw-r--r--dom/tests/mochitest/geolocation/test_featurePolicy.html51
-rw-r--r--dom/tests/mochitest/geolocation/test_garbageWatch.html56
-rw-r--r--dom/tests/mochitest/geolocation/test_geoGetCurrentPositionBlockedInInsecureContext.html47
-rw-r--r--dom/tests/mochitest/geolocation/test_geoWatchPositionBlockedInInsecureContext.html51
-rw-r--r--dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off.html36
-rw-r--r--dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off_iframe.html28
-rw-r--r--dom/tests/mochitest/geolocation/test_handlerSpinsEventLoop.html70
-rw-r--r--dom/tests/mochitest/geolocation/test_hidden.html110
-rw-r--r--dom/tests/mochitest/geolocation/test_manyCurrentConcurrent.html60
-rw-r--r--dom/tests/mochitest/geolocation/test_manyCurrentSerial.html47
-rw-r--r--dom/tests/mochitest/geolocation/test_manyWatchConcurrent.html60
-rw-r--r--dom/tests/mochitest/geolocation/test_manyWatchSerial.html56
-rw-r--r--dom/tests/mochitest/geolocation/test_manyWindows.html67
-rw-r--r--dom/tests/mochitest/geolocation/test_native_provider.html70
-rw-r--r--dom/tests/mochitest/geolocation/test_not_fully_active.html93
-rw-r--r--dom/tests/mochitest/geolocation/test_optional_api_params.html126
-rw-r--r--dom/tests/mochitest/geolocation/test_shutdown.html63
-rw-r--r--dom/tests/mochitest/geolocation/test_timeoutCurrent.html56
-rw-r--r--dom/tests/mochitest/geolocation/test_timeoutWatch.html65
-rw-r--r--dom/tests/mochitest/geolocation/test_timerRestartWatch.html75
-rw-r--r--dom/tests/mochitest/geolocation/test_windowClose.html36
-rw-r--r--dom/tests/mochitest/geolocation/test_worseAccuracyDoesNotBlockCallback.html51
-rw-r--r--dom/tests/mochitest/geolocation/windowTest.html44
42 files changed, 2503 insertions, 0 deletions
diff --git a/dom/tests/mochitest/geolocation/chrome.toml b/dom/tests/mochitest/geolocation/chrome.toml
new file mode 100644
index 0000000000..e392ab203e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/chrome.toml
@@ -0,0 +1,7 @@
+[DEFAULT]
+support-files = [
+ "geolocation_common.js",
+ "network_geolocation.sjs",
+]
+
+["test_handlerSpinsEventLoop.html"]
diff --git a/dom/tests/mochitest/geolocation/crossorigin_iframe.html b/dom/tests/mochitest/geolocation/crossorigin_iframe.html
new file mode 100644
index 0000000000..f370003ad8
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/crossorigin_iframe.html
@@ -0,0 +1,33 @@
+<!DOCTYPE HTML>
+<html>
+<body>
+<pre id="test">
+<script type="application/javascript">
+/**
+ Runs inside iframe in test_crossorigin_iframe.html.
+*/
+
+const BASE_GEO_URL = "http://mochi.test:8888/tests/dom/tests/mochitest/geolocation/network_geolocation.sjs";
+var pushPrefs = (...p) => SpecialPowers.pushPrefEnv({set: p});
+
+(async () => {
+ await pushPrefs(["geo.provider.network.debug.requestCache.enabled", false],
+ ["geo.prompt.testing", true],
+ ["geo.prompt.testing.allow", true],
+ ["geo.provider.network.url", BASE_GEO_URL]);
+
+ navigator.geolocation.getCurrentPosition(() => {
+ navigator.geolocation.watchPosition(() => {
+ parent.postMessage("allowed", "*");
+ }, () => {
+ parent.postMessage("denied", "*");
+ });
+ }, () => {
+ parent.postMessage("denied", "*");
+ });
+
+})().catch(e => setTimeout(() => { throw e; }));
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/file_featurePolicy.html b/dom/tests/mochitest/geolocation/file_featurePolicy.html
new file mode 100644
index 0000000000..3028321721
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/file_featurePolicy.html
@@ -0,0 +1,47 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+</head>
+<body>
+<script class="testbody" type="text/javascript">
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test_currentPosition);
+});
+
+let tests = [];
+
+function test_currentPosition() {
+ navigator.geolocation.getCurrentPosition(() => {
+ tests.push("allowed");
+ test_watchPosition();
+ }, () => {
+ tests.push("denied");
+ test_watchPosition();
+ })
+}
+
+function test_watchPosition() {
+ navigator.geolocation.watchPosition(() => {
+ tests.push("allowed");
+ send_results();
+ }, () => {
+ tests.push("denied");
+ send_results();
+ });
+}
+
+function send_results() {
+ if (tests.length != 2 || tests[0] != tests[1]) {
+ parent.continueTest("error");
+ return;
+ }
+
+ parent.continueTest(tests[0]);
+}
+
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/geolocation.html b/dom/tests/mochitest/geolocation/geolocation.html
new file mode 100644
index 0000000000..e62f13e7d3
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/geolocation.html
@@ -0,0 +1,13 @@
+<html>
+ <head>
+ <title>Simple access of geolocation</title>
+ <head>
+ <script>
+ function loadedWindow() {
+ opener.postMessage("loaded", "*");
+ }
+ navigator.geolocation.getCurrentPosition(loadedWindow, loadedWindow, {timeout:30000});
+ </script>
+ </head>
+ <body></body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/geolocation_common.js b/dom/tests/mochitest/geolocation/geolocation_common.js
new file mode 100644
index 0000000000..2868215e76
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/geolocation_common.js
@@ -0,0 +1,154 @@
+"use strict";
+
+var harness =
+ SimpleTest.harnessParameters.testRoot == "chrome" ? "chrome" : "tests";
+var BASE_URL =
+ "http://mochi.test:8888/" +
+ harness +
+ "/dom/tests/mochitest/geolocation/network_geolocation.sjs";
+
+function set_geo_wifi_uri(uri, callback) {
+ // Disable NetworkGeolocationProvider.js request cache because the cache
+ // does not remember from which location service it came from. We expect
+ // different results when we change the provider URL (geo.provider.network.url).
+ set_network_request_cache_enabled(false, () => {
+ SpecialPowers.pushPrefEnv(
+ { set: [["geo.provider.network.url", uri]] },
+ callback
+ );
+ });
+}
+
+function sleep(delay) {
+ var start = Date.now();
+ while (Date.now() < start + delay) {}
+}
+
+function force_prompt(allow, callback) {
+ SpecialPowers.pushPrefEnv(
+ {
+ set: [
+ ["geo.prompt.testing", true],
+ ["geo.prompt.testing.allow", allow],
+ ],
+ },
+ callback
+ );
+}
+
+function start_sending_garbage(callback) {
+ set_geo_wifi_uri(BASE_URL + "?action=respond-garbage", () => {
+ // we need to be sure that all location data has been purged/set.
+ sleep(1000);
+ callback.call();
+ });
+}
+
+function stop_sending_garbage(callback) {
+ set_geo_wifi_uri(BASE_URL + "", () => {
+ // we need to be sure that all location data has been purged/set.
+ sleep(1000);
+ callback.call();
+ });
+}
+
+function stop_geolocationProvider(callback) {
+ set_geo_wifi_uri(BASE_URL + "?action=stop-responding", () => {
+ // we need to be sure that all location data has been purged/set.
+ sleep(1000);
+ callback.call();
+ });
+}
+
+function set_network_request_cache_enabled(enabled, callback) {
+ SpecialPowers.pushPrefEnv(
+ { set: [["geo.provider.network.debug.requestCache.enabled", enabled]] },
+ callback
+ );
+}
+
+function worse_geolocationProvider(callback) {
+ set_geo_wifi_uri(BASE_URL + "?action=worse-accuracy", callback);
+}
+
+function resume_geolocationProvider(callback) {
+ set_geo_wifi_uri(BASE_URL + "", callback);
+}
+
+function delay_geolocationProvider(delay, callback) {
+ set_geo_wifi_uri(BASE_URL + "?delay=" + delay, callback);
+}
+
+function send404_geolocationProvider(callback) {
+ set_geo_wifi_uri(BASE_URL + "?action=send404", callback);
+}
+
+function check_geolocation(location) {
+ ok(location, "Check to see if this location is non-null");
+
+ const timestamp = location.timestamp;
+ dump(`timestamp=${timestamp}\n`);
+ ok(IsNumber(timestamp), "check timestamp type");
+ ok(timestamp > 0, "check timestamp range");
+
+ // eventually, coords may be optional (eg, when civic addresses are supported)
+ ok("coords" in location, "Check to see if this location has a coords");
+
+ const {
+ latitude,
+ longitude,
+ accuracy,
+ altitude,
+ altitudeAccuracy,
+ speed,
+ heading,
+ } = location.coords;
+
+ dump(`latitude=${latitude}\n`);
+ dump(`longitude=${longitude}\n`);
+ dump(`accuracy=${accuracy}\n`);
+ dump(`altitude=${altitude}\n`);
+ dump(`altitudeAccuracy=${altitudeAccuracy}\n`);
+ dump(`speed=${speed}\n`);
+ dump(`heading=${heading}\n`);
+
+ ok(IsNumber(latitude), "check latitude type");
+ ok(IsNumber(longitude), "check longitude type");
+
+ ok(
+ Math.abs(latitude - 37.41857) < 0.001,
+ "latitude matches hard-coded value"
+ );
+ ok(
+ Math.abs(longitude + 122.08769) < 0.001,
+ "longitude matches hard-coded value"
+ );
+
+ ok(IsNonNegativeNumber(accuracy), "check accuracy type and range");
+ ok(IsNumber(altitude) || altitude === null, "check accuracy type");
+
+ ok(
+ (IsNonNegativeNumber(altitudeAccuracy) && IsNumber(altitude)) ||
+ altitudeAccuracy === null,
+ "check altitudeAccuracy type and range"
+ );
+
+ ok(
+ IsNonNegativeNumber(speed) || speed === null,
+ "check speed type and range"
+ );
+
+ ok(
+ (IsNonNegativeNumber(heading) && heading < 360 && speed > 0) ||
+ heading === null,
+ "check heading type and range"
+ );
+}
+
+function IsNumber(x) {
+ return typeof x === "number" && !Number.isNaN(x);
+}
+
+function IsNonNegativeNumber(x) {
+ return IsNumber(x) && x >= 0;
+}
diff --git a/dom/tests/mochitest/geolocation/mochitest.toml b/dom/tests/mochitest/geolocation/mochitest.toml
new file mode 100644
index 0000000000..28af26ceb3
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/mochitest.toml
@@ -0,0 +1,100 @@
+[DEFAULT]
+# TODO: Refactor skip-if via Bug 1857079
+tags = "geolocation condprof"
+scheme = "https"
+support-files = [
+ "geolocation.html",
+ "geolocation_common.js",
+ "network_geolocation.sjs",
+ "windowTest.html",
+ "popup.html",
+]
+prefs = [
+ "dom.security.featurePolicy.header.enabled=true",
+ "dom.security.featurePolicy.webidl.enabled=true",
+]
+
+["test_allowCurrent.html"]
+
+["test_allowWatch.html"]
+
+["test_cachedPosition.html"]
+
+["test_cancelCurrent.html"]
+
+["test_cancelWatch.html"]
+
+["test_clearWatch.html"]
+
+["test_clearWatchBeforeAllowing.html"]
+
+["test_clearWatch_invalid.html"]
+
+["test_crossorigin_iframe.html"]
+support-files = ["crossorigin_iframe.html"]
+
+["test_enableHighAccuracy.html"]
+
+["test_errorcheck.html"]
+
+["test_featurePolicy.html"]
+support-files = ["file_featurePolicy.html"]
+
+["test_geoGetCurrentPositionBlockedInInsecureContext.html"]
+scheme = "http" # This test REQUIRES to run on HTTP (_NOT_ HTTPS).
+skip-if = [
+ "http3",
+ "http2",
+]
+
+["test_geoWatchPositionBlockedInInsecureContext.html"]
+scheme = "http" # This test REQUIRES to run on HTTP (_NOT_ HTTPS).
+skip-if = [
+ "http3",
+ "http2",
+]
+
+["test_geolocation_is_undefined_when_pref_is_off.html"]
+support-files = ["test_geolocation_is_undefined_when_pref_is_off_iframe.html"]
+
+["test_hidden.html"]
+skip-if = [
+ "os == 'android'", # test uses popup windows
+ "condprof",
+] #: timed out
+support-files = ["popup.html"]
+
+["test_manyCurrentConcurrent.html"]
+skip-if = ["condprof"] #: timed out
+
+["test_manyCurrentSerial.html"]
+skip-if = [
+ "condprof", #: timed out
+]
+
+["test_manyWatchConcurrent.html"]
+skip-if = ["condprof"] #: timed out
+
+["test_manyWatchSerial.html"]
+
+["test_manyWindows.html"]
+
+["test_native_provider.html"]
+skip-if = [
+ "os != 'android'",
+]
+
+["test_not_fully_active.html"]
+support-files = ["popup.html"]
+
+["test_optional_api_params.html"]
+
+["test_shutdown.html"]
+
+["test_timeoutCurrent.html"]
+
+["test_timerRestartWatch.html"]
+
+["test_windowClose.html"]
+
+["test_worseAccuracyDoesNotBlockCallback.html"]
diff --git a/dom/tests/mochitest/geolocation/network_geolocation.sjs b/dom/tests/mochitest/geolocation/network_geolocation.sjs
new file mode 100644
index 0000000000..4074e57c1c
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/network_geolocation.sjs
@@ -0,0 +1,79 @@
+function parseQueryString(str) {
+ if (str == "") {
+ return {};
+ }
+
+ var paramArray = str.split("&");
+ var regex = /^([^=]+)=(.*)$/;
+ var params = {};
+ for (var i = 0, sz = paramArray.length; i < sz; i++) {
+ var match = regex.exec(paramArray[i]);
+ if (!match) {
+ throw "Bad parameter in queryString! '" + paramArray[i] + "'";
+ }
+ params[decodeURIComponent(match[1])] = decodeURIComponent(match[2]);
+ }
+
+ return params;
+}
+
+function getPosition(action) {
+ var response = {
+ status: "OK",
+ location: {
+ lat: 37.41857,
+ lng: -122.08769,
+ },
+ accuracy: action == "worse-accuracy" ? 100 : 42,
+ };
+
+ return JSON.stringify(response);
+}
+
+var timer;
+function handleRequest(request, response) {
+ var params = parseQueryString(request.queryString);
+
+ if (params.action == "stop-responding") {
+ response.processAsync();
+ return;
+ }
+
+ var position = getPosition(params.action);
+
+ if (params.action == "respond-garbage") {
+ // better way?
+ var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
+ position = "";
+ var len = Math.floor(Math.random() * 5000);
+
+ for (var i = 0; i < len; i++) {
+ var c = Math.floor(Math.random() * chars.length);
+ position += chars.substring(c, c + 1);
+ }
+ }
+
+ var response;
+ response.processAsync();
+ response.setStatusLine("1.0", 200, "OK");
+ response.setHeader("Cache-Control", "no-cache", false);
+ response.setHeader("Content-Type", "aplication/x-javascript", false);
+
+ var delay = 0;
+ if ("delay" in params) {
+ delay = params.delay;
+ }
+ if (params.action === "send404") {
+ response.setStatusLine("1.0", 404, "Not Found");
+ position = "";
+ }
+ timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+ timer.initWithCallback(
+ function () {
+ response.write(position);
+ response.finish();
+ },
+ delay,
+ timer.TYPE_ONE_SHOT
+ );
+}
diff --git a/dom/tests/mochitest/geolocation/popup.html b/dom/tests/mochitest/geolocation/popup.html
new file mode 100644
index 0000000000..b286f16ff3
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/popup.html
@@ -0,0 +1,19 @@
+<html>
+ <head>
+ <title>Simple access of geolocation</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script src="geolocation_common.js"></script>
+ <meta charset="utf-8">
+ <head>
+ <script>
+ async function loadedWindow() {
+ await new Promise(r => force_prompt(true, r));
+ opener.postMessage("initialized", "*");
+ }
+ navigator.geolocation.getCurrentPosition(loadedWindow, loadedWindow, {timeout:30000});
+ </script>
+ </head>
+ <body>
+ <h1>Just a support file</h1>
+ </body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_allowCurrent.html b/dom/tests/mochitest/geolocation/test_allowCurrent.html
new file mode 100644
index 0000000000..a0ebefdc60
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_allowCurrent.html
@@ -0,0 +1,41 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+
+function successCallback(position) {
+ check_geolocation(position);
+ SimpleTest.finish();
+}
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_allowWatch.html b/dom/tests/mochitest/geolocation/test_allowWatch.html
new file mode 100644
index 0000000000..782d5ee071
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_allowWatch.html
@@ -0,0 +1,44 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for watchPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var watchID;
+
+function successCallback(position) {
+ check_geolocation(position);
+ navigator.geolocation.clearWatch(watchID);
+ SimpleTest.finish();
+}
+
+function test1() {
+ watchID = navigator.geolocation.watchPosition(successCallback, null, null);
+ is(watchID, 1, "initially returns 1");
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_cachedPosition.html b/dom/tests/mochitest/geolocation/test_cachedPosition.html
new file mode 100644
index 0000000000..1422c3c3bb
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_cachedPosition.html
@@ -0,0 +1,83 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=850442
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=850442">Mozilla Bug 850442</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+/*
+The request cache vs. the PositionOptions cache is confusing to the reader, to explain:
+
+Testing uses mochitest httpd, so the network cache must be disabled in order for requests
+to propagate to mochitest httpd. Otherwise, every request looks like a geoip request,
+and the network request cache sees no reason to make a web request for location for geoip
+if it has already made geoip (or better) requests.
+
+We should investigate providing fake wifi and cell scans to the
+network location provider, then the network cache does not need to be shut off
+AND it will get testing coverage.
+
+*/
+resume_geolocationProvider(function() {
+ force_prompt(true, function () {
+ set_network_request_cache_enabled(false, test_cachedPosition)
+ });
+});
+
+function done() {
+ set_network_request_cache_enabled(true, function() {
+ resume_geolocationProvider(function() {
+ SimpleTest.finish();
+ });
+ });
+}
+
+function errorCallback(err) {
+ ok(false, "error callback should not have been called");
+ done();
+}
+
+function test_cachedPosition() {
+ var cached = null;
+ navigator.geolocation.getCurrentPosition(function(pos) {
+ // first call is just to warm up the cache
+ check_geolocation(pos);
+
+ navigator.geolocation.getCurrentPosition(function(pos) {
+ check_geolocation(pos);
+ cached = pos;
+
+ navigator.geolocation.getCurrentPosition(function(pos) {
+ check_geolocation(pos);
+ is(pos.timestamp, cached.timestamp, "position should be equal to cached position");
+ navigator.geolocation.getCurrentPosition(function(pos) {
+ // force new position, can't be the one we have
+ check_geolocation(pos);
+ isnot(pos.timestamp, cached.timestamp, "position should not be equal to cached position");
+ done();
+ }, errorCallback, {maximumAge: 0});
+ }, errorCallback, {maximumAge: 21600000});
+ }, errorCallback, {maximumAge: 21600000});
+ }, errorCallback, {maximumAge: 21600000});
+ }
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_cancelCurrent.html b/dom/tests/mochitest/geolocation/test_cancelCurrent.html
new file mode 100644
index 0000000000..a5357ac938
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_cancelCurrent.html
@@ -0,0 +1,45 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(false, test1);
+});
+
+function failureCallback(error) {
+ ok(error.code == error.PERMISSION_DENIED, "Ensure that the error was PERMISSION_DENIED");
+ SimpleTest.finish();
+}
+
+function successCallback(position){
+ ok(0, "Success was called when it shouldn't have been. major problem");
+ SimpleTest.finish();
+}
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback, failureCallback, null);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_cancelWatch.html b/dom/tests/mochitest/geolocation/test_cancelWatch.html
new file mode 100644
index 0000000000..18ca21ba18
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_cancelWatch.html
@@ -0,0 +1,49 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for watchPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(false, test1);
+});
+
+var watchID;
+
+function failureCallback(error) {
+ ok(error.code == error.PERMISSION_DENIED, "Ensure that the error was PERMISSION_DENIED");
+ SimpleTest.finish();
+}
+
+function successCallback(position){
+ ok(0, "Success was called when it shouldn't have been. major problem");
+ SimpleTest.finish();
+}
+
+function test1() {
+ watchID = navigator.geolocation.watchPosition(successCallback, failureCallback, null);
+}
+</script>
+</pre>
+</body>
+</html>
+
+
+
diff --git a/dom/tests/mochitest/geolocation/test_clearWatch.html b/dom/tests/mochitest/geolocation/test_clearWatch.html
new file mode 100644
index 0000000000..4c1776331e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_clearWatch.html
@@ -0,0 +1,76 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for watchPosition and clearWatch</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var hasBeenCleared = false;
+var successWasCalledAfterClear = false;
+var firstCallback = true;
+
+function failureCallback(error)
+{
+ ok(0, "we should not be seeing failures from this watchPosition");
+}
+
+function successCallback(position) {
+ ok(true, "successCallback was called, hasBeenCleared=" + hasBeenCleared +
+ ", successWasCalledAfterClear=" + successWasCalledAfterClear);
+ check_geolocation(position);
+ if (hasBeenCleared == true) {
+ successWasCalledAfterClear = true;
+ }
+ if (firstCallback) {
+ SimpleTest.executeSoon(clearWatch);
+ firstCallback = false;
+ }
+}
+
+let watchID;
+
+function clearWatch() {
+ ok(true, "clearWatch was called, hasBeenCleared=" + hasBeenCleared +
+ ", successWasCalledAfterClear=" + successWasCalledAfterClear);
+ navigator.geolocation.clearWatch(watchID);
+ hasBeenCleared = true;
+ SimpleTest.executeSoon(testAccepted);
+}
+
+function testAccepted() {
+ ok(true, "testAccepted was called, hasBeenCleared=" + hasBeenCleared +
+ ", successWasCalledAfterClear=" + successWasCalledAfterClear);
+ ok(!successWasCalledAfterClear, "The successCallback should not be called after clear");
+ SimpleTest.finish();
+}
+
+function test1() {
+ ok(true, "Getting the watchPosition");
+ watchID = navigator.geolocation.watchPosition(successCallback, failureCallback, null);
+ ok(true, "Waiting");
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_clearWatchBeforeAllowing.html b/dom/tests/mochitest/geolocation/test_clearWatchBeforeAllowing.html
new file mode 100644
index 0000000000..3ac82232b7
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_clearWatchBeforeAllowing.html
@@ -0,0 +1,62 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=886026
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank"
+href="https://bugzilla.mozilla.org/show_bug.cgi?id=886026">Mozilla Bug 886026</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+resume_geolocationProvider(function() {
+ force_prompt(true, run_test);
+});
+
+function run_test() {
+ var successCallbackCalled = false,
+ errorCallbackCalled = false;
+
+ var watchId = navigator.geolocation.watchPosition(
+ function(pos) {
+ check_geolocation(pos);
+ successCallbackCalled = true;
+ }, function(err) {
+ errorCallbackCalled = true;
+ }
+ );
+
+ navigator.geolocation.getCurrentPosition(
+ function(pos) {
+ check_geolocation(pos);
+ SimpleTest.executeSoon(function() {
+ ok(successCallbackCalled == false,
+ "getCurrentPosition : Success callback should not have been called");
+
+ ok(errorCallbackCalled == false,
+ "getCurrentPosition : Error callback should not have been called");
+
+ SimpleTest.finish();
+ });
+ }
+ );
+
+ navigator.geolocation.clearWatch(watchId);
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_clearWatch_invalid.html b/dom/tests/mochitest/geolocation/test_clearWatch_invalid.html
new file mode 100644
index 0000000000..fe2007481e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_clearWatch_invalid.html
@@ -0,0 +1,47 @@
+ <!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=463039
+-->
+<head>
+ <title>Test for Bug 463039</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=463039">Mozilla Bug 463039</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+function test1() {
+ navigator.geolocation.watchPosition(function(){});
+
+ // there are no watches, so this should always throw
+ for (x=-10; x<10; x++) {
+ navigator.geolocation.clearWatch(x);
+ ok(1, "clearWatch should not throw");
+ }
+
+ // lets try something huge
+ navigator.geolocation.clearWatch(Number.MAX_VALUE);
+ ok(1, "clearWatch should not throw");
+
+ SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_crossorigin_iframe.html b/dom/tests/mochitest/geolocation/test_crossorigin_iframe.html
new file mode 100644
index 0000000000..545ba518ae
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_crossorigin_iframe.html
@@ -0,0 +1,61 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for geolocation is disabled by default, and set
+ allow="geolocation" in iframe could enable geolcation</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+var tests = [
+ // default cross-origin permission is denied
+ [ null, "denied" ],
+ [ "geolocation", "allowed"],
+];
+
+function checkGeolocationResult(test) {
+ return new Promise(resolve => {
+ function onMessage(event) {
+ is(event.data, test[1], "Expected " + test[1] + " for " + test[0]);
+ window.removeEventListener("message", onMessage);
+ resolve();
+ }
+
+ window.addEventListener("message", onMessage);
+ });
+}
+
+async function nextTest() {
+ if (!tests.length) {
+ SimpleTest.finish();
+ return;
+ }
+
+ let test = tests.shift();
+
+ var iframe = document.createElement("iframe");
+ if (test[0]) {
+ iframe.allow = test[0];
+ }
+
+ let geolocationPromise = checkGeolocationResult(test);
+ iframe.src =
+ "https://example.net/tests/dom/tests/mochitest/geolocation/crossorigin_iframe.html";
+ document.body.appendChild(iframe);
+ await geolocationPromise;
+
+ document.body.removeChild(iframe);
+ SimpleTest.executeSoon(nextTest);
+}
+
+SpecialPowers.pushPrefEnv({"set": [
+ ["dom.security.featurePolicy.header.enabled", true],
+ ["dom.security.featurePolicy.webidl.enabled", true],
+]}).then(nextTest);
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_enableHighAccuracy.html b/dom/tests/mochitest/geolocation/test_enableHighAccuracy.html
new file mode 100644
index 0000000000..f139c15d01
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_enableHighAccuracy.html
@@ -0,0 +1,73 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1765835
+-->
+<head>
+ <title>Tests for watchPosition and getCurrentPosition with setHighAccuracy</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1765835">Mozilla Bug 1765835</a>
+<script class="testbody" type="text/javascript">
+function mockChromeScript() {
+ function enableHighAccuracy(subject, topic, data) {
+ sendAsyncMessage("setHighAccuracy", data == "true");
+ };
+ Services.obs.addObserver(enableHighAccuracy, "testing-geolocation-high-accuracy");
+
+ addMessageListener("cleanup", () => {
+ Services.obs.removeObserver(enableHighAccuracy, "testing-geolocation-high-accuracy");
+ sendAsyncMessage("done");
+ });
+
+ sendAsyncMessage("ready");
+}
+
+add_task(async function test_watchPosition() {
+ await SpecialPowers.pushPrefEnv({
+ set: [["geo.prompt.testing", true], ["geo.prompt.testing.allow", true]],
+ });
+
+ let chromeScript = SpecialPowers.loadChromeScript(mockChromeScript);
+ await chromeScript.promiseOneMessage("ready");
+
+ let id = navigator.geolocation.watchPosition(() => {}, () => {}, { enableHighAccuracy: true });
+ let highAccuracy = await chromeScript.promiseOneMessage("setHighAccuracy");
+ ok(highAccuracy, "enableHighAccuracy option should be enabled");
+ navigator.geolocation.clearWatch(id);
+
+ id = navigator.geolocation.watchPosition(() => {}, () => {}, { enableHighAccuracy: false });
+ highAccuracy = await chromeScript.promiseOneMessage("setHighAccuracy");
+ ok(!highAccuracy, "enableHighAccuracy option should be disabled");
+ navigator.geolocation.clearWatch(id);
+
+ chromeScript.sendAsyncMessage("cleanup");
+ await chromeScript.promiseOneMessage("done");
+ chromeScript.destroy();
+});
+
+add_task(async function test_getCurrentPosition() {
+ await SpecialPowers.pushPrefEnv({
+ set: [["geo.prompt.testing", true], ["geo.prompt.testing.allow", true]],
+ });
+
+ let chromeScript = SpecialPowers.loadChromeScript(mockChromeScript);
+ await chromeScript.promiseOneMessage("ready");
+
+ navigator.geolocation.getCurrentPosition(() => {}, () => {}, { enableHighAccuracy: true });
+ let highAccuracy = await chromeScript.promiseOneMessage("setHighAccuracy");
+ ok(highAccuracy, "enableHighAccuracy option should be enabled");
+
+ navigator.geolocation.getCurrentPosition(() => {}, () => {}, { enableHighAccuracy: false });
+ highAccuracy = await chromeScript.promiseOneMessage("setHighAccuracy");
+ ok(!highAccuracy, "enableHighAccuracy option should be disabled");
+
+ chromeScript.sendAsyncMessage("cleanup");
+ await chromeScript.promiseOneMessage("done");
+ chromeScript.destroy();
+});
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_errorcheck.html b/dom/tests/mochitest/geolocation/test_errorcheck.html
new file mode 100644
index 0000000000..7900aed1e2
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_errorcheck.html
@@ -0,0 +1,52 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=684722
+-->
+<head>
+ <title>Test for ErrorChecking </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=684722">Mozilla Bug 684722</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+function test1() {
+ send404_geolocationProvider(test2);
+}
+
+function errorCallback(error) {
+ // GeolocationPositionError has no interface object, so we can't get constants off that.
+ is(error.code, error.POSITION_UNAVAILABLE,
+ "Geolocation error handler fired");
+ is(error.POSITION_UNAVAILABLE, 2,
+ "Value of POSITION_UNAVAILABLE should be correct");
+ SimpleTest.finish();
+}
+
+function successCallback(position) {
+ test2();
+}
+
+function test2() {
+ navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_featurePolicy.html b/dom/tests/mochitest/geolocation/test_featurePolicy.html
new file mode 100644
index 0000000000..f603c7f8f2
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_featurePolicy.html
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <title>Test for geolocation + featurePolicy</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+var tests = [
+ [ "geolocation 'none'", "denied"],
+ [ "geolocation", "allowed"],
+ [ "geolocation 'src'", "allowed"],
+ [ "geolocation 'self'", "allowed"],
+ [ "geolocation *", "allowed"],
+ [ "geolocation http://random.net", "denied"],
+ [ null, "allowed" ],
+];
+
+function nextTest() {
+ if (!tests.length) {
+ SimpleTest.finish();
+ return;
+ }
+
+ let test = tests.shift();
+
+ var iframe = document.createElement("iframe");
+ if (test[0]) {
+ iframe.setAttribute("allow", test[0]);
+ }
+
+ window.continueTest = msg => {
+ delete window.continueTest;
+
+ is(msg, test[1], "Expected " + test[1] + " for " + test[0]);
+ document.body.removeChild(iframe);
+ SimpleTest.executeSoon(nextTest);
+ };
+
+ iframe.src = "file_featurePolicy.html";
+ document.body.appendChild(iframe);
+}
+
+nextTest();
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_garbageWatch.html b/dom/tests/mochitest/geolocation/test_garbageWatch.html
new file mode 100644
index 0000000000..65e2fc8ac6
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_garbageWatch.html
@@ -0,0 +1,56 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=482260
+-->
+<head>
+ <title>Test for garbage data returned from location provider </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=455327">Mozilla Bug 482260</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+/** Test for Bug **/
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+function test1() {
+ start_sending_garbage(test2);
+}
+
+function successCallback(pos){
+ ok(false, "success should have never been called.");
+ stop_sending_garbage(function() {SimpleTest.finish();});
+}
+
+function errorCallback(err) {
+ ok(err.code == err.TIMEOUT, "ensure error is a timeout.");
+ stop_sending_garbage(function() { SimpleTest.finish(); });
+}
+
+var options = {
+ maximumAge: 0,
+ timeout: 1000,
+};
+
+function test2() {
+ navigator.geolocation.watchPosition(successCallback,
+ errorCallback,
+ options);
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_geoGetCurrentPositionBlockedInInsecureContext.html b/dom/tests/mochitest/geolocation/test_geoGetCurrentPositionBlockedInInsecureContext.html
new file mode 100644
index 0000000000..55435f6f16
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_geoGetCurrentPositionBlockedInInsecureContext.html
@@ -0,0 +1,47 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1269531
+-->
+<head>
+ <title>Test for Bug 1269531</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+ <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1269531">Mozilla Bug 1269531</a>
+ <p id="display"></p>
+ <div id="content" style="display: none">
+ </div>
+ <pre id="test">
+ <script class="testbody" type="text/javascript">
+
+ SimpleTest.waitForExplicitFinish();
+
+ // The test succeeds if the error callback is called because we expect
+ // failure when a getCurrentPosition() request is submitted in a non
+ // secure context.
+ function successCallback(position) {
+ ok(false, "Success callback is not expected to be called");
+ SimpleTest.finish();
+ }
+
+ function errorCallback(error) {
+ ok(true, "Check for the error callback to be called for insecure requests");
+ is(error.code, error.PERMISSION_DENIED, "Check error code for insecure requests");
+ SimpleTest.finish();
+ }
+
+ // Insecure requests should be blocked, for that we enable the relevant pref.
+ SpecialPowers.pushPrefEnv({"set": [["geo.security.allowinsecure", false]]}, function() {
+ force_prompt(true, function() {
+ navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
+ })
+ });
+
+ </script>
+ </pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_geoWatchPositionBlockedInInsecureContext.html b/dom/tests/mochitest/geolocation/test_geoWatchPositionBlockedInInsecureContext.html
new file mode 100644
index 0000000000..60bad9f644
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_geoWatchPositionBlockedInInsecureContext.html
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1269531
+-->
+<head>
+ <title>Test for Bug 1269531</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+ <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1269531">Mozilla Bug 1269531</a>
+ <p id="display"></p>
+ <div id="content" style="display: none">
+ </div>
+ <pre id="test">
+ <script class="testbody" type="text/javascript">
+
+ SimpleTest.waitForExplicitFinish();
+
+ var wid;
+
+ // The test succeeds if the error callback is called because we expect
+ // failure when a watchPosition() request is submitted in a non
+ // secure context.
+ function successCallback(position) {
+ ok(false, "Success callback is not expected to be called");
+ navigator.geolocation.clearWatch(wid);
+ SimpleTest.finish();
+ }
+
+ function errorCallback(error) {
+ ok(true, "Check for the error callback to be called for insecure requests");
+ is(error.code, error.PERMISSION_DENIED, "Check error code for insecure requests");
+ navigator.geolocation.clearWatch(wid);
+ SimpleTest.finish();
+ }
+
+ // Insecure requests should be blocked, for that we enable the relevant pref.
+ SpecialPowers.pushPrefEnv({"set": [["geo.security.allowinsecure", false]]}, function() {
+ force_prompt(true, function() {
+ wid = navigator.geolocation.watchPosition(successCallback, errorCallback);
+ })
+ });
+
+ </script>
+ </pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off.html b/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off.html
new file mode 100644
index 0000000000..cfdc537a1b
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off.html
@@ -0,0 +1,36 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=884921
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank"
+href="https://bugzilla.mozilla.org/show_bug.cgi?id=884921">Mozilla Bug 884921</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+// This test loads in an iframe, to ensure that the navigator instance is
+// loaded with the correct value of the preference.
+SimpleTest.waitForExplicitFinish();
+
+SpecialPowers.pushPrefEnv({set: [["geo.enabled", false]]}, function() {
+ let iframe = document.createElement("iframe");
+ iframe.id = "f1";
+ iframe.src = "test_geolocation_is_undefined_when_pref_is_off_iframe.html";
+ document.body.appendChild(iframe);
+});
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off_iframe.html b/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off_iframe.html
new file mode 100644
index 0000000000..1be69a2212
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_geolocation_is_undefined_when_pref_is_off_iframe.html
@@ -0,0 +1,28 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=884921
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank"
+href="https://bugzilla.mozilla.org/show_bug.cgi?id=884921">Mozilla Bug 884921</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+ window.parent.is(navigator.geolocation, undefined);
+ window.parent.is("geolocation" in navigator, false);
+ window.parent.SimpleTest.finish();
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_handlerSpinsEventLoop.html b/dom/tests/mochitest/geolocation/test_handlerSpinsEventLoop.html
new file mode 100644
index 0000000000..99ae586c4e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_handlerSpinsEventLoop.html
@@ -0,0 +1,70 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=911595
+-->
+<head>
+ <title>Test for spinning the event loop inside position handlers</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=911595 ">Mozilla Bug 911595</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+/*
+ * In bug 911595 , spinning the event loop from inside position
+ * handlers could cause both success and error callbacks to be
+ * fired for the same request if that request has a small timeout.
+ */
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var successCallbackCalled = false;
+function successCallback(position) {
+ successCallbackCalled = true;
+ check_geolocation(position);
+ while (!timeoutPassed) {
+ SpecialPowers.spinEventLoop(window);
+ }
+ info("TEST-INFO | successCallback called");
+ check();
+}
+
+var errorCallbackCalled = false;
+function errorCallback(error) {
+ errorCallbackCalled = true;
+ info("TEST-INFO | errorCallback called");
+ check();
+}
+
+function check() {
+ ok(successCallbackCalled != errorCallbackCalled, "Ensure only one callback is called");
+ SimpleTest.finish();
+}
+
+var timeoutPassed = false;
+var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
+function test1() {
+ SpecialPowers.pushPrefEnv({"set": [["geo.provider.network.timeToWaitBeforeSending", 10]]}, function() {
+ navigator.geolocation.getCurrentPosition(successCallback, errorCallback, {timeout: 500});
+ timer.initWithCallback(timer => {
+ timeoutPassed = true;
+ }, 600, Ci.nsITimer.TYPE_ONE_SHOT);
+ });
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_hidden.html b/dom/tests/mochitest/geolocation/test_hidden.html
new file mode 100644
index 0000000000..25f7f80d43
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_hidden.html
@@ -0,0 +1,110 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1653549
+-->
+<meta charset="utf-8">
+<title>Test that geolocation position can't be gotten when document is hidden</title>
+<script src="/tests/SimpleTest/SimpleTest.js"></script>
+<script src="geolocation_common.js"></script>
+<link rel="stylesheet" href="/tests/SimpleTest/test.css" />
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1653549">Mozilla Bug 1653549</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+</div>
+<pre id="test">
+
+<script class="testbody">
+SimpleTest.waitForExplicitFinish();
+
+// Little promise wrapper helper.
+function p(f) {
+ return new Promise((r) => f(r));
+}
+
+resume_geolocationProvider(async () => {
+ // Initialize
+ await new Promise((r) => force_prompt(true, r));
+ const popupWindow = window.open("popup.html");
+ popupWindow.opener = window;
+ await new Promise((r) =>
+ window.addEventListener("message", r, { once: true })
+ );
+
+ // Confirm everything is working ok...
+ const geo = popupWindow.navigator.geolocation;
+ await new Promise((resolve, reject) => {
+ geo.getCurrentPosition(resolve, reject);
+ });
+
+ // Hide the document...
+ const hiddenPromise = new Promise(
+ (r) => (popupWindow.document.onvisibilitychange = r)
+ );
+ await SimpleTest.promiseFocus(window);
+ await hiddenPromise;
+
+ // The following promises only resolve successfully when document is visible,
+ // meaning that position updates are ignored when the document is hidden.
+ let success = false;
+ let watchId = null;
+ const watchPositionPromise = new Promise((resolve) => {
+ watchId = geo.watchPosition(
+ () => {
+ ok(success, "watchPosition was called.");
+ if (!success) {
+ throw new Error("watchPosition was called too early");
+ }
+ resolve();
+ },
+ () => {
+ ok(false, "Error callback of watchPosition must not be called.");
+ }
+ );
+ });
+
+ const currentPositionPromise = new Promise((resolve) => {
+ geo.getCurrentPosition(
+ () => {
+ ok(success, "getCurrentPosition was called.");
+ if (!success){
+ throw new Error("getCurrentPosition was called too early");
+ }
+ resolve();
+ },
+ () => {
+ ok(false, "Error callback of getCurrentPosition must not be called.");
+ }
+ );
+ });
+
+ // Send data to be ignored...
+ await p(start_sending_garbage);
+ await p(stop_sending_garbage);
+ await p(resume_geolocationProvider);
+
+ // Refocus popup window...
+ const visiblePopupPromise = new Promise(
+ (r) => (popupWindow.document.onvisibilitychange = r)
+ );
+ await SimpleTest.promiseFocus(popupWindow);
+ await visiblePopupPromise;
+
+ // Resuming the geolocation events must now cause the promises to resolve correctly (with success = true).
+ success = true;
+ await p(resume_geolocationProvider);
+ await Promise.all([currentPositionPromise, watchPositionPromise]);
+
+ // Cleanup and finish!
+ geo.clearWatch(watchId);
+ await SimpleTest.promiseFocus(window);
+ popupWindow.close();
+ SimpleTest.finish();
+});
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_manyCurrentConcurrent.html b/dom/tests/mochitest/geolocation/test_manyCurrentConcurrent.html
new file mode 100644
index 0000000000..b70bf09633
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_manyCurrentConcurrent.html
@@ -0,0 +1,60 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=482260
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=482260">Mozilla Bug 482260</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var successCallbackCalled = 0;
+var failureCallbackCalled = 0;
+const totalResults = 100;
+
+function successCallback(position) {
+ check_geolocation(position);
+ successCallbackCalled++;
+ testPassed();
+}
+
+function failureCallback(code) {
+ failureCallbackCalled++;
+ testPassed();
+}
+
+function test1() {
+ for (var x = 0; x < totalResults; x++)
+ navigator.geolocation.getCurrentPosition(successCallback, failureCallback);
+}
+
+function testPassed() {
+ if (successCallbackCalled + failureCallbackCalled != totalResults)
+ return;
+ is(failureCallbackCalled, 0, "no failure callbacks should have been received");
+ SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_manyCurrentSerial.html b/dom/tests/mochitest/geolocation/test_manyCurrentSerial.html
new file mode 100644
index 0000000000..9afb62e52e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_manyCurrentSerial.html
@@ -0,0 +1,47 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=482260
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=482260">Mozilla Bug 482260</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var keepGoing = 10;
+
+function successCallback(position) {
+ if (keepGoing-- > 0) {
+ setTimeout(function() {navigator.geolocation.getCurrentPosition(successCallback);}, 0);
+ return;
+ }
+
+ ok(1, "100 successful calls");
+ SimpleTest.finish();
+}
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_manyWatchConcurrent.html b/dom/tests/mochitest/geolocation/test_manyWatchConcurrent.html
new file mode 100644
index 0000000000..8d22e442cb
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_manyWatchConcurrent.html
@@ -0,0 +1,60 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=482260
+-->
+<head>
+ <title>Test for watchPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=482260">Mozilla Bug 482260</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var successCallbackCalled = 0;
+var failureCallbackCalled = 0;
+const totalResults = 100;
+
+function test1() {
+ for (var x = 0; x < totalResults; x++) {
+ let watchID = navigator.geolocation.watchPosition(
+ position => {
+ check_geolocation(position);
+ successCallbackCalled++;
+ watchCallback(watchID);
+ },
+ error => {
+ failureCallbackCalled++;
+ watchCallback(watchID);
+ }
+ );
+ }
+}
+
+function watchCallback(watchID) {
+ navigator.geolocation.clearWatch(watchID);
+ if (successCallbackCalled + failureCallbackCalled != totalResults)
+ return;
+ is(failureCallbackCalled, 0, "no failure callbacks should have been received");
+ SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_manyWatchSerial.html b/dom/tests/mochitest/geolocation/test_manyWatchSerial.html
new file mode 100644
index 0000000000..bb27651d56
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_manyWatchSerial.html
@@ -0,0 +1,56 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=482260
+-->
+<head>
+ <title>Test for watchPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=482260">Mozilla Bug 482260</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var watchID = 0;
+var completeCount = 10;
+
+
+function successCallback(position) {
+ check_geolocation(position);
+
+ navigator.geolocation.clearWatch(watchID);
+
+ completeCount--;
+
+ if (completeCount==0) {
+ ok(1, "all watchPosition successCallbacks called");
+ SimpleTest.finish();
+ return;
+ }
+
+ watchID = navigator.geolocation.watchPosition(successCallback);
+}
+
+function test1() {
+ watchID = navigator.geolocation.watchPosition(successCallback);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_manyWindows.html b/dom/tests/mochitest/geolocation/test_manyWindows.html
new file mode 100644
index 0000000000..5e6e74c61f
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_manyWindows.html
@@ -0,0 +1,67 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=478911
+-->
+<head>
+ <title>Test for many windows </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank"
+href="https://bugzilla.mozilla.org/show_bug.cgi?id=478911">Crash in Multiple Windows</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+// ensure we are using the right testing provider
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+/** Test for Bug **/
+
+var numberOfWindows = 5; // 20 seems to be the default max popups during the mochitest run
+var loadedWindows = 0;
+
+var windows = new Array(numberOfWindows);
+
+addEventListener("message", function() {
+ ++loadedWindows;
+ if (loadedWindows == numberOfWindows) {
+ SimpleTest.executeSoon(closeWindows);
+ }
+}, false);
+
+function test1() {
+ for(var i = 0; i < numberOfWindows; i++) {
+ windows[i] = window.open("geolocation.html", "_blank", "width=700,height=400");
+ }
+}
+
+function closeWindows()
+{
+ for(var i = 0; i < numberOfWindows; i++) {
+ windows[i].close();
+ }
+ SimpleTest.waitForFocus(done);
+}
+
+SimpleTest.waitForExplicitFinish();
+
+function done()
+{
+ ok(navigator.geolocation, "Opened a bunch of windows and didn't crash.");
+ SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_native_provider.html b/dom/tests/mochitest/geolocation/test_native_provider.html
new file mode 100644
index 0000000000..e0c26c611a
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_native_provider.html
@@ -0,0 +1,70 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1596164
+https://bugzilla.mozilla.org/show_bug.cgi?id=1765835
+-->
+<head>
+ <title>Test for getCurrentPosition with native location provider</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1596164">Mozilla Bug 1596164</a>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1765835">Mozilla Bug 1765835</a>
+<script type="text/javascript">
+"use strict";
+
+add_task(async function test_maximumAge() {
+ await SpecialPowers.pushPrefEnv({
+ set: [["geo.provider.testing", false]],
+ });
+
+ await new Promise(resolve => {
+ navigator.geolocation.getCurrentPosition(() => {
+ ok(true, "can get position");
+ resolve();
+ }, () => {
+ ok(false, "error callback should not have been called");
+ resolve();
+ },
+ { maximumAge: 10000 });
+ });
+});
+
+add_task(async function test_highAccuracy() {
+ await SpecialPowers.pushPrefEnv({
+ set: [["geo.provider.testing", false]],
+ });
+
+ const lowAccuracy = await new Promise(resolve => {
+ navigator.geolocation.getCurrentPosition((pos) => {
+ resolve(pos.coords.accuracy);
+ }, () => {
+ ok(false, "error callback should not have been called on low accuracy call");
+ resolve();
+ },
+ { enableHighAccuracy: false});
+ });
+
+ const highAccuracy = await new Promise(resolve => {
+ navigator.geolocation.getCurrentPosition((pos) => {
+ resolve(pos.coords.accuracy);
+ }, () => {
+ ok(false, "error callback should not have been called on high accuracy call");
+ resolve();
+ },
+ { enableHighAccuracy: true});
+ });
+
+ // Low accuracy can sometimes be the same as high accuracy
+ // if a location provider recently provided a value
+ if(highAccuracy >= lowAccuracy ){
+ ok(true, "accuracy is correct");
+ } else {
+ ok(false, "lower accuracy calls should not out perform high accuracy calls");
+ }
+});
+</script>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_not_fully_active.html b/dom/tests/mochitest/geolocation/test_not_fully_active.html
new file mode 100644
index 0000000000..6fc7689c8c
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_not_fully_active.html
@@ -0,0 +1,93 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>
+ Test for when geolocation is used on non fully active documents
+ </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script src="geolocation_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+ </head>
+ <body>
+ <script>
+ SimpleTest.waitForExplicitFinish();
+
+ async function runTest() {
+ // Create the iframe, wait for it to load...
+ const iframe = document.createElement("iframe");
+
+ // We rely on this popup.html to acquire prompt privileges.
+ iframe.src = "popup.html";
+ document.body.appendChild(iframe);
+ iframe.contentWindow.opener = window;
+ await new Promise(r => window.addEventListener("message", r, { once: true }));
+
+ // Steal geolocation.
+ const geo = iframe.contentWindow.navigator.geolocation;
+
+ // No longer fully active.
+ iframe.remove();
+
+ // Try to watch a position while not fully active...
+ const watchError = await new Promise((resolve, reject) => {
+ const result = geo.watchPosition(
+ reject, // We don't want a position
+ resolve // We want an error!
+ );
+ is(result, 0, "watchPosition returns 0 on non-fully-active document");
+ });
+ is(
+ watchError.code,
+ GeolocationPositionError.POSITION_UNAVAILABLE,
+ "watchPosition returns an error on non-fully-active document"
+ );
+
+ // Now try to get current position while not fully active...
+ const positionError = await new Promise((resolve, reject) => {
+ const result = geo.getCurrentPosition(
+ reject, // We don't want a position
+ resolve // We want an error!
+ );
+ });
+ is(
+ positionError.code,
+ GeolocationPositionError.POSITION_UNAVAILABLE,
+ "getCurrentPosition returns an error on non-fully-active document"
+ );
+
+ // Re-attach, and go back to fully active.
+ document.body.appendChild(iframe);
+ iframe.contentWindow.opener = window;
+ await new Promise(r => window.addEventListener("message", r, { once: true }));
+
+ // And we are back to fully active.
+ let watchId;
+ let position = await new Promise((resolve, reject) => {
+ watchId = iframe.contentWindow.navigator.geolocation.watchPosition(
+ resolve,
+ reject
+ );
+ });
+ ok(watchId > 0, "Expected anything greater than 0");
+ ok(position, "Expected a position");
+
+ // Finally, let's get the position from the reattached document.
+ position = await new Promise((resolve, reject) => {
+ iframe.contentWindow.navigator.geolocation.getCurrentPosition(
+ resolve,
+ reject
+ );
+ });
+ ok(position, "Expected a position");
+ iframe.contentWindow.navigator.geolocation.clearWatch(watchId);
+ iframe.remove();
+ }
+
+ resume_geolocationProvider(async () => {
+ await new Promise(r => force_prompt(true, r));
+ await runTest();
+ SimpleTest.finish();
+ });
+ </script>
+ </body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_optional_api_params.html b/dom/tests/mochitest/geolocation/test_optional_api_params.html
new file mode 100644
index 0000000000..4aeb488a64
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_optional_api_params.html
@@ -0,0 +1,126 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=452566
+-->
+<head>
+ <title>Test for Bug 452566</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+ <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=452566">Mozilla Bug 452566</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+// ensure we are using the right testing provider
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+/** Test for Bug 452566 **/
+function test1() {
+ ok(navigator.geolocation, "Should have geolocation");
+
+ var exception = null;
+ try {
+ navigator.geolocation.getCurrentPosition();
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(exception, "Should have got an exception");
+
+ exception = null;
+ try {
+ navigator.geolocation.getCurrentPosition(function() {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ exception = null;
+ try {
+ navigator.geolocation.getCurrentPosition(function() {}, function() {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ exception = null;
+ try {
+ navigator.geolocation.getCurrentPosition(function() {}, function() {}, {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ exception = null;
+ try {
+ navigator.geolocation.watchPosition();
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(exception, "Should have got an exception");
+
+ exception = null;
+ try {
+ navigator.geolocation.watchPosition(function() {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ exception = null;
+ try {
+ navigator.geolocation.watchPosition(function() {}, function() {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ exception = null;
+ try {
+ navigator.geolocation.watchPosition(function() {}, function() {}, {});
+ } catch (ex) {
+ if (!(ex instanceof TypeError)) {
+ throw ex;
+ }
+ exception = ex;
+ }
+ ok(!exception, exception);
+
+ SimpleTest.finish();
+}
+
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_shutdown.html b/dom/tests/mochitest/geolocation/test_shutdown.html
new file mode 100644
index 0000000000..4b9133ef9d
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_shutdown.html
@@ -0,0 +1,63 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=716127
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=716127">Mozilla Bug 716127</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+function successCallback(position) {
+ check_geolocation(position);
+
+ SpecialPowers.pushPrefEnv({'set': [['geo.timeout', 100]]}, function() {
+ delay_geolocationProvider(1000, function() {
+ force_prompt(true, function() {
+ navigator.geolocation.getCurrentPosition(success2, handle_error, {maximumAge: 0});
+ });
+ });
+
+ });
+}
+
+function errorCallback() {
+ ok(false, "unexpected error");
+ SimpleTest.finish();
+}
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
+}
+
+function success2(position) {
+ check_geolocation(position);
+ SimpleTest.finish();
+}
+
+function handle_error() {
+ ok(false, "geolocation provider should not have timed out");
+ SimpleTest.finish();
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_timeoutCurrent.html b/dom/tests/mochitest/geolocation/test_timeoutCurrent.html
new file mode 100644
index 0000000000..283f26b1fe
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_timeoutCurrent.html
@@ -0,0 +1,56 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=858827
+-->
+<head>
+ <title>Test for timeout option </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=858827">Mozilla Bug 858827</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+
+function successCallback(position) {
+ ok(false, "we should not be seeing successCallback here.");
+ SimpleTest.finish();
+}
+
+function errorCallback(err) {
+ ok(err.code == err.TIMEOUT, "ensure error is a timeout.");
+ resume_geolocationProvider(function() {
+ SimpleTest.finish();
+ });
+}
+
+
+var options = {
+ maximumAge: 0,
+ timeout: 0
+};
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback,
+ errorCallback,
+ options);
+}
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_timeoutWatch.html b/dom/tests/mochitest/geolocation/test_timeoutWatch.html
new file mode 100644
index 0000000000..324886a4e7
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_timeoutWatch.html
@@ -0,0 +1,65 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=455327
+-->
+<head>
+ <title>Test for timeout option </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=455327">Mozilla Bug 478911</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+/** Test for Bug **/
+
+// ensure we are using the right testing provider
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+SimpleTest.waitForExplicitFinish();
+
+function test1() {
+ stop_geolocationProvider(test2);
+}
+
+function successCallback(pos){
+ ok(false, "success should have never been called.");
+ resume_geolocationProvider(function() {
+ SimpleTest.finish();
+ });
+}
+
+function errorCallback(err) {
+ if (err.code == err.POSITION_UNAVAILABLE)
+ ok(false, "nothing is hooked up to test against.");
+ else
+ ok(err.code == err.TIMEOUT, "ensure error is a timeout.");
+ resume_geolocationProvider(function() {
+ SimpleTest.finish();
+ });
+}
+
+
+var options = {
+ maximumAge: 0,
+ timeout: 10
+};
+
+function test2() {
+ navigator.geolocation.watchPosition(successCallback,
+ errorCallback,
+ options);
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_timerRestartWatch.html b/dom/tests/mochitest/geolocation/test_timerRestartWatch.html
new file mode 100644
index 0000000000..fe190951b7
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_timerRestartWatch.html
@@ -0,0 +1,75 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=526326
+-->
+<head>
+ <title>Test for watchPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=526326">Mozilla Bug 526326</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+// ensure we are using the right testing provider
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+var watchID;
+var times = 0;
+
+function errorCallback(err) {
+ ok(err.code === err.TIMEOUT ||
+ err.code === err.POSITION_UNAVAILABLE, `check error code: ${err.code}`);
+
+ times++;
+ isnot(times, 0, "times should be nonzero");
+ isnot(times, 4, "should not receive more than three error callbacks");
+
+ // make sure we got at least 3 times errorCallback
+ if (times >= 3) {
+ navigator.geolocation.clearWatch(watchID);
+ resume_geolocationProvider(function() {
+ set_network_request_cache_enabled(true,
+ function() { SimpleTest.finish(); } );
+ });
+ }
+}
+
+function successCallback(position) {
+ ok(1, "on success");
+ check_geolocation(position);
+
+ // Now that we got a success callback, let's try to ensure
+ // that we get a timeout error.
+ // The network cache is already off, now stop the sjs from reponding to requests
+ stop_geolocationProvider(function(){});
+}
+
+var options = {
+ maximumAge: 0,
+ timeout: 1000
+};
+
+function test1() {
+ set_network_request_cache_enabled(false,
+ function() {
+ watchID = navigator.geolocation.watchPosition(successCallback, errorCallback, options);
+ });
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/test_windowClose.html b/dom/tests/mochitest/geolocation/test_windowClose.html
new file mode 100644
index 0000000000..4ff858e8bb
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_windowClose.html
@@ -0,0 +1,36 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=493615
+-->
+<head>
+ <title>Test for geolocation in chrome </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=493615">Mozilla Bug 493615</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+function done() {
+ ok(1, "no crash, so pass.");
+ SimpleTest.finish();
+}
+
+SimpleTest.waitForExplicitFinish();
+force_prompt(true, function() {
+ window.open("windowTest.html");
+});
+
+</script>
+</pre>
+</body>
+</html>
+
diff --git a/dom/tests/mochitest/geolocation/test_worseAccuracyDoesNotBlockCallback.html b/dom/tests/mochitest/geolocation/test_worseAccuracyDoesNotBlockCallback.html
new file mode 100644
index 0000000000..76d60ac97b
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/test_worseAccuracyDoesNotBlockCallback.html
@@ -0,0 +1,51 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=494924
+-->
+<head>
+ <title>Test for getCurrentPosition </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=494924">Mozilla Bug 494924</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+SimpleTest.waitForExplicitFinish();
+
+resume_geolocationProvider(function() {
+ force_prompt(true, test1);
+});
+
+// The magic numbers 100 and 42 below are the accuracy results returned from the
+// mock geolocation service and defined in network_geolocation.sjs.
+function successCallback2(position) {
+ check_geolocation(position);
+ is(position.coords.accuracy, 100, "worse accuracy");
+ SimpleTest.finish();
+}
+
+function successCallback1(position) {
+ check_geolocation(position);
+ is(position.coords.accuracy, 42, "high accuracy");
+ worse_geolocationProvider(function() {
+ navigator.geolocation.getCurrentPosition(successCallback2, null, { maximumAge: 0 });
+ });
+}
+
+function test1() {
+ navigator.geolocation.getCurrentPosition(successCallback1, null, { maximumAge: 0 });
+}
+</script>
+</pre>
+</body>
+</html>
diff --git a/dom/tests/mochitest/geolocation/windowTest.html b/dom/tests/mochitest/geolocation/windowTest.html
new file mode 100644
index 0000000000..0366ffbe1e
--- /dev/null
+++ b/dom/tests/mochitest/geolocation/windowTest.html
@@ -0,0 +1,44 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=493615
+-->
+<head>
+ <title>Test for closing a window while it is doing a geolocation request </title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="text/javascript" src="geolocation_common.js"></script>
+
+<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=493615">Mozilla Bug 493615</a>
+<p id="display"></p>
+<div id="content" style="display: none">
+
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+"use strict";
+
+var finished = false;
+
+function successCallback(position) {
+ check_geolocation(position);
+
+ // watchPosition can notify more than once.
+ if (finished) {
+ return;
+ }
+ finished = true;
+
+ var opener = window.opener;
+ window.close();
+ opener.done();
+}
+
+navigator.geolocation.watchPosition(successCallback, null, null);
+</script>
+</pre>
+</body>
+</html>
+