summaryrefslogtreecommitdiffstats
path: root/dom/tests/unit
diff options
context:
space:
mode:
Diffstat (limited to 'dom/tests/unit')
-rw-r--r--dom/tests/unit/test_Fetch.js366
-rw-r--r--dom/tests/unit/test_PromiseDebugging.js21
-rw-r--r--dom/tests/unit/test_bug319968.js19
-rw-r--r--dom/tests/unit/test_bug465752.js26
-rw-r--r--dom/tests/unit/test_geolocation_monitor.js97
-rw-r--r--dom/tests/unit/test_geolocation_position_unavailable.js28
-rw-r--r--dom/tests/unit/test_geolocation_position_unavailable_wrap.js6
-rw-r--r--dom/tests/unit/test_geolocation_provider.js83
-rw-r--r--dom/tests/unit/test_geolocation_provider_timeout.js43
-rw-r--r--dom/tests/unit/test_geolocation_reset_accuracy.js93
-rw-r--r--dom/tests/unit/test_geolocation_reset_accuracy_wrap.js61
-rw-r--r--dom/tests/unit/test_geolocation_timeout.js59
-rw-r--r--dom/tests/unit/test_geolocation_timeout_wrap.js15
-rw-r--r--dom/tests/unit/test_xhr_init.js13
-rw-r--r--dom/tests/unit/xpcshell.ini24
15 files changed, 954 insertions, 0 deletions
diff --git a/dom/tests/unit/test_Fetch.js b/dom/tests/unit/test_Fetch.js
new file mode 100644
index 0000000000..c19f4e7ece
--- /dev/null
+++ b/dom/tests/unit/test_Fetch.js
@@ -0,0 +1,366 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+Cu.importGlobalProperties(["fetch"]);
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+const BinaryInputStream = Components.Constructor(
+ "@mozilla.org/binaryinputstream;1",
+ "nsIBinaryInputStream",
+ "setInputStream"
+);
+
+var server;
+
+function getBaseUrl() {
+ return "http://localhost:" + server.identity.primaryPort;
+}
+
+// a way to create some test defaults
+function createTestData(testPath) {
+ return {
+ testPath,
+ request: {
+ headers: {},
+ contentType: "application/json",
+ },
+ response: {
+ headers: {},
+ contentType: "application/json",
+ body: '{"Look": "Success!"}',
+ status: 200,
+ statusText: "OK",
+ },
+ };
+}
+
+// read body and content type information from a request
+function readDataFromRequest(aRequest) {
+ let requestData = {};
+ if (aRequest.method == "POST" || aRequest.method == "PUT") {
+ if (aRequest.bodyInputStream) {
+ let inputStream = new BinaryInputStream(aRequest.bodyInputStream);
+ let bytes = [];
+ let available;
+
+ while ((available = inputStream.available()) > 0) {
+ Array.prototype.push.apply(bytes, inputStream.readByteArray(available));
+ }
+
+ requestData.body = String.fromCharCode.apply(null, bytes);
+ requestData.contentType = aRequest.getHeader("Content-Type");
+ }
+ }
+ return requestData;
+}
+
+// write status information, content type information and body to a response
+function writeDataToResponse(aData, aResponse) {
+ aResponse.setStatusLine(null, aData.status, aData.statusText);
+ aResponse.setHeader("Content-Type", aData.contentType);
+ for (let header in aData.headers) {
+ aResponse.setHeader(header, aData.headers[header]);
+ }
+ aResponse.write(aData.body);
+}
+
+// test some GET functionality
+add_test(function test_GetData() {
+ do_test_pending();
+
+ let testData = createTestData("/getData");
+
+ // set up some headers to test
+ let headerNames = ["x-test-header", "x-other-test-header"];
+ for (let headerName of headerNames) {
+ testData.request.headers[headerName] = "test-value-for-" + headerName;
+ }
+
+ server.registerPathHandler(testData.testPath, function (aRequest, aResponse) {
+ try {
+ // check our request headers made it OK
+ for (let headerName of headerNames) {
+ Assert.equal(
+ testData.request.headers[headerName],
+ aRequest.getHeader(headerName)
+ );
+ }
+
+ // send a response
+ writeDataToResponse(testData.response, aResponse);
+ } catch (e) {
+ do_report_unexpected_exception(e);
+ }
+ });
+
+ // fetch, via GET, with some request headers set
+ fetch(getBaseUrl() + testData.testPath, { headers: testData.request.headers })
+ .then(function (response) {
+ // check response looks as expected
+ Assert.ok(response.ok);
+ Assert.equal(response.status, testData.response.status);
+ Assert.equal(response.statusText, testData.response.statusText);
+
+ // check a response header looks OK:
+ Assert.equal(
+ response.headers.get("Content-Type"),
+ testData.response.contentType
+ );
+
+ // ... and again to check header names are case insensitive
+ Assert.equal(
+ response.headers.get("content-type"),
+ testData.response.contentType
+ );
+
+ // ensure response.text() returns a promise that resolves appropriately
+ response.text().then(function (text) {
+ Assert.equal(text, testData.response.body);
+ do_test_finished();
+ run_next_test();
+ });
+ })
+ .catch(function (e) {
+ do_report_unexpected_exception(e);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+// test a GET with no init
+add_test(function test_GetDataNoInit() {
+ do_test_pending();
+
+ let testData = createTestData("/getData");
+
+ server.registerPathHandler(testData.testPath, function (aRequest, aResponse) {
+ try {
+ // send a response
+ writeDataToResponse(testData.response, aResponse);
+ } catch (e) {
+ do_report_unexpected_exception(e);
+ }
+ });
+
+ fetch(getBaseUrl() + testData.testPath, { headers: testData.request.headers })
+ .then(function (response) {
+ // check response looks as expected
+ Assert.ok(response.ok);
+ Assert.equal(response.status, testData.response.status);
+
+ // ensure response.text() returns a promise that resolves appropriately
+ response.text().then(function (text) {
+ Assert.equal(text, testData.response.body);
+ do_test_finished();
+ run_next_test();
+ });
+ })
+ .catch(function (e) {
+ do_report_unexpected_exception(e);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+// test some error responses
+add_test(function test_get40x() {
+ do_test_pending();
+
+ // prepare a response with some 40x error - a 404 will do
+ let notFoundData = createTestData("/getNotFound");
+ notFoundData.response.status = 404;
+ notFoundData.response.statusText = "Not found";
+ notFoundData.response.body = null;
+
+ // No need to register a path handler - httpd will return 404 anyway.
+ // Fetch, via GET, the resource that doesn't exist
+ fetch(getBaseUrl() + notFoundData.testPath).then(function (response) {
+ Assert.equal(response.status, 404);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+add_test(function test_get50x() {
+ do_test_pending();
+
+ // prepare a response with some server error - a 500 will do
+ let serverErrorData = createTestData("/serverError");
+ serverErrorData.response.status = 500;
+ serverErrorData.response.statusText = "The server broke";
+ serverErrorData.response.body = null;
+
+ server.registerPathHandler(
+ serverErrorData.testPath,
+ function (aRequest, aResponse) {
+ try {
+ // send the error response
+ writeDataToResponse(serverErrorData.response, aResponse);
+ } catch (e) {
+ do_report_unexpected_exception(e);
+ }
+ }
+ );
+
+ // fetch, via GET, the resource that creates a server error
+ fetch(getBaseUrl() + serverErrorData.testPath).then(function (response) {
+ Assert.equal(response.status, 500);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+// test a failure to connect
+add_test(function test_getTestFailedConnect() {
+ do_test_pending();
+ // try a server that's not there
+ fetch("http://localhost:4/should/fail")
+ .then(response => {
+ do_throw("Request should not succeed");
+ })
+ .catch(err => {
+ Assert.equal(true, err instanceof TypeError);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+add_test(function test_mozError() {
+ do_test_pending();
+ // try a server that's not there
+ fetch("http://localhost:4/should/fail", { mozErrors: true })
+ .then(response => {
+ do_throw("Request should not succeed");
+ })
+ .catch(err => {
+ Assert.equal(err.result, Cr.NS_ERROR_CONNECTION_REFUSED);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+add_test(function test_request_mozError() {
+ do_test_pending();
+ // try a server that's not there
+ const r = new Request("http://localhost:4/should/fail", { mozErrors: true });
+ fetch(r)
+ .then(response => {
+ do_throw("Request should not succeed");
+ })
+ .catch(err => {
+ Assert.equal(err.result, Cr.NS_ERROR_CONNECTION_REFUSED);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+// test POSTing some JSON data
+add_test(function test_PostJSONData() {
+ do_test_pending();
+
+ let testData = createTestData("/postJSONData");
+ testData.request.body = '{"foo": "bar"}';
+
+ server.registerPathHandler(testData.testPath, function (aRequest, aResponse) {
+ try {
+ let requestData = readDataFromRequest(aRequest);
+
+ // Check the request body is OK
+ Assert.equal(requestData.body, testData.request.body);
+
+ // Check the content type is as expected
+ Assert.equal(requestData.contentType, testData.request.contentType);
+
+ writeDataToResponse(testData.response, aResponse);
+ } catch (e) {
+ Assert.ok(false);
+ }
+ });
+
+ fetch(getBaseUrl() + testData.testPath, {
+ method: "POST",
+ body: testData.request.body,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ })
+ .then(function (aResponse) {
+ Assert.ok(aResponse.ok);
+ Assert.equal(aResponse.status, testData.response.status);
+ Assert.equal(aResponse.statusText, testData.response.statusText);
+
+ do_test_finished();
+ run_next_test();
+ })
+ .catch(function (e) {
+ do_report_unexpected_exception(e);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+// test POSTing some text
+add_test(function test_PostTextData() {
+ do_test_pending();
+
+ let testData = createTestData("/postTextData");
+ testData.request.body = "A plain text body";
+ testData.request.contentType = "text/plain";
+ let responseHeaderName = "some-response-header";
+ testData.response.headers[responseHeaderName] = "some header value";
+
+ server.registerPathHandler(testData.testPath, function (aRequest, aResponse) {
+ try {
+ let requestData = readDataFromRequest(aRequest);
+
+ // Check the request body is OK
+ Assert.equal(requestData.body, testData.request.body);
+
+ // Check the content type is as expected
+ Assert.equal(requestData.contentType, testData.request.contentType);
+
+ writeDataToResponse(testData.response, aResponse);
+ } catch (e) {
+ Assert.ok(false);
+ }
+ });
+
+ fetch(getBaseUrl() + testData.testPath, {
+ method: "POST",
+ body: testData.request.body,
+ headers: {
+ "Content-Type": testData.request.contentType,
+ },
+ })
+ .then(function (aResponse) {
+ Assert.ok(aResponse.ok);
+ Assert.equal(aResponse.status, testData.response.status);
+ Assert.equal(aResponse.statusText, testData.response.statusText);
+
+ // check the response header is set OK
+ Assert.equal(
+ aResponse.headers.get(responseHeaderName),
+ testData.response.headers[responseHeaderName]
+ );
+
+ do_test_finished();
+ run_next_test();
+ })
+ .catch(function (e) {
+ do_report_unexpected_exception(e);
+ do_test_finished();
+ run_next_test();
+ });
+});
+
+function run_test() {
+ // Set up an HTTP Server
+ server = new HttpServer();
+ server.start(-1);
+
+ run_next_test();
+
+ registerCleanupFunction(function () {
+ server.stop(function () {});
+ });
+}
diff --git a/dom/tests/unit/test_PromiseDebugging.js b/dom/tests/unit/test_PromiseDebugging.js
new file mode 100644
index 0000000000..2262d60d61
--- /dev/null
+++ b/dom/tests/unit/test_PromiseDebugging.js
@@ -0,0 +1,21 @@
+function run_test() {
+ // Hack around Promise.jsm being stuck on my global
+ Assert.equal(false, PromiseDebugging === undefined);
+ var res;
+ var p = new Promise(function (resolve, reject) {
+ res = resolve;
+ });
+ var state = PromiseDebugging.getState(p);
+ Assert.equal(state.state, "pending");
+
+ do_test_pending();
+
+ p.then(function () {
+ var state2 = PromiseDebugging.getState(p);
+ Assert.equal(state2.state, "fulfilled");
+ Assert.equal(state2.value, 5);
+ do_test_finished();
+ });
+
+ res(5);
+}
diff --git a/dom/tests/unit/test_bug319968.js b/dom/tests/unit/test_bug319968.js
new file mode 100644
index 0000000000..efde6843f0
--- /dev/null
+++ b/dom/tests/unit/test_bug319968.js
@@ -0,0 +1,19 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+function run_test() {
+ var domParser = new DOMParser();
+ var aDom = domParser.parseFromString(
+ "<root><feed><entry/><entry/></feed></root>",
+ "application/xml"
+ );
+ var feedList = aDom.getElementsByTagName("feed");
+ Assert.notEqual(feedList, null);
+ Assert.equal(feedList.length, 1);
+ Assert.notEqual(feedList[0], null);
+ Assert.equal(feedList[0].tagName, "feed");
+ var entry = feedList[0].getElementsByTagName("entry");
+ Assert.notEqual(entry, null);
+}
diff --git a/dom/tests/unit/test_bug465752.js b/dom/tests/unit/test_bug465752.js
new file mode 100644
index 0000000000..19c03a0e4d
--- /dev/null
+++ b/dom/tests/unit/test_bug465752.js
@@ -0,0 +1,26 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+function run_test() {
+ const str = "javascript:10";
+ var uri = Services.io.newURI(str);
+ var uri2 = Services.io.newURI(str);
+ const str2 = "http://example.org";
+ var uri3 = Services.io.newURI(str2);
+ Assert.ok(uri.equals(uri));
+ Assert.ok(uri.equals(uri2));
+ Assert.ok(uri2.equals(uri));
+ Assert.ok(uri2.equals(uri2));
+ Assert.ok(!uri3.equals(uri2));
+ Assert.ok(!uri2.equals(uri3));
+
+ var simple = Cc["@mozilla.org/network/simple-uri-mutator;1"]
+ .createInstance(Ci.nsIURIMutator)
+ .setSpec(str)
+ .finalize();
+ Assert.equal(simple.spec, uri.spec);
+ Assert.ok(!simple.equals(uri));
+ Assert.ok(!uri.equals(simple));
+}
diff --git a/dom/tests/unit/test_geolocation_monitor.js b/dom/tests/unit/test_geolocation_monitor.js
new file mode 100644
index 0000000000..161a017b35
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_monitor.js
@@ -0,0 +1,97 @@
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+let geolocation = null;
+let locations = [
+ [1, 2],
+ [3, 4],
+ [5, 6],
+];
+
+function geoHandler(metadata, response) {
+ let [lat, lng] = locations.shift();
+ response.setStatusLine("1.0", 200, "OK");
+ response.setHeader("Cache-Control", "no-cache", false);
+ response.setHeader("Content-Type", "application/x-javascript", false);
+ response.write(
+ JSON.stringify({
+ status: "OK",
+ location: { lat, lng },
+ accuracy: 42,
+ })
+ );
+}
+
+function toJSON(pos) {
+ return { lat: pos.coords.latitude, lng: pos.coords.longitude };
+}
+
+function getPosition() {
+ return new Promise(function (resolve, reject) {
+ geolocation.getCurrentPosition(resolve, reject);
+ });
+}
+
+function watchPosition() {
+ let seen = 0;
+ return new Promise(function (resolve, reject) {
+ let id = geolocation.watchPosition(position => {
+ seen++;
+ if (seen === 1) {
+ Assert.deepEqual(toJSON(position), { lat: 3, lng: 4 });
+ Assert.deepEqual(observer._lastData, { lat: 3, lng: 4 });
+ Assert.equal(observer._countEvents, 2);
+ } else if (seen === 2) {
+ Assert.deepEqual(toJSON(position), { lat: 5, lng: 6 });
+ Assert.deepEqual(observer._lastData, { lat: 5, lng: 6 });
+ Assert.equal(observer._countEvents, 3);
+ geolocation.clearWatch(id);
+ resolve();
+ }
+ }, reject);
+ });
+}
+
+let observer = {
+ QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
+
+ observe(subject, topic, data) {
+ Assert.equal(topic, "geolocation-position-events");
+ observer._countEvents++;
+ observer._lastData = toJSON(subject);
+ },
+
+ _lastData: null,
+ _countEvents: 0,
+};
+
+add_task(async function test_resetClient() {
+ do_get_profile();
+ geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
+
+ let server = new HttpServer();
+ server.registerPathHandler("/geo", geoHandler);
+ server.start(-1);
+
+ Services.prefs.setCharPref(
+ "geo.provider.network.url",
+ "http://localhost:" + server.identity.primaryPort + "/geo"
+ );
+ Services.prefs.setBoolPref(
+ "geo.provider.network.debug.requestCache.enabled",
+ false
+ );
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ var obs = Cc["@mozilla.org/observer-service;1"].getService();
+ obs = obs.QueryInterface(Ci.nsIObserverService);
+ obs.addObserver(observer, "geolocation-position-events");
+
+ let position = await getPosition();
+ Assert.deepEqual(toJSON(position), { lat: 1, lng: 2 });
+ Assert.equal(observer._countEvents, 1);
+ Assert.deepEqual(observer._lastData, { lat: 1, lng: 2 });
+
+ await watchPosition();
+
+ await new Promise(resolve => server.stop(resolve));
+});
diff --git a/dom/tests/unit/test_geolocation_position_unavailable.js b/dom/tests/unit/test_geolocation_position_unavailable.js
new file mode 100644
index 0000000000..667a84f050
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_position_unavailable.js
@@ -0,0 +1,28 @@
+function successCallback() {
+ Assert.ok(false);
+ do_test_finished();
+}
+
+function errorCallback(err) {
+ // GeolocationPositionError has no interface object, so we can't get constants off that.
+ Assert.equal(err.POSITION_UNAVAILABLE, err.code);
+ Assert.equal(2, err.code);
+ do_test_finished();
+}
+
+function run_test() {
+ do_test_pending();
+
+ if (Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+ Services.prefs.setCharPref("geo.provider.network.url", "UrlNotUsedHere:");
+ }
+
+ var geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
+ geolocation.getCurrentPosition(successCallback, errorCallback);
+}
diff --git a/dom/tests/unit/test_geolocation_position_unavailable_wrap.js b/dom/tests/unit/test_geolocation_position_unavailable_wrap.js
new file mode 100644
index 0000000000..ca80fdb83a
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_position_unavailable_wrap.js
@@ -0,0 +1,6 @@
+function run_test() {
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ Services.prefs.setCharPref("geo.provider.network.url", "UrlNotUsedHere");
+ run_test_in_child("./test_geolocation_position_unavailable.js");
+}
diff --git a/dom/tests/unit/test_geolocation_provider.js b/dom/tests/unit/test_geolocation_provider.js
new file mode 100644
index 0000000000..7dd072cf43
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_provider.js
@@ -0,0 +1,83 @@
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+var httpserver = null;
+var geolocation = null;
+var success = false;
+var watchID = -1;
+
+function terminate(succ) {
+ success = succ;
+ geolocation.clearWatch(watchID);
+}
+
+function successCallback(pos) {
+ terminate(true);
+}
+function errorCallback(pos) {
+ terminate(false);
+}
+
+var observer = {
+ QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
+
+ observe(subject, topic, data) {
+ if (data == "shutdown") {
+ Assert.ok(1);
+ this._numProviders--;
+ if (!this._numProviders) {
+ httpserver.stop(function () {
+ Assert.ok(success);
+ do_test_finished();
+ });
+ }
+ } else if (data == "starting") {
+ Assert.ok(1);
+ this._numProviders++;
+ }
+ },
+
+ _numProviders: 0,
+};
+
+function geoHandler(metadata, response) {
+ var georesponse = {
+ status: "OK",
+ location: {
+ lat: 42,
+ lng: 42,
+ },
+ accuracy: 42,
+ };
+ var position = JSON.stringify(georesponse);
+ response.setStatusLine("1.0", 200, "OK");
+ response.setHeader("Cache-Control", "no-cache", false);
+ response.setHeader("Content-Type", "aplication/x-javascript", false);
+ response.write(position);
+}
+
+function run_test() {
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ // only kill this test when shutdown is called on the provider.
+ do_test_pending();
+
+ httpserver = new HttpServer();
+ httpserver.registerPathHandler("/geo", geoHandler);
+ httpserver.start(-1);
+
+ Services.prefs.setCharPref(
+ "geo.provider.network.url",
+ "http://localhost:" + httpserver.identity.primaryPort + "/geo"
+ );
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ var obs = Cc["@mozilla.org/observer-service;1"].getService();
+ obs = obs.QueryInterface(Ci.nsIObserverService);
+ obs.addObserver(observer, "geolocation-device-events");
+
+ geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
+ watchID = geolocation.watchPosition(successCallback, errorCallback);
+}
diff --git a/dom/tests/unit/test_geolocation_provider_timeout.js b/dom/tests/unit/test_geolocation_provider_timeout.js
new file mode 100644
index 0000000000..6f311a5aa8
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_provider_timeout.js
@@ -0,0 +1,43 @@
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+var httpserver = null;
+var geolocation = null;
+
+function geoHandler(metadata, response) {
+ response.processAsync();
+}
+
+function successCallback() {
+ // The call shouldn't be sucessful.
+ Assert.ok(false);
+ do_test_finished();
+}
+
+function errorCallback() {
+ Assert.ok(true);
+ do_test_finished();
+}
+
+function run_test() {
+ do_test_pending();
+
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ httpserver = new HttpServer();
+ httpserver.registerPathHandler("/geo", geoHandler);
+ httpserver.start(-1);
+ Services.prefs.setCharPref(
+ "geo.provider.network.url",
+ "http://localhost:" + httpserver.identity.primaryPort + "/geo"
+ );
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ // Setting timeout to a very low value to ensure time out will happen.
+ Services.prefs.setIntPref("geo.provider.network.timeout", 5);
+
+ geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
+ geolocation.getCurrentPosition(successCallback, errorCallback);
+}
diff --git a/dom/tests/unit/test_geolocation_reset_accuracy.js b/dom/tests/unit/test_geolocation_reset_accuracy.js
new file mode 100644
index 0000000000..3cb3374e09
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_reset_accuracy.js
@@ -0,0 +1,93 @@
+const providerCID = Components.ID("{14aa4b81-e266-45cb-88f8-89595dece114}");
+const providerContract = "@mozilla.org/geolocation/provider;1";
+
+const categoryName = "geolocation-provider";
+
+var provider = {
+ QueryInterface: ChromeUtils.generateQI([
+ "nsIFactory",
+ "nsIGeolocationProvider",
+ ]),
+ createInstance: function eventsink_ci(iid) {
+ return this.QueryInterface(iid);
+ },
+ startup() {},
+ watch() {},
+ shutdown() {},
+ setHighAccuracy(enable) {
+ this._isHigh = enable;
+ if (enable) {
+ this._seenHigh = true;
+ executeSoon(stop_high_accuracy_watch);
+ }
+ },
+ _isHigh: false,
+ _seenHigh: false,
+};
+
+function successCallback() {
+ Assert.ok(false);
+ do_test_finished();
+}
+
+function errorCallback() {
+ Assert.ok(false);
+ do_test_finished();
+}
+
+var geolocation;
+var watchID2;
+
+function run_test() {
+ if (runningInParent) {
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ Components.manager.nsIComponentRegistrar.registerFactory(
+ providerCID,
+ "Unit test geo provider",
+ providerContract,
+ provider
+ );
+
+ Services.catMan.addCategoryEntry(
+ categoryName,
+ "unit test",
+ providerContract,
+ false,
+ true
+ );
+
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+ }
+
+ do_test_pending();
+
+ geolocation = Cc["@mozilla.org/geolocation;1"].createInstance(Ci.nsISupports);
+ geolocation.watchPosition(successCallback, errorCallback);
+ watchID2 = geolocation.watchPosition(successCallback, errorCallback, {
+ enableHighAccuracy: true,
+ });
+
+ if (!runningInParent) {
+ do_await_remote_message("high_acc_enabled", stop_high_accuracy_watch);
+ }
+}
+
+function stop_high_accuracy_watch() {
+ geolocation.clearWatch(watchID2);
+ check_results();
+ do_test_finished();
+}
+
+function check_results() {
+ if (runningInParent) {
+ // check the provider was set to high accuracy during the test
+ Assert.ok(provider._seenHigh);
+ // check the provider is not currently set to high accuracy
+ Assert.ok(!provider._isHigh);
+ }
+ do_test_finished();
+}
diff --git a/dom/tests/unit/test_geolocation_reset_accuracy_wrap.js b/dom/tests/unit/test_geolocation_reset_accuracy_wrap.js
new file mode 100644
index 0000000000..4160fda14a
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_reset_accuracy_wrap.js
@@ -0,0 +1,61 @@
+const providerCID = Components.ID("{14aa4b81-e266-45cb-88f8-89595dece114}");
+const providerContract = "@mozilla.org/geolocation/provider;1";
+
+const categoryName = "geolocation-provider";
+
+var provider = {
+ QueryInterface: ChromeUtils.generateQI([
+ "nsIFactory",
+ "nsIGeolocationProvider",
+ ]),
+ createInstance: function eventsink_ci(iid) {
+ return this.QueryInterface(iid);
+ },
+ startup() {},
+ watch() {},
+ shutdown() {},
+ setHighAccuracy(enable) {
+ this._isHigh = enable;
+ if (enable) {
+ this._seenHigh = true;
+ do_send_remote_message("high_acc_enabled");
+ }
+ },
+ _isHigh: false,
+ _seenHigh: false,
+};
+
+function run_test() {
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ Components.manager.nsIComponentRegistrar.registerFactory(
+ providerCID,
+ "Unit test geo provider",
+ providerContract,
+ provider
+ );
+
+ Services.catMan.addCategoryEntry(
+ categoryName,
+ "unit test",
+ providerContract,
+ false,
+ true
+ );
+
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ run_test_in_child("test_geolocation_reset_accuracy.js", check_results);
+}
+
+function check_results() {
+ // check the provider was set to high accuracy during the test
+ Assert.ok(provider._seenHigh);
+ // check the provider is not currently set to high accuracy
+ Assert.ok(!provider._isHigh);
+
+ do_test_finished();
+}
diff --git a/dom/tests/unit/test_geolocation_timeout.js b/dom/tests/unit/test_geolocation_timeout.js
new file mode 100644
index 0000000000..07d590a694
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_timeout.js
@@ -0,0 +1,59 @@
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+var httpserver = null;
+var geolocation = null;
+
+function geoHandler(metadata, response) {
+ var georesponse = {
+ status: "OK",
+ location: {
+ lat: 42,
+ lng: 42,
+ },
+ accuracy: 42,
+ };
+ var position = JSON.stringify(georesponse);
+ response.processAsync();
+ response.setStatusLine("1.0", 200, "OK");
+ response.setHeader("Cache-Control", "no-cache", false);
+ response.setHeader("Content-Type", "aplication/x-javascript", false);
+ do_timeout(5000, function () {
+ response.write(position);
+ response.finish();
+ });
+}
+
+function successCallback() {
+ Assert.ok(false);
+ do_test_finished();
+}
+
+function errorCallback() {
+ Assert.ok(true);
+ do_test_finished();
+}
+
+function run_test() {
+ do_test_pending();
+
+ if (Services.appinfo.processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
+ // XPCShell does not get a profile by default. The geolocation service
+ // depends on the settings service which uses IndexedDB and IndexedDB
+ // needs a place where it can store databases.
+ do_get_profile();
+
+ httpserver = new HttpServer();
+ httpserver.registerPathHandler("/geo", geoHandler);
+ httpserver.start(-1);
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+ Services.prefs.setCharPref(
+ "geo.provider.network.url",
+ "http://localhost:" + httpserver.identity.primaryPort + "/geo"
+ );
+ }
+
+ geolocation = Cc["@mozilla.org/geolocation;1"].getService(Ci.nsISupports);
+ geolocation.getCurrentPosition(successCallback, errorCallback, {
+ timeout: 2000,
+ });
+}
diff --git a/dom/tests/unit/test_geolocation_timeout_wrap.js b/dom/tests/unit/test_geolocation_timeout_wrap.js
new file mode 100644
index 0000000000..cdde9543bd
--- /dev/null
+++ b/dom/tests/unit/test_geolocation_timeout_wrap.js
@@ -0,0 +1,15 @@
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+var httpserver = null;
+
+function run_test() {
+ Services.prefs.setBoolPref("geo.provider.network.scan", false);
+
+ httpserver = new HttpServer();
+ httpserver.start(-1);
+ Services.prefs.setCharPref(
+ "geo.provider.network.url",
+ "http://localhost:" + httpserver.identity.primaryPort + "/geo"
+ );
+ run_test_in_child("./test_geolocation_timeout.js");
+}
diff --git a/dom/tests/unit/test_xhr_init.js b/dom/tests/unit/test_xhr_init.js
new file mode 100644
index 0000000000..a2eaf2b4d7
--- /dev/null
+++ b/dom/tests/unit/test_xhr_init.js
@@ -0,0 +1,13 @@
+function run_test() {
+ var x = new XMLHttpRequest({ mozAnon: true, mozSystem: false });
+ Assert.ok(x.mozAnon);
+ Assert.ok(x.mozSystem); // Because we're system principal
+
+ x = new XMLHttpRequest({ mozAnon: true });
+ Assert.ok(x.mozAnon);
+ Assert.ok(x.mozSystem);
+
+ x = new XMLHttpRequest();
+ Assert.ok(!x.mozAnon);
+ Assert.ok(x.mozSystem);
+}
diff --git a/dom/tests/unit/xpcshell.ini b/dom/tests/unit/xpcshell.ini
new file mode 100644
index 0000000000..1921621c0f
--- /dev/null
+++ b/dom/tests/unit/xpcshell.ini
@@ -0,0 +1,24 @@
+[DEFAULT]
+head =
+
+[test_bug319968.js]
+[test_bug465752.js]
+[test_Fetch.js]
+[test_geolocation_provider.js]
+[test_geolocation_monitor.js]
+[test_geolocation_timeout.js]
+[test_geolocation_timeout_wrap.js]
+skip-if = os == "mac"
+ os == "android"
+[test_geolocation_reset_accuracy.js]
+[test_geolocation_reset_accuracy_wrap.js]
+skip-if =
+ os == "mac"
+ os == "android"
+[test_geolocation_position_unavailable.js]
+[test_geolocation_position_unavailable_wrap.js]
+skip-if = os == "mac"
+ os == "android"
+[test_PromiseDebugging.js]
+[test_xhr_init.js]
+[test_geolocation_provider_timeout.js]