Skip to content

Commit

Permalink
Expose the advancementMode with the selectDocument messaging. (#27444)
Browse files Browse the repository at this point in the history
* Expose the advancementMode with the selectDocument API.

* Add unit test for time based advancement.
  • Loading branch information
gmajoulet committed Mar 30, 2020
1 parent 04ef79c commit e7def6b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 19 deletions.
23 changes: 16 additions & 7 deletions extensions/amp-story/1.0/amp-story.js
Expand Up @@ -1313,7 +1313,13 @@ export class AmpStory extends AMP.BaseElement {
*/
onNoNextPage_() {
if (this.viewer_.hasCapability('swipe') && this.viewerMessagingHandler_) {
this.viewerMessagingHandler_.send('selectDocument', dict({'next': true}));
const advancementMode = this.storeService_.get(
StateProperty.ADVANCEMENT_MODE
);
this.viewerMessagingHandler_.send(
'selectDocument',
dict({'next': true, 'advancementMode': advancementMode})
);
return;
}

Expand Down Expand Up @@ -1342,9 +1348,12 @@ export class AmpStory extends AMP.BaseElement {
*/
onNoPreviousPage_() {
if (this.viewer_.hasCapability('swipe') && this.viewerMessagingHandler_) {
const advancementMode = this.storeService_.get(
StateProperty.ADVANCEMENT_MODE
);
this.viewerMessagingHandler_.send(
'selectDocument',
dict({'previous': true})
dict({'previous': true, 'advancementMode': advancementMode})
);
return;
}
Expand All @@ -1359,18 +1368,18 @@ export class AmpStory extends AMP.BaseElement {
* @private
*/
performTapNavigation_(direction) {
this.storeService_.dispatch(
Action.SET_ADVANCEMENT_MODE,
AdvancementMode.MANUAL_ADVANCE
);

if (
this.storeService_.get(StateProperty.UI_STATE) === UIType.DESKTOP_PANELS
) {
this.next_();
return;
}

this.storeService_.dispatch(
Action.SET_ADVANCEMENT_MODE,
AdvancementMode.MANUAL_ADVANCE
);

if (direction === TapNavigationDirection.NEXT) {
this.next_();
} else if (direction === TapNavigationDirection.PREVIOUS) {
Expand Down
4 changes: 2 additions & 2 deletions extensions/amp-story/1.0/page-advancement.js
Expand Up @@ -756,11 +756,11 @@ class TimeBasedAdvancement extends AdvancementConfig {

/** @override */
onAdvance() {
super.onAdvance();
this.storeService_.dispatch(
Action.SET_ADVANCEMENT_MODE,
AdvancementMode.AUTO_ADVANCE_TIME
);
super.onAdvance();
}

/**
Expand Down Expand Up @@ -1031,11 +1031,11 @@ class MediaBasedAdvancement extends AdvancementConfig {

/** @override */
onAdvance() {
super.onAdvance();
this.storeService_.dispatch(
Action.SET_ADVANCEMENT_MODE,
AdvancementMode.AUTO_ADVANCE_MEDIA
);
super.onAdvance();
}

/**
Expand Down
59 changes: 49 additions & 10 deletions extensions/amp-story/1.0/test/test-amp-story.js
Expand Up @@ -23,6 +23,7 @@ import {
UIType,
} from '../amp-story-store-service';
import {ActionTrust} from '../../../../src/action-constants';
import {AdvancementMode} from '../story-analytics';
import {AmpStory} from '../amp-story';
import {AmpStoryBookend} from '../bookend/amp-story-bookend';
import {AmpStoryConsent} from '../amp-story-consent';
Expand Down Expand Up @@ -51,26 +52,30 @@ describes.realWin(
},
},
env => {
let win, ampdoc;
let ampdoc;
let element;
let hasSwipeCapability = false;
let isEmbedded = false;
let story;
let replaceStateStub;
let win;

/**
* @param {number} count
* @param {Array<string>=} opt_ids
* @param {Array<string>=} ids
* @return {!Array<!Element>}
*/
async function createStoryWithPages(count, opt_ids) {
async function createStoryWithPages(count, ids = [], autoAdvance = false) {
element = win.document.createElement('amp-story');

Array(count)
.fill(undefined)
.map((unused, i) => {
const page = win.document.createElement('amp-story-page');
page.id = opt_ids && opt_ids[i] ? opt_ids[i] : `-page-${i}`;
if (autoAdvance) {
page.setAttribute('auto-advance-after', '2s');
}
page.id = ids && ids[i] ? ids[i] : `-page-${i}`;
element.appendChild(page);
return page;
});
Expand Down Expand Up @@ -1149,9 +1154,39 @@ describes.realWin(
story.activePage_.element.dispatchEvent(clickEvent);
await waitFor(() => {
if (sendMessageStub.calledOnce) {
expect(
sendMessageStub
).to.be.calledWithExactly('selectDocument', {next: true});
expect(sendMessageStub).to.be.calledWithExactly(
'selectDocument',
{
next: true,
advancementMode: AdvancementMode.MANUAL_ADVANCE,
}
);
return true;
}
return false;
}, 'sendMessageStub should be called');
});

it('should send a message when auto-advancing on last page in viewer', async () => {
await createStoryWithPages(1, ['cover'], true /** autoAdvance */);
const sendMessageStub = env.sandbox.stub(
story.viewerMessagingHandler_,
'send'
);

await story.layoutCallback();

story.activePage_.advancement_.onAdvance();

await waitFor(() => {
if (sendMessageStub.calledOnce) {
expect(sendMessageStub).to.be.calledWithExactly(
'selectDocument',
{
next: true,
advancementMode: AdvancementMode.AUTO_ADVANCE_TIME,
}
);
return true;
}
return false;
Expand Down Expand Up @@ -1202,9 +1237,13 @@ describes.realWin(
story.activePage_.element.dispatchEvent(clickEvent);
await waitFor(() => {
if (sendMessageStub.calledOnce) {
expect(
sendMessageStub
).to.be.calledWithExactly('selectDocument', {previous: true});
expect(sendMessageStub).to.be.calledWithExactly(
'selectDocument',
{
previous: true,
advancementMode: AdvancementMode.MANUAL_ADVANCE,
}
);
return true;
}
return false;
Expand Down

0 comments on commit e7def6b

Please sign in to comment.