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-21 18:24:39 +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
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
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' );
|
2014-03-04 01:58:27 +00:00
|
|
|
} );
|
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: placeholder big enough to show, 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
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
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' );
|
|
|
|
} );
|
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
QUnit.test( 'maybeDisplayPlaceholder: big-enough placeholder to show, 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
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
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' );
|
|
|
|
} );
|
|
|
|
|
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
|
|
|
|
2024-05-15 18:40:29 +00:00
|
|
|
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-11-12 16:33:24 +00:00
|
|
|
}() );
|