summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.reuse.embed.test.js
blob: 69ca2466e9b0e83acdd0b309b04f869bdfa3c6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer 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.
 *
 * MultimediaViewer 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 MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

( function ( mw, $ ) {
	var $qf = $( '#qunit-fixture' );

	QUnit.module( 'mmv.ui.reuse.Embed', QUnit.newMwEnvironment() );

	QUnit.test( 'Sanity test, object creation and UI construction', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf );

		assert.ok( embed, 'Embed UI element is created.' );
		assert.strictEqual( embed.$pane.length, 1, 'Pane div is created.' );
		assert.ok( embed.embedTextHtml, 'Html snipped text area created.' );
		assert.ok( embed.embedTextWikitext, 'Wikitext snipped text area created.' );
		assert.ok( embed.embedSwitch, 'Snipped selection buttons created.' );
		assert.ok( embed.embedSizeSwitchWikitext, 'Size selection menu for wikitext created.' );
		assert.ok( embed.embedSizeSwitchHtml, 'Size selection menu for html created.' );
		assert.strictEqual( embed.$currentMainEmbedText.length, 1, 'Size selection menu for html created.' );
		assert.strictEqual( embed.isSizeMenuDefaultReset, false, 'Reset flag intialized correctly.' );
		assert.ok( embed.defaultHtmlItem, 'Default item for html size selection intialized.' );
		assert.ok( embed.defaultWikitextItem, 'Default item for wikitext size selection intialized.' );
		assert.ok( embed.currentSizeMenu, 'Current size menu intialized.' );
		assert.ok( embed.currentDefaultItem, 'Current default item intialized.' );
	} );

	QUnit.test( 'changeSize(): Skip if no item selected.', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 10,
			height = 20;

		assert.expect( 0 );

		// deselect items
		embed.embedSwitch.selectItem();

		embed.updateEmbedHtml = function () {
			assert.ok( false, 'No item selected, this should not have been called.' );
		};
		embed.updateEmbedWikitext = function () {
			assert.ok( false, 'No item selected, this should not have been called.' );
		};

		embed.changeSize( width, height );
	} );

	QUnit.test( 'changeSize(): HTML size menu item selected.', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 10,
			height = 20;

		embed.embedSwitch.findSelectedItem = function () {
			return { getData: function () { return 'html'; } };
		};
		embed.updateEmbedHtml = function ( thumb, w, h ) {
			assert.strictEqual( thumb.url, undefined, 'Empty thumbnail passed.' );
			assert.strictEqual( w, width, 'Correct width passed.' );
			assert.strictEqual( h, height, 'Correct height passed.' );
		};
		embed.updateEmbedWikitext = function () {
			assert.ok( false, 'Dealing with HTML menu, this should not have been called.' );
		};
		embed.select = function () {
			assert.ok( true, 'Item selected after update.' );
		};

		embed.changeSize( width, height );
	} );

	QUnit.test( 'changeSize(): Wikitext size menu item selected.', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 10,
			height = 20;

		embed.embedSwitch.findSelectedItem = function () {
			return { getData: function () { return 'wikitext'; } };
		};
		embed.updateEmbedHtml = function () {
			assert.ok( false, 'Dealing with wikitext menu, this should not have been called.' );
		};
		embed.updateEmbedWikitext = function ( w ) {
			assert.strictEqual( w, width, 'Correct width passed.' );
		};
		embed.select = function () {
			assert.ok( true, 'Item selected after update.' );
		};

		embed.changeSize( width, height );
	} );

	QUnit.test( 'updateEmbedHtml(): Do nothing if set() not called before.', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 10,
			height = 20;

		assert.expect( 0 );

		embed.formatter.getThumbnailHtml = function () {
			assert.ok( false, 'formatter.getThumbnailHtml() should not have been called.' );
		};
		embed.updateEmbedHtml( {}, width, height );
	} );

	QUnit.test( 'updateEmbedHtml():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			url = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
			thumbUrl = 'https://upload.wikimedia.org/wikipedia/thumb/Foobar.jpg',
			imageInfo = { url: url },
			repoInfo = {},
			caption = '-',
			info = new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, caption ),
			width = 10,
			height = 20;

		embed.set( imageInfo, repoInfo, caption );

		// Small image, no thumbnail info is passed
		embed.formatter.getThumbnailHtml = function ( i, u, w, h ) {
			assert.deepEqual( i, info, 'Info passed correctly.' );
			assert.strictEqual( u, url, 'Image URL passed correctly.' );
			assert.strictEqual( w, width, 'Correct width passed.' );
			assert.strictEqual( h, height, 'Correct height passed.' );
		};
		embed.updateEmbedHtml( {}, width, height );

		// Small image, thumbnail info present
		embed.formatter.getThumbnailHtml = function ( i, u ) {
			assert.strictEqual( u, thumbUrl, 'Image src passed correctly.' );
		};
		embed.updateEmbedHtml( { url: thumbUrl }, width, height );

		// Big image, thumbnail info present
		embed.formatter.getThumbnailHtml = function ( i, u ) {
			assert.strictEqual( u, url, 'Image src passed correctly.' );
		};
		width = 1300;
		embed.updateEmbedHtml( { url: thumbUrl }, width, height );
	} );

	QUnit.test( 'updateEmbedWikitext(): Do nothing if set() not called before.', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 10;

		assert.expect( 0 );

		embed.formatter.getThumbnailWikitext = function () {
			assert.ok( false, 'formatter.getThumbnailWikitext() should not have been called.' );
		};
		embed.updateEmbedWikitext( width );
	} );

	QUnit.test( 'updateEmbedWikitext():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			imageInfo = {},
			repoInfo = {},
			caption = '-',
			info = new mw.mmv.model.EmbedFileInfo( imageInfo, repoInfo, caption ),
			width = 10;

		embed.set( imageInfo, repoInfo, caption );

		embed.formatter.getThumbnailWikitextFromEmbedFileInfo = function ( i, w ) {
			assert.deepEqual( i, info, 'EmbedFileInfo passed correctly.' );
			assert.strictEqual( w, width, 'Width passed correctly.' );
		};
		embed.updateEmbedWikitext( width );
	} );

	QUnit.test( 'getPossibleImageSizesForWikitext()', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			exampleSizes = [
				// Big wide image
				{
					width: 2048, height: 1536,
					expected: {
						small: { width: 300, height: 225 },
						medium: { width: 400, height: 300 },
						large: { width: 500, height: 375 },
						'default': { width: null, height: null }
					}
				},

				// Big tall image
				{
					width: 201, height: 1536,
					expected: {
						'default': { width: null, height: null }
					}
				},

				// Very small image
				{
					width: 15, height: 20,
					expected: {
						'default': { width: null, height: null }
					}
				}
			],
			i, cursize, opts;
		for ( i = 0; i < exampleSizes.length; i++ ) {
			cursize = exampleSizes[ i ];
			opts = embed.getPossibleImageSizesForWikitext( cursize.width, cursize.height );
			assert.deepEqual( opts, cursize.expected, 'We got the expected results out of the size calculation function.' );
		}
	} );

	QUnit.test( 'set():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			title = mw.Title.newFromText( 'File:Foobar.jpg' ),
			src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
			url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
			embedFileInfo = new mw.mmv.model.EmbedFileInfo( title, src, url ),
			calledSelect = false,
			width = 15,
			height = 20;

		embed.utils.updateMenuOptions = function ( sizes, options ) {
			assert.strictEqual( options.length, 4, 'Options passed correctly.' );
		};
		embed.resetCurrentSizeMenuToDefault = function () {
			assert.ok( true, 'resetCurrentSizeMenuToDefault() is called.' );
		};
		embed.utils.getThumbnailUrlPromise = function () {
			return $.Deferred().resolve().promise();
		};
		embed.updateEmbedHtml = function () {
			assert.ok( true, 'updateEmbedHtml() is called after data is collected.' );
		};
		embed.select = function () {
			calledSelect = true;
		};

		assert.ok( !embed.embedFileInfo, 'embedFileInfo not set yet.' );

		embed.set( { width: width, height: height }, embedFileInfo );

		assert.ok( embed.embedFileInfo, 'embedFileInfo correctly set.' );
		assert.strictEqual( embed.isSizeMenuDefaultReset, false, 'Reset flag cleared.' );
		assert.strictEqual( calledSelect, true, 'select() is called' );
	} );

	QUnit.test( 'empty():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			width = 15,
			height = 20;

		embed.formatter = {
			getThumbnailWikitextFromEmbedFileInfo: function () { return 'wikitext'; },
			getThumbnailHtml: function () { return 'html'; }
		};

		embed.set( {}, {} );
		embed.updateEmbedHtml( { url: 'x' }, width, height );
		embed.updateEmbedWikitext( width );

		assert.notStrictEqual( embed.embedTextHtml.getValue(), '', 'embedTextHtml is not empty.' );
		assert.notStrictEqual( embed.embedTextWikitext.getValue(), '', 'embedTextWikitext is not empty.' );

		embed.empty();

		assert.strictEqual( embed.embedTextHtml.getValue(), '', 'embedTextHtml is empty.' );
		assert.strictEqual( embed.embedTextWikitext.getValue(), '', 'embedTextWikitext is empty.' );
		assert.ok( !embed.embedSizeSwitchHtml.getMenu().isVisible(), 'Html size menu should be hidden.' );
		assert.ok( !embed.embedSizeSwitchWikitext.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );
	} );

	QUnit.test( 'attach()/unattach():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf ),
			title = mw.Title.newFromText( 'File:Foobar.jpg' ),
			src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
			url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg',
			embedFileInfo = new mw.mmv.model.EmbedFileInfo( title, src, url ),
			width = 15,
			height = 20;

		embed.set( { width: width, height: height }, embedFileInfo );

		embed.selectAllOnEvent = function () {
			assert.ok( false, 'selectAllOnEvent should not have been called.' );
		};
		embed.handleTypeSwitch = function () {
			assert.ok( false, 'handleTypeSwitch should not have been called.' );
		};
		embed.handleSizeSwitch = function () {
			assert.ok( false, 'handleTypeSwitch should not have been called.' );
		};

		// Triggering action events before attaching should do nothing
		// use of focus() would run into jQuery bug #14740 and similar issues
		embed.embedTextHtml.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedTextWikitext.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedSwitch.emit( 'select' );
		embed.embedSizeSwitchHtml.getMenu().emit(
			'choose', embed.embedSizeSwitchHtml.getMenu().findSelectedItem() );
		embed.embedSizeSwitchWikitext.getMenu().emit(
			'choose', embed.embedSizeSwitchWikitext.getMenu().findSelectedItem() );

		embed.selectAllOnEvent = function () {
			assert.ok( true, 'selectAllOnEvent was called.' );
		};
		embed.handleTypeSwitch = function () {
			assert.ok( true, 'handleTypeSwitch was called.' );
		};
		embed.handleSizeSwitch = function () {
			assert.ok( true, 'handleTypeSwitch was called.' );
		};

		embed.attach();

		// Action events should be handled now
		embed.embedTextHtml.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedTextWikitext.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedSwitch.emit( 'select' );
		embed.embedSizeSwitchHtml.getMenu().emit(
			'choose', embed.embedSizeSwitchHtml.getMenu().findSelectedItem() );
		embed.embedSizeSwitchWikitext.getMenu().emit(
			'choose', embed.embedSizeSwitchWikitext.getMenu().findSelectedItem() );

		// Test the unattach part
		embed.selectAllOnEvent = function () {
			assert.ok( false, 'selectAllOnEvent should not have been called.' );
		};
		embed.handleTypeSwitch = function () {
			assert.ok( false, 'handleTypeSwitch should not have been called.' );
		};
		embed.handleSizeSwitch = function () {
			assert.ok( false, 'handleTypeSwitch should not have been called.' );
		};

		embed.unattach();

		// Triggering action events now that we are unattached should do nothing
		embed.embedTextHtml.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedTextWikitext.$element.find( 'textarea' ).triggerHandler( 'focus' );
		embed.embedSwitch.emit( 'select' );
		embed.embedSizeSwitchHtml.getMenu().emit(
			'choose', embed.embedSizeSwitchHtml.getMenu().findSelectedItem() );
		embed.embedSizeSwitchWikitext.getMenu().emit(
			'choose', embed.embedSizeSwitchWikitext.getMenu().findSelectedItem() );
	} );

	QUnit.test( 'handleTypeSwitch():', function ( assert ) {
		var embed = new mw.mmv.ui.reuse.Embed( $qf );

		assert.strictEqual( embed.isSizeMenuDefaultReset, false, 'Reset flag intialized correctly.' );

		embed.resetCurrentSizeMenuToDefault = function () {
			assert.ok( true, 'resetCurrentSizeMenuToDefault() called.' );
		};

		// HTML selected
		embed.handleTypeSwitch( { getData: function () { return 'html'; } } );

		assert.strictEqual( embed.isSizeMenuDefaultReset, true, 'Reset flag updated correctly.' );
		assert.ok( !embed.embedSizeSwitchWikitext.getMenu().isVisible(), 'Wikitext size menu should be hidden.' );

		embed.resetCurrentSizeMenuToDefault = function () {
			assert.ok( false, 'resetCurrentSizeMenuToDefault() should not have been called.' );
		};

		// Wikitext selected, we are done resetting defaults
		embed.handleTypeSwitch( { getData: function () { return 'wikitext'; } } );

		assert.strictEqual( embed.isSizeMenuDefaultReset, true, 'Reset flag updated correctly.' );
		assert.ok( !embed.embedSizeSwitchHtml.getMenu().isVisible(), 'HTML size menu should be hidden.' );
	} );

	QUnit.test( 'Logged out', function ( assert ) {
		var embed,
			oldUserIsAnon = mw.user.isAnon;

		mw.user.isAnon = function () { return true; };

		embed = new mw.mmv.ui.reuse.Embed( $qf );

		assert.ok( !embed.embedSizeSwitchWikitext.$element.hasClass( 'active' ), 'Wikitext widget should be hidden.' );
		assert.ok( embed.embedSizeSwitchHtml.$element.hasClass( 'active' ), 'HTML widget should be visible.' );
		assert.ok( !embed.embedTextWikitext.$element.hasClass( 'active' ), 'Wikitext input should be hidden.' );
		assert.ok( embed.embedTextHtml.$element.hasClass( 'active' ), 'HTML input should be visible.' );

		mw.user.isAnon = oldUserIsAnon;
	} );

}( mediaWiki, jQuery ) );