Debian Codegraph @

extensions/MultimediaViewer/tests/qunit/mmv/mmv.bootstrap.test.js

raw symbols SCIP
1 const { MultimediaViewerBootstrap } = require( 'mmv.bootstrap' );
2 const { asyncMethod, waitForAsync, getMultimediaViewer } = require( './mmv.testhelpers.js' );
3
4 ( function () {
5 QUnit.module( 'mmv.bootstrap', QUnit.newMwEnvironment( {
6 // mw.Title relies on these three config vars
7 // Restore them after each test run
8 config: {
9 wgFormattedNamespaces: {
10 '-2': 'Media',
11 '-1': 'Special',
12 0: '',
13 1: 'Talk',
14 2: 'User',
15 3: 'User talk',
16 4: 'Wikipedia',
17 5: 'Wikipedia talk',
18 6: 'File',
19 7: 'File talk',
20 8: 'MediaWiki',
21 9: 'MediaWiki talk',
22 10: 'Template',
23 11: 'Template talk',
24 12: 'Help',
25 13: 'Help talk',
26 14: 'Category',
27 15: 'Category talk',
28 // testing custom / localized namespace
29 100: 'Penguins'
30 },
31 wgNamespaceIds: {
32 /* eslint-disable camelcase */
33 media: -2,
34 special: -1,
35 '': 0,
36 talk: 1,
37 user: 2,
38 user_talk: 3,
39 wikipedia: 4,
40 wikipedia_talk: 5,
41 file: 6,
42 file_talk: 7,
43 mediawiki: 8,
44 mediawiki_talk: 9,
45 template: 10,
46 template_talk: 11,
47 help: 12,
48 help_talk: 13,
49 category: 14,
50 category_talk: 15,
51 image: 6,
52 image_talk: 7,
53 project: 4,
54 project_talk: 5,
55 // Testing custom namespaces and aliases
56 penguins: 100,
57 antarctic_waterfowl: 100
58 /* eslint-enable camelcase */
59 },
60 wgCaseSensitiveNamespaces: []
61 },
62 beforeEach: function () {
63 mw.config.set( 'wgMediaViewer', true );
64 mw.config.set( 'wgMediaViewerOnClick', true );
65 this.sandbox.stub( mw.user, 'isAnon' ).returns( false );
66 }
67 } ) );
68
69 function createGallery( imageSrc, caption ) {
70 const $div = $( '<div>' ).addClass( 'gallery' ).appendTo( '#qunit-fixture' );
71 const $galleryBox = $( '<div>' ).addClass( 'gallerybox' ).appendTo( $div );
72 const $thumbwrap = $( '<div>' ).addClass( 'thumb' ).appendTo( $galleryBox );
73 const $link = $( '<a>' ).addClass( 'image' ).appendTo( $thumbwrap );
74
75 $( '<img>' ).attr( 'src', ( imageSrc || 'thumb.jpg' ) ).appendTo( $link );
76 $( '<div>' ).addClass( 'gallerytext' ).text( caption || 'Foobar' ).appendTo( $galleryBox );
77
78 return $div;
79 }
80
81 function createThumb( imageSrc, caption, alt ) {
82 const $div = $( '<div>' ).addClass( 'thumb' ).appendTo( '#qunit-fixture' );
83 const $link = $( '<a>' ).addClass( 'image' ).appendTo( $div );
84
85 $( '<div>' ).addClass( 'thumbcaption' ).appendTo( $div ).text( caption );
86 $( '<img>' ).attr( 'src', ( imageSrc || 'thumb.jpg' ) ).attr( 'alt', alt ).appendTo( $link );
87
88 return $div;
89 }
90
91 function createNormal( imageSrc, caption ) {
92 const $link = $( '<a>' ).prop( 'title', caption ).addClass( 'image' ).appendTo( '#qunit-fixture' );
93 $( '<img>' ).prop( 'src', ( imageSrc || 'thumb.jpg' ) ).appendTo( $link );
94 return $link;
95 }
96
97 function createMultipleImage( images ) {
98 const $contain = $( '<div>' ).addClass( 'thumb' );
99 const $thumbinner = $( '<div>' ).addClass( 'thumbinner' ).appendTo( $contain );
100 for ( let i = 0; i < images.length; ++i ) {
101 const $div = $( '<div>' ).appendTo( $thumbinner );
102 const $thumbimage = $( '<div>' ).addClass( 'thumbimage' ).appendTo( $div );
103 const $link = $( '<a>' ).addClass( 'image' ).appendTo( $thumbimage );
104 $( '<img>' ).prop( 'src', images[ i ][ 0 ] ).appendTo( $link );
105 $( '<div>' ).addClass( 'thumbcaption' ).text( images[ i ][ 1 ] ).appendTo( $div );
106 }
107 return $contain;
108 }
109
110 function createBootstrap( viewer ) {
111 const bootstrap = new MultimediaViewerBootstrap();
112
113 bootstrap.processThumbs( $( '#qunit-fixture' ) );
114
115 // MultimediaViewerBootstrap.ensureEventHandlersAreSetUp() is a weird workaround for gadget bugs.
116 // MediaViewer should work without it, and so should the tests.
117 bootstrap.ensureEventHandlersAreSetUp = function () {};
118
119 bootstrap.getViewer = function () {
120 return viewer || {
121 loadImageByTitle: function () {},
122 initWithThumbs: function () {},
123 hash: function () {},
124 router: { checkRoute: function () {} }
125 };
126 };
127
128 return bootstrap;
129 }
130
131 function hashTest( prefix, bootstrap, assert ) {
132 const hash = prefix + '/foo';
133 let callCount = 0;
134
135 bootstrap.loadViewer = function () {
136 callCount++;
137 return $.Deferred().reject();
138 };
139
140 // Hijack loadViewer, which will return a promise that we'll have to
141 // wait for if we want to see these tests through
142 asyncMethod( bootstrap, 'loadViewer' );
143
144 // invalid hash, should not trigger MMV load
145 location.hash = 'Foo';
146
147 // actual hash we want to test for, should trigger MMV load
148 // use setTimeout to add new hash change to end of the call stack,
149 // ensuring that event handlers for our previous change can execute
150 // without us interfering with another immediate change
151 setTimeout( () => {
152 location.hash = hash;
153 } );
154
155 return waitForAsync().then( () => {
156 assert.strictEqual( callCount, 1, 'Viewer should be loaded once' );
157 bootstrap.cleanupEventHandlers();
158 location.hash = '';
159 } );
160 }
161
162 QUnit.test( 'Promise does not hang on ResourceLoader errors', function ( assert ) {
163 const errorMessage = 'loading failed';
164 const done = assert.async();
165
166 this.sandbox.stub( mw.loader, 'using' )
167 .callsArgWith( 2, new Error( errorMessage, [ 'mmv' ] ) )
168 .withArgs( 'mediawiki.notification' ).returns( $.Deferred().reject() ); // needed for mw.notify
169
170 const bootstrap = createBootstrap();
171 this.sandbox.stub( bootstrap, 'setupOverlay' );
172 this.sandbox.stub( bootstrap, 'cleanupOverlay' );
173
174 bootstrap.loadViewer( true ).fail( ( message ) => {
175 assert.strictEqual( bootstrap.setupOverlay.called, true, 'Overlay was set up' );
176 assert.strictEqual( bootstrap.cleanupOverlay.called, true, 'Overlay was cleaned up' );
177 assert.strictEqual( message, errorMessage, 'promise is rejected with the error message when loading fails' );
178 done();
179 } );
180 } );
181
182 QUnit.test( 'Clicks are not captured once the loading fails', function ( assert ) {
183 const bootstrap = new MultimediaViewerBootstrap();
184 const clock = this.sandbox.useFakeTimers();
185
186 this.sandbox.stub( mw.loader, 'using' )
187 .callsArgWith( 2, new Error( 'loading failed', [ 'mmv' ] ) )
188 .withArgs( 'mediawiki.notification' ).returns( $.Deferred().reject() ); // needed for mw.notify
189 bootstrap.ensureEventHandlersAreSetUp = function () {};
190
191 // trigger first click, which will cause MMV to be loaded (which we've
192 // set up to fail)
193 const event = new $.Event( 'click', { button: 0, which: 1 } );
194 const returnValue = bootstrap.click( event, mw.Title.newFromText( 'Foo' ) );
195 clock.tick( 10 );
196 assert.true( event.isDefaultPrevented(), 'First click is caught' );
197 assert.strictEqual( returnValue, false, 'First click is caught' );
198 clock.restore();
199 } );
200
201 // FIXME: Tests suspended as they do not pass in QUnit 2.x+ – T192932
202 QUnit.skip( 'Check viewer invoked when clicking on valid image links', function ( assert ) {
203 // TODO: Is <div class="gallery"><span class="image"><img/></span></div> valid ???
204 const viewer = { initWithThumbs: function () {}, loadImageByTitle: this.sandbox.stub() };
205 const clock = this.sandbox.useFakeTimers();
206
207 // Create gallery with valid link image
208 const div = createGallery();
209 const $link = div.find( 'a.image' );
210
211 // Valid isolated thumbnail
212 const $link2 = $( '<a>' ).addClass( 'image' ).appendTo( '#qunit-fixture' );
213 $( '<img>' ).attr( 'src', 'thumb2.jpg' ).appendTo( $link2 );
214
215 // Non-valid fragment
216 const $link3 = $( '<a>' ).addClass( 'noImage' ).appendTo( div );
217 $( '<img>' ).attr( 'src', 'thumb3.jpg' ).appendTo( $link3 );
218
219 mw.config.set( 'wgTitle', 'Thumb4.jpg' );
220 mw.config.set( 'wgNamespaceNumber', 6 );
221 $( '<div>' ).addClass( 'fullMedia' ).appendTo( div );
222 $( '<img>' ).attr( 'src', 'thumb4.jpg' ).appendTo(
223 $( '<a>' )
224 .appendTo(
225 $( '<div>' )
226 .attr( 'id', 'file' )
227 .appendTo( '#qunit-fixture' )
228 )
229 );
230
231 // Create a new bootstrap object to trigger the DOM scan, etc.
232 const bootstrap = createBootstrap( viewer );
233 this.sandbox.stub( bootstrap, 'setupOverlay' );
234
235 const $link4 = $( '.fullMedia .mw-mmv-view-expanded' );
236 assert.ok( $link4.length, 'Link for viewing expanded file was set up.' );
237
238 // Click on valid link
239 $link.trigger( { type: 'click', which: 1 } );
240 clock.tick( 10 );
241 // FIXME: Actual bootstrap.setupOverlay.callCount: 2
242 assert.equal( bootstrap.setupOverlay.callCount, 1, 'setupOverlay called (1st click)' );
243 assert.equal( viewer.loadImageByTitle.callCount, 1, 'loadImageByTitle called (1st click)' );
244 this.sandbox.reset();
245
246 // Click on valid link
247 $link2.trigger( { type: 'click', which: 1 } );
248 clock.tick( 10 );
249 assert.equal( bootstrap.setupOverlay.callCount, 1, 'setupOverlay called (2nd click)' );
250 assert.equal( viewer.loadImageByTitle.callCount, 1, 'loadImageByTitle called (2nd click)' );
251 this.sandbox.reset();
252
253 // Click on valid link
254 $link4.trigger( { type: 'click', which: 1 } );
255 clock.tick( 10 );
256 assert.equal( bootstrap.setupOverlay.callCount, 1, 'setupOverlay called (3rd click)' );
257 assert.equal( viewer.loadImageByTitle.callCount, 1, 'loadImageByTitle called (3rd click)' );
258 this.sandbox.reset();
259
260 // Click on valid link even when preference says not to
261 mw.config.set( 'wgMediaViewerOnClick', false );
262 $link4.trigger( { type: 'click', which: 1 } );
263 clock.tick( 10 );
264 mw.config.set( 'wgMediaViewerOnClick', true );
265 assert.equal( bootstrap.setupOverlay.callCount, 1, 'setupOverlay called on-click with pref off' );
266 assert.equal( viewer.loadImageByTitle.callCount, 1, 'loadImageByTitle called on-click with pref off' );
267 this.sandbox.reset();
268
269 // @todo comment that above clicks should result in call, below clicks should not
270
271 // Click on non-valid link
272 $link3.trigger( { type: 'click', which: 1 } );
273 clock.tick( 10 );
274 assert.equal( bootstrap.setupOverlay.callCount, 0, 'setupOverlay not called on non-valid link click' );
275 assert.equal( viewer.loadImageByTitle.callCount, 0, 'loadImageByTitle not called on non-valid link click' );
276 this.sandbox.reset();
277
278 // Click on valid links with preference off
279 mw.config.set( 'wgMediaViewerOnClick', false );
280 $link.trigger( { type: 'click', which: 1 } );
281 $link2.trigger( { type: 'click', which: 1 } );
282 clock.tick( 10 );
283 assert.equal( bootstrap.setupOverlay.callCount, 0, 'setupOverlay not called on non-valid link click with pref off' );
284 assert.equal( viewer.loadImageByTitle.callCount, 0, 'loadImageByTitle not called on non-valid link click with pref off' );
285
286 clock.restore();
287 } );
288
289 QUnit.test( 'Skip images with invalid extensions', function ( assert ) {
290 const viewer = { initWithThumbs: function () {}, loadImageByTitle: this.sandbox.stub() };
291 const clock = this.sandbox.useFakeTimers();
292
293 // Create gallery with image that has invalid name extension
294 const div = createGallery( 'thumb.badext' );
295 const link = div.find( 'a.image' );
296
297 // Create a new bootstrap object to trigger the DOM scan, etc.
298 createBootstrap( viewer );
299
300 // Click on valid link with wrong image extension
301 link.trigger( { type: 'click', which: 1 } );
302 clock.tick( 10 );
303
304 assert.strictEqual( viewer.loadImageByTitle.called, false, 'Image should not be loaded' );
305
306 clock.restore();
307 } );
308
309 // FIXME: Tests suspended as they do not pass in QUnit 2.x+ – T192932
310 QUnit.skip( 'Accept only left clicks without modifier keys, skip the rest', function ( assert ) {
311 const viewer = { initWithThumbs: function () {}, loadImageByTitle: this.sandbox.stub() };
312 const clock = this.sandbox.useFakeTimers();
313
314 // Create gallery with image that has valid name extension
315 const $div = createGallery();
316
317 // Create a new bootstrap object to trigger the DOM scan, etc.
318 const bootstrap = createBootstrap( viewer );
319 this.sandbox.stub( bootstrap, 'setupOverlay' );
320
321 const $link = $div.find( 'a.image' );
322
323 // Handle valid left click, it should try to load the image
324 $link.trigger( { type: 'click', which: 1 } );
325 clock.tick( 10 );
326
327 // FIXME: Actual bootstrap.setupOverlay.callCount: 2
328 assert.equal( bootstrap.setupOverlay.callCount, 1, 'Left-click: Set up overlay' );
329 assert.equal( viewer.loadImageByTitle.callCount, 1, 'Left-click: Load image' );
330 this.sandbox.reset();
331
332 // Skip Ctrl-left-click, no image is loaded
333 $link.trigger( { type: 'click', which: 1, ctrlKey: true } );
334 clock.tick( 10 );
335 assert.equal( bootstrap.setupOverlay.callCount, 0, 'Ctrl-left-click: No overlay' );
336 assert.equal( viewer.loadImageByTitle.callCount, 0, 'Ctrl-left-click: No image load' );
337 this.sandbox.reset();
338
339 // Skip invalid right click, no image is loaded
340 $link.trigger( { type: 'click', which: 2 } );
341 clock.tick( 10 );
342 assert.equal( bootstrap.setupOverlay.callCount, 0, 'Right-click: No overlay' );
343 assert.equal( viewer.loadImageByTitle.callCount, 0, 'Right-click: Image was not loaded' );
344
345 clock.restore();
346 } );
347
348 QUnit.test( 'Ensure that the correct title is loaded when clicking', function ( assert ) {
349 const viewer = { initWithThumbs: function () {}, loadImageByTitle: this.sandbox.stub() };
350 const clock = this.sandbox.useFakeTimers();
351
352 // Create a new bootstrap object to trigger the DOM scan, etc.
353 const bootstrap = createBootstrap( viewer );
354 this.sandbox.stub( bootstrap, 'setupOverlay' );
355
356 bootstrap.route( 'File:Foo.jpg' );
357 clock.tick( 10 );
358 assert.true( bootstrap.setupOverlay.called, 'Overlay was set up' );
359 assert.strictEqual( viewer.loadImageByTitle.firstCall.args[ 0 ].getPrefixedDb(), 'File:Foo.jpg', 'Titles are identical' );
360
361 clock.tick( 10 );
362 clock.restore();
363 } );
364
365 // FIXME: Tests suspended as they do not pass in QUnit 2.x+ – T192932
366 QUnit.test( 'Validate new LightboxImage object has sensible constructor parameters', function ( assert ) {
367 const viewer = getMultimediaViewer();
368 const fname = 'valid.jpg';
369 const imgSrc = '/' + fname + '/300px-' + fname;
370 createThumb( imgSrc, 'Blah blah', 'meow' );
371
372 // Create a new bootstrap object to trigger the DOM scan, etc.
373 const bootstrap = createBootstrap( viewer );
374 this.sandbox.stub( bootstrap, 'setupOverlay' );
375 viewer.loadImage = function () {};
376 const done = assert.async();
377 bootstrap.loadViewer().then( () => {
378 assert.strictEqual( bootstrap.thumbs.length, 1, 'One thumbnail' );
379 /** @property {LightboxImage} */
380 const thumb = bootstrap.thumbs[ 0 ];
381 assert.true( new RegExp( imgSrc + '$' ).test( thumb.src ), 'Thumbnail URL used in creating new image object' );
382 assert.strictEqual( thumb.filePageTitle.title, fname, 'Filename is correct when passed into new image constructor' );
383 assert.strictEqual( thumb.index, 0, 'The only image we created in the gallery is set at index 0 in the images array' );
384 assert.strictEqual( thumb.caption, 'Blah blah', 'The caption passed in is correct' );
385 assert.strictEqual( thumb.alt, 'meow', 'The alt text passed in is correct' );
386 done();
387 } );
388 bootstrap.setupOverlay.reset();
389 } );
390
391 QUnit.test( 'Only load the viewer on a valid hash', ( assert ) => {
392 location.hash = '';
393
394 const bootstrap = createBootstrap();
395
396 return hashTest( '/media', bootstrap, assert );
397 } );
398
399 QUnit.test( 'Load the viewer on a legacy hash', ( assert ) => {
400 location.hash = '';
401
402 const bootstrap = createBootstrap();
403
404 return hashTest( 'mediaviewer', bootstrap, assert );
405 } );
406
407 QUnit.test( 'Overlay is set up on hash change', function ( assert ) {
408 location.hash = '#/media/foo';
409
410 const bootstrap = createBootstrap();
411 this.sandbox.stub( bootstrap, 'setupOverlay' );
412
413 bootstrap.hash();
414
415 assert.true( bootstrap.setupOverlay.called, 'Overlay is set up' );
416 } );
417
418 QUnit.test( 'Overlay is not set up on an irrelevant hash change', function ( assert ) {
419 location.hash = '#foo';
420
421 const bootstrap = createBootstrap();
422 this.sandbox.stub( bootstrap, 'setupOverlay' );
423 bootstrap.loadViewer();
424 bootstrap.setupOverlay.reset();
425
426 bootstrap.hash();
427
428 assert.strictEqual( bootstrap.setupOverlay.called, false, 'Overlay is not set up' );
429 } );
430
431 QUnit.test( 'Restoring article scroll position', function ( assert ) {
432 let stubbedScrollTop;
433 const bootstrap = createBootstrap();
434 const $window = $( window );
435 const done = assert.async();
436
437 this.sandbox.stub( $.fn, 'scrollTop', function ( scrollTop ) {
438 if ( scrollTop !== undefined ) {
439 stubbedScrollTop = scrollTop;
440 return this;
441 } else {
442 return stubbedScrollTop;
443 }
444 } );
445
446 $window.scrollTop( 50 );
447 bootstrap.setupOverlay();
448 // Calling this a second time because it can happen in history navigation context
449 bootstrap.setupOverlay();
450 // Clear scrollTop to check it is restored
451 $window.scrollTop( 0 );
452 bootstrap.cleanupOverlay();
453
454 // Scroll restoration is on a setTimeout
455 setTimeout( () => {
456 assert.strictEqual( $( window ).scrollTop(), 50, 'Scroll is correctly reset to original top position' );
457 done();
458 } );
459 } );
460
461 QUnit.test( 'Preload JS/CSS dependencies on thumb hover', function ( assert ) {
462 const clock = this.sandbox.useFakeTimers();
463 const viewer = { initWithThumbs: function () {} };
464
465 // Create gallery with image that has valid name extension
466 const $div = createThumb();
467
468 // Create a new bootstrap object to trigger the DOM scan, etc.
469 const bootstrap = createBootstrap( viewer );
470
471 this.sandbox.stub( mw.loader, 'load' );
472
473 $div.trigger( 'mouseenter' );
474 clock.tick( bootstrap.hoverWaitDuration - 50 );
475 $div.trigger( 'mouseleave' );
476
477 assert.strictEqual( mw.loader.load.called, false, 'Dependencies should not be preloaded if the thumb is not hovered long enough' );
478
479 $div.trigger( 'mouseenter' );
480 clock.tick( bootstrap.hoverWaitDuration + 50 );
481 $div.trigger( 'mouseleave' );
482
483 assert.strictEqual( mw.loader.load.called, true, 'Dependencies should be preloaded if the thumb is hovered long enough' );
484
485 clock.restore();
486 } );
487
488 QUnit.test( 'isAllowedThumb', ( assert ) => {
489 const $container = $( '<div>' );
490 const $thumb = $( '<img>' ).appendTo( $container );
491 const bootstrap = createBootstrap();
492
493 assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), true, 'Normal image in a div is allowed.' );
494
495 $container.addClass( 'metadata' );
496 assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a metadata container is disallowed.' );
497
498 $container.removeClass().addClass( 'noviewer' );
499 assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in a noviewer container is disallowed.' );
500
501 $container.removeClass().addClass( 'noarticletext' );
502 assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image in an empty article is disallowed.' );
503
504 $container.removeClass().addClass( 'noviewer' );
505 assert.strictEqual( bootstrap.isAllowedThumb( $thumb ), false, 'Image with a noviewer class is disallowed.' );
506 } );
507
508 QUnit.test( 'findCaption', ( assert ) => {
509 const gallery = createGallery( 'foo.jpg', 'Baz' );
510 const thumb = createThumb( 'foo.jpg', 'Quuuuux' );
511 const link = createNormal( 'foo.jpg', 'Foobar' );
512 const multiple = createMultipleImage( [
513 [ 'foo.jpg', 'Image #1' ],
514 [ 'bar.jpg', 'Image #2' ],
515 [ 'foobar.jpg', 'Image #3' ]
516 ] );
517 const bootstrap = createBootstrap();
518
519 assert.strictEqual( bootstrap.findCaption( gallery.find( '.thumb' ), gallery.find( 'a.image' ) ), 'Baz', 'A gallery caption is found.' );
520 assert.strictEqual( bootstrap.findCaption( thumb, thumb.find( 'a.image' ) ), 'Quuuuux', 'A thumbnail caption is found.' );
521 assert.strictEqual( bootstrap.findCaption( $(), link ), 'Foobar', 'The caption is found even if the image is not a thumbnail.' );
522 assert.strictEqual( bootstrap.findCaption( multiple, multiple.find( 'img[src="bar.jpg"]' ).closest( 'a' ) ), 'Image #2', 'The caption is found in {{Multiple image}}.' );
523 } );
524 }() );