' ).attr( 'src', ( imageSrc || 'thumb.jpg' ) ).appendTo( link );
return div;
}
QUnit.test( 'Check viewer invoked when clicking on an legit image links', 4, function ( assert ) {
// TODO: Is
valid ???
var div, link, link2, link3, viewer;
// Create viewer, no images in page.
viewer = new mw.MultimediaViewer();
assert.strictEqual( viewer.lightbox, null, 'There are not legit links, a lightbox should not be created.' );
// Create gallery with legit link image
div = createGallery();
link = div.find( 'a.image' );
// Legit isolated thumbnail
link2 = $( '
',
whitelisted = 'Blahblah',
okjq = $.parseHTML( okhtml ),
nwljq = $.parseHTML( needswhitelisting ),
$sandbox = $( '
' );
viewer.whitelistHtml( $sandbox.empty().append( okjq ) );
assert.strictEqual( $sandbox.html(), okhtml, 'Whitelisted elements are let through.' );
viewer.whitelistHtml( $sandbox.empty().append( nwljq ) );
assert.strictEqual( $sandbox.html(), whitelisted, 'Not-whitelisted elements are removed.' );
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.test( 'The location data is set correctly', 11, function ( assert ) {
var viewer,
origlatitude = '39.40192938201',
origlongitude = '-117.908329012',
imageData = new mw.mmv.model.Image(
mw.Title.newFromText( 'File:Foobar.jpg' ),
10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0', origlatitude, origlongitude
);
createGallery();
viewer = new mw.MultimediaViewer();
viewer.lightbox.iface.setLocationData = function (
latdeg, latmin, latsec, latmsg,
longdeg, longmin, longsec, longmsg,
latitude, longitude, langcode, titleText
) {
assert.strictEqual( latdeg, 39, 'Degree count is correct (latitude)' );
assert.strictEqual( latmin, 24, 'Minute count is correct (latitude)' );
assert.strictEqual( latsec, 6.95, 'Second count is correct (latitude)' );
assert.strictEqual( latmsg, 'multimediaviewer-geoloc-north', 'Cardinal direction is correct (latitude)' );
assert.strictEqual( longdeg, 117, 'Degree count is correct (longitude)' );
assert.strictEqual( longmin, 54, 'Minute count is correct (longitude)' );
assert.strictEqual( longsec, 29.98, 'Second count is correct (longitude)' );
assert.strictEqual( longmsg, 'multimediaviewer-geoloc-west', 'Cardinal direction is correct (longitude)' );
assert.strictEqual( latitude, origlatitude, 'Original latitude is passed in' );
assert.strictEqual( longitude, origlongitude, 'Original longitude is passed in' );
assert.strictEqual( titleText, 'Foobar.jpg', 'Filename is passed in correctly' );
};
viewer.setLocationData( imageData );
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.test( 'The location data is set correctly with weird values', 11, function ( assert ) {
var viewer,
origlatitude = '0',
origlongitude = '0',
imageData = new mw.mmv.model.Image(
mw.Title.newFromText( 'File:Foobar.pdf.jpg' ),
10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0', origlatitude, origlongitude
);
createGallery();
viewer = new mw.MultimediaViewer();
viewer.lightbox.iface.setLocationData = function (
latdeg, latmin, latsec, latmsg,
longdeg, longmin, longsec, longmsg,
latitude, longitude, langcode, titleText
) {
assert.strictEqual( latdeg, 0, 'Degree count is correct (latitude)' );
assert.strictEqual( latmin, 0, 'Minute count is correct (latitude)' );
assert.strictEqual( latsec, 0, 'Second count is correct (latitude)' );
assert.strictEqual( latmsg, 'multimediaviewer-geoloc-north', 'Cardinal direction is correct (latitude)' );
assert.strictEqual( longdeg, 0, 'Degree count is correct (longitude)' );
assert.strictEqual( longmin, 0, 'Minute count is correct (longitude)' );
assert.strictEqual( longsec, 0, 'Second count is correct (longitude)' );
assert.strictEqual( longmsg, 'multimediaviewer-geoloc-east', 'Cardinal direction is correct (longitude)' );
assert.strictEqual( latitude, origlatitude, 'Original latitude is passed in' );
assert.strictEqual( longitude, origlongitude, 'Original longitude is passed in' );
assert.strictEqual( titleText, 'Foobar.pdf.jpg', 'Filename is passed in correctly' );
};
viewer.setLocationData( imageData );
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.test( 'The location data is not set if no value is passed in', 1, function ( assert ) {
var viewer,
called = false,
imageData = new mw.mmv.model.Image(
mw.Title.newFromText( 'File:Foobar.pdf.jpg' ),
10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0'
);
createGallery();
viewer = new mw.MultimediaViewer();
viewer.lightbox.iface.setLocationData = function () {
called = true;
};
viewer.setLocationData( imageData );
assert.strictEqual( called, false, 'The interface data-setter method is not called if there are no coordinates available for the image.' );
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.test( 'getImageSizeApiArgs(): Limited by height and limited by width', 4, function ( assert ) {
var widths,
viewer = new mw.MultimediaViewer(),
ui = new mw.LightboxInterface( viewer );
// Fake thumbnail, width/height == 1.5
ui.currentImage = {
thumbnail: {
height: 100,
width: 150
}
};
ui.attach( '#qunit-fixture' );
// Fake viewport dimensions, width/height == 2.0, we are limited by height
ui.$imageWrapper.height( 200 );
ui.$imageWrapper.width( 400 );
widths = viewer.getImageSizeApiArgs( ui );
assert.strictEqual( widths.css, 150/100*200, 'Correct CSS width was computed.' );
assert.strictEqual( widths.real, 320 * $.devicePixelRatio(), 'Correct real width was computed.' );
// Fake viewport dimensions, width/height == 1.0, we are limited by width
ui.$imageWrapper.height( 600 );
ui.$imageWrapper.width( 600 );
widths = viewer.getImageSizeApiArgs( ui );
assert.strictEqual( widths.css, 600, 'Correct CSS width was computed.' );
assert.strictEqual( widths.real, 640 * $.devicePixelRatio(), 'Correct real width was computed.' );
ui.unattach();
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.asyncTest( 'loadAndSetImage(): Basic load', 9, function ( assert ) {
var targetWidth,
requestedWidth,
profileEvent,
pid = 4321,
viewer = new mw.MultimediaViewer(),
ui = new mw.LightboxInterface(),
size = 120,
width = 10,
height = 11,
imageUrl = 'http://en.wikipedia.org/w/skins/vector/images/search-ltr.png',
imageData = new mw.mmv.model.Image(
mw.Title.newFromText( 'File:Foobar.pdf.jpg' ),
size, width, height, 'image/jpeg',
imageUrl,
'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0', 0, 0
);
// Assert funtions are called with correct data
viewer.profileStart = function ( type, imgSize, fileType ) {
assert.strictEqual( type, profileEvent, 'Correct event type for profile start.' );
assert.strictEqual( imgSize.width, width, 'Correct width for profile start.' );
assert.strictEqual( imgSize.height, height, 'Correct height for profile start.' );
assert.strictEqual( imgSize.fileSize, size, 'Correct fileSize for profile start.' );
assert.strictEqual( fileType, 'image/jpeg', 'Correct fileType for profile start.' );
return pid;
};
viewer.profileEnd = function ( id ) {
assert.strictEqual( id, pid, 'Correct pid to end profiling. Image loaded correctly.' );
QUnit.start();
};
ui.replaceImageWith = function ( image ) {
assert.strictEqual( image.src, imageUrl, 'Image to replace has correct "src" attribute.' );
assert.strictEqual( image.width, targetWidth, 'Image to replace has correct "width" attribute.' );
};
viewer.updateControls = function () {
assert.ok( true, 'Controls updated.' );
};
// Test case when image loaded is bigger than current area
targetWidth = 8; // Current area < imageData.width
requestedWidth = 640;
profileEvent = 'image-load';
viewer.loadAndSetImage( ui, imageData, targetWidth, requestedWidth, profileEvent );
// Clean up the viewer, to avoid seeing it catch events when running other tests
mw.mmvTestHelpers.resetViewer();
} );
QUnit.test( 'Hash handling', 5, function ( assert ) {
var oldLoadImage = mw.mediaViewer.loadImage,
oldLightbox = mw.mediaViewer.lightbox,
imageSrc = 'Foo bar.jpg',
image = { filePageTitle: new mw.Title( 'File:' + imageSrc ) };
document.location.hash = '';
mw.mediaViewer.lightbox = { iface: { unattach: function() {
assert.ok( true, 'Interface unattached' );
} } };
// Verify that passing an invalid mmv hash triggers unattach()
document.location.hash = 'Foo';
mw.mediaViewer.lightbox = { images: [ image ] };
$( '#qunit-fixture' ).append( '
' );
mw.mediaViewer.loadImage = function( img, src ) {
assert.strictEqual( img, image, 'The image object matches' );
assert.ok( src.match( encodeURIComponent( imageSrc ) ), 'The image url matches' );
};
// Open a valid mmv hash link and check that the right image is requested.
// imageSrc contains a space without any encoding on purpose
document.location.hash = 'mediaviewer/File:' + imageSrc + '/0';
// Reset the hash, because for some browsers switching from the non-URI-encoded to
// the non-URI-encoded version of the same text with a space will not trigger a hash change
document.location.hash = '';
// Try again with an URI-encoded imageSrc containing a space
document.location.hash = 'mediaviewer/File:' + encodeURIComponent( imageSrc ) + '/0';
mw.mediaViewer.lightbox = oldLightbox;
mw.mediaViewer.loadImage = oldLoadImage;
document.location.hash = '';
} );
}( mediaWiki, jQuery ) );