Calling removeTrack on different RTCPeerConnection should throw InvalidAccessError
[WebKit-https.git] / LayoutTests / fast / mediastream / RTCPeerConnection-add-removeTrack.html
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3     <head>
4         <script src="../../resources/js-test-pre.js"></script>
5     </head>
6     <body>
7         <script>
8             var stream;
9             var stream2;
10             var track;
11             var track2;
12             var sender;
13             var sender2;
14             var senderFromPc2
15             var receiver;
16             var transceiver;
17
18             description("Test basic behavior of RTCPeerConnection.add/removeTrack()");
19
20             if (window.testRunner)
21                 testRunner.setUserMediaPermission(true);
22             else {
23                 debug("This test can not be run without the testRunner");
24                 finishJSTest();
25             }
26
27             var pc = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});
28             var pc2 = new RTCPeerConnection({iceServers:[{urls:'stun:foo.com'}]});
29
30             navigator.mediaDevices.getUserMedia({ "video": true })
31             .then(function (s) {
32                 stream = s;
33                 track = stream.getTracks()[0];
34
35                 debug("Test bad addTrack() arguments");
36                 shouldThrow("pc.addTrack()");
37                 shouldThrow("pc.addTrack(null)");
38                 shouldThrow("pc.addTrack({})");
39                 shouldThrow("pc.addTrack(track, null)");
40                 shouldThrow("pc.addTrack(track, {})");
41                 shouldThrow("pc.addTrack(track, stream, null)");
42                 debug("");
43
44                 debug("Test bad removeTrack() arguments");
45                 shouldThrow("pc.removeTrack()");
46                 shouldThrow("pc.removeTrack(null)");
47                 shouldThrow("pc.removeTrack({})");
48                 debug("");
49
50                 shouldBe("pc.getSenders().length", "0");
51
52                 shouldNotThrow("sender = pc.addTrack(track, stream)");
53                 shouldBeType("sender", "RTCRtpSender");
54                 shouldBe("sender.track", "track");
55                 shouldBe("pc.getSenders().length", "1");
56                 shouldBe("pc.getSenders()[0]", "sender");
57
58                 shouldBe("pc.getReceivers().length", "1");
59                 receiver = pc.getReceivers()[0];
60                 shouldBeType("receiver.track", "MediaStreamTrack");
61                 shouldBeTrue("receiver.track.muted");
62
63                 shouldBe("receiver.track.kind", "sender.track.kind");
64
65                 shouldBe("pc.getTransceivers().length", "1");
66                 transceiver = pc.getTransceivers()[0];
67                 shouldBeNull("transceiver.mid");
68                 shouldBe("transceiver.sender", "sender");
69                 shouldBe("transceiver.receiver", "receiver");
70                 shouldBeFalse("transceiver.stopped");
71
72                 debug("Try to add same track again");
73                 shouldThrow("sender = pc.addTrack(track, stream)");
74
75                 track2 = track.clone();
76                 stream2 = stream.clone();
77
78                 shouldNotThrow("sender2 = pc.addTrack(track2, stream, stream2)");
79                 shouldBe("pc.getSenders().length", "2");
80
81                 // Order in list returned by getSenders() is unspecified.
82                 if (pc.getSenders()[0] == sender) {
83                     shouldBe("pc.getSenders()[0]", "sender");
84                     shouldBe("pc.getSenders()[1]", "sender2");
85                 } else {
86                     shouldBe("pc.getSenders()[0]", "sender2");
87                     shouldBe("pc.getSenders()[1]", "sender1");
88                 }
89
90                 shouldNotThrow("pc.removeTrack(sender)");
91                 debug("Sender is still in getSenders() list")
92                 shouldBe("pc.getSenders().length", "2");
93
94                 shouldThrow("pc.addTrack(null);");
95                 shouldThrow("pc.addTrack(undefined);");
96                 shouldThrow("pc.removeTrack(null);");
97                 shouldThrow("pc.removeTrack(undefined);");
98
99                 finishJSTest();
100             })
101             .catch(function (error) {
102                 testFailed("Error caught in promise chain: " + error);
103                 finishJSTest();
104             });
105
106             window.jsTestIsAsync = true;
107             window.successfullyParsed = true;
108
109         </script>
110         <script src="../../resources/js-test-post.js"></script>
111     </body>
112 </html>