UNPKG

8.98 kBJavaScriptView Raw
1'use strict';
2// This is a basic test file for use with testling.
3// The test script language comes from tape.
4/* jshint node: true */
5/* global Promise */
6var test = require('tape');
7
8var m = require('../adapter.js');
9
10test('Browser identified', function(t) {
11 t.plan(3);
12 t.ok(m.webrtcDetectedBrowser, 'Browser detected:' + m.webrtcDetectedBrowser);
13 t.ok(m.webrtcDetectedVersion,
14 'Browser version detected:' + m.webrtcDetectedVersion);
15 t.ok(m.webrtcMinimumVersion, 'Minimum Browser version detected');
16});
17
18test('Browser supported by adapter.js', function(t) {
19 t.plan(1);
20 t.ok(m.webrtcDetectedVersion >= m.webrtcMinimumVersion,
21 'Browser version supported by adapter.js');
22});
23
24test('create RTCPeerConnection', function(t) {
25 t.plan(1);
26 t.ok(typeof(new RTCPeerConnection()) === 'object',
27 'RTCPeerConnection constructor');
28});
29
30test('call getUserMedia with constraints', function(t) {
31 var impossibleConstraints = {
32 video: {
33 width: 1280,
34 height: {min: 200, ideal: 720, max: 1080},
35 frameRate: {exact: 0} // to fail
36 },
37 };
38 new Promise(function(resolve, reject) {
39 navigator.getUserMedia(impossibleConstraints, resolve, reject);
40 })
41 .then(function() {
42 t.fail('getUserMedia(impossibleConstraints) must fail');
43 t.end();
44 })
45 .catch(function(err) {
46 t.pass('getUserMedia(impossibleConstraints) must fail');
47 t.ok(err.name.indexOf('Error') >= 0, 'must fail with named Error');
48 t.end();
49 });
50});
51
52test('basic connection establishment', function(t) {
53 var pc1 = new RTCPeerConnection(null);
54 var pc2 = new RTCPeerConnection(null);
55 var ended = false;
56
57 pc1.createDataChannel('somechannel');
58 pc1.oniceconnectionstatechange = function() {
59 if (pc1.iceConnectionState === 'connected' ||
60 pc1.iceConnectionState === 'completed') {
61 t.pass('P2P connection established');
62 if (!ended) {
63 ended = true;
64 t.end();
65 }
66 }
67 };
68
69 var addCandidate = function(pc, event) {
70 if (event.candidate) {
71 var cand = new RTCIceCandidate(event.candidate);
72 pc.addIceCandidate(cand,
73 function() {
74 t.pass('addIceCandidate');
75 },
76 function(err) {
77 t.fail('addIceCandidate ' + err.toString());
78 }
79 );
80 }
81 };
82 pc1.onicecandidate = function(event) {
83 addCandidate(pc2, event);
84 };
85 pc2.onicecandidate = function(event) {
86 addCandidate(pc1, event);
87 };
88
89 pc1.createOffer(
90 function(offer) {
91 t.pass('pc1.createOffer');
92 pc1.setLocalDescription(offer,
93 function() {
94 t.pass('pc1.setLocalDescription');
95
96 offer = new RTCSessionDescription(offer);
97 t.pass('created RTCSessionDescription from offer');
98 pc2.setRemoteDescription(offer,
99 function() {
100 t.pass('pc2.setRemoteDescription');
101 pc2.createAnswer(
102 function(answer) {
103 t.pass('pc2.createAnswer');
104 pc2.setLocalDescription(answer,
105 function() {
106 t.pass('pc2.setLocalDescription');
107 answer = new RTCSessionDescription(answer);
108 t.pass('created RTCSessionDescription from answer');
109 pc1.setRemoteDescription(answer,
110 function() {
111 t.pass('pc1.setRemoteDescription');
112 },
113 function(err) {
114 t.fail('pc1.setRemoteDescription ' + err.toString());
115 }
116 );
117 },
118 function(err) {
119 t.fail('pc2.setLocalDescription ' + err.toString());
120 }
121 );
122 },
123 function(err) {
124 t.fail('pc2.createAnswer ' + err.toString());
125 }
126 );
127 },
128 function(err) {
129 t.fail('pc2.setRemoteDescription ' + err.toString());
130 }
131 );
132 },
133 function(err) {
134 t.fail('pc1.setLocalDescription ' + err.toString());
135 }
136 );
137 },
138 function(err) {
139 t.fail('pc1 failed to create offer ' + err.toString());
140 }
141 );
142});
143
144test('basic connection establishment with promise', function(t) {
145 var pc1 = new m.RTCPeerConnection(null);
146 var pc2 = new m.RTCPeerConnection(null);
147 var ended = false;
148
149 pc1.createDataChannel('somechannel');
150 pc1.oniceconnectionstatechange = function() {
151 if (pc1.iceConnectionState === 'connected' ||
152 pc1.iceConnectionState === 'completed') {
153 t.pass('P2P connection established');
154 if (!ended) {
155 ended = true;
156 t.end();
157 }
158 }
159 };
160
161 var addCandidate = function(pc, event) {
162 if (event.candidate) {
163 var cand = new RTCIceCandidate(event.candidate);
164 pc.addIceCandidate(cand).catch(function(err) {
165 t.fail('addIceCandidate ' + err.toString());
166 });
167 }
168 };
169 pc1.onicecandidate = function(event) {
170 addCandidate(pc2, event);
171 };
172 pc2.onicecandidate = function(event) {
173 addCandidate(pc1, event);
174 };
175
176 pc1.createOffer().then(function(offer) {
177 t.pass('pc1.createOffer');
178 return pc1.setLocalDescription(offer);
179 }).then(function() {
180 t.pass('pc1.setLocalDescription');
181 return pc2.setRemoteDescription(pc1.localDescription);
182 }).then(function() {
183 t.pass('pc2.setRemoteDescription');
184 return pc2.createAnswer();
185 }).then(function(answer) {
186 t.pass('pc2.createAnswer');
187 return pc2.setLocalDescription(answer);
188 }).then(function() {
189 t.pass('pc2.setLocalDescription');
190 return pc1.setRemoteDescription(pc2.localDescription);
191 }).then(function() {
192 t.pass('pc1.setRemoteDescription');
193 }).catch(function(err) {
194 t.fail(err.toString());
195 });
196});
197
198test('call enumerateDevices', function(t) {
199 var step = 'enumerateDevices() must succeed';
200 navigator.mediaDevices.enumerateDevices()
201 .then(function(devices) {
202 t.pass(step);
203 step = 'valid enumerateDevices output: ' + JSON.stringify(devices);
204 t.ok(typeof devices.length === 'number', 'Produced a devices array');
205 devices.forEach(function(d) {
206 t.ok(d.kind === 'videoinput' ||
207 d.kind === 'audioinput' ||
208 d.kind === 'audiooutput', 'Known device kind');
209 t.ok(d.deviceId.length !== undefined, 'device id present');
210 t.ok(d.label.length !== undefined, 'device label present');
211 });
212 t.pass(step);
213 t.end();
214 })
215 .catch(function(err) {
216 t.fail(step + ' - ' + err.toString());
217 t.end();
218 });
219});
220
221// test that adding and removing an eventlistener on navigator.mediaDevices
222// is possible. The usecase for this is the devicechanged event.
223// This does not test whether devicechanged is actually called.
224test('navigator.mediaDevices eventlisteners', function(t) {
225 t.plan(2);
226 t.ok(typeof(navigator.mediaDevices.addEventListener) === 'function',
227 'navigator.mediaDevices.addEventListener is a function');
228 t.ok(typeof(navigator.mediaDevices.removeEventListener) === 'function',
229 'navigator.mediaDevices.removeEventListener is a function');
230});
231
232// Test Chrome polyfill for getStats.
233test('getStats', function(t) {
234 var pc1 = new m.RTCPeerConnection(null);
235
236 // Test expected new behavior.
237 new Promise(function(resolve, reject) {
238 pc1.getStats(null, resolve, reject);
239 })
240 .then(function(report) {
241 t.equal(typeof(report), 'object', 'report is an object.');
242 for (var key in report) {
243 // This avoids problems with Firefox
244 if (typeof(report[key]) === 'function') {
245 continue;
246 }
247 t.equal(report[key].id, key, 'report key matches stats id.');
248 }
249 t.end();
250 })
251 .catch(function(err) {
252 t.fail('getStats() should never fail with error: ' + err.toString());
253 t.end();
254 });
255});
256
257// Test that polyfill for Chrome getStats falls back to builtin functionality
258// when the old getStats function signature is used; when the callback is passed
259// as the first argument.
260test('originalChromeGetStats', function(t) {
261 var pc1 = new m.RTCPeerConnection(null);
262
263 if (m.webrtcDetectedBrowser === 'chrome') {
264 new Promise(function(resolve, reject) { // jshint ignore: line
265 pc1.getStats(resolve, null);
266 })
267 .then(function(response) {
268 var reports = response.result();
269 reports.forEach(function(report) {
270 t.equal(typeof(report), 'object');
271 t.equal(typeof(report.id), 'string');
272 t.equal(typeof(report.type), 'string');
273 t.equal(typeof(report.timestamp), 'object');
274 report.names().forEach(function(name) {
275 t.notEqual(report.stat(name), null,
276 'stat ' +
277 name + ' not equal to null');
278 });
279 });
280 t.end();
281 })
282 .catch(function(err) {
283 t.fail('getStats() should never fail with error: ' + err.toString());
284 t.end();
285 });
286 } else {
287 // All other browsers.
288 t.end();
289 }
290});