summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-record/MediaRecorder-pause-resume.html
blob: 84efe52e06b7788d71bc44fbec7c9963216f2734 (plain)
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
<!doctype html>
<html>
<head>
    <title>MediaRecorder Pause and Resume</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>
    <script src="utils/sources.js"></script>
</head>
<body>
<script>
    function recordEvents(target, events) {
        let arr = [];
        for (let ev of events) {
            target.addEventListener(ev, _ => arr.push(ev));
        }
        return arr;
    }

    promise_test(async t => {
        const {stream: video, control} = createVideoStream(t);
        control.addVideoFrame();
        const recorder = new MediaRecorder(video);
        const events = recordEvents(recorder,
            ["start", "stop", "dataavailable", "pause", "resume", "error"]);

        recorder.start();
        assert_equals(recorder.state, "recording", "MediaRecorder has been started successfully");
        await new Promise(r => recorder.onstart = r);

        recorder.pause();
        assert_equals(recorder.state, "paused", "MediaRecorder should be paused immediately following pause()");

        // A second call to pause should be idempotent
        recorder.pause();
        assert_equals(recorder.state, "paused", "MediaRecorder should be paused immediately following pause()");

        let event = await new Promise(r => recorder.onpause = r);
        assert_equals(event.type, "pause", "the event type should be pause");
        assert_true(event.isTrusted, "isTrusted should be true when the event is created by C++");

        recorder.resume();
        assert_equals(recorder.state, "recording", "MediaRecorder state should be recording immediately following resume() call");

        // A second call to resume should be idempotent
        recorder.resume();
        assert_equals(recorder.state, "recording", "MediaRecorder state should be recording immediately following resume() call");

        event = await new Promise(r => recorder.onresume = r);
        assert_equals(event.type, "resume", "the event type should be resume");
        assert_true(event.isTrusted, "isTrusted should be true when the event is created by C++");

        recorder.stop();
        await new Promise(r => recorder.onstop = r);

        assert_array_equals(events, ["start", "pause", "resume", "dataavailable", "stop"],
            "Should have gotten expected events");
    }, "MediaRecorder handles pause() and resume() calls appropriately in state and events");

    promise_test(async t => {
        const {stream: video, control} = createVideoStream(t);
        control.addVideoFrame();
        const recorder = new MediaRecorder(video);
        const events = recordEvents(recorder,
            ["start", "stop", "dataavailable", "pause", "resume", "error"]);

        recorder.start();
        assert_equals(recorder.state, "recording", "MediaRecorder has been started successfully");
        await new Promise(r => recorder.onstart = r);

        recorder.pause();
        assert_equals(recorder.state, "paused", "MediaRecorder should be paused immediately following pause()");
        let event = await new Promise(r => recorder.onpause = r);
        assert_equals(event.type, "pause", "the event type should be pause");
        assert_true(event.isTrusted, "isTrusted should be true when the event is created by C++");

        recorder.stop();
        assert_equals(recorder.state, "inactive", "MediaRecorder should be inactive after being stopped");
        await new Promise(r => recorder.onstop = r);

        recorder.start();
        assert_equals(recorder.state, "recording", "MediaRecorder has been started successfully");
        await new Promise(r => recorder.onstart = r);

        assert_array_equals(events, ["start", "pause", "dataavailable", "stop", "start"],
            "Should have gotten expected events");
    }, "MediaRecorder handles stop() in paused state appropriately");
</script>
</body>
</html>