summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_info_leak.html
blob: e3ff057cfb922beb02ca9b7816614ec2d7db5b91 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=478957
-->
<head>
  <title>Test for Bug 478957</title>
  <script type="application/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>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=478957">Mozilla Bug 478957</a>
<p id="display"></p>
<div id="content" style="display: none">
  
</div>

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

<pre id="test">
<script type="application/javascript">

/** Test for Bug 478957 **/

// Tests whether we leak events and state change info when loading stuff from local files from a webserver.

var manager = new MediaTestManager;

var gEventTypes = [ 'loadstart', 'progress', 'suspend', 'abort', 'error', 'emptied', 'stalled', 'play', 'pause', 'loadedmetadata', 'loadeddata', 'waiting', 'playing', 'canplay', 'canplaythrough', 'seeking', 'seeked', 'timeupdate', 'ended', 'ratechange', 'durationchange', 'volumechange' ];

var gExpectedEvents = ['ratechange', 'loadstart', 'error'];

function createTestArray() {
  var tests = [];
  var tmpVid = document.createElement("video");

  for (var testNum=0; testNum<gInfoLeakTests.length; testNum++) {
    var test = gInfoLeakTests[testNum];
    if (!tmpVid.canPlayType(test.type)) {
      continue;
    }

    var t = new Object;
    t.name = test.src;
    t.type = test.type;

    tests.push(t);
  }
  return tests;
}

function log(msg) {
  info(msg);
  var l = document.getElementById('log');
  l.innerHTML += msg + "<br>";
}

function finish(v) {
  log("finish: " + v.name);
  clearInterval(v.checkStateInterval);

  for (var i=0; i<gEventTypes.length; i++) {
    v.removeEventListener(gEventTypes[i], listener, false);
  }
  removeNodeAndSource(v);

  manager.finished(v.token);
  v = null;
}

function listener(evt) {
  var v = evt.target;
  log(filename(v.name) + ': got ' + evt.type);

  // On slow machines like B2G emulator, progress timer could time out before
  // receiving any HTTP notification. We will ignore the 'stalled' event to
  // pass the tests.
  if (evt.type == 'stalled') {
    return;
  }

  ok(v.eventNum < gExpectedEvents.length, filename(v.name)  + " Too many events received");
  var expected = (v.eventNum < gExpectedEvents.length) ? gExpectedEvents[v.eventNum] : "NoEvent";
  is(evt.type, expected, filename(v.name) + " Events received in wrong order");
  v.eventNum++;
  if (v.eventNum == gExpectedEvents.length) {
    // In one second, move onto the next test. This give a chance for any
    // other events to come in. Note: we don't expect any events to come
    // in, unless we've leaked some info, and 1 second should be enough time
    // for the leak to show up.
    setTimeout(function() {finish(v);}, 1000); 
  }
}

function createMedia(type, src, token) {
  var tag = getMajorMimeType(type);
  var v = document.createElement(tag);
  for (var i=0; i<gEventTypes.length; i++) {
    v.addEventListener(gEventTypes[i], listener, false);
  }
  v.preload = "metadata";
  v.src = src;
  v.name = src;
  document.body.appendChild(v);
  v.eventNum = 0;
  v.token = token;
  setTimeout(
  function() {
    v.checkStateInterval = setInterval(function(){checkState(v);},1);
  }, 0);
}

// Define our own ok() and is() functions. The mochitest ones take ages constructing the log
// of all the passes, so only report failures.
function test_ok(b, msg) {
  if (!b) {
    log("FAILED test_ok: " + msg);
    ok(b, msg);
  }
}

function test_is(a, b, msg) {
  if (a != b) {
    log("FAILED test_is: " + msg);
    is(a,b,msg);
  }
}

function filename(uri) {
  return uri.substr(uri.lastIndexOf("/")+1);
}

function checkState(v) {
  test_ok(v.networkState <= HTMLMediaElement.NETWORK_LOADING || 
          v.networkState == HTMLMediaElement.NETWORK_NO_SOURCE,
          "NetworkState of " + v.networkState + " was leaked.");
  test_ok(v.readyState == HTMLMediaElement.HAVE_NOTHING,
          "Ready state of " + v.readyState + " was leaked");
  test_is(v.seeking, false, "Seeking leaked");
  test_is(v.currentTime, 0, "Leaked currentTime");
  test_ok(isNaN(v.duration), "Leaked duration");
  test_is(v.paused, true, "Paused leaked");
  test_is(v.ended, false, "Ended leaked");
  test_is(v.autoplay, false, "Autoplay leaked");
  test_is(v.controls, false, "Controls leaked");
  test_is(v.muted, false, "muted leaked");
  test_ok(v.error==null || v.error.code==MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED,
          "Error code should not exist or be SRC_NOT_SUPPORTED. v.error=" +
          (v.error ? v.error.code : "null"));
  test_ok(filename(v.currentSrc) == filename(v.name) ||
          v.networkState == HTMLMediaElement.NETWORK_NO_SOURCE,
          "currentSrc should match candidate uri, if we've got a valid source");
}


function startTest(test, token) {
  manager.started(token);
  log("Testing: " + test.type + " @ " + test.name);
  createMedia(test.type, test.name, token);  
}

manager.runTests(createTestArray(), startTest);

</script>
</pre>

</body>
</html>