2014-05-05 21:44:27 +00:00
/ *
* This file is part of the MediaWiki extension MediaViewer .
*
* MediaViewer is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 2 of the License , or
* ( at your option ) any later version .
*
* MediaViewer is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with MediaViewer . If not , see < http : //www.gnu.org/licenses/>.
* /
2018-11-12 16:33:24 +00:00
( function ( ) {
2014-05-17 13:24:36 +00:00
QUnit . module ( 'mmv.ui.metadataPanelScroller' , QUnit . newMwEnvironment ( {
2022-05-13 20:41:15 +00:00
beforeEach : function ( ) {
2014-05-17 13:24:36 +00:00
this . clock = this . sandbox . useFakeTimers ( ) ;
}
} ) ) ;
2014-05-05 21:44:27 +00:00
2017-07-25 23:38:21 +00:00
QUnit . test ( 'empty()' , function ( assert ) {
2014-05-05 21:44:27 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getFakeLocalStorage ( ) ,
scroller = new mw . mmv . ui . MetadataPanelScroller ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , localStorage ) ;
2014-05-05 21:44:27 +00:00
scroller . empty ( ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( scroller . $container . hasClass ( 'invite' ) , false , 'We successfully reset the invite' ) ;
2014-05-05 21:44:27 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Metadata div is only animated once' , function ( assert ) {
2014-05-05 21:44:27 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
displayCount = null , // pretend it doesn't exist at first
localStorage = mw . mmv . testHelpers . createLocalStorage ( {
2014-05-05 21:44:27 +00:00
// We simulate localStorage to avoid test side-effects
getItem : function ( ) { return displayCount ; } ,
setItem : function ( _ , val ) { displayCount = val ; }
2017-06-02 09:29:41 +00:00
} ) ,
scroller = new mw . mmv . ui . MetadataPanelScroller ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , localStorage ) ;
2014-05-05 21:44:27 +00:00
scroller . attach ( ) ;
scroller . animateMetadataOnce ( ) ;
2022-05-20 00:18:43 +00:00
assert . true ( scroller . hasAnimatedMetadata ,
2014-05-05 21:44:27 +00:00
'The first call to animateMetadataOnce set hasAnimatedMetadata to true' ) ;
2022-05-20 00:18:43 +00:00
assert . true ( $qf . hasClass ( 'invite' ) ,
2014-05-05 21:44:27 +00:00
'The first call to animateMetadataOnce led to an animation' ) ;
$qf . removeClass ( 'invite' ) ;
scroller . animateMetadataOnce ( ) ;
assert . strictEqual ( scroller . hasAnimatedMetadata , true , 'The second call to animateMetadataOnce did not change the value of hasAnimatedMetadata' ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( $qf . hasClass ( 'invite' ) , false ,
2014-05-05 21:44:27 +00:00
'The second call to animateMetadataOnce did not lead to an animation' ) ;
scroller . unattach ( ) ;
scroller . attach ( ) ;
scroller . animateMetadataOnce ( ) ;
2022-05-20 00:18:43 +00:00
assert . true ( $qf . hasClass ( 'invite' ) ,
2014-05-05 21:44:27 +00:00
'After closing and opening the viewer, the panel is animated again' ) ;
2014-06-10 20:01:19 +00:00
scroller . unattach ( ) ;
2014-05-05 21:44:27 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'No localStorage' , function ( assert ) {
2014-05-05 21:44:27 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . getUnsupportedLocalStorage ( ) ,
scroller = new mw . mmv . ui . MetadataPanelScroller ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , localStorage ) ;
2014-05-05 21:44:27 +00:00
2017-09-13 18:04:22 +00:00
this . sandbox . stub ( $ . fn , 'scrollTop' , function ( ) { return 10 ; } ) ;
2014-05-05 21:44:27 +00:00
scroller . scroll ( ) ;
2014-06-17 22:23:18 +00:00
assert . strictEqual ( scroller . hasOpenedMetadata , true , 'We store hasOpenedMetadata flag for the session' ) ;
2014-05-05 21:44:27 +00:00
} ) ;
2017-07-25 23:38:21 +00:00
QUnit . test ( 'localStorage is full' , function ( assert ) {
2014-05-05 21:44:27 +00:00
var $qf = $ ( '#qunit-fixture' ) ,
2017-06-02 09:29:41 +00:00
localStorage = mw . mmv . testHelpers . createLocalStorage ( {
getItem : this . sandbox . stub ( ) . returns ( null ) ,
setItem : this . sandbox . stub ( ) . throwsException ( 'I am full' )
} ) ,
2014-06-17 22:23:18 +00:00
scroller = new mw . mmv . ui . MetadataPanelScroller ( $qf , $ ( '<div>' ) . appendTo ( $qf ) , localStorage ) ;
2014-05-05 21:44:27 +00:00
2017-09-13 18:04:22 +00:00
this . sandbox . stub ( $ . fn , 'scrollTop' , function ( ) { return 10 ; } ) ;
2014-07-16 23:15:38 +00:00
scroller . attach ( ) ;
2014-05-05 21:44:27 +00:00
scroller . scroll ( ) ;
2014-06-17 22:23:18 +00:00
assert . strictEqual ( scroller . hasOpenedMetadata , true , 'We store hasOpenedMetadata flag for the session' ) ;
scroller . scroll ( ) ;
2022-05-20 00:18:43 +00:00
assert . true ( localStorage . store . setItem . calledOnce , 'localStorage only written once' ) ;
2014-07-16 23:15:38 +00:00
scroller . unattach ( ) ;
2014-05-05 21:44:27 +00:00
} ) ;
2014-05-17 13:24:36 +00:00
/ * *
* We need to set up a proxy on the jQuery scrollTop function and the jQuery . scrollTo plugin ,
* that will let us pretend that the document really scrolled and that will return values
* as if the scroll happened .
2016-07-18 13:49:27 +00:00
*
2014-05-17 13:24:36 +00:00
* @ param { sinon . sandbox } sandbox
* @ param { mw . mmv . ui . MetadataPanelScroller } scroller
* /
function stubScrollFunctions ( sandbox , scroller ) {
2017-09-13 18:04:22 +00:00
var memorizedScrollTop = 0 ;
2014-05-17 13:24:36 +00:00
sandbox . stub ( $ . fn , 'scrollTop' , function ( scrollTop ) {
2017-09-13 18:04:22 +00:00
if ( scrollTop !== undefined ) {
memorizedScrollTop = scrollTop ;
scroller . scroll ( ) ;
return this ;
} else {
return memorizedScrollTop ;
2014-05-17 13:24:36 +00:00
}
} ) ;
2017-09-13 18:04:22 +00:00
sandbox . stub ( $ . fn , 'animate' , function ( props ) {
if ( 'scrollTop' in props ) {
memorizedScrollTop = props . scrollTop ;
2014-05-17 13:24:36 +00:00
scroller . scroll ( ) ;
}
2017-09-13 18:04:22 +00:00
return this ;
2014-05-17 13:24:36 +00:00
} ) ;
}
2017-07-25 23:38:21 +00:00
QUnit . test ( 'Metadata scrolling' , function ( assert ) {
2017-09-13 18:04:22 +00:00
var $window = $ ( window ) ,
$qf = $ ( '#qunit-fixture' ) ,
2014-05-17 13:24:36 +00:00
$container = $ ( '<div>' ) . css ( 'height' , 100 ) . appendTo ( $qf ) ,
2014-07-16 23:15:38 +00:00
$aboveFold = $ ( '<div>' ) . css ( 'height' , 50 ) . appendTo ( $container ) ,
2017-06-02 09:29:41 +00:00
fakeLocalStorage = mw . mmv . testHelpers . createLocalStorage ( {
getItem : this . sandbox . stub ( ) . returns ( null ) ,
2019-02-05 23:59:52 +00:00
setItem : function ( ) { }
2017-06-02 09:29:41 +00:00
} ) ,
2015-01-23 12:48:27 +00:00
scroller = new mw . mmv . ui . MetadataPanelScroller ( $container , $aboveFold , fakeLocalStorage ) ,
2014-05-17 13:24:36 +00:00
keydown = $ . Event ( 'keydown' ) ;
stubScrollFunctions ( this . sandbox , scroller ) ;
2017-06-02 09:29:41 +00:00
this . sandbox . stub ( fakeLocalStorage . store , 'setItem' ) ;
2014-05-17 13:24:36 +00:00
// First phase of the test: up and down arrows
scroller . hasAnimatedMetadata = false ;
scroller . attach ( ) ;
2017-09-13 18:04:22 +00:00
assert . strictEqual ( $window . scrollTop ( ) , 0 , 'scrollTop should be set to 0' ) ;
2014-05-17 13:24:36 +00:00
2018-06-06 18:23:25 +00:00
assert . strictEqual ( fakeLocalStorage . store . setItem . called , false , 'The metadata hasn\'t been open yet, no entry in localStorage' ) ;
2014-05-17 13:24:36 +00:00
keydown . which = 38 ; // Up arrow
scroller . keydown ( keydown ) ;
2018-06-06 18:23:25 +00:00
assert . strictEqual ( fakeLocalStorage . store . setItem . calledWithExactly ( 'mmv.hasOpenedMetadata' , '1' ) , true , 'localStorage knows that the metadata has been open' ) ;
2014-05-17 13:24:36 +00:00
keydown . which = 40 ; // Down arrow
scroller . keydown ( keydown ) ;
2017-09-13 18:04:22 +00:00
assert . strictEqual ( $window . scrollTop ( ) , 0 ,
'scrollTop should be set to 0 after pressing down arrow' ) ;
2014-05-17 13:24:36 +00:00
// Unattach lightbox from document
scroller . unattach ( ) ;
// Second phase of the test: scroll memory
scroller . attach ( ) ;
// To make sure that the details are out of view, the lightbox is supposed to scroll to the top when open
2017-09-13 18:04:22 +00:00
assert . strictEqual ( $window . scrollTop ( ) , 0 , 'Page scrollTop should be set to 0' ) ;
2014-05-17 13:24:36 +00:00
// Scroll down to check that the scrollTop memory doesn't affect prev/next (bug 59861)
2017-09-13 18:04:22 +00:00
$window . scrollTop ( 20 ) ;
2014-05-17 13:24:36 +00:00
this . clock . tick ( 100 ) ;
// This extra attach() call simulates the effect of prev/next seen in bug 59861
scroller . attach ( ) ;
// The lightbox was already open at this point, the scrollTop should be left untouched
2017-09-13 18:04:22 +00:00
assert . strictEqual ( $window . scrollTop ( ) , 20 , 'Page scrollTop should be set to 20' ) ;
2014-05-17 13:24:36 +00:00
scroller . unattach ( ) ;
} ) ;
2018-11-12 16:33:24 +00:00
} ( ) ) ;