Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Kos authored and Chris Sinchok committed Oct 31, 2014
1 parent 3c1364d commit 0432666
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 167 deletions.
131 changes: 17 additions & 114 deletions spec/VastPluginSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,19 @@ describe('videojs.vast plugin', function() {
});

it("should bail out if player.ads isn't defined", function() {
spyOn(console, "log");
spyOn(this.p.vast, "createSourceObjects");
spyOn(console, "error");
this.p.ads = undefined;
this.p.vast({url:"i wanna go VAST!"});
expect(console.log).toHaveBeenCalledWith(jasmine.any(String));
expect(this.p.vast.createSourceObjects).not.toHaveBeenCalled();
var result = this.p.vast({url:"i wanna go VAST!"});
expect(result).toBe(null);
expect(console.error).toHaveBeenCalledWith(jasmine.any(String));
});

it("should bail out if no url is provided", function() {
spyOn(this.p.vast, "createSourceObjects");
spyOn(this.p, "trigger");
this.p.ads();
this.p.vast({});
var result = this.p.vast({});
expect(result).toBe(null);
expect(this.p.trigger).toHaveBeenCalledWith("adtimeout");
expect(this.p.vast.createSourceObjects).not.toHaveBeenCalled();
});

it("should request an ad if a source is already loaded", function() {
Expand All @@ -96,6 +94,7 @@ describe('videojs.vast plugin', function() {
describe("createSourceObjects", function() {

it("should return objects with 'src', 'type', 'width', and 'height' properties", function() {
player.vast({ url: 'balhbahblhab' });
var media_files = [
{
fileURL: "TRL",
Expand All @@ -118,82 +117,13 @@ describe('videojs.vast plugin', function() {
expect(sources[s].height).toBeDefined();
}
});

//phantomjs does not support video
xit("should not accept all video formats", function() {
var media_files = [
{
fileURL: "TRL",
mimeType: "video/flv",
width: 640,
height: 360
},
{
fileURL: "So Trill",
mimeType: "video/x-flv",
width: 512,
height: 288
},
{
fileURL: "Skrill, WHERES MY SKRILL",
mimeType: "video/mpeg",
width: 0,
height: 0
},
{
fileURL: "Jack and Jill",
mimeType: "video/ogg",
width: 1280,
height: 720
},
{
fileURL: "-- Code Raps -- ",
mimeType: "hls/playlist.m3u8",
width: 640,
height: 480
},
{
fileURL: "I'm tapped",
mimeType: "video/avi",
width: 512,
height: 384
},
{
fileURL: "BuddhaForRill",
mimeType: "video/mp4",
width: 1920,
height: 1080
}
];
var sources = player.vast.createSourceObjects(media_files);
expect(sources.length).toBe(1);
});

//phantomjs does not support video
xit("can return sources with duplicate formats", function() {
var media_files = [
{
fileURL: "TRL",
mimeType: "video/mp4",
width: 640,
height: 360
},
{
fileURL: "BuddhaForRill",
mimeType: "video/mp4",
width: 853,
height: 480
}
];
var sources = player.vast.createSourceObjects(media_files);
expect(sources.length).toBe(2);
});

});

describe("tearDown", function() {

it("should end the linear ad", function() {
player.vast({ url: 'balhbahblhab' });

spyOn(player.ads, "endLinearAdMode");
spyOn(player, "off");

Expand All @@ -207,11 +137,13 @@ describe('videojs.vast plugin', function() {
expect(player.off).toHaveBeenCalledWith("ended", jasmine.any(Function));
expect(player.ads.endLinearAdMode).toHaveBeenCalled();
});

});

describe("preroll", function() {

beforeEach(function() {
player.vast({ url: 'balhbahblhab' });
player.vastTracker = {
clickThroughURLTemplate: "a whole new page",
clickTrackingURLTemplate: "a new fantastic advertisement",
Expand All @@ -224,6 +156,7 @@ describe('videojs.vast plugin', function() {
it("should start the ad", function() {
spyOn(player.ads, "startLinearAdMode");
spyOn(player, "src");
player.vast.sources = [];
player.vast.preroll();
expect(player.ads.startLinearAdMode).toHaveBeenCalled();
expect(player.src).toHaveBeenCalledWith(jasmine.any(Array));
Expand All @@ -239,6 +172,7 @@ describe('videojs.vast plugin', function() {

describe("getContent", function() {
it("should bail out if there aren't playable media files", function() {
player.vast({ url: 'balhbahblhab' });
spyOn(DMVAST.client, "get").and.callFake(function(url, callback){
var fake_response = {
ads: [{
Expand All @@ -250,15 +184,15 @@ describe('videojs.vast plugin', function() {
};
callback(fake_response);
});

spyOn(player, "trigger");
player.vast.getContent("some url");
expect(player.trigger).toHaveBeenCalledWith("adtimeout");
});

describe("linear ads", function() {
beforeEach(function() {
spyOn(vast.client, "get")
spyOn(DMVAST.client, "get")
.and.callFake(function(url, callback){
var fake_response = {
ads: [{
Expand All @@ -271,38 +205,6 @@ describe('videojs.vast plugin', function() {
callback(fake_response);
});
});

//phantomjs does not support video
xit("should create a vast tracker", function() {
spyOn(vast, "tracker");
player.vast.getContent("some url");
expect(vast.tracker).toHaveBeenCalled();
expect(player.vastTracker).toBeDefined();
});

//phantomjs does not support video
xit("should guarantee that assetDuration is defined", function() {
spyOn(vast, "tracker").and.returnValue({
setProgress: function(){}
});
spyOn(player, "duration").and.returnValue(11);
spyOn(player, "currentTime").and.returnValue(11);
player.vast.getContent("some url");

/*
Forcing some implementation here, but the
duration fail-safe is a hack anyway.
*/
player.trigger("timeupdate");
expect(player.vastTracker.assetDuration).toBe(11);
});

//phantomjs does not support video
xit("should trigger the 'adsready' event", function() {
spyOn(player, "trigger");
player.vast.getContent("some url");
expect(player.trigger).toHaveBeenCalledWith("adsready");
});
});

describe("non-linear ads", function() {
Expand All @@ -322,6 +224,7 @@ describe('videojs.vast plugin', function() {
});
});
it("should do nothing with non-linear ads, and report the error", function() {
player.vast({ url: 'balhbahblhab' });
spyOn(DMVAST.util, "track");
spyOn(player, "trigger");
player.vast.getContent("some url");
Expand Down
104 changes: 51 additions & 53 deletions videojs.vast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

(function(vjs, vast) {
(function(window, vjs, vast) {
'use strict';

var extend = function(obj) {
var arg, i, k;
Expand All @@ -22,54 +21,54 @@

Vast = function (player, settings) {

var createSourceObjects = function (media_files) {
var sourcesByFormat = {}, i, j, tech;
var techOrder = player.options().techOrder;
for (i = 0, j = techOrder.length; i < j; i++) {
var techName = techOrder[i].charAt(0).toUpperCase() + techOrder[i].slice(1);
tech = window.videojs[techName];

// Check if the current tech is defined before continuing
if (!tech) {
continue;
}
// return vast plugin
return {
createSourceObjects: function (media_files) {
var sourcesByFormat = {}, i, j, tech;
var techOrder = player.options().techOrder;
for (i = 0, j = techOrder.length; i < j; i++) {
var techName = techOrder[i].charAt(0).toUpperCase() + techOrder[i].slice(1);
tech = window.videojs[techName];

// Check if the current tech is defined before continuing
if (!tech) {
continue;
}

// Check if the browser supports this technology
if (tech.isSupported()) {
// Loop through each source object
for (var a = 0, b = media_files.length; a < b; a++) {
var media_file = media_files[a];
var source = {type:media_file.mimeType, src:media_file.fileURL};
// Check if source can be played with this technology
if (tech.canPlaySource(source)) {
if (sourcesByFormat[techOrder[i]] === undefined) {
sourcesByFormat[techOrder[i]] = [];
// Check if the browser supports this technology
if (tech.isSupported()) {
// Loop through each source object
for (var a = 0, b = media_files.length; a < b; a++) {
var media_file = media_files[a];
var source = {type:media_file.mimeType, src:media_file.fileURL};
// Check if source can be played with this technology
if (tech.canPlaySource(source)) {
if (sourcesByFormat[techOrder[i]] === undefined) {
sourcesByFormat[techOrder[i]] = [];
}
sourcesByFormat[techOrder[i]].push({
type:media_file.mimeType,
src: media_file.fileURL,
width: media_file.width,
height: media_file.height
});
}
sourcesByFormat[techOrder[i]].push({
type:media_file.mimeType,
src: media_file.fileURL,
width: media_file.width,
height: media_file.height
});
}
}
}
}
// Create sources in preferred format order
var sources = [];
for (j = 0; j < techOrder.length; j++) {
tech = techOrder[j];
if (sourcesByFormat[tech] !== undefined) {
for (i = 0; i < sourcesByFormat[tech].length; i++) {
sources.push(sourcesByFormat[tech][i]);
// Create sources in preferred format order
var sources = [];
for (j = 0; j < techOrder.length; j++) {
tech = techOrder[j];
if (sourcesByFormat[tech] !== undefined) {
for (i = 0; i < sourcesByFormat[tech].length; i++) {
sources.push(sourcesByFormat[tech][i]);
}
}
}
}
return sources;
};
return sources;
},

// return vast plugin
return {
getContent: function () {

// query vast url given in settings
Expand All @@ -85,7 +84,7 @@

if (creative.mediaFiles.length) {

player.vast.sources = createSourceObjects(creative.mediaFiles);
player.vast.sources = player.vast.createSourceObjects(creative.mediaFiles);

if (!player.vast.sources.length) {
player.trigger('adtimeout');
Expand Down Expand Up @@ -167,13 +166,12 @@
preroll: function() {
player.ads.startLinearAdMode();
player.vast.showControls = player.controls();
if (player.vast.showControls ) {
if (player.vast.showControls) {
player.controls(false);
}

// load linear ad sources and start playing them
var adSources = player.vast.sources;
player.src(adSources);
player.src(player.vast.sources);

var clickthrough;
if (player.vastTracker.clickThroughURLTemplate) {
Expand All @@ -185,7 +183,7 @@
}
)[0];
}
var blocker = document.createElement("a");
var blocker = window.document.createElement("a");
blocker.className = "vast-blocker";
blocker.href = clickthrough || "#";
blocker.target = "_blank";
Expand All @@ -203,7 +201,7 @@
player.vast.blocker = blocker;
player.el().insertBefore(blocker, player.controlBar.el());

var skipButton = document.createElement("div");
var skipButton = window.document.createElement("div");
skipButton.className = "vast-skip-button";
if (settings.skip < 0) {
skipButton.style.display = "none";
Expand All @@ -218,7 +216,7 @@
player.vastTracker.skip();
player.vast.tearDown();
}
if(Event.prototype.stopPropagation !== undefined) {
if(window.Event.prototype.stopPropagation !== undefined) {
e.stopPropagation();
} else {
return false;
Expand Down Expand Up @@ -272,7 +270,7 @@

// check that we have the ads plugin
if (player.ads === undefined) {
console.error('vast video plugin requires videojs-contrib-ads, vast plugin not initialized');
window.console.error('vast video plugin requires videojs-contrib-ads, vast plugin not initialized');
return null;
}

Expand All @@ -287,7 +285,6 @@

player.on('vast-ready', function () {
// vast is prepared with content, set up ads and trigger ready function
player.ads(settings.adsSettings);
player.trigger('adsready');
});

Expand Down Expand Up @@ -323,4 +320,5 @@
};

vjs.plugin('vast', vastPlugin);
}(window.videojs, window.DMVAST));

}(window, videojs, DMVAST));

0 comments on commit 0432666

Please sign in to comment.