Debian Codegraph @

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

raw symbols SCIP
1 const { LightboxInterface } = require( 'mmv' );
2 const { getMultimediaViewer } = require( './mmv.testhelpers.js' );
3
4 ( function () {
5 let oldScrollTo;
6
7 function stubScrollTo() {
8 oldScrollTo = $.scrollTo;
9 $.scrollTo = function () {
10 return { scrollTop: () => {}, on: () => {}, off: () => {} };
11 };
12 }
13
14 function restoreScrollTo() {
15 $.scrollTo = oldScrollTo;
16 }
17
18 QUnit.module( 'mmv.lightboxInterface', QUnit.newMwEnvironment( {
19 beforeEach: function () {
20 // animation would keep running, conflict with other tests
21 this.sandbox.stub( $.fn, 'animate' ).returnsThis();
22 }
23 } ) );
24
25 QUnit.test( 'Sense test, object creation and ui construction', ( assert ) => {
26 const lightbox = new LightboxInterface();
27
28 stubScrollTo();
29
30 function checkIfUIAreasAttachedToDocument( inDocument ) {
31 const msg = ( inDocument === 1 ? ' ' : ' not ' ) + 'attached.';
32 assert.strictEqual( $( '.mw-mmv-wrapper' ).length, inDocument, 'Wrapper area' + msg );
33 assert.strictEqual( $( '.mw-mmv-main' ).length, inDocument, 'Main area' + msg );
34 assert.strictEqual( $( '.mw-mmv-title' ).length, inDocument, 'Title area' + msg );
35 assert.strictEqual( $( '.mw-mmv-credit' ).length, inDocument, 'Author/source area' + msg );
36 assert.strictEqual( $( '.mw-mmv-image-desc' ).length, inDocument, 'Description area' + msg );
37 assert.strictEqual( $( '.mw-mmv-image-links' ).length, inDocument, 'Links area' + msg );
38 }
39
40 // UI areas not attached to the document yet.
41 checkIfUIAreasAttachedToDocument( 0 );
42
43 // Attach lightbox to testing fixture to avoid interference with other tests.
44 lightbox.attach( '#qunit-fixture' );
45
46 // UI areas should now be attached to the document.
47 checkIfUIAreasAttachedToDocument( 1 );
48
49 // Check that the close button on the lightbox still follow the spec (being visible right away)
50 assert.strictEqual( $( '#qunit-fixture .mw-mmv-close' ).length, 1, 'There should be a close button' );
51 assert.true( $( '#qunit-fixture .mw-mmv-close' ).is( ':visible' ), 'The close button should be visible' );
52
53 // Unattach lightbox from document
54 lightbox.unattach();
55
56 // UI areas not attached to the document anymore.
57 checkIfUIAreasAttachedToDocument( 0 );
58
59 restoreScrollTo();
60 } );
61
62 QUnit.test( 'Handler registration and clearance work OK', function ( assert ) {
63 const lightbox = new LightboxInterface();
64 let handlerCalls = 0;
65 const clock = this.sandbox.useFakeTimers();
66
67 function handleEvent() {
68 handlerCalls++;
69 }
70
71 lightbox.handleEvent( 'test', handleEvent );
72 $( document ).trigger( 'test' );
73 clock.tick( 10 );
74 assert.strictEqual( handlerCalls, 1, 'The handler was called when we triggered the event.' );
75
76 lightbox.clearEvents();
77
78 $( document ).trigger( 'test' );
79 clock.tick( 10 );
80 assert.strictEqual( handlerCalls, 1, 'The handler was not called after calling lightbox.clearEvents().' );
81
82 clock.restore();
83 } );
84
85 QUnit.test( 'Fullscreen mode init', ( assert ) => {
86 const lightbox = new LightboxInterface();
87 const enterFullscreen = Element.prototype.requestFullscreen;
88
89 // Since we don't want these tests to really open fullscreen
90 // which is subject to user security confirmation,
91 // we use a mock that pretends regular jquery.fullscreen behavior happened
92 Element.prototype.requestFullscreen = function () {};
93
94 stubScrollTo();
95
96 lightbox.buttons.fadeOut = function () {};
97
98 // Attach lightbox to testing fixture to avoid interference with other tests.
99 lightbox.attach( '#qunit-fixture' );
100
101 lightbox.setupCanvasButtons();
102
103 // Entering fullscreen
104 lightbox.$fullscreenButton.trigger( 'click' );
105
106 assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ), true,
107 'Fullscreened area has the fullscreen class' );
108 assert.strictEqual( lightbox.isFullscreen, true, 'Lightbox knows it\'s in fullscreen mode' );
109
110 // Exiting fullscreen
111 lightbox.$fullscreenButton.trigger( 'click' );
112
113 assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ), false,
114 'Fullscreened area doesn\'t have the fullscreen class anymore' );
115 assert.strictEqual( lightbox.isFullscreen, false, 'Lightbox knows it\'s not in fullscreen mode' );
116
117 // Entering fullscreen
118 lightbox.$fullscreenButton.trigger( 'click' );
119
120 // Hard-exiting fullscreen
121 lightbox.$closeButton.trigger( 'click' );
122
123 // Re-attach after hard-exit
124 lightbox.attach( '#qunit-fixture' );
125
126 assert.strictEqual( lightbox.$main.hasClass( 'jq-fullscreened' ), false,
127 'Fullscreened area doesn\'t have the fullscreen class anymore' );
128 assert.strictEqual( lightbox.isFullscreen, false, 'Lightbox knows it\'s not in fullscreen mode' );
129
130 // Unattach lightbox from document
131 lightbox.unattach();
132
133 Element.prototype.requestFullscreen = enterFullscreen;
134 restoreScrollTo();
135 } );
136
137 QUnit.test( 'Fullscreen mode', ( assert ) => {
138 const lightbox = new LightboxInterface();
139 const viewer = getMultimediaViewer();
140 const enterFullscreen = Element.prototype.requestFullscreen;
141
142 stubScrollTo();
143
144 // ugly hack to avoid preloading which would require lightbox list being set up
145 viewer.preloadDistance = -1;
146
147 // Since we don't want these tests to really open fullscreen
148 // which is subject to user security confirmation,
149 // we use a mock that pretends regular jquery.fullscreen behavior happened
150 Element.prototype.requestFullscreen = function () {};
151
152 // Attach lightbox to testing fixture to avoid interference with other tests.
153 lightbox.attach( '#qunit-fixture' );
154 viewer.ui = lightbox;
155 viewer.ui = lightbox;
156
157 assert.strictEqual( lightbox.isFullscreen, false, 'Lightbox knows that it\'s not in fullscreen mode' );
158 assert.strictEqual( lightbox.panel.$imageMetadata.is( ':visible' ), true, 'Image metadata is visible' );
159
160 lightbox.buttons.fadeOut = function () {
161 assert.true( true, 'Opening fullscreen triggers a fadeout' );
162 };
163
164 // Pretend that the mouse cursor is on top of the button
165 const buttonOffset = lightbox.buttons.$fullscreen.offset();
166 lightbox.mousePosition = { x: buttonOffset.left, y: buttonOffset.top };
167
168 // Enter fullscreen
169 lightbox.buttons.$fullscreen.trigger( 'click' );
170
171 lightbox.buttons.fadeOut = function () {};
172 assert.true( lightbox.isFullscreen, 'Lightbox knows that it\'s in fullscreen mode' );
173
174 const oldRevealButtonsAndFadeIfNeeded = lightbox.buttons.revealAndFade;
175
176 lightbox.buttons.revealAndFade = function ( position ) {
177 assert.true( true, 'Moving the cursor triggers a reveal + fade' );
178
179 oldRevealButtonsAndFadeIfNeeded.call( this, position );
180 };
181
182 // Pretend that the mouse cursor moved to the top-left corner
183 lightbox.mousemove( { pageX: 0, pageY: 0 } );
184
185 lightbox.buttons.revealAndFadeIfNeeded = function () {};
186
187 let panelBottom = $( '.mw-mmv-post-image' ).position().top + $( '.mw-mmv-post-image' ).height();
188
189 assert.strictEqual(
190 panelBottom.toFixed(),
191 $( window ).height().toFixed(),
192 'Image metadata does not extend beyond the viewport'
193 );
194
195 lightbox.buttons.revealAndFade = function ( position ) {
196 assert.true( true, 'Closing fullscreen triggers a reveal + fade' );
197
198 oldRevealButtonsAndFadeIfNeeded.call( this, position );
199 };
200
201 // Exiting fullscreen
202 lightbox.buttons.$fullscreen.trigger( 'click' );
203
204 panelBottom = $( '.mw-mmv-post-image' ).position().top + $( '.mw-mmv-post-image' ).height();
205
206 assert.false( panelBottom > $( window ).height(), 'Image metadata does not extend beyond the viewport' );
207 assert.strictEqual( lightbox.isFullscreen, false, 'Lightbox knows that it\'s not in fullscreen mode' );
208
209 // Unattach lightbox from document
210 lightbox.unattach();
211
212 Element.prototype.requestFullscreen = enterFullscreen;
213 restoreScrollTo();
214 } );
215
216 QUnit.test( 'Keyboard prev/next', ( assert ) => {
217 const viewer = getMultimediaViewer();
218 const lightbox = new LightboxInterface();
219
220 viewer.setupEventHandlers();
221
222 // Since we define both, the test works regardless of RTL settings
223 lightbox.on( 'next', () => assert.true( true, 'Next image was open' ) );
224 lightbox.on( 'prev', () => assert.true( true, 'Prev image was open' ) );
225
226 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowLeft' } ) );
227 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowRight' } ) );
228
229 lightbox.off( 'next' ).on( 'next', () => assert.true( false, 'Next image should not have been open' ) );
230 lightbox.off( 'prev' ).on( 'prev', () => assert.true( false, 'Prev image should not have been open' ) );
231
232 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowLeft', altKey: true } ) );
233 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowRight', altKey: true } ) );
234 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowLeft', ctrlKey: true } ) );
235 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowRight', ctrlKey: true } ) );
236 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowLeft', shiftKey: true } ) );
237 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowRight', shiftKey: true } ) );
238 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowLeft', metaKey: true } ) );
239 lightbox.keydown( $.Event( 'keydown', { key: 'ArrowRight', metaKey: true } ) );
240
241 viewer.cleanupEventHandlers();
242 } );
243 }() );