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