2014-03-17 08:07:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
const { ReuseDialog, Repo } = require( 'mmv' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-06-25 02:15:56 +00:00
|
|
|
function makeReuseDialog( sandbox ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const $fixture = $( '#qunit-fixture' );
|
|
|
|
const config = { getFromLocalStorage: sandbox.stub(), setInLocalStorage: sandbox.stub() };
|
2023-05-20 08:30:52 +00:00
|
|
|
return new ReuseDialog( $fixture, $( '<div>' ).appendTo( $fixture ), config );
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.module( 'mmv.ui.reuse.Dialog', QUnit.newMwEnvironment() );
|
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'Sense test, object creation and UI construction', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2023-05-20 08:30:52 +00:00
|
|
|
assert.true( reuseDialog instanceof ReuseDialog, 'Reuse UI element is created.' );
|
2014-09-16 20:33:05 +00:00
|
|
|
assert.strictEqual( reuseDialog.$dialog.length, 1, 'Reuse dialog div created.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'handleOpenCloseClick():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
|
|
|
reuseDialog.openDialog = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'openDialog called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
reuseDialog.closeDialog = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'closeDialog should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Dialog is closed by default, we should open it
|
|
|
|
reuseDialog.handleOpenCloseClick();
|
|
|
|
|
|
|
|
reuseDialog.openDialog = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'openDialog should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
reuseDialog.closeDialog = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'closeDialog called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
reuseDialog.isOpen = true;
|
|
|
|
|
|
|
|
// Dialog open now, we should close it.
|
|
|
|
reuseDialog.handleOpenCloseClick();
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'handleTabSelection():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-04-11 14:05:58 +00:00
|
|
|
reuseDialog.initTabs();
|
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
// Share pane is selected
|
2023-06-27 20:33:13 +00:00
|
|
|
reuseDialog.handleTabSelection( { getData: () => 'share' } );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( reuseDialog.tabs.share.$pane.hasClass( 'active' ), true, 'Share tab shown.' );
|
|
|
|
assert.strictEqual( reuseDialog.tabs.embed.$pane.hasClass( 'active' ), false, 'Embed tab hidden.' );
|
|
|
|
assert.strictEqual( reuseDialog.config.setInLocalStorage.calledWith( 'mmv-lastUsedTab', 'share' ), true,
|
2014-06-25 02:15:56 +00:00
|
|
|
'Tab state saved in local storage.' );
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
// Embed pane is selected
|
2023-06-27 20:33:13 +00:00
|
|
|
reuseDialog.handleTabSelection( { getData: () => 'embed' } );
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( reuseDialog.tabs.share.$pane.hasClass( 'active' ), false, 'Share tab hidden.' );
|
|
|
|
assert.strictEqual( reuseDialog.tabs.embed.$pane.hasClass( 'active' ), true, 'Embed tab shown.' );
|
2014-06-25 02:15:56 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'default tab:', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
let reuseDialog = makeReuseDialog( this.sandbox );
|
2014-06-25 02:15:56 +00:00
|
|
|
reuseDialog.initTabs();
|
2014-09-16 20:33:05 +00:00
|
|
|
assert.strictEqual( reuseDialog.selectedTab, 'share', 'Share tab is default' );
|
2014-06-25 02:15:56 +00:00
|
|
|
|
|
|
|
reuseDialog = makeReuseDialog( this.sandbox );
|
|
|
|
reuseDialog.config.getFromLocalStorage.withArgs( 'mmv-lastUsedTab' ).returns( 'share' );
|
|
|
|
reuseDialog.initTabs();
|
|
|
|
assert.strictEqual( reuseDialog.selectedTab, 'share', 'Default can be overridden' );
|
2014-03-19 02:00:26 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'attach()/unattach():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2014-04-11 14:05:58 +00:00
|
|
|
reuseDialog.initTabs();
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleOpenCloseClick = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'handleOpenCloseClick should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleTabSelection = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'handleTabSelection should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Triggering action events before attaching should do nothing
|
2014-04-01 01:41:57 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-open' );
|
2014-03-17 08:07:53 +00:00
|
|
|
reuseDialog.reuseTabs.emit( 'select' );
|
|
|
|
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleOpenCloseClick = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'handleOpenCloseClick called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleTabSelection = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'handleTabSelection called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
reuseDialog.attach();
|
|
|
|
|
|
|
|
// Action events should be handled now
|
2014-04-01 01:41:57 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-open' );
|
2014-03-17 08:07:53 +00:00
|
|
|
reuseDialog.reuseTabs.emit( 'select' );
|
|
|
|
|
|
|
|
// Test the unattach part
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleOpenCloseClick = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'handleOpenCloseClick should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
2015-01-23 12:48:27 +00:00
|
|
|
reuseDialog.handleTabSelection = function () {
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( false, 'handleTabSelection should not have been called.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
reuseDialog.unattach();
|
|
|
|
|
|
|
|
// Triggering action events now that we are unattached should do nothing
|
2014-04-01 01:41:57 +00:00
|
|
|
$( document ).trigger( 'mmv-reuse-open' );
|
2014-03-17 08:07:53 +00:00
|
|
|
reuseDialog.reuseTabs.emit( 'select' );
|
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'start/stopListeningToOutsideClick():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
|
|
|
const realCloseDialog = reuseDialog.closeDialog;
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2014-04-11 14:05:58 +00:00
|
|
|
reuseDialog.initTabs();
|
|
|
|
|
2014-03-17 08:07:53 +00:00
|
|
|
function clickOutsideDialog() {
|
2023-10-24 09:53:23 +00:00
|
|
|
const event = new $.Event( 'click', { target: reuseDialog.$container[ 0 ] } );
|
2014-03-17 08:07:53 +00:00
|
|
|
reuseDialog.$container.trigger( event );
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
function clickInsideDialog() {
|
2023-10-24 09:53:23 +00:00
|
|
|
const event = new $.Event( 'click', { target: reuseDialog.$dialog[ 0 ] } );
|
2014-09-16 20:33:05 +00:00
|
|
|
reuseDialog.$dialog.trigger( event );
|
2014-03-17 08:07:53 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertDialogDoesNotCatchClicks() {
|
2023-06-27 20:33:13 +00:00
|
|
|
reuseDialog.closeDialog = () => assert.true( false, 'Dialog is not affected by click' );
|
2023-10-24 09:53:23 +00:00
|
|
|
const event = clickOutsideDialog();
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( event.isDefaultPrevented(), false, 'Dialog does not affect click' );
|
|
|
|
assert.strictEqual( event.isPropagationStopped(), false, 'Dialog does not affect click propagation' );
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
function assertDialogCatchesOutsideClicksOnly() {
|
2023-06-27 20:33:13 +00:00
|
|
|
reuseDialog.closeDialog = () => assert.true( false, 'Dialog is not affected by inside click' );
|
2023-10-24 09:53:23 +00:00
|
|
|
let event = clickInsideDialog();
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( event.isDefaultPrevented(), false, 'Dialog does not affect inside click' );
|
|
|
|
assert.strictEqual( event.isPropagationStopped(), false, 'Dialog does not affect inside click propagation' );
|
2023-06-27 20:33:13 +00:00
|
|
|
reuseDialog.closeDialog = () => assert.true( true, 'Dialog is closed by outside click' );
|
2014-03-17 08:07:53 +00:00
|
|
|
event = clickOutsideDialog();
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( event.isDefaultPrevented(), true, 'Dialog catches outside click' );
|
|
|
|
assert.strictEqual( event.isPropagationStopped(), true, 'Dialog stops outside click propagation' );
|
2014-03-17 08:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assertDialogDoesNotCatchClicks();
|
|
|
|
reuseDialog.openDialog();
|
|
|
|
assertDialogCatchesOutsideClicksOnly();
|
|
|
|
realCloseDialog.call( reuseDialog );
|
|
|
|
assertDialogDoesNotCatchClicks();
|
|
|
|
reuseDialog.openDialog();
|
|
|
|
reuseDialog.unattach();
|
|
|
|
assertDialogDoesNotCatchClicks();
|
|
|
|
} );
|
|
|
|
|
2021-12-10 00:43:28 +00:00
|
|
|
QUnit.test( 'set()/empty() sense check:', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
|
|
|
const title = mw.Title.newFromText( 'File:Foobar.jpg' );
|
|
|
|
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
|
|
|
|
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
|
|
|
|
const image = { // fake ImageModel
|
|
|
|
title: title,
|
|
|
|
url: src,
|
|
|
|
descriptionUrl: url,
|
|
|
|
width: 100,
|
|
|
|
height: 80
|
|
|
|
};
|
|
|
|
const embedFileInfo = {
|
|
|
|
imageInfo: title,
|
|
|
|
repoInfo: src,
|
|
|
|
caption: url
|
|
|
|
};
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2014-03-19 02:00:26 +00:00
|
|
|
reuseDialog.set( image, embedFileInfo );
|
2014-03-17 08:07:53 +00:00
|
|
|
reuseDialog.empty();
|
|
|
|
|
2022-05-20 00:18:43 +00:00
|
|
|
assert.true( true, 'Set/empty did not cause an error.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
} );
|
|
|
|
|
2017-07-25 23:38:21 +00:00
|
|
|
QUnit.test( 'openDialog()/closeDialog():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
|
|
|
const title = mw.Title.newFromText( 'File:Foobar.jpg' );
|
|
|
|
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
|
|
|
|
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
|
|
|
|
const image = { // fake ImageModel
|
|
|
|
title: title,
|
|
|
|
url: src,
|
|
|
|
descriptionUrl: url,
|
|
|
|
width: 100,
|
|
|
|
height: 80
|
|
|
|
};
|
|
|
|
const repoInfo = new Repo( 'Wikipedia', '//wikipedia.org/favicon.ico', true );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2014-04-11 14:05:58 +00:00
|
|
|
reuseDialog.initTabs();
|
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
reuseDialog.set( image, repoInfo );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( reuseDialog.isOpen, false, 'Dialog closed by default.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
|
|
|
reuseDialog.openDialog();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( reuseDialog.isOpen, true, 'Dialog open now.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
|
|
|
|
reuseDialog.closeDialog();
|
|
|
|
|
2018-06-06 18:23:25 +00:00
|
|
|
assert.strictEqual( reuseDialog.isOpen, false, 'Dialog closed now.' );
|
2014-03-17 08:07:53 +00:00
|
|
|
} );
|
|
|
|
|
2014-12-07 10:09:25 +00:00
|
|
|
QUnit.test( 'getImageWarnings():', function ( assert ) {
|
2023-10-24 09:53:23 +00:00
|
|
|
const reuseDialog = makeReuseDialog( this.sandbox );
|
|
|
|
const title = mw.Title.newFromText( 'File:Foobar.jpg' );
|
|
|
|
const src = 'https://upload.wikimedia.org/wikipedia/commons/3/3a/Foobar.jpg';
|
|
|
|
const url = 'https://commons.wikimedia.org/wiki/File:Foobar.jpg';
|
|
|
|
const image = { // fake ImageModel
|
|
|
|
title: title,
|
|
|
|
url: src,
|
|
|
|
descriptionUrl: url,
|
|
|
|
width: 100,
|
|
|
|
height: 80
|
|
|
|
};
|
|
|
|
const imageDeleted = Object.assign( { deletionReason: 'deleted file test' }, image );
|
2014-12-07 10:09:25 +00:00
|
|
|
|
|
|
|
// Test that the lack of license is picked up
|
2018-06-06 17:23:16 +00:00
|
|
|
assert.strictEqual( reuseDialog.getImageWarnings( image ).length, 1, 'Lack of license detected' );
|
2014-12-07 10:09:25 +00:00
|
|
|
|
|
|
|
// Test that deletion supersedes other warnings and only that one is reported
|
2018-06-06 17:23:16 +00:00
|
|
|
assert.strictEqual( reuseDialog.getImageWarnings( imageDeleted ).length, 1, 'Deletion detected' );
|
2014-12-07 10:09:25 +00:00
|
|
|
} );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|