requestmediakeysystemaccess.js (16025B)
1 function runTest(config, qualifier) { 2 3 var prefix = testnamePrefix(qualifier, config.keysystem) + ', requestMediaKeySystemAccess: '; 4 5 function expect_error(keySystem, configurations, expectedError, testname) { 6 7 var audioCapabilities = configurations.length ? configurations[0].audioCapabilities : undefined, 8 videoCapabilities = configurations.length ? configurations[0].videoCapabilities : undefined, 9 audiocontenttypes = audioCapabilities ? audioCapabilities.map( function(ac) { return "'" + ac.contentType + "'"; } ).join(',') : '', 10 videocontenttypes = videoCapabilities ? videoCapabilities.map( function(ac) { return "'" + ac.contentType + "'"; } ).join(',') : '', 11 modifiedtestname = testname.replace( '%audiocontenttype', audiocontenttypes ).replace( '%videocontenttype', videocontenttypes ); 12 13 promise_test(function(test) { 14 var p = navigator.requestMediaKeySystemAccess(keySystem, configurations); 15 // expectedError is a string name for the error. We can differentiate 16 // JS Errors from DOMExceptions by checking whether 17 // window[expectedError] exists. If it does, expectedError is the name 18 // of a JS Error subclass and window[expectedError] is the constructor 19 // for that subclass. Otherwise it's a name for a DOMException. 20 if (window[expectedError]) { 21 return promise_rejects_js(test, window[expectedError], p); 22 } else { 23 return promise_rejects_dom(test, expectedError, p); 24 } 25 }, prefix + modifiedtestname + ' should result in ' + expectedError ); 26 } 27 28 function assert_subset(actual, expected, path) { 29 if (typeof expected == 'string') { 30 assert_equals(actual, expected, path); 31 } else { 32 if (expected.hasOwnProperty('length')) { 33 assert_equals(actual.length, expected.length, path + '.length'); 34 } 35 for (property in expected) { 36 assert_subset(actual[property], expected[property], path + '.' + property); 37 } 38 } 39 } 40 41 function expect_config(keySystem, configurations, expectedConfiguration, testname) { 42 promise_test(function(test) { 43 return navigator.requestMediaKeySystemAccess(keySystem, configurations).then(function(a) { 44 assert_subset(a.getConfiguration(), expectedConfiguration, testname + ': '); 45 }); 46 }, testname); 47 } 48 49 // Tests for Key System. 50 expect_error('', [{}], 'TypeError', 'Empty Key System'); 51 expect_error('com.example.unsupported', [{}], 'NotSupportedError', 'Unsupported Key System'); 52 expect_error(config.keysystem + '.', [{}], 'NotSupportedError', 'Key System ending in "."'); 53 expect_error(config.keysystem.toUpperCase(), [{}], 'NotSupportedError', 'Capitalized Key System'); 54 expect_error(config.keysystem + '\u028F', [{}], 'NotSupportedError', 'Non-ASCII Key System'); 55 56 // Parent of Clear Key not supported. 57 expect_error(config.keysystem.match(/^(.*?)\./)[1], [{}], 'NotSupportedError', 'Root domain of Key System alone'); 58 expect_error(config.keysystem.match(/^(.*?)\./)[0], [{}], 'NotSupportedError', 'Root domain of Key System, with dot'); 59 expect_error(config.keysystem.match(/^(.*?\..*?)\./)[1], [{}], 'NotSupportedError', 'Domain of Key System along'); 60 expect_error(config.keysystem.match(/^(.*?\..*?)\./)[0], [{}], 'NotSupportedError', 'Domain of Key System, with dot'); 61 62 // Child of Clear Key not supported. 63 expect_error(config.keysystem+'.foo', [{}], 'NotSupportedError', 'Child of Key System'); 64 65 // Prefixed Clear Key not supported. 66 expect_error('webkit-'+config.keysystem, [{}], 'NotSupportedError', 'Prefixed Key System'); 67 68 // Incomplete names. 69 expect_error(config.keysystem.substr(0,7)+config.keysystem.substr(8), [{}], 'NotSupportedError', 'Missing characters in middle of Key System name'); 70 expect_error(config.keysystem.substr(0,config.keysystem.length-1), [{}], 'NotSupportedError', 'Missing characters at end of Key System name'); 71 72 // Spaces in key system name not supported. 73 expect_error(' '+config.keysystem, [{}], 'NotSupportedError', 'Leading space in Key System name'); 74 expect_error(config.keysystem.substr(0,6) + ' ' + config.keysystem.substr(6), [{}], 'NotSupportedError', 'Extra space in Key System name'); 75 expect_error(config.keysystem + ' ', [{}], 'NotSupportedError', 'Trailing space in Key System name'); 76 77 // Extra dots in key systems names not supported. 78 expect_error('.' + config.keysystem, [{}], 'NotSupportedError', 'Leading dot in Key System name'); 79 expect_error(config.keysystem.substr(0,6) + '.' + config.keysystem.substr(6), [{}], 'NotSupportedError', 'Extra dot in middle of Key System name'); 80 expect_error(config.keysystem + '.', [{}], 'NotSupportedError', 'Trailing dot in Key System name'); 81 82 // Key system name is case sensitive. 83 if (config.keysystem !== config.keysystem.toUpperCase()) { 84 expect_error(config.keysystem.toUpperCase(), [{}], 'NotSupportedError', 'Key System name is case sensitive'); 85 } 86 87 if (config.keysystem !== config.keysystem.toLowerCase()) { 88 expect_error(config.keysystem.toLowerCase(), [{}], 'NotSupportedError', 'Key System name is case sensitive'); 89 } 90 91 // Tests for trivial configurations. 92 expect_error(config.keysystem, [], 'TypeError', 'Empty supportedConfigurations'); 93 expect_error(config.keysystem, [{}], 'NotSupportedError', 'Empty configuration'); 94 95 // Various combinations of supportedConfigurations. 96 expect_config(config.keysystem, [{ 97 initDataTypes: [config.initDataType], 98 audioCapabilities: [{contentType: config.audioType}], 99 videoCapabilities: [{contentType: config.videoType}], 100 label: 'abcd', 101 }], { 102 initDataTypes: [config.initDataType], 103 audioCapabilities: [{contentType: config.audioType}], 104 videoCapabilities: [{contentType: config.videoType}], 105 label: 'abcd', 106 }, 'Basic supported configuration'); 107 108 expect_config(config.keysystem, [{ 109 initDataTypes: ['fakeidt', config.initDataType], 110 audioCapabilities: [{contentType: 'audio/fake'}, {contentType: config.audioType}], 111 videoCapabilities: [{contentType: 'video/fake'}, {contentType: config.videoType}], 112 }], { 113 initDataTypes: [config.initDataType], 114 audioCapabilities: [{contentType: config.audioType}], 115 videoCapabilities: [{contentType: config.videoType}], 116 }, 'Partially supported configuration'); 117 118 expect_config(config.keysystem, [{ 119 audioCapabilities: [{contentType: config.audioType}], 120 }], { 121 audioCapabilities: [{contentType: config.audioType}], 122 }, 'Supported audio codec'); 123 124 expect_config(config.keysystem, [{ 125 audioCapabilities: [{contentType: config.audioType.replace(/^(.*?);(.*)/, "$1; $2")}], 126 }], { 127 audioCapabilities: [{contentType: config.audioType.replace(/^(.*?);(.*)/, "$1; $2")}], 128 }, 'ContentType formatting must be preserved'); 129 130 expect_error(config.keysystem, [{ 131 audioCapabilities: [{contentType: 'audio/webm; codecs=fake'}], 132 }], 'NotSupportedError', 'Unsupported audio codec (%audiocontenttype)'); 133 134 expect_error(config.keysystem, [{ 135 audioCapabilities: [{contentType: 'video/webm; codecs=fake'}], 136 }], 'NotSupportedError', 'Unsupported video codec (%videocontenttype)'); 137 138 expect_error(config.keysystem, [{ 139 audioCapabilities: [ 140 {contentType: 'audio/webm; codecs=mp4a'}, 141 {contentType: 'audio/webm; codecs=mp4a.40.2'} 142 ], 143 }], 'NotSupportedError', 'Mismatched audio container/codec (%audiocontenttype)'); 144 145 expect_error(config.keysystem, [{ 146 audioCapabilities: [{contentType: config.videoType}], 147 }], 'NotSupportedError', 'Video codec specified in audio field (%audiocontenttype)'); 148 149 expect_error(config.keysystem, [{ 150 videoCapabilities: [{contentType: config.audioType}], 151 }], 'NotSupportedError', 'Audio codec specified in video field (%videocontenttype)'); 152 153 expect_error(config.keysystem, [{ 154 audioCapabilities: [ 155 {contentType: 'audio/webm; codecs=avc1'}, 156 {contentType: 'audio/webm; codecs=avc1.42e01e'} 157 ], 158 }], 'NotSupportedError', 'Mismatched audio container/codec (%audiocontenttype)'); 159 160 expect_error(config.keysystem, [{ 161 audioCapabilities: [ 162 {contentType: 'audio/mp4; codecs=vorbis'} 163 ], 164 }], 'NotSupportedError', 'Mismatched audio container/codec (%audiocontenttype)'); 165 166 expect_config(config.keysystem, [{ 167 initDataTypes: ['fakeidt'], 168 videoCapabilities: [{contentType: config.videoType}] 169 }, { 170 initDataTypes: [config.initDataType], 171 videoCapabilities: [{contentType: config.videoType}] 172 } 173 ], { 174 initDataTypes: [config.initDataType], 175 videoCapabilities: [{contentType: config.videoType}] 176 }, 'Two configurations, one supported'); 177 178 expect_config(config.keysystem, [{ 179 initDataTypes: [config.initDataType], 180 videoCapabilities: [{contentType: config.videoType}] 181 }, { 182 videoCapabilities: [{contentType: config.videoType}] 183 } 184 ], { 185 initDataTypes: [config.initDataType], 186 videoCapabilities: [{contentType: config.videoType}] 187 }, 'Two configurations, both supported'); 188 189 // Audio MIME type does not support video codecs. 190 expect_error(config.keysystem, [{ 191 audioCapabilities: [ 192 {contentType: 'audio/webm; codecs="vp8,vorbis"'}, 193 {contentType: 'audio/webm; codecs="vorbis, vp8"'}, 194 {contentType: 'audio/webm; codecs="vp8"'} 195 ], 196 }], 'NotSupportedError', 'Audio MIME type does not support video codecs (webm) (%audiocontenttype)'); 197 198 expect_error(config.keysystem, [{ 199 audioCapabilities: [ 200 {contentType: 'audio/mp4; codecs="avc1"'}, 201 {contentType: 'audio/mp4; codecs="avc1.4d401e"'}, 202 ], 203 }], 'NotSupportedError', 'Audio MIME type does not support video codecs (mp4) (%audiocontenttype)'); 204 205 // Video MIME type does not support audio codecs. 206 expect_error(config.keysystem, [{ 207 videoCapabilities: [ 208 {contentType: 'video/webm; codecs="vp8,vorbis"'}, 209 {contentType: 'video/webm; codecs="vorbis, vp8"'}, 210 {contentType: 'video/webm; codecs="vorbis"'} 211 ], 212 }], 'NotSupportedError', 'Video MIME type does not support audio codecs (webm) (%videocontenttype)'); 213 214 expect_error(config.keysystem, [{ 215 videoCapabilities: [ 216 {contentType: 'video/mp4; codecs="mp4a"'}, 217 {contentType: 'video/mp4; codecs="mp4a.40.2"'} 218 ], 219 }], 'NotSupportedError', 'Video MIME type does not support audio codecs (mp4) (%videocontenttype)'); 220 221 // WebM does not support AVC1/AAC. 222 expect_error(config.keysystem, [{ 223 audioCapabilities: [ 224 {contentType: 'audio/webm; codecs="aac"'}, 225 {contentType: 'audio/webm; codecs="avc1"'}, 226 {contentType: 'audio/webm; codecs="vp8,aac"'} 227 ], 228 }], 'NotSupportedError', 'WebM audio does not support AVC1/AAC (%audiocontenttype)'); 229 230 expect_error(config.keysystem, [{ 231 videoCapabilities: [ 232 {contentType: 'video/webm; codecs="aac"'}, 233 {contentType: 'video/webm; codecs="avc1"'}, 234 {contentType: 'video/webm; codecs="vp8,aac"'} 235 ], 236 }], 'NotSupportedError', 'WebM video does not support AVC1/AAC (%videocontenttype)'); 237 238 // Extra space is allowed in contentType. 239 expect_config(config.keysystem, [{ 240 videoCapabilities: [{contentType: ' ' + config.videoType}], 241 }], { 242 videoCapabilities: [{contentType: ' ' + config.videoType}], 243 }, 'Leading space in contentType'); 244 245 expect_config(config.keysystem, [{ 246 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?);(.*)/, "$1 ;$2")}], 247 }], { 248 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?);(.*)/, "$1 ;$2")}], 249 }, 'Space before ; in contentType'); 250 251 252 expect_config(config.keysystem, [{ 253 videoCapabilities: [{contentType: config.videoType + ' '}], 254 }], { 255 videoCapabilities: [{contentType: config.videoType + ' '}], 256 }, 'Trailing space in contentType'); 257 258 expect_config(config.keysystem, [{ 259 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?codecs=\")(.*)/, "$1 $2")}], 260 }], { 261 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?codecs=\")(.*)/, "$1 $2")}], 262 }, 'Space at start of codecs parameter'); 263 264 expect_config(config.keysystem, [{ 265 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?codecs=\".*)\"/, "$1 \"")}], 266 }], { 267 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?codecs=\".*)\"/, "$1 \"")}], 268 }, 'Space at end of codecs parameter'); 269 270 // contentType is not case sensitive (except the codec names). 271 expect_config(config.keysystem, [{ 272 videoCapabilities: [{contentType: 'V' + config.videoType.substr(1)}], 273 }], { 274 videoCapabilities: [{contentType: 'V' + config.videoType.substr(1)}], 275 }, 'Video/' ); 276 277 expect_config(config.keysystem, [{ 278 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?)c(odecs.*)/, "$1C$2")}], 279 }], { 280 videoCapabilities: [{contentType: config.videoType.replace( /^(.*?)c(odecs.*)/, "$1C$2")}], 281 }, 'Codecs='); 282 283 var t = config.videoType.match(/(.*?)(;.*)/); 284 expect_config(config.keysystem, [{ 285 videoCapabilities: [{contentType: t[1].toUpperCase() + t[2]}], 286 }], { 287 videoCapabilities: [{contentType: t[1].toUpperCase() + t[2]}], 288 }, 'Upper case MIME type'); 289 290 t = config.videoType.match(/(.*?)codecs(.*)/); 291 expect_config(config.keysystem, [{ 292 videoCapabilities: [{contentType: t[1] + 'CODECS' + t[2]}], 293 }], { 294 videoCapabilities: [{contentType: t[1] + 'CODECS' + t[2]}], 295 }, 'CODECS='); 296 297 // Unrecognized attributes are not allowed. 298 expect_error(config.keysystem, [{ 299 videoCapabilities: [{contentType: 'video/webm; foo="bar"'}], 300 }], 'NotSupportedError', 'Unrecognized foo with webm (%videocontenttype)'); 301 302 expect_error(config.keysystem, [{ 303 videoCapabilities: [{contentType: 'video/mp4; foo="bar"'}], 304 }], 'NotSupportedError', 'Unrecognized foo with mp4 (%videocontenttype)'); 305 306 expect_error(config.keysystem, [{ 307 videoCapabilities: [{contentType: config.videoType + '; foo="bar"'}], 308 }], 'NotSupportedError', 'Unrecognized foo with codecs (%videocontenttype)'); 309 310 // Invalid contentTypes. 311 expect_error(config.keysystem, [{ 312 videoCapabilities: [{contentType: 'fake'}], 313 }], 'NotSupportedError', 'contentType: %videocontenttype'); 314 315 expect_error(config.keysystem, [{ 316 audioCapabilities: [{contentType: 'audio/fake'}], 317 }], 'NotSupportedError', 'contentType: %audiocontenttype'); 318 319 expect_error(config.keysystem, [{ 320 videoCapabilities: [{contentType: 'video/fake'}], 321 }], 'NotSupportedError', 'contentType: %videocontenttype'); 322 323 // The actual codec names are case sensitive. 324 t = config.videoType.match( /(.*?codecs=\")(.*?\")(.*)/ ); 325 if (t[2] !== t[2].toUpperCase()) { 326 expect_error(config.keysystem, [{ 327 videoCapabilities: [{contentType: t[1] + t[2].toUpperCase() + t[3] }], 328 }], 'NotSupportedError', 'contentType: %videocontenttype'); 329 } 330 331 if (t[2] !== t[2].toLowerCase()) { 332 expect_error(config.keysystem, [{ 333 videoCapabilities: [{contentType: t[1] + t[2].toLowerCase() + t[3] }], 334 }], 'NotSupportedError', 'contentType: %videocontenttype'); 335 } 336 337 // Extra comma is not allowed in codecs. 338 expect_error(config.keysystem, [{ 339 videoCapabilities: [{contentType: t[1] + ',' + t[2] + t[3] }], 340 }], 'NotSupportedError', 'contentType: %videocontenttype'); 341 }