keystatuses.js (8538B)
1 function runTest(config,qualifier) 2 { 3 var testname = testnamePrefix(qualifier, config.keysystem) + ', temporary, keystatuses'; 4 5 var configuration = getSimpleConfigurationForContent(config.content); 6 7 if (config.initDataType && config.initData) { 8 configuration.initDataTypes = [config.initDataType]; 9 } 10 11 async_test(function(test) 12 { 13 var mediaKeySession; 14 var initDataType; 15 var initData; 16 var closed = false; 17 18 // Even though key ids are uint8, using printable values so that 19 // they can be verified easily. 20 var key1 = new Uint8Array(config.content.keys[0].kid), 21 key2 = new Uint8Array(config.content.keys[1].kid), 22 key1String = arrayBufferAsString(key1), 23 key2String = arrayBufferAsString(key2); 24 25 function onFailure(error) { 26 forceTestFailureFromPromise(test, error); 27 } 28 29 function processMessage(event) 30 { 31 // No keys added yet. 32 assert_equals(mediaKeySession.keyStatuses.size, 0); 33 34 waitForEventAndRunStep('keystatuseschange', mediaKeySession, processKeyStatusesChange, test); 35 36 // Add keys to session 37 config.messagehandler(event.messageType, event.message).then(function(response) { 38 return event.target.update(response); 39 }).catch(onFailure); 40 } 41 42 function checkKeyStatusFor2Keys() 43 { 44 // Two keys added, so both should show up in |keyStatuses|. 45 assert_equals(mediaKeySession.keyStatuses.size, 2); 46 47 // Check |keyStatuses| for 2 entries. 48 var result = []; 49 for (let item of mediaKeySession.keyStatuses) { 50 result.push({ key: arrayBufferAsString(item[0]), value: item[1] }); 51 } 52 function lexicographical( a, b ) { return a < b ? -1 : a === b ? 0 : +1; } 53 function lexicographicalkey( a, b ) { return lexicographical( a.key, b.key ); } 54 var expected1 = [{ key: key1String, value: 'usable'}, { key: key2String, value: 'usable'}].sort( lexicographicalkey ); 55 var expected2 = [{ key: key1String, value: 'status-pending'}, { key: key2String, value: 'status-pending'}].sort( lexicographicalkey ); 56 assert_in_array( JSON.stringify(result), 57 [ JSON.stringify(expected1),JSON.stringify(expected2) ], 58 "keystatuses should have the two expected keys with keystatus 'usable' or 'status-pending'"); 59 60 // |keyStatuses| must contain both keys. 61 result = []; 62 for (var key of mediaKeySession.keyStatuses.keys()) { 63 result.push(arrayBufferAsString(key)); 64 } 65 assert_array_equals(result, 66 [key1String, key2String].sort( lexicographical ), 67 "keyStatuses.keys() should return an iterable over the two expected keys"); 68 69 // Both values in |mediaKeySession| should be 'usable' or 'status-pending'. 70 result = []; 71 for (var value of mediaKeySession.keyStatuses.values()) { 72 result.push(value); 73 } 74 75 assert_equals( result.length, 2, "keyStatuses.values() should have two elements" ); 76 assert_equals( result[0], result[1], "the values in keyStatuses.values() should be equal" ); 77 assert_in_array( result[0], [ 'usable', 'status-pending' ] ); 78 79 // Check |keyStatuses.entries()|. 80 result = []; 81 for (var entry of mediaKeySession.keyStatuses.entries()) { 82 result.push({ key: arrayBufferAsString(entry[0]), value: entry[1] }); 83 } 84 assert_in_array(JSON.stringify(result), 85 [ JSON.stringify(expected1), JSON.stringify(expected2) ], 86 "keyStatuses.entries() should return an iterable over the two expected keys, with keystatus 'usable' or 'status-pending'"); 87 88 // forEach() should return both entries. 89 result = []; 90 mediaKeySession.keyStatuses.forEach(function(status, keyId) { 91 result.push({ key: arrayBufferAsString(keyId), value: status }); 92 }); 93 assert_in_array(JSON.stringify(result), 94 [ JSON.stringify(expected1), JSON.stringify(expected2) ], 95 "keyStatuses.forEach() should iterate over the two expected keys, with keystatus 'usable' or 'status-pending'"); 96 97 // has() and get() should return the expected values. 98 assert_true(mediaKeySession.keyStatuses.has(key1), "keyStatuses should have key1"); 99 assert_true(mediaKeySession.keyStatuses.has(key2), "keyStatuses should have key2"); 100 assert_in_array(mediaKeySession.keyStatuses.get(key1), [ 'usable', 'status-pending' ], "key1 should have status 'usable' or 'status-pending'"); 101 assert_in_array(mediaKeySession.keyStatuses.get(key2), [ 'usable', 'status-pending' ], "key2 should have status 'usable' or 'status-pending'"); 102 103 // Try some invalid keyIds. 104 var invalid1 = key1.subarray(0, key1.length - 1); 105 assert_false(mediaKeySession.keyStatuses.has(invalid1), "keystatuses should not have invalid key (1)"); 106 assert_equals(mediaKeySession.keyStatuses.get(invalid1), undefined, "keystatus value for invalid key should be undefined (1)"); 107 108 var invalid2 = key1.subarray(1); 109 assert_false(mediaKeySession.keyStatuses.has(invalid2), "keystatuses should not have invalid key (2)"); 110 assert_equals(mediaKeySession.keyStatuses.get(invalid2), undefined, "keystatus value for invalid key should be undefined (2)"); 111 112 var invalid3 = new Uint8Array(key1); 113 invalid3[0] += 1; 114 assert_false(mediaKeySession.keyStatuses.has(invalid3), "keystatuses should not have invalid key (3)"); 115 assert_equals(mediaKeySession.keyStatuses.get(invalid3), undefined, "keystatus value for invalid key should be undefined (3)"); 116 117 var invalid4 = new Uint8Array(key1); 118 invalid4[invalid4.length - 1] -= 1; 119 assert_false(mediaKeySession.keyStatuses.has(invalid4), "keystatuses should not have invalid key (4)"); 120 assert_equals(mediaKeySession.keyStatuses.get(invalid4), undefined, "keystatus value for invalid key should be undefined (4)"); 121 122 var invalid5 = new Uint8Array(key1.length + 1); 123 invalid5.set(key1, 1); // First element will be 0. 124 assert_false(mediaKeySession.keyStatuses.has(invalid5), "keystatuses should not have invalid key (5)"); 125 assert_equals(mediaKeySession.keyStatuses.get(invalid5), undefined, "keystatus value for invalid key should be undefined (5)"); 126 127 var invalid6 = new Uint8Array(key1.length + 1); 128 invalid6.set(key1, 0); // Last element will be 0. 129 assert_false(mediaKeySession.keyStatuses.has(invalid6), "keystatuses should not have invalid key (6)"); 130 assert_equals(mediaKeySession.keyStatuses.get(invalid6), undefined, "keystatus value for invalid key should be undefined (6)"); 131 } 132 133 function processKeyStatusesChange(event) 134 { 135 if (!closed) 136 { 137 // The first keystatuseschange (caused by update()) 138 // should include both keys. 139 checkKeyStatusFor2Keys(); 140 141 mediaKeySession.close().catch(onFailure); 142 closed = true; 143 } 144 else 145 { 146 // The second keystatuseschange (caused by close()) 147 // should not have any keys. 148 assert_equals(mediaKeySession.keyStatuses.size, 0); 149 test.done(); 150 } 151 } 152 153 navigator.requestMediaKeySystemAccess(config.keysystem, [configuration]).then(function(access) { 154 return access.createMediaKeys(); 155 }).then(test.step_func(function(mediaKeys) { 156 mediaKeySession = mediaKeys.createSession(); 157 158 // There should be no keys defined yet. 159 //verifyKeyStatuses(mediaKeySession.keyStatuses, { expected: [], unexpected: [key1, key2] }); 160 161 waitForEventAndRunStep('message', mediaKeySession, processMessage, test); 162 return mediaKeySession.generateRequest(config.initDataType, config.initData); 163 })).catch(onFailure); 164 }, testname ); 165 }