Files
shaka-player/test/cast/cast_utils_unit.js
T
Jacob Trimble 3253221e99 Add test to verify CastProxy includes every Player member.
Issue #675

Change-Id: I0fe18ea28571ad2dd6c92587e5df345c80b223c6
2017-02-07 12:39:35 -08:00

264 lines
8.6 KiB
JavaScript

/**
* @license
* Copyright 2016 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.
*/
describe('CastUtils', function() {
var CastUtils;
var FakeEvent;
beforeAll(function() {
CastUtils = shaka.cast.CastUtils;
FakeEvent = shaka.util.FakeEvent;
});
it('includes every Player member', function() {
var ignoredMembers = [
'constructor', // JavaScript added field
'getNetworkingEngine', // Handled specially
'setMaxHardwareResolution',
'destroy', // Should use CastProxy.destroy instead
// Test helper methods (not @export'd)
'createDrmEngine',
'createNetworkingEngine',
'createPlayhead',
'createMediaSource',
'createMediaSourceEngine',
'createStreamingEngine'
];
var castMembers = shaka.cast.CastUtils.PlayerVoidMethods
.concat(shaka.cast.CastUtils.PlayerGetterMethods)
.concat(shaka.cast.CastUtils.PlayerPromiseMethods);
var playerMembers = Object.keys(shaka.Player.prototype).filter(
function(name) {
// Private members end with _.
return ignoredMembers.indexOf(name) < 0 &&
name.substr(name.length - 1) != '_';
});
// To make debugging easier, don't check that they are equal; instead check
// that neither has any extra entries.
var extraCastMembers = castMembers.filter(function(name) {
return playerMembers.indexOf(name) < 0;
});
var extraPlayerMembers = castMembers.filter(function(name) {
return castMembers.indexOf(name) < 0;
});
expect(extraCastMembers).toEqual([]);
expect(extraPlayerMembers).toEqual([]);
});
describe('serialize/deserialize', function() {
it('transfers infinite values and NaN', function() {
var orig = {
'nan': NaN,
'positive_infinity': Infinity,
'negative_infinity': -Infinity,
'null': null,
'true': true,
'false': false,
'one': 1,
'string': 'a string'
};
var serialized = CastUtils.serialize(orig);
// The object is turned into a string.
expect(typeof serialized).toBe('string');
// The deserialized object matches the original.
var deserialized = CastUtils.deserialize(serialized);
for (var k in orig) {
expect(deserialized[k]).toEqual(orig[k]);
}
});
it('transfers real Events', function() {
// new Event() is not usable on IE11:
var event = document.createEvent('CustomEvent');
event.initCustomEvent('myEventType', false, false, null);
// Properties that can definitely be transferred.
var nativeProperties = [
'bubbles',
'type',
'cancelable',
'defaultPrevented'
];
var extraProperties = {
'key': 'value',
'true': true,
'one': 1
};
for (var k in extraProperties) {
event[k] = extraProperties[k];
}
// The event is turned into a string.
var serialized = CastUtils.serialize(event);
expect(typeof serialized).toBe('string');
// The string is turned back into an object.
var deserialized = CastUtils.deserialize(serialized);
expect(typeof deserialized).toBe('object');
// The object can be used to construct a FakeEvent.
var fakeEvent = new FakeEvent(deserialized['type'], deserialized);
// The fake event has the same type and properties as the original.
nativeProperties.forEach(function(k) {
expect(fakeEvent[k]).toEqual(event[k]);
});
for (var k in extraProperties) {
expect(fakeEvent[k]).toEqual(event[k]);
}
});
it('transfers dispatched FakeEvents', function(done) {
var event = new FakeEvent('custom');
// Properties that can definitely be transferred.
var nativeProperties = [
'bubbles',
'type',
'cancelable',
'defaultPrevented'
];
var extraProperties = {
'key': 'value',
'true': true,
'one': 1
};
for (var k in extraProperties) {
event[k] = extraProperties[k];
}
var target = new shaka.util.FakeEventTarget();
target.addEventListener(event.type, function() {
try {
// The event is turned into a string.
var serialized = CastUtils.serialize(event);
expect(typeof serialized).toBe('string');
// The string is turned back into an object.
var deserialized = CastUtils.deserialize(serialized);
expect(typeof deserialized).toBe('object');
// The deserialized event has the same type and properties as the
// original.
nativeProperties.forEach(function(k) {
expect(deserialized[k]).toEqual(event[k]);
});
for (var k in extraProperties) {
expect(deserialized[k]).toEqual(event[k]);
}
} catch (exception) {
fail(exception);
}
done();
});
target.dispatchEvent(event);
});
describe('TimeRanges', function() {
var video;
var eventManager;
var mediaSourceEngine;
beforeAll(function() {
video = /** @type {HTMLMediaElement} */(
document.createElement('video'));
document.body.appendChild(video);
});
beforeEach(function(done) {
// The TimeRanges constructor cannot be used directly, so we load a clip
// to get ranges to use.
var mediaSource = new MediaSource();
var mimeType = 'video/mp4; codecs="avc1.42c01e"';
var initSegmentUrl = '/base/test/test/assets/sintel-video-init.mp4';
var videoSegmentUrl = '/base/test/test/assets/sintel-video-segment.mp4';
// Wait for the media source to be open.
eventManager = new shaka.util.EventManager();
video.src = window.URL.createObjectURL(mediaSource);
eventManager.listen(video, 'error', onError);
eventManager.listen(mediaSource, 'sourceopen', onSourceOpen);
function onError() {
fail('Error code ' + (video.error ? video.error.code : 0));
}
function onSourceOpen() {
eventManager.unlisten(mediaSource, 'sourceopen');
mediaSourceEngine = new shaka.media.MediaSourceEngine(
video, mediaSource, /* TextTrack */ null);
mediaSourceEngine.init({'video': mimeType}, false);
shaka.test.Util.fetch(initSegmentUrl).then(function(data) {
return mediaSourceEngine.appendBuffer('video', data, null, null);
}).then(function() {
return shaka.test.Util.fetch(videoSegmentUrl);
}).then(function(data) {
return mediaSourceEngine.appendBuffer('video', data, null, null);
}).catch(fail).then(done);
}
});
afterEach(function(done) {
eventManager.destroy().then(function() {
if (mediaSourceEngine) {
return mediaSourceEngine.destroy();
}
}).then(function() {
video.removeAttribute('src');
video.load();
done();
});
});
afterAll(function() {
document.body.removeChild(video);
});
it('deserialize into equivalent objects', function() {
var buffered = video.buffered;
// The test is less interesting if the ranges are empty.
expect(buffered.length).toBeGreaterThan(0);
// The TimeRanges object is turned into a string.
var serialized = CastUtils.serialize(buffered);
expect(typeof serialized).toBe('string');
// Expect the deserialized version to look like the original.
var deserialized = CastUtils.deserialize(serialized);
expect(deserialized.length).toEqual(buffered.length);
expect(deserialized.start).toEqual(jasmine.any(Function));
expect(deserialized.end).toEqual(jasmine.any(Function));
for (var i = 0; i < deserialized.length; ++i) {
// Not exact because of the possibility of rounding errors.
expect(deserialized.start(i)).toBeCloseTo(buffered.start(i));
expect(deserialized.end(i)).toBeCloseTo(buffered.end(i));
}
});
});
});
});