2014-02-17 15:09:23 +00:00
|
|
|
( function ( mw, $ ) {
|
|
|
|
QUnit.module( 'mmv.bootstrap', QUnit.newMwEnvironment() );
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
function createGallery( imageSrc ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
var div = $( '<div>' ).addClass( 'gallery' ).appendTo( '#qunit-fixture' ),
|
|
|
|
link = $( '<a>' ).addClass( 'image' ).appendTo( div );
|
|
|
|
|
|
|
|
$( '<img>' ).attr( 'src', ( imageSrc || 'thumb.jpg' ) ).appendTo( link );
|
|
|
|
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createThumb( imageSrc, caption ) {
|
|
|
|
var div = $( '<div>' ).addClass( 'thumb' ).appendTo( '#qunit-fixture' ),
|
|
|
|
link = $( '<a>' ).addClass( 'image' ).appendTo( div );
|
|
|
|
|
|
|
|
$( '<div>' ).addClass( 'thumbcaption' ).appendTo( div ).text( caption );
|
|
|
|
$( '<img>' ).attr( 'src', ( imageSrc || 'thumb.jpg' ) ).appendTo( link );
|
|
|
|
|
|
|
|
return div;
|
|
|
|
}
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
function createBootstrap( viewer ) {
|
|
|
|
var bootstrap = new mw.mmv.MultimediaViewerBootstrap();
|
|
|
|
bootstrap.getViewer = function() { return viewer ? viewer : { initWithThumbs : $.noop }; };
|
|
|
|
|
|
|
|
return bootstrap;
|
|
|
|
}
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
QUnit.test( 'Promise does not hang on ResourceLoader errors', 3, function ( assert ) {
|
2014-02-14 00:36:10 +00:00
|
|
|
var oldUsing = mw.loader.using,
|
|
|
|
bootstrap,
|
|
|
|
errorMessage = 'loading failed';
|
|
|
|
|
|
|
|
mw.loader.using = function ( module, success, error ) {
|
|
|
|
if ( $.isFunction( error ) ) {
|
|
|
|
error( new Error( errorMessage, ['mmv'] ) );
|
|
|
|
}
|
|
|
|
};
|
2014-03-04 11:53:53 +00:00
|
|
|
|
|
|
|
bootstrap = createBootstrap();
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was set up' );
|
|
|
|
};
|
|
|
|
|
|
|
|
bootstrap.cleanupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was cleaned up' );
|
|
|
|
};
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
QUnit.stop();
|
2014-02-14 00:36:10 +00:00
|
|
|
|
|
|
|
bootstrap.loadViewer().fail( function ( message ) {
|
|
|
|
assert.strictEqual( message, errorMessage, 'promise is rejected with the error message when loading fails' );
|
|
|
|
QUnit.start();
|
2014-03-04 11:53:53 +00:00
|
|
|
mw.loader.using = oldUsing;
|
2014-02-14 00:36:10 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2014-04-08 21:01:07 +00:00
|
|
|
QUnit.test( 'Check viewer invoked when clicking on legit image links', 7, function ( assert ) {
|
2014-02-17 15:09:23 +00:00
|
|
|
// TODO: Is <div class="gallery"><span class="image"><img/></span></div> valid ???
|
2014-04-08 21:01:07 +00:00
|
|
|
var div, link, link2, link3, link4, bootstrap,
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer = { initWithThumbs : $.noop };
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
// Create gallery with legit link image
|
|
|
|
div = createGallery();
|
|
|
|
link = div.find( 'a.image' );
|
|
|
|
|
|
|
|
// Legit isolated thumbnail
|
|
|
|
link2 = $( '<a>' ).addClass( 'image' ).appendTo( '#qunit-fixture' );
|
|
|
|
$( '<img>' ).attr( 'src', 'thumb2.jpg' ).appendTo( link2 );
|
|
|
|
|
|
|
|
// Non-legit fragment
|
|
|
|
link3 = $( '<a>' ).addClass( 'noImage' ).appendTo( div );
|
|
|
|
$( '<img>' ).attr( 'src', 'thumb3.jpg' ).appendTo( link3 );
|
|
|
|
|
2014-04-08 21:01:07 +00:00
|
|
|
$( '<div>' ).addClass( 'fullMedia' ).appendTo( div );
|
|
|
|
$( '<img>' ).attr( 'src', 'thumb4.jpg' ).appendTo(
|
|
|
|
$( '<a>' )
|
|
|
|
.appendTo(
|
|
|
|
$( '<div>' )
|
|
|
|
.attr( 'id', 'file' )
|
|
|
|
.appendTo( '#qunit-fixture' )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Create a new bootstrap object to trigger the DOM scan, etc.
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap( viewer );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-04-08 21:01:07 +00:00
|
|
|
link4 = $( '.fullMedia .mw-mmv-view-expanded' );
|
|
|
|
assert.ok( link4.length, 'Link for viewing expanded file was set up.' );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function() {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( true, 'Image loaded' );
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Click on legit link
|
|
|
|
link.trigger( { type : 'click', which : 1 } );
|
|
|
|
|
|
|
|
// Click on legit link
|
|
|
|
link2.trigger( { type : 'click', which : 1 } );
|
|
|
|
|
2014-04-08 21:01:07 +00:00
|
|
|
// Click on legit link
|
|
|
|
link4.trigger( { type: 'click', which: 1 } );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( false, 'Overlay was not set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function() {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( false, 'Image should not be loaded' );
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Click on non-legit link
|
|
|
|
link3.trigger( { type : 'click', which : 1 } );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Skip images with invalid extensions', 0, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
var div, link, bootstrap,
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer = { initWithThumbs : $.noop };
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
// Create gallery with image that has invalid name extension
|
|
|
|
div = createGallery( 'thumb.badext' );
|
|
|
|
link = div.find( 'a.image' );
|
|
|
|
|
|
|
|
// Create a new bootstrap object to trigger the DOM scan, etc.
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap( viewer );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function() {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( false, 'Image should not be loaded' );
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Click on legit link with wrong image extension
|
|
|
|
link.trigger( { type : 'click', which : 1 } );
|
|
|
|
} );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
QUnit.test( 'Accept only left clicks without modifier keys, skip the rest', 2, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
var $div, $link, bootstrap,
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer = { initWithThumbs : $.noop };
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
// Create gallery with image that has invalid name extension
|
|
|
|
$div = createGallery();
|
|
|
|
|
|
|
|
// Create a new bootstrap object to trigger the DOM scan, etc.
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap( viewer );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
|
|
|
$link = $div.find( 'a.image' );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function() {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( true, 'Image loaded' );
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Handle valid left click, it should try to load the image
|
|
|
|
$link.trigger( { type : 'click', which : 1 } );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( false, 'Overlay was not set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function() {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( false, 'Image should not be loaded' );
|
|
|
|
};
|
|
|
|
|
2014-02-17 15:09:23 +00:00
|
|
|
// Skip Ctrl-left-click, no image is loaded
|
|
|
|
$link.trigger( { type : 'click', which : 1, ctrlKey : true } );
|
|
|
|
|
|
|
|
// Skip invalid right click, no image is loaded
|
|
|
|
$link.trigger( { type : 'click', which : 2 } );
|
|
|
|
} );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
QUnit.test( 'Ensure that the correct title is loaded when clicking', 2, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
var bootstrap,
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer = { initWithThumbs : $.noop },
|
2014-02-21 11:16:30 +00:00
|
|
|
$div = createGallery( 'foo.jpg' ),
|
2014-02-17 15:09:23 +00:00
|
|
|
$link = $div.find( 'a.image' );
|
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImageByTitle = function ( loadedTitle ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.strictEqual( loadedTitle, 'File:Foo.jpg', 'Titles are identical' );
|
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
// Create a new bootstrap object to trigger the DOM scan, etc.
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap( viewer );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
$link.trigger( { type : 'click', which : 1 } );
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
QUnit.test( 'Validate new LightboxImage object has sane constructor parameters', 7, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
var bootstrap,
|
|
|
|
$div,
|
|
|
|
$link,
|
2014-03-04 11:53:53 +00:00
|
|
|
viewer = new mw.mmv.MultimediaViewer(),
|
2014-02-21 11:16:30 +00:00
|
|
|
fname = 'valid',
|
|
|
|
imgSrc = '/' + fname + '.jpg/300px-' + fname + '.jpg',
|
|
|
|
imgRegex = new RegExp( imgSrc + '$' );
|
|
|
|
|
|
|
|
$div = createThumb( imgSrc, 'Blah blah' );
|
|
|
|
$link = $div.find( 'a.image' );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.loadImage = $.noop;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-25 02:28:49 +00:00
|
|
|
viewer.createNewImage = function ( fileLink, filePageLink, fileTitle, index, thumb, caption ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( fileLink.match( imgRegex ), 'Thumbnail URL used in creating new image object' );
|
|
|
|
assert.strictEqual( filePageLink, '', 'File page link is sane when creating new image object' );
|
|
|
|
assert.strictEqual( fileTitle.title, fname, 'Filename is correct when passed into new image constructor' );
|
|
|
|
assert.strictEqual( index, 0, 'The only image we created in the gallery is set at index 0 in the images array' );
|
|
|
|
assert.strictEqual( thumb.outerHTML, '<img src="' + imgSrc + '">', 'The image element passed in is the thumbnail we want.' );
|
|
|
|
assert.strictEqual( caption, 'Blah blah', 'The caption passed in is correct' );
|
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
// Create a new bootstrap object to trigger the DOM scan, etc.
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap( viewer );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-28 08:06:53 +00:00
|
|
|
bootstrap.setupOverlay = function () {
|
|
|
|
assert.ok( true, 'Overlay was set up' );
|
|
|
|
};
|
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
$link.trigger( { type : 'click', which : 1 } );
|
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
QUnit.asyncTest( 'Only load the viewer on a valid hash', 1, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
var bootstrap;
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
window.location.hash = '';
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap = createBootstrap();
|
|
|
|
bootstrap.setupEventHandlers();
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
bootstrap.loadViewer = function () {
|
|
|
|
assert.ok( false, 'Viewer should not be loaded' );
|
|
|
|
return $.Deferred().reject();
|
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
window.location.hash = 'Foo';
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
bootstrap.loadViewer = function () {
|
2014-02-24 09:29:14 +00:00
|
|
|
QUnit.start();
|
2014-02-21 11:16:30 +00:00
|
|
|
assert.ok( true, 'Viewer should be loaded' );
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap.cleanupEventHandlers();
|
2014-02-24 09:29:14 +00:00
|
|
|
window.location.hash = '';
|
2014-02-21 11:16:30 +00:00
|
|
|
return $.Deferred().reject();
|
|
|
|
};
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-02-21 11:16:30 +00:00
|
|
|
window.location.hash = 'mediaviewer/foo';
|
2014-02-24 09:29:14 +00:00
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
QUnit.test( 'internalHashChange', 1, function ( assert ) {
|
2014-02-21 11:16:30 +00:00
|
|
|
window.location.hash = '';
|
2014-02-24 09:29:14 +00:00
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
var bootstrap = createBootstrap(),
|
2014-02-24 09:29:14 +00:00
|
|
|
hash = '#mediaviewer/foo',
|
|
|
|
oldHash = bootstrap.hash;
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap.setupEventHandlers();
|
|
|
|
|
2014-02-24 09:29:14 +00:00
|
|
|
bootstrap.hash = function () {
|
|
|
|
oldHash.call( this );
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
bootstrap.cleanupEventHandlers();
|
2014-02-24 09:29:14 +00:00
|
|
|
window.location.hash = '';
|
|
|
|
QUnit.start();
|
|
|
|
};
|
|
|
|
|
|
|
|
bootstrap.loadViewer = function () {
|
|
|
|
assert.ok( false, 'Viewer should not be loaded' );
|
|
|
|
return $.Deferred().reject();
|
|
|
|
};
|
|
|
|
|
2014-03-04 11:53:53 +00:00
|
|
|
QUnit.stop();
|
2014-02-24 09:29:14 +00:00
|
|
|
bootstrap.internalHashChange( { hash: hash } );
|
|
|
|
|
|
|
|
assert.strictEqual( window.location.hash, hash, 'Window\'s hash has been updated correctly' );
|
2014-02-17 15:09:23 +00:00
|
|
|
} );
|
2014-02-24 17:06:11 +00:00
|
|
|
|
|
|
|
QUnit.test( 'isCSSReady', 3, function ( assert ) {
|
2014-03-04 11:53:53 +00:00
|
|
|
var bootstrap = createBootstrap(),
|
2014-02-24 17:06:11 +00:00
|
|
|
deferred = $.Deferred(),
|
|
|
|
CSSclass = 'foo-' + $.now(),
|
|
|
|
$style = $( '<style type="text/css" />' )
|
|
|
|
.text( '.' + CSSclass + ' { display: inline; }' );
|
|
|
|
|
|
|
|
bootstrap.readinessCSSClass = CSSclass;
|
|
|
|
// This speeds up the test execution
|
|
|
|
// It's not zero because if the test fails, the browser would get hammered indefinitely
|
|
|
|
bootstrap.readinessWaitDuration = 30;
|
|
|
|
|
|
|
|
bootstrap.isCSSReady( deferred );
|
|
|
|
|
|
|
|
assert.strictEqual( deferred.state(), 'pending', 'The style isn\'t on the page yet' );
|
|
|
|
|
|
|
|
QUnit.stop();
|
|
|
|
|
|
|
|
deferred.then( function() {
|
|
|
|
QUnit.start();
|
|
|
|
assert.ok( true, 'The style is on the page' );
|
|
|
|
assert.strictEqual( $( '.' + CSSclass ).length, 0, 'There are no leftover test elements' );
|
|
|
|
$style.remove();
|
|
|
|
} );
|
|
|
|
|
|
|
|
$style.appendTo( 'head' );
|
|
|
|
} );
|
2014-02-17 15:09:23 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|