summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/embedded-content/media-elements/mime-types/canPlayType.html
blob: 56edf25aa8fb93c66fbbad5bbfb2e9652e7297d0 (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
<!doctype html>
<title>canPlayType</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<audio id="audio"></audio>
<video id="video"></video>
<div id="log"></div>
<script>
let VIDEO_ELEM = document.getElementById('video');
let AUDIO_ELEM = document.getElementById('audio');

function t(type, expected) {
  assert_equals(canPlayType(type), expected, type);
}

function mime(type, codecs) {
  if (codecs.length) {
    return type + '; codecs="' + codecs.join(', ') + '"';
  }
  return type;
}

test(function() {
  assert_equals(mime('video/webm', []), 'video/webm');
  assert_equals(mime('video/webm', ['vp8']), 'video/webm; codecs="vp8"');
  assert_equals(mime('video/webm', ['vp8', 'vorbis']), 'video/webm; codecs="vp8, vorbis"');
}, 'utility code');

function canPlayType(type) {
  let audioCanPlay = AUDIO_ELEM.canPlayType(type);
  let videoCanPlay = VIDEO_ELEM.canPlayType(type);
  assert_equals(audioCanPlay, videoCanPlay,
                'audio.canPlayType() and video.canPlayType() agree');
  assert_in_array(audioCanPlay, ['', 'maybe', 'probably'],
                  'return value is one of "", "maybe" and "probably"');
  return audioCanPlay;
}

test(function() {
  t('application/octet-stream', '');
  t('application/octet-stream; codecs="vorbis"', '');
  t('application/octet-stream; codecs="vp8, vorbis"', '');
  t('application/octet-stream; codecs="mp4a.40.2"', '');
  t('application/octet-stream; codecs="theora, vorbis"', '');
  t('application/octet-stream; codecs="avc1.42E01E, mp4a.40.2"', '');
}, 'application/octet-stream not supported');

test(function() {
  t('application/marks-fantasmagorical-format', '');
  t('video/x-new-fictional-format', '');
  t('video/x-new-fictional-format;codecs="kittens,bunnies"', '');
}, 'fictional formats and codecs not supported');

function type_codecs_test(type, audioCodecs, videoCodecs) {
  var typeSupported = false;
  var codecSupported = false;

  // Test 'type' without codecs.
  // Spec: Generally, a user agent should never return "probably" for a type
  // that allows the codecs parameter if that parameter is not present.
  test(function() {
    t(type, 'maybe');
    t(type + ';', 'maybe');
    t(type + ';codecs', 'maybe');
    t(type + ';codecs=', 'maybe');
    typeSupported = true;
  }, type + ' (optional)');

  function test_codec(codec) {
    var typeWithCodec = mime(type, [codec]);
    test(function() {
      t(typeWithCodec, 'probably');
      codecSupported = true;
    }, typeWithCodec + ' (optional)');
  }

  // Test each audio and video codec separately.
  audioCodecs.forEach(test_codec);
  videoCodecs.forEach(test_codec);

  // Test different pairings and orderings of audio+video codecs.
  if (audioCodecs.length > 0 && videoCodecs.length > 0) {
    test(function() {
      audioCodecs.forEach(function(ac) {
        videoCodecs.forEach(function(vc) {
          var canPlayBoth = canPlayType(mime(type, [ac, vc]));
          if (canPlayBoth) {
            t(mime(type, [ac]), canPlayBoth);
            t(mime(type, [vc]), canPlayBoth);
          }
        });
      });
    }, type + ' codecs subset');

    test(function() {
      audioCodecs.forEach(function(ac) {
        videoCodecs.forEach(function(vc) {
          assert_equals(canPlayType(mime(type, [ac, vc])),
                        canPlayType(mime(type, [vc, ac])));
        });
      });
    }, type + ' codecs order');
  }

  test(function() {
    t(mime(type, ['bogus']), '');
  }, type + ' with bogus codec');

  test(function() {
    // At least one known codec must be supported if the container format is.
    assert_equals(typeSupported, codecSupported);
  }, type + ' with and without codecs');
}

type_codecs_test('audio/mp4', ['mp4a.40.2'], []);
type_codecs_test('audio/ogg', ['opus', 'vorbis'], []);
type_codecs_test('audio/wav', ['1'], []);
type_codecs_test('audio/webm', ['opus', 'vorbis'], []);
type_codecs_test('video/3gpp', ['samr'], ['mp4v.20.8']);
type_codecs_test('video/mp4', ['mp4a.40.2'], ['avc1.42E01E', 'avc1.4D401E', 'avc1.58A01E', 'avc1.64001E', 'mp4v.20.8', 'mp4v.20.240']);
type_codecs_test('video/ogg', ['opus', 'vorbis'], ['theora']);
type_codecs_test('video/webm', ['opus', 'vorbis'], ['vp8', 'vp8.0', 'vp9', 'vp9.0']);
</script>