OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <title>Test multiple MediaKeys lifetimes</title> | 4 <title>Test multiple MediaKeys lifetimes</title> |
5 <script src="encrypted-media-utils.js"></script> | 5 <script src="encrypted-media-utils.js"></script> |
6 <script src="../../resources/testharness.js"></script> | 6 <script src="../../resources/testharness.js"></script> |
7 <script src="../../resources/testharnessreport.js"></script> | 7 <script src="../../resources/testharnessreport.js"></script> |
8 </head> | 8 </head> |
9 <body> | 9 <body> |
10 <script> | 10 <script> |
11 // For this test, create several MediaKeys and verify lifetime. | 11 // For this test, create several MediaKeys and verify lifetime. |
12 async_test(function(test) | 12 promise_test(function(test) |
13 { | 13 { |
14 gc(); | 14 var mediaKeys1; |
15 var mediaKeys; | 15 var mediaKeys2; |
16 var startingMediaKeysCount = window.internals.mediaKeysCount(); | 16 var mediaKeys3; |
17 | 17 var mediaKeys4; |
18 function numMediaKeysCreated() | 18 var mediaKeys5; |
19 { | |
20 return window.internals.mediaKeysCount() - startingMediaKeys
Count; | |
21 } | |
22 | 19 |
23 // Create a MediaKeys object. Returns a promise that resolves | 20 // Create a MediaKeys object. Returns a promise that resolves |
24 // with the new MediaKeys object. | 21 // with the new MediaKeys object. |
25 function createMediaKeys() | 22 function createMediaKeys() |
26 { | 23 { |
27 return navigator.requestMediaKeySystemAccess('org.w3.clearke
y', getSimpleConfiguration()).then(function(access) { | 24 return navigator.requestMediaKeySystemAccess('org.w3.clearke
y', getSimpleConfiguration()).then(function(access) { |
28 return access.createMediaKeys(); | 25 return access.createMediaKeys(); |
29 }).then(function(mediaKeys) { | |
30 return mediaKeys; | |
31 }); | 26 }); |
32 } | 27 } |
33 | 28 |
34 // Create a few MediaKeys objects. Only keep a reference to the | 29 // Create a few MediaKeys objects. Keep references to them, |
35 // last one created. | 30 // to avoid issues if gc() runs. |
36 createMediaKeys().then(function(result) { | 31 // Even though there should be no existing objects, start by |
37 assert_equals(numMediaKeysCreated(), 1); | 32 // running gc() and verifying that none exist. This also |
| 33 // allows the failures to be reported from inside a promise |
| 34 // so that the test fails properly. |
| 35 return createGCPromise().then(function() { |
| 36 verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After initial g
c()'); |
38 | 37 |
39 return createMediaKeys(); | 38 return createMediaKeys(); |
40 }).then(function(result) { | 39 }).then(function(result) { |
41 assert_equals(numMediaKeysCreated(), 2); | 40 assert_not_equals(result, null); |
| 41 mediaKeys1 = result; |
| 42 verifyMediaKeyAndMediaKeySessionCount(1, 0, 'Create first Me
diaKeys'); |
42 | 43 |
43 return createMediaKeys(); | 44 return createMediaKeys(); |
44 }).then(function(result) { | 45 }).then(function(result) { |
45 assert_equals(numMediaKeysCreated(), 3); | 46 assert_not_equals(result, null); |
| 47 mediaKeys2 = result; |
| 48 verifyMediaKeyAndMediaKeySessionCount(2, 0, 'Create second M
ediaKeys'); |
46 | 49 |
47 return createMediaKeys(); | 50 return createMediaKeys(); |
48 }).then(function(result) { | 51 }).then(function(result) { |
49 assert_equals(numMediaKeysCreated(), 4); | 52 assert_not_equals(result, null); |
| 53 mediaKeys3 = result; |
| 54 verifyMediaKeyAndMediaKeySessionCount(3, 0, 'Create third Me
diaKeys'); |
50 | 55 |
51 return createMediaKeys(); | 56 return createMediaKeys(); |
52 }).then(function(result) { | 57 }).then(function(result) { |
53 assert_equals(numMediaKeysCreated(), 5); | 58 assert_not_equals(result, null); |
| 59 mediaKeys4 = result; |
| 60 verifyMediaKeyAndMediaKeySessionCount(4, 0, 'Create fourth M
ediaKeys'); |
54 | 61 |
55 // |mediaKeys| refers to the most recently created MediaKeys | 62 return createMediaKeys(); |
56 // object. | 63 }).then(function(result) { |
57 mediaKeys = result; | 64 assert_not_equals(result, null); |
| 65 mediaKeys5 = result; |
| 66 verifyMediaKeyAndMediaKeySessionCount(5, 0, 'Create fifth Me
diaKeys'); |
58 | 67 |
59 // In order for the MediaKey objects to be garbage | 68 // In order for the MediaKey objects to be garbage |
60 // collected, it needs time to process any pending events. | 69 // collected, it needs time to process any pending events. |
61 return delayToAllowEventProcessingPromise(); | 70 return delayToAllowEventProcessingPromise(); |
62 }).then(function(result) { | 71 }).then(function() { |
63 assert_equals(numMediaKeysCreated(), 5); | 72 verifyMediaKeyAndMediaKeySessionCount(5, 0, 'All alive'); |
64 | 73 |
65 // As we only have a reference (|mediaKeys|) to the last | 74 // Now run garbage collection. Since we have references to |
66 // created MediaKeys object, the other 4 MediaKeys objects | 75 // all 5 MediaKeys, none will be collected. |
67 // are available to be garbage collected. | |
68 return createGCPromise(); | 76 return createGCPromise(); |
69 }).then(function(result) { | 77 }).then(function() { |
70 assert_equals(numMediaKeysCreated(), 1); | 78 verifyMediaKeyAndMediaKeySessionCount(5, 0, 'All still alive
'); |
71 assert_equals(typeof mediaKeys.createSession, 'function'); | 79 |
| 80 // Drop references to 4 of the MediaKeys. As we will only |
| 81 // have a reference to the last created MediaKeys object, |
| 82 // the other 4 MediaKeys objects are available to be |
| 83 // garbage collected. |
| 84 mediaKeys1 = null; |
| 85 mediaKeys2 = null; |
| 86 mediaKeys3 = null; |
| 87 mediaKeys4 = null; |
| 88 return createGCPromise(); |
| 89 }).then(function() { |
| 90 verifyMediaKeyAndMediaKeySessionCount(1, 0, 'Only 1 left'); |
72 | 91 |
73 // Release the last MediaKeys object created. | 92 // Release the last MediaKeys object created. |
74 mediaKeys = null; | 93 mediaKeys5 = null; |
75 | 94 |
76 // Run gc() again to reclaim the remaining MediaKeys object. | 95 // Run gc() again to reclaim the remaining MediaKeys object. |
77 return createGCPromise(); | 96 return createGCPromise(); |
78 }).then(function(result) { | 97 }).then(function() { |
79 assert_equals(numMediaKeysCreated(), 0); | 98 verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After final gc(
)'); |
80 test.done(); | |
81 }).catch(function(error) { | |
82 forceTestFailureFromPromise(test, error); | |
83 }); | 99 }); |
84 }, 'Multiple MediaKeys lifetime'); | 100 }, 'Multiple MediaKeys lifetime'); |
85 </script> | 101 </script> |
86 </body> | 102 </body> |
87 </html> | 103 </html> |
OLD | NEW |