4 <title>Test EME syntax
</title>
5 <script src=
"encrypted-media-utils.js"></script>
6 <script src=
"../../resources/testharness.js"></script>
7 <script src=
"../../resources/testharnessreport.js"></script>
12 // Since promises catch any exception and convert it into a
13 // rejected Promise, there is no current way to have the W3C
14 // test framework report a failed test. For now, simply force
15 // a timeout to indicate failure.
16 // FIXME: Once W3C test framework handles Promises, fix this.
18 // This function checks that calling |testCase.func| returns a
19 // rejected Promise with the error.name equal to
20 // |testCase.exception|.
21 function test_exception(testCase
/*...*/)
23 var func
= testCase
.func
;
24 var exception
= testCase
.exception
;
25 var args
= Array
.prototype.slice
.call(arguments
, 1);
27 // Currently blink throws for TypeErrors rather than returning
28 // a rejected promise (http://crbug.com/359386).
29 // FIXME: Remove try/catch once they become failed promises.
31 return func
.apply(null, args
).then(
34 assert_unreached(format_value(func
));
38 assert_equals(error
.name
, exception
, format_value(func
));
39 assert_not_equals(error
.message
, "", format_value(func
));
43 // Only allow 'TypeError' exceptions to be thrown.
44 // Everything else should be a failed promise.
45 assert_equals('TypeError', exception
, format_value(func
));
46 assert_equals(e
.name
, exception
, format_value(func
));
50 var kRequestMediaKeySystemAccessExceptionsTestCases
= [
51 // Too few parameters.
53 exception
: 'TypeError',
54 func: function() { return navigator
.requestMediaKeySystemAccess(); }
57 exception
: 'TypeError',
58 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey'); }
60 // Invalid key systems. Note that JavaScript converts all these
61 // values into strings by calling toString(), so they fail due
62 // to the key system not being supported, not due to the type.
64 exception
: 'NotSupportedError',
65 func: function() { return navigator
.requestMediaKeySystemAccess(null, [{}]); }
68 exception
: 'NotSupportedError',
69 func: function() { return navigator
.requestMediaKeySystemAccess(undefined, [{}]); }
72 exception
: 'NotSupportedError',
73 func: function() { return navigator
.requestMediaKeySystemAccess(1, [{}]); }
76 exception
: 'InvalidAccessError',
77 func: function() { return navigator
.requestMediaKeySystemAccess(new Uint8Array(0), [{}]); }
80 exception
: 'InvalidAccessError',
81 func: function() { return navigator
.requestMediaKeySystemAccess('', [{}]); }
84 exception
: 'NotSupportedError',
85 func: function() { return navigator
.requestMediaKeySystemAccess('unsupported', [{}]); }
89 exception
: 'NotSupportedError',
90 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3\u263A.clearkey', [{}]); }
92 // Empty sequence of MediaKeySystemConfiguration.
94 exception
: 'InvalidAccessError',
95 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', []); }
97 // Invalid sequences of MediaKeySystemConfigurations.
99 exception
: 'TypeError',
100 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', {}); }
103 exception
: 'TypeError',
104 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', "invalid"); }
107 exception
: 'TypeError',
108 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}, 6]); }
111 exception
: 'TypeError',
112 func: function() { return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', ["invalid", "upsupported"]); }
116 async_test(function(test
)
118 var createPromises
= kRequestMediaKeySystemAccessExceptionsTestCases
.map(function(testCase
) {
119 return test_exception(testCase
);
122 Promise
.all(createPromises
).then(function(result
) {
124 }).catch(function(error
) {
125 forceTestFailureFromPromise(test
, error
, 'requestMediaKeySystemAccess() tests failed');
127 }, 'Test Navigator.requestMediaKeySystemAccess() exceptions.');
129 async_test(function(test
)
131 assert_equals(typeof navigator
.requestMediaKeySystemAccess
, 'function');
132 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
133 assert_not_equals(access
, null);
134 assert_equals(typeof access
, 'object');
135 assert_equals(access
.keySystem
, 'org.w3.clearkey');
136 assert_equals(typeof access
.getConfiguration
, 'function');
137 assert_equals(typeof access
.createMediaKeys
, 'function');
139 }).catch(function(error
) {
140 forceTestFailureFromPromise(test
, error
, 'requestMediaKeySystemAccess() tests failed');
142 }, 'Test Navigator.requestMediaKeySystemAccess().');
144 async_test(function(test
)
148 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(result
) {
150 assert_equals(access
.keySystem
, 'org.w3.clearkey');
151 return access
.createMediaKeys();
152 }).then(function(mediaKeys
) {
153 assert_not_equals(mediaKeys
, null);
154 assert_equals(typeof mediaKeys
, 'object');
155 assert_equals(typeof mediaKeys
.createSession
, 'function');
156 assert_equals(typeof mediaKeys
.setServerCertificate
, 'function');
158 // Test creation of a second MediaKeys.
159 // The extra parameter is ignored.
160 return access
.createMediaKeys('extra');
161 }).then(function(mediaKeys
) {
162 assert_not_equals(mediaKeys
, null);
163 assert_equals(typeof mediaKeys
, 'object');
164 assert_equals(typeof mediaKeys
.createSession
, 'function');
165 assert_equals(typeof mediaKeys
.setServerCertificate
, 'function');
167 }).catch(function(error
) {
168 forceTestFailureFromPromise(test
, error
, 'create() tests failed');
170 }, 'Test MediaKeys create().');
172 var kCreateSessionExceptionsTestCases
= [
173 // Tests in this set use a shortened parameter name due to
174 // format_value() only returning the first 60 characters as the
175 // result. With a longer name the first 60 characters is not
176 // enough to determine which test failed.
178 // Invalid parameters.
180 exception
: 'TypeError',
181 func: function(mk
) { return mk
.createSession(); }
184 exception
: 'TypeError',
185 func: function(mk
) { return mk
.createSession(''); }
188 exception
: 'TypeError',
189 func: function(mk
) { return mk
.createSession(null); }
192 exception
: 'TypeError',
193 func: function(mk
) { return mk
.createSession(undefined); }
196 exception
: 'TypeError',
197 func: function(mk
) { return mk
.createSession(1); }
200 exception
: 'TypeError',
201 func: function(mk
) { return mk
.createSession(new Uint8Array(0)); }
204 exception
: 'TypeError',
205 func: function(mk
) { return mk
.createSession('TEMPORARY'); }
209 async_test(function(test
)
211 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
212 return access
.createMediaKeys();
213 }).then(function(mediaKeys
) {
214 var sessionPromises
= kCreateSessionExceptionsTestCases
.map(function(testCase
) {
215 return test_exception(testCase
, mediaKeys
);
218 assert_not_equals(sessionPromises
.length
, 0);
219 return Promise
.all(sessionPromises
);
220 }).then(function(result
) {
222 }).catch(function(error
) {
223 forceTestFailureFromPromise(test
, error
, 'createSession() tests failed');
225 }, 'Test MediaKeys createSession() exceptions.');
227 var kGenerateRequestExceptionsTestCases
= [
228 // Tests in this set use a shortened parameter name due to
229 // format_value() only returning the first 60 characters as the
230 // result. With a longer name the first 60 characters is not
231 // enough to determine which test failed. Even with the
232 // shortened name, the error message for the last couple of
233 // tests is the same.
234 // Too few parameters.
236 exception
: 'TypeError',
237 func: function(mk1
) { return mk1
.createSession().generateRequest(); }
240 exception
: 'TypeError',
241 func: function(mk2
) { return mk2
.createSession().generateRequest(''); }
244 exception
: 'TypeError',
245 func: function(mk3
) { return mk3
.createSession().generateRequest(null); }
248 exception
: 'TypeError',
249 func: function(mk4
) { return mk4
.createSession().generateRequest(undefined); }
252 exception
: 'TypeError',
253 func: function(mk5
) { return mk5
.createSession().generateRequest(1); }
256 exception
: 'TypeError',
257 func: function(mk6
) { return mk6
.createSession().generateRequest(new Uint8Array(0)); }
260 exception
: 'TypeError',
261 func: function(mk7
, _
, initData
) { return mk7
.createSession().generateRequest(initData
); }
263 // Invalid parameters.
265 exception
: 'InvalidAccessError',
266 func: function(mk8
, _
, initData
) { return mk8
.createSession().generateRequest('', initData
); }
268 // Not supported initDataTypes.
270 exception
: 'NotSupportedError',
271 func: function(mk9
, _
, initData
) { return mk9
.createSession().generateRequest(null, initData
); }
274 exception
: 'NotSupportedError',
275 func: function(mk10
, _
, initData
) { return mk10
.createSession().generateRequest(undefined, initData
); }
278 exception
: 'NotSupportedError',
279 func: function(mk11
, _
, initData
) { return mk11
.createSession().generateRequest(1, initData
); }
282 exception
: 'InvalidAccessError',
283 func: function(mk12
, _
, initData
) { return mk12
.createSession().generateRequest(new Uint8Array(0), initData
); }
286 exception
: 'NotSupportedError',
287 func: function(mk13
, _
, initData
) { return mk13
.createSession().generateRequest('unsupported', initData
); }
290 exception
: 'NotSupportedError',
291 func: function(mk14
, _
, initData
) { return mk14
.createSession().generateRequest('video/webm', initData
); }
294 exception
: 'NotSupportedError',
295 func: function(mk15
, _
, initData
) { return mk15
.createSession().generateRequest('video/mp4', initData
); }
298 exception
: 'NotSupportedError',
299 func: function(mk16
, _
, initData
) { return mk16
.createSession().generateRequest('video/cenc', initData
); }
302 exception
: 'NotSupportedError',
303 func: function(mk17
, _
, initData
) { return mk17
.createSession().generateRequest('web\u263A', initData
); }
307 var kTypeSpecificGenerateRequestExceptionsTestCases
= [
308 // Tests in this set use a shortened parameter name due to
309 // format_value() only returning the first 60 characters as the
310 // result. With a longer name the first 60 characters is not
311 // enough to determine which test failed. Even with the
312 // shortened name, the error message for the last couple of
313 // tests is the same.
315 // Too few parameters.
317 exception
: 'TypeError',
318 func: function(mk1
, type
) { return mk1
.createSession().generateRequest(type
); }
320 // Invalid parameters.
322 exception
: 'TypeError',
323 func: function(mk2
, type
) { return mk2
.createSession().generateRequest(type
, ''); }
326 exception
: 'TypeError',
327 func: function(mk3
, type
) { return mk3
.createSession().generateRequest(type
, null); }
330 exception
: 'TypeError',
331 func: function(mk4
, type
) { return mk4
.createSession().generateRequest(type
, undefined); }
334 exception
: 'TypeError',
335 func: function(mk5
, type
) { return mk5
.createSession().generateRequest(type
, 1); }
338 exception
: 'InvalidAccessError',
339 func: function(mk6
, type
) { return mk6
.createSession().generateRequest(type
, new Uint8Array(0)); }
343 async_test(function(test
)
348 isInitDataTypeSupported('webm').then(function(result
) {
349 isWebmSupported
= result
;
350 return isInitDataTypeSupported('cenc');
351 }).then(function(result
) {
352 isCencSupported
= result
;
353 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
354 }).then(function(access
) {
355 return access
.createMediaKeys();
356 }).then(function(mediaKeys
) {
357 var initData
= stringToUint8Array('init data');
358 var sessionPromises
= kGenerateRequestExceptionsTestCases
.map(function(testCase
) {
359 return test_exception(testCase
, mediaKeys
, '', initData
);
362 // Test that WebM sessions generate the expected error, if
364 if (isWebmSupported
) {
365 var WebmSessionPromises
= kTypeSpecificGenerateRequestExceptionsTestCases
.map(function(testCase
) {
366 return test_exception(testCase
, mediaKeys
, 'webm', getInitData('webm'));
368 sessionPromises
= sessionPromises
.concat(WebmSessionPromises
);
371 // Repeat for MP4, if supported.
372 if (isCencSupported
) {
373 var mp4SessionPromises
= kTypeSpecificGenerateRequestExceptionsTestCases
.map(function(testCase
) {
374 return test_exception(testCase
, mediaKeys
, 'cenc', getInitData('cenc'));
376 sessionPromises
= sessionPromises
.concat(mp4SessionPromises
);
379 assert_not_equals(sessionPromises
.length
, 0);
380 return Promise
.all(sessionPromises
);
381 }).then(function(result
) {
383 }).catch(function(error
) {
384 forceTestFailureFromPromise(test
, error
, 'generateRequest() tests failed');
386 }, 'Test MediaKeys generateRequest() exceptions.');
388 var kLoadExceptionsTestCases
= [
389 // Too few parameters.
391 exception
: 'TypeError',
392 func: function(mk1
) { return mk1
.createSession('temporary').load(); }
394 // 'temporary' sessions are never allowed, so always return
395 // 'InvalidAccessError'.
397 exception
: 'InvalidAccessError',
398 func: function(mk3
) { return mk3
.createSession('temporary').load(''); }
401 exception
: 'InvalidAccessError',
402 func: function(mk4
) { return mk4
.createSession('temporary').load(1); }
405 exception
: 'InvalidAccessError',
406 func: function(mk5
) { return mk5
.createSession('temporary').load('!@#$%^&*()'); }
409 exception
: 'InvalidAccessError',
410 func: function(mk6
) { return mk6
.createSession('temporary').load('1234'); }
414 async_test(function(test
)
416 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
417 return access
.createMediaKeys();
418 }).then(function(mediaKeys
) {
419 var initData
= stringToUint8Array('init data');
420 var sessionPromises
= kLoadExceptionsTestCases
.map(function(testCase
) {
421 return test_exception(testCase
, mediaKeys
);
424 assert_not_equals(sessionPromises
.length
, 0);
425 return Promise
.all(sessionPromises
);
426 }).then(function(result
) {
428 }).catch(function(error
) {
429 forceTestFailureFromPromise(test
, error
, 'load() tests failed');
431 }, 'Test MediaKeys load() exceptions.');
433 // All calls to |func| in this group are supposed to succeed.
434 // However, the spec notes that some things are optional for
435 // Clear Key. In particular, support for persistent sessions
436 // is optional. Since some implementations won't support some
437 // features, a NotSupportedError is treated as a success
438 // if |isNotSupportedAllowed| is true.
439 var kCreateSessionTestCases
= [
440 // Use the default sessionType.
442 func: function(mk
) { return mk
.createSession(); },
443 isNotSupportedAllowed
: false
445 // Try variations of sessionType.
447 func: function(mk
) { return mk
.createSession('temporary'); },
448 isNotSupportedAllowed
: false
451 func: function(mk
) { return mk
.createSession(undefined); },
452 isNotSupportedAllowed
: false
455 // Since this is optional, some Clear Key implementations
456 // will succeed, others will return a "NotSupportedError".
457 // Both are allowed results.
458 func: function(mk
) { return mk
.createSession('persistent-license'); },
459 isNotSupportedAllowed
: true
461 // Try additional parameter, which should be ignored.
463 func: function(mk
) { return mk
.createSession('temporary', 'extra'); },
464 isNotSupportedAllowed
: false
468 // This function checks that calling |testCase.func| creates a
469 // MediaKeySession object with some default values. It also
470 // allows for an NotSupportedError to be generated and treated as a
471 // success, if allowed. See comment above kCreateSessionTestCases.
472 function test_createSession(testCase
, mediaKeys
)
476 mediaKeySession
= testCase
.func
.call(null, mediaKeys
);
478 assert_true(testCase
.isNotSupportedAllowed
);
482 // FIXME: Update this set of tests when done
483 // implementing the latest spec.
484 assert_equals(typeof mediaKeySession
, 'object');
485 assert_equals(typeof mediaKeySession
.addEventListener
, 'function');
486 assert_equals(typeof mediaKeySession
.generateRequest
, 'function');
487 assert_equals(typeof mediaKeySession
.update
, 'function');
488 assert_equals(typeof mediaKeySession
.close
, 'function');
489 assert_equals(typeof mediaKeySession
.remove
, 'function');
490 assert_equals(mediaKeySession
.sessionId
, '');
491 assert_equals(typeof mediaKeySession
.sessionId
, 'string');
492 assert_equals(typeof mediaKeySession
.onopen
, 'undefined');
493 assert_equals(typeof mediaKeySession
.onmessage
, 'undefined');
494 assert_equals(typeof mediaKeySession
.onclose
, 'undefined');
495 assert_equals(typeof mediaKeySession
.onerror
, 'undefined');
498 async_test(function(test
)
500 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
501 return access
.createMediaKeys();
502 }).then(function(mediaKeys
) {
503 kCreateSessionTestCases
.map(function(testCase
) {
504 test_createSession(testCase
, mediaKeys
);
507 }).catch(function(error
) {
508 forceTestFailureFromPromise(test
, error
, 'createSession() tests failed');
510 }, 'Test MediaKeys createSession().');
512 // This function checks that calling generateRequest() works for
513 // various sessions. |testCase.func| creates a MediaKeySession
514 // object, and then generateRequest() is called on that object. It
515 // allows for an NotSupportedError to be generated and treated as a
516 // success, if allowed. See comment above kCreateSessionTestCases.
517 function test_generateRequest(testCase
, mediaKeys
, type
, initData
)
520 var mediaKeySession
= testCase
.func
.call(null, mediaKeys
);
521 return mediaKeySession
.generateRequest(type
, initData
);
523 assert_true(testCase
.isNotSupportedAllowed
);
527 async_test(function(test
)
532 isInitDataTypeSupported('webm').then(function(result
) {
533 isWebmSupported
= result
;
534 return isInitDataTypeSupported('cenc');
535 }).then(function(result
) {
536 isCencSupported
= result
;
537 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
538 }).then(function(access
) {
539 return access
.createMediaKeys();
540 }).then(function(mediaKeys
) {
541 var sessionPromises
= [];
543 // Test that WebM sessions generate the expected error, if
545 if (isWebmSupported
) {
546 var WebmSessionPromises
= kCreateSessionTestCases
.map(function(testCase
) {
547 return test_generateRequest(testCase
, mediaKeys
, 'webm', getInitData('webm'));
549 sessionPromises
= sessionPromises
.concat(WebmSessionPromises
);
552 // Repeat for MP4, if supported.
553 if (isCencSupported
) {
554 var mp4SessionPromises
= kCreateSessionTestCases
.map(function(testCase
) {
555 return test_generateRequest(testCase
, mediaKeys
, 'cenc', getInitData('cenc'));
557 sessionPromises
= sessionPromises
.concat(mp4SessionPromises
);
560 assert_not_equals(sessionPromises
.length
, 0);
561 return Promise
.all(sessionPromises
);
562 }).then(function(result
) {
564 }).catch(function(error
) {
565 forceTestFailureFromPromise(test
, error
, 'generateRequest() tests failed');
567 }, 'Test MediaKeys generateRequest().');
569 var kUpdateSessionExceptionsTestCases
= [
570 // Tests in this set use a shortened parameter name due to
571 // format_value() only returning the first 60 characters as the
572 // result. With a longer name (mediaKeySession) the first 60
573 // characters is not enough to determine which test failed.
575 // Too few parameters.
577 exception
: 'TypeError',
578 func: function(s
) { return s
.update(); }
580 // Invalid parameters.
582 exception
: 'TypeError',
583 func: function(s
) { return s
.update(''); }
586 exception
: 'TypeError',
587 func: function(s
) { return s
.update(null); }
590 exception
: 'TypeError',
591 func: function(s
) { return s
.update(undefined); }
594 exception
: 'TypeError',
595 func: function(s
) { return s
.update(1); }
598 exception
: 'InvalidAccessError',
599 func: function(s
) { return s
.update(new Uint8Array(0)); }
603 async_test(function(test
)
608 isInitDataTypeSupported('webm').then(function(result
) {
609 isWebmSupported
= result
;
610 return isInitDataTypeSupported('cenc');
611 }).then(function(result
) {
612 isCencSupported
= result
;
613 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
614 }).then(function(access
) {
615 return access
.createMediaKeys();
616 }).then(function(mediaKeys
) {
619 if (isWebmSupported
) {
620 var WebmSessionPromises
= kUpdateSessionExceptionsTestCases
.map(function(testCase
) {
621 var mediaKeySession
= mediaKeys
.createSession();
622 return mediaKeySession
.generateRequest('webm', getInitData('webm')).then(function(result
) {
623 return test_exception(testCase
, mediaKeySession
);
626 promises
= promises
.concat(WebmSessionPromises
);
629 if (isCencSupported
) {
630 var mp4SessionPromises
= kUpdateSessionExceptionsTestCases
.map(function(testCase
) {
631 var mediaKeySession
= mediaKeys
.createSession();
632 return mediaKeySession
.generateRequest('cenc', getInitData('cenc')).then(function(result
) {
633 return test_exception(testCase
, mediaKeySession
);
636 promises
= promises
.concat(mp4SessionPromises
);
639 assert_not_equals(promises
.length
, 0);
640 return Promise
.all(promises
);
641 }).then(function(result
) {
643 }).catch(function(error
) {
644 forceTestFailureFromPromise(test
, error
, 'update() tests failed');
646 }, 'Test MediaKeySession update() exceptions.');
648 function create_update_test(mediaKeys
, type
, initData
)
650 var mediaKeySession
= mediaKeys
.createSession();
651 var promise
= mediaKeySession
.generateRequest(type
, initData
).then(function(result
) {
652 var validLicense
= stringToUint8Array(createJWKSet(createJWK(stringToUint8Array('123'), stringToUint8Array('1234567890abcdef'))));
653 return mediaKeySession
.update(validLicense
);
654 }).then(function(result
) {
655 // Call update() with a different license and an extra
656 // parameter. The extra parameter is ignored.
657 var validLicense
= stringToUint8Array(createJWKSet(createJWK(stringToUint8Array('4567890'), stringToUint8Array('01234567890abcde'))));
658 return mediaKeySession
.update(validLicense
, 'extra');
663 async_test(function(test
)
668 isInitDataTypeSupported('webm').then(function(result
) {
669 isWebmSupported
= result
;
670 return isInitDataTypeSupported('cenc');
671 }).then(function(result
) {
672 isCencSupported
= result
;
673 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
674 }).then(function(access
) {
675 return access
.createMediaKeys();
676 }).then(function(mediaKeys
) {
679 if (isWebmSupported
) {
680 promises
.push(create_update_test(mediaKeys
, 'webm', getInitData('webm')));
683 if (isCencSupported
) {
684 promises
.push(create_update_test(mediaKeys
, 'cenc', getInitData('cenc')));
687 assert_not_equals(promises
.length
, 0);
688 return Promise
.all(promises
);
689 }).then(function(result
) {
691 }).catch(function(error
) {
692 forceTestFailureFromPromise(test
, error
, 'update() tests failed');
694 }, 'Test MediaKeySession update().');
696 function create_close_exception_test(mediaKeys
, type
, initData
)
698 var mediaKeySession
= mediaKeys
.createSession();
699 return mediaKeySession
.close().then(function(result
) {
700 assert_unreached('close() should not succeed if session uninitialized');
701 }).catch(function(error
) {
702 assert_equals(error
.name
, 'InvalidStateError');
703 // Return something so the promise resolves.
704 return Promise
.resolve();
708 async_test(function(test
)
713 isInitDataTypeSupported('webm').then(function(result
) {
714 isWebmSupported
= result
;
715 return isInitDataTypeSupported('cenc');
716 }).then(function(result
) {
717 isCencSupported
= result
;
718 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
719 }).then(function(access
) {
720 return access
.createMediaKeys();
721 }).then(function(mediaKeys
) {
724 if (isWebmSupported
) {
725 promises
.push(create_close_exception_test(mediaKeys
, 'webm', getInitData('webm')));
728 if (isCencSupported
) {
729 promises
.push(create_close_exception_test(mediaKeys
, 'cenc', getInitData('cenc')));
732 assert_not_equals(promises
.length
, 0);
733 return Promise
.all(promises
);
734 }).then(function(result
) {
736 }).catch(function(error
) {
737 forceTestFailureFromPromise(test
, error
, 'close() exception tests failed');
739 }, 'Test MediaKeySession close() exceptions.');
742 function create_close_test(mediaKeys
, type
, initData
)
744 var mediaKeySession
= mediaKeys
.createSession();
745 var promise
= mediaKeySession
.generateRequest(type
, initData
).then(function(result
) {
746 return mediaKeySession
.close();
747 }).then(function(result
) {
748 // Call close() again with an extra parameter. The extra
749 // parameter is ignored.
750 return mediaKeySession
.close('extra');
755 async_test(function(test
)
760 isInitDataTypeSupported('webm').then(function(result
) {
761 isWebmSupported
= result
;
762 return isInitDataTypeSupported('cenc');
763 }).then(function(result
) {
764 isCencSupported
= result
;
765 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
766 }).then(function(access
) {
767 return access
.createMediaKeys();
768 }).then(function(mediaKeys
) {
771 if (isWebmSupported
) {
772 promises
.push(create_close_test(mediaKeys
, 'webm', getInitData('webm')));
775 if (isCencSupported
) {
776 promises
.push(create_close_test(mediaKeys
, 'cenc', getInitData('cenc')));
779 assert_not_equals(promises
.length
, 0);
780 return Promise
.all(promises
);
781 }).then(function(result
) {
783 }).catch(function(error
) {
784 forceTestFailureFromPromise(test
, error
, 'close() tests failed');
786 }, 'Test MediaKeySession close().');
788 function create_remove_exception_test(mediaKeys
, type
, initData
)
790 // remove() on an uninitialized session should fail.
791 var mediaKeySession
= mediaKeys
.createSession('temporary');
792 return mediaKeySession
.remove().then(function(result
) {
793 assert_unreached('remove() should not succeed if session uninitialized');
795 assert_equals(error
.name
, 'InvalidStateError');
797 // remove() on a temporary session should fail.
798 return mediaKeySession
.generateRequest(type
, initData
);
799 }).then(function(result
) {
800 return mediaKeySession
.remove();
801 }).then(function(result
) {
802 assert_unreached('remove() should not succeed for temporary sessions');
804 assert_equals(error
.name
, 'InvalidAccessError');
808 async_test(function(test
)
813 isInitDataTypeSupported('webm').then(function(result
) {
814 isWebmSupported
= result
;
815 return isInitDataTypeSupported('cenc');
816 }).then(function(result
) {
817 isCencSupported
= result
;
818 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
819 }).then(function(access
) {
820 return access
.createMediaKeys();
821 }).then(function(mediaKeys
) {
824 if (isWebmSupported
) {
825 promises
.push(create_remove_exception_test(mediaKeys
, 'webm', getInitData('webm')));
828 if (isCencSupported
) {
829 promises
.push(create_remove_exception_test(mediaKeys
, 'cenc', getInitData('cenc')));
832 assert_not_equals(promises
.length
, 0);
833 return Promise
.all(promises
);
834 }).then(function(result
) {
836 }).catch(function(error
) {
837 forceTestFailureFromPromise(test
, error
, 'remove() exception tests failed');
839 }, 'Test MediaKeySession remove() exceptions.');
841 function create_remove_test(mediaKeys
, type
, initData
)
843 // Clear Key may not support persistent-license sessions.
846 mediaKeySession
= mediaKeys
.createSession('persistent-license');
848 // Not supported, so return a resolved promise.
849 assert_equals(error
.name
, 'NotSupportedError');
850 return Promise
.resolve();
852 return mediaKeySession
.generateRequest(type
, initData
).then(function(result
) {
853 return mediaKeySession
.remove();
857 async_test(function(test
)
862 isInitDataTypeSupported('webm').then(function(result
) {
863 isWebmSupported
= result
;
864 return isInitDataTypeSupported('cenc');
865 }).then(function(result
) {
866 isCencSupported
= result
;
867 return navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
868 }).then(function(access
) {
869 return access
.createMediaKeys();
870 }).then(function(mediaKeys
) {
873 if (isWebmSupported
) {
874 promises
.push(create_remove_test(mediaKeys
, 'webm', getInitData('webm')));
877 if (isCencSupported
) {
878 promises
.push(create_remove_test(mediaKeys
, 'cenc', getInitData('cenc')));
881 assert_not_equals(promises
.length
, 0);
882 return Promise
.all(promises
);
883 }).then(function(result
) {
885 }).catch(function(error
) {
886 forceTestFailureFromPromise(test
, error
, 'remove() tests failed');
888 }, 'Test MediaKeySession remove().');
890 var kSetServerCertificateExceptionsTestCases
= [
891 // Too few parameters.
893 exception
: 'TypeError',
894 func: function(mk
) { return mk
.setServerCertificate(); }
896 // Invalid parameters.
898 exception
: 'TypeError',
899 func: function(mk
) { return mk
.setServerCertificate(''); }
902 exception
: 'TypeError',
903 func: function(mk
) { return mk
.setServerCertificate(null); }
906 exception
: 'TypeError',
907 func: function(mk
) { return mk
.setServerCertificate(undefined); }
910 exception
: 'TypeError',
911 func: function(mk
) { return mk
.setServerCertificate(1); }
915 exception
: 'InvalidAccessError',
916 func: function(mk
) { return mk
.setServerCertificate(new Uint8Array(0)); }
918 // Valid calls, but not supported by Clear Key.
920 exception
: 'NotSupportedError',
921 func: function(mk
) { var cert
= new Uint8Array(200); assert_true(ArrayBuffer
.isView(cert
)); return mk
.setServerCertificate(cert
); }
924 // Pass in ArrayBuffer
925 exception
: 'NotSupportedError',
926 func: function(mk
) { var cert
= new Uint8Array(200); assert_false(ArrayBuffer
.isView(cert
.buffer
)); return mk
.setServerCertificate(cert
.buffer
); }
930 async_test(function(test
)
932 navigator
.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access
) {
933 return access
.createMediaKeys();
934 }).then(function(mediaKeys
) {
935 var promises
= kSetServerCertificateExceptionsTestCases
.map(function(testCase
) {
936 return test_exception(testCase
, mediaKeys
);
939 assert_not_equals(promises
.length
, 0);
940 return Promise
.all(promises
);
941 }).then(function(result
) {
943 }).catch(function(error
) {
944 forceTestFailureFromPromise(test
, error
, 'setServerCertificate() exception tests failed');
946 }, 'Test MediaKeys setServerCertificate() exceptions.');
948 // FIXME: Add test for successful setServerCertificate(). Note that
949 // Clear Key does not support it.
951 // FIXME: Add syntax checks for MediaKeys.IsTypeSupported().
952 // FIXME: Add syntax checks for MediaKeyError and MediaKeySession events.
953 // FIXME: Add HTMLMediaElement syntax checks, e.g. setMediaKeys, mediakeys, onencrypted.