summaryrefslogtreecommitdiffstats
path: root/dom/media/tests/mochitest/test_getUserMedia_addtrack_removetrack_events.html
blob: 833653ebb2df9bc694628c9206c533cd41d945a0 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="mediaStreamPlayback.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
"use strict";

createHTML({
  title: "MediaStream's 'addtrack' and 'removetrack' events shouldn't fire on manual operations",
  bug: "1208328"
});

var spinEventLoop = () => new Promise(r => setTimeout(r, 0));

var stream;
var clone;
var newStream;
var tracks = [];

var addTrack = track => {
  info("Adding track " + track.id);
  stream.addTrack(track);
};
var removeTrack = track => {
  info("Removing track " + track.id);
  stream.removeTrack(track);
};
var stopTrack = track => {
  if (track.readyState == "live") {
    info("Stopping track " + track.id);
  }
  track.stop();
};

runTest(() => getUserMedia({audio: true, video: true})
  .then(s => {
    stream = s;
    clone = s.clone();
    stream.addEventListener("addtrack", function onAddtrack(event) {
      ok(false, "addtrack fired unexpectedly for track " + event.track.id);
    });
    stream.addEventListener("removetrack", function onRemovetrack(event) {
      ok(false, "removetrack fired unexpectedly for track " + event.track.id);
    });

    return getUserMedia({audio: true, video: true});
  })
  .then(s => {
    newStream = s;
    info("Stopping an original track");
    stopTrack(stream.getTracks()[0]);

    return spinEventLoop();
  })
  .then(() => {
    info("Removing original tracks");
    stream.getTracks().forEach(t => (stream.removeTrack(t), tracks.push(t)));

    return spinEventLoop();
  })
  .then(() => {
    info("Adding other gUM tracks");
    newStream.getTracks().forEach(t => addTrack(t))

    return spinEventLoop();
  })
  .then(() => {
    info("Adding cloned tracks");
    let clone = stream.clone();
    clone.getTracks().forEach(t => addTrack(t));

    return spinEventLoop();
  })
  .then(() => {
    info("Removing a clone");
    removeTrack(clone.getTracks()[0]);

    return spinEventLoop();
  })
  .then(() => {
    info("Stopping clones");
    clone.getTracks().forEach(t => stopTrack(t));

    return spinEventLoop();
  })
  .then(() => {
    info("Stopping originals");
    stream.getTracks().forEach(t => stopTrack(t));
    tracks.forEach(t => stopTrack(t));

    return spinEventLoop();
  })
  .then(() => {
    info("Removing remaining tracks");
    stream.getTracks().forEach(t => removeTrack(t));

    return spinEventLoop();
  })
  .then(() => {
      // Test MediaStreamTrackEvent required args here.
      mustThrowWith("MediaStreamTrackEvent without required args",
                    "TypeError", () => new MediaStreamTrackEvent("addtrack", {}));
  }));
</script>
</pre>
</body>
</html>