2013-12-11 02:08:07 +00:00
|
|
|
( function ( mw, $ ) {
|
2014-01-23 21:44:26 +00:00
|
|
|
QUnit.module( 'mmv.lightboxInterface', QUnit.newMwEnvironment() );
|
2013-12-11 02:08:07 +00:00
|
|
|
|
2014-01-23 00:38:01 +00:00
|
|
|
QUnit.test( 'Sanity test, object creation and ui construction', 14, function ( assert ) {
|
2014-01-30 07:22:26 +00:00
|
|
|
var lightbox = new mw.LightboxInterface( mw.mediaViewer );
|
2013-12-11 02:08:07 +00:00
|
|
|
|
|
|
|
function checkIfUIAreasAttachedToDocument( inDocument ) {
|
|
|
|
var msg = inDocument === 1 ? ' ' : ' not ';
|
|
|
|
assert.strictEqual( $( '.mw-mlb-title' ).length, inDocument, 'Title area' + msg + 'attached.' );
|
|
|
|
assert.strictEqual( $( '.mw-mlb-author' ).length, inDocument, 'Author area' + msg + 'attached.' );
|
|
|
|
assert.strictEqual( $( '.mw-mlb-image-desc' ).length, inDocument, 'Description area' + msg + 'attached.' );
|
|
|
|
assert.strictEqual( $( '.mw-mlb-image-links' ).length, inDocument, 'Links area' + msg + 'attached.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// UI areas not attached to the document yet.
|
|
|
|
checkIfUIAreasAttachedToDocument(0);
|
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
// UI areas should now be attached to the document.
|
|
|
|
checkIfUIAreasAttachedToDocument(1);
|
|
|
|
|
2014-01-14 15:42:52 +00:00
|
|
|
// Check that the close button on the lightbox still follow the spec (being visible right away)
|
|
|
|
assert.strictEqual( $( '#qunit-fixture .mlb-close' ).length, 1, 'There should be a close button' );
|
2014-02-03 11:23:31 +00:00
|
|
|
assert.ok( $( '#qunit-fixture .mlb-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.
|
|
|
|
checkIfUIAreasAttachedToDocument(0);
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Handler registration and clearance work OK', 2, function ( assert ) {
|
2014-01-30 07:22:26 +00:00
|
|
|
var lightbox = new mw.LightboxInterface( mw.mediaViewer ),
|
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-03 11:23:31 +00:00
|
|
|
QUnit.test( 'Fullscreen mode', 8, function ( assert ) {
|
2014-01-30 07:22:26 +00:00
|
|
|
var lightbox = new mw.LightboxInterface( mw.mediaViewer ),
|
|
|
|
oldFnEnterFullscreen = $.fn.enterFullscreen,
|
2014-02-03 11:23:31 +00:00
|
|
|
oldFnExitFullscreen = $.fn.exitFullscreen,
|
|
|
|
oldRevealButtonsAndFadeIfNeeded,
|
2014-02-13 09:52:40 +00:00
|
|
|
oldPreloadDistance = mw.mediaViewer.preloadDistance,
|
2014-02-03 11:23:31 +00:00
|
|
|
buttonOffset;
|
2014-01-30 07:22:26 +00:00
|
|
|
|
2014-02-13 09:52:40 +00:00
|
|
|
// ugly hack to avoid preloading which would require lightbox list being set up
|
|
|
|
mw.mediaViewer.preloadDistance = -1;
|
|
|
|
|
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
|
|
|
|
$.fn.enterFullscreen = mw.mmvTestHelpers.enterFullscreenMock;
|
|
|
|
$.fn.exitFullscreen = mw.mmvTestHelpers.exitFullscreenMock;
|
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
lightbox.viewer.ui = lightbox;
|
|
|
|
lightbox.viewer.lightbox = lightbox;
|
|
|
|
|
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
|
|
|
|
2014-02-04 23:44:36 +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
|
|
|
|
2014-02-04 23:44:36 +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
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( !lightbox.panel.$imageMetadata.is( ':visible' ), 'Image metadata is hidden' );
|
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
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( lightbox.panel.$imageMetadata.is( ':visible' ), 'Image metadata is visible' );
|
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-02-13 09:52:40 +00:00
|
|
|
mw.mediaViewer.preloadDistance = oldPreloadDistance;
|
2014-01-30 07:22:26 +00:00
|
|
|
} );
|
|
|
|
|
2014-02-03 11:23:31 +00:00
|
|
|
QUnit.test( 'isAnyActiveButtonHovered', 20, function ( assert ) {
|
|
|
|
var lightbox = new mw.LightboxInterface( mw.mediaViewer );
|
|
|
|
|
|
|
|
// 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-01-23 00:38:01 +00:00
|
|
|
QUnit.test( 'Metadata scrolling', 13, function ( assert ) {
|
|
|
|
var lightbox = new mw.LightboxInterface( mw.mediaViewer ),
|
|
|
|
keydown = $.Event( 'keydown' ),
|
|
|
|
$document = $( document ),
|
|
|
|
scrollTopBeforeOpeningLightbox,
|
|
|
|
originalJQueryScrollTop = $.fn.scrollTop,
|
|
|
|
memorizedScrollToScroll = 0,
|
|
|
|
originalJQueryScrollTo = $.scrollTo,
|
|
|
|
oldMWLightbox = mw.mediaViewer.lightbox,
|
|
|
|
oldMWUI = mw.mediaViewer.ui;
|
|
|
|
|
|
|
|
// Pretend that we have things hooked up
|
|
|
|
mw.mediaViewer.ui = lightbox;
|
|
|
|
|
|
|
|
// We need to set up a proxy on the jQuery scrollTop function
|
|
|
|
// that will let us pretend that the document really scrolled
|
|
|
|
// and that will return values as if the scroll happened
|
|
|
|
$.fn.scrollTop = function ( scrollTop ) {
|
|
|
|
// On some browsers $.scrollTo() != $document
|
|
|
|
if ( $.scrollTo().is( this ) ) {
|
|
|
|
if ( scrollTop !== undefined ) {
|
|
|
|
memorizedScrollToScroll = scrollTop;
|
|
|
|
return this;
|
|
|
|
} else {
|
|
|
|
return memorizedScrollToScroll;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return originalJQueryScrollTop.call( this, scrollTop );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Same idea as above, for the scrollTo plugin
|
|
|
|
$.scrollTo = function ( scrollTo ) {
|
|
|
|
var $element;
|
|
|
|
|
|
|
|
if ( scrollTo !== undefined ) {
|
|
|
|
memorizedScrollToScroll = scrollTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$element = originalJQueryScrollTo.call( this, scrollTo, 0 );
|
|
|
|
|
|
|
|
if ( scrollTo !== undefined ) {
|
|
|
|
// Trigger event manually
|
|
|
|
mw.mediaViewer.scroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
};
|
|
|
|
|
|
|
|
// First phase of the test: up and down arrows
|
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
// Pretend that we have things hooked up
|
|
|
|
mw.mediaViewer.lightbox = { currentIndex: 0 };
|
|
|
|
|
|
|
|
// This lets us avoid pushing a state to the history, which might interfere with other tests
|
|
|
|
lightbox.comingFromPopstate = true;
|
|
|
|
// Load is needed to start listening to metadata events
|
|
|
|
lightbox.load( { getImageElement: function() { return $.Deferred().reject(); } } );
|
|
|
|
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), 0, 'scrollTo scrollTop should be set to 0' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( !lightbox.panel.$dragIcon.hasClass( 'pointing-down' ),
|
2014-01-23 00:38:01 +00:00
|
|
|
'Chevron pointing up' );
|
|
|
|
|
|
|
|
keydown.which = 38; // Up arrow
|
|
|
|
$document.trigger( keydown );
|
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.strictEqual( Math.round( $.scrollTo().scrollTop() ), lightbox.panel.$imageMetadata.height() + 1,
|
2014-01-23 00:38:01 +00:00
|
|
|
'scrollTo scrollTop should be set to the metadata height + 1 after pressing up arrow' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( lightbox.panel.$dragIcon.hasClass( 'pointing-down' ),
|
2014-01-23 00:38:01 +00:00
|
|
|
'Chevron pointing down after pressing up arrow' );
|
|
|
|
|
|
|
|
keydown.which = 40; // Down arrow
|
|
|
|
$document.trigger( keydown );
|
|
|
|
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), 0,
|
|
|
|
'scrollTo scrollTop should be set to 0 after pressing down arrow' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( !lightbox.panel.$dragIcon.hasClass( 'pointing-down' ),
|
2014-01-23 00:38:01 +00:00
|
|
|
'Chevron pointing up after pressing down arrow' );
|
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
lightbox.panel.$dragIcon.click();
|
2014-01-23 00:38:01 +00:00
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.strictEqual( Math.round( $.scrollTo().scrollTop() ), lightbox.panel.$imageMetadata.height() + 1,
|
2014-01-23 00:38:01 +00:00
|
|
|
'scrollTo scrollTop should be set to the metadata height + 1 after clicking the chevron once' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( lightbox.panel.$dragIcon.hasClass( 'pointing-down' ),
|
2014-01-23 00:38:01 +00:00
|
|
|
'Chevron pointing down after clicking the chevron once' );
|
|
|
|
|
2014-02-07 14:47:00 +00:00
|
|
|
lightbox.panel.$dragIcon.click();
|
2014-01-23 00:38:01 +00:00
|
|
|
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), 0,
|
|
|
|
'scrollTo scrollTop should be set to 0 after clicking the chevron twice' );
|
2014-02-07 14:47:00 +00:00
|
|
|
assert.ok( !lightbox.panel.$dragIcon.hasClass( 'pointing-down' ),
|
2014-01-23 00:38:01 +00:00
|
|
|
'Chevron pointing up after clicking the chevron twice' );
|
|
|
|
|
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
|
|
|
|
|
|
|
|
|
|
|
// Second phase of the test: scroll memory
|
|
|
|
|
|
|
|
// Scroll down a little bit to check that the scroll memory works
|
|
|
|
$.scrollTo( 10, 0 );
|
|
|
|
|
|
|
|
scrollTopBeforeOpeningLightbox = $.scrollTo().scrollTop();
|
|
|
|
|
|
|
|
// Attach lightbox to testing fixture to avoid interference with other tests.
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
// To make sure that the details are out of view, the lightbox is supposed to scroll to the top when open
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), 0, 'Page scrollTop should be set to 0' );
|
|
|
|
|
|
|
|
// Scroll down to check that the scrollTop memory doesn't affect prev/next (bug 59861)
|
|
|
|
$.scrollTo( 20, 0 );
|
|
|
|
|
|
|
|
// This extra attach() call simulates the effect of prev/next seen in bug 59861
|
|
|
|
lightbox.attach( '#qunit-fixture' );
|
|
|
|
|
|
|
|
// The lightbox was already open at this point, the scrollTop should be left untouched
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), 20, 'Page scrollTop should be set to 20' );
|
|
|
|
|
|
|
|
// Unattach lightbox from document
|
|
|
|
lightbox.unattach();
|
|
|
|
|
|
|
|
// Lightbox is supposed to restore the document scrollTop value that was set prior to opening it
|
|
|
|
assert.strictEqual( $.scrollTo().scrollTop(), scrollTopBeforeOpeningLightbox, 'document scrollTop value has been restored correctly' );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Let's restore all originals, to make sure this test is free of side-effect
|
|
|
|
$.fn.scrollTop = originalJQueryScrollTop;
|
|
|
|
$.scrollTo = originalJQueryScrollTo;
|
|
|
|
mw.mediaViewer.lightbox = oldMWLightbox;
|
|
|
|
mw.mediaViewer.ui = oldMWUI;
|
|
|
|
} );
|
2013-12-11 02:08:07 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|