Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / media / encrypted-media / encrypted-media-lifetime-mediakeys-with-session.html
1 <!DOCTYPE html>
2 <html>
3     <head>
4         <title>Test MediaKeys lifetime when adding a session</title>
5         <script src="encrypted-media-utils.js"></script>
6         <script src="../../resources/testharness.js"></script>
7         <script src="../../resources/testharnessreport.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 the test 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
20             //       AND the session has not received a close() event)
21             // In the tests below, we do not close any session nor keep a
22             // Javascript reference to any session, so MediaKeySessions remain
23             // as long as the associated MediaKeys object is around.
24
25             // For this test, create a MediaKeySession (which is an
26             // ActiveDOMObject) and verify lifetime.
27             async_test(function(test)
28             {
29                 var mediaKeys;
30                 var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
31
32                 function numActiveDOMObjectsCreated()
33                 {
34                     return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
35                 }
36
37                 // Create a MediaKeys object with a session.
38                 navigator.requestMediaKeySystemAccess('org.w3.clearkey').then(function(access) {
39                     assert_equals(access.keySystem, 'org.w3.clearkey');
40                     return access.createMediaKeys();
41                 }).then(function(result) {
42                     mediaKeys = result;
43
44                     // Verify MediaKeys are not an ActiveDOMObject.
45                     assert_equals(numActiveDOMObjectsCreated(), 0, 'MediaKeys.create()');
46
47                     var initDataType = getInitDataType();
48                     var mediaKeySession = mediaKeys.createSession();
49                     return mediaKeySession.generateRequest(initDataType, getInitData(initDataType));
50                 }).then(function() {
51                     // 1 MediaKeySession.
52                     assert_equals(numActiveDOMObjectsCreated(), 1, 'MediaKeys.createSession()');
53
54                     // Run gc(), should not affect MediaKeys object nor the
55                     // session since we still have a reference to it.
56                     return createGCPromise();
57                 }).then(function(result) {
58                     assert_equals(typeof mediaKeys.createSession, 'function');
59                     // Ensure that MediaKeySession (but not PromiseResolver) is
60                     // still around.
61                     assert_equals(numActiveDOMObjectsCreated(), 1, 'After gc()');
62
63                     // Drop reference to the MediaKeys object and run gc()
64                     // again. Object should be collected this time. Since
65                     // MediaKeySessions remain alive as long as MediaKeys is
66                     // around, it is possible that gc() checks the
67                     // MediaKeySession object first, and doesn't collect it
68                     // since MediaKeys hasn't been collected yet. Thus run gc()
69                     // twice, to ensure that the unreferenced MediaKeySession
70                     // object get collected.
71                     mediaKeys = null;
72                     return createGCPromise();
73                 }).then(function(result) {
74                     return createGCPromise();
75                 }).then(function(result) {
76                     assert_equals(numActiveDOMObjectsCreated(), 0);
77                     test.done();
78                 }).catch(function(error) {
79                     forceTestFailureFromPromise(test, error);
80                 });
81             }, 'MediaKeys lifetime with session');
82         </script>
83     </body>
84 </html>