forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-amp-auto-ads.js
331 lines (285 loc) · 10.4 KB
/
test-amp-auto-ads.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* 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.
*/
import {AmpAutoAds} from '../amp-auto-ads';
import {
toggleExperiment,
forceExperimentBranch,
} from '../../../../src/experiments';
import {xhrFor} from '../../../../src/services';
import {waitForChild} from '../../../../src/dom';
import {viewportForDoc} from '../../../../src/services';
import {
ADSENSE_AMP_AUTO_ADS_HOLDOUT_EXPERIMENT_NAME,
AdSenseAmpAutoAdsHoldoutBranches,
} from '../../../../ads/google/adsense-amp-auto-ads';
describes.realWin('amp-auto-ads', {
amp: {
runtimeOn: true,
ampdoc: 'single',
extensions: ['amp-ad', 'amp-auto-ads'],
},
}, env => {
const AD_CLIENT = 'ca-pub-1234';
let sandbox;
let container;
let anchor1;
let anchor2;
let anchor3;
let anchor4;
let ampAutoAds;
let ampAutoAdsElem;
let xhr;
let configObj;
beforeEach(() => {
// There seem to be a lot of tests that don't clean up after themselves,
// meaning there are a lot of <AMP-AD>s left over in the DOM.
// An alternative to doing this clean up would be for this test to do all
// its DOM stuff in a dedicated window, but trying that seems to result in a
// lot of errors.
const ampAds = env.win.document.getElementsByTagName('AMP-AD');
while (ampAds.length) {
ampAds[0].parentNode.removeChild(ampAds[0]);
}
toggleExperiment(env.win, 'amp-auto-ads', true);
sandbox = env.sandbox;
const viewportMock = sandbox.mock(viewportForDoc(env.win.document));
viewportMock.expects('getSize').returns(
{width: 320, height: 500}).atLeast(1);
container = env.win.document.createElement('div');
env.win.document.body.appendChild(container);
anchor1 = env.win.document.createElement('div');
anchor1.id = 'anId1';
container.appendChild(anchor1);
const spacer = env.win.document.createElement('div');
spacer.style.height = '1000px';
container.appendChild(spacer);
anchor2 = env.win.document.createElement('div');
anchor2.id = 'anId2';
container.appendChild(anchor2);
const spacer2 = env.win.document.createElement('div');
spacer2.style.height = '499px';
container.appendChild(spacer2);
anchor3 = env.win.document.createElement('div');
anchor3.id = 'anId3';
container.appendChild(anchor3);
const spacer3 = env.win.document.createElement('div');
spacer3.style.height = '500px';
container.appendChild(spacer3);
anchor4 = env.win.document.createElement('div');
anchor4.id = 'anId4';
container.appendChild(anchor4);
ampAutoAdsElem = env.win.document.createElement('amp-auto-ads');
env.win.document.body.appendChild(ampAutoAdsElem);
configObj = {
placements: [
{
anchor: {
selector: 'DIV#anId1',
},
pos: 2,
type: 1,
},
{
anchor: {
selector: 'DIV#anId2',
},
pos: 2,
type: 1,
},
{
anchor: {
selector: 'DIV#anId3',
},
pos: 2,
type: 1,
},
{
anchor: {
selector: 'DIV#anId4',
},
pos: 2,
type: 1,
},
],
};
xhr = xhrFor(env.win);
xhr.fetchJson = () => {
return Promise.resolve({
json() {
return Promise.resolve(configObj);
},
});
};
sandbox.spy(xhr, 'fetchJson');
ampAutoAds = new AmpAutoAds(ampAutoAdsElem);
});
function verifyAdElement(adElement) {
expect(adElement.tagName).to.equal('AMP-AD');
expect(adElement.getAttribute('type')).to.equal('adsense');
expect(adElement.getAttribute('data-ad-client')).to.equal(AD_CLIENT);
}
it('should insert three ads on page using config', () => {
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return new Promise(resolve => {
waitForChild(anchor4, parent => {
return parent.childNodes.length > 0;
}, () => {
expect(anchor1.childNodes).to.have.lengthOf(1);
expect(anchor2.childNodes).to.have.lengthOf(1);
expect(anchor3.childNodes).to.have.lengthOf(0);
expect(anchor4.childNodes).to.have.lengthOf(1);
verifyAdElement(anchor1.childNodes[0]);
verifyAdElement(anchor2.childNodes[0]);
verifyAdElement(anchor4.childNodes[0]);
resolve();
});
});
});
it('should insert ads on the page when in holdout experiment branch', () => {
forceExperimentBranch(env.win,
ADSENSE_AMP_AUTO_ADS_HOLDOUT_EXPERIMENT_NAME,
AdSenseAmpAutoAdsHoldoutBranches.EXPERIMENT);
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return new Promise(resolve => {
waitForChild(anchor4, parent => {
return parent.childNodes.length > 0;
}, () => {
expect(anchor1.childNodes).to.have.lengthOf(1);
expect(anchor2.childNodes).to.have.lengthOf(1);
expect(anchor3.childNodes).to.have.lengthOf(0);
expect(anchor4.childNodes).to.have.lengthOf(1);
verifyAdElement(anchor1.childNodes[0]);
verifyAdElement(anchor2.childNodes[0]);
verifyAdElement(anchor4.childNodes[0]);
resolve();
});
});
});
it('should not insert ads on the page when in holdout control branch', () => {
forceExperimentBranch(env.win,
ADSENSE_AMP_AUTO_ADS_HOLDOUT_EXPERIMENT_NAME,
AdSenseAmpAutoAdsHoldoutBranches.CONTROL);
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return new Promise(resolve => {
setTimeout(() => {
expect(anchor1.childNodes).to.have.lengthOf(0);
expect(anchor2.childNodes).to.have.lengthOf(0);
expect(anchor3.childNodes).to.have.lengthOf(0);
expect(anchor4.childNodes).to.have.lengthOf(0);
resolve();
}, 500);
});
});
it('should insert ads with the config provided attributes', () => {
configObj.attributes = {
'bad-name': 'should be filtered',
'data-custom-att-1': 'val-1',
'data-custom-att-2': 'val-2',
};
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return new Promise(resolve => {
waitForChild(anchor4, parent => {
return parent.childNodes.length > 0;
}, () => {
expect(anchor1.childNodes).to.have.lengthOf(1);
expect(anchor1.childNodes[0].getAttribute('data-custom-att-1'))
.to.equal('val-1');
expect(anchor1.childNodes[0].getAttribute('data-custom-att-2'))
.to.equal('val-2');
expect(anchor2.childNodes).to.have.lengthOf(1);
expect(anchor2.childNodes[0].getAttribute('data-custom-att-1'))
.to.equal('val-1');
expect(anchor2.childNodes[0].getAttribute('data-custom-att-2'))
.to.equal('val-2');
expect(anchor4.childNodes).to.have.lengthOf(1);
expect(anchor4.childNodes[0].getAttribute('data-custom-att-1'))
.to.equal('val-1');
expect(anchor4.childNodes[0].getAttribute('data-custom-att-2'))
.to.equal('val-2');
resolve();
});
});
});
it('should override ad network type with config provided attribute', () => {
configObj.attributes = {
'type': 'doubleclick',
};
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return new Promise(resolve => {
waitForChild(anchor4, parent => {
return parent.childNodes.length > 0;
}, () => {
expect(anchor1.childNodes).to.have.lengthOf(1);
expect(anchor1.childNodes[0].getAttribute('type'))
.to.equal('doubleclick');
expect(anchor2.childNodes).to.have.lengthOf(1);
expect(anchor2.childNodes[0].getAttribute('type'))
.to.equal('doubleclick');
expect(anchor4.childNodes).to.have.lengthOf(1);
expect(anchor4.childNodes[0].getAttribute('type'))
.to.equal('doubleclick');
resolve();
});
});
});
it('should fetch a config from the correct URL', () => {
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
ampAutoAds.buildCallback();
return ampAutoAds.layoutCallback().then(() => {
expect(xhr.fetchJson).to.have.been.calledWith(
'//pagead2.googlesyndication.com/getconfig/ama?client=' +
AD_CLIENT + '&plah=localhost&ama_t=amp', {
mode: 'cors',
method: 'GET',
credentials: 'omit',
requireAmpResponseSourceOrigin: false,
});
expect(xhr.fetchJson).to.be.calledOnce;
});
});
it('should throw an error if no type', () => {
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
expect(() => ampAutoAds.buildCallback())
.to.throw(/Missing type attribute/);
expect(xhr.fetchJson).not.to.have.been.called;
});
it('should not try and fetch config if unknown type', () => {
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'unknowntype');
expect(() => ampAutoAds.buildCallback())
.to.throw(/No AdNetworkConfig for type: unknowntype/);
expect(xhr.fetchJson).not.to.have.been.called;
});
it('should not try and fetch config if experiment off', () => {
toggleExperiment(env.win, 'amp-auto-ads', false);
ampAutoAdsElem.setAttribute('data-ad-client', AD_CLIENT);
ampAutoAdsElem.setAttribute('type', 'adsense');
expect(() => ampAutoAds.buildCallback())
.to.throw(/Experiment is off/);
expect(xhr.fetchJson).not.to.have.been.called;
});
});