Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amp-video-iframe: use InOb instead of ce.getIntersectionChangeEntry. #31730

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions extensions/amp-video-iframe/0.1/amp-video-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import {getData, listen} from '../../../src/event-helper';
import {installVideoManagerForDoc} from '../../../src/service/video-manager-impl';
import {isLayoutSizeDefined} from '../../../src/layout';
import {measureIntersection} from '../../../src/utils/intersection';
import {once} from '../../../src/utils/function';

/** @private @const */
Expand Down Expand Up @@ -275,6 +276,7 @@ class AmpVideoIframe extends AMP.BaseElement {

/**
* @param {!Event} event
* @return {!Promise|undefined}
* @private
*/
onMessage_(event) {
Expand Down Expand Up @@ -302,8 +304,9 @@ class AmpVideoIframe extends AMP.BaseElement {

if (methodReceived) {
if (methodReceived == 'getIntersection') {
this.postIntersection_(messageId);
return;
return measureIntersection(this.element).then((intersection) => {
this.postIntersection_(messageId, intersection);
});
}
userAssert(false, 'Unknown method `%s`.', methodReceived);
return;
Expand Down Expand Up @@ -361,10 +364,11 @@ class AmpVideoIframe extends AMP.BaseElement {

/**
* @param {number} messageId
* @param {!IntersectionObserverEntry} intersection
* @private
*/
postIntersection_(messageId) {
const {time, intersectionRatio} = this.element.getIntersectionChangeEntry();
postIntersection_(messageId, intersection) {
const {intersectionRatio, time} = intersection;

// Only post ratio > 0 when in autoplay range to prevent internal autoplay
// implementations that differ from ours.
Expand Down
28 changes: 15 additions & 13 deletions extensions/amp-video-iframe/0.1/test/test-amp-video-iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ describes.realWin(
);
}

function stubIntersectionEntry(element, time, intersectionRatio) {
const entry = {time, intersectionRatio};
env.sandbox
./*OK*/ stub(element, 'getIntersectionChangeEntry')
.returns(entry);
return entry;
function stubMeasureIntersection(target, time, intersectionRatio) {
env.win.IntersectionObserver = (callback) => ({
observe() {
Promise.resolve().then(() => {
callback([{target, time, intersectionRatio}]);
});
},
unobserve() {},
disconnect() {},
});
}

describe('#layoutCallback', () => {
Expand Down Expand Up @@ -288,12 +292,10 @@ describes.realWin(

const message = getIntersectionMessage(id);

const expectedResponseMessage = {
id,
args: stubIntersectionEntry(videoIframe, time, intersectionRatio),
};
stubMeasureIntersection(videoIframe, time, intersectionRatio);
const expectedResponseMessage = {id, args: {time, intersectionRatio}};

videoIframe.implementation_.onMessage_(message);
await videoIframe.implementation_.onMessage_(message);

expect(postMessage.withArgs(env.sandbox.match(expectedResponseMessage)))
.to.have.been.calledOnce;
Expand All @@ -311,7 +313,7 @@ describes.realWin(

const postMessage = stubPostMessage(videoIframe);

stubIntersectionEntry(videoIframe, time, intersectionRatio);
stubMeasureIntersection(videoIframe, time, intersectionRatio);

acceptMockedMessages(videoIframe);

Expand All @@ -325,7 +327,7 @@ describes.realWin(
},
};

videoIframe.implementation_.onMessage_(message);
await videoIframe.implementation_.onMessage_(message);

expect(postMessage.withArgs(env.sandbox.match(expectedResponseMessage)))
.to.have.been.calledOnce;
Expand Down