chromium/third_party/blink/web_tests/media/encrypted-media/encrypted-media-lifetime-mediakeys-with-session.html

<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeys lifetime when adding a session</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // MediaKeySessions remain as long as:
            //   JavaScript has a reference to it
            //   OR (MediaKeys is around
            //       AND the session has not received a close() event)
            // In the tests below, we do not close any session nor keep a
            // Javascript reference to any session, so MediaKeySessions remain
            // as long as the associated MediaKeys object is around.

            // For this test, create a MediaKeySession and verify lifetime.
            promise_test(function(test)
            {
                var initDataType;
                var initData;
                var mediaKeys;

                // Even though there should be no existing objects, start by
                // running gc() and verifying that none exist. This also
                // allows the failures to be reported from inside a promise
                // so that the test fails properly.
                return createGCPromise().then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After initial gc()');

                    // Create a MediaKeys object with a session.
                    return navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration());
                }).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(result) {
                    mediaKeys = result;
                    verifyMediaKeyAndMediaKeySessionCount(1, 0, 'MediaKeys.create()');

                    var mediaKeySession = mediaKeys.createSession();
                    return mediaKeySession.generateRequest(initDataType, initData);
                }).then(function() {
                    verifyMediaKeyAndMediaKeySessionCount(1, 1, 'MediaKeys.createSession()');

                    // Run gc(), should not affect MediaKeys object nor the
                    // session since we still have a reference to it.
                    return createGCPromise();
                }).then(function(result) {
                    assert_equals(typeof mediaKeys.createSession, 'function');
                    verifyMediaKeyAndMediaKeySessionCount(1, 1, 'After gc()');

                    // Drop reference to the MediaKeys object and run gc()
                    // again. Object should be collected this time. Since
                    // MediaKeySessions remain alive as long as MediaKeys is
                    // around, it is possible that gc() checks the
                    // MediaKeySession object first, and doesn't collect it
                    // since MediaKeys hasn't been collected yet. Thus run gc()
                    // twice, to ensure that the unreferenced MediaKeySession
                    // object get collected.
                    mediaKeys = null;
                    return createGCPromise();
                }).then(function(result) {
                    return createGCPromise();
                }).then(function(result) {
                    verifyMediaKeyAndMediaKeySessionCount(0, 0, 'After final gc()');
                });
            }, 'MediaKeys lifetime with session');
        </script>
    </body>
</html>