blob: f888949c23707f92b2229cd9e5816e5dd1162b9e [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test adding and removing tracks.");
var stream1;
var stream2;
var audioTrack;
var videoTrack;
function error() {
testFailed('Stream generation failed.');
finishJSTest();
}
function getUserMedia(dictionary, callback) {
try {
navigator.webkitGetUserMedia(dictionary, callback, error);
} catch (e) {
testFailed('webkitGetUserMedia threw exception :' + e);
finishJSTest();
}
}
function tryAddTrack(stream, track) {
try {
stream.addTrack(track);
} catch (exception) {
testFailed("addTrack threw an exception.");
finishJSTest();
}
}
function tryRemoveTrack(stream, track) {
try {
stream.removeTrack(track);
} catch (exception) {
testFailed("removeTrack threw an exception.");
finishJSTest();
}
}
function shouldNotFire() {
testFailed("\"addtrack\" or \"removetrack\" events should not fire as a result of local addTrack() or removeTrack() operations.");
}
function gotStream2(s) {
stream2 = s;
shouldBe('stream1.getAudioTracks().length', '1');
shouldBe('stream1.getVideoTracks().length', '1');
shouldBe('stream2.getAudioTracks().length', '1');
shouldBe('stream2.getVideoTracks().length', '1');
stream1.onaddtrack = shouldNotFire;
stream1.onremovetrack = shouldNotFire;
audioTrack = stream1.getAudioTracks()[0];
videoTrack = stream1.getVideoTracks()[0];
debug("<br>*** remove an audio track");
tryRemoveTrack(stream1, audioTrack);
shouldBe('stream1.getAudioTracks().length', '0');
shouldBeNull('stream1.getTrackById(audioTrack.id)');
debug("<br>*** remove a non-existent (already removed) track (should not throw)");
tryRemoveTrack(stream1, audioTrack);
debug("<br>*** add the track back to the stream");
tryAddTrack(stream1, audioTrack);
debug("<br>*** remove a video track");
tryRemoveTrack(stream1, videoTrack);
shouldBe('stream1.getVideoTracks().length', '0');
shouldBeNull('stream1.getTrackById(videoTrack.id)');
debug("<br>*** remove a non-existent (already removed) track (should not throw)");
tryRemoveTrack(stream1, videoTrack);
debug("<br>*** add the track back to the stream");
tryAddTrack(stream1, videoTrack);
debug("<br>*** verify added tracks");
shouldBe('stream1.getAudioTracks().length', '1');
shouldBe('stream1.getVideoTracks().length', '1');
shouldBe('stream1.getAudioTracks()[0].kind', 'audioTrack.kind');
shouldBe('stream1.getAudioTracks()[0].label', 'audioTrack.label');
shouldBe('stream1.getVideoTracks()[0].kind', 'videoTrack.kind');
shouldBe('stream1.getVideoTracks()[0].label', 'videoTrack.label');
debug("<br>*** try adding a stream's own tracks again (nothing should happen)");
tryAddTrack(stream1, stream1.getAudioTracks()[0]);
tryAddTrack(stream1, stream1.getVideoTracks()[0]);
shouldBe('stream1.getAudioTracks().length', '1');
shouldBe('stream1.getVideoTracks().length', '1');
debug("<br>*** add tracks from another stream");
tryAddTrack(stream1, stream2.getAudioTracks()[0]);
tryAddTrack(stream1, stream2.getVideoTracks()[0]);
debug("<br>*** verify added tracks");
shouldBe('stream1.getAudioTracks().length', '2');
shouldBe('stream1.getVideoTracks().length', '2');
debug("<br>*** remove all tracks, stream.ended should return true");
tryRemoveTrack(stream2, stream2.getAudioTracks()[0]);
tryRemoveTrack(stream2, stream2.getVideoTracks()[0]);
shouldBe('stream2.getAudioTracks().length', '0');
shouldBe('stream2.getVideoTracks().length', '0');
shouldBeTrue('stream2.ended');
debug("<br>*** it should be impossible to add a track after the stream has ended");
shouldThrow('stream2.addTrack(audioTrack)', '"Error: InvalidStateError: DOM Exception 11"');
shouldThrow('stream2.removeTrack(audioTrack)', '"Error: InvalidStateError: DOM Exception 11"');
setTimeout(finishJSTest, 0);
}
function gotStream1(s) {
stream1 = s;
getUserMedia({audio:true, video:true}, gotStream2);
}
getUserMedia({audio:true, video:true}, gotStream1);
window.jsTestIsAsync = true;
window.successfullyParsed = true;
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>