DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
<!doctype html>
<html>
<head>
    <title>MediaRecorder Stop</title>
    <link rel="help" href="https://w3c.github.io/mediacapture-record/MediaRecorder.html#mediarecorder">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
</head>
<body>
<canvas id="canvas" width="200" height="200">
</canvas>
<script>
    function createVideoStream() {
        let canvas = document.getElementById("canvas");
        canvas.getContext('2d');
        return canvas.captureStream();
    }

    function recordEvents(target, events) {
        let arr = [];
        for (let ev of events) {
            target.addEventListener(ev, _ => arr.push(ev));
        }
        return arr;
    }

    // This function is used to check that elements of |actual| is a sub
    // sequence in the |expected| sequence.
    function assertSequenceIn(actual, expected) {
      let i = 0;
      for (event of actual) {
        const j = expected.slice(i).indexOf(event);
        assert_greater_than_equal(
            j, 0, "Sequence element " + event + " is not included in " +
            expected.slice(i));
        i = j;
      }
      return true;
    }

    promise_test(async t => {
        let video = createVideoStream();
        let recorder = new MediaRecorder(video);
        let events = recordEvents(recorder,
            ["start", "stop", "dataavailable", "pause", "resume", "error"]);
        assert_equals(video.getVideoTracks().length, 1, "video mediastream starts with one track");
        recorder.start();
        assert_equals(recorder.state, "recording", "MediaRecorder has been started successfully");
        video.getVideoTracks()[0].stop();
        assert_equals(recorder.state, "recording", "MediaRecorder state should be recording immediately following last track ending");
        let event = await new Promise(r => recorder.onstop = r);

        assert_equals(event.type, "stop", "the event type should be stop");
        assert_true(event.isTrusted, "isTrusted should be true when the event is created by C++");
        assert_equals(recorder.state, "inactive", "MediaRecorder is inactive after stop event");

        // As the test is written, it's not guaranteed that
        // onstart/ondataavailable is invoked, but it's fine if they are.
        // The stop element is guaranteed to be in events when we get here.
        assertSequenceIn(events, ["start", "dataavailable", "stop"]);
    }, "MediaRecorder will stop recording and fire a stop event when all tracks are ended");

    promise_test(async t => {
        let video = createVideoStream();
        let recorder = new MediaRecorder(video);
        let events = recordEvents(recorder,
            ["start", "stop", "dataavailable", "pause", "resume", "error"]);
        recorder.start();
        assert_equals(recorder.state, "recording", "MediaRecorder has been started successfully");
        recorder.stop();
        assert_equals(recorder.state, "inactive", "MediaRecorder state should be inactive immediately following stop() call");

        let event = await new Promise (r => recorder.onstop = r);
        assert_equals(event.type, "stop", "the event type should be stop");
        assert_true(event.isTrusted, "isTrusted should be true when the event is created by C++");
        assert_equals(recorder.state, "inactive", "MediaRecorder is inactive after stop event");

        // As the test is written, it's not guaranteed that
        // onstart/ondataavailable is invoked, but it's fine if they are.
        // The stop element is guaranteed to be in events when we get here.
        assertSequenceIn(events, ["start", "dataavailable", "stop"]);
    }, "MediaRecorder will stop recording and fire a stop event when stop() is called");

    promise_test(async t => {
        const recorder = new MediaRecorder(createVideoStream());
        assert_throws_dom("InvalidStateError", () => { recorder.stop(); });
        await Promise.race([
            new Promise((_, reject) => recorder.onstop =
                _ => reject(new Error("onstop should never have been called"))),
            new Promise(r => t.step_timeout(r, 0))]);
    }, "MediaRecorder will fire an exception when stopped after creation");

    promise_test(async t => {
        const recorder = new MediaRecorder(createVideoStream());
        recorder.start();
        recorder.stop();
        let event = await new Promise(r => recorder.onstop = r);
        assert_throws_dom("InvalidStateError", () => { recorder.stop(); });
        await Promise.race([
            new Promise((_, reject) => recorder.onstop =
                _ => reject(new Error("onstop should never have been called"))),
            new Promise(r => t.step_timeout(r, 0))]);
    }, "MediaRecorder will fire an exception when stopped after having just been stopped");

    promise_test(async t => {
        const stream = createVideoStream();
        const recorder = new MediaRecorder(stream);
        recorder.start();
        stream.getVideoTracks()[0].stop();
        let event = await new Promise(r => recorder.onstop = r);
        assert_throws_dom("InvalidStateError", () => { recorder.stop(); });
        await Promise.race([
            new Promise((_, reject) => recorder.onstop =
                _ => reject(new Error("onstop should never have been called"))),
            new Promise(r => t.step_timeout(r, 0))]);
    }, "MediaRecorder will fire an exception when stopped after having just been spontaneously stopped");

</script>
</body>
</html>