Commit 0afa72c1 authored by jrummell@chromium.org's avatar jrummell@chromium.org

Add lifetime tests for MediaKeySession

Since MediaKeySessions are ActiveDOMObjects and can be counted, adding tests
to verify that garbage collection frees the sessions when they are no longer
needed. One test simply uses JS references to keep the sessions around, while
the other calls release() on the session to free them.

BUG=341567
TEST=new tests pass

Review URL: https://codereview.chromium.org/209103002

git-svn-id: svn://svn.chromium.org/blink/trunk@170341 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 5d5c49be
This is a testharness.js-based test.
PASS MediaKeySession lifetime without release()
Harness: the test ran to completion.
<!DOCTYPE html>
<html>
<head>
<title>Test MediaKeySession lifetime</title>
<script src="../w3c-media-utils.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../resources/gc.js"></script>
</head>
<body>
<div id="log"></div>
<script>
// Since MediaKeySession (but not MediaKeys) are ActiveDOMObjects,
// we can determine when they are garbage collected.
// MediaKeySessions remain as long as:
// JavaScript has a reference to it
// OR (MediaKeys is around AND the session has not received a close() event)
test(function()
{
var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
function numActiveDOMObjectsCreated()
{
return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
}
var mediaKeys = new MediaKeys("org.w3.clearkey");
assert_equals(mediaKeys.keySystem, 'org.w3.clearkey');
// Verify MediaKeys are not ActiveDOMObjects.
assert_equals(numActiveDOMObjectsCreated(), 0, "MediaKeys created an ActiveDOMObject");
// Create 3 sessions.
var mediaKeySession1 = mediaKeys.createSession("video/webm", initData);
assert_equals(numActiveDOMObjectsCreated(), 1);
var mediaKeySession2 = mediaKeys.createSession("video/webm", initData);
assert_equals(numActiveDOMObjectsCreated(), 2);
var mediaKeySession3 = mediaKeys.createSession("video/webm", initData);
assert_equals(numActiveDOMObjectsCreated(), 3);
// Run gc(). All sessions should remain as we have a reference
// to each one.
gc();
assert_equals(numActiveDOMObjectsCreated(), 3);
// Now drop references to 2 of the sessions. Even though we
// don't have a reference, MediaKeys is still around (and the
// sessions aren't closed), so the objects won't be collected.
mediaKeySession2 = null;
mediaKeySession3 = null;
gc();
assert_equals(numActiveDOMObjectsCreated(), 3);
// Now drop the reference to MediaKeys. It and the 2
// unreferenced sessions should be collected. Since
// MediaKeySessions remain alive as long as MediaKeys is around,
// it is possible that gc() checks one or both MediaKeySession
// objects first, and doesn't collect them since MediaKeys
// hasn't been collected yet. Thus run gc() twice, to ensure
// that the unreferenced MediaKeySession objects get collected.
mediaKeys = null;
gc();
gc();
assert_equals(numActiveDOMObjectsCreated(), 1);
// Drop the reference to the first session. It should get
// collected now since MediaKeys is gone.
mediaKeySession1 = null;
gc();
assert_equals(numActiveDOMObjectsCreated(), 0);
}, "MediaKeySession lifetime without release()");
</script>
</body>
</html>
EVENT(close)
EVENT(close)
This is a testharness.js-based test.
PASS MediaKeySession lifetime after release()
Harness: the test ran to completion.
EVENT(close)
EVENT(close)
This is a testharness.js-based test.
PASS MediaKeySession lifetime after release() without references
Harness: the test ran to completion.
<!DOCTYPE html>
<html>
<head>
<title>Test MediaKeySession lifetime</title>
<script src="../w3c-media-utils.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../resources/gc.js"></script>
</head>
<body>
<div id="log"></div>
<script>
// Since MediaKeySession (but not MediaKeys) are ActiveDOMObjects,
// we can determine when they are garbage collected.
// MediaKeySessions remain as long as:
// JavaScript has a reference to it
// OR (MediaKeys is around AND the session has not received a close() event)
async_test(function(test)
{
var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
function numActiveDOMObjectsCreated()
{
return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
}
// Create 2 sessions.
var mediaKeys = new MediaKeys("org.w3.clearkey");
var mediaKeySession1 = mediaKeys.createSession('video/webm', initData);
assert_equals(numActiveDOMObjectsCreated(), 1);
var mediaKeySession2 = mediaKeys.createSession('video/webm', initData);
assert_equals(numActiveDOMObjectsCreated(), 2);
var openSessions = 2;
// Release the sessions. Once the close() event is received,
// they should get garbage collected as there are no JS
// references to them.
mediaKeySession1.release();
waitForEventAndRunStep("close", mediaKeySession1, onClose, test);
mediaKeySession1 = null;
mediaKeySession2.release();
waitForEventAndRunStep("close", mediaKeySession2, onClose, test);
mediaKeySession2 = null;
function onClose(event)
{
assert_equals(mediaKeySession1, null);
assert_equals(mediaKeySession2, null);
--openSessions;
if (openSessions > 0)
return;
// Delay to give time for close to complete since
// event.target is a reference to the MediaKeySession.
setTimeout(finish, 1);
}
function finish()
{
// Since both sessions have been closed and there are no
// JS references to them, they should be garbage-collected.
assert_equals(openSessions, 0);
assert_not_equals(mediaKeys, null);
assert_equals(mediaKeySession1, null);
assert_equals(mediaKeySession2, null);
gc();
assert_equals(numActiveDOMObjectsCreated(), 0);
test.done();
}
}, "MediaKeySession lifetime after release() without references");
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Test MediaKeySession lifetime</title>
<script src="../w3c-media-utils.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../../resources/gc.js"></script>
</head>
<body>
<div id="log"></div>
<script>
// Since MediaKeySession (but not MediaKeys) are ActiveDOMObjects,
// we can determine when they are garbage collected.
// MediaKeySessions remain as long as:
// JavaScript has a reference to it
// OR (MediaKeys is around AND the session has not received a close() event)
async_test(function(test)
{
var initData = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7]);
var startingActiveDOMObjectCount = window.internals.activeDOMObjectCount(document);
function numActiveDOMObjectsCreated()
{
return window.internals.activeDOMObjectCount(document) - startingActiveDOMObjectCount;
}
// Create 2 sessions.
var mediaKeys = new MediaKeys("org.w3.clearkey");
var mediaKeySession1 = mediaKeys.createSession('video/webm', initData);
assert_equals(numActiveDOMObjectsCreated(), 1);
var mediaKeySession2 = mediaKeys.createSession('video/webm', initData);
assert_equals(numActiveDOMObjectsCreated(), 2);
var openSessions = 2;
// Release the sessions. Once the close() event is received,
// only the JS reference to them keeps them around.
mediaKeySession1.release();
waitForEventAndRunStep("close", mediaKeySession1, onClose, test);
mediaKeySession2.release();
waitForEventAndRunStep("close", mediaKeySession2, onClose, test);
function onClose(event)
{
--openSessions;
if (openSessions > 0)
return;
// Delay to give time for close to complete since
// event.target is a reference to the MediaKeySession.
setTimeout(finish, 1);
}
function finish()
{
// Since both sessions have been closed, dropping the
// reference to them from JS will result in the session
// being garbage-collected.
assert_equals(openSessions, 0);
assert_not_equals(mediaKeys, null);
assert_equals(numActiveDOMObjectsCreated(), 2);
mediaKeySession1 = null;
gc();
assert_equals(numActiveDOMObjectsCreated(), 1, "mediaKeySession1 not collected");
mediaKeySession2 = null;
gc();
assert_equals(numActiveDOMObjectsCreated(), 0, "mediaKeySession2 not collected");
test.done();
}
}, "MediaKeySession lifetime after release()");
</script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment