From 9b28edb1be431d19e66a8ae6271ac777e263dcf8 Mon Sep 17 00:00:00 2001 From: Mark Holmquist Date: Tue, 7 Jan 2014 11:58:29 -0800 Subject: [PATCH] Refactor animation, plus test Change-Id: I688c573b7fbd136782177f0b690d3aab994c6d10 --- .../ext.multimediaViewer.js | 12 +++++----- tests/qunit/ext.multimediaViewer.test.js | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/resources/ext.multimediaViewer/ext.multimediaViewer.js b/resources/ext.multimediaViewer/ext.multimediaViewer.js index 942e7bd96..81ecac2eb 100755 --- a/resources/ext.multimediaViewer/ext.multimediaViewer.js +++ b/resources/ext.multimediaViewer/ext.multimediaViewer.js @@ -608,9 +608,7 @@ imageEle = new Image(), targetWidth = size; - if ( !viewer.hasAnimatedMetadata ) { - viewer.animateMetadataDiv(); - } + viewer.animateMetadataDivOnce(); imageEle.onload = function () { if ( imageEle.width > targetWidth ) { @@ -639,10 +637,12 @@ comingFromPopstate = false; }; - MMVP.animateMetadataDiv = function () { - $.scrollTo( 40, 400, { onAfter: function() { $.scrollTo( 0, 400 ); } } ); + MMVP.animateMetadataDivOnce = function () { + if ( !this.hasAnimatedMetadata ) { + $.scrollTo( 40, 400, { onAfter: function() { $.scrollTo( 0, 400 ); } } ); - this.hasAnimatedMetadata = true; + this.hasAnimatedMetadata = true; + } }; /** diff --git a/tests/qunit/ext.multimediaViewer.test.js b/tests/qunit/ext.multimediaViewer.test.js index 54b095f3d..f6c0af0ec 100644 --- a/tests/qunit/ext.multimediaViewer.test.js +++ b/tests/qunit/ext.multimediaViewer.test.js @@ -255,4 +255,26 @@ assert.strictEqual( viewer.findNextHighestImageSize( 3000 ), 2880, 'The image bucketing also works on REALLY big screens' ); } ); + QUnit.test( 'Metadata div is only animated once', 4, function ( assert ) { + var viewer = new mw.MultimediaViewer(), + backupAnimation = $.fn.animate, + animationRan = false; + + $.fn.animate = function () { + animationRan = true; + return this; + }; + + viewer.animateMetadataDivOnce(); + assert.strictEqual( viewer.hasAnimatedMetadata, true, 'The first call to animateMetadataDivOnce set hasAnimatedMetadata to true' ); + assert.strictEqual( animationRan, true, 'The first call to animateMetadataDivOnce led to an animation' ); + + animationRan = false; + viewer.animateMetadataDivOnce(); + assert.strictEqual( viewer.hasAnimatedMetadata, true, 'The second call to animateMetadataDivOnce did not change the value of hasAnimatedMetadata' ); + assert.strictEqual( animationRan, false, 'The second call to animateMetadataDivOnce did not lead to an animation' ); + + $.fn.animate = backupAnimation; + } ); + }( mediaWiki, jQuery ) );