summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_mediatrack_consuming_mediaresource.html
blob: 515df5c053780b4845612a70f30904ed32cbd751 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE HTML>
<html>
<head>
  <title>Test track interfaces when consuming media resources</title>
  <script 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">

const manager = new MediaTestManager;

function startTest(test, token) {
  const elemType = getMajorMimeType(test.type);
  const element = document.createElement(elemType);

  let audioOnchange = 0;
  let audioOnaddtrack = 0;
  let audioOnremovetrack = 0;
  let videoOnchange = 0;
  let videoOnaddtrack = 0;
  let videoOnremovetrack = 0;
  let isPlaying = false;

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

  element.audioTracks.onaddtrack = function(e) {
    audioOnaddtrack++;
  }

  element.audioTracks.onremovetrack = function(e) {
    audioOnremovetrack++;
  }

  element.audioTracks.onchange = function(e) {
    audioOnchange++;
  }

  element.videoTracks.onaddtrack = function(e) {
    videoOnaddtrack++;
  }

  element.videoTracks.onremovetrack = function(e) {
    videoOnremovetrack++;
  }

  element.videoTracks.onchange = function(e) {
    videoOnchange++;
  }

  function checkTrackNotRemoved() {
    is(audioOnremovetrack, 0, 'Should have no calls of onremovetrack on audioTracks.');
    is(videoOnremovetrack, 0, 'Should have no calls of onremovetrack on videoTracks.');
    if (isPlaying) {
      is(element.audioTracks.length, test.hasAudio ? 1 : 0,
        'Expected length of audioTracks.');
      is(element.videoTracks.length, test.hasVideo ? 1 : 0,
        'Expected length of videoTracks.');
    }
  }

  function checkTrackRemoved() {
    is(element.audioTracks.length, 0, 'The length of audioTracks should be 0.');
    is(element.videoTracks.length, 0, 'The length of videoTracks should be 0.');
    if (isPlaying) {
      is(audioOnremovetrack, test.hasAudio ? 1 : 0,
        'Expected calls of onremovetrack on audioTracks.');
      is(videoOnremovetrack, test.hasVideo ? 1 : 0,
        'Expected calls of onremovetrack on videoTracks.');
    }
  }

  function onended() {
    ok(true, 'Event ended is expected to be fired on element.');
    checkTrackNotRemoved();
    element.onended = null;
    element.onplaying = null;
    element.onpause = null;
    element.src = "";
    is(element.audioTracks.length, 0, 'audioTracks have been forgotten');
    is(element.videoTracks.length, 0, 'videoTracks have been forgotten');
    is(audioOnremovetrack, 0, 'No audio removetrack events yet');
    is(videoOnremovetrack, 0, 'No video removetrack events yet');
    setTimeout(() => {
      checkTrackRemoved();
      manager.finished(element.token);
    }, 100);
  }

  function checkTrackAdded() {
    isPlaying = true;
    if (test.hasAudio) {
      is(audioOnaddtrack, 1, 'Calls of onaddtrack on audioTracks should be 1.');
      is(element.audioTracks.length, 1, 'The length of audioTracks should be 1.');
      ok(element.audioTracks[0].enabled, 'Audio track should be enabled as default.');
    }
    if (test.hasVideo) {
      is(videoOnaddtrack, 1, 'Calls of onaddtrack on videoTracks should be 1.');
      is(element.videoTracks.length, 1, 'The length of videoTracks should be 1.');
      is(element.videoTracks.selectedIndex, 0,
         'The first video track is set selected as default.');
    }
  }

  function setTrackEnabled(enabled) {
    if (test.hasAudio) {
      element.audioTracks[0].enabled = enabled;
    }
    if (test.hasVideo) {
      element.videoTracks[0].selected = enabled;
    }
  }

  function checkTrackChanged(calls, enabled) {
    if (test.hasAudio) {
      is(audioOnchange, calls, 'Calls of onchange on audioTracks should be '+calls);
      is(element.audioTracks[0].enabled, enabled,
         'Enabled value of the audio track should be ' +enabled);
    }
    if (test.hasVideo) {
      is(videoOnchange, calls, 'Calls of onchange on videoTracks should be '+calls);
      is(element.videoTracks[0].selected, enabled,
         'Selected value of the video track should be ' +enabled);
      var index = enabled ? 0 : -1;
      is(element.videoTracks.selectedIndex, index,
         'SelectedIndex of video tracks should be ' +index);
    }
  }

  function onpause() {
    element.onpause = null;
    if (element.ended) {
      return;
    }
    if (steps == 1) {
      setTrackEnabled(false);
      element.onplaying = onplaying;
      element.play();
      steps++;
    } else if (steps == 2) {
      setTrackEnabled(true);
      element.onplaying = onplaying;
      element.play();
      steps++;
    }
  }

  function onplaying() {
    element.onplaying = null;
    if (element.ended) {
      return;
    }
    if (steps == 1) {
      element.onpause = onpause;
      element.pause();
      checkTrackAdded();
    } else if (steps == 2) {
      element.onpause = onpause;
      element.pause();
      checkTrackChanged(1, false);
    } else if (steps == 3) {
      checkTrackChanged(2, true);
    }
  }

  var steps = 0;

  element.token = token;
  manager.started(token);

  element.src = test.name;
  element.test = test;
  element.onplaying = onplaying;
  element.onended = onended;
  element.play();
  steps++;
}

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

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