Skip to content

Commit

Permalink
SpotX Bid Adapter: default to 4/3 aspect ratio when response doesn't …
Browse files Browse the repository at this point in the history
…contain w or h (#6159)

* Default to 4/3 aspect ratio when response doesn't contain w or h

* SpotX bid adapter: reorder tests and remove extra assertions
  • Loading branch information
agdillon authored Jan 28, 2021
1 parent 4f2af66 commit 5f56b18
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
46 changes: 21 additions & 25 deletions modules/spotxBidAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const spec = {
return false;
}
if (!utils.getBidIdParameter('slot', bid.params.outstream_options)) {
utils.logError(BIDDER_CODE + ': please define parameters slot outstream_options object in the configuration.');
utils.logError(BIDDER_CODE + ': please define parameter slot in outstream_options object in the configuration.');
return false;
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ export const spec = {
}
});
} catch (err) {
utils.logWarn('Prebid Error calling setRender or setEve,tHandlers on renderer', err);
utils.logWarn('Prebid Error calling setRender or setEventHandlers on renderer', err);
}
bid.renderer = renderer;
}
Expand All @@ -408,7 +408,7 @@ function createOutstreamScript(bid) {
dataSpotXParams['data-spotx_content_page_url'] = bid.renderer.config.content_page_url;
dataSpotXParams['data-spotx_ad_unit'] = 'incontent';

utils.logMessage('[SPOTX][renderer] Default beahavior');
utils.logMessage('[SPOTX][renderer] Default behavior');
if (utils.getBidIdParameter('ad_mute', bid.renderer.config.outstream_options)) {
dataSpotXParams['data-spotx_ad_mute'] = '1';
}
Expand All @@ -419,30 +419,26 @@ function createOutstreamScript(bid) {

const playersizeAutoAdapt = utils.getBidIdParameter('playersize_auto_adapt', bid.renderer.config.outstream_options);
if (playersizeAutoAdapt && utils.isBoolean(playersizeAutoAdapt) && playersizeAutoAdapt === true) {
if (bid.width && utils.isNumber(bid.width) && bid.height && utils.isNumber(bid.height)) {
const ratio = bid.width / bid.height;
const slotClientWidth = window.document.getElementById(slot).clientWidth;
let playerWidth = bid.renderer.config.player_width;
let playerHeight = bid.renderer.config.player_height;
let contentWidth = 0;
let contentHeight = 0;
if (slotClientWidth < playerWidth) {
playerWidth = slotClientWidth;
playerHeight = playerWidth / ratio;
}
if (ratio <= 1) {
contentWidth = Math.round(playerHeight * ratio);
contentHeight = playerHeight;
} else {
contentWidth = playerWidth;
contentHeight = Math.round(playerWidth / ratio);
}

dataSpotXParams['data-spotx_content_width'] = '' + contentWidth;
dataSpotXParams['data-spotx_content_height'] = '' + contentHeight;
const ratio = bid.width && utils.isNumber(bid.width) && bid.height && utils.isNumber(bid.height) ? bid.width / bid.height : 4 / 3;
const slotClientWidth = window.document.getElementById(slot).clientWidth;
let playerWidth = bid.renderer.config.player_width;
let playerHeight = bid.renderer.config.player_height;
let contentWidth = 0;
let contentHeight = 0;
if (slotClientWidth < playerWidth) {
playerWidth = slotClientWidth;
playerHeight = playerWidth / ratio;
}
if (ratio <= 1) {
contentWidth = Math.round(playerHeight * ratio);
contentHeight = playerHeight;
} else {
utils.logWarn('[SPOTX][renderer] PlayerSize auto adapt: bid.width and bid.height are incorrect');
contentWidth = playerWidth;
contentHeight = Math.round(playerWidth / ratio);
}

dataSpotXParams['data-spotx_content_width'] = '' + contentWidth;
dataSpotXParams['data-spotx_content_height'] = '' + contentHeight;
}

const customOverride = utils.getBidIdParameter('custom_override', bid.renderer.config.outstream_options);
Expand Down
43 changes: 40 additions & 3 deletions test/spec/modules/spotxBidAdapter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ describe('the spotx adapter', function () {
});
});

describe('oustreamRender', function() {
describe('outstreamRender', function() {
var serverResponse, bidderRequestObj;

beforeEach(function() {
Expand Down Expand Up @@ -545,7 +545,7 @@ describe('the spotx adapter', function () {
it('should attempt to insert the EASI script', function() {
var scriptTag;
sinon.stub(window.document, 'getElementById').returns({
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script })
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script; })
});
var responses = spec.interpretResponse(serverResponse, bidderRequestObj);

Expand Down Expand Up @@ -573,7 +573,7 @@ describe('the spotx adapter', function () {
nodeName: 'IFRAME',
contentDocument: {
body: {
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script })
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script; })
}
}
});
Expand All @@ -598,5 +598,42 @@ describe('the spotx adapter', function () {
expect(scriptTag.getAttribute('data-spotx_content_height')).to.equal('300');
window.document.getElementById.restore();
});

it('should adjust width and height to match slot clientWidth if playersize_auto_adapt is used', function() {
var scriptTag;
sinon.stub(window.document, 'getElementById').returns({
clientWidth: 200,
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script; })
});
var responses = spec.interpretResponse(serverResponse, bidderRequestObj);

responses[0].renderer.render(responses[0]);

expect(scriptTag.getAttribute('type')).to.equal('text/javascript');
expect(scriptTag.getAttribute('src')).to.equal('https://js.spotx.tv/easi/v1/12345.js');
expect(scriptTag.getAttribute('data-spotx_content_width')).to.equal('200');
expect(scriptTag.getAttribute('data-spotx_content_height')).to.equal('150');
window.document.getElementById.restore();
});

it('should use a default 4/3 ratio if playersize_auto_adapt is used and response does not contain width or height', function() {
delete serverResponse.body.seatbid[0].bid[0].w;
delete serverResponse.body.seatbid[0].bid[0].h;

var scriptTag;
sinon.stub(window.document, 'getElementById').returns({
clientWidth: 200,
appendChild: sinon.stub().callsFake(function(script) { scriptTag = script; })
});
var responses = spec.interpretResponse(serverResponse, bidderRequestObj);

responses[0].renderer.render(responses[0]);

expect(scriptTag.getAttribute('type')).to.equal('text/javascript');
expect(scriptTag.getAttribute('src')).to.equal('https://js.spotx.tv/easi/v1/12345.js');
expect(scriptTag.getAttribute('data-spotx_content_width')).to.equal('200');
expect(scriptTag.getAttribute('data-spotx_content_height')).to.equal('150');
window.document.getElementById.restore();
});
});
});

0 comments on commit 5f56b18

Please sign in to comment.