2013-12-11 02:08:07 +00:00
( function ( mw , $ ) {
var thingsShouldBeEmptied = [
'$license' ,
'$imageDesc' ,
'$title' ,
'$credit' ,
'$username' ,
2014-01-13 21:40:22 +00:00
'$location' ,
2013-12-11 02:08:07 +00:00
'$repo' ,
2013-12-10 19:54:07 +00:00
'$datetime' ,
'$imageBackupDesc'
2013-12-11 02:08:07 +00:00
] ,
thingsShouldHaveEmptyClass = [
'$license' ,
'$imageDescDiv' ,
'$credit' ,
'$usernameLi' ,
2014-01-13 21:40:22 +00:00
'$locationLi' ,
2013-12-11 02:08:07 +00:00
'$repoLi' ,
'$datetimeLi' ,
'$useFileLi' ,
'$imageDiv'
] ;
QUnit . module ( 'ext.multimediaViewer.lightboxInterface' , QUnit . newMwEnvironment ( ) ) ;
2014-01-14 15:42:52 +00:00
QUnit . test ( 'Sanity test, object creation and ui construction' , 17 , function ( assert ) {
2014-01-09 16:44:00 +00:00
var lightbox = new mw . LightboxInterface ( ) ,
$document = $ ( document ) ,
2014-01-09 19:24:24 +00:00
scrollTopBeforeOpeningLightbox ,
originalJQueryScrollTop = $ . fn . scrollTop ,
memorizedDocumentScroll ;
2013-12-11 02:08:07 +00:00
function checkIfUIAreasAttachedToDocument ( inDocument ) {
var msg = inDocument === 1 ? ' ' : ' not ' ;
assert . strictEqual ( $ ( '.mw-mlb-title' ) . length , inDocument , 'Title area' + msg + 'attached.' ) ;
assert . strictEqual ( $ ( '.mw-mlb-author' ) . length , inDocument , 'Author area' + msg + 'attached.' ) ;
assert . strictEqual ( $ ( '.mw-mlb-image-desc' ) . length , inDocument , 'Description area' + msg + 'attached.' ) ;
assert . strictEqual ( $ ( '.mw-mlb-image-links' ) . length , inDocument , 'Links area' + msg + 'attached.' ) ;
}
2014-01-09 19:24:24 +00:00
// We need to set up a proxy on the jQuery scrollTop function
// that will let us pretend that the document really scrolled
// and that will return values as if the scroll happened
$ . fn . scrollTop = function ( scrollTop ) {
if ( $document . is ( this ) ) {
if ( scrollTop !== undefined ) {
memorizedDocumentScroll = scrollTop ;
return this ;
} else {
return memorizedDocumentScroll ;
}
}
return originalJQueryScrollTop . call ( this , scrollTop ) ;
} ;
2014-01-09 16:44:00 +00:00
// Scroll down a little bit to check that the scroll memory works
$document . scrollTop ( 10 ) ;
scrollTopBeforeOpeningLightbox = $document . scrollTop ( ) ;
2013-12-11 02:08:07 +00:00
// UI areas not attached to the document yet.
checkIfUIAreasAttachedToDocument ( 0 ) ;
// Attach lightbox to testing fixture to avoid interference with other tests.
lightbox . attach ( '#qunit-fixture' ) ;
2014-01-09 16:44:00 +00:00
// To make sure that the details are out of view, the lightbox is supposed to scroll to the top when open
assert . strictEqual ( $document . scrollTop ( ) , 0 , 'Document scrollTop should be set to 0' ) ;
2014-01-09 19:24:24 +00:00
// Scroll down to check that the scrollTop memory doesn't affect prev/next (bug 59861)
$document . scrollTop ( 20 ) ;
// This extra attach() call simulates the effect of prev/next seen in bug 59861
lightbox . attach ( '#qunit-fixture' ) ;
// The lightbox was already open at this point, the scrollTop should be left untouched
assert . strictEqual ( $document . scrollTop ( ) , 20 , 'Document scrollTop should be set to 20' ) ;
2013-12-11 02:08:07 +00:00
// UI areas should now be attached to the document.
checkIfUIAreasAttachedToDocument ( 1 ) ;
2014-01-14 15:42:52 +00:00
// Check that the close button on the lightbox still follow the spec (being visible right away)
assert . strictEqual ( $ ( '#qunit-fixture .mlb-close' ) . length , 1 , 'There should be a close button' ) ;
assert . strictEqual ( $ ( '#qunit-fixture .mlb-close' ) . is ( ':visible' ) , true , 'The close button should be visible' ) ;
2013-12-11 02:08:07 +00:00
// Unattach lightbox from document
lightbox . unattach ( ) ;
2014-01-09 16:44:00 +00:00
// Lightbox is supposed to restore the document scrollTop value that was set prior to opening it
assert . strictEqual ( $document . scrollTop ( ) , scrollTopBeforeOpeningLightbox , 'document scrollTop value has been restored correctly' ) ;
2013-12-11 02:08:07 +00:00
// UI areas not attached to the document anymore.
checkIfUIAreasAttachedToDocument ( 0 ) ;
2014-01-09 19:24:24 +00:00
// Let's remove our scrollTop proxy, to make sure this test is free of side-effect
$ . fn . scrollTop = originalJQueryScrollTop ;
2013-12-11 02:08:07 +00:00
} ) ;
QUnit . test ( 'The interface is emptied properly when necessary' , thingsShouldBeEmptied . length + thingsShouldHaveEmptyClass . length , function ( assert ) {
var i ,
lightbox = new mw . LightboxInterface ( ) ;
lightbox . empty ( ) ;
for ( i = 0 ; i < thingsShouldBeEmptied . length ; i ++ ) {
assert . strictEqual ( lightbox [ thingsShouldBeEmptied [ i ] ] . text ( ) , '' , 'We successfully emptied the ' + thingsShouldBeEmptied [ i ] + ' element' ) ;
}
for ( i = 0 ; i < thingsShouldHaveEmptyClass . length ; i ++ ) {
assert . strictEqual ( lightbox [ thingsShouldHaveEmptyClass [ i ] ] . hasClass ( 'empty' ) , true , 'We successfully applied the empty class to the ' + thingsShouldHaveEmptyClass [ i ] + ' element' ) ;
}
} ) ;
QUnit . test ( 'Handler registration and clearance work OK' , 2 , function ( assert ) {
var lightbox = new mw . LightboxInterface ( ) ,
handlerCalls = 0 ;
function handleEvent ( ) {
handlerCalls ++ ;
}
lightbox . handleEvent ( 'test' , handleEvent ) ;
$ ( document ) . trigger ( 'test' ) ;
assert . strictEqual ( handlerCalls , 1 , 'The handler was called when we triggered the event.' ) ;
lightbox . clearEvents ( ) ;
$ ( document ) . trigger ( 'test' ) ;
assert . strictEqual ( handlerCalls , 1 , 'The handler was not called after calling lightbox.clearEvents().' ) ;
} ) ;
2014-01-10 21:11:21 +00:00
QUnit . test ( 'Setting repository information in the UI works as expected' , 3 , function ( assert ) {
var lightbox = new mw . LightboxInterface ( ) ;
2013-12-20 00:40:53 +00:00
2014-01-15 16:18:33 +00:00
lightbox . setRepoDisplay ( 'Example Wiki' ) ;
2013-12-20 00:40:53 +00:00
assert . strictEqual ( lightbox . $repo . text ( ) , 'Learn more on Example Wiki' , 'Text set to something useful for remote wiki - if this fails it might be because of localisation' ) ;
2014-01-15 16:18:33 +00:00
lightbox . setRepoDisplay ( ) ;
2013-12-20 00:40:53 +00:00
assert . strictEqual ( lightbox . $repo . text ( ) , 'Learn more on ' + mw . config . get ( 'wgSiteName' ) , 'Text set to something useful for local wiki - if this fails it might be because of localisation' ) ;
2014-01-10 21:11:21 +00:00
lightbox . setFilePageLink ( 'https://commons.wikimedia.org/wiki/File:Foobar.jpg' ) ;
assert . strictEqual ( lightbox . $repo . prop ( 'href' ) , 'https://commons.wikimedia.org/wiki/File:Foobar.jpg' , 'The file link was set successfully.' ) ;
2013-12-20 00:40:53 +00:00
} ) ;
2014-01-13 21:40:22 +00:00
QUnit . test ( 'Setting location information works as expected' , 2 , function ( assert ) {
var lightbox = new mw . LightboxInterface ( ) ;
lightbox . setLocationData (
50 , 10 , 20 , 'multimediaviewer-geoloc-north' ,
70 , 30 , 40 , 'multimediaviewer-geoloc-east' ,
12.3456789 , 98.7654321 , 'en' , 'Foobar.jpg'
) ;
assert . strictEqual (
lightbox . $location . text ( ) ,
'50° 10′ 20″ N, 70° 30′ 40″ E' ,
'Location text is set as expected - if this fails it may be due to i18n issues.'
) ;
assert . strictEqual (
lightbox . $location . prop ( 'href' ) ,
'http://tools.wmflabs.org/geohack/geohack.php?pagename=File:Foobar.jpg¶ms=12.3456789_N_98.7654321_E_&language=en' ,
'Location URL is set as expected'
) ;
} ) ;
2013-12-11 02:08:07 +00:00
} ( mediaWiki , jQuery ) ) ;