blob: 907d187a941c35ba52635334db23ad2b81a27802 [file] [log] [blame]
(function(window) {
EventExpectationsManager = function(test) {
this.test_ = test;
this.eventTargetList_ = [];
this.waitCallbacks_ = [];
};
EventExpectationsManager.prototype.expectEvent = function(object, eventName, description)
{
var eventInfo = { 'target': object, 'type': eventName, 'description': description};
var expectations = this.getExpectations_(object);
expectations.push(eventInfo);
var t = this;
var waitHandler = this.test_.step_func(function() { t.handleWaitCallback_(); });
var eventHandler = this.test_.step_func(function(event)
{
object.removeEventListener(eventName, eventHandler);
var expected = expectations[0];
assert_equals(event.target, expected.target, "Event target match.");
assert_equals(event.type, expected.type, "Event types match.");
assert_equals(eventInfo.description, expected.description, "Descriptions match for '" + event.type + "'.");
expectations.shift(1);
if (t.waitCallbacks_.length > 0)
setTimeout(waitHandler, 0);
});
object.addEventListener(eventName, eventHandler);
};
EventExpectationsManager.prototype.waitForExpectedEvents = function(callback)
{
this.waitCallbacks_.push(callback);
setTimeout(this.handleWaitCallback_.bind(this), 0);
};
EventExpectationsManager.prototype.expectingEvents = function()
{
for (var i = 0; i < this.eventTargetList_.length; ++i) {
if (this.eventTargetList_[i].expectations.length > 0) {
return true;
}
}
return false;
}
EventExpectationsManager.prototype.handleWaitCallback_ = function()
{
if (this.waitCallbacks_.length == 0 || this.expectingEvents())
return;
var callback = this.waitCallbacks_.shift(1);
callback();
};
EventExpectationsManager.prototype.getExpectations_ = function(target)
{
for (var i = 0; i < this.eventTargetList_.length; ++i) {
var info = this.eventTargetList_[i];
if (info.target == target) {
return info.expectations;
}
}
var expectations = [];
this.eventTargetList_.push({ 'target': target, 'expectations': expectations });
return expectations;
};
function loadData_(test, url, callback, isBinary)
{
var request = new XMLHttpRequest();
request.open("GET", url, true);
if (isBinary) {
request.responseType = 'arraybuffer';
}
request.onload = test.step_func(function(event)
{
if (request.status != 200) {
assert_unreached("Unexpected status code : " + request.status);
return;
}
var response = request.response;
if (isBinary) {
response = new Uint8Array(response);
}
callback(response);
});
request.onerror = test.step_func(function(event)
{
assert_unreached("Unexpected error");
});
request.send();
}
function openMediaSource_(test, mediaTag, callback)
{
var mediaSource = new MediaSource();
var mediaSourceURL = URL.createObjectURL(mediaSource);
var eventHandler = test.step_func(onSourceOpen);
function onSourceOpen(event)
{
mediaSource.removeEventListener('sourceopen', eventHandler);
URL.revokeObjectURL(mediaSourceURL);
callback(mediaSource);
}
mediaSource.addEventListener('sourceopen', eventHandler);
mediaTag.src = mediaSourceURL;
}
var MediaSourceUtil = {};
MediaSourceUtil.loadTextData = function(test, url, callback)
{
loadData_(test, url, callback, false);
};
MediaSourceUtil.loadBinaryData = function(test, url, callback)
{
loadData_(test, url, callback, true);
};
function getFirstSupportedType(typeList)
{
for (var i = 0; i < typeList.length; ++i) {
if (MediaSource.isTypeSupported(typeList[i]))
return typeList[i];
}
return "";
}
var audioOnlyTypes = ['audio/webm;codecs="vorbis"', 'audio/mp4;codecs="mp4a.40.2"'];
var videoOnlyTypes = ['video/webm;codecs="vp8"', 'video/mp4;codecs="avc1.4D4001"'];
var audioVideoTypes = ['video/webm;codecs="vp8,vorbis"', 'video/mp4;codecs="mp4a.40.2"'];
MediaSourceUtil.AUDIO_ONLY_TYPE = getFirstSupportedType(audioOnlyTypes);
MediaSourceUtil.VIDEO_ONLY_TYPE = getFirstSupportedType(videoOnlyTypes);
MediaSourceUtil.AUDIO_VIDEO_TYPE = getFirstSupportedType(audioVideoTypes);
function addExtraTestMethods(test)
{
test.failOnEvent = function(object, eventName)
{
object.addEventListener(eventName, test.step_func(function(event)
{
assert_unreached("Unexpected event '" + eventName + "'");
}));
};
test.endOnEvent = function(object, eventName)
{
object.addEventListener(eventName, test.step_func(function(event) { test.done(); }));
};
test.eventExpectations_ = new EventExpectationsManager(test);
test.expectEvent = function(object, eventName, description)
{
test.eventExpectations_.expectEvent(object, eventName, description);
};
test.waitForExpectedEvents = function(callback)
{
test.eventExpectations_.waitForExpectedEvents(callback);
};
var oldTestDone = test.done.bind(test);
test.done = function()
{
if (test.status == test.PASS)
assert_false(test.eventExpectations_.expectingEvents(), "No pending event expectations.");
oldTestDone();
};
};
window['MediaSourceUtil'] = MediaSourceUtil;
window['mediasource_test'] = function(testFunction, description, options)
{
options = options || {};
return async_test(function(test) {
var mediaTag = document.createElement("video");
document.body.appendChild(mediaTag);
addExtraTestMethods(test);
// Overload done() so that element added to the document can be removed.
test.removeMediaElement_ = true;
var oldTestDone = test.done.bind(test);
test.done = function()
{
if (test.removeMediaElement_) {
document.body.removeChild(mediaTag);
test.removeMediaElement_ = false;
}
oldTestDone();
};
openMediaSource_(test, mediaTag, function(mediaSource)
{
testFunction(test, mediaTag, mediaSource);
});
}, description, options);
};
})(window);