-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtest-amp-story-system-layer.js
More file actions
511 lines (424 loc) · 18.3 KB
/
Copy pathtest-amp-story-system-layer.js
File metadata and controls
511 lines (424 loc) · 18.3 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import {Services} from '#service';
import {LocalizationService} from '#service/localization';
import {waitFor} from '#testing/helpers/service';
import {registerServiceBuilder} from '../../../../src/service-helpers';
import LocalizedStringsEn from '../_locales/en.json' assert {type: 'json'}; // lgtm[js/syntax-error]
import LocalizedStringsEs from '../_locales/es.json' assert {type: 'json'}; // lgtm[js/syntax-error]
import {getLocalizationService} from '../amp-story-localization-service';
import {
Action,
AmpStoryStoreService,
StateProperty,
} from '../amp-story-store-service';
import {SystemLayer} from '../amp-story-system-layer';
import {ProgressBar} from '../progress-bar';
const NOOP = () => {};
describes.fakeWin('amp-story system layer', {amp: true}, (env) => {
let win;
let storeService;
let systemLayer;
let progressBarStub;
let progressBarRoot;
beforeEach(() => {
win = env.win;
const localizationService = new LocalizationService(win.document.body);
env.sandbox
.stub(Services, 'localizationForDoc')
.returns(localizationService);
storeService = new AmpStoryStoreService(win);
registerServiceBuilder(win, 'story-store', function () {
return storeService;
});
progressBarRoot = win.document.createElement('div');
progressBarStub = {
build: env.sandbox.stub().returns(progressBarRoot),
getRoot: env.sandbox.stub().returns(progressBarRoot),
updateProgress: env.sandbox.spy(),
};
env.sandbox.stub(ProgressBar, 'create').returns(progressBarStub);
env.sandbox.stub(Services, 'vsyncFor').returns({
mutate: (fn) => fn(),
mutatePromise: (fn) => fn(),
});
systemLayer = new SystemLayer(win, win.document.body);
});
it('should build UI', () => {
const initializeListeners = env.sandbox
.stub(systemLayer, 'initializeListeners_')
.callsFake(NOOP);
const root = systemLayer.build();
expect(root).to.not.be.null;
expect(initializeListeners).to.have.been.called;
});
// TODO(alanorozco, #12476): Make this test work with sinon 4.0.
it.skip('should attach event handlers', () => {
const rootMock = {addEventListener: env.sandbox.spy()};
env.sandbox.stub(systemLayer, 'root_').callsFake(rootMock);
env.sandbox.stub(systemLayer, 'win_').callsFake(rootMock);
systemLayer.initializeListeners_();
expect(rootMock.addEventListener).to.have.been.calledWith('click');
});
it('should set an attribute to toggle the UI when an ad is shown', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_AD, true);
expect(systemLayer.getShadowRoot()).to.have.attribute('ad-showing');
});
it('should show that sound off on a page when muted', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, true);
storeService.dispatch(Action.TOGGLE_MUTED, true);
expect(systemLayer.getShadowRoot()).to.have.attribute('muted');
});
it('should show that this page has no sound when unmuted', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, false);
storeService.dispatch(Action.TOGGLE_MUTED, false);
expect(systemLayer.getShadowRoot()).to.not.have.attribute('muted');
expect(systemLayer.getShadowRoot()).to.not.have.attribute(
'i-amphtml-current-page-has-audio'
);
});
it('should show that the sound is on when unmuted', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, true);
storeService.dispatch(Action.TOGGLE_MUTED, false);
expect(systemLayer.getShadowRoot()).to.not.have.attribute('muted');
expect(systemLayer.getShadowRoot()).to.have.attribute(
'i-amphtml-current-page-has-audio'
);
});
it('should hide system layer on SYSTEM_UI_IS_VISIBLE_STATE change', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_SYSTEM_UI_IS_VISIBLE, false);
expect(systemLayer.getShadowRoot()).to.have.class('i-amphtml-story-hidden');
});
it('should link the share button to the canonical URL', () => {
systemLayer.build();
const shareButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-share-control');
expect(shareButton).to.not.be.null;
expect(shareButton.tagName).to.equal('BUTTON');
// Default "canonical"
expect(shareButton.href).to.equal('http://localhost:9876/context.html');
});
it('should show paused button if story has element with playback', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
expect(systemLayer.getShadowRoot()).to.have.class(
'i-amphtml-story-has-playback-ui'
);
});
it('should enable paused button if page has element with playback', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
expect(
systemLayer
.getShadowRoot()
.querySelector('button.i-amphtml-story-pause-control')
).to.not.have.attribute('disabled');
expect(
systemLayer
.getShadowRoot()
.querySelector('button.i-amphtml-story-play-control')
).to.not.have.attribute('disabled');
});
it('should disable paused button if page does not has elements with playback', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, false);
expect(
systemLayer
.getShadowRoot()
.querySelector('button.i-amphtml-story-pause-control')
).to.have.attribute('disabled');
expect(
systemLayer
.getShadowRoot()
.querySelector('button.i-amphtml-story-play-control')
).to.have.attribute('disabled');
});
it('setting paused state to true should add the paused flag', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, true);
expect(systemLayer.getShadowRoot()).to.have.attribute('paused');
});
it('setting paused state to false should not add the paused flag', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, false);
expect(systemLayer.getShadowRoot()).to.not.have.attribute('paused');
});
it('click on the play button should change state to false', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, true);
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-play-control')
.click();
expect(storeService.get(StateProperty.PAUSED_STATE)).to.be.false;
expect(systemLayer.getShadowRoot()).to.not.have.attribute('paused');
});
it('click on the captions button should change state to false', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_CAPTIONS, true);
storeService.dispatch(Action.TOGGLE_CAPTIONS, true);
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-captions-control')
.click();
expect(storeService.get(StateProperty.CAPTIONS_STATE)).to.be.false;
expect(systemLayer.getShadowRoot()).to.not.have.attribute('captions-on');
});
it('click on the no captions button should change state to true', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_CAPTIONS, true);
storeService.dispatch(Action.TOGGLE_CAPTIONS, true);
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-nocaptions-control')
.click();
expect(storeService.get(StateProperty.CAPTIONS_STATE)).to.be.true;
expect(systemLayer.getShadowRoot()).to.have.attribute('captions-on');
});
it('click on the pause button should change state to true', () => {
systemLayer.build();
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, false);
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-pause-control')
.click();
expect(storeService.get(StateProperty.PAUSED_STATE)).to.be.true;
expect(systemLayer.getShadowRoot()).to.have.attribute('paused');
});
describe('paired button toggle focus restoration', () => {
/**
* Stubs the system layer's root so activeElement returns the given element.
* @param {?Element} activeElement
*/
function stubActiveElement(activeElement) {
env.sandbox
.stub(systemLayer.systemLayerEl_, 'getRootNode')
.returns({activeElement});
}
beforeEach(() => {
systemLayer.build();
});
it('should move focus to the nocaptions button when captions are turned off', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_CAPTIONS, true);
storeService.dispatch(Action.TOGGLE_CAPTIONS, true);
const captionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-captions-control');
const nocaptionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-nocaptions-control');
const focusSpy = env.sandbox.spy(nocaptionsButton, 'focus');
stubActiveElement(captionsButton);
storeService.dispatch(Action.TOGGLE_CAPTIONS, false);
expect(focusSpy).to.have.been.calledOnce;
});
it('should move focus to the captions button when captions are turned on', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_CAPTIONS, true);
storeService.dispatch(Action.TOGGLE_CAPTIONS, false);
const captionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-captions-control');
const nocaptionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-nocaptions-control');
const focusSpy = env.sandbox.spy(captionsButton, 'focus');
stubActiveElement(nocaptionsButton);
storeService.dispatch(Action.TOGGLE_CAPTIONS, true);
expect(focusSpy).to.have.been.calledOnce;
});
it('should not move focus on caption state change if no paired button is focused', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_CAPTIONS, true);
storeService.dispatch(Action.TOGGLE_CAPTIONS, true);
const captionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-captions-control');
const nocaptionsButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-nocaptions-control');
const captionsFocusSpy = env.sandbox.spy(captionsButton, 'focus');
const nocaptionsFocusSpy = env.sandbox.spy(nocaptionsButton, 'focus');
stubActiveElement(win.document.body);
storeService.dispatch(Action.TOGGLE_CAPTIONS, false);
expect(captionsFocusSpy).to.not.have.been.called;
expect(nocaptionsFocusSpy).to.not.have.been.called;
});
it('should move focus to the unmute button when audio is muted', () => {
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, true);
storeService.dispatch(Action.TOGGLE_MUTED, false);
const muteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-mute-audio-control');
const unmuteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-unmute-audio-control');
const focusSpy = env.sandbox.spy(unmuteButton, 'focus');
stubActiveElement(muteButton);
storeService.dispatch(Action.TOGGLE_MUTED, true);
expect(focusSpy).to.have.been.calledOnce;
});
it('should move focus to the mute button when audio is unmuted', () => {
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, true);
storeService.dispatch(Action.TOGGLE_MUTED, true);
const muteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-mute-audio-control');
const unmuteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-unmute-audio-control');
const focusSpy = env.sandbox.spy(muteButton, 'focus');
stubActiveElement(unmuteButton);
storeService.dispatch(Action.TOGGLE_MUTED, false);
expect(focusSpy).to.have.been.calledOnce;
});
it('should not move focus on muted state change if no paired button is focused', () => {
storeService.dispatch(Action.TOGGLE_PAGE_HAS_AUDIO, true);
storeService.dispatch(Action.TOGGLE_MUTED, false);
const muteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-mute-audio-control');
const unmuteButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-unmute-audio-control');
const muteFocusSpy = env.sandbox.spy(muteButton, 'focus');
const unmuteFocusSpy = env.sandbox.spy(unmuteButton, 'focus');
stubActiveElement(win.document.body);
storeService.dispatch(Action.TOGGLE_MUTED, true);
expect(muteFocusSpy).to.not.have.been.called;
expect(unmuteFocusSpy).to.not.have.been.called;
});
it('should move focus to the play button when the story is paused', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, false);
const pauseButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-pause-control');
const playButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-play-control');
const focusSpy = env.sandbox.spy(playButton, 'focus');
stubActiveElement(pauseButton);
storeService.dispatch(Action.TOGGLE_PAUSED, true);
expect(focusSpy).to.have.been.calledOnce;
});
it('should move focus to the pause button when the story is resumed', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, true);
const pauseButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-pause-control');
const playButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-play-control');
const focusSpy = env.sandbox.spy(pauseButton, 'focus');
stubActiveElement(playButton);
storeService.dispatch(Action.TOGGLE_PAUSED, false);
expect(focusSpy).to.have.been.calledOnce;
});
it('should not move focus on paused state change if no paired button is focused', () => {
storeService.dispatch(Action.TOGGLE_STORY_HAS_PLAYBACK_UI, true);
storeService.dispatch(Action.TOGGLE_PAGE_HAS_ELEMENT_WITH_PLAYBACK, true);
storeService.dispatch(Action.TOGGLE_PAUSED, false);
const pauseButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-pause-control');
const playButton = systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-play-control');
const pauseFocusSpy = env.sandbox.spy(pauseButton, 'focus');
const playFocusSpy = env.sandbox.spy(playButton, 'focus');
stubActiveElement(win.document.body);
storeService.dispatch(Action.TOGGLE_PAUSED, true);
expect(pauseFocusSpy).to.not.have.been.called;
expect(playFocusSpy).to.not.have.been.called;
});
});
describe('localization', () => {
it('should load the localized aria-labels for buttons if strings are available', async () => {
getLocalizationService(win.document.body).registerLocalizedStringBundles({
'en': LocalizedStringsEn,
});
systemLayer.build();
await waitFor(
() =>
systemLayer.getShadowRoot().querySelectorAll('[aria-label]').length
);
expect(
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-info-control')
.getAttribute('aria-label')
).to.equal('Story information');
});
it('should load the localized aria-labels for buttons if strings are available after building', async () => {
systemLayer.build();
getLocalizationService(win.document.body).registerLocalizedStringBundles({
'en': LocalizedStringsEn,
});
await waitFor(
() =>
systemLayer.getShadowRoot().querySelectorAll('[aria-label]').length
);
expect(
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-info-control')
.getAttribute('aria-label')
).to.equal('Story information');
});
it('should load the localized aria-labels for the correct language', async () => {
win.document.body.setAttribute('lang', 'es');
getLocalizationService(win.document.body).registerLocalizedStringBundles({
'default': LocalizedStringsEn,
'es': LocalizedStringsEs,
});
systemLayer.build();
await waitFor(
() =>
systemLayer.getShadowRoot().querySelectorAll('[aria-label]').length
);
expect(
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-info-control')
.getAttribute('aria-label')
).to.equal('Información de la historia');
});
it('should load the localized text content', async () => {
getLocalizationService(win.document.body).registerLocalizedStringBundles({
'en': LocalizedStringsEn,
});
systemLayer.build();
await waitFor(() =>
systemLayer
.getShadowRoot()
.querySelectorAll('.i-amphtml-story-has-new-page-text')
);
expect(
systemLayer
.getShadowRoot()
.querySelector('.i-amphtml-story-has-new-page-text').textContent
).to.equal('Updated');
});
});
});