Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / media / encrypted-media / encrypted-media-lifetime-mediakeys.html
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 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.
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.activeDOMObjectCount(document);
50
51                 function numActiveDOMObjectsCreated()
52                 {
53                     return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
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.activeDOMObjectCount(document);
88
89                 function numActiveDOMObjectsCreated()
90                 {
91                     return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
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                 assert_equals(mediaKeys.keySystem, 'org.w3.clearkey');
112
113                 // Release the last MediaKeys object created.
114                 mediaKeys = null;
115                 gc();
116                 gc();
117                 assert_equals(numActiveDOMObjectsCreated(), 0);
118             }, "Multiple MediaKeys lifetime");
119         </script>
120     </body>
121 </html>