Commit 3f7fc22a authored by Elvis Angelaccio's avatar Elvis Angelaccio
Browse files

CreateDialog: introduce unit test

The API is a bit refactored in order to be easier to test.
parent 24d793d9
......@@ -327,7 +327,7 @@ void MainWindow::newArchive()
m_openArgs.metaData()[QStringLiteral("createNewArchive")] = QStringLiteral("true");
m_openArgs.metaData()[QStringLiteral("encryptionPassword")] = password;
if (dialog.data()->isHeaderEncryptionChecked()) {
if (dialog.data()->isHeaderEncryptionEnabled()) {
m_openArgs.metaData()[QStringLiteral("encryptHeader")] = QStringLiteral("true");
}
......
......@@ -105,7 +105,7 @@ bool AddToArchive::showAddDialog()
setFilename(dialog.data()->selectedUrls().at(0));
setMimeType(dialog.data()->currentMimeFilter());
setPassword(dialog.data()->password());
setHeaderEncryptionEnabled(dialog.data()->isHeaderEncryptionChecked());
setHeaderEncryptionEnabled(dialog.data()->isHeaderEncryptionEnabled());
}
delete dialog.data();
......
......@@ -91,7 +91,7 @@ CreateDialog::CreateDialog(QWidget *parent,
connect(this, &QDialog::accepted, this, &CreateDialog::updateDefaultMimeType);
connect(this, &QDialog::finished, this, &CreateDialog::slotSaveWindowSize);
connect(m_fileWidget, &KFileWidget::filterChanged, this, &CreateDialog::updateDisplayedOptions);
connect(m_fileWidget, &KFileWidget::filterChanged, this, &CreateDialog::slotFilterChanged);
m_ui = new CreateDialogUI(this);
m_ui->groupEncryptionOptions->hide();
......@@ -103,7 +103,7 @@ CreateDialog::CreateDialog(QWidget *parent,
m_vlayout->addWidget(m_ui);
connect(m_ui->encryptCheckBox, &QCheckBox::toggled, this, &CreateDialog::encryptionToggled);
connect(m_ui->encryptCheckBox, &QCheckBox::toggled, this, &CreateDialog::slotEncryptionToggled);
}
QSize CreateDialog::sizeHint() const
......@@ -127,14 +127,29 @@ QString CreateDialog::password() const
return m_ui->pwdWidget->password();
}
bool CreateDialog::isHeaderEncryptionChecked() const
bool CreateDialog::isEncryptionAvailable() const
{
return (m_ui->encryptHeaderCheckBox->isEnabled() && m_ui->encryptHeaderCheckBox->isChecked());
return m_ui->encryptCheckBox->isEnabled();
}
bool CreateDialog::isEncryptionEnabled() const
{
return isEncryptionAvailable() && m_ui->encryptCheckBox->isChecked() && m_ui->groupEncryptionOptions->isEnabled();
}
bool CreateDialog::isHeaderEncryptionAvailable() const
{
return isEncryptionEnabled() && m_ui->encryptHeaderCheckBox->isEnabled();
}
bool CreateDialog::isHeaderEncryptionEnabled() const
{
return isHeaderEncryptionAvailable() && m_ui->encryptHeaderCheckBox->isChecked();
}
void CreateDialog::accept()
{
if (!m_ui->encryptCheckBox->isChecked()) {
if (!isEncryptionEnabled()) {
QDialog::accept();
return;
}
......@@ -155,6 +170,35 @@ void CreateDialog::accept()
}
}
void CreateDialog::slotFilterChanged(const QString &filter)
{
qCDebug(ARK) << "Current selected mime filter: " << filter;
if (Kerfuffle::supportedEncryptEntriesMimeTypes().contains(filter)) {
m_ui->encryptCheckBox->setEnabled(true);
m_ui->encryptCheckBox->setToolTip(QString());
m_ui->groupEncryptionOptions->setEnabled(true);
} else {
m_ui->encryptCheckBox->setEnabled(false);
m_ui->encryptCheckBox->setToolTip(i18n("Protection of the archive with password is not possible with the %1 format.",
QMimeDatabase().mimeTypeForName(filter).comment()));
m_ui->groupEncryptionOptions->setEnabled(false);
}
if (Kerfuffle::supportedEncryptHeaderMimeTypes().contains(filter)) {
m_ui->encryptHeaderCheckBox->setEnabled(true);
m_ui->encryptHeaderCheckBox->setToolTip(QString());
} else {
m_ui->encryptHeaderCheckBox->setEnabled(false);
// Show the tooltip only if the encryption is still enabled.
// This is needed because if the new filter is e.g. tar, the whole encryption group gets disabled.
if (isEncryptionEnabled()) {
m_ui->encryptHeaderCheckBox->setToolTip(i18n("Protection of the list of files is not possible with the %1 format.",
QMimeDatabase().mimeTypeForName(filter).comment()));
}
}
}
void CreateDialog::restoreWindowSize()
{
// Restore window size from config file, needs a windowHandle so must be called after show()
......@@ -184,7 +228,7 @@ void CreateDialog::slotOkButtonClicked()
m_fileWidget->slotOk();
}
void CreateDialog::encryptionToggled(bool checked)
void CreateDialog::slotEncryptionToggled(bool checked)
{
m_ui->groupEncryptionOptions->setVisible(checked);
}
......@@ -194,34 +238,6 @@ void CreateDialog::updateDefaultMimeType()
m_config.writeEntry("LastMimeType", m_fileWidget->currentFilterMimeType().name());
}
void CreateDialog::updateDisplayedOptions(const QString &filter)
{
qCDebug(ARK) << "Current selected mime filter: " << filter;
if (Kerfuffle::supportedEncryptEntriesMimeTypes().contains(filter)) {
m_ui->encryptCheckBox->setEnabled(true);
m_ui->encryptCheckBox->setToolTip(QString());
m_ui->groupEncryptionOptions->setEnabled(true);
} else {
m_ui->encryptCheckBox->setEnabled(false);
m_ui->encryptCheckBox->setToolTip(i18n("Protection of the archive with password is not possible with the %1 format.",
QMimeDatabase().mimeTypeForName(filter).comment()));
m_ui->groupEncryptionOptions->setEnabled(false);
}
if (Kerfuffle::supportedEncryptHeaderMimeTypes().contains(filter)) {
m_ui->encryptHeaderCheckBox->setEnabled(true);
m_ui->encryptHeaderCheckBox->setToolTip(QString());
} else {
m_ui->encryptHeaderCheckBox->setEnabled(false);
// show the tooltip only if the whole group is enabled
if (m_ui->groupEncryptionOptions->isEnabled()) {
m_ui->encryptHeaderCheckBox->setToolTip(i18n("Protection of the list of files is not possible with the %1 format.",
QMimeDatabase().mimeTypeForName(filter).comment()));
}
}
}
void CreateDialog::loadConfiguration()
{
m_config = KConfigGroup(KSharedConfig::openConfig()->group("AddDialog"));
......
......@@ -55,19 +55,38 @@ public:
QList<QUrl> selectedUrls() const;
QString currentMimeFilter() const;
QString password() const;
bool isHeaderEncryptionChecked() const;
/**
* @return Whether the user can encrypt the new archive.
*/
bool isEncryptionAvailable() const;
/**
* @return Whether the user has chosen to encrypt the new archive.
*/
bool isEncryptionEnabled() const;
/**
* @return Whether the user can encrypt the list of files in the new archive.
*/
bool isHeaderEncryptionAvailable() const;
/**
* @return Whether the user has chosen to encrypt the list of files in the new archive.
*/
bool isHeaderEncryptionEnabled() const;
public slots:
virtual void accept() Q_DECL_OVERRIDE;
void slotFilterChanged(const QString &filter);
void restoreWindowSize();
protected slots:
void slotSaveWindowSize();
void slotOkButtonClicked();
void encryptionToggled(bool checked);
void slotEncryptionToggled(bool checked);
void updateDefaultMimeType();
void updateDisplayedOptions(const QString &filter);
protected:
class CreateDialogUI *m_ui;
......
......@@ -21,4 +21,5 @@ KERFUFFLE_UNIT_TESTS(
archivetest
jobstest
mimetypetest
createdialogtest
)
/*
* Copyright (c) 2016 Elvis Angelaccio <elvis.angelaccio@kdemail.net>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "kerfuffle/createdialog.h"
#include <QCheckBox>
#include <QTest>
using namespace Kerfuffle;
class CreateDialogTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testEncryption_data();
void testEncryption();
};
void CreateDialogTest::testEncryption_data()
{
QTest::addColumn<QString>("filter");
QTest::addColumn<bool>("isEncryptionAvailable");
QTest::addColumn<bool>("isHeaderEncryptionAvailable");
QTest::newRow("zip") << QStringLiteral("application/zip") << true << false;
QTest::newRow("7z") << QStringLiteral("application/x-7z-compressed") << true << true;
QTest::newRow("rar") << QStringLiteral("application/x-rar") << true << true;
QTest::newRow("tar") << QStringLiteral("application/x-compressed-tar") << false << false;
}
void CreateDialogTest::testEncryption()
{
CreateDialog *dialog = new CreateDialog(Q_NULLPTR, QString(), QUrl());
QFETCH(QString, filter);
QFETCH(bool, isEncryptionAvailable);
QFETCH(bool, isHeaderEncryptionAvailable);
auto encryptCheckBox = dialog->findChild<QCheckBox*>(QStringLiteral("encryptCheckBox"));
auto encryptHeaderCheckBox = dialog->findChild<QCheckBox*>(QStringLiteral("encryptHeaderCheckBox"));
QVERIFY(encryptCheckBox);
QVERIFY(encryptHeaderCheckBox);
dialog->slotFilterChanged(filter);
// Encryption is initially not enabled.
QVERIFY(!dialog->isEncryptionEnabled());
QVERIFY(!dialog->isHeaderEncryptionEnabled());
if (isEncryptionAvailable) {
QVERIFY(dialog->isEncryptionAvailable());
encryptCheckBox->setChecked(true);
QVERIFY(dialog->isEncryptionEnabled());
if (isHeaderEncryptionAvailable) {
QVERIFY(dialog->isHeaderEncryptionAvailable());
// Header encryption is enabled by default, if available.
QVERIFY(dialog->isHeaderEncryptionEnabled());
encryptHeaderCheckBox->setChecked(false);
QVERIFY(!dialog->isHeaderEncryptionEnabled());
} else {
QVERIFY(!dialog->isHeaderEncryptionAvailable());
}
encryptCheckBox->setChecked(false);
QVERIFY(!dialog->isEncryptionEnabled());
QVERIFY(!dialog->isHeaderEncryptionEnabled());
} else {
QVERIFY(!dialog->isEncryptionAvailable());
QVERIFY(!dialog->isEncryptionEnabled());
QVERIFY(!dialog->isHeaderEncryptionAvailable());
QVERIFY(!dialog->isHeaderEncryptionEnabled());
}
}
QTEST_MAIN(CreateDialogTest)
#include "createdialogtest.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment