summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_mediatrack_events.html
blob: 2f96865986904dfe01646993e3579a7d6e9579b3 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE HTML>
<html>
<head>
  <title>Test events of media track interfaces</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
function startTest() {
  navigator.mozGetUserMedia({audio:true, video:true, fake:true},
    function(stream) {
      var element = document.createElement("video");

      isnot(element.audioTracks, undefined,
            'HTMLMediaElement::AudioTracks() property should be available.');
      isnot(element.videoTracks, undefined,
            'HTMLMediaElement::VideoTracks() property should be available.');

      function verifyEvent(e, type) {
        is(e.type, type, "Event type should be " + type);
        ok(e.isTrusted, "Event should be trusted.");
        ok(!e.bubbles, "Event shouldn't bubble.");
        ok(!e.cancelable, "Event shouldn't be cancelable.");
      }

      element.audioTracks.onaddtrack = function(e) {
        ok(e instanceof TrackEvent, "Event fired from onaddtrack should be a TrackEvent");
        ok(true, 'onaddtrack is expected to be called from audioTracks.');
        verifyEvent(e, "addtrack");
      }

      element.audioTracks.onremovetrack = function(e) {
        ok(e instanceof TrackEvent, "Event fired from onremovetrack should be a TrackEvent");
        ok(true, 'onremovetrack is expected to be called from audioTracks.');
        verifyEvent(e, "removetrack");
      }

      element.audioTracks.onchange = function(e) {
        ok(e instanceof window.Event, "Event fired from onchange should be a simple event.");
        ok(true, 'onchange is expected to be called from audioTracks.');
        verifyEvent(e, "change");
      }

      element.videoTracks.onaddtrack = function(e) {
        ok(e instanceof TrackEvent, "Event fired from onaddtrack should be a TrackEvent");
        ok(true, 'onaddtrack is expected to be called from videoTracks.');
        verifyEvent(e, "addtrack");
      }

      element.videoTracks.onremovetrack = function(e) {
        ok(e instanceof TrackEvent, "Event fired from onremovetrack should be a TrackEvent");
        ok(true, 'onremovetrack is expected to be called from videoTracks.');
        verifyEvent(e, "removetrack");
      }

      element.videoTracks.onchange = function(e) {
        ok(e instanceof window.Event, "Event fired from onchange should be a simple event.");
        ok(true, 'onchange is expected to be called from videoTracks.');
        verifyEvent(e, "change");
      }

     function onended() {
        ok(true, 'Event ended is expected to be fired on element.');
        element.onended = null;
        element.onplaying = null;
        element.onpause = null;
        //This helps to prevent these events from firing after SimpleTest.finish()
        //on B2G ICS Emulator, but not sure they have been run at all, then
        element.audioTracks.onremovetrack = null;
        element.audioTracks.onaddtrack = null;
        element.audioTracks.onchange = null;
        element.videoTracks.onremovetrack = null;
        element.videoTracks.onaddtrack = null;
        element.videoTracks.onchange = null;
        SimpleTest.finish();
      }

      function onpause() {
        element.onpause = null;
        if (element.ended) {
          return;
        }
        if (steps == 1) {
          element.audioTracks[0].enabled = false;
          element.videoTracks[0].selected = false;
          element.onplaying = onplaying;
          element.play();
          steps++;
        }
      }

      function onplaying() {
        element.onplaying = null;
        if (element.ended) {
          return;
        }
        if (steps == 1) {
          element.onpause = onpause;
          element.pause();
        } else if (steps == 2) {
          stream.stop();
        }
      }

      var steps = 0;
      element.mozSrcObject = stream;
      element.onplaying = onplaying;
      element.onended = onended;
      element.play();
      steps++;
    },
    function(err) {
      ok(false, 'Unexpected error fired with: ' + err);
      SimpleTest.finish();
    }
  );
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv(
  {
    "set": [
      ["media.track.enabled", true]
    ]
  }, startTest);

</script>
</pre>
</body>
</html>