2014-02-17 15:09:23 +00:00
( function ( mw , $ ) {
2014-04-15 21:59:39 +00:00
QUnit . module ( 'mmv.bootstrap' , QUnit . newMwEnvironment ( {
setup : function ( ) {
2014-06-10 23:29:50 +00:00
mw . config . set ( 'wgMediaViewer' , true ) ;
2014-04-15 21:59:39 +00:00
mw . config . set ( 'wgMediaViewerOnClick' , true ) ;
2015-01-23 12:48:27 +00:00
this . sandbox . stub ( mw . user , 'isAnon' ) . returns ( false ) ;
2014-06-10 23:29:50 +00:00
}
} ) ) ;
2014-02-17 15:09:23 +00:00
2014-04-25 23:16:48 +00:00
function createGallery ( imageSrc , caption ) {
2016-07-18 13:49:27 +00:00
var $div = $ ( '<div>' ) . addClass ( 'gallery' ) . appendTo ( '#qunit-fixture' ) ,
$galleryBox = $ ( '<div>' ) . addClass ( 'gallerybox' ) . appendTo ( $div ) ,
$thumbwrap = $ ( '<div>' ) . addClass ( 'thumb' ) . appendTo ( $galleryBox ) ,
$link = $ ( '<a>' ) . addClass ( 'image' ) . appendTo ( $thumbwrap ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 13:49:27 +00:00
$ ( '<img>' ) . attr ( 'src' , ( imageSrc || 'thumb.jpg' ) ) . appendTo ( $link ) ;
$ ( '<div>' ) . addClass ( 'gallerytext' ) . text ( caption || 'Foobar' ) . appendTo ( $galleryBox ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 13:49:27 +00:00
return $div ;
2014-02-17 15:09:23 +00:00
}
2014-12-30 05:10:27 +00:00
function createThumb ( imageSrc , caption , alt ) {
2016-07-18 13:49:27 +00:00
var $div = $ ( '<div>' ) . addClass ( 'thumb' ) . appendTo ( '#qunit-fixture' ) ,
$link = $ ( '<a>' ) . addClass ( 'image' ) . appendTo ( $div ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 13:49:27 +00:00
$ ( '<div>' ) . addClass ( 'thumbcaption' ) . appendTo ( $div ) . text ( caption ) ;
$ ( '<img>' ) . attr ( 'src' , ( imageSrc || 'thumb.jpg' ) ) . attr ( 'alt' , alt ) . appendTo ( $link ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 13:49:27 +00:00
return $div ;
2014-02-17 15:09:23 +00:00
}
2014-04-25 23:16:48 +00:00
function createNormal ( imageSrc , caption ) {
2016-07-18 13:49:27 +00:00
var $link = $ ( '<a>' ) . prop ( 'title' , caption ) . addClass ( 'image' ) . appendTo ( '#qunit-fixture' ) ;
$ ( '<img>' ) . prop ( 'src' , ( imageSrc || 'thumb.jpg' ) ) . appendTo ( $link ) ;
return $link ;
2014-04-25 23:16:48 +00:00
}
2014-12-30 01:26:17 +00:00
function createMultipleImage ( images ) {
2016-07-18 13:49:27 +00:00
var i , $div , $thumbimage , $link ,
$contain = $ ( '<div>' ) . addClass ( 'thumb' ) ,
$thumbinner = $ ( '<div>' ) . addClass ( 'thumbinner' ) . appendTo ( $contain ) ;
for ( i = 0 ; i < images . length ; ++ i ) {
$div = $ ( '<div>' ) . appendTo ( $thumbinner ) ;
$thumbimage = $ ( '<div>' ) . addClass ( 'thumbimage' ) . appendTo ( $div ) ;
$link = $ ( '<a>' ) . addClass ( 'image' ) . appendTo ( $thumbimage ) ;
$ ( '<img>' ) . prop ( 'src' , images [ i ] [ 0 ] ) . appendTo ( $link ) ;
$ ( '<div>' ) . addClass ( 'thumbcaption' ) . text ( images [ i ] [ 1 ] ) . appendTo ( $div ) ;
2014-12-30 01:26:17 +00:00
}
2016-07-18 13:49:27 +00:00
return $contain ;
2014-12-30 01:26:17 +00:00
}
2014-03-04 11:53:53 +00:00
function createBootstrap ( viewer ) {
var bootstrap = new mw . mmv . MultimediaViewerBootstrap ( ) ;
2014-09-12 17:13:42 +00:00
2016-07-18 10:00:46 +00:00
bootstrap . processThumbs ( $ ( '#qunit-fixture' ) ) ;
2014-09-12 17:13:42 +00:00
// MultimediaViewerBootstrap.ensureEventHandlersAreSetUp() is a weird workaround for gadget bugs.
// MediaViewer should work without it, and so should the tests.
bootstrap . ensureEventHandlersAreSetUp = $ . noop ;
2016-12-14 13:09:10 +00:00
bootstrap . getViewer = function ( ) {
return viewer || { initWithThumbs : $ . noop , hash : $ . noop } ;
} ;
2014-03-04 11:53:53 +00:00
return bootstrap ;
}
2015-02-25 14:55:37 +00:00
function hashTest ( prefix , bootstrap , assert ) {
2017-05-05 15:53:28 +00:00
var hash = prefix + '/foo' ,
callCount = 0 ;
2014-04-11 09:31:38 +00:00
bootstrap . loadViewer = function ( ) {
2017-05-05 15:53:28 +00:00
callCount ++ ;
2014-04-11 09:31:38 +00:00
return $ . Deferred ( ) . reject ( ) ;
} ;
2017-05-05 15:53:28 +00:00
// Hijack loadViewer, which will return a promise that we'll have to
// wait for if we want to see these tests through
mw . mmv . testHelpers . asyncMethod ( bootstrap , 'loadViewer' ) ;
bootstrap . setupEventHandlers ( ) ;
// invalid hash, should not trigger MMV load
2014-04-11 09:31:38 +00:00
window . location . hash = 'Foo' ;
2017-05-05 15:53:28 +00:00
// actual hash we want to test for, should trigger MMV load
// use setTimeout to add new hash change to end of the call stack,
// ensuring that event handlers for our previous change can execute
// without us interfering with another immediate change
setTimeout ( function ( ) {
window . location . hash = hash ;
} ) ;
return mw . mmv . testHelpers . waitForAsync ( ) . then ( function ( ) {
assert . ok ( callCount === 1 , 'Viewer should be loaded once' ) ;
2014-04-11 09:31:38 +00:00
bootstrap . cleanupEventHandlers ( ) ;
window . location . hash = '' ;
2017-05-05 15:53:28 +00:00
} ) ;
2014-04-11 09:31:38 +00:00
}
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Promise does not hang on ResourceLoader errors' , function ( assert ) {
2014-04-29 01:35:07 +00:00
var bootstrap ,
2017-05-05 15:53:28 +00:00
errorMessage = 'loading failed' ,
done = assert . async ( ) ;
2014-02-14 00:36:10 +00:00
2014-08-15 13:46:43 +00:00
this . sandbox . stub ( mw . loader , 'using' )
2016-07-18 13:49:27 +00:00
. callsArgWith ( 2 , new Error ( errorMessage , [ 'mmv' ] ) )
2014-08-15 13:46:43 +00:00
. withArgs ( 'mediawiki.notification' ) . returns ( $ . Deferred ( ) . reject ( ) ) ; // needed for mw.notify
2014-03-04 11:53:53 +00:00
bootstrap = createBootstrap ( ) ;
2017-05-05 15:53:28 +00:00
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
this . sandbox . stub ( bootstrap , 'cleanupOverlay' ) ;
2014-02-14 00:36:10 +00:00
2015-12-03 21:35:30 +00:00
bootstrap . loadViewer ( true ) . fail ( function ( message ) {
2017-05-05 15:53:28 +00:00
assert . ok ( bootstrap . setupOverlay . called , 'Overlay was set up' ) ;
assert . ok ( bootstrap . cleanupOverlay . called , 'Overlay was cleaned up' ) ;
2014-02-14 00:36:10 +00:00
assert . strictEqual ( message , errorMessage , 'promise is rejected with the error message when loading fails' ) ;
2017-05-05 15:53:28 +00:00
done ( ) ;
2014-02-14 00:36:10 +00:00
} ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Clicks are not captured once the loading fails' , function ( assert ) {
2014-04-29 01:35:07 +00:00
var event , returnValue ,
2017-05-05 15:53:28 +00:00
bootstrap = new mw . mmv . MultimediaViewerBootstrap ( ) ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-04-29 01:35:07 +00:00
2014-08-15 13:46:43 +00:00
this . sandbox . stub ( mw . loader , 'using' )
2016-07-18 13:49:27 +00:00
. callsArgWith ( 2 , new Error ( 'loading failed' , [ 'mmv' ] ) )
2014-08-15 13:46:43 +00:00
. withArgs ( 'mediawiki.notification' ) . returns ( $ . Deferred ( ) . reject ( ) ) ; // needed for mw.notify
2014-09-12 17:13:42 +00:00
bootstrap . ensureEventHandlersAreSetUp = $ . noop ;
2014-04-29 01:35:07 +00:00
2017-05-05 15:53:28 +00:00
// trigger first click, which will cause MMV to be loaded (which we've
// set up to fail)
2014-04-29 01:35:07 +00:00
event = new $ . Event ( 'click' , { button : 0 , which : 1 } ) ;
returnValue = bootstrap . click ( { } , event , 'foo' ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
2014-04-29 01:35:07 +00:00
assert . ok ( event . isDefaultPrevented ( ) , 'First click is caught' ) ;
assert . strictEqual ( returnValue , false , 'First click is caught' ) ;
2017-05-05 15:53:28 +00:00
// wait until MMW is loaded (or failed to load, in this case) before we
// trigger another click - which should then not be caught
2014-04-29 01:35:07 +00:00
event = new $ . Event ( 'click' , { button : 0 , which : 1 } ) ;
returnValue = bootstrap . click ( { } , event , 'foo' ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
2014-04-29 01:35:07 +00:00
assert . ok ( ! event . isDefaultPrevented ( ) , 'Click after loading failure is not caught' ) ;
assert . notStrictEqual ( returnValue , false , 'Click after loading failure is not caught' ) ;
2017-05-05 15:53:28 +00:00
clock . restore ( ) ;
2014-04-29 01:35:07 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Check viewer invoked when clicking on valid image links' , function ( assert ) {
2014-02-17 15:09:23 +00:00
// TODO: Is <div class="gallery"><span class="image"><img/></span></div> valid ???
2014-10-03 09:15:28 +00:00
var div , link , link2 , link3 , link4 , link5 , bootstrap ,
2017-05-05 15:53:28 +00:00
viewer = { initWithThumbs : $ . noop , loadImageByTitle : this . sandbox . stub ( ) } ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 10:39:29 +00:00
// Create gallery with valid link image
2014-02-17 15:09:23 +00:00
div = createGallery ( ) ;
link = div . find ( 'a.image' ) ;
2016-07-18 10:39:29 +00:00
// Valid isolated thumbnail
2014-02-17 15:09:23 +00:00
link2 = $ ( '<a>' ) . addClass ( 'image' ) . appendTo ( '#qunit-fixture' ) ;
2015-01-23 12:48:27 +00:00
$ ( '<img>' ) . attr ( 'src' , 'thumb2.jpg' ) . appendTo ( link2 ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 10:39:29 +00:00
// Non-valid fragment
2014-02-17 15:09:23 +00:00
link3 = $ ( '<a>' ) . addClass ( 'noImage' ) . appendTo ( div ) ;
2015-01-23 12:48:27 +00:00
$ ( '<img>' ) . attr ( 'src' , 'thumb3.jpg' ) . appendTo ( link3 ) ;
2014-02-17 15:09:23 +00:00
2014-04-08 21:01:07 +00:00
$ ( '<div>' ) . addClass ( 'fullMedia' ) . appendTo ( div ) ;
$ ( '<img>' ) . attr ( 'src' , 'thumb4.jpg' ) . appendTo (
$ ( '<a>' )
. appendTo (
$ ( '<div>' )
. attr ( 'id' , 'file' )
. appendTo ( '#qunit-fixture' )
)
) ;
2014-02-17 15:09:23 +00:00
// Create a new bootstrap object to trigger the DOM scan, etc.
2014-03-04 11:53:53 +00:00
bootstrap = createBootstrap ( viewer ) ;
2017-05-05 15:53:28 +00:00
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
2014-02-17 15:09:23 +00:00
2014-04-08 21:01:07 +00:00
link4 = $ ( '.fullMedia .mw-mmv-view-expanded' ) ;
assert . ok ( link4 . length , 'Link for viewing expanded file was set up.' ) ;
2014-10-03 09:15:28 +00:00
link5 = $ ( '.fullMedia .mw-mmv-view-config' ) ;
assert . ok ( link5 . length , 'Link for opening enable/disable configuration was set up.' ) ;
2016-07-18 10:39:29 +00:00
// Click on valid link
2015-01-23 12:48:27 +00:00
link . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 1 , 'setupOverlay was called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 1 , 'loadImageByTitle was called' ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 10:39:29 +00:00
// Click on valid link
2015-01-23 12:48:27 +00:00
link2 . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 2 , 'setupOverlay was called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 2 , 'loadImageByTitle was called' ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 10:39:29 +00:00
// Click on valid link
2014-04-08 21:01:07 +00:00
link4 . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 3 , 'setupOverlay was called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 3 , 'loadImageByTitle was called' ) ;
2014-04-08 21:01:07 +00:00
2016-07-18 10:39:29 +00:00
// Click on valid link even when preference says not to
2014-04-15 21:59:39 +00:00
mw . config . set ( 'wgMediaViewerOnClick' , false ) ;
link4 . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
2014-04-15 21:59:39 +00:00
mw . config . set ( 'wgMediaViewerOnClick' , true ) ;
2017-05-05 15:53:28 +00:00
assert . ok ( bootstrap . setupOverlay . callCount === 4 , 'setupOverlay was called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 4 , 'loadImageByTitle was called' ) ;
2014-04-15 21:59:39 +00:00
2017-05-05 15:53:28 +00:00
// @todo comment that above clicks should result in call, below clicks should not
2014-02-21 11:16:30 +00:00
2016-07-18 10:39:29 +00:00
// Click on non-valid link
2015-01-23 12:48:27 +00:00
link3 . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 4 , 'setupOverlay was not called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 4 , 'loadImageByTitle was not called' ) ;
2014-04-15 21:59:39 +00:00
2016-07-18 10:39:29 +00:00
// Click on valid links with preference off
2014-04-15 21:59:39 +00:00
mw . config . set ( 'wgMediaViewerOnClick' , false ) ;
2015-01-23 12:48:27 +00:00
link . trigger ( { type : 'click' , which : 1 } ) ;
link2 . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 4 , 'setupOverlay was not called' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 4 , 'loadImageByTitle was not called' ) ;
clock . restore ( ) ;
2014-02-17 15:09:23 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Skip images with invalid extensions' , function ( assert ) {
2016-12-14 13:09:10 +00:00
var div , link ,
2017-05-05 15:53:28 +00:00
viewer = { initWithThumbs : $ . noop , loadImageByTitle : this . sandbox . stub ( ) } ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-17 15:09:23 +00:00
// Create gallery with image that has invalid name extension
div = createGallery ( 'thumb.badext' ) ;
link = div . find ( 'a.image' ) ;
// Create a new bootstrap object to trigger the DOM scan, etc.
2016-12-14 13:09:10 +00:00
createBootstrap ( viewer ) ;
2014-02-17 15:09:23 +00:00
2016-07-18 10:39:29 +00:00
// Click on valid link with wrong image extension
2015-01-23 12:48:27 +00:00
link . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( ! viewer . loadImageByTitle . called , 'Image should not be loaded' ) ;
clock . restore ( ) ;
2014-02-17 15:09:23 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Accept only left clicks without modifier keys, skip the rest' , function ( assert ) {
2014-02-21 11:16:30 +00:00
var $div , $link , bootstrap ,
2017-05-05 15:53:28 +00:00
viewer = { initWithThumbs : $ . noop , loadImageByTitle : this . sandbox . stub ( ) } ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-17 15:09:23 +00:00
2014-04-24 13:47:59 +00:00
// Create gallery with image that has valid name extension
2014-02-17 15:09:23 +00:00
$div = createGallery ( ) ;
// Create a new bootstrap object to trigger the DOM scan, etc.
2014-03-04 11:53:53 +00:00
bootstrap = createBootstrap ( viewer ) ;
2017-05-05 15:53:28 +00:00
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
2014-02-17 15:09:23 +00:00
$link = $div . find ( 'a.image' ) ;
// Handle valid left click, it should try to load the image
2015-01-23 12:48:27 +00:00
$link . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 1 , 'Overlay was set up' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 1 , 'Image was loaded' ) ;
2014-02-21 11:16:30 +00:00
2014-02-17 15:09:23 +00:00
// Skip Ctrl-left-click, no image is loaded
2015-01-23 12:48:27 +00:00
$link . trigger ( { type : 'click' , which : 1 , ctrlKey : true } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 1 , 'Overlay was not set up' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 1 , 'Image was not loaded' ) ;
2014-02-17 15:09:23 +00:00
// Skip invalid right click, no image is loaded
2015-01-23 12:48:27 +00:00
$link . trigger ( { type : 'click' , which : 2 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . callCount === 1 , 'Overlay was not set up' ) ;
assert . ok ( viewer . loadImageByTitle . callCount === 1 , 'Image was not loaded' ) ;
clock . restore ( ) ;
2014-02-17 15:09:23 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Ensure that the correct title is loaded when clicking' , function ( assert ) {
2014-02-21 11:16:30 +00:00
var bootstrap ,
2017-05-05 15:53:28 +00:00
viewer = { initWithThumbs : $ . noop , loadImageByTitle : this . sandbox . stub ( ) } ,
2014-02-21 11:16:30 +00:00
$div = createGallery ( 'foo.jpg' ) ,
2017-05-05 15:53:28 +00:00
$link = $div . find ( 'a.image' ) ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-17 15:09:23 +00:00
2014-02-21 11:16:30 +00:00
// Create a new bootstrap object to trigger the DOM scan, etc.
2014-03-04 11:53:53 +00:00
bootstrap = createBootstrap ( viewer ) ;
2017-05-05 15:53:28 +00:00
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
2014-03-28 08:06:53 +00:00
2015-01-23 12:48:27 +00:00
$link . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . called , 'Overlay was set up' ) ;
assert . strictEqual ( viewer . loadImageByTitle . firstCall . args [ 0 ] . getPrefixedDb ( ) , 'File:Foo.jpg' , 'Titles are identical' ) ;
clock . restore ( ) ;
2014-02-17 15:09:23 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Validate new LightboxImage object has sane constructor parameters' , function ( assert ) {
2014-02-21 11:16:30 +00:00
var bootstrap ,
$div ,
$link ,
2016-04-03 09:18:26 +00:00
viewer = mw . mmv . testHelpers . getMultimediaViewer ( ) ,
2014-02-21 11:16:30 +00:00
fname = 'valid' ,
imgSrc = '/' + fname + '.jpg/300px-' + fname + '.jpg' ,
2017-05-05 15:53:28 +00:00
imgRegex = new RegExp ( imgSrc + '$' ) ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-21 11:16:30 +00:00
2015-01-23 12:48:27 +00:00
$div = createThumb ( imgSrc , 'Blah blah' , 'meow' ) ;
2014-02-21 11:16:30 +00:00
$link = $div . find ( 'a.image' ) ;
2014-02-17 15:09:23 +00:00
2017-05-05 15:53:28 +00:00
// Create a new bootstrap object to trigger the DOM scan, etc.
bootstrap = createBootstrap ( viewer ) ;
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
this . sandbox . stub ( viewer , 'createNewImage' ) ;
2014-02-25 02:28:49 +00:00
viewer . loadImage = $ . noop ;
2014-12-30 05:10:27 +00:00
viewer . createNewImage = function ( fileLink , filePageLink , fileTitle , index , thumb , caption , alt ) {
2015-05-20 10:22:25 +00:00
var html = thumb . outerHTML ;
2014-02-21 11:16:30 +00:00
assert . ok ( fileLink . match ( imgRegex ) , 'Thumbnail URL used in creating new image object' ) ;
assert . strictEqual ( filePageLink , '' , 'File page link is sane when creating new image object' ) ;
assert . strictEqual ( fileTitle . title , fname , 'Filename is correct when passed into new image constructor' ) ;
assert . strictEqual ( index , 0 , 'The only image we created in the gallery is set at index 0 in the images array' ) ;
2015-05-20 10:22:25 +00:00
assert . ok ( html . indexOf ( ' src="' + imgSrc + '"' ) > 0 , 'The image element passed in contains the src=... we want.' ) ;
assert . ok ( html . indexOf ( ' alt="meow"' ) > 0 , 'The image element passed in contains the alt=... we want.' ) ;
2014-02-21 11:16:30 +00:00
assert . strictEqual ( caption , 'Blah blah' , 'The caption passed in is correct' ) ;
2014-12-30 05:10:27 +00:00
assert . strictEqual ( alt , 'meow' , 'The alt text passed in is correct' ) ;
2014-02-21 11:16:30 +00:00
} ;
2014-02-17 15:09:23 +00:00
2015-01-23 12:48:27 +00:00
$link . trigger ( { type : 'click' , which : 1 } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
assert . ok ( bootstrap . setupOverlay . called , 'Overlay was set up' ) ;
clock . reset ( ) ;
2014-02-21 11:16:30 +00:00
} ) ;
2014-02-17 15:09:23 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Only load the viewer on a valid hash (modern browsers)' , function ( assert ) {
2014-02-21 11:16:30 +00:00
var bootstrap ;
2014-02-17 15:09:23 +00:00
2014-02-21 11:16:30 +00:00
window . location . hash = '' ;
2014-02-17 15:09:23 +00:00
2014-03-04 11:53:53 +00:00
bootstrap = createBootstrap ( ) ;
2014-02-17 15:09:23 +00:00
2017-05-05 15:53:28 +00:00
return hashTest ( '/media' , bootstrap , assert ) ;
2014-04-11 09:31:38 +00:00
} ) ;
2014-02-17 15:09:23 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Only load the viewer on a valid hash (old browsers)' , function ( assert ) {
2014-04-11 09:31:38 +00:00
var bootstrap ;
2014-02-17 15:09:23 +00:00
2014-04-11 09:31:38 +00:00
window . location . hash = '' ;
2014-02-17 15:09:23 +00:00
2014-04-11 09:31:38 +00:00
bootstrap = createBootstrap ( ) ;
bootstrap . browserHistory = undefined ;
2017-05-05 15:53:28 +00:00
return hashTest ( '/media' , bootstrap , assert ) ;
2014-02-24 09:29:14 +00:00
} ) ;
2014-02-17 15:09:23 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Load the viewer on a legacy hash (modern browsers)' , function ( assert ) {
2015-02-25 14:55:37 +00:00
var bootstrap ;
window . location . hash = '' ;
bootstrap = createBootstrap ( ) ;
2017-05-05 15:53:28 +00:00
return hashTest ( 'mediaviewer' , bootstrap , assert ) ;
2015-02-25 14:55:37 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Load the viewer on a legacy hash (old browsers)' , function ( assert ) {
2015-02-25 14:55:37 +00:00
var bootstrap ;
window . location . hash = '' ;
bootstrap = createBootstrap ( ) ;
bootstrap . browserHistory = undefined ;
2017-05-05 15:53:28 +00:00
return hashTest ( 'mediaviewer' , bootstrap , assert ) ;
2015-02-25 14:55:37 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Overlay is set up on hash change' , function ( assert ) {
2015-12-03 21:35:30 +00:00
var bootstrap ;
window . location . hash = '#/media/foo' ;
bootstrap = createBootstrap ( ) ;
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
bootstrap . hash ( ) ;
assert . ok ( bootstrap . setupOverlay . called , 'Overlay is set up' ) ;
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Overlay is not set up on an irrelevant hash change' , function ( assert ) {
2015-12-03 21:35:30 +00:00
var bootstrap ;
window . location . hash = '#foo' ;
bootstrap = createBootstrap ( ) ;
this . sandbox . stub ( bootstrap , 'setupOverlay' ) ;
bootstrap . loadViewer ( ) ;
bootstrap . setupOverlay . reset ( ) ;
bootstrap . hash ( ) ;
assert . ok ( ! bootstrap . setupOverlay . called , 'Overlay is not set up' ) ;
} ) ;
2015-02-25 14:55:37 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'internalHashChange' , function ( assert ) {
2015-02-25 14:55:37 +00:00
var bootstrap = createBootstrap ( ) ,
2017-05-05 15:53:28 +00:00
hash = '#/media/foo' ,
callCount = 0 ,
clock = this . sandbox . useFakeTimers ( ) ;
2015-02-25 14:55:37 +00:00
window . location . hash = '' ;
bootstrap . loadViewer = function ( ) {
2017-05-05 15:53:28 +00:00
callCount ++ ;
2015-02-25 14:55:37 +00:00
return $ . Deferred ( ) . reject ( ) ;
} ;
2017-05-05 15:53:28 +00:00
bootstrap . setupEventHandlers ( ) ;
2015-02-25 14:55:37 +00:00
bootstrap . internalHashChange ( { hash : hash } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
2015-02-25 14:55:37 +00:00
2017-05-05 15:53:28 +00:00
assert . ok ( callCount === 0 , 'Viewer should not be loaded' ) ;
2015-02-25 14:55:37 +00:00
assert . strictEqual ( window . location . hash , hash , 'Window\'s hash has been updated correctly' ) ;
bootstrap . cleanupEventHandlers ( ) ;
window . location . hash = '' ;
2017-05-05 15:53:28 +00:00
clock . restore ( ) ;
2015-02-25 14:55:37 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'internalHashChange (legacy)' , function ( assert ) {
2014-03-04 11:53:53 +00:00
var bootstrap = createBootstrap ( ) ,
2017-05-05 15:53:28 +00:00
hash = '#mediaviewer/foo' ,
callCount = 0 ,
clock = this . sandbox . useFakeTimers ( ) ;
2014-02-24 09:29:14 +00:00
2014-04-11 09:31:38 +00:00
window . location . hash = '' ;
2014-02-24 09:29:14 +00:00
bootstrap . loadViewer = function ( ) {
2017-05-05 15:53:28 +00:00
callCount ++ ;
2014-02-24 09:29:14 +00:00
return $ . Deferred ( ) . reject ( ) ;
} ;
2017-05-05 15:53:28 +00:00
bootstrap . setupEventHandlers ( ) ;
2014-02-24 09:29:14 +00:00
bootstrap . internalHashChange ( { hash : hash } ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( 10 ) ;
2014-02-24 09:29:14 +00:00
2017-05-05 15:53:28 +00:00
assert . ok ( callCount === 0 , 'Viewer should not be loaded' ) ;
2014-02-24 09:29:14 +00:00
assert . strictEqual ( window . location . hash , hash , 'Window\'s hash has been updated correctly' ) ;
2014-04-11 09:31:38 +00:00
bootstrap . cleanupEventHandlers ( ) ;
window . location . hash = '' ;
2017-05-05 15:53:28 +00:00
clock . restore ( ) ;
2014-02-17 15:09:23 +00:00
} ) ;
2014-02-24 17:06:11 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Restoring article scroll position' , function ( assert ) {
2017-09-13 18:04:22 +00:00
var stubbedScrollTop ,
bootstrap = createBootstrap ( ) ,
$window = $ ( window ) ,
done = assert . async ( ) ;
this . sandbox . stub ( $ . fn , 'scrollTop' , function ( scrollTop ) {
if ( scrollTop !== undefined ) {
stubbedScrollTop = scrollTop ;
return this ;
2014-04-11 09:31:38 +00:00
} else {
2017-09-13 18:04:22 +00:00
return stubbedScrollTop ;
2014-04-11 09:31:38 +00:00
}
} ) ;
2017-09-13 18:04:22 +00:00
$window . scrollTop ( 50 ) ;
2014-04-11 09:31:38 +00:00
bootstrap . setupOverlay ( ) ;
// Calling this a second time because it can happen in history navigation context
bootstrap . setupOverlay ( ) ;
2017-09-13 18:04:22 +00:00
// Clear scrollTop to check it is restored
$window . scrollTop ( 0 ) ;
2014-04-11 09:31:38 +00:00
bootstrap . cleanupOverlay ( ) ;
2017-09-13 18:04:22 +00:00
// Scroll restoration is on a setTimeout
setTimeout ( function ( ) {
assert . strictEqual ( $ ( window ) . scrollTop ( ) , 50 , 'Scroll is correctly reset to original top position' ) ;
done ( ) ;
} ) ;
2014-04-11 09:31:38 +00:00
} ) ;
2014-04-24 13:47:59 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Preload JS/CSS dependencies on thumb hover' , function ( assert ) {
2014-04-24 13:47:59 +00:00
var $div , bootstrap ,
2017-05-05 15:53:28 +00:00
clock = this . sandbox . useFakeTimers ( ) ,
2015-01-23 12:48:27 +00:00
viewer = { initWithThumbs : $ . noop } ;
2014-04-24 13:47:59 +00:00
// Create gallery with image that has valid name extension
$div = createThumb ( ) ;
// Create a new bootstrap object to trigger the DOM scan, etc.
bootstrap = createBootstrap ( viewer ) ;
this . sandbox . stub ( mw . loader , 'load' ) ;
$div . mouseenter ( ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( bootstrap . hoverWaitDuration - 50 ) ;
2014-04-24 13:47:59 +00:00
$div . mouseleave ( ) ;
assert . ok ( ! mw . loader . load . called , 'Dependencies should not be preloaded if the thumb is not hovered long enough' ) ;
$div . mouseenter ( ) ;
2017-05-05 15:53:28 +00:00
clock . tick ( bootstrap . hoverWaitDuration + 50 ) ;
2014-04-24 13:47:59 +00:00
$div . mouseleave ( ) ;
assert . ok ( mw . loader . load . called , 'Dependencies should be preloaded if the thumb is hovered long enough' ) ;
2017-05-05 15:53:28 +00:00
clock . restore ( ) ;
2014-04-24 13:47:59 +00:00
} ) ;
2014-06-11 18:42:29 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'isAllowedThumb' , function ( assert ) {
2014-06-11 18:42:29 +00:00
var $container = $ ( '<div>' ) ,
$thumb = $ ( '<img>' ) . appendTo ( $container ) ,
bootstrap = createBootstrap ( ) ;
assert . ok ( bootstrap . isAllowedThumb ( $thumb ) , 'Normal image in a div is allowed.' ) ;
$container . addClass ( 'metadata' ) ;
assert . strictEqual ( bootstrap . isAllowedThumb ( $thumb ) , false , 'Image in a metadata container is disallowed.' ) ;
$container . prop ( 'class' , '' ) ;
$container . addClass ( 'noviewer' ) ;
assert . strictEqual ( bootstrap . isAllowedThumb ( $thumb ) , false , 'Image in a noviewer container is disallowed.' ) ;
$container . prop ( 'class' , '' ) ;
$container . addClass ( 'noarticletext' ) ;
assert . strictEqual ( bootstrap . isAllowedThumb ( $thumb ) , false , 'Image in an empty article is disallowed.' ) ;
$container . prop ( 'class' , '' ) ;
$thumb . addClass ( 'noviewer' ) ;
assert . strictEqual ( bootstrap . isAllowedThumb ( $thumb ) , false , 'Image with a noviewer class is disallowed.' ) ;
} ) ;
2014-04-25 23:16:48 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'findCaption' , function ( assert ) {
2014-04-25 23:16:48 +00:00
var gallery = createGallery ( 'foo.jpg' , 'Baz' ) ,
thumb = createThumb ( 'foo.jpg' , 'Quuuuux' ) ,
link = createNormal ( 'foo.jpg' , 'Foobar' ) ,
2014-12-30 01:26:17 +00:00
multiple = createMultipleImage ( [ [ 'foo.jpg' , 'Image #1' ] , [ 'bar.jpg' , 'Image #2' ] ,
[ 'foobar.jpg' , 'Image #3' ] ] ) ,
2014-04-25 23:16:48 +00:00
bootstrap = createBootstrap ( ) ;
assert . strictEqual ( bootstrap . findCaption ( gallery . find ( '.thumb' ) , gallery . find ( 'a.image' ) ) , 'Baz' , 'A gallery caption is found.' ) ;
assert . strictEqual ( bootstrap . findCaption ( thumb , thumb . find ( 'a.image' ) ) , 'Quuuuux' , 'A thumbnail caption is found.' ) ;
assert . strictEqual ( bootstrap . findCaption ( $ ( ) , link ) , 'Foobar' , 'The caption is found even if the image is not a thumbnail.' ) ;
2014-12-30 01:26:17 +00:00
assert . strictEqual ( bootstrap . findCaption ( multiple , multiple . find ( 'img[src="bar.jpg"]' ) . closest ( 'a' ) ) , 'Image #2' , 'The caption is found in {{Multiple image}}.' ) ;
2014-04-25 23:16:48 +00:00
} ) ;
2014-02-17 15:09:23 +00:00
} ( mediaWiki , jQuery ) ) ;