From a82728ccdbe92a0d6021d47f549733f03b619d47 Mon Sep 17 00:00:00 2001 From: Gilles Dubuc Date: Fri, 28 Feb 2014 12:50:06 +0100 Subject: [PATCH] Add test for clicking prev/next Change-Id: I02653d97500d5a876f142a518d9b53b0d4746aef Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/145 --- MultimediaViewerHooks.php | 1 + tests/qunit/mmv/mmv.test.js | 48 ++++++++++++++++------- tests/qunit/mmv/ui/mmv.ui.buttons.test.js | 40 +++++++++++++++++++ tests/qunit/mmv/ui/mmv.ui.canvas.test.js | 2 +- 4 files changed, 76 insertions(+), 15 deletions(-) create mode 100644 tests/qunit/mmv/ui/mmv.ui.buttons.test.js diff --git a/MultimediaViewerHooks.php b/MultimediaViewerHooks.php index cf8859154..79e1207ad 100644 --- a/MultimediaViewerHooks.php +++ b/MultimediaViewerHooks.php @@ -139,6 +139,7 @@ class MultimediaViewerHooks { 'tests/qunit/mmv/provider/mmv.provider.UserInfo.test.js', 'tests/qunit/mmv/provider/mmv.provider.Image.test.js', 'tests/qunit/mmv/ui/mmv.ui.test.js', + 'tests/qunit/mmv/ui/mmv.ui.buttons.test.js', 'tests/qunit/mmv/ui/mmv.ui.canvas.test.js', 'tests/qunit/mmv/ui/mmv.ui.categories.test.js', 'tests/qunit/mmv/ui/mmv.ui.description.test.js', diff --git a/tests/qunit/mmv/mmv.test.js b/tests/qunit/mmv/mmv.test.js index 98f1268c9..1b2f0db70 100644 --- a/tests/qunit/mmv/mmv.test.js +++ b/tests/qunit/mmv/mmv.test.js @@ -1,23 +1,31 @@ ( function ( mw, $ ) { + var oldSetupEventHandlers; + QUnit.module( 'mmv', QUnit.newMwEnvironment() ); + // Because we don't want that throwaway instance to listen to events, could interfere with other tests + function newViewer() { + oldSetupEventHandlers = mw.mmv.MultimediaViewer.prototype.setupEventHandlers; + mw.mmv.MultimediaViewer.prototype.setupEventHandlers = $.noop; + return new mw.mmv.MultimediaViewer(); + } + + function cleanNewViewer() { + mw.mmv.MultimediaViewer.prototype.setupEventHandlers = oldSetupEventHandlers; + } + QUnit.test( 'Metadata div is only animated once', 4, function ( assert ) { localStorage.removeItem( 'mmv.hasOpenedMetadata' ); - var viewer, + var viewer = newViewer(), backupAnimation = $.fn.animate, - animationRan = false, - oldSetupEventHandlers = mw.mmv.MultimediaViewer.prototype.setupEventHandlers; + animationRan = false; $.fn.animate = function () { animationRan = true; return this; }; - // Because we don't want that throwaway instance to listen to events, could interfere with other tests - mw.mmv.MultimediaViewer.prototype.setupEventHandlers = $.noop; - viewer = new mw.mmv.MultimediaViewer(); - 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' ); @@ -29,7 +37,7 @@ $.fn.animate = backupAnimation; - mw.mmv.MultimediaViewer.prototype.setupEventHandlers = oldSetupEventHandlers; + cleanNewViewer(); } ); QUnit.test( 'eachPrealoadableLightboxIndex()', 11, function ( assert ) { @@ -155,7 +163,7 @@ QUnit.test( 'Progress', 3, function ( assert ) { var imageDeferred = $.Deferred(), - viewer = new mw.mmv.MultimediaViewer(), + viewer = newViewer(), oldImageGet = mw.mmv.provider.Image.prototype.get, oldImageInfoGet = mw.mmv.provider.ImageInfo.prototype.get, oldThumbnailInfoGet = mw.mmv.provider.ThumbnailInfo.prototype.get, @@ -203,10 +211,12 @@ mw.mmv.provider.Image.prototype.get = oldImageGet; mw.mmv.provider.ImageInfo.prototype.get = oldImageInfoGet; mw.mmv.provider.ThumbnailInfo.prototype.get = oldThumbnailInfoGet; + + cleanNewViewer(); } ); QUnit.test( 'resetBlurredThumbnailStates', 4, function ( assert ) { - var viewer = new mw.mmv.MultimediaViewer(); + var viewer = newViewer(); assert.ok( !viewer.realThumbnailShown, 'Real thumbnail state is correct' ); assert.ok( !viewer.blurredThumbnailShown, 'Placeholder state is correct' ); @@ -218,10 +228,12 @@ assert.ok( !viewer.realThumbnailShown, 'Real thumbnail state is correct' ); assert.ok( !viewer.blurredThumbnailShown, 'Placeholder state is correct' ); + + cleanNewViewer(); } ); QUnit.test( 'Placeholder first, then real thumbnail', 4, function ( assert ) { - var viewer = new mw.mmv.MultimediaViewer(); + var viewer = newViewer(); viewer.setImage = $.noop; viewer.lightbox = { iface : { canvas : { @@ -237,10 +249,12 @@ assert.ok( viewer.realThumbnailShown, 'Real thumbnail state is correct' ); assert.ok( viewer.blurredThumbnailShown, 'Placeholder state is correct' ); + + cleanNewViewer(); } ); QUnit.test( 'Real thumbnail first, then placeholder', 4, function ( assert ) { - var viewer = new mw.mmv.MultimediaViewer(); + var viewer = newViewer(); viewer.setImage = $.noop; viewer.lightbox = { iface : { @@ -256,10 +270,12 @@ assert.ok( viewer.realThumbnailShown, 'Real thumbnail state is correct' ); assert.ok( !viewer.blurredThumbnailShown, 'Placeholder state is correct' ); + + cleanNewViewer(); } ); QUnit.test( 'displayRealThumbnail', 1, function ( assert ) { - var viewer = new mw.mmv.MultimediaViewer(); + var viewer = newViewer(); viewer.setImage = $.noop; viewer.lightbox = { iface : { canvas : { @@ -276,12 +292,14 @@ // Should result in an unblur (image didn't come from cache) viewer.displayRealThumbnail( undefined, undefined, undefined, 1000 ); + + cleanNewViewer(); } ); QUnit.test( 'New image loaded while another one is loading', 1, function ( assert ) { var imageDeferred, ligthboxInfoDeferred, - viewer = new mw.mmv.MultimediaViewer(), + viewer = newViewer(), oldImageGet = mw.mmv.provider.Image.prototype.get, oldImageInfoGet = mw.mmv.provider.ImageInfo.prototype.get, oldThumbnailInfoGet = mw.mmv.provider.ThumbnailInfo.prototype.get, @@ -348,5 +366,7 @@ mw.mmv.provider.Image.prototype.get = oldImageGet; mw.mmv.provider.ImageInfo.prototype.get = oldImageInfoGet; mw.mmv.provider.ThumbnailInfo.prototype.get = oldThumbnailInfoGet; + + cleanNewViewer(); } ); }( mediaWiki, jQuery ) ); diff --git a/tests/qunit/mmv/ui/mmv.ui.buttons.test.js b/tests/qunit/mmv/ui/mmv.ui.buttons.test.js new file mode 100644 index 000000000..e4cf3ed83 --- /dev/null +++ b/tests/qunit/mmv/ui/mmv.ui.buttons.test.js @@ -0,0 +1,40 @@ +/* + * This file is part of the MediaWiki extension MediaViewer. + * + * MediaViewer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * MediaViewer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MediaViewer. If not, see . + */ + +( function( mw, $ ) { + QUnit.module( 'mmv.ui.Buttons', QUnit.newMwEnvironment() ); + + QUnit.test( 'Prev/Next', 2, function( assert ) { + var $qf = $( '#qunit-fixture' ), + buttons = new mw.mmv.ui.Buttons( $qf, $( '
' ), $( '
' ) ), + viewer = mw.mmv.mediaViewer, + oldLoadIndex = viewer.loadIndex, + oldLightbox = viewer.lightbox; + + viewer.lightbox = {}; + + viewer.loadIndex = function () { + assert.ok( true, 'Switched to next/prev image' ); + }; + + buttons.$next.click(); + buttons.$prev.click(); + + viewer.loadIndex = oldLoadIndex; + viewer.lightbox = oldLightbox; + } ); +}( mediaWiki, jQuery ) ); diff --git a/tests/qunit/mmv/ui/mmv.ui.canvas.test.js b/tests/qunit/mmv/ui/mmv.ui.canvas.test.js index dfc410db9..774c66dad 100644 --- a/tests/qunit/mmv/ui/mmv.ui.canvas.test.js +++ b/tests/qunit/mmv/ui/mmv.ui.canvas.test.js @@ -16,7 +16,7 @@ */ ( function( mw, $ ) { - QUnit.module( 'mw.mmv.ui.Canvas', QUnit.newMwEnvironment() ); + QUnit.module( 'mmv.ui.Canvas', QUnit.newMwEnvironment() ); QUnit.test( 'Constructor sanity check', 3, function( assert ) { var $qf = $( '#qunit-fixture' ),