| 1 |
const { MultimediaViewer } = require( 'mmv' ); |
| 2 |
const { getMultimediaViewer } = require( './mmv.testhelpers.js' ); |
| 3 |
const { MultimediaViewerBootstrap } = require( 'mmv.bootstrap' ); |
| 4 |
|
| 5 |
( function () { |
| 6 |
QUnit.module( 'mmv', QUnit.newMwEnvironment( { |
| 7 |
beforeEach: function () { |
| 8 |
|
| 9 |
this.sandbox.stub( require( 'mediawiki.router' ), 'back' ); |
| 10 |
} |
| 11 |
} ) ); |
| 12 |
|
| 13 |
QUnit.test( 'eachPreloadableLightboxIndex()', ( assert ) => { |
| 14 |
const viewer = getMultimediaViewer(); |
| 15 |
let expectedIndices; |
| 16 |
let i; |
| 17 |
|
| 18 |
viewer.preloadDistance = 3; |
| 19 |
viewer.thumbs = []; |
| 20 |
|
| 21 |
|
| 22 |
for ( i = 0; i < 11; i++ ) { |
| 23 |
viewer.thumbs.push( { image: false } ); |
| 24 |
} |
| 25 |
|
| 26 |
viewer.currentIndex = 2; |
| 27 |
i = 0; |
| 28 |
expectedIndices = [ 2, 3, 1, 4, 0, 5 ]; |
| 29 |
viewer.eachPreloadableLightboxIndex( ( index ) => { |
| 30 |
assert.strictEqual( index, expectedIndices[ i++ ], 'preload on left edge' ); |
| 31 |
} ); |
| 32 |
|
| 33 |
viewer.currentIndex = 9; |
| 34 |
i = 0; |
| 35 |
expectedIndices = [ 9, 10, 8, 7, 6 ]; |
| 36 |
viewer.eachPreloadableLightboxIndex( ( index ) => { |
| 37 |
assert.strictEqual( index, expectedIndices[ i++ ], 'preload on right edge' ); |
| 38 |
} ); |
| 39 |
} ); |
| 40 |
|
| 41 |
QUnit.test( 'Progress', function ( assert ) { |
| 42 |
const imageDeferred = $.Deferred(); |
| 43 |
const viewer = getMultimediaViewer(); |
| 44 |
const fakeImage = { |
| 45 |
filePageTitle: new mw.Title( 'File:Stuff.jpg' ) |
| 46 |
}; |
| 47 |
|
| 48 |
const clock = this.sandbox.useFakeTimers(); |
| 49 |
|
| 50 |
viewer.thumbs = []; |
| 51 |
viewer.displayPlaceholderThumbnail = function () {}; |
| 52 |
viewer.setImage = function () {}; |
| 53 |
viewer.scroll = function () {}; |
| 54 |
viewer.fetchSizeIndependentLightboxInfo = () => $.Deferred().resolve( {} ); |
| 55 |
viewer.ui = { |
| 56 |
setFileReuseData: function () {}, |
| 57 |
setupForLoad: function () {}, |
| 58 |
canvas: { set: function () {}, |
| 59 |
getCurrentImageWidths: function () { |
| 60 |
return { real: 0 }; |
| 61 |
}, |
| 62 |
getDimensions: function () { |
| 63 |
return {}; |
| 64 |
} |
| 65 |
}, |
| 66 |
panel: { |
| 67 |
setImageInfo: function () {}, |
| 68 |
scroller: { |
| 69 |
animateMetadataOnce: function () {} |
| 70 |
}, |
| 71 |
progressBar: { |
| 72 |
hide: this.sandbox.stub(), |
| 73 |
animateTo: this.sandbox.stub(), |
| 74 |
jumpTo: this.sandbox.stub() |
| 75 |
} |
| 76 |
}, |
| 77 |
open: function () {} }; |
| 78 |
|
| 79 |
viewer.imageProvider.get = () => imageDeferred.promise(); |
| 80 |
viewer.imageInfoProvider.get = () => $.Deferred().resolve( {} ); |
| 81 |
viewer.thumbnailInfoProvider.get = () => $.Deferred().resolve( {} ); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
viewer.loadImage( fakeImage ); |
| 86 |
clock.tick( 10 ); |
| 87 |
assert.strictEqual( viewer.ui.panel.progressBar.jumpTo.lastCall.calledWith( 0 ), true, |
| 88 |
'Percentage correctly reset by loadImage' ); |
| 89 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.firstCall.calledWith( 5 ), true, |
| 90 |
'Percentage correctly animated to 5 by loadImage' ); |
| 91 |
|
| 92 |
imageDeferred.notify( 'response', 45 ); |
| 93 |
clock.tick( 10 ); |
| 94 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.secondCall.calledWith( 45 ), true, |
| 95 |
'Percentage correctly funneled to panel UI' ); |
| 96 |
|
| 97 |
imageDeferred.resolve( {}, {} ); |
| 98 |
clock.tick( 10 ); |
| 99 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.thirdCall.calledWith( 100 ), true, |
| 100 |
'Percentage correctly funneled to panel UI' ); |
| 101 |
|
| 102 |
clock.restore(); |
| 103 |
|
| 104 |
viewer.close(); |
| 105 |
} ); |
| 106 |
|
| 107 |
QUnit.test( 'Progress when switching images', function ( assert ) { |
| 108 |
const firstImageDeferred = $.Deferred(); |
| 109 |
const secondImageDeferred = $.Deferred(); |
| 110 |
const firstImage = { |
| 111 |
index: 1, |
| 112 |
filePageTitle: new mw.Title( 'File:First.jpg' ) |
| 113 |
}; |
| 114 |
const secondImage = { |
| 115 |
index: 2, |
| 116 |
filePageTitle: new mw.Title( 'File:Second.jpg' ) |
| 117 |
}; |
| 118 |
const viewer = getMultimediaViewer(); |
| 119 |
|
| 120 |
const clock = this.sandbox.useFakeTimers(); |
| 121 |
|
| 122 |
viewer.thumbs = []; |
| 123 |
viewer.displayPlaceholderThumbnail = function () {}; |
| 124 |
viewer.setImage = function () {}; |
| 125 |
viewer.scroll = function () {}; |
| 126 |
viewer.preloadImagesMetadata = function () {}; |
| 127 |
viewer.preloadThumbnails = function () {}; |
| 128 |
viewer.fetchSizeIndependentLightboxInfo = () => $.Deferred().resolve( {} ); |
| 129 |
viewer.ui = { |
| 130 |
setFileReuseData: function () {}, |
| 131 |
setupForLoad: function () {}, |
| 132 |
canvas: { set: function () {}, |
| 133 |
getCurrentImageWidths: function () { |
| 134 |
return { real: 0 }; |
| 135 |
}, |
| 136 |
getDimensions: function () { |
| 137 |
return {}; |
| 138 |
} |
| 139 |
}, |
| 140 |
panel: { |
| 141 |
setImageInfo: function () {}, |
| 142 |
scroller: { |
| 143 |
animateMetadataOnce: function () {} |
| 144 |
}, |
| 145 |
progressBar: { |
| 146 |
hide: this.sandbox.stub(), |
| 147 |
animateTo: this.sandbox.stub(), |
| 148 |
jumpTo: this.sandbox.stub() |
| 149 |
} |
| 150 |
}, |
| 151 |
open: function () {}, |
| 152 |
empty: function () {} }; |
| 153 |
|
| 154 |
viewer.imageInfoProvider.get = () => $.Deferred().resolve( {} ); |
| 155 |
viewer.thumbnailInfoProvider.get = () => $.Deferred().resolve( {} ); |
| 156 |
|
| 157 |
|
| 158 |
viewer.imageProvider.get = this.sandbox.stub().returns( firstImageDeferred ); |
| 159 |
viewer.loadImage( firstImage ); |
| 160 |
clock.tick( 10 ); |
| 161 |
assert.strictEqual( viewer.ui.panel.progressBar.jumpTo.getCall( 0 ).calledWith( 0 ), true, |
| 162 |
'Percentage correctly reset for new first image' ); |
| 163 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.getCall( 0 ).calledWith( 5 ), true, |
| 164 |
'Percentage correctly animated to 5 for first new image' ); |
| 165 |
|
| 166 |
|
| 167 |
firstImageDeferred.notify( 'response', 20 ); |
| 168 |
clock.tick( 10 ); |
| 169 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.getCall( 1 ).calledWith( 20 ), true, |
| 170 |
'Percentage correctly animated when active image is loading' ); |
| 171 |
|
| 172 |
|
| 173 |
viewer.imageProvider.get = this.sandbox.stub().returns( secondImageDeferred ); |
| 174 |
viewer.loadImage( secondImage ); |
| 175 |
clock.tick( 10 ); |
| 176 |
assert.strictEqual( viewer.ui.panel.progressBar.jumpTo.getCall( 1 ).calledWith( 0 ), true, |
| 177 |
'Percentage correctly reset for second new image' ); |
| 178 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.getCall( 2 ).calledWith( 5 ), true, |
| 179 |
'Percentage correctly animated to 5 for second new image' ); |
| 180 |
|
| 181 |
|
| 182 |
secondImageDeferred.notify( 'response', 30 ); |
| 183 |
clock.tick( 10 ); |
| 184 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.getCall( 3 ).calledWith( 30 ), true, |
| 185 |
'Percentage correctly animated when active image is loading' ); |
| 186 |
|
| 187 |
|
| 188 |
firstImageDeferred.notify( 'response', 40 ); |
| 189 |
clock.tick( 10 ); |
| 190 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.callCount, 4, |
| 191 |
'Percentage not animated when inactive image is loading' ); |
| 192 |
|
| 193 |
|
| 194 |
secondImageDeferred.notify( 'response', 50 ); |
| 195 |
clock.tick( 10 ); |
| 196 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.getCall( 4 ).calledWith( 50 ), true, |
| 197 |
'Percentage correctly ignored inactive image & only animated when active image is loading' ); |
| 198 |
|
| 199 |
|
| 200 |
viewer.imageProvider.get = this.sandbox.stub().returns( firstImageDeferred ); |
| 201 |
viewer.loadImage( firstImage ); |
| 202 |
clock.tick( 10 ); |
| 203 |
assert.strictEqual( viewer.ui.panel.progressBar.jumpTo.getCall( 2 ).calledWith( 40 ), true, |
| 204 |
'Percentage jumps to right value when changing images' ); |
| 205 |
|
| 206 |
secondImageDeferred.resolve( {}, {} ); |
| 207 |
clock.tick( 10 ); |
| 208 |
assert.strictEqual( viewer.ui.panel.progressBar.hide.called, false, |
| 209 |
'Progress bar not hidden when something finishes in the background' ); |
| 210 |
|
| 211 |
|
| 212 |
viewer.imageProvider.get = this.sandbox.stub().returns( secondImageDeferred ); |
| 213 |
viewer.loadImage( secondImage ); |
| 214 |
clock.tick( 10 ); |
| 215 |
assert.strictEqual( viewer.ui.panel.progressBar.hide.called, true, |
| 216 |
'Progress bar hidden when switching to finished image' ); |
| 217 |
|
| 218 |
clock.restore(); |
| 219 |
|
| 220 |
viewer.close(); |
| 221 |
} ); |
| 222 |
|
| 223 |
QUnit.test( 'resetThumbnailStates', ( assert ) => { |
| 224 |
const viewer = getMultimediaViewer(); |
| 225 |
|
| 226 |
assert.strictEqual( viewer.realThumbnailShown, false, 'Real thumbnail state is correct' ); |
| 227 |
|
| 228 |
viewer.realThumbnailShown = true; |
| 229 |
|
| 230 |
viewer.resetThumbnailStates(); |
| 231 |
|
| 232 |
assert.strictEqual( viewer.realThumbnailShown, false, 'Real thumbnail state is correct' ); |
| 233 |
} ); |
| 234 |
|
| 235 |
QUnit.test( 'Placeholder first, then real thumbnail', function ( assert ) { |
| 236 |
const viewer = getMultimediaViewer(); |
| 237 |
|
| 238 |
viewer.setImage = function () {}; |
| 239 |
viewer.ui = { canvas: { |
| 240 |
maybeDisplayPlaceholder: function () { |
| 241 |
} |
| 242 |
} }; |
| 243 |
viewer.imageInfoProvider.get = this.sandbox.stub(); |
| 244 |
|
| 245 |
viewer.displayPlaceholderThumbnail( { originalWidth: 100, originalHeight: 100 }, undefined, undefined ); |
| 246 |
|
| 247 |
assert.strictEqual( viewer.realThumbnailShown, false, 'Real thumbnail state is correct' ); |
| 248 |
|
| 249 |
viewer.displayRealThumbnail( { url: undefined } ); |
| 250 |
|
| 251 |
assert.strictEqual( viewer.realThumbnailShown, true, 'Real thumbnail state is correct' ); |
| 252 |
} ); |
| 253 |
|
| 254 |
QUnit.test( 'Placeholder first, then real thumbnail - missing size', function ( assert ) { |
| 255 |
const viewer = getMultimediaViewer(); |
| 256 |
|
| 257 |
viewer.currentIndex = 1; |
| 258 |
viewer.setImage = function () {}; |
| 259 |
viewer.ui = { canvas: { |
| 260 |
maybeDisplayPlaceholder: function () { |
| 261 |
} |
| 262 |
} }; |
| 263 |
viewer.imageInfoProvider.get = this.sandbox.stub().returns( $.Deferred().resolve( { width: 100, height: 100 } ) ); |
| 264 |
|
| 265 |
viewer.displayPlaceholderThumbnail( { index: 1 }, undefined, undefined ); |
| 266 |
|
| 267 |
assert.strictEqual( viewer.realThumbnailShown, false, 'Real thumbnail state is correct' ); |
| 268 |
|
| 269 |
viewer.displayRealThumbnail( { url: undefined } ); |
| 270 |
|
| 271 |
assert.strictEqual( viewer.realThumbnailShown, true, 'Real thumbnail state is correct' ); |
| 272 |
} ); |
| 273 |
|
| 274 |
QUnit.test( 'Real thumbnail first, then placeholder', ( assert ) => { |
| 275 |
const viewer = getMultimediaViewer(); |
| 276 |
|
| 277 |
viewer.setImage = function () {}; |
| 278 |
viewer.ui = { |
| 279 |
showImage: function () {}, |
| 280 |
canvas: {} |
| 281 |
}; |
| 282 |
|
| 283 |
viewer.displayRealThumbnail( { url: undefined } ); |
| 284 |
|
| 285 |
assert.strictEqual( viewer.realThumbnailShown, true, 'Real thumbnail state is correct' ); |
| 286 |
|
| 287 |
viewer.displayPlaceholderThumbnail( {}, undefined, undefined ); |
| 288 |
|
| 289 |
assert.strictEqual( viewer.realThumbnailShown, true, 'Real thumbnail state is correct' ); |
| 290 |
} ); |
| 291 |
|
| 292 |
QUnit.test( 'New image loaded while another one is loading', function ( assert ) { |
| 293 |
const viewer = getMultimediaViewer(); |
| 294 |
const firstImageDeferred = $.Deferred(); |
| 295 |
const secondImageDeferred = $.Deferred(); |
| 296 |
const firstLigthboxInfoDeferred = $.Deferred(); |
| 297 |
const secondLigthboxInfoDeferred = $.Deferred(); |
| 298 |
const firstImage = { |
| 299 |
filePageTitle: new mw.Title( 'File:Foo.jpg' ), |
| 300 |
index: 0 |
| 301 |
}; |
| 302 |
const secondImage = { |
| 303 |
filePageTitle: new mw.Title( 'File:Bar.jpg' ), |
| 304 |
index: 1 |
| 305 |
}; |
| 306 |
|
| 307 |
const clock = this.sandbox.useFakeTimers(); |
| 308 |
|
| 309 |
viewer.fetchSizeIndependentLightboxInfo = this.sandbox.stub(); |
| 310 |
viewer.ui = { |
| 311 |
setFileReuseData: function () {}, |
| 312 |
setupForLoad: function () {}, |
| 313 |
canvas: { |
| 314 |
set: function () {}, |
| 315 |
getCurrentImageWidths: function () { |
| 316 |
return { real: 0 }; |
| 317 |
}, |
| 318 |
getDimensions: function () { |
| 319 |
return {}; |
| 320 |
} |
| 321 |
}, |
| 322 |
panel: { |
| 323 |
setImageInfo: this.sandbox.stub(), |
| 324 |
scroller: { |
| 325 |
animateMetadataOnce: function () {} |
| 326 |
}, |
| 327 |
progressBar: { |
| 328 |
hide: this.sandbox.stub(), |
| 329 |
animateTo: this.sandbox.stub(), |
| 330 |
jumpTo: this.sandbox.stub() |
| 331 |
}, |
| 332 |
empty: function () {} |
| 333 |
}, |
| 334 |
open: function () {}, |
| 335 |
empty: function () {} }; |
| 336 |
viewer.displayRealThumbnail = this.sandbox.stub(); |
| 337 |
viewer.eachPreloadableLightboxIndex = function () {}; |
| 338 |
viewer.animateMetadataDivOnce = this.sandbox.stub().returns( $.Deferred().reject() ); |
| 339 |
viewer.imageProvider.get = this.sandbox.stub(); |
| 340 |
viewer.imageInfoProvider.get = () => $.Deferred().reject( {} ); |
| 341 |
viewer.thumbnailInfoProvider.get = () => $.Deferred().resolve( {} ); |
| 342 |
|
| 343 |
viewer.imageProvider.get.returns( firstImageDeferred.promise() ); |
| 344 |
viewer.fetchSizeIndependentLightboxInfo.returns( firstLigthboxInfoDeferred.promise() ); |
| 345 |
viewer.loadImage( firstImage ); |
| 346 |
clock.tick( 10 ); |
| 347 |
assert.strictEqual( viewer.animateMetadataDivOnce.called, false, 'Metadata of the first image should not be animated' ); |
| 348 |
assert.strictEqual( viewer.ui.panel.setImageInfo.called, false, 'Metadata of the first image should not be shown' ); |
| 349 |
|
| 350 |
viewer.imageProvider.get.returns( secondImageDeferred.promise() ); |
| 351 |
viewer.fetchSizeIndependentLightboxInfo.returns( secondLigthboxInfoDeferred.promise() ); |
| 352 |
viewer.loadImage( secondImage ); |
| 353 |
clock.tick( 10 ); |
| 354 |
|
| 355 |
viewer.ui.panel.progressBar.animateTo.reset(); |
| 356 |
firstImageDeferred.notify( undefined, 45 ); |
| 357 |
clock.tick( 10 ); |
| 358 |
assert.strictEqual( viewer.ui.panel.progressBar.animateTo.reset.called, undefined, 'Progress of the first image should not be shown' ); |
| 359 |
|
| 360 |
firstImageDeferred.resolve( {}, {} ); |
| 361 |
firstLigthboxInfoDeferred.resolve( {} ); |
| 362 |
clock.tick( 10 ); |
| 363 |
assert.strictEqual( viewer.displayRealThumbnail.called, false, 'The first image being done loading should have no effect' ); |
| 364 |
|
| 365 |
viewer.displayRealThumbnail = this.sandbox.spy( () => viewer.close() ); |
| 366 |
secondImageDeferred.resolve( {}, {} ); |
| 367 |
secondLigthboxInfoDeferred.resolve( {} ); |
| 368 |
clock.tick( 10 ); |
| 369 |
assert.strictEqual( viewer.displayRealThumbnail.called, true, 'The second image being done loading should result in the image being shown' ); |
| 370 |
|
| 371 |
clock.restore(); |
| 372 |
} ); |
| 373 |
|
| 374 |
QUnit.test( 'Events are not trapped after the viewer is closed', function ( assert ) { |
| 375 |
const viewer = getMultimediaViewer(); |
| 376 |
const $document = $( document ); |
| 377 |
const $qf = $( '#qunit-fixture' ); |
| 378 |
const eventTypes = [ 'keydown', 'keyup', 'keypress', 'click', 'mousedown', 'mouseup' ]; |
| 379 |
const modifiers = [ undefined, 'altKey', 'ctrlKey', 'shiftKey', 'metaKey' ]; |
| 380 |
|
| 381 |
const done = assert.async(); |
| 382 |
const oldScrollTo = $.scrollTo; |
| 383 |
|
| 384 |
assert.expect( 0 ); |
| 385 |
|
| 386 |
|
| 387 |
this.sandbox.stub( $.fn, 'animate' ).returnsThis(); |
| 388 |
|
| 389 |
$.scrollTo = function () { |
| 390 |
return { scrollTop: () => {}, on: () => {}, off: () => {} }; |
| 391 |
}; |
| 392 |
|
| 393 |
viewer.setupEventHandlers(); |
| 394 |
|
| 395 |
viewer.imageProvider.get = () => $.Deferred().reject(); |
| 396 |
viewer.imageInfoProvider.get = () => $.Deferred().reject(); |
| 397 |
viewer.thumbnailInfoProvider.get = () => $.Deferred().reject(); |
| 398 |
|
| 399 |
viewer.initWithThumbs( [] ); |
| 400 |
|
| 401 |
viewer.loadImage( |
| 402 |
{ |
| 403 |
filePageTitle: new mw.Title( 'File:Stuff.jpg' ), |
| 404 |
thumbnail: new Image( 10, 10 ) |
| 405 |
}, |
| 406 |
new Image() |
| 407 |
); |
| 408 |
|
| 409 |
viewer.ui.$closeButton.trigger( 'click' ); |
| 410 |
|
| 411 |
function eventHandler( e ) { |
| 412 |
if ( e.isDefaultPrevented() ) { |
| 413 |
assert.true( false, 'Event was incorrectly trapped: ' + e.which ); |
| 414 |
} |
| 415 |
|
| 416 |
e.preventDefault(); |
| 417 |
|
| 418 |
|
| 419 |
if ( e.which === 32 && e.type === 'mouseup' ) { |
| 420 |
$document.off( '.mmvtest' ); |
| 421 |
viewer.cleanupEventHandlers(); |
| 422 |
$.scrollTo = oldScrollTo; |
| 423 |
done(); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
for ( let j = 0; j < eventTypes.length; j++ ) { |
| 428 |
$document.on( eventTypes[ j ] + '.mmvtest', eventHandler ); |
| 429 |
|
| 430 |
eventloop: |
| 431 |
for ( let i = 0; i < 256; i++ ) { |
| 432 |
|
| 433 |
if ( i > 32 ) { |
| 434 |
switch ( eventTypes[ j ] ) { |
| 435 |
case 'click': |
| 436 |
case 'mousedown': |
| 437 |
case 'mouseup': |
| 438 |
break eventloop; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
for ( let k = 0; k < modifiers.length; k++ ) { |
| 443 |
const eventParameters = { which: i }; |
| 444 |
if ( modifiers[ k ] !== undefined ) { |
| 445 |
eventParameters[ modifiers[ k ] ] = true; |
| 446 |
} |
| 447 |
$qf.trigger( $.Event( eventTypes[ j ], eventParameters ) ); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
} ); |
| 452 |
|
| 453 |
QUnit.test( 'Viewer is closed then navigating to #foo/bar/baz (page section including slash)', function ( assert ) { |
| 454 |
location.hash = '#/media/foo'; |
| 455 |
|
| 456 |
const viewer = getMultimediaViewer(); |
| 457 |
viewer.isOpen = true; |
| 458 |
viewer.ui = undefined; |
| 459 |
this.sandbox.stub( viewer, 'close' ); |
| 460 |
|
| 461 |
location.hash = '#foo/bar/baz'; |
| 462 |
|
| 463 |
|
| 464 |
const done = assert.async(); |
| 465 |
setTimeout( () => { |
| 466 |
assert.true( viewer.close.called, 'The viewer was closed' ); |
| 467 |
done(); |
| 468 |
}, 100 ); |
| 469 |
} ); |
| 470 |
|
| 471 |
QUnit.test( 'Refuse to load too-big thumbnails', ( assert ) => { |
| 472 |
let expectedWidth; |
| 473 |
const reuestedWidth = 1000; |
| 474 |
const originalWidth = 50; |
| 475 |
const viewer = getMultimediaViewer(); |
| 476 |
|
| 477 |
viewer.thumbnailInfoProvider.get = function ( fileTitle, sampleUrl, width ) { |
| 478 |
assert.strictEqual( width, expectedWidth ); |
| 479 |
return $.Deferred().reject(); |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
let title = mw.Title.newFromText( 'File:Foobar.png' ); |
| 484 |
expectedWidth = originalWidth; |
| 485 |
viewer.fetchThumbnail( title, reuestedWidth, null, originalWidth, 60 ); |
| 486 |
|
| 487 |
|
| 488 |
title = mw.Title.newFromText( 'File:Foobar.svg' ); |
| 489 |
expectedWidth = reuestedWidth; |
| 490 |
viewer.fetchThumbnail( title, reuestedWidth, null, originalWidth, 60 ); |
| 491 |
} ); |
| 492 |
|
| 493 |
QUnit.test( 'fetchThumbnail()', function ( assert ) { |
| 494 |
let guessedThumbnailInfoStub; |
| 495 |
let thumbnailInfoStub; |
| 496 |
let imageStub; |
| 497 |
let promise; |
| 498 |
let useThumbnailGuessing; |
| 499 |
const viewer = new MultimediaViewer( { |
| 500 |
language: function () {} |
| 501 |
} ); |
| 502 |
const sandbox = this.sandbox; |
| 503 |
const file = new mw.Title( 'File:Copyleft.svg' ); |
| 504 |
const sampleURL = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Copyleft.svg/300px-Copyleft.svg.png'; |
| 505 |
const width = 100; |
| 506 |
const originalWidth = 1000; |
| 507 |
const originalHeight = 1000; |
| 508 |
const image = {}; |
| 509 |
|
| 510 |
const clock = this.sandbox.useFakeTimers(); |
| 511 |
|
| 512 |
function setupStubs() { |
| 513 |
guessedThumbnailInfoStub = viewer.guessedThumbnailInfoProvider.get = sandbox.stub(); |
| 514 |
thumbnailInfoStub = viewer.thumbnailInfoProvider.get = sandbox.stub(); |
| 515 |
imageStub = viewer.imageProvider.get = sandbox.stub(); |
| 516 |
} |
| 517 |
|
| 518 |
useThumbnailGuessing = true; |
| 519 |
|
| 520 |
|
| 521 |
setupStubs(); |
| 522 |
guessedThumbnailInfoStub.returns( $.Deferred().resolve( { url: 'guessedURL' } ) ); |
| 523 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 524 |
imageStub.returns( $.Deferred().resolve( image ) ); |
| 525 |
promise = viewer.fetchThumbnail( file, width, '', originalWidth, originalHeight, useThumbnailGuessing ); |
| 526 |
clock.tick( 10 ); |
| 527 |
assert.strictEqual( guessedThumbnailInfoStub.called, false, 'When we lack sample URL and original dimensions, GuessedThumbnailInfoProvider is not called' ); |
| 528 |
assert.strictEqual( thumbnailInfoStub.calledOnce, true, 'When we lack sample URL and original dimensions, ThumbnailInfoProvider is called once' ); |
| 529 |
assert.strictEqual( imageStub.calledOnce, true, 'When we lack sample URL and original dimensions, ImageProvider is called once' ); |
| 530 |
assert.strictEqual( imageStub.calledWith( 'apiURL' ), true, 'When we lack sample URL and original dimensions, ImageProvider is called with the API url' ); |
| 531 |
assert.strictEqual( promise.state(), 'resolved', 'When we lack sample URL and original dimensions, fetchThumbnail resolves' ); |
| 532 |
|
| 533 |
|
| 534 |
setupStubs(); |
| 535 |
guessedThumbnailInfoStub.returns( $.Deferred().reject() ); |
| 536 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 537 |
imageStub.returns( $.Deferred().resolve( image ) ); |
| 538 |
promise = viewer.fetchThumbnail( file, width, sampleURL, originalWidth, originalHeight, useThumbnailGuessing ); |
| 539 |
clock.tick( 10 ); |
| 540 |
assert.strictEqual( guessedThumbnailInfoStub.calledOnce, true, 'When the guesser bails out, GuessedThumbnailInfoProvider is called once' ); |
| 541 |
assert.strictEqual( thumbnailInfoStub.calledOnce, true, 'When the guesser bails out, ThumbnailInfoProvider is called once' ); |
| 542 |
assert.strictEqual( imageStub.calledOnce, true, 'When the guesser bails out, ImageProvider is called once' ); |
| 543 |
assert.strictEqual( imageStub.calledWith( 'apiURL' ), true, 'When the guesser bails out, ImageProvider is called with the API url' ); |
| 544 |
assert.strictEqual( promise.state(), 'resolved', 'When the guesser bails out, fetchThumbnail resolves' ); |
| 545 |
|
| 546 |
|
| 547 |
setupStubs(); |
| 548 |
guessedThumbnailInfoStub.returns( $.Deferred().resolve( { url: 'guessedURL' } ) ); |
| 549 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 550 |
imageStub.returns( $.Deferred().resolve( image ) ); |
| 551 |
promise = viewer.fetchThumbnail( file, width, sampleURL, originalWidth, originalHeight, useThumbnailGuessing ); |
| 552 |
clock.tick( 10 ); |
| 553 |
assert.strictEqual( guessedThumbnailInfoStub.calledOnce, true, 'When the guesser returns an URL, GuessedThumbnailInfoProvider is called once' ); |
| 554 |
assert.strictEqual( thumbnailInfoStub.called, false, 'When the guesser returns an URL, ThumbnailInfoProvider is not called' ); |
| 555 |
assert.strictEqual( imageStub.calledOnce, true, 'When the guesser returns an URL, ImageProvider is called once' ); |
| 556 |
assert.strictEqual( imageStub.calledWith( 'guessedURL' ), true, 'When the guesser returns an URL, ImageProvider is called with the guessed url' ); |
| 557 |
assert.strictEqual( promise.state(), 'resolved', 'When the guesser returns an URL, fetchThumbnail resolves' ); |
| 558 |
|
| 559 |
|
| 560 |
setupStubs(); |
| 561 |
guessedThumbnailInfoStub.returns( $.Deferred().resolve( { url: 'guessedURL' } ) ); |
| 562 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 563 |
imageStub.withArgs( 'guessedURL' ).returns( $.Deferred().reject() ); |
| 564 |
imageStub.withArgs( 'apiURL' ).returns( $.Deferred().resolve( image ) ); |
| 565 |
promise = viewer.fetchThumbnail( file, width, sampleURL, originalWidth, originalHeight, useThumbnailGuessing ); |
| 566 |
clock.tick( 10 ); |
| 567 |
assert.strictEqual( guessedThumbnailInfoStub.calledOnce, true, 'When the guesser returns an URL, but that returns 404, GuessedThumbnailInfoProvider is called once' ); |
| 568 |
assert.strictEqual( thumbnailInfoStub.calledOnce, true, 'When the guesser returns an URL, but that returns 404, ThumbnailInfoProvider is called once' ); |
| 569 |
assert.strictEqual( imageStub.calledTwice, true, 'When the guesser returns an URL, but that returns 404, ImageProvider is called twice' ); |
| 570 |
assert.strictEqual( imageStub.getCall( 0 ).calledWith( 'guessedURL' ), true, 'When the guesser returns an URL, but that returns 404, ImageProvider is called first with the guessed url' ); |
| 571 |
assert.strictEqual( imageStub.getCall( 1 ).calledWith( 'apiURL' ), true, 'When the guesser returns an URL, but that returns 404, ImageProvider is called second with the guessed url' ); |
| 572 |
assert.strictEqual( promise.state(), 'resolved', 'When the guesser returns an URL, but that returns 404, fetchThumbnail resolves' ); |
| 573 |
|
| 574 |
|
| 575 |
setupStubs(); |
| 576 |
guessedThumbnailInfoStub.returns( $.Deferred().resolve( { url: 'guessedURL' } ) ); |
| 577 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 578 |
imageStub.withArgs( 'guessedURL' ).returns( $.Deferred().reject() ); |
| 579 |
imageStub.withArgs( 'apiURL' ).returns( $.Deferred().reject() ); |
| 580 |
promise = viewer.fetchThumbnail( file, width, sampleURL, originalWidth, originalHeight, useThumbnailGuessing ); |
| 581 |
clock.tick( 10 ); |
| 582 |
assert.strictEqual( guessedThumbnailInfoStub.calledOnce, true, 'When even the retry fails, GuessedThumbnailInfoProvider is called once' ); |
| 583 |
assert.strictEqual( thumbnailInfoStub.calledOnce, true, 'When even the retry fails, ThumbnailInfoProvider is called once' ); |
| 584 |
assert.strictEqual( imageStub.calledTwice, true, 'When even the retry fails, ImageProvider is called twice' ); |
| 585 |
assert.strictEqual( imageStub.getCall( 0 ).calledWith( 'guessedURL' ), true, 'When even the retry fails, ImageProvider is called first with the guessed url' ); |
| 586 |
assert.strictEqual( imageStub.getCall( 1 ).calledWith( 'apiURL' ), true, 'When even the retry fails, ImageProvider is called second with the guessed url' ); |
| 587 |
assert.strictEqual( promise.state(), 'rejected', 'When even the retry fails, fetchThumbnail rejects' ); |
| 588 |
|
| 589 |
useThumbnailGuessing = false; |
| 590 |
|
| 591 |
|
| 592 |
setupStubs(); |
| 593 |
guessedThumbnailInfoStub.returns( $.Deferred().resolve( { url: 'guessedURL' } ) ); |
| 594 |
thumbnailInfoStub.returns( $.Deferred().resolve( { url: 'apiURL' } ) ); |
| 595 |
imageStub.returns( $.Deferred().resolve( image ) ); |
| 596 |
promise = viewer.fetchThumbnail( file, width, sampleURL, originalWidth, originalHeight, useThumbnailGuessing ); |
| 597 |
clock.tick( 10 ); |
| 598 |
assert.strictEqual( guessedThumbnailInfoStub.called, false, 'When guessing is disabled, GuessedThumbnailInfoProvider is not called' ); |
| 599 |
assert.strictEqual( thumbnailInfoStub.calledOnce, true, 'When guessing is disabled, ThumbnailInfoProvider is called once' ); |
| 600 |
assert.strictEqual( imageStub.calledOnce, true, 'When guessing is disabled, ImageProvider is called once' ); |
| 601 |
assert.strictEqual( imageStub.calledWith( 'apiURL' ), true, 'When guessing is disabled, ImageProvider is called with the API url' ); |
| 602 |
assert.strictEqual( promise.state(), 'resolved', 'When guessing is disabled, fetchThumbnail resolves' ); |
| 603 |
|
| 604 |
clock.restore(); |
| 605 |
} ); |
| 606 |
|
| 607 |
QUnit.test( 'document.title', function ( assert ) { |
| 608 |
const viewer = getMultimediaViewer(); |
| 609 |
const bootstrap = new MultimediaViewerBootstrap(); |
| 610 |
const title = new mw.Title( 'File:This_should_show_up_in_document_title.png' ); |
| 611 |
const oldDocumentTitle = document.title; |
| 612 |
|
| 613 |
this.sandbox.stub( mw.loader, 'using' ).returns( $.Deferred().resolve( viewer ) ); |
| 614 |
viewer.currentImage = { filePageTitle: title }; |
| 615 |
bootstrap.setupEventHandlers(); |
| 616 |
viewer.setTitle(); |
| 617 |
|
| 618 |
assert.notStrictEqual( document.title.match( title.getNameText() ), null, 'File name is visible in title' ); |
| 619 |
|
| 620 |
viewer.close(); |
| 621 |
bootstrap.cleanupEventHandlers(); |
| 622 |
|
| 623 |
assert.strictEqual( document.title, oldDocumentTitle, 'Original title restored after viewer is closed' ); |
| 624 |
} ); |
| 625 |
}() ); |