Add a very simple resize test

Add test to verify that in case of a resize event no image
replacement takes place when api data is empty/undefined.

Change-Id: I2a880ce4b2e6c158763b1473f6a9f751922187b0
This commit is contained in:
Aaron Arcos 2013-12-02 17:58:09 -08:00 committed by MarkTraceur
parent b704f294f8
commit add9a9b435
2 changed files with 40 additions and 6 deletions

View file

@ -227,14 +227,19 @@
this.loadImage( thisImage, initial );
};
/**
* Handles resize events in viewer.
*
* @protected
*
* @param {mw.LightboxInterface} ui lightbox that got resized
*/
MMVP.resize = function ( ui ) {
// TODO: Reuse the api member, fix everywhere.
var api = new mw.Api(),
viewer = this,
var viewer = this,
density = $.devicePixelRatio(),
filename = ui.currentImageFilename;
api.get( {
this.api.get( {
action: 'query',
format: 'json',
titles: filename,
@ -243,6 +248,21 @@
iiurlwidth: Math.floor( density * ui.$imageWrapper.width() ),
iiurlheight: Math.floor( density * ui.$imageWrapper.height() )
} ).done( function ( data ) {
viewer.loadResizedImage( ui, data );
} );
};
/**
* Replaces the resized image in the viewer providing we actually got some data.
*
* @protected
*
* @param {mw.LightboxInterface} ui lightbox that got resized
* @param {Object} data information regarding the new resized image
*/
MMVP.loadResizedImage = function ( ui, data ) {
// Replace image only if data was returned.
if ( data && data.query && data.query.pages ) {
var imageInfo, innerInfo,
image = new Image();
@ -255,11 +275,11 @@
image.onload = function () {
ui.replaceImageWith( image );
viewer.updateControls();
this.updateControls();
};
image.src = innerInfo.thumburl || innerInfo.url;
} );
}
};
MMVP.updateControls = function () {
@ -494,6 +514,7 @@
username = innerInfo.user;
if ( username ) {
// TODO: Reuse the api member, fix everywhere.
// Fetch the gender from the uploader's home wiki
// TODO this is ugly as hell, let's fix this in core.
new mw.Api( {

View file

@ -107,4 +107,17 @@
link.trigger( rightClick );
} );
QUnit.test( 'Do not load the resized image if no data returning from the api', 1, function ( assert ) {
var ui,
data,
viewer = new mw.MultimediaViewer();
// Calling loadResizedImage() with empty/undefined data should not fail.
viewer.loadResizedImage( ui, data );
viewer.loadResizedImage( ui, {} );
viewer.loadResizedImage( ui, { query: {} } );
assert.ok( true, 'Resized image is not replaced since we have not data.' );
} );
}( mediaWiki, jQuery ) );