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