forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaRecorder-events-and-exceptions.html
116 lines (99 loc) · 4.48 KB
/
MediaRecorder-events-and-exceptions.html
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
<!doctype html>
<html>
<head>
<title>MediaRecorder events and exceptions</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="320" height="320"></canvas>
<script>
// This test exercises the MediaRecorder API event sequence:
// onStart -> onPause -> onResume -> onDataAvailable -> onStop
// verifying the |state| and a few exceptions that are supposed to be thrown
// when doing the wrong thing.
function createVideoStream() {
let canvas = document.getElementById("canvas");
canvas.getContext('2d').lineTo(10, 10);
return canvas.captureStream();
}
function drawSomethingOnCanvas() {
let canvas = document.getElementById("canvas");
// Drawing something on the canvas generates a frame in its captured stream.
let context = canvas.getContext("2d");
context.fillStyle = "red";
context.fillRect(0, 0, 10, 10);
}
async_test(test => {
recorderOnUnexpectedEvent = test.step_func(() => {
assert_unreached('Unexpected event.');
});
recorderOnDataAvailable = test.step_func(event => {
// TODO(mcasas): ondataavailable might never be pinged with an empty Blob
// data on recorder.stop(), see http://crbug.com/54428
assert_equals(recorder.state, "inactive");
assert_equals(event.data.size, 0, 'We should have gotten an empty Blob');
});
recorderOnStop = test.step_func(function() {
assert_equals(recorder.state, "inactive");
assert_throws("InvalidStateError", function() { recorder.stop() },
"recorder cannot be stop()ped in |inactive| state");
assert_throws("InvalidStateError", function() { recorder.pause() },
"recorder cannot be pause()ed in |inactive| state");
assert_throws("InvalidStateError", function() { recorder.resume() },
"recorder cannot be resume()d in |inactive| state");
assert_throws("InvalidStateError", function() { recorder.requestData() },
"cannot requestData() if recorder is in |inactive| state");
recorder.onstop = recorderOnUnexpectedEvent;
test.done();
});
recorderOnResume = test.step_func(function() {
assert_equals(recorder.state, "recording");
recorder.onresume = recorderOnUnexpectedEvent;
recorder.onstop = recorderOnStop;
recorder.stop();
});
recorderOnPause = test.step_func(function() {
assert_equals(recorder.state, "paused");
recorder.onpause = recorderOnUnexpectedEvent;
recorder.onresume = recorderOnResume;
recorder.resume();
});
recorderOnStart = test.step_func(function() {
assert_equals(recorder.state, "recording");
recorder.onstart = recorderOnUnexpectedEvent;
recorder.onpause = recorderOnPause;
recorder.pause();
});
let stream = createVideoStream();
assert_equals(stream.getAudioTracks().length, 0);
assert_equals(stream.getVideoTracks().length, 1);
assert_equals(stream.getVideoTracks()[0].readyState, 'live');
assert_throws("NotSupportedError",
function() {
recorder =
new MediaRecorder(stream, {mimeType : "video/invalid"});
},
"recorder should throw() with unsupported mimeType");
let recorder = new MediaRecorder(stream);
assert_equals(recorder.state, "inactive");
assert_throws("InvalidStateError", function(){recorder.stop()},
"recorder cannot be stop()ped in |inactive| state");
assert_throws("InvalidStateError", function(){recorder.pause()},
"recorder cannot be pause()ed in |inactive| state");
assert_throws("InvalidStateError", function(){recorder.resume()},
"recorder cannot be resume()d in |inactive| state");
assert_throws("InvalidStateError", function(){recorder.requestData()},
"cannot requestData() if recorder is in |inactive| state");
drawSomethingOnCanvas();
recorder.onstop = recorderOnUnexpectedEvent;
recorder.onpause = recorderOnUnexpectedEvent;
recorder.onresume = recorderOnUnexpectedEvent;
recorder.onerror = recorderOnUnexpectedEvent;
recorder.ondataavailable = recorderOnDataAvailable;
recorder.onstart = recorderOnStart;
recorder.start();
assert_equals(recorder.state, "recording");
});
</script>