Skip to content

Commit

Permalink
StickyAdsTV bidder adapter update (prebid#1311)
Browse files Browse the repository at this point in the history
* add stickyadsTV bidder adapter

* init unit test file

* ad some unit tests

* fix unit test on ad format with parameters

* add some unit tests

* add unit tests on getBid method

* add some test cases in unit tests

* minor fix on component id tag.

* remove adapters-sticky.json test file

* use top most accessible window instead of window.top

* Pass in the bid request in the createBid call.

* use top most accessible window instead of window.top

* add unit tests

* update unit tests

* fix unit test.

* fix CI build

* add alias freewheel-ssp

* update unit tests on bidderCode value

* fix component id values and add unit tests

* allws to use any outstream format.

* fix ASLoader on futur outstream format versions

* minor: fix code format.

* update unit tests

* minor fix code format

* minor: add missing new line at eof
  • Loading branch information
guillaume-sticky authored and dbemiller committed Jul 17, 2017
1 parent 2c8ec16 commit e748bef
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 54 deletions.
90 changes: 43 additions & 47 deletions modules/stickyadstvBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ var adaptermanager = require('src/adaptermanager');
var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
var STICKYADS_BIDDERCODE = 'stickyadstv';
var MUSTANG_URL = '//cdn.stickyadstv.com/mustang/mustang.min.js';
var INTEXTROLL_URL = '//cdn.stickyadstv.com/prime-time/intext-roll.min.js';
var SCREENROLL_URL = '//cdn.stickyadstv.com/prime-time/screen-roll.min.js';
var OUTSTREAM_URL = '//cdn.stickyadstv.com/prime-time/[COMP-ID].min.js';

var topMostWindow = getTopMostWindow();
topMostWindow.stickyadstv_cache = {};
Expand All @@ -34,11 +33,9 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
var integrationType = bid.params.format ? bid.params.format : 'inbanner';
var urltoLoad = MUSTANG_URL;

if (integrationType === 'intext-roll') {
urltoLoad = INTEXTROLL_URL;
}
if (integrationType === 'screen-roll') {
urltoLoad = SCREENROLL_URL;
if (integrationType !== 'inbanner') {
// integration types are equals to component ids
urltoLoad = OUTSTREAM_URL.replace('[COMP-ID]', integrationType);
}

var bidRegistered = false;
Expand All @@ -56,6 +53,12 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
var zoneId = bid.params.zoneId || bid.params.zone; // accept both
var size = getBiggerSize(bid.sizes);

// some of our formats doesn't have tools API exposed
var toolsAPI = window.com.stickyadstv.tools;
if (toolsAPI && toolsAPI.ASLoader) {
topMostWindow.stickyadstv_asLoader = new toolsAPI.ASLoader(zoneId, getComponentId(bid.params.format));
}

var vastLoader = new window.com.stickyadstv.vast.VastLoader();
bid.vast = topMostWindow.stickyadstv_cache[bid.placementCode] = vastLoader.getVast();

Expand All @@ -79,17 +82,36 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
zoneId: zoneId,
playerSize: size[0] + 'x' + size[1],
vastUrlParams: bid.params.vastUrlParams,
componentId: 'prebid-sticky' + (bid.params.format ? '-' + bid.params.format : '')
componentId: getComponentId(bid.params.format)
};

if (bid.params.format === 'screen-roll') {
// in screenroll case we never use the original div size.
config.playerSize = window.com.stickyadstv.screenroll.getPlayerSize();
var api = window.com.stickyadstv[getAPIName(bid.params.format)];
if (api && typeof api.getPlayerSize === 'function') {
// in screenroll and similar cases we don't use the original div size.
config.playerSize = api.getPlayerSize();
}

vastLoader.load(config, vastCallback);
}

function getComponentId(inputFormat) {
var component = 'mustang'; // default component id

if (inputFormat && inputFormat !== 'inbanner') {
// format identifiers are equals to their component ids.
component = inputFormat;
}

return component;
}

function getAPIName(componentId) {
componentId = componentId || '';

// remove dash in componentId to get API name
return componentId.replace('-', '');
}

function getBiggerSize(array) {
var result = [1, 1];
for (var i = 0; i < array.length; i++) {
Expand All @@ -114,14 +136,15 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
' autoPlay:true' +
'};' +
'var ad = new topWindow.com.stickyadstv.vpaid.Ad(document.getElementById("stickyadstv_prebid_target"),config);' +
'if(topWindow.stickyadstv_asLoader) topWindow.stickyadstv_asLoader.registerEvents(ad);' +
'ad.initAd(' + size[0] + ',' + size[1] + ',"",0,"","");' +

'</script>';

return divHtml + script;
};

var formatIntextHTML = function(bid) {
var formatOutstreamHTML = function(bid) {
var placementCode = bid.placementCode;

var config = bid.params;
Expand All @@ -136,7 +159,8 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
'var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();' +
'var vast = topWindow.stickyadstv_cache["' + placementCode + '"];' +
'var config = {' +
' preloadedVast:vast';
' preloadedVast:vast,' +
' ASLoader:topWindow.stickyadstv_asLoader';

for (var key in config) {
// dont' send format parameter
Expand All @@ -147,50 +171,20 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
}
script += '};' +

'topWindow.com.stickyadstv.intextroll.start(config);' +
'topWindow.com.stickyadstv.' + getAPIName(bid.params.format) + '.start(config);' +

'</script>';

return script;
};

var formatScreenRollHTML = function(bid) {
var placementCode = bid.placementCode;

var config = bid.params;

var script = "<script type='text/javascript'>" +

// get the top most accessible window
'var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();' +
'var vast = topWindow.stickyadstv_cache["' + placementCode + '"];' +
'var config = {' +
' preloadedVast:vast';

for (var key in config) {
// dont' send format parameter
// neither zone nor vastUrlParams values as Vast is already loaded
if (config.hasOwnProperty(key) && key !== 'format' && key !== 'zone' && key !== 'zoneId' && key !== 'vastUrlParams') {
script += ',' + key + ':"' + config[key] + '"';
}
}
script += '};' +

'topWindow.com.stickyadstv.screenroll.start(config);' +

'</script>';

return script;
};

function formatAdHTML(bid, size) {
var integrationType = bid.params.format;

var html = '';
if (integrationType === 'intext-roll') {
html = formatIntextHTML(bid);
} else if (integrationType === 'screen-roll') {
html = formatScreenRollHTML(bid);
if (integrationType && integrationType !== 'inbanner') {
html = formatOutstreamHTML(bid);
} else {
html = formatInBannerHTML(bid, size);
}
Expand All @@ -202,7 +196,7 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
var priceData = vast.getPricing();

if (!priceData) {
utils.logWarn("StickyAdsTV: Bid pricing Can't be retreived. You may need to enable pricing on you're zone. Please get in touch with your sticky contact.");
console.warn("freewheel-ssp: Bid pricing Can't be retreived. You may need to enable pricing on you're zone. Please get in touch with your Freewheel contact.");
}

return priceData;
Expand Down Expand Up @@ -265,6 +259,8 @@ var StickyAdsTVAdapter = function StickyAdsTVAdapter() {
getBiggerSize: getBiggerSize,
getBid: getBid,
getTopMostWindow: getTopMostWindow,
getComponentId: getComponentId,
getAPIName: getAPIName,
createNew: StickyAdsTVAdapter.createNew // enable alias feature (to be used for freewheel-ssp alias)
});
};
Expand Down
31 changes: 24 additions & 7 deletions test/spec/modules/stickyadstvBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ describe('StickyAdsTV Adapter', function () {
delete window.com.stickyadstv;
});

it('should have returned a valid bidObject', function () {
it('should return a valid bidObject', function () {
expect(bidResponse).to.have.property('cpm', 4.000);
expect(bidResponse).to.have.property('ad', "<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache[\"foo\"];var config = { preloadedVast:vast};topWindow.com.stickyadstv.screenroll.start(config);</script>");
expect(bidResponse).to.have.property('ad', "<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache[\"foo\"];var config = { preloadedVast:vast, ASLoader:topWindow.stickyadstv_asLoader,domId:\"foo\"};topWindow.com.stickyadstv.screenroll.start(config);</script>");
expect(bidResponse).to.have.property('bidderCode', 'stickyadstv');
expect(bidResponse).to.have.property('currencyCode', 'USD');
expect(bidResponse).to.have.property('width', 300);
Expand Down Expand Up @@ -173,24 +173,24 @@ describe('StickyAdsTV Adapter', function () {
it('should create an inBanner ad format', function () {
let result = adapter.formatAdHTML({placementCode: 'placementCodeValue', params: {}}, [200, 300]);

expect(result).to.equal('<div id="stickyadstv_prebid_target"></div><script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast, autoPlay:true};var ad = new topWindow.com.stickyadstv.vpaid.Ad(document.getElementById("stickyadstv_prebid_target"),config);ad.initAd(200,300,"",0,"","");</script>');
expect(result).to.equal('<div id="stickyadstv_prebid_target"></div><script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast, autoPlay:true};var ad = new topWindow.com.stickyadstv.vpaid.Ad(document.getElementById("stickyadstv_prebid_target"),config);if(topWindow.stickyadstv_asLoader) topWindow.stickyadstv_asLoader.registerEvents(ad);ad.initAd(200,300,"",0,"","");</script>');
});

it('should create an intext ad format', function () {
let result = adapter.formatAdHTML({placementCode: 'placementCodeValue', params: {format: 'intext-roll', auto: 'v2', smartPlay: 'true'}}, [200, 300]);

expect(result).to.equal('<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast,auto:"v2",smartPlay:"true"};topWindow.com.stickyadstv.intextroll.start(config);</script>');
expect(result).to.equal('<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast, ASLoader:topWindow.stickyadstv_asLoader,auto:"v2",smartPlay:"true"};topWindow.com.stickyadstv.intextroll.start(config);</script>');
});

it('should create a screenroll ad format', function () {
let result = adapter.formatAdHTML({placementCode: 'placementCodeValue', params: {format: 'screen-roll', smartPlay: 'true'}}, [200, 300]);

expect(result).to.equal('<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast,smartPlay:"true"};topWindow.com.stickyadstv.screenroll.start(config);</script>');
expect(result).to.equal('<script type=\'text/javascript\'>var topWindow = (function(){var res=window; try{while(top != res){if(res.parent.location.href.length)res=res.parent;}}catch(e){}return res;})();var vast = topWindow.stickyadstv_cache["placementCodeValue"];var config = { preloadedVast:vast, ASLoader:topWindow.stickyadstv_asLoader,smartPlay:"true",domId:"placementCodeValue"};topWindow.com.stickyadstv.screenroll.start(config);</script>');
});
});

describe('getBiggerSize', function () {
it('should returns the bigger size', function () {
it('should return the bigger size', function () {
let result = adapter.getBiggerSize([[1, 4000], [4000, 1], [200, 300], [0, 0]]);

expect(result[0]).to.equal(200);
Expand All @@ -199,10 +199,27 @@ describe('StickyAdsTV Adapter', function () {
});

describe('top most window', function () {
it('should returns the top most window', function () {
it('should return the top most window', function () {
let result = adapter.getTopMostWindow();

expect(result).to.equal(window.top);
});
});

describe('get component id', function() {
it('should return valid component ids', function() {
expect(adapter.getComponentId('inbanner')).to.equal('mustang');
expect(adapter.getComponentId('intext-roll')).to.equal('intext-roll');
expect(adapter.getComponentId('screen-roll')).to.equal('screen-roll');
});
});

describe('get API name', function() {
it('should return valid API names', function() {
expect(adapter.getAPIName()).to.equal('');
expect(adapter.getAPIName('intext-roll')).to.equal('intextroll');
expect(adapter.getAPIName('screen-roll')).to.equal('screenroll');
expect(adapter.getAPIName('floorad')).to.equal('floorad');
});
});
});

0 comments on commit e748bef

Please sign in to comment.