forked from ampproject/amphtml
/
test-highlight-handler.js
298 lines (254 loc) · 10.9 KB
/
test-highlight-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
288
289
290
291
292
293
294
295
296
297
298
/**
* Copyright 2018 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 {HighlightHandler, getHighlightParam} from '../highlight-handler';
import {Messaging, WindowPortEmulator} from '../messaging/messaging';
import {Services} from '../../../../src/services';
import {VisibilityState} from '../../../../src/visibility-state';
import {layoutRectLtwh} from '../../../../src/layout-rect';
describes.fakeWin('getHighlightParam', {
amp: {
ampdoc: 'single',
},
}, env => {
it('get a param', () => {
// URL encoded '{"s":["amp","highlight"]}'.
env.win.location = 'page.html#highlight=' +
'%7B%22s%22%3A%5B%22amp%22%2C%22highlight%22%5D%7D';
expect(getHighlightParam(env.ampdoc)).to.deep.equal({
sentences: ['amp', 'highlight'],
skipRendering: false,
});
});
it('do nothing flag', () => {
// URL encoded '{"s":["amp","highlight"], "n": 1}'
env.win.location = 'page.html#highlight=' +
'%7B%22s%22%3A%5B%22amp%22%2C%22highlight%22%5D%2C%20%22n%22%3A%201%7D';
expect(getHighlightParam(env.ampdoc)).to.deep.equal({
sentences: ['amp', 'highlight'],
skipRendering: true,
});
});
it('no param', () => {
env.win.location = 'page.html';
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('too large json', () => {
const rep = 100 << 10;
env.win.location = 'page.html#highlight=' +
'['.repeat(rep) + ']'.repeat(rep);
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('too many sentences', () => {
const sens = [];
for (let i = 0; i < 20; i++) {
sens.push('a');
}
env.win.location = 'page.html#highlight=' + JSON.stringify({'s': sens});
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('too many chars', () => {
const sens = [];
for (let i = 0; i < 5; i++) {
sens.push('a'.repeat(400));
}
env.win.location = 'page.html#highlight=' + JSON.stringify({'s': sens});
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('if s is not array', () => {
env.win.location = 'page.html#highlight=' +
JSON.stringify({'s': 'invalid'});
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('empty in an array', () => {
env.win.location = 'page.html#highlight=' +
JSON.stringify({'s': ['a', '', 'b']});
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
it('number array', () => {
env.win.location = 'page.html#highlight=' + JSON.stringify({'s': [1, 2]});
expect(getHighlightParam(env.ampdoc)).to.be.a('null');
});
});
describes.realWin('HighlightHandler', {
// We can not overwrite win.location with realWin.
amp: {
ampdoc: 'single',
},
}, env => {
let root = null;
beforeEach(() => {
const {document} = env.win;
root = document.createElement('div');
document.body.appendChild(root);
const div0 = document.createElement('div');
div0.textContent = 'text in amp doc';
root.appendChild(div0);
const div1 = document.createElement('div');
div1.textContent = 'highlighted text';
root.appendChild(div1);
});
it('initialize with visibility=visible', () => {
const {ampdoc} = env;
const scrollStub = sandbox.stub(
Services.viewportForDoc(ampdoc), 'animateScrollIntoView');
scrollStub.returns(Promise.reject());
const sendMsgStub = sandbox.stub(
Services.viewerForDoc(ampdoc), 'sendMessage');
const setScrollTop = sandbox.stub(
Services.viewportForDoc(ampdoc), 'setScrollTop');
const handler = new HighlightHandler(
ampdoc,{sentences: ['amp', 'highlight']});
expect(setScrollTop).to.be.calledOnce;
expect(setScrollTop.firstCall.args.length).to.equal(1);
expect(scrollStub).to.be.calledOnce;
expect(scrollStub.firstCall.args.length).to.equal(1);
expect(scrollStub.firstCall.args[0].style.pointerEvents).to.equal('none');
expect(scrollStub.firstCall.args[0].style.display).to.equal('block');
// For some reason, expect(args).to.deep.equal does not work.
expect(sendMsgStub.callCount).to.equal(2);
expect(sendMsgStub.firstCall.args[0]).to.equal('highlightState');
expect(sendMsgStub.firstCall.args[1]).to.deep.equal(
{state: 'found', scroll: 0});
expect(sendMsgStub.secondCall.args[1]).to.deep.equal(
{state: 'auto_scroll'});
expect(root.innerHTML).to.equal(
'<div>text in <span style="background-color: rgb(255, 150, 50); ' +
'color: rgb(0, 0, 0);">amp</span> doc</div><div>' +
'<span style="background-color: rgb(255, 150, 50); color: ' +
'rgb(0, 0, 0);">highlight</span>ed text</div>');
const viewerOrigin = 'http://localhost:9876';
const port = new WindowPortEmulator(
window, viewerOrigin);
port.addEventListener = function() {};
port.postMessage = function() {};
const messaging = new Messaging(env.win, port);
handler.setupMessaging(messaging);
messaging.handleRequest_({
name: 'highlightDismiss',
});
expect(root.innerHTML).to.equal(
'<div>text in <span style="">amp</span> doc</div><div>' +
'<span style="">highlight</span>ed text</div>');
});
it('initialize with skipRendering', () => {
const {ampdoc} = env;
const scrollStub = sandbox.stub(
Services.viewportForDoc(ampdoc), 'animateScrollIntoView');
scrollStub.returns(Promise.reject());
const sendMsgStub = sandbox.stub(
Services.viewerForDoc(ampdoc), 'sendMessage');
new HighlightHandler(ampdoc,
{sentences: ['amp', 'highlight'], skipRendering: true});
expect(scrollStub).not.to.be.called;
// For some reason, expect(args).to.deep.equal does not work.
expect(sendMsgStub.callCount).to.equal(1);
expect(sendMsgStub.firstCall.args[0]).to.equal('highlightState');
expect(sendMsgStub.firstCall.args[1]).to.deep.equal(
{state: 'found', scroll: 0});
expect(root.innerHTML).to.equal(
'<div>text in <span>amp</span> doc</div><div><span>highlight</span>' +
'ed text</div>');
});
it('initialize with amp-access', () => {
// Inject <script id="amp-access"> to emulate pages with <amp-access>.
const {document} = env.win;
const script = document.createElement('script');
script.id = 'amp-access';
document.body.appendChild(script);
const {ampdoc} = env;
const scrollStub = sandbox.stub(
Services.viewportForDoc(ampdoc), 'animateScrollIntoView');
scrollStub.returns(Promise.reject());
const sendMsgStub = sandbox.stub(
Services.viewerForDoc(ampdoc), 'sendMessage');
new HighlightHandler(ampdoc, {sentences: ['amp', 'highlight']});
expect(scrollStub).not.to.be.called;
// For some reason, expect(args).to.deep.equal does not work.
expect(sendMsgStub.callCount).to.equal(1);
expect(sendMsgStub.firstCall.args[0]).to.equal('highlightState');
expect(sendMsgStub.firstCall.args[1]).to.deep.equal(
{state: 'has_amp_access'});
expect(root.innerHTML).to.equal(
'<div>text in amp doc</div><div>highlighted text</div>');
});
it('initialize with visibility=prerender', () => {
// If visibility != visible, highlight texts and scroll to the start
// position of the animation. But do not trigger the animation.
const {ampdoc} = env;
const scrollStub = sandbox.stub(
Services.viewportForDoc(ampdoc), 'animateScrollIntoView');
scrollStub.returns(Promise.reject());
const sendMsgStub = sandbox.stub(
Services.viewerForDoc(ampdoc), 'sendMessage');
const setScrollTop = sandbox.stub(
Services.viewportForDoc(ampdoc), 'setScrollTop');
sandbox.stub(Services.viewerForDoc(ampdoc), 'getVisibilityState')
.returns(VisibilityState.PRERENDER);
new HighlightHandler(ampdoc,{sentences: ['amp', 'highlight']});
expect(setScrollTop).to.be.calledOnce;
expect(setScrollTop.firstCall.args.length).to.equal(1);
expect(scrollStub).not.to.be.called;
// For some reason, expect(args).to.deep.equal does not work.
expect(sendMsgStub.callCount).to.equal(1);
expect(sendMsgStub.firstCall.args[1]).to.deep.equal(
{state: 'found', scroll: 0});
expect(root.innerHTML).to.have.string('highlight</span>ed');
});
it('calcTopToCenterHighlightedNodes_ center elements', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
expect(handler.highlightedNodes_).not.to.be.null;
const viewport = Services.viewportForDoc(env.ampdoc);
sandbox.stub(viewport, 'getLayoutRect').returns(
layoutRectLtwh(0, 500, 100, 50));
sandbox.stub(viewport, 'getHeight').returns(300);
sandbox.stub(viewport, 'getPaddingTop').returns(50);
// 525px (The center of the element) - 0.5 * 250px (window height)
// - 50px (padding top) = 350px.
expect(handler.calcTopToCenterHighlightedNodes_()).to.equal(350);
});
it('calcTopToCenterHighlightedNodes_ too tall element', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
expect(handler.highlightedNodes_).not.to.be.null;
const viewport = Services.viewportForDoc(env.ampdoc);
sandbox.stub(viewport, 'getLayoutRect').returns(
layoutRectLtwh(0, 500, 100, 500));
sandbox.stub(viewport, 'getHeight').returns(300);
sandbox.stub(viewport, 'getPaddingTop').returns(50);
// Scroll to the top of the element with PAGE_TOP_MARGIN margin
// because it's too tall.
// 500px (The top of the element) - 50px (padding top)
// - 80px (PAGE_TOP_MARGIN) = 370px.
expect(handler.calcTopToCenterHighlightedNodes_()).to.equal(370);
});
it('mayAdjustTop_', () => {
const handler = new HighlightHandler(env.ampdoc, {sentences: ['amp']});
expect(handler.highlightedNodes_).not.to.be.null;
// Set up an environment where calcTopToCenterHighlightedNodes_
// returns 350.
const viewport = Services.viewportForDoc(env.ampdoc);
sandbox.stub(viewport, 'getLayoutRect').returns(
layoutRectLtwh(0, 500, 100, 50));
sandbox.stub(viewport, 'getHeight').returns(300);
sandbox.stub(viewport, 'getPaddingTop').returns(50);
// The current top is 500.
sandbox.stub(viewport, 'getScrollTop').returns(500);
const setScrollTopStub = sandbox.stub(viewport, 'setScrollTop');
const param = handler.mayAdjustTop_(400);
expect(param).to.deep.equal({'nd': 150, 'od': 100});
expect(setScrollTopStub).to.be.calledOnce;
expect(setScrollTopStub.firstCall.args[0]).to.equal(350);
});
});