Skip to content

Commit e7def6b

Browse files
authored
Expose the advancementMode with the selectDocument messaging. (ampproject#27444)
* Expose the advancementMode with the selectDocument API. * Add unit test for time based advancement.
1 parent 04ef79c commit e7def6b

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed

extensions/amp-story/1.0/amp-story.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,13 @@ export class AmpStory extends AMP.BaseElement {
13131313
*/
13141314
onNoNextPage_() {
13151315
if (this.viewer_.hasCapability('swipe') && this.viewerMessagingHandler_) {
1316-
this.viewerMessagingHandler_.send('selectDocument', dict({'next': true}));
1316+
const advancementMode = this.storeService_.get(
1317+
StateProperty.ADVANCEMENT_MODE
1318+
);
1319+
this.viewerMessagingHandler_.send(
1320+
'selectDocument',
1321+
dict({'next': true, 'advancementMode': advancementMode})
1322+
);
13171323
return;
13181324
}
13191325

@@ -1342,9 +1348,12 @@ export class AmpStory extends AMP.BaseElement {
13421348
*/
13431349
onNoPreviousPage_() {
13441350
if (this.viewer_.hasCapability('swipe') && this.viewerMessagingHandler_) {
1351+
const advancementMode = this.storeService_.get(
1352+
StateProperty.ADVANCEMENT_MODE
1353+
);
13451354
this.viewerMessagingHandler_.send(
13461355
'selectDocument',
1347-
dict({'previous': true})
1356+
dict({'previous': true, 'advancementMode': advancementMode})
13481357
);
13491358
return;
13501359
}
@@ -1359,18 +1368,18 @@ export class AmpStory extends AMP.BaseElement {
13591368
* @private
13601369
*/
13611370
performTapNavigation_(direction) {
1371+
this.storeService_.dispatch(
1372+
Action.SET_ADVANCEMENT_MODE,
1373+
AdvancementMode.MANUAL_ADVANCE
1374+
);
1375+
13621376
if (
13631377
this.storeService_.get(StateProperty.UI_STATE) === UIType.DESKTOP_PANELS
13641378
) {
13651379
this.next_();
13661380
return;
13671381
}
13681382

1369-
this.storeService_.dispatch(
1370-
Action.SET_ADVANCEMENT_MODE,
1371-
AdvancementMode.MANUAL_ADVANCE
1372-
);
1373-
13741383
if (direction === TapNavigationDirection.NEXT) {
13751384
this.next_();
13761385
} else if (direction === TapNavigationDirection.PREVIOUS) {

extensions/amp-story/1.0/page-advancement.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,11 @@ class TimeBasedAdvancement extends AdvancementConfig {
756756

757757
/** @override */
758758
onAdvance() {
759-
super.onAdvance();
760759
this.storeService_.dispatch(
761760
Action.SET_ADVANCEMENT_MODE,
762761
AdvancementMode.AUTO_ADVANCE_TIME
763762
);
763+
super.onAdvance();
764764
}
765765

766766
/**
@@ -1031,11 +1031,11 @@ class MediaBasedAdvancement extends AdvancementConfig {
10311031

10321032
/** @override */
10331033
onAdvance() {
1034-
super.onAdvance();
10351034
this.storeService_.dispatch(
10361035
Action.SET_ADVANCEMENT_MODE,
10371036
AdvancementMode.AUTO_ADVANCE_MEDIA
10381037
);
1038+
super.onAdvance();
10391039
}
10401040

10411041
/**

extensions/amp-story/1.0/test/test-amp-story.js

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
UIType,
2424
} from '../amp-story-store-service';
2525
import {ActionTrust} from '../../../../src/action-constants';
26+
import {AdvancementMode} from '../story-analytics';
2627
import {AmpStory} from '../amp-story';
2728
import {AmpStoryBookend} from '../bookend/amp-story-bookend';
2829
import {AmpStoryConsent} from '../amp-story-consent';
@@ -51,26 +52,30 @@ describes.realWin(
5152
},
5253
},
5354
env => {
54-
let win, ampdoc;
55+
let ampdoc;
5556
let element;
5657
let hasSwipeCapability = false;
5758
let isEmbedded = false;
5859
let story;
5960
let replaceStateStub;
61+
let win;
6062

6163
/**
6264
* @param {number} count
63-
* @param {Array<string>=} opt_ids
65+
* @param {Array<string>=} ids
6466
* @return {!Array<!Element>}
6567
*/
66-
async function createStoryWithPages(count, opt_ids) {
68+
async function createStoryWithPages(count, ids = [], autoAdvance = false) {
6769
element = win.document.createElement('amp-story');
6870

6971
Array(count)
7072
.fill(undefined)
7173
.map((unused, i) => {
7274
const page = win.document.createElement('amp-story-page');
73-
page.id = opt_ids && opt_ids[i] ? opt_ids[i] : `-page-${i}`;
75+
if (autoAdvance) {
76+
page.setAttribute('auto-advance-after', '2s');
77+
}
78+
page.id = ids && ids[i] ? ids[i] : `-page-${i}`;
7479
element.appendChild(page);
7580
return page;
7681
});
@@ -1149,9 +1154,39 @@ describes.realWin(
11491154
story.activePage_.element.dispatchEvent(clickEvent);
11501155
await waitFor(() => {
11511156
if (sendMessageStub.calledOnce) {
1152-
expect(
1153-
sendMessageStub
1154-
).to.be.calledWithExactly('selectDocument', {next: true});
1157+
expect(sendMessageStub).to.be.calledWithExactly(
1158+
'selectDocument',
1159+
{
1160+
next: true,
1161+
advancementMode: AdvancementMode.MANUAL_ADVANCE,
1162+
}
1163+
);
1164+
return true;
1165+
}
1166+
return false;
1167+
}, 'sendMessageStub should be called');
1168+
});
1169+
1170+
it('should send a message when auto-advancing on last page in viewer', async () => {
1171+
await createStoryWithPages(1, ['cover'], true /** autoAdvance */);
1172+
const sendMessageStub = env.sandbox.stub(
1173+
story.viewerMessagingHandler_,
1174+
'send'
1175+
);
1176+
1177+
await story.layoutCallback();
1178+
1179+
story.activePage_.advancement_.onAdvance();
1180+
1181+
await waitFor(() => {
1182+
if (sendMessageStub.calledOnce) {
1183+
expect(sendMessageStub).to.be.calledWithExactly(
1184+
'selectDocument',
1185+
{
1186+
next: true,
1187+
advancementMode: AdvancementMode.AUTO_ADVANCE_TIME,
1188+
}
1189+
);
11551190
return true;
11561191
}
11571192
return false;
@@ -1202,9 +1237,13 @@ describes.realWin(
12021237
story.activePage_.element.dispatchEvent(clickEvent);
12031238
await waitFor(() => {
12041239
if (sendMessageStub.calledOnce) {
1205-
expect(
1206-
sendMessageStub
1207-
).to.be.calledWithExactly('selectDocument', {previous: true});
1240+
expect(sendMessageStub).to.be.calledWithExactly(
1241+
'selectDocument',
1242+
{
1243+
previous: true,
1244+
advancementMode: AdvancementMode.MANUAL_ADVANCE,
1245+
}
1246+
);
12081247
return true;
12091248
}
12101249
return false;

0 commit comments

Comments
 (0)