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>
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.
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 close() 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.
24 // For this test, simply verify that creating and destroying
25 // MediaKeys doesn't crash.
28 // Create a MediaKeys object and free immediately.
29 var mediaKeys = new MediaKeys("org.w3.clearkey");
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");
37 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey');
41 }, "MediaKeys lifetime");
43 // For this test, create a MediaKeySession (which is an
44 // ActiveDOMObject) and verify lifetime.
48 var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
49 var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
51 function numActiveDOMObjectsCreated()
53 return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
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);
61 // Run gc(), should not affect MediaKeys object nor the session
62 // since we still have a reference to it.
64 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey');
65 assert_equals(numActiveDOMObjectsCreated(), 1);
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.
77 assert_equals(numActiveDOMObjectsCreated(), 0);
79 }, "MediaKeys lifetime with session");
81 // For this test, create several MediaKeys (with MediaKeySessions so
82 // they can be counted) and verify lifetime.
86 var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
87 var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
89 function numActiveDOMObjectsCreated()
91 return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
94 // Create a few MediaKeys objects. Only keep a reference to the
96 for (var i = 0; i < 5; ++i) {
97 mediaKeys = new MediaKeys("org.w3.clearkey");
98 mediaKeys.createSession("video/webm", initData);
100 assert_equals(numActiveDOMObjectsCreated(), 5);
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.
110 assert_equals(numActiveDOMObjectsCreated(), 1);
111 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey');
113 // Release the last MediaKeys object created.
117 assert_equals(numActiveDOMObjectsCreated(), 0);
118 }, "Multiple MediaKeys lifetime");