summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/tests/qunit/mmv/ui/mmv.ui.download.pane.test.js
blob: 8cc6008fc113591975fe906487f2e5c16d490b35 (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
/*
 * 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, $ ) {
	QUnit.module( 'mmv.ui.download.pane', QUnit.newMwEnvironment() );

	QUnit.test( 'Sanity test, object creation and UI construction', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) );

		assert.ok( download, 'download UI element is created.' );
		assert.strictEqual( download.$pane.length, 1, 'Pane div created.' );
		assert.ok( download.$downloadButton && download.$selectionArrow, 'Download button created.' );
		assert.ok( download.downloadSizeMenu, 'Image size pulldown menu created.' );
		assert.ok( download.$previewLink, 'Preview link created.' );
		assert.ok( download.defaultItem, 'Default item set.' );

		assert.strictEqual( download.$downloadButton.html(), '', 'Button has empty content.' );
		assert.strictEqual( download.$downloadButton.attr( 'href' ), undefined, 'Button href is empty.' );
		assert.strictEqual( download.$previewLink.attr( 'href' ), undefined, 'Preview link href is empty.' );
	} );

	QUnit.test( 'set()/empty():', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
			src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
			image = { // fake mw.mmv.model.Image
				title: new mw.Title( 'File:Foobar.jpg' ),
				url: src
			};

		assert.strictEqual( download.imageExtension, undefined, 'Image extension is not set.' );

		download.utils.updateMenuOptions = function () {
			assert.ok( true, 'Menu options updated.' );
		};
		download.downloadSizeMenu.getMenu().selectItem = function () {
			assert.ok( true, 'Default item selected to update the labels.' );
		};

		download.set( image );

		assert.strictEqual( download.imageExtension, 'jpg', 'Image extension is set correctly.' );

		download.empty();

		assert.strictEqual( download.imageExtension, undefined, 'Image extension is not set.' );
	} );

	QUnit.test( 'attach()/unattach():', function ( assert ) {
		var hsstub, tstub,
			download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
			image = {
				title: new mw.Title( 'File:Foobar.jpg' ),
				url: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'
			};

		download.set( image );

		hsstub = this.sandbox.stub( download, 'handleSizeSwitch' );
		tstub = this.sandbox.stub( download.downloadSizeMenu.getMenu(), 'toggle' );

		// Triggering action events before attaching should do nothing
		download.downloadSizeMenu.getMenu().emit(
			'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
		download.$selectionArrow.click();

		assert.ok( !hsstub.called, 'handleSizeSwitch not called' );
		assert.ok( !tstub.called, 'Menu selection did not happen' );

		hsstub.reset();
		tstub.reset();

		download.attach();

		// Action events should be handled now
		download.downloadSizeMenu.getMenu().emit(
			'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
		download.$selectionArrow.click();

		assert.ok( hsstub.called, 'handleSizeSwitch was called' );
		assert.ok( tstub.called, 'Menu selection happened' );

		hsstub.reset();
		tstub.reset();

		download.unattach();

		// Triggering action events now that we are unattached should do nothing
		download.downloadSizeMenu.getMenu().emit(
			'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
		download.$selectionArrow.click();

		assert.ok( !hsstub.called, 'handleSizeSwitch not called' );
		assert.ok( !tstub.called, 'Menu selection did not happen' );
	} );

	QUnit.test( 'handleSizeSwitch():', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
			newImageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/NewFoobar.jpg';

		download.utils.getThumbnailUrlPromise = function () {
			return $.Deferred().resolve( { url: newImageUrl } ).promise();
		};

		download.setDownloadUrl = function ( url ) {
			assert.strictEqual( url, newImageUrl, 'URL passed to setDownloadUrl is correct' );
		};

		download.handleSizeSwitch( download.downloadSizeMenu.getMenu().findSelectedItem() );

		assert.ok( download.$downloadButton.html().match( /original.*/ ), 'Button message updated.' );

		download.image = { url: newImageUrl };

		download.utils.getThumbnailUrlPromise = function () {
			assert.ok( false, 'Should not fetch the thumbnail if the image is original size.' );
		};

		download.handleSizeSwitch( download.downloadSizeMenu.getMenu().findSelectedItem() );
	} );

	QUnit.test( 'setButtonText() sanity check:', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
			message;

		download.setButtonText( 'large', 'jpg', 100, 200 );
		assert.ok( true, 'Setting the text did not cause any errors' );

		message = download.$downloadButton.html();
		download.setButtonText( 'small', 'png', 1000, 2000 );
		assert.notStrictEqual( download.$downloadButton.html(), message, 'Button text was updated' );
	} );

	QUnit.test( 'getExtensionFromUrl():', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) );

		assert.strictEqual( download.getExtensionFromUrl( 'http://example.com/bing/foo.bar.png' ),
			'png', 'Extension is parsed correctly' );
	} );

	QUnit.test( 'setDownloadUrl', function ( assert ) {
		var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
			imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/NewFoobar.jpg';

		download.setDownloadUrl( imageUrl );

		assert.strictEqual( download.$downloadButton.attr( 'href' ), imageUrl + '?download', 'Download link is set correctly.' );
		assert.strictEqual( download.$previewLink.attr( 'href' ), imageUrl, 'Preview link is set correctly.' );
		assert.ok( !download.$downloadButton.hasClass( 'disabledLink' ), 'Download link is enabled.' );
	} );
}( mediaWiki, jQuery ) );