Files
shaka-player/spec/util.js
T
Joey Parrish cf53eda1f5 MediaSourceEngine integration tests
Change-Id: I9dea53285a18def6b6284ec9262e46b2d2b48b73
2015-12-01 21:52:51 +00:00

107 lines
2.9 KiB
JavaScript

/**
* @license
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.require('shaka.asserts');
goog.require('shaka.polyfill.CustomEvent');
goog.require('shaka.polyfill.Promise');
/**
* Capture a Promise's status and attach it to the Promise.
* @param {!Promise} promise
*/
function capturePromiseStatus(promise) {
promise.status = 'pending';
promise.then(function() {
promise.status = 'resolved';
}, function() {
promise.status = 'rejected';
});
}
/**
* Returns a Promise which is resolved after the given delay.
*
* @param {number} seconds The delay in seconds.
* @return {!Promise}
*/
function delay(seconds) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, seconds * 1000.0);
});
}
/**
* Replace shaka.asserts and console.assert with a version which hooks into
* jasmine. This converts all failed assertions into failed tests.
*/
var assertsToFailures = {
uninstall: function() {
shaka.asserts = assertsToFailures.originalShakaAsserts_;
console.assert = assertsToFailures.originalConsoleAssert_;
},
install: function() {
assertsToFailures.originalShakaAsserts_ = shaka.asserts;
assertsToFailures.originalConsoleAssert_ = console.assert;
var realAssert = console.assert.bind(console);
var jasmineAssert = function(condition, opt_message) {
realAssert(condition, opt_message);
if (!condition) {
var message = opt_message || 'Assertion failed.';
console.error(message);
try {
throw new Error(message);
} catch (exception) {
fail(message);
}
}
};
shaka.asserts = {
assert: function(condition, opt_message) {
jasmineAssert(condition, opt_message);
},
notImplemented: function() {
jasmineAssert(false, 'Not implemented.');
},
unreachable: function() {
jasmineAssert(false, 'Unreachable reached.');
}
};
console.assert = jasmineAssert;
}
};
// Make sure assertions are converted into failures for all tests.
beforeAll(assertsToFailures.install);
afterAll(assertsToFailures.uninstall);
// The library cannot function without certain browser features, and therefore
// neither can many of our tests. If needed, install the Promise and
// CustomEvent polyfills. In particular, this is needed on IE11.
beforeAll(function() {
shaka.polyfill.Promise.install();
shaka.polyfill.CustomEvent.install();
});