1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
<!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"/>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1228508">Test PresentationAvailability</a>
<script type="application/javascript;version=1.8">
"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();
}
function runTests() {
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);
}
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout('Test for guarantee not firing async event');
SpecialPowers.pushPermissions([
{type: "presentation-device-manage", allow: false, context: document},
], function() {
SpecialPowers.pushPrefEnv({ "set": [["dom.presentation.enabled", true],
["dom.presentation.controller.enabled", true],
["dom.presentation.session_transport.data_channel.enable", false]]},
runTests);
});
</script>
</body>
</html>
|