summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/encrypted-media/Google/migrated_to_root_disabled/encrypted-media-syntax.html
blob: 5c4c667cb999734a2aba488d9cd8b8deb0e87a10 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>Test EME syntax</title>
        <script src="encrypted-media-utils.js"></script>
<!--
        Test has been migrated to the root directory and is being disabled here.
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
-->
    </head>
    <body>
        <script>
            // Since promises catch any exception and convert it into a
            // rejected Promise, there is no current way to have the W3C
            // test framework report a failed test. For now, simply force
            // a timeout to indicate failure.
            // FIXME: Once W3C test framework handles Promises, fix this.

            // This function checks that calling |testCase.func| returns a
            // rejected Promise with the error.name equal to
            // |testCase.exception|.
            function test_exception(testCase /*...*/)
            {
                var func = testCase.func;
                var exception = testCase.exception;
                var args = Array.prototype.slice.call(arguments, 1);

                // Currently blink throws for TypeErrors rather than returning
                // a rejected promise (http://crbug.com/359386).
                // FIXME: Remove try/catch once they become failed promises.
                try {
                    return func.apply(null, args).then(
                        function(result)
                        {
                            assert_unreached(format_value(func));
                        },
                        function(error)
                        {
                            assert_equals(error.name, exception, format_value(func));
                            assert_not_equals(error.message, "", format_value(func));
                        }
                    );
                } catch (e) {
                    // Only allow 'TypeError' exceptions to be thrown.
                    // Everything else should be a failed promise.
                    assert_equals('TypeError', exception, format_value(func));
                    assert_equals(e.name, exception, format_value(func));
                }
            }

            var kRequestMediaKeySystemAccessExceptionsTestCases = [
                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess(); }
                },
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey'); }
                },
                // Invalid key systems. Note that JavaScript converts all these
                // values into strings by calling toString(), so they fail due
                // to the key system not being supported, not due to the type.
                {
                    exception: 'NotSupportedError',
                    func: function() { return navigator.requestMediaKeySystemAccess(null, [{}]); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function() { return navigator.requestMediaKeySystemAccess(undefined, [{}]); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function() { return navigator.requestMediaKeySystemAccess(1, [{}]); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function() { return navigator.requestMediaKeySystemAccess(new Uint8Array(0), [{}]); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function() { return navigator.requestMediaKeySystemAccess('', [{}]); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function() { return navigator.requestMediaKeySystemAccess('unsupported', [{}]); }
                },
                // Non-ASCII names.
                {
                    exception: 'NotSupportedError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3\u263A.clearkey', [{}]); }
                },
                // Empty sequence of MediaKeySystemConfiguration.
                {
                    exception: 'InvalidAccessError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey', []); }
                },
                // Invalid sequences of MediaKeySystemConfigurations.
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey', {}); }
                },
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey', "invalid"); }
                },
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}, 6]); }
                },
                {
                    exception: 'TypeError',
                    func: function() { return navigator.requestMediaKeySystemAccess('org.w3.clearkey', ["invalid", "upsupported"]); }
                }
            ];

            async_test(function(test)
            {
                var createPromises = kRequestMediaKeySystemAccessExceptionsTestCases.map(function(testCase) {
                    return test_exception(testCase);
                });

                Promise.all(createPromises).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'requestMediaKeySystemAccess() tests failed');
                });
            }, 'Test Navigator.requestMediaKeySystemAccess() exceptions.');

            async_test(function(test)
            {
                assert_equals(typeof navigator.requestMediaKeySystemAccess, 'function');
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    assert_not_equals(access, null);
                    assert_equals(typeof access, 'object');
                    assert_equals(access.keySystem, 'org.w3.clearkey');
                    assert_equals(typeof access.getConfiguration, 'function');
                    assert_equals(typeof access.createMediaKeys, 'function');
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'requestMediaKeySystemAccess() tests failed');
                });
            }, 'Test Navigator.requestMediaKeySystemAccess().');

            async_test(function(test)
            {
                var access;

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(result) {
                    access = result;
                    assert_equals(access.keySystem, 'org.w3.clearkey');
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    assert_not_equals(mediaKeys, null);
                    assert_equals(typeof mediaKeys, 'object');
                    assert_equals(typeof mediaKeys.createSession, 'function');
                    assert_equals(typeof mediaKeys.setServerCertificate, 'function');

                    // Test creation of a second MediaKeys.
                    // The extra parameter is ignored.
                    return access.createMediaKeys('extra');
                }).then(function(mediaKeys) {
                    assert_not_equals(mediaKeys, null);
                    assert_equals(typeof mediaKeys, 'object');
                    assert_equals(typeof mediaKeys.createSession, 'function');
                    assert_equals(typeof mediaKeys.setServerCertificate, 'function');
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'create() tests failed');
                });
            }, 'Test MediaKeySystemAccess createMediaKeys().');

            var kCreateSessionExceptionsTestCases = [
                // Tests in this set use a shortened parameter name due to
                // format_value() only returning the first 60 characters as the
                // result. With a longer name the first 60 characters is not
                // enough to determine which test failed.

                // Invalid parameters.
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(''); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(null); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(undefined); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(1); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession(new Uint8Array(0)); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.createSession('TEMPORARY'); }
                }
            ];

            // This function checks that calling createSession() with an
            // unsupported session type doesn't create a MediaKeySession object.
            // Since requestMediaKeySystemAccess() is called without specifying
            // persistent sessions, only temporary sessions will be allowed.
            function test_unsupported_sessionType(mediaKeys)
            {
                var mediaKeySession = 'test';

                try {
                    mediaKeySession = mediaKeys.createSession('persistent-license');
                    assert_unreached('Session should not be created.');
                } catch (error) {
                    assert_equals(error.name, 'NotSupportedError');
                    assert_not_equals(error.message, "");

                    // Since createSession() failed, |mediaKeySession| is not
                    // touched.
                    assert_equals(mediaKeySession, 'test');
                }
            }

            async_test(function(test)
            {
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var sessionPromises = kCreateSessionExceptionsTestCases.map(function(testCase) {
                        return test_exception(testCase, mediaKeys);
                    });
                    sessionPromises = sessionPromises.concat(test_unsupported_sessionType(mediaKeys));

                    assert_not_equals(sessionPromises.length, 0);
                    return Promise.all(sessionPromises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'createSession() tests failed');
                });
            }, 'Test MediaKeys createSession() exceptions.');

            var kGenerateRequestExceptionsTestCases = [
                // Tests in this set use a shortened parameter name due to
                // format_value() only returning the first 60 characters as the
                // result. With a longer name the first 60 characters is not
                // enough to determine which test failed. Even with the
                // shortened name, the error message for the last couple of
                // tests is the same.
                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function(mk1) { return mk1.createSession().generateRequest(); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk2) { return mk2.createSession().generateRequest(''); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk3) { return mk3.createSession().generateRequest(null); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk4) { return mk4.createSession().generateRequest(undefined); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk5) { return mk5.createSession().generateRequest(1); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk6) { return mk6.createSession().generateRequest(new Uint8Array(0)); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk7, _, initData) { return mk7.createSession().generateRequest(initData); }
                },
                // Invalid parameters.
                {
                    exception: 'InvalidAccessError',
                    func: function(mk8, _, initData) { return mk8.createSession().generateRequest('', initData); }
                },
                // Not supported initDataTypes.
                {
                    exception: 'NotSupportedError',
                    func: function(mk9, _, initData) { return mk9.createSession().generateRequest(null, initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk10, _, initData) { return mk10.createSession().generateRequest(undefined, initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk11, _, initData) { return mk11.createSession().generateRequest(1, initData); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(mk12, _, initData) { return mk12.createSession().generateRequest(new Uint8Array(0), initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk13, _, initData) { return mk13.createSession().generateRequest('unsupported', initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk14, _, initData) { return mk14.createSession().generateRequest('video/webm', initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk15, _, initData) { return mk15.createSession().generateRequest('video/mp4', initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk16, _, initData) { return mk16.createSession().generateRequest('video/cenc', initData); }
                },
                {
                    exception: 'NotSupportedError',
                    func: function(mk17, _, initData) { return mk17.createSession().generateRequest('web\u263A', initData); }
                }
            ];

            var kTypeSpecificGenerateRequestExceptionsTestCases = [
                // Tests in this set use a shortened parameter name due to
                // format_value() only returning the first 60 characters as the
                // result. With a longer name the first 60 characters is not
                // enough to determine which test failed. Even with the
                // shortened name, the error message for the last couple of
                // tests is the same.

                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function(mk1, type) { return mk1.createSession().generateRequest(type); }
                },
                // Invalid parameters.
                {
                    exception: 'TypeError',
                    func: function(mk2, type) { return mk2.createSession().generateRequest(type, ''); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk3, type) { return mk3.createSession().generateRequest(type, null); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk4, type) { return mk4.createSession().generateRequest(type, undefined); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk5, type) { return mk5.createSession().generateRequest(type, 1); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(mk6, type) { return mk6.createSession().generateRequest(type, new Uint8Array(0)); }
                }
            ];

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var initData = stringToUint8Array('init data');
                    var sessionPromises = kGenerateRequestExceptionsTestCases.map(function(testCase) {
                        return test_exception(testCase, mediaKeys, '', initData);
                    });

                    // Test that WebM sessions generate the expected error, if
                    // supported.
                    if (isWebmSupported) {
                        var WebmSessionPromises = kTypeSpecificGenerateRequestExceptionsTestCases.map(function(testCase) {
                            return test_exception(testCase, mediaKeys, 'webm', getInitData('webm'));
                        });
                        sessionPromises = sessionPromises.concat(WebmSessionPromises);
                    }

                    // Repeat for MP4, if supported.
                    if (isCencSupported) {
                        var mp4SessionPromises = kTypeSpecificGenerateRequestExceptionsTestCases.map(function(testCase) {
                            return test_exception(testCase, mediaKeys, 'cenc', getInitData('cenc'));
                        });
                        sessionPromises = sessionPromises.concat(mp4SessionPromises);
                    }

                    assert_not_equals(sessionPromises.length, 0);
                    return Promise.all(sessionPromises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'generateRequest() tests failed');
                });
            }, 'Test MediaKeys generateRequest() exceptions.');

            var kLoadExceptionsTestCases = [
                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function(mk1) { return mk1.createSession('temporary').load(); }
                },
                // 'temporary' sessions are never allowed, so always return
                // 'InvalidAccessError'.
                {
                    exception: 'InvalidAccessError',
                    func: function(mk3) { return mk3.createSession('temporary').load(''); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(mk4) { return mk4.createSession('temporary').load(1); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(mk5) { return mk5.createSession('temporary').load('!@#$%^&*()'); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(mk6) { return mk6.createSession('temporary').load('1234'); }
                }
            ];

            async_test(function(test)
            {
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var initData = stringToUint8Array('init data');
                    var sessionPromises = kLoadExceptionsTestCases.map(function(testCase) {
                        return test_exception(testCase, mediaKeys);
                    });

                    assert_not_equals(sessionPromises.length, 0);
                    return Promise.all(sessionPromises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'load() tests failed');
                });
            }, 'Test MediaKeys load() exceptions.');

            // All calls to |func| in this group are supposed to succeed.
            // However, the spec notes that some things are optional for
            // Clear Key. In particular, support for persistent sessions
            // is optional. Since some implementations won't support some
            // features, a NotSupportedError is treated as a success
            // if |isNotSupportedAllowed| is true.
            var kCreateSessionTestCases = [
                // Use the default sessionType.
                {
                    func: function(mk) { return mk.createSession(); },
                    isNotSupportedAllowed: false
                },
                // Try variations of sessionType.
                {
                    func: function(mk) { return mk.createSession('temporary'); },
                    isNotSupportedAllowed: false
                },
                {
                    func: function(mk) { return mk.createSession(undefined); },
                    isNotSupportedAllowed: false
                },
                {
                    // Since this is optional, some Clear Key implementations
                    // will succeed, others will return a "NotSupportedError".
                    // Both are allowed results.
                    func: function(mk) { return mk.createSession('persistent-license'); },
                    isNotSupportedAllowed: true
                },
                // Try additional parameter, which should be ignored.
                {
                    func: function(mk) { return mk.createSession('temporary', 'extra'); },
                    isNotSupportedAllowed: false
                }
            ];

            // This function checks that calling |testCase.func| creates a
            // MediaKeySession object with some default values. It also
            // allows for an NotSupportedError to be generated and treated as a
            // success, if allowed. See comment above kCreateSessionTestCases.
            function test_createSession(testCase, mediaKeys)
            {
                var mediaKeySession;
                try {
                    mediaKeySession = testCase.func.call(null, mediaKeys);
                } catch (e) {
                    assert_true(testCase.isNotSupportedAllowed);
                    return;
                }

                assert_equals(typeof mediaKeySession, 'object');
                assert_equals(typeof mediaKeySession.addEventListener, 'function');
                assert_equals(typeof mediaKeySession.sessionId, 'string');
                assert_equals(typeof mediaKeySession.expiration, 'number');
                assert_equals(typeof mediaKeySession.closed, 'object');
                assert_equals(typeof mediaKeySession.keyStatuses, 'object');
                assert_equals(typeof mediaKeySession.onkeystatuseschange, 'object');
                assert_equals(typeof mediaKeySession.onmessage, 'object');
                assert_equals(typeof mediaKeySession.generateRequest, 'function');
                assert_equals(typeof mediaKeySession.load, 'function');
                assert_equals(typeof mediaKeySession.update, 'function');
                assert_equals(typeof mediaKeySession.close, 'function');
                assert_equals(typeof mediaKeySession.remove, 'function');
                assert_equals(mediaKeySession.sessionId, '');
                assert_equals(typeof mediaKeySession.onopen, 'undefined');
                assert_equals(typeof mediaKeySession.onclose, 'undefined');
                assert_equals(typeof mediaKeySession.onerror, 'undefined');
            }

            async_test(function(test)
            {
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    kCreateSessionTestCases.map(function(testCase) {
                        test_createSession(testCase, mediaKeys);
                    });
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'createSession() tests failed');
                });
            }, 'Test MediaKeys createSession().');

            // This function checks that calling generateRequest() works for
            // various sessions. |testCase.func| creates a MediaKeySession
            // object, and then generateRequest() is called on that object. It
            // allows for an NotSupportedError to be generated and treated as a
            // success, if allowed. See comment above kCreateSessionTestCases.
            function test_generateRequest(testCase, mediaKeys, type, initData)
            {
                try {
                    var mediaKeySession = testCase.func.call(null, mediaKeys);
                    return mediaKeySession.generateRequest(type, initData);
                } catch (e) {
                    assert_true(testCase.isNotSupportedAllowed);
                }
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var sessionPromises = [];

                    // Test that WebM sessions generate the expected error, if
                    // supported.
                    if (isWebmSupported) {
                        var WebmSessionPromises = kCreateSessionTestCases.map(function(testCase) {
                            return test_generateRequest(testCase, mediaKeys, 'webm', getInitData('webm'));
                        });
                        sessionPromises = sessionPromises.concat(WebmSessionPromises);
                    }

                    // Repeat for MP4, if supported.
                    if (isCencSupported) {
                        var mp4SessionPromises = kCreateSessionTestCases.map(function(testCase) {
                            return test_generateRequest(testCase, mediaKeys, 'cenc', getInitData('cenc'));
                        });
                        sessionPromises = sessionPromises.concat(mp4SessionPromises);
                    }

                    assert_not_equals(sessionPromises.length, 0);
                    return Promise.all(sessionPromises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'generateRequest() tests failed');
                });
            }, 'Test MediaKeys generateRequest().');

            var kUpdateSessionExceptionsTestCases = [
                // Tests in this set use a shortened parameter name due to
                // format_value() only returning the first 60 characters as the
                // result. With a longer name (mediaKeySession) the first 60
                // characters is not enough to determine which test failed.

                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function(s) { return s.update(); }
                },
                // Invalid parameters.
                {
                    exception: 'TypeError',
                    func: function(s) { return s.update(''); }
                },
                {
                    exception: 'TypeError',
                    func: function(s) { return s.update(null); }
                },
                {
                    exception: 'TypeError',
                    func: function(s) { return s.update(undefined); }
                },
                {
                    exception: 'TypeError',
                    func: function(s) { return s.update(1); }
                },
                {
                    exception: 'InvalidAccessError',
                    func: function(s) { return s.update(new Uint8Array(0)); }
                }
            ];

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = [];

                    if (isWebmSupported) {
                        var WebmSessionPromises = kUpdateSessionExceptionsTestCases.map(function(testCase) {
                            var mediaKeySession = mediaKeys.createSession();
                            return mediaKeySession.generateRequest('webm', getInitData('webm')).then(function(result) {
                                return test_exception(testCase, mediaKeySession);
                            });
                        });
                        promises = promises.concat(WebmSessionPromises);
                    }

                    if (isCencSupported) {
                        var mp4SessionPromises = kUpdateSessionExceptionsTestCases.map(function(testCase) {
                          var mediaKeySession = mediaKeys.createSession();
                          return mediaKeySession.generateRequest('cenc', getInitData('cenc')).then(function(result) {
                              return test_exception(testCase, mediaKeySession);
                          });
                      });
                      promises = promises.concat(mp4SessionPromises);
                    }

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'update() tests failed');
                });
            }, 'Test MediaKeySession update() exceptions.');

            function create_update_test(mediaKeys, type, initData)
            {
                var mediaKeySession = mediaKeys.createSession();
                var promise = mediaKeySession.generateRequest(type, initData).then(function(result) {
                    var validLicense = stringToUint8Array(createJWKSet(createJWK(stringToUint8Array('123'), stringToUint8Array('1234567890abcdef'))));
                    return mediaKeySession.update(validLicense);
                }).then(function(result) {
                    // Call update() with a different license and an extra
                    // parameter. The extra parameter is ignored.
                    var validLicense = stringToUint8Array(createJWKSet(createJWK(stringToUint8Array('4567890'), stringToUint8Array('01234567890abcde'))));
                    return mediaKeySession.update(validLicense, 'extra');
                });
                return promise;
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                  var promises = [];

                  if (isWebmSupported) {
                      promises.push(create_update_test(mediaKeys, 'webm', getInitData('webm')));
                  }

                  if (isCencSupported) {
                      promises.push(create_update_test(mediaKeys, 'cenc', getInitData('cenc')));
                  }

                  assert_not_equals(promises.length, 0);
                  return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'update() tests failed');
                });
            }, 'Test MediaKeySession update().');

            function create_close_exception_test(mediaKeys, type, initData)
            {
                var mediaKeySession = mediaKeys.createSession();
                return mediaKeySession.close().then(function(result) {
                    assert_unreached('close() should not succeed if session uninitialized');
                }).catch(function(error) {
                    assert_equals(error.name, 'InvalidStateError');
                    // Return something so the promise resolves.
                    return Promise.resolve();
                });
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = [];

                    if (isWebmSupported) {
                        promises.push(create_close_exception_test(mediaKeys, 'webm', getInitData('webm')));
                    }

                    if (isCencSupported) {
                        promises.push(create_close_exception_test(mediaKeys, 'cenc', getInitData('cenc')));
                    }

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'close() exception tests failed');
                });
            }, 'Test MediaKeySession close() exceptions.');


            function create_close_test(mediaKeys, type, initData)
            {
                var mediaKeySession = mediaKeys.createSession();
                var promise = mediaKeySession.generateRequest(type, initData).then(function(result) {
                    return mediaKeySession.close();
                }).then(function(result) {
                    // Call close() again with an extra parameter. The extra
                    // parameter is ignored.
                    return mediaKeySession.close('extra');
                });
                return promise;
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = [];

                    if (isWebmSupported) {
                        promises.push(create_close_test(mediaKeys, 'webm', getInitData('webm')));
                    }

                    if (isCencSupported) {
                        promises.push(create_close_test(mediaKeys, 'cenc', getInitData('cenc')));
                    }

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'close() tests failed');
                });
            }, 'Test MediaKeySession close().');

            function create_remove_exception_test(mediaKeys, type, initData)
            {
                // remove() on an uninitialized session should fail.
                var mediaKeySession = mediaKeys.createSession('temporary');
                return mediaKeySession.remove().then(function(result) {
                    assert_unreached('remove() should not succeed if session uninitialized');
                }, function(error) {
                    assert_equals(error.name, 'InvalidStateError');

                    // remove() on a temporary session should fail.
                    return mediaKeySession.generateRequest(type, initData);
                }).then(function(result) {
                    return mediaKeySession.remove();
                }).then(function(result) {
                    assert_unreached('remove() should not succeed for temporary sessions');
                }, function(error) {
                    assert_equals(error.name, 'InvalidAccessError');
                });
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = [];

                    if (isWebmSupported) {
                        promises.push(create_remove_exception_test(mediaKeys, 'webm', getInitData('webm')));
                    }

                    if (isCencSupported) {
                        promises.push(create_remove_exception_test(mediaKeys, 'cenc', getInitData('cenc')));
                    }

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'remove() exception tests failed');
                });
            }, 'Test MediaKeySession remove() exceptions.');

            function create_remove_test(mediaKeys, type, initData)
            {
                // Clear Key may not support persistent-license sessions.
                var mediaKeySession;
                try {
                    mediaKeySession = mediaKeys.createSession('persistent-license');
                } catch (error) {
                    // Not supported, so return a resolved promise.
                    assert_equals(error.name, 'NotSupportedError');
                    return Promise.resolve();
                }
                return mediaKeySession.generateRequest(type, initData).then(function(result) {
                    return mediaKeySession.remove();
                });
            }

            async_test(function(test)
            {
                var isWebmSupported;
                var isCencSupported;

                isInitDataTypeSupported('webm').then(function(result) {
                    isWebmSupported = result;
                    return isInitDataTypeSupported('cenc');
                }).then(function(result) {
                    isCencSupported = result;
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]);
                }).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = [];

                    if (isWebmSupported) {
                        promises.push(create_remove_test(mediaKeys, 'webm', getInitData('webm')));
                    }

                    if (isCencSupported) {
                        promises.push(create_remove_test(mediaKeys, 'cenc', getInitData('cenc')));
                    }

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'remove() tests failed');
                });
            }, 'Test MediaKeySession remove().');

            var kSetServerCertificateExceptionsTestCases = [
                // Too few parameters.
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.setServerCertificate(); }
                },
                // Invalid parameters.
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.setServerCertificate(''); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.setServerCertificate(null); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.setServerCertificate(undefined); }
                },
                {
                    exception: 'TypeError',
                    func: function(mk) { return mk.setServerCertificate(1); }
                },
                // Empty array.
                {
                    exception: 'InvalidAccessError',
                    func: function(mk) { return mk.setServerCertificate(new Uint8Array(0)); }
                }
            ];

            async_test(function(test)
            {
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = kSetServerCertificateExceptionsTestCases.map(function(testCase) {
                        return test_exception(testCase, mediaKeys);
                    });

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'setServerCertificate() exception tests failed');
                });
            }, 'Test MediaKeys setServerCertificate() exceptions.');

            // All calls to |func| in this group are expected to resolve.
            var kSetServerCertificateTestCases = [
                {
                    // Pass in ArrayBufferView
                    func: function(mk) {
                              var cert = new Uint8Array(200);
                              assert_true(ArrayBuffer.isView(cert));
                              return mk.setServerCertificate(cert);
                    },
                    expected: false,
                },
                {
                    // Pass in ArrayBuffer
                    func: function(mk) {
                              var buffer = new ArrayBuffer(200);
                              assert_false(ArrayBuffer.isView(buffer));
                              return mk.setServerCertificate(buffer);
                    },
                    expected: false,
                }
            ];

            async_test(function(test)
            {
                var expected_result;
                navigator.requestMediaKeySystemAccess('org.w3.clearkey', [{}]).then(function(access) {
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    var promises = kSetServerCertificateTestCases.map(function(testCase) {
                        return testCase.func.call(null, mediaKeys);
                    });
                    expected_result = kSetServerCertificateTestCases.map(function(testCase) {
                        return testCase.expected;
                    });

                    assert_not_equals(promises.length, 0);
                    return Promise.all(promises);
                }).then(function(result) {
                    assert_array_equals(result, expected_result);
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error, 'setServerCertificate() test failed');
                });
            }, 'Test MediaKeys setServerCertificate().');


            // FIXME: Add syntax checks for MediaKeys.IsTypeSupported().
            // FIXME: Add syntax checks for MediaKeyError and MediaKeySession events.
            // FIXME: Add HTMLMediaElement syntax checks, e.g. setMediaKeys, mediakeys, onencrypted.
        </script>
    </body>
</html>