Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/provider/mmv.provider.GuessedThumbnailInfo.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 { GuessedThumbnailInfo } = require( 'mmv' );
19
20 ( function () {
21 QUnit.module( 'mmv.provider.GuessedThumbnailInfo', QUnit.newMwEnvironment() );
22
23 QUnit.test( 'Constructor sense check', ( assert ) => {
24 const provider = new GuessedThumbnailInfo();
25 assert.true( provider instanceof GuessedThumbnailInfo, 'Constructor call successful' );
26 } );
27
28 QUnit.test( 'get()', ( assert ) => {
29 const provider = new GuessedThumbnailInfo();
30 const file = new mw.Title( 'File:Copyleft.svg' );
31 const sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/180px-Copyleft.svg.png';
32 const width = 300;
33 const originalWidth = 512;
34 const originalHeight = 512;
35 const resultUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png';
36 const done = assert.async();
37
38 provider.getUrl = () => resultUrl;
39 let result = provider.get( file, sampleUrl, width, originalWidth, originalHeight );
40 assert.true( typeof result.then === 'function', 'Result is a promise' );
41 assert.strictEqual( result.state(), 'resolved', 'Result is resolved' );
42 result.then( ( thumbnailInfo ) => {
43 assert.true( typeof thumbnailInfo.width === 'number', 'Width is set' );
44 assert.true( typeof thumbnailInfo.height === 'number', 'Height is set' );
45 assert.strictEqual( thumbnailInfo.url, resultUrl, 'URL is set' );
46 done();
47 } );
48
49 provider.getUrl = () => undefined;
50 result = provider.get( file, sampleUrl, width, originalWidth, originalHeight );
51 assert.true( typeof result.then === 'function', 'Result is a promise' );
52 assert.strictEqual( result.state(), 'rejected', 'Result is rejected' );
53 } );
54
55 QUnit.test( 'getUrl()', ( assert ) => {
56 const provider = new GuessedThumbnailInfo();
57 let file = new mw.Title( 'File:Elizabeth_I_George_Gower.jpg' );
58 let originalWidth = 922;
59 let originalHeight = 968;
60
61 let sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/7/78/Elizabeth_I_George_Gower.jpg';
62 let width = 1000;
63 let expectedUrl = 'http://upload.wikimedia.org/wikipedia/commons/7/78/Elizabeth_I_George_Gower.jpg';
64 let resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
65 assert.strictEqual( resultUrl, expectedUrl, 'Simple case - full image, needs no resize' );
66
67 sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Elizabeth_I_George_Gower.jpg/180px-Elizabeth_I_George_Gower.jpg';
68 width = 400;
69 expectedUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Elizabeth_I_George_Gower.jpg/400px-Elizabeth_I_George_Gower.jpg';
70 resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
71 assert.strictEqual( resultUrl, expectedUrl, 'Mostly simple case - just need to replace size' );
72
73 sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/7/78/Elizabeth_I_George_Gower.jpg';
74 width = 400;
75 expectedUrl = undefined;
76 resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
77 assert.strictEqual( resultUrl, expectedUrl, 'We bail on hard case - full to thumbnail' );
78
79 sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/78/Elizabeth_I_George_Gower.jpg/180px-Elizabeth_I_George_Gower.jpg';
80 width = 1000;
81 expectedUrl = 'http://upload.wikimedia.org/wikipedia/commons/7/78/Elizabeth_I_George_Gower.jpg';
82 resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
83 assert.strictEqual( resultUrl, expectedUrl, 'Thumbnail to full-size, image with limited size' );
84
85 file = new mw.Title( 'File:Ranunculus_gmelinii_NRCS-2.tiff' );
86 sampleUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Ranunculus_gmelinii_NRCS-2.tiff/lossy-page1-428px-Ranunculus_gmelinii_NRCS-2.tiff.jpg';
87 width = 2000;
88 originalWidth = 1500;
89 originalHeight = 2100;
90 expectedUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Ranunculus_gmelinii_NRCS-2.tiff/lossy-page1-1500px-Ranunculus_gmelinii_NRCS-2.tiff.jpg';
91 resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
92 assert.strictEqual( resultUrl, expectedUrl, 'Thumbnail to full-size, image which cannot be displayed directly' );
93
94 file = new mw.Title( 'File:Copyleft.svg' );
95 sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/180px-Copyleft.svg.png';
96 width = 1000;
97 originalWidth = 512;
98 originalHeight = 512;
99 expectedUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/1000px-Copyleft.svg.png';
100 resultUrl = provider.getUrl( file, sampleUrl, width, originalWidth, originalHeight );
101 assert.strictEqual( resultUrl, expectedUrl, 'Thumbnail to "full-size", image with unlimited size' );
102 } );
103
104 QUnit.test( 'needsOriginal()', ( assert ) => {
105 const provider = new GuessedThumbnailInfo();
106 let file = new mw.Title( 'File:Copyleft.svg' );
107
108 assert.strictEqual( provider.needsOriginal( file, 100, 1000 ), false, 'Thumbnail of an SVG smaller than the original size doesn\'t need original' );
109 assert.strictEqual( provider.needsOriginal( file, 1000, 1000 ), false, 'Thumbnail of an SVG equal to the original size doesn\'t need original' );
110 assert.strictEqual( provider.needsOriginal( file, 2000, 1000 ), false, 'Thumbnail of an SVG bigger than the original size doesn\'t need original' );
111
112 file = new mw.Title( 'File:Foo.png' );
113
114 assert.strictEqual( provider.needsOriginal( file, 100, 1000 ), false, 'Thumbnail of a PNG smaller than the original size doesn\'t need original' );
115 assert.strictEqual( provider.needsOriginal( file, 1000, 1000 ), true, 'Thumbnail of a PNG equal to the original size needs original' );
116 assert.strictEqual( provider.needsOriginal( file, 2000, 1000 ), true, 'Thumbnail of a PNG bigger than the original size needs original' );
117 } );
118
119 QUnit.test( 'isFullSizeUrl()', ( assert ) => {
120 const provider = new GuessedThumbnailInfo();
121 const file = new mw.Title( 'File:Copyleft.svg' );
122
123 assert.strictEqual( provider.isFullSizeUrl( 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', file ),
124 false,
125 'Thumbnail url recognized as not being full size' );
126 assert.strictEqual( provider.isFullSizeUrl( 'http://upload.wikimedia.org/wikipedia/commons/8/8b/Copyleft.svg', file ),
127 true,
128 'Original url recognized as being full size' );
129 } );
130
131 QUnit.test( 'obscureFilename()', ( assert ) => {
132 const provider = new GuessedThumbnailInfo();
133 let file = new mw.Title( 'File:Copyleft.svg' );
134
135 assert.strictEqual( provider.obscureFilename( 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', file ),
136 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/<filename>/300px-<filename>.png', 'Filename correctly obscured' );
137
138 file = new mw.Title( 'File:Hoag\'s_object.jpg' );
139
140 assert.strictEqual( provider.obscureFilename( 'http://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Hoag%27s_object.jpg/180px-Hoag%27s_object.jpg', file ),
141 'http://upload.wikimedia.org/wikipedia/commons/thumb/d/da/<filename>/180px-<filename>', 'Filename with urlencoded character correctly obscured' );
142 } );
143
144 QUnit.test( 'restoreFilename()', ( assert ) => {
145 const provider = new GuessedThumbnailInfo();
146 const file = new mw.Title( 'File:Copyleft.svg' );
147
148 assert.strictEqual( provider.restoreFilename( 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/<filename>/300px-<filename>.png', file ),
149 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', 'Filename correctly restored' );
150
151 } );
152
153 QUnit.test( 'canHaveLargerThumbnailThanOriginal()', ( assert ) => {
154 const provider = new GuessedThumbnailInfo();
155 let file = new mw.Title( 'File:Copyleft.svg' );
156
157 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), true, 'SVG can have a larger thumbnail than the original' );
158
159 file = new mw.Title( 'File:Foo.jpg' );
160
161 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), false, 'JPG can\'t have a larger thumbnail than the original' );
162
163 file = new mw.Title( 'File:Foo.png' );
164
165 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), false, 'PNG can\'t have a larger thumbnail than the original' );
166
167 file = new mw.Title( 'File:Foo.jpeg' );
168
169 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), false, 'JPEG can\'t have a larger thumbnail than the original' );
170
171 file = new mw.Title( 'File:Foo.tiff' );
172
173 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), false, 'TIFF can\'t have a larger thumbnail than the original' );
174
175 file = new mw.Title( 'File:Foo.gif' );
176
177 assert.strictEqual( provider.canHaveLargerThumbnailThanOriginal( file ), false, 'GIF can\'t have a larger thumbnail than the original' );
178 } );
179
180 QUnit.test( 'canBeDisplayedInBrowser()', ( assert ) => {
181 const provider = new GuessedThumbnailInfo();
182 let file = new mw.Title( 'File:Copyleft.svg' );
183
184 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), false, 'SVG can\'t be displayed as-is in the browser' );
185
186 file = new mw.Title( 'File:Foo.jpg' );
187
188 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), true, 'JPG can be displayed as-is in the browser' );
189
190 file = new mw.Title( 'File:Foo.png' );
191
192 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), true, 'PNG can be displayed as-is in the browser' );
193
194 file = new mw.Title( 'File:Foo.jpeg' );
195
196 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), true, 'JPEG can be displayed as-is in the browser' );
197
198 file = new mw.Title( 'File:Foo.tiff' );
199
200 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), false, 'TIFF can\'t be displayed as-is in the browser' );
201
202 file = new mw.Title( 'File:Foo.gif' );
203
204 assert.strictEqual( provider.canBeDisplayedInBrowser( file ), true, 'GIF can be displayed as-is in the browser' );
205 } );
206
207 QUnit.test( 'guessWidth()', ( assert ) => {
208 const provider = new GuessedThumbnailInfo();
209 let file = new mw.Title( 'File:Copyleft.svg' );
210
211 assert.strictEqual( provider.guessWidth( file, 100, 1000 ), 100, 'Width correctly guessed for SVG thumbnail smaller than the original' );
212 assert.strictEqual( provider.guessWidth( file, 2000, 1000 ), 2000, 'Width correctly guessed for SVG thumbnail bigger than the original' );
213
214 file = new mw.Title( 'File:Copyleft.jpg' );
215
216 assert.strictEqual( provider.guessWidth( file, 100, 1000 ), 100, 'Width correctly guessed for JPG thumbnail smaller than the original' );
217 assert.strictEqual( provider.guessWidth( file, 2000, 1000 ), 1000, 'Width correctly guessed for JPG thumbnail bigger than the original' );
218 } );
219
220 QUnit.test( 'guessHeight()', ( assert ) => {
221 const provider = new GuessedThumbnailInfo();
222 let file = new mw.Title( 'File:Copyleft.svg' );
223
224 assert.strictEqual( provider.guessHeight( file, 100, 1000, 500 ), 50, 'Height correctly guessed for SVG thumbnail smaller than the original' );
225 assert.strictEqual( provider.guessHeight( file, 2000, 1000, 500 ), 1000, 'Height correctly guessed for SVG thumbnail bigger than the original' );
226
227 file = new mw.Title( 'File:Copyleft.jpg' );
228
229 assert.strictEqual( provider.guessHeight( file, 100, 1000, 500 ), 50, 'Height correctly guessed for JPG thumbnail smaller than the original' );
230 assert.strictEqual( provider.guessHeight( file, 2000, 1000, 500 ), 500, 'Height correctly guessed for JPG thumbnail bigger than the original' );
231 } );
232
233 QUnit.test( 'replaceSize()', ( assert ) => {
234 const provider = new GuessedThumbnailInfo();
235 let file = new mw.Title( 'File:Copyleft.svg' );
236
237 assert.strictEqual( provider.replaceSize( file, 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', 220 ),
238 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/220px-Copyleft.svg.png', 'Incorrect size correctly replaced' );
239 assert.strictEqual( provider.replaceSize( file, 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', 300 ),
240 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png', 'Identical size correctly left the same' );
241 assert.strictEqual( provider.replaceSize( file, 'http://upload.wikimedia.org/wikipedia/commons/8/8b/Copyleft.svg', 220 ),
242 undefined, 'Returns undefined when it cannot handle the URL' );
243
244 file = new mw.Title( 'File:Copyleft-300px.svg' );
245 assert.strictEqual( provider.replaceSize( file, 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft-300px.svg/300px-Copyleft-300px.svg.png', 220 ),
246 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft-300px.svg/220px-Copyleft-300px.svg.png', 'Works with strange filename' );
247
248 file = new mw.Title( 'File:Ranunculus_gmelinii_NRCS-2.tiff' );
249 assert.strictEqual( provider.replaceSize( file, 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Ranunculus_gmelinii_NRCS-2.tiff/lossy-page1-428px-Ranunculus_gmelinii_NRCS-2.tiff.jpg', 220 ),
250 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/Ranunculus_gmelinii_NRCS-2.tiff/lossy-page1-220px-Ranunculus_gmelinii_NRCS-2.tiff.jpg', 'Works with extra parameters' );
251 } );
252
253 QUnit.test( 'guessFullUrl()', ( assert ) => {
254 const provider = new GuessedThumbnailInfo();
255 let file = new mw.Title( 'File:Copyleft.svg' );
256 let fullUrl = 'http://upload.wikimedia.org/wikipedia/commons/8/8b/Copyleft.svg';
257 let sampleUrl = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png';
258
259 let result = provider.guessFullUrl( file, sampleUrl );
260
261 assert.strictEqual( result, fullUrl, 'guessFullUrl returns correct full URL for SVG' );
262
263 file = new mw.Title( 'File:அணில்-3-தென்னையின்_வளர்நிலை.jpg' );
264 fullUrl = 'https://upload.wikimedia.org/wikipedia/commons/1/15/%E0%AE%85%E0%AE%A3%E0%AE%BF%E0%AE%B2%E0%AF%8D-3-%E0%AE%A4%E0%AF%86%E0%AE%A9%E0%AF%8D%E0%AE%A9%E0%AF%88%E0%AE%AF%E0%AE%BF%E0%AE%A9%E0%AF%8D_%E0%AE%B5%E0%AE%B3%E0%AE%B0%E0%AF%8D%E0%AE%A8%E0%AE%BF%E0%AE%B2%E0%AF%88.jpg';
265 sampleUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/%E0%AE%85%E0%AE%A3%E0%AE%BF%E0%AE%B2%E0%AF%8D-3-%E0%AE%A4%E0%AF%86%E0%AE%A9%E0%AF%8D%E0%AE%A9%E0%AF%88%E0%AE%AF%E0%AE%BF%E0%AE%A9%E0%AF%8D_%E0%AE%B5%E0%AE%B3%E0%AE%B0%E0%AF%8D%E0%AE%A8%E0%AE%BF%E0%AE%B2%E0%AF%88.jpg/800px-%E0%AE%85%E0%AE%A3%E0%AE%BF%E0%AE%B2%E0%AF%8D-3-%E0%AE%A4%E0%AF%86%E0%AE%A9%E0%AF%8D%E0%AE%A9%E0%AF%88%E0%AE%AF%E0%AE%BF%E0%AE%A9%E0%AF%8D_%E0%AE%B5%E0%AE%B3%E0%AE%B0%E0%AF%8D%E0%AE%A8%E0%AE%BF%E0%AE%B2%E0%AF%88.jpg';
266
267 result = provider.guessFullUrl( file, sampleUrl );
268
269 assert.strictEqual( result, fullUrl, 'guessFullUrl returns correct full URL for JPG with unicode name' );
270
271 file = new mw.Title( 'File:அணில்-3-தென்னையின்_வளர்நிலை.jpg' );
272 sampleUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/அணில்-3-தென்னையின்_வளர்நிலை.jpg/800px-அணில்-3-தென்னையின்_வளர்நிலை.jpg';
273
274 result = provider.guessFullUrl( file, sampleUrl );
275
276 assert.strictEqual( result, undefined, 'guessFullUrl bails out when URL encoding is not as expected' );
277 } );
278 }() );