Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: third_party/WebKit/LayoutTests/imported/wpt/encrypted-media/scripts/syntax-mediakeysession.js

Issue 2546853003: Add W3C encrypted-media tests (Closed)
Patch Set: rebase now that content files landed Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 function runTest(config) {
2 var keysystem = config.keysystem;
3 var testname = testnamePrefix(null, config.keysystem);
4 var initDataType = config.initDataType;
5 var initData = config.initData;
6 var configuration = {
7 initDataTypes: [config.initDataType],
8 audioCapabilities: [{contentType: config.audioType}],
9 videoCapabilities: [{contentType: config.videoType}],
10 sessionTypes: ['temporary']
11 };
12
13 var kTypeSpecificGenerateRequestExceptionsTestCases = [
14 // Tests in this set use a shortened parameter name due to
15 // format_value() only returning the first 60 characters as the
16 // result. With a longer name the first 60 characters is not
17 // enough to determine which test failed. Even with the
18 // shortened name, the error message for the last couple of
19 // tests is the same.
20
21 // Too few parameters.
22 {
23 exception: 'TypeError',
24 func: function (mk1, type) {
25 return mk1.createSession().generateRequest(type);
26 }
27 },
28 // Invalid parameters.
29 {
30 exception: 'TypeError',
31 func: function (mk2, type) {
32 return mk2.createSession().generateRequest(type, '');
33 }
34 },
35 {
36 exception: 'TypeError',
37 func: function (mk3, type) {
38 return mk3.createSession().generateRequest(type, null);
39 }
40 },
41 {
42 exception: 'TypeError',
43 func: function (mk4, type) {
44 return mk4.createSession().generateRequest(type, undefined);
45 }
46 },
47 {
48 exception: 'TypeError',
49 func: function (mk5, type) {
50 return mk5.createSession().generateRequest(type, 1);
51 }
52 },
53 // (new Uint8Array(0)) returns empty array. So 'TypeError' should
54 // be returned.
55 {
56 exception: 'TypeError',
57 func: function (mk6, type) {
58 return mk6.createSession().generateRequest(type, new Uint8Array( 0));
59 }
60 }
61 ];
62 function generateRequestTestExceptions(){
63 return new Promise(function(resolve, reject){
64 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
65 assert_true(isTypeSupported, "initDataType not supported");
66 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
67 }).then(function (access) {
68 return access.createMediaKeys();
69 }).then(function (mediaKeys) {
70 var mp4SessionPromises = kTypeSpecificGenerateRequestExcepti onsTestCases.map(function (testCase) {
71 return test_exception(testCase, mediaKeys, initDataType, initData);
72 });
73 assert_not_equals(mp4SessionPromises.length, 0);
74 return Promise.all(mp4SessionPromises);
75 }).then(function (result) {
76 resolve();
77 }).catch(function (error) {
78 reject(error);
79 });
80 })
81 }
82 promise_test(function() {
83 return generateRequestTestExceptions();
84 }, testname + ' test MediaKeySession generateRequest() exceptions.');
85
86 var kLoadExceptionsTestCases = [
87 // Too few parameters.
88 {
89 exception: 'TypeError',
90 func: function (mk1) {
91 return mk1.createSession('temporary').load();
92 }
93 },
94 {
95 exception: 'TypeError',
96 func: function (mk3) {
97 return mk3.createSession('temporary').load('');
98 }
99 },
100 {
101 exception: 'TypeError',
102 func: function (mk4) {
103 return mk4.createSession('temporary').load(1);
104 }
105 },
106 {
107 exception: 'TypeError',
108 func: function (mk5) {
109 return mk5.createSession('temporary').load('!@#$%^&*()');
110 }
111 },
112 {
113 exception: 'TypeError',
114 func: function (mk6) {
115 return mk6.createSession('temporary').load('1234');
116 }
117 }
118 ];
119 function loadTestExceptions(){
120 return new Promise(function(resolve, reject){
121 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
122 assert_true(isTypeSupported, "initDataType not supported");
123 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
124 }).then(function (access) {
125 return access.createMediaKeys();
126 }).then(function (mediaKeys) {
127 var sessionPromises = kLoadExceptionsTestCases.map(function (testCase) {
128 return test_exception(testCase, mediaKeys);
129 });
130 assert_not_equals(sessionPromises.length, 0);
131 return Promise.all(sessionPromises);
132 }).then(function () {
133 resolve();
134 }).catch(function (error) {
135 reject(error);
136 });
137 })
138 }
139 promise_test(function() {
140 return loadTestExceptions();
141 }, testname + ' test MediaKeySession load() exceptions.');
142
143 // All calls to |func| in this group are supposed to succeed.
144 // However, the spec notes that some things are optional for
145 // Clear Key. In particular, support for persistent sessions
146 // is optional. Since some implementations won't support some
147 // features, a NotSupportedError is treated as a success
148 // if |isNotSupportedAllowed| is true.
149 var kCreateSessionTestCases = [
150 // Use the default sessionType.
151 {
152 func: function(mk) { return mk.createSession(); },
153 isNotSupportedAllowed: false
154 },
155 // Try variations of sessionType.
156 {
157 func: function(mk) { return mk.createSession('temporary'); },
158 isNotSupportedAllowed: false
159 },
160 {
161 func: function(mk) { return mk.createSession(undefined); },
162 isNotSupportedAllowed: false
163 },
164 {
165 // Since this is optional, some Clear Key implementations
166 // will succeed, others will return a "NotSupportedError".
167 // Both are allowed results.
168 func: function(mk) { return mk.createSession('persistent-license'); },
169 isNotSupportedAllowed: true
170 },
171 // Try additional parameter, which should be ignored.
172 {
173 func: function(mk) { return mk.createSession('temporary', 'extra'); },
174 isNotSupportedAllowed: false
175 }
176 ];
177 // This function checks that calling generateRequest() works for
178 // various sessions. |testCase.func| creates a MediaKeySession
179 // object, and then generateRequest() is called on that object. It
180 // allows for an NotSupportedError to be generated and treated as a
181 // success, if allowed. See comment above kCreateSessionTestCases.
182 function test_generateRequest(testCase, mediaKeys, type, initData) {
183 var mediaKeySession;
184 try {
185 mediaKeySession = testCase.func.call(null, mediaKeys);
186 } catch (e) {
187 assert_true(testCase.isNotSupportedAllowed);
188 assert_equals(e.name, 'NotSupportedError');
189 return Promise.resolve('not supported');
190 }
191 return mediaKeySession.generateRequest(type, initData);
192 }
193 function generateRequestForVariousSessions(){
194 return new Promise(function(resolve, reject){
195 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
196 assert_true(isTypeSupported, "initDataType should be support ed");
197 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
198 }).then(function (access) {
199 return access.createMediaKeys();
200 }).then(function (mediaKeys) {
201 var mp4SessionPromises = kCreateSessionTestCases.map(functio n (testCase) {
202 return test_generateRequest(testCase, mediaKeys, initDat aType, initData);
203 });
204 assert_not_equals(mp4SessionPromises.length, 0);
205 return Promise.all(mp4SessionPromises);
206 }).then(function () {
207 resolve();
208 }).catch(function (error) {
209 reject(error);
210 });
211 })
212 }
213 promise_test(function() {
214 return generateRequestForVariousSessions();
215 }, testname + ' test if MediaKeySession generateRequest() resolves for vario us sessions');
216
217 var kUpdateSessionExceptionsTestCases = [
218 // Tests in this set use a shortened parameter name due to
219 // format_value() only returning the first 60 characters as the
220 // result. With a longer name (mediaKeySession) the first 60
221 // characters is not enough to determine which test failed.
222
223 // Too few parameters.
224 {
225 exception: 'TypeError',
226 func: function (s) {
227 return s.update();
228 }
229 },
230 // Invalid parameters.
231 {
232 exception: 'TypeError',
233 func: function (s) {
234 return s.update('');
235 }
236 },
237 {
238 exception: 'TypeError',
239 func: function (s) {
240 return s.update(null);
241 }
242 },
243 {
244 exception: 'TypeError',
245 func: function (s) {
246 return s.update(undefined);
247 }
248 },
249 {
250 exception: 'TypeError',
251 func: function (s) {
252 return s.update(1);
253 }
254 },
255 // (new Uint8Array(0)) returns empty array. So 'TypeError' should
256 // be returned.
257 {
258 exception: 'TypeError',
259 func: function (s) {
260 return s.update(new Uint8Array(0));
261 }
262 }
263 ];
264
265 function updateTestExceptions(){
266 return new Promise(function(resolve, reject){
267 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
268 assert_true(isTypeSupported, "initDataType not supported");
269 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
270 }).then(function (access) {
271 return access.createMediaKeys();
272 }).then(function (mediaKeys) {
273 var mp4SessionPromises = kUpdateSessionExceptionsTestCases.m ap(function (testCase) {
274 var mediaKeySession = mediaKeys.createSession();
275 return mediaKeySession.generateRequest(initDataType, ini tData).then(function (result) {
276 return test_exception(testCase, mediaKeySession);
277 });
278 });
279 assert_not_equals(mp4SessionPromises.length, 0);
280 return Promise.all(mp4SessionPromises);
281 }).then(function () {
282 resolve();
283 }).catch(function (error) {
284 reject(error);
285 });
286 })
287 }
288 promise_test(function() {
289 return updateTestExceptions();
290 }, testname + ' test MediaKeySession update() exceptions.');
291
292 function create_close_exception_test(mediaKeys) {
293 var mediaKeySession = mediaKeys.createSession();
294 return mediaKeySession.close().then(function (result) {
295 assert_unreached('close() should not succeed if session uninitia lized');
296 }).catch(function (error) {
297 assert_equals(error.name, 'InvalidStateError');
298 // Return something so the promise resolves.
299 return Promise.resolve();
300 });
301 }
302 function closeTestExceptions(){
303 return new Promise(function(resolve, reject){
304 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
305 assert_true(isTypeSupported, "initDataType not supported");
306 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
307 }).then(function (access) {
308 return access.createMediaKeys();
309 }).then(function (mediaKeys) {
310 return create_close_exception_test(mediaKeys);
311 }).then(function () {
312 resolve();
313 }).catch(function (error) {
314 reject(error);
315 });
316 });
317 }
318 promise_test(function() {
319 return closeTestExceptions();
320 }, testname + ' test MediaKeySession close() exceptions.');
321
322 function create_remove_exception_test(mediaKeys, type, initData) {
323 // remove() on an uninitialized session should fail.
324 var mediaKeySession = mediaKeys.createSession('temporary');
325 return mediaKeySession.remove().then(function (result) {
326 assert_unreached('remove() should not succeed if session uniniti alized');
327 }, function (error) {
328 assert_equals(error.name, 'InvalidStateError');
329 });
330 }
331 function removeTestException(){
332 return new Promise(function(resolve, reject){
333 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
334 assert_true(isTypeSupported, "initDataType not supported");
335 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
336 }).then(function (access) {
337 return access.createMediaKeys();
338 }).then(function (mediaKeys) {
339 return create_remove_exception_test(mediaKeys, initDataType, initData);
340 }).then(function () {
341 resolve();
342 }).catch(function (error) {
343 reject(error);
344 });
345 });
346 }
347 promise_test(function() {
348 return removeTestException();
349 }, testname + ' test MediaKeySession remove() exceptions.');
350
351 // All calls to |func| in this group are supposed to succeed.
352 // However, the spec notes that some things are optional for
353 // Clear Key. In particular, support for persistent sessions
354 // is optional. Since some implementations won't support some
355 // features, a NotSupportedError is treated as a success
356 // if |isNotSupportedAllowed| is true.
357 var kCreateSessionTestCases = [
358 // Use the default sessionType.
359 {
360 func: function (mk) {
361 return mk.createSession();
362 },
363 isNotSupportedAllowed: false
364 },
365 // Try variations of sessionType.
366 {
367 func: function (mk) {
368 return mk.createSession('temporary');
369 },
370 isNotSupportedAllowed: false
371 },
372 {
373 func: function (mk) {
374 return mk.createSession(undefined);
375 },
376 isNotSupportedAllowed: false
377 },
378 {
379 // Since this is optional, some Clear Key implementations
380 // will succeed, others will return a "NotSupportedError".
381 // Both are allowed results.
382 func: function (mk) {
383 return mk.createSession('persistent-license');
384 },
385 isNotSupportedAllowed: true
386 },
387 // Try additional parameter, which should be ignored.
388 {
389 func: function (mk) {
390 return mk.createSession('temporary', 'extra');
391 },
392 isNotSupportedAllowed: false
393 }
394 ];
395
396 // This function checks that calling |testCase.func| creates a
397 // MediaKeySession object with some default values. It also
398 // allows for an NotSupportedError to be generated and treated as a
399 // success, if allowed. See comment above kCreateSessionTestCases.
400 function test_createSession(testCase, mediaKeys) {
401 var mediaKeySession;
402 try {
403 mediaKeySession = testCase.func.call(null, mediaKeys);
404 } catch (e) {
405 assert_true(testCase.isNotSupportedAllowed);
406 return;
407 }
408 assert_equals(typeof mediaKeySession, 'object');
409 assert_equals(typeof mediaKeySession.addEventListener, 'function');
410 assert_equals(typeof mediaKeySession.sessionId, 'string');
411 assert_equals(typeof mediaKeySession.expiration, 'number');
412 assert_equals(typeof mediaKeySession.closed, 'object');
413 assert_equals(typeof mediaKeySession.keyStatuses, 'object');
414 assert_equals(typeof mediaKeySession.onkeystatuseschange, 'object');
415 assert_equals(typeof mediaKeySession.onmessage, 'object');
416 assert_equals(typeof mediaKeySession.generateRequest, 'function');
417 assert_equals(typeof mediaKeySession.load, 'function');
418 assert_equals(typeof mediaKeySession.update, 'function');
419 assert_equals(typeof mediaKeySession.close, 'function');
420 assert_equals(typeof mediaKeySession.remove, 'function');
421 assert_equals(mediaKeySession.sessionId, '');
422 }
423 function createSessionTest(){
424 return new Promise(function(resolve, reject){
425 isInitDataTypeSupported(keysystem, initDataType).then(function (isTy peSupported) {
426 assert_true(isTypeSupported, "initDataType not supported");
427 return navigator.requestMediaKeySystemAccess(keysystem, [con figuration]);
428 }).then(function (access) {
429 return access.createMediaKeys();
430 }).then(function (mediaKeys) {
431 kCreateSessionTestCases.map(function (testCase) {
432 test_createSession(testCase, mediaKeys);
433 });
434 resolve();
435 }).catch(function (error) {
436 reject(error);
437 });
438 })
439 }
440 promise_test(function() {
441 return createSessionTest();
442 }, testname + ' test MediaKeySession attribute syntax.');
443
444
445 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698