summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_mediatrack_replay_from_end.html
blob: 16b0cbeb97fb083bbc7991aaeea96a87c199d807 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test media tracks if replay after playback has ended</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) {
  // Scenario to test:
  // 1. Audio tracks and video tracks should be added to the track list when
  //    metadata has loaded, and all tracks should remain even after we seek to
  //    the end.
  // 2. No tracks should be added back to the list if we replay from the end,
  //    and no tracks should be removed from the list after we seek to the end.
  // 3. After seek to the middle from end of playback, all tracks should remain
  //    in the list if we play from here, and no tracks should be removed from
  //    the list after we seek to the end.
  // 4. Unsetting the media element's src attribute should remove all tracks.

  const elemType = getMajorMimeType(test.type);
  const element = document.createElement(elemType);

  let audioOnaddtrack = 0;
  let audioOnremovetrack = 0;
  let videoOnaddtrack = 0;
  let videoOnremovetrack = 0;
  let isPlaying = false;
  let steps = 0;

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

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

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

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

  function testExpectedAddtrack(expectedCalls) {
    if (test.hasAudio) {
      is(audioOnaddtrack, expectedCalls,
         'Calls of onaddtrack on audioTracks should be '+expectedCalls+' times.');
    }
    if (test.hasVideo) {
      is(videoOnaddtrack, expectedCalls,
         'Calls of onaddtrack on videoTracks should be '+expectedCalls+' times.');
    }
  }

  function testExpectedRemovetrack(expectedCalls) {
    if (test.hasAudio) {
      is(audioOnremovetrack, expectedCalls,
         'Calls of onremovetrack on audioTracks should be '+expectedCalls+' times.');
    }
    if (test.hasVideo) {
      is(videoOnremovetrack, expectedCalls,
         'Calls of onremovetrack on videoTracks should be '+expectedCalls+' times.');
    }
  }

  function finishTesting() {
    element.onpause = null;
    element.onseeked = null;
    element.onplaying = null;
    element.onended = null;
    manager.finished(element.token);
  }

  function onended() {
    if (isPlaying) {
      switch(steps) {
        case 1:
          testExpectedAddtrack(1);
          testExpectedRemovetrack(0);
          element.onplaying = onplaying;
          element.play();
          steps++;
          break;
        case 2:
          testExpectedAddtrack(1);
          testExpectedRemovetrack(0);
          element.currentTime = element.duration * 0.5;
          element.onplaying = onplaying;
          element.play();
          steps++;
          break;
        case 3:
          testExpectedAddtrack(1);
          testExpectedRemovetrack(0);
          element.src = "";
          setTimeout(() => {
            testExpectedAddtrack(1);
            testExpectedRemovetrack(1);
            finishTesting();
          }, 0);
          break;
      }
    } else {
      ok(true, 'Finish the test anyway if ended is fired before other events.');
      finishTesting();
    }
  }

  function seekToEnd() {
    element.onpause = null;
    element.currentTime = element.duration * 1.1;
  }

  function onseeked() {
    element.onseeked = null;
    element.onpause = seekToEnd;
    element.pause();
  }

  function onplaying() {
    isPlaying = true;
    element.onplaying = null;
    element.onseeked = onseeked;
  }

  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>