2013-12-11 02:08:07 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-03-29 01:38:22 +00:00
|
|
|
var oldScrollTo;
|
|
|
|
|
|
|
|
function stubScrollTo() {
|
|
|
|
oldScrollTo = $.scrollTo;
|
2015-01-23 12:48:27 +00:00
|
|
|
$.scrollTo = function () { return { scrollTop: $.noop, on: $.noop, off: $.noop }; };
|
2014-03-29 01:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function restoreScrollTo() {
|
|
|
|
$.scrollTo = oldScrollTo;
|
|
|
|
}
|
|
|
|
|
2015-06-24 18:54:22 +00:00
|
|
|
QUnit.module( 'mmv.lightboxInterface', QUnit.newMwEnvironment( {
|
|
|
|
setup: function () {
|
|
|
|
// animation would keep running, conflict with other tests
|
|
|
|
this.sandbox.stub( $.fn, 'animate' ).returnsThis();
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
QUnit.test( 'Sanity test, object creation and ui construction', 20, function ( assert ) {
|
2014-03-04 12:52:08 +00:00
|
|
|
var lightbox = new mw.mmv.LightboxInterface();
|
2013-12-11 02:08:07 +00:00
|
|
|
|
2014-03-29 01:38:22 +00:00
|
|
|
stubScrollTo();
|
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
function checkIfUIAreasAttachedToDocument( inDocument ) {
|
2014-03-27 06:05:47 +00:00
|
|
|
var msg = ( inDocument === 1 ? ' ' : ' not ' ) + 'attached.';
|
|
|
|
assert.strictEqual( $( '.mw-mmv-wrapper' ).length, inDocument, 'Wrapper area' + msg );
|
|
|
|
assert.strictEqual( $( '.mw-mmv-main' ).length, inDocument, 'Main area' + msg );
|
|
|
|
assert.strictEqual( $( '.mw-mmv-title' ).length, inDocument, 'Title area' + msg );
|
|
|
|
assert.strictEqual( $( '.mw-mmv-credit' ).length, inDocument, 'Author/source area' + msg );
|
|
|
|
assert.strictEqual( $( '.mw-mmv-image-desc' ).length, inDocument, 'Description area' + msg );
|
|
|
|
assert.strictEqual( $( '.mw-mmv-image-links' ).length, inDocument, 'Links area' + msg );
|
2013-12-11 02:08:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UI areas not attached to the document yet.
|
2015-01-23 12:48:27 +00:00
|
|
|
checkIfUIAreasAttachedToDocument( 0 );
|
2013-12-11 02:08:07 +00:00
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
// UI areas should now be attached to the document.
|
2015-01-23 12:48:27 +00:00
|
|
|
checkIfUIAreasAttachedToDocument( 1 );
|
2013-12-11 02:08:07 +00:00
|
|
|
|
2014-01-14 15:42:52 +00:00
|
|
|
// Check that the close button on the lightbox still follow the spec (being visible right away)
|
2014-03-31 21:33:12 +00:00
|
|
|
assert.strictEqual( $( '#qunit-fixture .mw-mmv-close' ).length, 1, 'There should be a close button' );
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( $( '#qunit-fixture .mw-mmv-close' ).is( ':visible' ), 'The close button should be visible' );
|
2014-01-14 15:42:52 +00:00
|
|
|
|
2013-12-11 02:08:07 +00:00
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
|
|
|
|
|
|
|
// UI areas not attached to the document anymore.
|
2015-01-23 12:48:27 +00:00
|
|
|
checkIfUIAreasAttachedToDocument( 0 );
|
2014-03-29 01:38:22 +00:00
|
|
|
|
|
|
|
restoreScrollTo();
|
2013-12-11 02:08:07 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Handler registration and clearance work OK', 2, function ( assert ) {
|
2014-03-04 12:52:08 +00:00
|
|
|
var lightbox = new mw.mmv.LightboxInterface(),
|
2013-12-11 02:08:07 +00:00
|
|
|
handlerCalls = 0;
|
|
|
|
|
|
|
|
function handleEvent() {
|
|
|
|
handlerCalls++;
|
|
|
|
}
|
|
|
|
|
|
|
|
lightbox.handleEvent( 'test', handleEvent );
|
|
|
|
$( document ).trigger( 'test' );
|
|
|
|
assert.strictEqual( handlerCalls, 1, 'The handler was called when we triggered the event.' );
|
|
|
|
lightbox.clearEvents();
|
|
|
|
$( document ).trigger( 'test' );
|
|
|
|
assert.strictEqual( handlerCalls, 1, 'The handler was not called after calling lightbox.clearEvents().' );
|
|
|
|
} );
|
|
|
|
|
2014-02-25 01:54:05 +00:00
|
|
|
QUnit.test( 'Fullscreen mode', 8, function ( assert ) {
|
2014-03-04 12:52:08 +00:00
|
|
|
var lightbox = new mw.mmv.LightboxInterface(),
|
2014-02-25 01:54:05 +00:00
|
|
|
oldFnEnterFullscreen = $.fn.enterFullscreen,
|
|
|
|
oldFnExitFullscreen = $.fn.exitFullscreen,
|
|
|
|
oldSupportFullscreen = $.support.fullscreen;
|
|
|
|
|
|
|
|
// Since we don't want these tests to really open fullscreen
|
|
|
|
// which is subject to user security confirmation,
|
|
|
|
// we use a mock that pretends regular jquery.fullscreen behavior happened
|
2014-02-25 02:28:49 +00:00
|
|
|
$.fn.enterFullscreen = mw.mmv.testHelpers.enterFullscreenMock;
|
|
|
|
$.fn.exitFullscreen = mw.mmv.testHelpers.exitFullscreenMock;
|
2014-02-25 01:54:05 +00:00
|
|
|
|
2014-03-29 01:38:22 +00:00
|
|
|
stubScrollTo();
|
|
|
|
|
|
|
|
lightbox.buttons.fadeOut = $.noop;
|
|
|
|
|
2014-02-25 01:54:05 +00:00
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
$.support.fullscreen = false;
|
2014-04-04 09:32:30 +00:00
|
|
|
lightbox.setupCanvasButtons();
|
2014-02-25 01:54:05 +00:00
|
|
|
|
2014-04-04 09:32:30 +00:00
|
|
|
assert.strictEqual( lightbox.$fullscreenButton.css( 'display' ), 'none',
|
2014-02-25 01:54:05 +00:00
|
|
|
'Fullscreen button is hidden when fullscreen mode is unavailable' );
|
|
|
|
|
|
|
|
$.support.fullscreen = true;
|
2014-04-04 09:32:30 +00:00
|
|
|
lightbox.setupCanvasButtons();
|
2014-02-25 01:54:05 +00:00
|
|
|
|
2014-04-04 09:32:30 +00:00
|
|
|
assert.strictEqual( lightbox.$fullscreenButton.css( 'display' ), 'block',
|
2014-02-25 01:54:05 +00:00
|
|
|
'Fullscreen button is visible when fullscreen mode is available' );
|
|
|
|
|
|
|
|
// Entering fullscreen
|
|
|
|
lightbox.$fullscreenButton.click();
|
|
|
|
|
|
|
|
assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ) , true,
|
2015-01-23 12:48:27 +00:00
|
|
|
'Fullscreened area has the fullscreen class' );
|
|
|
|
assert.strictEqual( lightbox.isFullscreen , true, 'Lightbox knows it\'s in fullscreen mode' );
|
2014-02-25 01:54:05 +00:00
|
|
|
|
|
|
|
// Exiting fullscreen
|
|
|
|
lightbox.$fullscreenButton.click();
|
|
|
|
|
|
|
|
assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ) , false,
|
2015-01-23 12:48:27 +00:00
|
|
|
'Fullscreened area doesn\'t have the fullscreen class anymore' );
|
|
|
|
assert.strictEqual( lightbox.isFullscreen , false, 'Lightbox knows it\'s not in fullscreen mode' );
|
2014-02-25 01:54:05 +00:00
|
|
|
|
|
|
|
// Entering fullscreen
|
|
|
|
lightbox.$fullscreenButton.click();
|
|
|
|
|
|
|
|
// Hard-exiting fullscreen
|
|
|
|
lightbox.$closeButton.click();
|
|
|
|
|
|
|
|
// Re-attach after hard-exit
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ) , false,
|
2015-01-23 12:48:27 +00:00
|
|
|
'Fullscreened area doesn\'t have the fullscreen class anymore' );
|
|
|
|
assert.strictEqual( lightbox.isFullscreen , false, 'Lightbox knows it\'s not in fullscreen mode' );
|
2014-02-25 01:54:05 +00:00
|
|
|
|
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
|
|
|
|
|
|
|
$.fn.enterFullscreen = oldFnEnterFullscreen;
|
|
|
|
$.fn.exitFullscreen = oldFnExitFullscreen;
|
|
|
|
$.support.fullscreen = oldSupportFullscreen;
|
2014-03-29 01:38:22 +00:00
|
|
|
restoreScrollTo();
|
2014-02-25 01:54:05 +00:00
|
|
|
} );
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
QUnit.test( 'Fullscreen mode', 8, function ( assert ) {
|
2014-10-27 16:17:49 +00:00
|
|
|
var buttonOffset, panelBottom,
|
|
|
|
oldRevealButtonsAndFadeIfNeeded,
|
|
|
|
lightbox = new mw.mmv.LightboxInterface(),
|
2016-04-03 09:18:26 +00:00
|
|
|
viewer = mw.mmv.testHelpers.getMultimediaViewer(),
|
2014-01-30 07:22:26 +00:00
|
|
|
oldFnEnterFullscreen = $.fn.enterFullscreen,
|
2014-10-27 16:17:49 +00:00
|
|
|
oldFnExitFullscreen = $.fn.exitFullscreen;
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2014-03-29 01:38:22 +00:00
|
|
|
stubScrollTo();
|
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
// ugly hack to avoid preloading which would require lightbox list being set up
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer.preloadDistance = -1;
|
2014-02-13 09:52:40 +00:00
|
|
|
|
2014-01-30 07:22:26 +00:00
|
|
|
// Since we don't want these tests to really open fullscreen
|
|
|
|
// which is subject to user security confirmation,
|
|
|
|
// we use a mock that pretends regular jquery.fullscreen behavior happened
|
2014-02-25 02:28:49 +00:00
|
|
|
$.fn.enterFullscreen = mw.mmv.testHelpers.enterFullscreenMock;
|
|
|
|
$.fn.exitFullscreen = mw.mmv.testHelpers.exitFullscreenMock;
|
2014-01-30 07:22:26 +00:00
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
2014-03-04 12:52:08 +00:00
|
|
|
viewer.ui = lightbox;
|
2014-03-05 02:24:18 +00:00
|
|
|
viewer.ui = lightbox;
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( !lightbox.isFullscreen, 'Lightbox knows that it\'s not in fullscreen mode' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( lightbox.panel.$imageMetadata.is( ':visible' ), 'Image metadata is visible' );
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
lightbox.buttons.fadeOut = function () {
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( true, 'Opening fullscreen triggers a fadeout' );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Pretend that the mouse cursor is on top of the button
|
2014-02-04 23:44:36 +00:00
|
|
|
buttonOffset = lightbox.buttons.$fullscreen.offset();
|
2014-02-03 11:23:31 +00:00
|
|
|
lightbox.mousePosition = { x: buttonOffset.left, y: buttonOffset.top };
|
|
|
|
|
|
|
|
// Enter fullscreen
|
2014-02-04 23:44:36 +00:00
|
|
|
lightbox.buttons.$fullscreen.click();
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
lightbox.buttons.fadeOut = $.noop;
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( lightbox.isFullscreen, 'Lightbox knows that it\'s in fullscreen mode' );
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
oldRevealButtonsAndFadeIfNeeded = lightbox.buttons.revealAndFade;
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
lightbox.buttons.revealAndFade = function ( position ) {
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( true, 'Moving the cursor triggers a reveal + fade' );
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
oldRevealButtonsAndFadeIfNeeded.call( this, position );
|
2014-02-03 11:23:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Pretend that the mouse cursor moved to the top-left corner
|
|
|
|
lightbox.mousemove( { pageX: 0, pageY: 0 } );
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
lightbox.buttons.revealAndFadeIfNeeded = $.noop;
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
panelBottom = $( '.mw-mmv-post-image' ).position().top + $( '.mw-mmv-post-image' ).height();
|
2014-10-27 16:17:49 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( panelBottom === $( window ).height(), 'Image metadata does not extend beyond the viewport' );
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
// Exiting fullscreen
|
|
|
|
lightbox.buttons.$fullscreen.click();
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
panelBottom = $( '.mw-mmv-post-image' ).position().top + $( '.mw-mmv-post-image' ).height();
|
2014-10-27 16:17:49 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
assert.ok( panelBottom > $( window ).height(), 'Image metadata extends beyond the viewport' );
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( !lightbox.isFullscreen, 'Lightbox knows that it\'s not in fullscreen mode' );
|
2014-01-30 07:22:26 +00:00
|
|
|
|
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
|
|
|
|
|
|
|
$.fn.enterFullscreen = oldFnEnterFullscreen;
|
|
|
|
$.fn.exitFullscreen = oldFnExitFullscreen;
|
2014-03-29 01:38:22 +00:00
|
|
|
restoreScrollTo();
|
2014-01-30 07:22:26 +00:00
|
|
|
} );
|
|
|
|
|
2014-07-23 23:00:37 +00:00
|
|
|
QUnit.test( 'isAnyActiveButtonHovered', 35, function ( assert ) {
|
2014-03-04 12:52:08 +00:00
|
|
|
var lightbox = new mw.mmv.LightboxInterface();
|
2014-02-03 11:23:31 +00:00
|
|
|
|
2014-03-29 01:38:22 +00:00
|
|
|
stubScrollTo();
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
$.each ( lightbox.buttons.$buttons, function ( idx, e ) {
|
2014-02-03 11:23:31 +00:00
|
|
|
var $e = $( e ),
|
2014-01-31 09:58:28 +00:00
|
|
|
offset = $e.show().offset(),
|
2014-02-03 11:23:31 +00:00
|
|
|
width = $e.width(),
|
|
|
|
height = $e.height(),
|
|
|
|
disabled = $e.hasClass( 'disabled' );
|
|
|
|
|
2014-02-04 23:44:36 +00:00
|
|
|
assert.strictEqual( lightbox.buttons.isAnyActiveButtonHovered( offset.left, offset.top ),
|
2014-02-03 11:23:31 +00:00
|
|
|
!disabled,
|
|
|
|
'Hover detection works for top-left corner of element' );
|
2014-02-04 23:44:36 +00:00
|
|
|
assert.strictEqual( lightbox.buttons.isAnyActiveButtonHovered( offset.left + width, offset.top ),
|
2014-02-03 11:23:31 +00:00
|
|
|
!disabled,
|
|
|
|
'Hover detection works for top-right corner of element' );
|
2014-02-04 23:44:36 +00:00
|
|
|
assert.strictEqual( lightbox.buttons.isAnyActiveButtonHovered( offset.left, offset.top + height ),
|
2014-02-03 11:23:31 +00:00
|
|
|
!disabled,
|
|
|
|
'Hover detection works for bottom-left corner of element' );
|
2014-02-04 23:44:36 +00:00
|
|
|
assert.strictEqual( lightbox.buttons.isAnyActiveButtonHovered( offset.left + width, offset.top + height ),
|
2014-02-03 11:23:31 +00:00
|
|
|
!disabled,
|
|
|
|
'Hover detection works for bottom-right corner of element' );
|
2014-02-04 23:44:36 +00:00
|
|
|
assert.strictEqual( lightbox.buttons.isAnyActiveButtonHovered(
|
2014-02-03 11:23:31 +00:00
|
|
|
offset.left + ( width / 2 ), offset.top + ( height / 2 ) ),
|
|
|
|
!disabled,
|
|
|
|
'Hover detection works for center of element' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
2014-03-29 01:38:22 +00:00
|
|
|
restoreScrollTo();
|
2014-02-03 11:23:31 +00:00
|
|
|
} );
|
|
|
|
|
2014-02-26 17:04:29 +00:00
|
|
|
QUnit.test( 'Keyboard prev/next', 2, function ( assert ) {
|
2016-04-03 09:18:26 +00:00
|
|
|
var viewer = mw.mmv.testHelpers.getMultimediaViewer(),
|
2014-03-04 12:52:08 +00:00
|
|
|
lightbox = new mw.mmv.LightboxInterface();
|
|
|
|
|
|
|
|
viewer.setupEventHandlers();
|
2014-02-26 17:04:29 +00:00
|
|
|
|
|
|
|
// Since we define both, the test works regardless of RTL settings
|
2015-09-25 19:55:00 +00:00
|
|
|
lightbox.on( 'next', function () {
|
2014-02-26 17:04:29 +00:00
|
|
|
assert.ok( true, 'Next image was open' );
|
2015-09-25 19:55:00 +00:00
|
|
|
} );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
2015-09-25 19:55:00 +00:00
|
|
|
lightbox.on( 'prev', function () {
|
2014-02-26 17:04:29 +00:00
|
|
|
assert.ok( true, 'Prev image was open' );
|
2015-09-25 19:55:00 +00:00
|
|
|
} );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
|
|
|
// 37 is left arrow, 39 is right arrow
|
2015-01-23 12:48:27 +00:00
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 37 } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 39 } ) );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
2015-09-25 19:55:00 +00:00
|
|
|
lightbox.off( 'next' ).on( 'next', function () {
|
2014-02-26 17:04:29 +00:00
|
|
|
assert.ok( false, 'Next image should not have been open' );
|
2015-09-25 19:55:00 +00:00
|
|
|
} );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
2015-09-25 19:55:00 +00:00
|
|
|
lightbox.off( 'prev' ).on( 'prev', function () {
|
2014-02-26 17:04:29 +00:00
|
|
|
assert.ok( false, 'Prev image should not have been open' );
|
2015-09-25 19:55:00 +00:00
|
|
|
} );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 37, altKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 39, altKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 37, ctrlKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 39, ctrlKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 37, shiftKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 39, shiftKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 37, metaKey: true } ) );
|
|
|
|
lightbox.keydown( $.Event( 'keydown', { which: 39, metaKey: true } ) );
|
2014-02-26 17:04:29 +00:00
|
|
|
|
2014-03-04 12:52:08 +00:00
|
|
|
viewer.cleanupEventHandlers();
|
2014-02-26 17:04:29 +00:00
|
|
|
} );
|
2013-12-11 02:08:07 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|