tenfourfox/dom/presentation/tests/mochitest/test_presentation_device_info.html
Cameron Kaiser c9b2922b70 hello FPR
2017-04-19 00:56:45 -07:00

149 lines
5.0 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 B2G Presentation Device Info API</title>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1080474">Test for B2G Presentation Device Info API</a>
<script type="application/javascript;version=1.8">
'use strict';
SimpleTest.waitForExplicitFinish();
var testDevice = {
id: 'id',
name: 'name',
type: 'type',
};
var gUrl = SimpleTest.getTestFileURL('PresentationDeviceInfoChromeScript.js');
var gScript = SpecialPowers.loadChromeScript(gUrl);
function testSetup() {
return new Promise(function(resolve, reject) {
gScript.addMessageListener('setup-complete', function() {
resolve();
});
gScript.sendAsyncMessage('setup');
});
}
function testForceDiscovery() {
info('test force discovery');
return new Promise(function(resolve, reject) {
gScript.addMessageListener('force-discovery', function() {
ok(true, 'nsIPresentationDeviceProvider.forceDiscovery is invoked');
resolve();
});
navigator.mozPresentationDeviceInfo.forceDiscovery();
});
}
function testDeviceAdd() {
info('test device add');
return new Promise(function(resolve, reject) {
navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
let detail = e.detail;
is(detail.type, 'add', 'expected update type');
is(detail.deviceInfo.id, testDevice.id, 'expected device id');
is(detail.deviceInfo.name, testDevice.name, 'expected device name');
is(detail.deviceInfo.type, testDevice.type, 'expected device type');
navigator.mozPresentationDeviceInfo.getAll()
.then(function(devices) {
is(devices.length, 1, 'expected 1 available device');
is(devices[0].id, testDevice.id, 'expected device id');
is(devices[0].name, testDevice.name, 'expected device name');
is(devices[0].type, testDevice.type, 'expected device type');
resolve();
});
});
gScript.sendAsyncMessage('trigger-device-add', testDevice);
});
}
function testDeviceUpdate() {
info('test device update');
return new Promise(function(resolve, reject) {
testDevice.name = 'name-update';
navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
let detail = e.detail;
is(detail.type, 'update', 'expected update type');
is(detail.deviceInfo.id, testDevice.id, 'expected device id');
is(detail.deviceInfo.name, testDevice.name, 'expected device name');
is(detail.deviceInfo.type, testDevice.type, 'expected device type');
navigator.mozPresentationDeviceInfo.getAll()
.then(function(devices) {
is(devices.length, 1, 'expected 1 available device');
is(devices[0].id, testDevice.id, 'expected device id');
is(devices[0].name, testDevice.name, 'expected device name');
is(devices[0].type, testDevice.type, 'expected device type');
resolve();
});
});
gScript.sendAsyncMessage('trigger-device-update', testDevice);
});
}
function testDeviceRemove() {
info('test device remove');
return new Promise(function(resolve, reject) {
navigator.mozPresentationDeviceInfo.addEventListener('devicechange', function deviceChangeHandler(e) {
navigator.mozPresentationDeviceInfo.removeEventListener('devicechange', deviceChangeHandler);
let detail = e.detail;
is(detail.type, 'remove', 'expected update type');
is(detail.deviceInfo.id, testDevice.id, 'expected device id');
is(detail.deviceInfo.name, testDevice.name, 'expected device name');
is(detail.deviceInfo.type, testDevice.type, 'expected device type');
navigator.mozPresentationDeviceInfo.getAll()
.then(function(devices) {
is(devices.length, 0, 'expected 0 available device');
resolve();
});
});
gScript.sendAsyncMessage('trigger-device-remove');
});
}
function runTests() {
testSetup()
.then(testForceDiscovery)
.then(testDeviceAdd)
.then(testDeviceUpdate)
.then(testDeviceRemove)
.then(function() {
info('test finished, teardown');
gScript.sendAsyncMessage('teardown', '');
gScript.destroy();
SimpleTest.finish();
});
}
window.addEventListener('load', function() {
SpecialPowers.pushPermissions([
{type: 'presentation-device-manage', allow: true, context: document},
], function() {
SpecialPowers.pushPrefEnv({
'set': [
['dom.presentation.enabled', true],
]
}, runTests);
});
});
</script>
</pre>
</body>
</html>