Update web-platform-tests.

This commit is contained in:
Ms2ger 2015-04-09 13:39:50 +02:00
parent 74afd086d2
commit 71008d816d
62 changed files with 793 additions and 150 deletions

View file

@ -470,6 +470,74 @@ policies and contribution forms [3].
});
}
/**
* This constructor helper allows DOM events to be handled using Promises,
* which can make it a lot easier to test a very specific series of events,
* including ensuring that unexpected events are not fired at any point.
*/
function EventWatcher(test, watchedNode, eventTypes)
{
if (typeof eventTypes == 'string') {
eventTypes = [eventTypes];
}
var waitingFor = null;
var eventHandler = test.step_func(function(evt) {
assert_true(!!waitingFor,
'Not expecting event, but got ' + evt.type + ' event');
assert_equals(evt.type, waitingFor.types[0],
'Expected ' + waitingFor.types[0] + ' event, but got ' +
evt.type + ' event instead');
if (waitingFor.types.length > 1) {
// Pop first event from array
waitingFor.types.shift();
return;
}
// We need to null out waitingFor before calling the resolve function
// since the Promise's resolve handlers may call wait_for() which will
// need to set waitingFor.
var resolveFunc = waitingFor.resolve;
waitingFor = null;
resolveFunc(evt);
});
for (var i = 0; i < eventTypes.length; i++) {
watchedNode.addEventListener(eventTypes[i], eventHandler);
}
/**
* Returns a Promise that will resolve after the specified event or
* series of events has occured.
*/
this.wait_for = function(types) {
if (waitingFor) {
return Promise.reject('Already waiting for an event or events');
}
if (typeof types == 'string') {
types = [types];
}
return new Promise(function(resolve, reject) {
waitingFor = {
types: types,
resolve: resolve,
reject: reject
};
});
};
function stop_watching() {
for (var i = 0; i < eventTypes.length; i++) {
watchedNode.removeEventListener(eventTypes[i], eventHandler);
}
};
test.add_cleanup(stop_watching);
return this;
}
expose(EventWatcher, 'EventWatcher');
function setup(func_or_properties, maybe_properties)
{
var func = null;