chromium/third_party/blink/web_tests/media/encrypted-media/encrypted-media-reset-src-during-setmediakeys.html

<!DOCTYPE html>
<html>
    <head>
        <title>Reset src during setMediaKeys()</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <video></video>
        <script>
            async_test(function(test)
            {
                var mediaKeys;
                var video = document.querySelector('video');
                assert_not_equals(video, null);
                var encryptedEventIndex = 0;

                // Content to be played. These files must be the same format.
                var content = '../content/test-encrypted.webm';
                var alternateContent = '../content/test-encrypted-different-av-keys.webm';

                function setMediaKeys() {
                  // Create a MediaKeys object and assign it to video.
                  video.setMediaKeys(mediaKeys)
                      .then(function(result) {
                          // setMediaKeys() should succeed even if src is reset before setMediaKeys() promise is resolved.
                          assert_not_equals(video.mediaKeys, null, 'set successfully');
                          assert_true(video.mediaKeys === mediaKeys);
                      })
                      .catch(function(error) {
                          forceTestFailureFromPromise(test, error);
                      });
                }

                var onEncrypted = function(event)
                {
                    encryptedEventIndex++;

                    // This event is fired once for the audio stream and once
                    // for the video stream each time .src is set.
                    if (encryptedEventIndex == 2) {
                        assert_equals(video.mediaKeys, null);
                        setMediaKeys();

                        // Reset src without waiting for setMediaKeys() to finish.
                        video.src = alternateContent;
                    } else if (encryptedEventIndex == 4) {
                        // Finished second video.
                        test.done();
                    }
                };

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getConfigurationForFile(content))
                    .then(function(access) {
                        assert_equals(access.keySystem, 'org.w3.clearkey');
                        return access.createMediaKeys();
                    })
                    .then(function(result) {
                        mediaKeys = result;
                        assert_not_equals(mediaKeys, null);

                        // Set src without setMediaKeys().
                        video.src = content;
                        waitForEventAndRunStep('encrypted', video, onEncrypted, test);
                    })
                    .catch(function(error) {
                        forceTestFailureFromPromise(test, error);
                    })
            }, 'Reset src during setMediaKeys().');
        </script>
    </body>
</html>