fune/dom/presentation/tests/mochitest/test_presentation_availability_iframe.html
Mark Banner a0026662dd Bug 1475004 - Enable ESLint for dom/presentation - automatic fixes. r=mccr8
This is based on the same rules as for the majority of the rest of mozilla-central.

MozReview-Commit-ID: 2O1jH8cNXIj

--HG--
extra : rebase_source : b6788a231f70521398ef8783bebce99384a344d9
2018-07-03 16:19:05 +01:00

227 lines
7.3 KiB
HTML

<!DOCTYPE HTML>
<html>
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
<meta charset="utf-8">
<title>Test for PresentationAvailability</title>
<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=1228508">Test PresentationAvailability</a>
<script type="application/javascript">
let ok = window.parent.ok;
let is = window.parent.is;
let isnot = window.parent.isnot;
let SimpleTest = window.parent.SimpleTest;
let SpecialPowers = window.parent.SpecialPowers;
"use strict";
var testDevice = {
id: "id",
name: "name",
type: "type",
};
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL("PresentationDeviceInfoChromeScript.js"));
var request;
var availability;
function testSetup() {
return new Promise(function(aResolve, aReject) {
gScript.addMessageListener("setup-complete", function() {
aResolve();
});
gScript.sendAsyncMessage("setup");
});
}
function testInitialUnavailable() {
request = new PresentationRequest("https://example.com");
return request.getAvailability().then(function(aAvailability) {
is(aAvailability.value, false, "Should have no available device after setup");
aAvailability.onchange = function() {
aAvailability.onchange = null;
ok(aAvailability.value, "Device should be available.");
};
availability = aAvailability;
gScript.sendAsyncMessage("trigger-device-add", testDevice);
}).catch(function(aError) {
ok(false, "Error occurred when getting availability: " + aError);
teardown();
});
}
function testInitialAvailable() {
let anotherRequest = new PresentationRequest("https://example.net");
return anotherRequest.getAvailability().then(function(aAvailability) {
is(aAvailability.value, true, "Should have available device initially");
isnot(aAvailability, availability, "Should get different availability object for different request URL");
}).catch(function(aError) {
ok(false, "Error occurred when getting availability: " + aError);
teardown();
});
}
function testSameObject() {
let sameUrlRequest = new PresentationRequest("https://example.com");
return sameUrlRequest.getAvailability().then(function(aAvailability) {
is(aAvailability, availability, "Should get same availability object for same request URL");
}).catch(function(aError) {
ok(false, "Error occurred when getting availability: " + aError);
teardown();
});
}
function testOnChangeEvent() {
return new Promise(function(aResolve, aReject) {
availability.onchange = function() {
availability.onchange = null;
is(availability.value, false, "Should have no available device after device removed");
aResolve();
};
gScript.sendAsyncMessage("trigger-device-remove");
});
}
function testConsecutiveGetAvailability() {
let request = new PresentationRequest("https://example.org");
let firstAvailabilityResolved = false;
return Promise.all([
request.getAvailability().then(function() {
firstAvailabilityResolved = true;
}),
request.getAvailability().then(function() {
ok(firstAvailabilityResolved, "getAvailability() should be resolved in sequence");
})
]).catch(function(aError) {
ok(false, "Error occurred when getting availability: " + aError);
teardown();
});
}
function testUnsupportedDeviceAvailability() {
return Promise.race([
new Promise(function(aResolve, aReject) {
let request = new PresentationRequest("https://test.com");
request.getAvailability().then(function(aAvailability) {
availability = aAvailability;
aAvailability.onchange = function() {
availability.onchange = null;
ok(false, "Should not get onchange event.");
teardown();
};
});
gScript.sendAsyncMessage("trigger-add-unsupport-url-device");
}),
new Promise(function(aResolve, aReject) {
setTimeout(function() {
ok(true, "Should not get onchange event.");
availability.onchange = null;
gScript.sendAsyncMessage("trigger-remove-unsupported-device");
aResolve();
}, 3000);
}),
]);
}
function testMultipleAvailabilityURLs() {
let request1 = new PresentationRequest(["https://example.com",
"https://example1.com"]);
let request2 = new PresentationRequest(["https://example1.com",
"https://example2.com"]);
return Promise.all([
request1.getAvailability().then(function(aAvailability) {
return new Promise(function(aResolve) {
aAvailability.onchange = function() {
aAvailability.onchange = null;
ok(true, "Should get onchange event.");
aResolve();
};
});
}),
request2.getAvailability().then(function(aAvailability) {
return new Promise(function(aResolve) {
aAvailability.onchange = function() {
aAvailability.onchange = null;
ok(true, "Should get onchange event.");
aResolve();
};
});
}),
new Promise(function(aResolve) {
gScript.sendAsyncMessage("trigger-add-multiple-devices");
aResolve();
}),
]).then(new Promise(function(aResolve) {
gScript.sendAsyncMessage("trigger-remove-multiple-devices");
aResolve();
}));
}
function testPartialSupportedDeviceAvailability() {
let request1 = new PresentationRequest(["https://supportedUrl.com"]);
let request2 = new PresentationRequest(["http://notSupportedUrl.com"]);
return Promise.all([
request1.getAvailability().then(function(aAvailability) {
return new Promise(function(aResolve) {
aAvailability.onchange = function() {
aAvailability.onchange = null;
ok(true, "Should get onchange event.");
aResolve();
};
});
}),
Promise.race([
request2.getAvailability().then(function(aAvailability) {
return new Promise(function(aResolve) {
aAvailability.onchange = function() {
aAvailability.onchange = null;
ok(false, "Should get onchange event.");
aResolve();
};
});
}),
new Promise(function(aResolve) {
setTimeout(function() {
ok(true, "Should not get onchange event.");
availability.onchange = null;
aResolve();
}, 3000);
}),
]),
new Promise(function(aResolve) {
gScript.sendAsyncMessage("trigger-add-https-devices");
aResolve();
}),
]).then(new Promise(function(aResolve) {
gScript.sendAsyncMessage("trigger-remove-https-devices");
aResolve();
}));
}
function teardown() {
request = null;
availability = null;
gScript.sendAsyncMessage("teardown");
gScript.destroy();
SimpleTest.finish();
}
ok(navigator.presentation, "navigator.presentation should be available.");
testSetup().then(testInitialUnavailable)
.then(testInitialAvailable)
.then(testSameObject)
.then(testOnChangeEvent)
.then(testConsecutiveGetAvailability)
.then(testMultipleAvailabilityURLs)
.then(testUnsupportedDeviceAvailability)
.then(testPartialSupportedDeviceAvailability)
.then(teardown);
</script>
</body>
</html>