/
test-amp-ad-xorigin-iframe-handler.js
287 lines (260 loc) · 9.28 KB
/
test-amp-ad-xorigin-iframe-handler.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
/**
* 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 {AmpAdXOriginIframeHandler} from '../amp-ad-xorigin-iframe-handler';
import {BaseElement} from '../../../../src/base-element';
import {ampdocServiceFor} from '../../../../src/ampdoc';
import {
createIframeWithMessageStub,
expectPostMessage,
} from '../../../../testing/iframe';
import {AmpAdUIHandler} from '../amp-ad-ui';
import {timerFor} from '../../../../src/timer';
import * as sinon from 'sinon';
describe('amp-ad-xorigin-iframe-handler', () => {
let sandbox;
let adImpl;
let iframeHandler;
let iframe;
let testIndex = 0;
beforeEach(() => {
sandbox = sinon.sandbox.create();
const ampdocService = ampdocServiceFor(window);
const ampdoc = ampdocService.getAmpDoc();
const adElement = document.createElement('container-element');
adElement.getAmpDoc = () => ampdoc;
adImpl = new BaseElement(adElement);
document.body.appendChild(adElement);
adImpl.uiHandler = new AmpAdUIHandler(adImpl);
iframeHandler = new AmpAdXOriginIframeHandler(adImpl);
testIndex++;
iframe = createIframeWithMessageStub(window);
iframe.setAttribute('data-amp-3p-sentinel', 'amp3ptest' + testIndex);
iframe.name = 'test_nomaster';
});
afterEach(() => {
sandbox.restore();
document.body.removeChild(adImpl.element);
});
describe('init() returned promise', () => {
let initPromise;
describe('if render-start is implemented', () => {
let noContentSpy;
beforeEach(() => {
adImpl.config = {renderStartImplemented: true};
sandbox.stub(adImpl, 'attemptChangeSize', (height, width) => {
expect(height).to.equal(217);
expect(width).to.equal(114);
return Promise.resolve();
});
noContentSpy =
sandbox.spy/*OK*/(iframeHandler, 'freeXOriginIframe');
initPromise = iframeHandler.init(iframe);
});
it('should resolve on message "render-start"', () => {
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'render-start',
});
return initPromise.then(() => {
expect(iframe.style.visibility).to.equal('');
}).then(() => {
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'no-content',
});
return expectPostMessage(iframe.contentWindow, window, {
sentinel: 'amp3ptest' + testIndex,
type: 'no-content',
}).then(() => {
expect(noContentSpy).to.not.been.called;
});
});
});
it('should resolve and resize on message "render-start" w/ size', () => {
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
width: 114,
height: '217', // should be tolerant to string number
type: 'render-start',
sentinel: 'amp3ptest' + testIndex,
});
return initPromise.then(() => {
expect(iframe.style.visibility).to.equal('');
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
});
});
it('should resolve on message "no-content" ' +
'and remove non-master iframe', () => {
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'no-content',
});
return initPromise.then(() => {
expect(noContentSpy).to.be.calledWith(false);
expect(iframeHandler.iframe).to.be.null;
});
});
it('should NOT remove master iframe on message "no-content"', () => {
iframe.name = 'test_master';
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'no-content',
});
return initPromise.then(() => {
expect(iframe.style.visibility).to.equal('');
expect(noContentSpy).to.be.calledOnce;
expect(noContentSpy).to.be.calledWith(true);
});
});
it('should NOT resolve on message "bootstrap-loaded"', () => {
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'bootstrap-loaded',
});
return expectPostMessage(iframe.contentWindow, window, {
sentinel: 'amp3ptest' + testIndex,
type: 'bootstrap-loaded',
}).then(() => {
const clock = sandbox.useFakeTimers();
clock.tick(0);
const timeoutPromise =
timerFor(window).timeoutPromise(2000, initPromise);
clock.tick(2001);
return expect(timeoutPromise).to.eventually
.be.rejectedWith(/timeout/);
});
});
});
it('should resolve on message "bootstrap-loaded" if render-start is'
+ 'NOT implemented', done => {
initPromise = iframeHandler.init(iframe);
iframe.onload = () => {
expect(iframe.style.visibility).to.equal('hidden');
iframe.postMessageToParent({
sentinel: 'amp3ptest' + testIndex,
type: 'bootstrap-loaded',
});
initPromise.then(() => {
expect(iframe.style.visibility).to.equal('');
done();
});
};
});
it('should resolve on timeout', done => {
const noContentSpy =
sandbox.spy/*OK*/(iframeHandler, 'freeXOriginIframe');
const clock = sandbox.useFakeTimers();
iframe.name = 'test_master';
initPromise = iframeHandler.init(iframe);
clock.tick(9999);
expect(noContentSpy).to.not.be.called;
clock.tick(1);
initPromise.then(() => {
expect(iframe.style.visibility).to.equal('');
expect(noContentSpy).to.be.calledOnce;
expect(noContentSpy).to.be.calledWith(true);
done();
});
});
it('should resolve directly if it is A4A', () => {
return iframeHandler.init(iframe, true).then(() => {
expect(iframe.style.visibility).to.equal('');
});
});
});
describe('Initialized iframe', () => {
beforeEach(() => {
iframeHandler.init(iframe);
});
it('should be able to use embed-state API', () => {
iframe.postMessageToParent({
type: 'send-embed-state',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
inViewport: false,
pageHidden: false,
type: 'embed-state',
sentinel: 'amp3ptest' + testIndex,
}));
});
it('should be able to use embed-size API, change size deny', () => {
sandbox.stub(adImpl, 'attemptChangeSize', (height, width) => {
expect(height).to.equal(217);
expect(width).to.equal(114);
return Promise.reject(new Error('for testing'));
});
iframe.postMessageToParent({
width: 114,
height: 217,
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-denied',
sentinel: 'amp3ptest' + testIndex,
}));
});
it('should be able to use embed-size API, change size succeed', () => {
sandbox.stub(adImpl, 'attemptChangeSize', (height, width) => {
expect(height).to.equal(217);
expect(width).to.equal(114);
return Promise.resolve();
});
iframe.postMessageToParent({
width: 114,
height: '217', // should be tolerant to string number
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: 114,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
});
it('should be able to use embed-size API to resize height only', () => {
sandbox.stub(adImpl, 'attemptChangeSize', (height, width) => {
expect(height).to.equal(217);
expect(width).to.be.undefined;
return Promise.resolve();
});
iframe.postMessageToParent({
height: 217,
type: 'embed-size',
sentinel: 'amp3ptest' + testIndex,
});
return iframe.expectMessageFromParent('amp-' + JSON.stringify({
requestedWidth: undefined,
requestedHeight: 217,
type: 'embed-size-changed',
sentinel: 'amp3ptest' + testIndex,
}));
});
});
});