2014-02-26 20:09:37 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MediaViewer.
|
|
|
|
*
|
|
|
|
* MediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-05-10 07:57:42 +00:00
|
|
|
const { LightboxImage } = require( 'mmv.bootstrap' );
|
|
|
|
const { Canvas } = require( 'mmv' );
|
2023-05-20 08:30:52 +00:00
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-02-28 11:50:06 +00:00
|
|
|
QUnit.module( 'mmv.ui.Canvas', QUnit.newMwEnvironment() );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'Constructor sense check', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const canvas = new Canvas( $qf, $qf, $qf );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.strictEqual( canvas.$imageDiv.length, 1, 'Image container is created.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
assert.strictEqual( canvas.$imageWrapper, $qf, '$imageWrapper is set correctly.' );
|
|
|
|
assert.strictEqual( canvas.$mainWrapper, $qf, '$mainWrapper is set correctly.' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'empty() and set()', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const canvas = new Canvas( $qf );
|
|
|
|
const image = new Image();
|
|
|
|
const $imageElem = $( image );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
canvas.empty();
|
|
|
|
|
|
|
|
assert.strictEqual( canvas.$imageDiv.html(), '', 'Canvas is empty.' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), true, 'Canvas is not visible.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
canvas.set( imageRawMetadata, $imageElem );
|
|
|
|
|
|
|
|
assert.strictEqual( canvas.$image, $imageElem, 'Image element set correctly.' );
|
|
|
|
assert.strictEqual( canvas.imageRawMetadata, imageRawMetadata, 'Raw metadata set correctly.' );
|
2014-09-11 17:39:19 +00:00
|
|
|
assert.strictEqual( canvas.$imageDiv.html(), '<img>', 'Image added to container.' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), false, 'Canvas is visible.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
canvas.empty();
|
|
|
|
|
|
|
|
assert.strictEqual( canvas.$imageDiv.html(), '', 'Canvas is empty.' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), true, 'Canvas is not visible.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'setImageAndMaxDimensions()', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const $mainWrapper = $( '<div>' ).appendTo( $qf );
|
|
|
|
const $innerWrapper = $( '<div>' ).appendTo( $mainWrapper );
|
|
|
|
const $imageWrapper = $( '<div>' ).appendTo( $innerWrapper );
|
|
|
|
const canvas = new Canvas( $innerWrapper, $imageWrapper, $mainWrapper );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
|
|
|
const image = new Image();
|
|
|
|
const $imageElem = $( image );
|
|
|
|
const image2 = new Image();
|
|
|
|
let thumbnailWidth = 10;
|
|
|
|
const screenWidth = 100;
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
// Need to call set() before using setImageAndMaxDimensions()
|
|
|
|
canvas.set( imageRawMetadata, $imageElem );
|
2023-10-24 09:53:23 +00:00
|
|
|
const originalWidth = image.width;
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
// Call with the same image
|
2014-03-04 01:58:27 +00:00
|
|
|
canvas.setImageAndMaxDimensions(
|
|
|
|
{ width: thumbnailWidth },
|
|
|
|
image,
|
|
|
|
{ cssWidth: screenWidth }
|
|
|
|
);
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( image.width, originalWidth, 'Image width was not modified.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
assert.strictEqual( canvas.$image, $imageElem, 'Image element still set correctly.' );
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $currentImage = canvas.$image;
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
// Call with a new image bigger than screen size
|
|
|
|
thumbnailWidth = 150;
|
|
|
|
canvas.setImageAndMaxDimensions(
|
|
|
|
{ width: thumbnailWidth },
|
|
|
|
image2,
|
|
|
|
{ cssWidth: screenWidth }
|
|
|
|
);
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
assert.strictEqual( image2.width, screenWidth, 'Image width was trimmed correctly.' );
|
|
|
|
assert.notStrictEqual( canvas.$image, $currentImage, 'Image element switched correctly.' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: Constrained area for SVG files', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.svg' );
|
|
|
|
const canvas = new Canvas( $qf );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
imageRawMetadata.filePageTitle = {
|
2024-02-13 00:44:51 +00:00
|
|
|
getExtension: function () {
|
|
|
|
return 'svg';
|
|
|
|
}
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
canvas.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
|
|
|
canvas.set = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'Placeholder is not shown' );
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $image = $( '<img>' ).width( 10 ).height( 5 );
|
2014-03-04 01:58:27 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const blurredThumbnailShown = canvas.maybeDisplayPlaceholder(
|
2015-01-23 12:48:27 +00:00
|
|
|
{ width: 200, height: 100 },
|
2014-03-04 01:58:27 +00:00
|
|
|
$image,
|
2015-01-23 12:48:27 +00:00
|
|
|
{ cssWidth: 300, cssHeight: 150 }
|
2014-03-04 01:58:27 +00:00
|
|
|
);
|
|
|
|
|
2014-04-15 00:01:03 +00:00
|
|
|
assert.strictEqual( $image.width(), 10, 'Placeholder width was not set to max' );
|
|
|
|
assert.strictEqual( $image.height(), 5, 'Placeholder height was not set to max' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $image.hasClass( 'blurred' ), false, 'Placeholder is not blurred' );
|
|
|
|
assert.strictEqual( blurredThumbnailShown, false, 'Placeholder state is correct' );
|
2014-03-04 01:58:27 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: placeholder big enough that it doesn\'t need blurring, actual image bigger than the lightbox', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
|
|
|
const canvas = new Canvas( $qf );
|
2014-03-04 01:58:27 +00:00
|
|
|
|
|
|
|
imageRawMetadata.filePageTitle = {
|
2024-02-13 00:44:51 +00:00
|
|
|
getExtension: function () {
|
|
|
|
return 'png';
|
|
|
|
}
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
canvas.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
canvas.set = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'Placeholder shown' );
|
2014-02-26 20:09:37 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $image = $( '<img>' ).width( 200 ).height( 100 );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const blurredThumbnailShown = canvas.maybeDisplayPlaceholder(
|
2015-01-23 12:48:27 +00:00
|
|
|
{ width: 1000, height: 500 },
|
2014-02-26 20:09:37 +00:00
|
|
|
$image,
|
2015-01-23 12:48:27 +00:00
|
|
|
{ cssWidth: 300, cssHeight: 150 }
|
2014-02-26 20:09:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual( $image.width(), 300, 'Placeholder has the right width' );
|
|
|
|
assert.strictEqual( $image.height(), 150, 'Placeholder has the right height' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $image.hasClass( 'blurred' ), false, 'Placeholder is not blurred' );
|
|
|
|
assert.strictEqual( blurredThumbnailShown, false, 'Placeholder state is correct' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: big-enough placeholder that needs blurring, actual image bigger than the lightbox', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
|
|
|
const canvas = new Canvas( $qf );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
imageRawMetadata.filePageTitle = {
|
2024-02-13 00:44:51 +00:00
|
|
|
getExtension: function () {
|
|
|
|
return 'png';
|
|
|
|
}
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
canvas.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
canvas.set = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'Placeholder shown' );
|
2014-02-26 20:09:37 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $image = $( '<img>' ).width( 100 ).height( 50 );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const blurredThumbnailShown = canvas.maybeDisplayPlaceholder(
|
2015-01-23 12:48:27 +00:00
|
|
|
{ width: 1000, height: 500 },
|
2014-02-26 20:09:37 +00:00
|
|
|
$image,
|
2015-01-23 12:48:27 +00:00
|
|
|
{ cssWidth: 300, cssHeight: 150 }
|
2014-02-26 20:09:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual( $image.width(), 300, 'Placeholder has the right width' );
|
|
|
|
assert.strictEqual( $image.height(), 150, 'Placeholder has the right height' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $image.hasClass( 'blurred' ), true, 'Placeholder is blurred' );
|
|
|
|
assert.strictEqual( blurredThumbnailShown, true, 'Placeholder state is correct' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: big-enough placeholder that needs blurring, actual image smaller than the lightbox', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
|
|
|
const canvas = new Canvas( $qf );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
imageRawMetadata.filePageTitle = {
|
2024-02-13 00:44:51 +00:00
|
|
|
getExtension: function () {
|
|
|
|
return 'png';
|
|
|
|
}
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
canvas.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
canvas.set = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'Placeholder shown' );
|
2014-02-26 20:09:37 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $image = $( '<img>' ).width( 100 ).height( 50 );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const blurredThumbnailShown = canvas.maybeDisplayPlaceholder(
|
2015-01-23 12:48:27 +00:00
|
|
|
{ width: 1000, height: 500 },
|
2014-02-26 20:09:37 +00:00
|
|
|
$image,
|
2015-01-23 12:48:27 +00:00
|
|
|
{ cssWidth: 1200, cssHeight: 600 }
|
2014-02-26 20:09:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual( $image.width(), 1000, 'Placeholder has the right width' );
|
|
|
|
assert.strictEqual( $image.height(), 500, 'Placeholder has the right height' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $image.hasClass( 'blurred' ), true, 'Placeholder is blurred' );
|
|
|
|
assert.strictEqual( blurredThumbnailShown, true, 'Placeholder state is correct' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: placeholder too small to be displayed, actual image bigger than the lightbox', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const imageRawMetadata = new LightboxImage( 'foo.png' );
|
|
|
|
const canvas = new Canvas( $qf );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2014-03-04 01:58:27 +00:00
|
|
|
imageRawMetadata.filePageTitle = {
|
2024-02-13 00:44:51 +00:00
|
|
|
getExtension: function () {
|
|
|
|
return 'png';
|
|
|
|
}
|
2014-03-04 01:58:27 +00:00
|
|
|
};
|
|
|
|
canvas.imageRawMetadata = imageRawMetadata;
|
|
|
|
|
2014-02-26 20:09:37 +00:00
|
|
|
canvas.set = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'Placeholder shown when it should not' );
|
2014-02-26 20:09:37 +00:00
|
|
|
};
|
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const $image = $( '<img>' ).width( 10 ).height( 5 );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2023-10-24 09:53:23 +00:00
|
|
|
const blurredThumbnailShown = canvas.maybeDisplayPlaceholder(
|
2015-01-23 12:48:27 +00:00
|
|
|
{ width: 1000, height: 500 },
|
2014-02-26 20:09:37 +00:00
|
|
|
$image,
|
2015-01-23 12:48:27 +00:00
|
|
|
{ cssWidth: 300, cssHeight: 150 }
|
2014-02-26 20:09:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual( $image.width(), 10, 'Placeholder has the right width' );
|
|
|
|
assert.strictEqual( $image.height(), 5, 'Placeholder has the right height' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( $image.hasClass( 'blurred' ), false, 'Placeholder is not blurred' );
|
|
|
|
assert.strictEqual( blurredThumbnailShown, false, 'Placeholder state is correct' );
|
2014-02-26 20:09:37 +00:00
|
|
|
} );
|
|
|
|
|
2019-01-03 13:47:16 +00:00
|
|
|
QUnit.test( 'unblurWithAnimation', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $qf = $( '#qunit-fixture' );
|
|
|
|
const canvas = new Canvas( $qf );
|
|
|
|
const oldAnimate = $.fn.animate;
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
$.fn.animate = function ( target, options ) {
|
|
|
|
if ( options ) {
|
|
|
|
if ( options.step ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
for ( const key in target ) {
|
2019-01-03 13:47:16 +00:00
|
|
|
options.step.call( this, target[ key ] /* , tween object */ );
|
|
|
|
}
|
2014-02-26 20:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( options.complete ) {
|
|
|
|
options.complete.call( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
canvas.$image = $( '<img>' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
Refactor progressbar & blur handling
This tries to fix a number of related issues:
* the blurred thumbnail was visible for a split-second sometimes
when switching back to an already-loaded image. (Presumably when
JS was sluggish enough to take more than 10 ms to execute.) We
now check whether the promise is pending before showing a placeholder.
(More generally, a lot of unnecessary logic was executed when paging
through already loaded images, like displaying the placeholder, so
this might make the UI a bit more responsive.)
* the blur could get stuck sometimes - I have seen this a few times,
but have never been able to reproduce it, so I'm only guessing, but
maybe the timing was really unfortunate, and we switched back less
than 10 ms before loading finished. We now remove the blur on every
branch, just to be sure.
* adding a progress handler to a promise might not have any immediate
effect, so when switching to an image which was loading, the progress
bar reacted too late. We now store the progress state per thumbnail
so it is always available immediately.
* the progress would animate from 0 to its actual state whenever we
navigated to the image. The change on paging is now instant; the
progress bar only animates when we are looking at it.
* switching quickly back and forthe between a loaded and a loading
image resulted in the loading image becoming unblurred. This seems
fixed now, I'm not sure why. Maybe the "skip on non-pending promise"
logic affects it somehow.
Also removes some unused things / renames some things which were
confusing, and makes an unrelated fix in the image provider, which kept
amassing fail handlers.
Change-Id: I580becff246f197ec1bc65e82acd422620e35578
Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/489
2014-04-25 22:26:34 +00:00
|
|
|
canvas.unblurWithAnimation();
|
2014-02-26 20:09:37 +00:00
|
|
|
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( !canvas.$image.css( '-webkit-filter' ) || !canvas.$image.css( '-webkit-filter' ).length,
|
2014-05-14 10:02:39 +00:00
|
|
|
'Image has no -webkit-filter left' );
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( !canvas.$image.css( 'filter' ) || !canvas.$image.css( 'filter' ).length || canvas.$image.css( 'filter' ) === 'none',
|
2014-02-26 20:09:37 +00:00
|
|
|
'Image has no filter left' );
|
|
|
|
assert.strictEqual( parseInt( canvas.$image.css( 'opacity' ), 10 ), 1,
|
|
|
|
'Image is fully opaque' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( canvas.$image.hasClass( 'blurred' ), false, 'Image has no "blurred" class' );
|
2014-02-26 20:09:37 +00:00
|
|
|
|
|
|
|
$.fn.animate = oldAnimate;
|
|
|
|
} );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|