Skip to content

Commit

Permalink
Suppress errors from autoplay detection play call (ampproject#6083)
Browse files Browse the repository at this point in the history
  • Loading branch information
aghassemi authored Nov 8, 2016
1 parent d55f5d3 commit 6c27a1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/service/video-manager-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,17 @@ export function supportsAutoplay(win, isLiteViewer) {
opacity: '0',
});

detectionElement.play();
try {
const playPromise = detectionElement.play();
if (playPromise && playPromise.catch) {
playPromise.catch(() => {
// Suppress any errors, useless to report as they are expected.
});
}
} catch (e) {
// Suppress any errors, useless to report as they are expected.
}

const supportsAutoplay = !detectionElement.paused;
return supportsAutoplayCache_ = Promise.resolve(supportsAutoplay);
}
Expand Down
33 changes: 29 additions & 4 deletions test/functional/test-video-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Supports Autoplay', () => {

let createElementSpy;
let setAttributeSpy;
let playSpy;
let playStub;

it('should create an invisible test video element', () => {
return supportsAutoplay(win, isLite).then(() => {
Expand Down Expand Up @@ -75,7 +75,7 @@ describe('Supports Autoplay', () => {
video.paused = true;
return supportsAutoplay(win, isLite).then(supportsAutoplay => {
expect(supportsAutoplay).to.be.false;
expect(playSpy.called).to.be.true;
expect(playStub.called).to.be.true;
expect(createElementSpy.called).to.be.true;
});
});
Expand All @@ -84,7 +84,32 @@ describe('Supports Autoplay', () => {
video.paused = false;
return supportsAutoplay(win, isLite).then(supportsAutoplay => {
expect(supportsAutoplay).to.be.true;
expect(playSpy.called).to.be.true;
expect(playStub.called).to.be.true;
expect(createElementSpy.called).to.be.true;
});
});

it('should suppress errors if detection play call throws', () => {
playStub.throws();
video.paused = true;
expect(supportsAutoplay(win, isLite)).not.to.throw;
return supportsAutoplay(win, isLite).then(supportsAutoplay => {
expect(supportsAutoplay).to.be.false;
expect(playStub.called).to.be.true;
expect(createElementSpy.called).to.be.true;
});
});

it('should suppress errors if detection play call rejects a promise', () => {
const p = Promise.reject('play() can only be initiated by a user gesture.');
const promiseCatchSpy = sandbox.spy(p, 'catch');
playStub.returns(p);
video.paused = true;
expect(supportsAutoplay(win, isLite)).not.to.throw;
return supportsAutoplay(win, isLite).then(supportsAutoplay => {
expect(promiseCatchSpy.called).to.be.true;
expect(supportsAutoplay).to.be.false;
expect(playStub.called).to.be.true;
expect(createElementSpy.called).to.be.true;
});
});
Expand Down Expand Up @@ -142,7 +167,7 @@ describe('Supports Autoplay', () => {

createElementSpy = sandbox.spy(doc, 'createElement');
setAttributeSpy = sandbox.spy(video, 'setAttribute');
playSpy = sandbox.spy(video, 'play');
playStub = sandbox.stub(video, 'play');
});

afterEach(() => {
Expand Down

0 comments on commit 6c27a1f

Please sign in to comment.