OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <html> | |
3 <head> | |
4 <title>Test MediaKeys lifetime</title> | |
5 <script src="../../resources/testharness.js"></script> | |
6 <script src="../../resources/testharnessreport.js"></script> | |
7 <script src="../../resources/gc.js"></script> | |
8 </head> | |
9 <body> | |
10 <div id="log"></div> | |
11 <script> | |
12 // Since MediaKeys are not ActiveDOMObjects, it is hard to | |
13 // determine when they are garbage collected. For some tests below | |
14 // a MediaKeySession (which is an ActiveDOMObject) is added so | |
15 // there is something to count. | |
16 | |
17 // MediaKeySessions remain as long as: | |
18 // JavaScript has a reference to it | |
19 // OR (MediaKeys is around AND the session has not received a clos e() event) | |
20 // In the tests below, we do not close any session nor keep a | |
21 // Javascript reference to any session, so MediaKeySessions remain | |
22 // as long as the associated MediaKeys object is around. | |
23 | |
24 // For this test, simply verify that creating and destroying | |
25 // MediaKeys doesn't crash. | |
26 test(function() | |
27 { | |
28 // Create a MediaKeys object and free immediately. | |
29 var mediaKeys = new MediaKeys("org.w3.clearkey"); | |
30 mediaKeys = null; | |
31 gc(); | |
32 | |
33 // Create a MediaKeys object and make sure gc doesn't free it | |
34 // as long as we have a reference. | |
35 mediaKeys = new MediaKeys("org.w3.clearkey"); | |
36 gc(); | |
37 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey'); | |
38 mediaKeys = null; | |
39 gc(); | |
40 | |
41 }, "MediaKeys lifetime"); | |
42 | |
43 // For this test, create a MediaKeySession (which is an | |
44 // ActiveDOMObject) and verify lifetime. | |
45 test(function() | |
46 { | |
47 var mediaKeys; | |
48 var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); | |
49 var startingActiveDOMObjectCount = window.internals.activeDOMObj ectCount(document); | |
50 | |
51 function numActiveDOMObjectsCreated() | |
ddorwin
2014/04/01 23:44:52
This could be defined outside the individual tests
jrummell
2014/04/02 01:09:15
Had separate functions as outside the test they wo
| |
52 { | |
53 return window.internals.activeDOMObjectCount(document) - sta rtingActiveDOMObjectCount; | |
54 } | |
55 | |
56 // Create a MediaKeys object with a session. | |
57 mediaKeys = new MediaKeys("org.w3.clearkey"); | |
58 mediaKeys.createSession("video/webm", initData); | |
59 assert_equals(numActiveDOMObjectsCreated(), 1); | |
60 | |
61 // Run gc(), should not affect MediaKeys object nor the session | |
62 // since we still have a reference to it. | |
63 gc(); | |
64 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey'); | |
65 assert_equals(numActiveDOMObjectsCreated(), 1); | |
66 | |
67 // Drop reference to the MediaKeys object and run gc again. | |
68 // Object should be collected this time. Since | |
69 // MediaKeySessions remain alive as long as MediaKeys is around, | |
70 // it is possible that gc() checks the MediaKeySession object | |
71 // first, and doesn't collect it since MediaKeys hasn't been | |
72 // collected yet. Thus run gc() twice, to ensure that the | |
73 // unreferenced MediaKeySession object get collected. | |
74 mediaKeys = null; | |
75 gc(); | |
76 gc(); | |
77 assert_equals(numActiveDOMObjectsCreated(), 0); | |
78 | |
79 }, "MediaKeys lifetime with session"); | |
80 | |
81 // For this test, create several MediaKeys (with MediaKeySessions so | |
82 // they can be counted) and verify lifetime. | |
83 test(function() | |
84 { | |
85 var mediaKeys; | |
86 var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]); | |
87 var startingActiveDOMObjectCount = window.internals.activeDOMObj ectCount(document); | |
88 | |
89 function numActiveDOMObjectsCreated() | |
90 { | |
91 return window.internals.activeDOMObjectCount(document) - sta rtingActiveDOMObjectCount; | |
92 } | |
93 | |
94 // Create a few MediaKeys objects. Only keep a reference to the | |
95 // last one created. | |
96 for (var i = 0; i < 5; ++i) { | |
97 mediaKeys = new MediaKeys("org.w3.clearkey"); | |
98 mediaKeys.createSession("video/webm", initData); | |
99 } | |
100 assert_equals(numActiveDOMObjectsCreated(), 5); | |
101 | |
102 // All but the last one created should be garbage collected. | |
103 // Since MediaKeySessions remain alive as long as MediaKeys is | |
104 // around, it is possible that gc() checks the MediaKeySession | |
105 // object first, and doesn't collect it since MediaKeys hasn't | |
106 // been collected yet. Thus run gc() twice, to ensure that the | |
107 // unreferenced MediaKeySession objects get collected. | |
108 gc(); | |
109 gc(); | |
110 assert_equals(numActiveDOMObjectsCreated(), 1); | |
111 | |
112 // Last MediaKeys object created should still be referenced. | |
ddorwin
2014/04/01 23:44:52
This comment probably makes sense above 110. Reall
jrummell
2014/04/02 01:09:15
Removed comment. Basically restating what is on li
| |
113 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey'); | |
114 | |
115 // Release the last MediaKeys object created. | |
116 mediaKeys = null; | |
117 gc(); | |
118 gc(); | |
119 assert_equals(numActiveDOMObjectsCreated(), 0); | |
120 }, "Multiple MediaKeys lifetime"); | |
121 </script> | |
ddorwin
2014/04/01 23:44:52
Do we have a test where the MK is dereferenced, we
jrummell
2014/04/02 01:09:15
encrypted-media-lifetime-mediakeysession-reference
| |
122 </body> | |
ddorwin
2014/04/01 23:44:52
In addition to that, we should be able to have jus
jrummell
2014/04/02 01:09:15
Good idea for another session test. I'll do that i
| |
123 </html> | |
OLD | NEW |