summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_load.html
blob: a502a6b23a1090f727f61a104ad660fd2773ed95 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=479859
-->
<head>
  <title>Test for Bug 479859</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="manifest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=479859">Mozilla Bug 479859</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>
<pre id="test">
<script type="text/javascript">

function log() {
  //document.getElementById('log').innerHTML += "<p>" + msg + "</p>";
}

// We don't track: progress, canplay, canplaythrough and stalled events,
// as these can be delivered out of order, and/or multiple times.
var gEventTypes = [ 'loadstart', 'abort', 'error', 'emptied', 'play',
  'pause', 'loadedmetadata', 'loadeddata', 'waiting', 'playing', 'seeking',
  'seeked', 'timeupdate', 'ended', 'ratechange', 'durationchange', 'volumechange' ];

var gEventNum = 0;
var gTestNum = 0;
var gTestFileNum = 0;
var gExpectedEvents = null;
var gTest = null;
var gTestName = "?";

function listener(evt) {
  log('event ' + evt.type);
  ok(gEventNum < gExpectedEvents.length, gTestName+" - corrent number of events received");
  var expected = (gEventNum < gExpectedEvents.length) ? gExpectedEvents[gEventNum] : "NoEvent";
  is(evt.type, expected, gTestName+" - events received in order");
  gEventNum++;
  if (gEventNum == gExpectedEvents.length) {
    setTimeout(nextTest, 0); 
  }
}

function source_error(evt) {
  log('event source_error');
  ok(evt.type == "error", "Should only get error events here");
  ok(gEventNum < gExpectedEvents.length, gTestName+" - corrent number of events received");
  var expected = (gEventNum < gExpectedEvents.length) ? gExpectedEvents[gEventNum] : "NoEvent";
  is("source_error", expected, gTestName+" - events received in order");
  gEventNum++;
  if (gEventNum == gExpectedEvents.length) {
    setTimeout(nextTest, 0); 
  }
}

var gMedia = null;

function createMedia(tag) {
  gMedia = document.createElement(tag);
  gMedia.preload = "metadata";
  for (var i=0; i<gEventTypes.length; i++) {
    gMedia.addEventListener(gEventTypes[i], listener);
  }
}

function addSource(src, type) {
  var s = document.createElement("source");
  s.addEventListener("error", source_error);
  s.src = src;
  s.type = type;
  gMedia.appendChild(s);
  return s;
}

function prependSource(src, type) {
  var s = document.createElement("source");
  s.addEventListener("error", source_error);
  s.src = src;
  s.type = type;
  gMedia.insertBefore(s, gMedia.firstChild);
  return s;
}

var gTests = [
  {
    // Test 0: adding video to doc, then setting src should load implicitly.
    create(src) {
        document.body.appendChild(gMedia);
        gMedia.src = src;
      },
    expectedEvents: ['loadstart', 'durationchange', 'loadedmetadata', 'loadeddata']
  }, {
    // Test 1: adding video to doc, then adding source.
    create(src, type) {
        document.body.appendChild(gMedia);
        addSource(src, type);
      },
    expectedEvents: ['loadstart', 'durationchange', 'loadedmetadata', 'loadeddata']
  },{
    // Test 2: video with multiple source, the first of which are bad, we should load the last,
    // and receive error events for failed loads on the source children.
    create(src, type) {
        document.body.appendChild(gMedia);
        addSource("404a", type);
        addSource("404b", type);
        addSource(src, type);
      },
    expectedEvents: ['loadstart', 'source_error', 'source_error', 'durationchange', 'loadedmetadata', 'loadeddata']
  }, {
    // Test 3:  video with bad src, good <source>, ensure that <source> aren't used.
    create(src, type) {
        gMedia.src = "404a";
        addSource(src, type);
        document.body.appendChild(gMedia);
      },
    expectedEvents: ['loadstart', 'error']
  }, {
    // Test 4:  video with only bad source, loading, then adding a good source
    // -  should resume load.
    create(src, type) {
        addSource("404a", type);
        var s2 = addSource("404b", type);
        s2.addEventListener("error",
          function() {
            // Should awaken waiting load, causing successful load.
            addSource(src, type);
          });
        document.body.appendChild(gMedia);
      },
    expectedEvents: ['loadstart', 'source_error', 'source_error', 'durationchange', 'loadedmetadata', 'loadeddata']
  }, {
    // Test 5: video with only 1 bad source, let it fail to load, then prepend
    // a good <source> to the video, it shouldn't be selected, because the
    // "pointer" should be after the last child - the bad source.
    prepended: false,
    create(src, type) {
        var prepended = false;
        addSource("404a", type);
        var s2 = addSource("404b", type);
        s2.addEventListener("error",
          function() {
            // Should awaken waiting load, causing successful load.
            if (!prepended) {
              prependSource(src, type);
              prepended = true;
            }
          });
        document.body.appendChild(gMedia);
      },
    expectedEvents: ['loadstart', 'source_error', 'source_error']
  }, {
    // Test 6: (Bug 1165203) preload="none" then followed by an explicit
    // call to load() should load metadata
    create(src, type) {
        gMedia.preload = "none";
        gMedia.src = src;
        document.body.appendChild(gMedia);
        addSource(src, type);
        gMedia.load();
      },
    expectedEvents: ['emptied',  'loadstart', 'durationchange', 'loadedmetadata', 'loadeddata']
  }
];

function nextTest() {
  if (gMedia) {
    for (var i=0; i<gEventTypes.length; i++) {
      gMedia.removeEventListener(gEventTypes[i], listener);
    }
    removeNodeAndSource(gMedia);
    gMedia = null;
  }
  gEventNum = 0;

  if (gTestNum == gTests.length) {
    gTestNum = 0;
    ++gTestFileNum;
    if (gTestFileNum == gSmallTests.length) {
      SimpleTest.finish();
      return;
    }
  }

  var src = gSmallTests[gTestFileNum].name;
  var type = gSmallTests[gTestFileNum].type;

  var t = gTests[gTestNum];
  gTestNum++;
  
  createMedia(type.match(/^audio\//) ? "audio" : "video");
  if (!gMedia.canPlayType(type)) {
    // Unsupported type, skip to next test
    nextTest();
    return;
  }

  gTestName = "Test " + src + " " + (gTestNum - 1);
  log("Starting " + gTestName);
  gExpectedEvents = t.expectedEvents;

  t.create(src, type);
}

addLoadEvent(nextTest);
SimpleTest.waitForExplicitFinish();

</script>
</pre>

<div id="log" style="font-size: small"></div>
</body>
</html>