Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.canvas.test.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { LightboxImage } = require( 'mmv.bootstrap' );
19 const { Canvas } = require( 'mmv' );
20
21 ( function () {
22 QUnit.module( 'mmv.ui.Canvas', QUnit.newMwEnvironment() );
23
24 QUnit.test( 'Constructor sense check', ( assert ) => {
25 const $qf = $( '#qunit-fixture' );
26 const canvas = new Canvas( $qf, $qf, $qf );
27
28 assert.strictEqual( canvas.$imageDiv.length, 1, 'Image container is created.' );
29 assert.strictEqual( canvas.$imageWrapper, $qf, '$imageWrapper is set correctly.' );
30 assert.strictEqual( canvas.$mainWrapper, $qf, '$mainWrapper is set correctly.' );
31 } );
32
33 QUnit.test( 'empty() and set()', ( assert ) => {
34 const $qf = $( '#qunit-fixture' );
35 const canvas = new Canvas( $qf );
36 const image = new Image();
37 const $imageElem = $( image );
38 const imageRawMetadata = new LightboxImage( 'foo.png' );
39
40 canvas.empty();
41
42 assert.strictEqual( canvas.$imageDiv.html(), '', 'Canvas is empty.' );
43 assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), true, 'Canvas is not visible.' );
44
45 canvas.set( imageRawMetadata, $imageElem );
46
47 assert.strictEqual( canvas.$image, $imageElem, 'Image element set correctly.' );
48 assert.strictEqual( canvas.imageRawMetadata, imageRawMetadata, 'Raw metadata set correctly.' );
49 assert.strictEqual( canvas.$imageDiv.html(), '<img>', 'Image added to container.' );
50 assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), false, 'Canvas is visible.' );
51
52 canvas.empty();
53
54 assert.strictEqual( canvas.$imageDiv.html(), '', 'Canvas is empty.' );
55 assert.strictEqual( canvas.$imageDiv.hasClass( 'empty' ), true, 'Canvas is not visible.' );
56 } );
57
58 QUnit.test( 'setImageAndMaxDimensions()', ( assert ) => {
59 const $qf = $( '#qunit-fixture' );
60 const $mainWrapper = $( '<div>' ).appendTo( $qf );
61 const $innerWrapper = $( '<div>' ).appendTo( $mainWrapper );
62 const $imageWrapper = $( '<div>' ).appendTo( $innerWrapper );
63 const canvas = new Canvas( $innerWrapper, $imageWrapper, $mainWrapper );
64 const imageRawMetadata = new LightboxImage( 'foo.png' );
65 const image = new Image();
66 const $imageElem = $( image );
67 const image2 = new Image();
68 let thumbnailWidth = 10;
69 const screenWidth = 100;
70
71 // Need to call set() before using setImageAndMaxDimensions()
72 canvas.set( imageRawMetadata, $imageElem );
73 const originalWidth = image.width;
74
75 // Call with the same image
76 canvas.setImageAndMaxDimensions(
77 { width: thumbnailWidth },
78 image,
79 { cssWidth: screenWidth }
80 );
81
82 assert.strictEqual( image.width, originalWidth, 'Image width was not modified.' );
83 assert.strictEqual( canvas.$image, $imageElem, 'Image element still set correctly.' );
84
85 const $currentImage = canvas.$image;
86
87 // Call with a new image bigger than screen size
88 thumbnailWidth = 150;
89 canvas.setImageAndMaxDimensions(
90 { width: thumbnailWidth },
91 image2,
92 { cssWidth: screenWidth }
93 );
94
95 assert.strictEqual( image2.width, screenWidth, 'Image width was trimmed correctly.' );
96 assert.notStrictEqual( canvas.$image, $currentImage, 'Image element switched correctly.' );
97 } );
98
99 QUnit.test( 'maybeDisplayPlaceholder: Constrained area for SVG files', ( assert ) => {
100 const $qf = $( '#qunit-fixture' );
101 const imageRawMetadata = new LightboxImage( 'foo.svg' );
102 const canvas = new Canvas( $qf );
103
104 imageRawMetadata.filePageTitle = {
105 getExtension: function () {
106 return 'svg';
107 }
108 };
109 canvas.imageRawMetadata = imageRawMetadata;
110
111 canvas.set = function () {
112 assert.true( false, 'Placeholder is not shown' );
113 };
114
115 const $image = $( '<img>' ).width( 10 ).height( 5 );
116
117 canvas.maybeDisplayPlaceholder(
118 { width: 200, height: 100 },
119 $image,
120 { cssWidth: 300, cssHeight: 150 }
121 );
122
123 assert.strictEqual( $image.width(), 10, 'Placeholder width was not set to max' );
124 assert.strictEqual( $image.height(), 5, 'Placeholder height was not set to max' );
125 } );
126
127 QUnit.test( 'maybeDisplayPlaceholder: placeholder big enough to show, actual image bigger than the lightbox', ( assert ) => {
128 const $qf = $( '#qunit-fixture' );
129 const imageRawMetadata = new LightboxImage( 'foo.png' );
130 const canvas = new Canvas( $qf );
131
132 imageRawMetadata.filePageTitle = {
133 getExtension: function () {
134 return 'png';
135 }
136 };
137 canvas.imageRawMetadata = imageRawMetadata;
138
139 canvas.set = function () {
140 assert.true( true, 'Placeholder shown' );
141 };
142
143 const $image = $( '<img>' ).width( 200 ).height( 100 );
144
145 canvas.maybeDisplayPlaceholder(
146 { width: 1000, height: 500 },
147 $image,
148 { cssWidth: 300, cssHeight: 150 }
149 );
150
151 assert.strictEqual( $image.width(), 300, 'Placeholder has the right width' );
152 assert.strictEqual( $image.height(), 150, 'Placeholder has the right height' );
153 } );
154
155 QUnit.test( 'maybeDisplayPlaceholder: big-enough placeholder to show, actual image smaller than the lightbox', ( assert ) => {
156 const $qf = $( '#qunit-fixture' );
157 const imageRawMetadata = new LightboxImage( 'foo.png' );
158 const canvas = new Canvas( $qf );
159
160 imageRawMetadata.filePageTitle = {
161 getExtension: function () {
162 return 'png';
163 }
164 };
165 canvas.imageRawMetadata = imageRawMetadata;
166
167 canvas.set = function () {
168 assert.true( true, 'Placeholder shown' );
169 };
170
171 const $image = $( '<img>' ).width( 100 ).height( 50 );
172
173 canvas.maybeDisplayPlaceholder(
174 { width: 1000, height: 500 },
175 $image,
176 { cssWidth: 1200, cssHeight: 600 }
177 );
178
179 assert.strictEqual( $image.width(), 1000, 'Placeholder has the right width' );
180 assert.strictEqual( $image.height(), 500, 'Placeholder has the right height' );
181 } );
182
183 QUnit.test( 'maybeDisplayPlaceholder: placeholder too small to be displayed, actual image bigger than the lightbox', ( assert ) => {
184 const $qf = $( '#qunit-fixture' );
185 const imageRawMetadata = new LightboxImage( 'foo.png' );
186 const canvas = new Canvas( $qf );
187
188 imageRawMetadata.filePageTitle = {
189 getExtension: function () {
190 return 'png';
191 }
192 };
193 canvas.imageRawMetadata = imageRawMetadata;
194
195 canvas.set = function () {
196 assert.true( false, 'Placeholder shown when it should not' );
197 };
198
199 const $image = $( '<img>' ).width( 10 ).height( 5 );
200
201 canvas.maybeDisplayPlaceholder(
202 { width: 1000, height: 500 },
203 $image,
204 { cssWidth: 300, cssHeight: 150 }
205 );
206
207 assert.strictEqual( $image.width(), 10, 'Placeholder has the right width' );
208 assert.strictEqual( $image.height(), 5, 'Placeholder has the right height' );
209 } );
210
211 }() );