2014-03-31 16:08:02 +00:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-09-16 20:33:05 +00:00
|
|
|
QUnit.module( 'mmv.ui.download.pane', QUnit.newMwEnvironment() );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'Sense test, object creation and UI construction', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
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.' );
|
2014-04-02 02:22:16 +00:00
|
|
|
assert.ok( download.defaultItem, 'Default item set.' );
|
2014-04-21 17:17:11 +00:00
|
|
|
|
|
|
|
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.' );
|
2014-03-31 16:08:02 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'set()/empty():', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
|
2014-03-31 16:08:02 +00:00
|
|
|
src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg',
|
|
|
|
image = { // fake mw.mmv.model.Image
|
|
|
|
title: new mw.Title( 'File:Foobar.jpg' ),
|
2014-04-19 01:28:30 +00:00
|
|
|
url: src
|
2014-03-31 16:08:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
assert.strictEqual( download.imageExtension, undefined, 'Image extension is not set.' );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
download.utils.updateMenuOptions = function () {
|
2014-03-31 16:08:02 +00:00
|
|
|
assert.ok( true, 'Menu options updated.' );
|
|
|
|
};
|
2015-01-23 12:48:27 +00:00
|
|
|
download.downloadSizeMenu.getMenu().selectItem = function () {
|
2014-04-02 02:22:16 +00:00
|
|
|
assert.ok( true, 'Default item selected to update the labels.' );
|
2014-03-31 16:08:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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.' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'attach()/unattach():', function ( assert ) {
|
2015-02-13 11:52:45 +00:00
|
|
|
var hsstub, tstub,
|
|
|
|
download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
|
2014-03-31 16:08:02 +00:00
|
|
|
image = {
|
|
|
|
title: new mw.Title( 'File:Foobar.jpg' ),
|
2014-04-19 01:28:30 +00:00
|
|
|
url: 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg'
|
2014-03-31 16:08:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
download.set( image );
|
|
|
|
|
2015-02-13 11:52:45 +00:00
|
|
|
hsstub = this.sandbox.stub( download, 'handleSizeSwitch' );
|
|
|
|
tstub = this.sandbox.stub( download.downloadSizeMenu.getMenu(), 'toggle' );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
// Triggering action events before attaching should do nothing
|
|
|
|
download.downloadSizeMenu.getMenu().emit(
|
2018-02-27 05:57:36 +00:00
|
|
|
'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
|
2018-04-24 14:47:41 +00:00
|
|
|
download.$selectionArrow.trigger( 'click' );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( hsstub.called, false, 'handleSizeSwitch not called' );
|
|
|
|
assert.strictEqual( tstub.called, false, 'Menu selection did not happen' );
|
2015-02-13 11:52:45 +00:00
|
|
|
|
|
|
|
hsstub.reset();
|
|
|
|
tstub.reset();
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
download.attach();
|
|
|
|
|
|
|
|
// Action events should be handled now
|
|
|
|
download.downloadSizeMenu.getMenu().emit(
|
2018-02-27 05:57:36 +00:00
|
|
|
'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
|
2018-04-24 14:47:41 +00:00
|
|
|
download.$selectionArrow.trigger( 'click' );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( hsstub.called, true, 'handleSizeSwitch was called' );
|
|
|
|
assert.strictEqual( tstub.called, true, 'Menu selection happened' );
|
2015-02-13 11:52:45 +00:00
|
|
|
|
|
|
|
hsstub.reset();
|
|
|
|
tstub.reset();
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
download.unattach();
|
|
|
|
|
|
|
|
// Triggering action events now that we are unattached should do nothing
|
|
|
|
download.downloadSizeMenu.getMenu().emit(
|
2018-02-27 05:57:36 +00:00
|
|
|
'choose', download.downloadSizeMenu.getMenu().findSelectedItem() );
|
2018-04-24 14:47:41 +00:00
|
|
|
download.$selectionArrow.trigger( 'click' );
|
2015-02-13 11:52:45 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( hsstub.called, false, 'handleSizeSwitch not called' );
|
|
|
|
assert.strictEqual( tstub.called, false, 'Menu selection did not happen' );
|
2014-03-31 16:08:02 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'handleSizeSwitch():', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
|
2014-03-31 16:08:02 +00:00
|
|
|
newImageUrl = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/NewFoobar.jpg';
|
|
|
|
|
2014-04-21 17:17:11 +00:00
|
|
|
download.utils.getThumbnailUrlPromise = function () {
|
2014-03-31 16:08:02 +00:00
|
|
|
return $.Deferred().resolve( { url: newImageUrl } ).promise();
|
|
|
|
};
|
|
|
|
|
2014-04-21 17:17:11 +00:00
|
|
|
download.setDownloadUrl = function ( url ) {
|
|
|
|
assert.strictEqual( url, newImageUrl, 'URL passed to setDownloadUrl is correct' );
|
|
|
|
};
|
|
|
|
|
2018-02-27 05:57:36 +00:00
|
|
|
download.handleSizeSwitch( download.downloadSizeMenu.getMenu().findSelectedItem() );
|
2014-03-31 16:08:02 +00:00
|
|
|
|
|
|
|
assert.ok( download.$downloadButton.html().match( /original.*/ ), 'Button message updated.' );
|
2014-04-21 17:17:11 +00:00
|
|
|
|
|
|
|
download.image = { url: newImageUrl };
|
|
|
|
|
|
|
|
download.utils.getThumbnailUrlPromise = function () {
|
|
|
|
assert.ok( false, 'Should not fetch the thumbnail if the image is original size.' );
|
|
|
|
};
|
|
|
|
|
2018-02-27 05:57:36 +00:00
|
|
|
download.handleSizeSwitch( download.downloadSizeMenu.getMenu().findSelectedItem() );
|
2014-03-31 16:08:02 +00:00
|
|
|
} );
|
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'setButtonText() sense check:', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
|
2014-04-19 01:28:30 +00:00
|
|
|
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' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'getExtensionFromUrl():', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) );
|
2014-04-19 01:28:30 +00:00
|
|
|
|
|
|
|
assert.strictEqual( download.getExtensionFromUrl( 'http://example.com/bing/foo.bar.png' ),
|
|
|
|
'png', 'Extension is parsed correctly' );
|
|
|
|
} );
|
2014-04-21 17:17:11 +00:00
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'setDownloadUrl', function ( assert ) {
|
2014-09-16 20:33:05 +00:00
|
|
|
var download = new mw.mmv.ui.download.Pane( $( '#qunit-fixture' ) ),
|
2014-04-21 17:17:11 +00:00
|
|
|
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.' );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( download.$downloadButton.hasClass( 'disabledLink' ), false, 'Download link is enabled.' );
|
2014-04-21 17:17:11 +00:00
|
|
|
} );
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|