From d5b70b1befa92e363788b60a960eac8751b8b986 Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Tue, 8 Oct 2013 22:09:20 +0200 Subject: [PATCH] Add a QMessageBox wrapper class to help gui tests. QMessageBox displays modal dialogs which blocks the gui tests. To work around this we add a MessageBox wrapper class where the tests can set the answer for the next dialog. The answer is then returned without actually showing the dialog. --- src/CMakeLists.txt | 1 + src/gui/ChangeMasterKeyWidget.cpp | 13 +++-- src/gui/DatabaseOpenWidget.cpp | 9 ++-- src/gui/DatabaseTabWidget.cpp | 16 +++--- src/gui/DatabaseWidget.cpp | 10 ++-- src/gui/EditWidgetIcons.cpp | 8 +-- src/gui/KeePass1OpenWidget.cpp | 6 +-- src/gui/MessageBox.cpp | 86 +++++++++++++++++++++++++++++++ src/gui/MessageBox.h | 51 ++++++++++++++++++ src/gui/UnlockDatabaseWidget.cpp | 5 +- src/gui/entry/EditEntryWidget.cpp | 12 ++--- 11 files changed, 176 insertions(+), 41 deletions(-) create mode 100644 src/gui/MessageBox.cpp create mode 100644 src/gui/MessageBox.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b51013efa..176ecb34c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -87,6 +87,7 @@ set(keepassx_SOURCES gui/KeePass1OpenWidget.cpp gui/LineEdit.cpp gui/MainWindow.cpp + gui/MessageBox.cpp gui/PasswordGeneratorWidget.cpp gui/SettingsWidget.cpp gui/SortFilterHideProxyModel.cpp diff --git a/src/gui/ChangeMasterKeyWidget.cpp b/src/gui/ChangeMasterKeyWidget.cpp index e3cf8023f..abe5f4b51 100644 --- a/src/gui/ChangeMasterKeyWidget.cpp +++ b/src/gui/ChangeMasterKeyWidget.cpp @@ -18,11 +18,10 @@ #include "ChangeMasterKeyWidget.h" #include "ui_ChangeMasterKeyWidget.h" -#include - #include "keys/FileKey.h" #include "keys/PasswordKey.h" #include "gui/FileDialog.h" +#include "gui/MessageBox.h" ChangeMasterKeyWidget::ChangeMasterKeyWidget(QWidget* parent) : DialogyWidget(parent) @@ -56,7 +55,7 @@ void ChangeMasterKeyWidget::createKeyFile() QString errorMsg; bool created = FileKey::create(fileName, &errorMsg); if (!created) { - QMessageBox::warning(this, tr("Error"), tr("Unable to create Key File : ") + errorMsg); + MessageBox::warning(this, tr("Error"), tr("Unable to create Key File : ") + errorMsg); } else { m_ui->keyFileCombo->setEditText(fileName); @@ -105,16 +104,16 @@ void ChangeMasterKeyWidget::generateKey() if (m_ui->passwordGroup->isChecked()) { if (m_ui->enterPasswordEdit->text() == m_ui->repeatPasswordEdit->text()) { if (m_ui->enterPasswordEdit->text().isEmpty()) { - if (QMessageBox::question(this, tr("Question"), - tr("Do you really want to use an empty string as password?"), - QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) { + if (MessageBox::question(this, tr("Question"), + tr("Do you really want to use an empty string as password?"), + QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes) { return; } } m_key.addKey(PasswordKey(m_ui->enterPasswordEdit->text())); } else { - QMessageBox::warning(this, tr("Error"), tr("Different passwords supplied.")); + MessageBox::warning(this, tr("Error"), tr("Different passwords supplied.")); m_ui->enterPasswordEdit->setText(""); m_ui->repeatPasswordEdit->setText(""); return; diff --git a/src/gui/DatabaseOpenWidget.cpp b/src/gui/DatabaseOpenWidget.cpp index bed1eb266..4adb9b3a0 100644 --- a/src/gui/DatabaseOpenWidget.cpp +++ b/src/gui/DatabaseOpenWidget.cpp @@ -18,11 +18,10 @@ #include "DatabaseOpenWidget.h" #include "ui_DatabaseOpenWidget.h" -#include - #include "core/Config.h" #include "core/Database.h" #include "gui/FileDialog.h" +#include "gui/MessageBox.h" #include "format/KeePass2Reader.h" #include "keys/FileKey.h" #include "keys/PasswordKey.h" @@ -115,8 +114,8 @@ void DatabaseOpenWidget::openDatabase() Q_EMIT editFinished(true); } else { - QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1") - .arg(reader.errorString())); + MessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1") + .arg(reader.errorString())); m_ui->editPassword->clear(); } } @@ -136,7 +135,7 @@ CompositeKey DatabaseOpenWidget::databaseKey() QString keyFilename = m_ui->comboKeyFile->currentText(); QString errorMsg; if (!key.load(keyFilename, &errorMsg)) { - QMessageBox::warning(this, tr("Error"), tr("Can't open key file:\n%1").arg(errorMsg)); + MessageBox::warning(this, tr("Error"), tr("Can't open key file:\n%1").arg(errorMsg)); return CompositeKey(); } masterKey.addKey(key); diff --git a/src/gui/DatabaseTabWidget.cpp b/src/gui/DatabaseTabWidget.cpp index f6b608b01..cf701b4bc 100644 --- a/src/gui/DatabaseTabWidget.cpp +++ b/src/gui/DatabaseTabWidget.cpp @@ -18,7 +18,6 @@ #include "DatabaseTabWidget.h" #include -#include #include #include "autotype/AutoType.h" @@ -30,6 +29,7 @@ #include "gui/DatabaseWidget.h" #include "gui/DragTabBar.h" #include "gui/FileDialog.h" +#include "gui/MessageBox.h" #include "gui/entry/EntryView.h" #include "gui/group/GroupView.h" @@ -106,7 +106,7 @@ void DatabaseTabWidget::openDatabase(const QString& fileName, const QString& pw, QFileInfo fileInfo(fileName); QString canonicalFilePath = fileInfo.canonicalFilePath(); if (canonicalFilePath.isEmpty()) { - QMessageBox::warning(this, tr("Warning"), tr("File not found!")); + MessageBox::warning(this, tr("Warning"), tr("File not found!")); return; } @@ -191,7 +191,7 @@ bool DatabaseTabWidget::closeDatabase(Database* db) } if (dbStruct.dbWidget->currentMode() == DatabaseWidget::EditMode && db->hasKey()) { QMessageBox::StandardButton result = - QMessageBox::question( + MessageBox::question( this, tr("Close?"), tr("\"%1\" is in edit mode.\nClose anyway?").arg(dbName), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); @@ -205,7 +205,7 @@ bool DatabaseTabWidget::closeDatabase(Database* db) } else { QMessageBox::StandardButton result = - QMessageBox::question( + MessageBox::question( this, tr("Save changes?"), tr("\"%1\" was modified.\nSave changes?").arg(dbName), QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Yes); @@ -270,8 +270,8 @@ void DatabaseTabWidget::saveDatabase(Database* db) updateTabName(db); } else { - QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" - + saveFile.errorString()); + MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" + + saveFile.errorString()); } } else { @@ -309,8 +309,8 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db) updateLastDatabases(dbStruct.filePath); } else { - QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" - + saveFile.errorString()); + MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n" + + saveFile.errorString()); } } } diff --git a/src/gui/DatabaseWidget.cpp b/src/gui/DatabaseWidget.cpp index 52ec89286..ef33c01cb 100644 --- a/src/gui/DatabaseWidget.cpp +++ b/src/gui/DatabaseWidget.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -36,6 +35,7 @@ #include "gui/DatabaseOpenWidget.h" #include "gui/DatabaseSettingsWidget.h" #include "gui/KeePass1OpenWidget.h" +#include "gui/MessageBox.h" #include "gui/UnlockDatabaseWidget.h" #include "gui/entry/EditEntryWidget.h" #include "gui/entry/EntryView.h" @@ -248,14 +248,14 @@ void DatabaseWidget::deleteEntries() QMessageBox::StandardButton result; if (selected.size() == 1) { - result = QMessageBox::question( + result = MessageBox::question( this, tr("Delete entry?"), tr("Do you really want to delete the entry \"%1\" for good?") .arg(selectedEntries.first()->title()), QMessageBox::Yes | QMessageBox::No); } else { - result = QMessageBox::question( + result = MessageBox::question( this, tr("Delete entries?"), tr("Do you really want to delete %1 entries for good?") .arg(selected.size()), @@ -270,7 +270,7 @@ void DatabaseWidget::deleteEntries() } else { if (selected.size() > 1) { - QMessageBox::StandardButton result = QMessageBox::question( + QMessageBox::StandardButton result = MessageBox::question( this, tr("Move entries to recycle bin?"), tr("Do you really want to move %1 entries to the recycle bin?") .arg(selected.size()), @@ -371,7 +371,7 @@ void DatabaseWidget::deleteGroup() bool inRecylceBin = Tools::hasChild(m_db->metadata()->recycleBin(), currentGroup); if (inRecylceBin || !m_db->metadata()->recycleBinEnabled()) { - QMessageBox::StandardButton result = QMessageBox::question( + QMessageBox::StandardButton result = MessageBox::question( this, tr("Delete group?"), tr("Do you really want to delete the group \"%1\" for good?") .arg(currentGroup->name()), diff --git a/src/gui/EditWidgetIcons.cpp b/src/gui/EditWidgetIcons.cpp index d5e824f13..396448dab 100644 --- a/src/gui/EditWidgetIcons.cpp +++ b/src/gui/EditWidgetIcons.cpp @@ -19,12 +19,12 @@ #include "ui_EditWidgetIcons.h" #include -#include #include "core/Group.h" #include "core/Metadata.h" #include "core/Tools.h" #include "gui/IconModels.h" +#include "gui/MessageBox.h" IconStruct::IconStruct() : uuid(Uuid()) @@ -192,9 +192,9 @@ void EditWidgetIcons::removeCustomIcon() } } else { - QMessageBox::information(this, tr("Can't delete icon!"), - tr("Can't delete icon. Still used by %1 items.") - .arg(iconUsedCount)); + MessageBox::information(this, tr("Can't delete icon!"), + tr("Can't delete icon. Still used by %1 items.") + .arg(iconUsedCount)); } } } diff --git a/src/gui/KeePass1OpenWidget.cpp b/src/gui/KeePass1OpenWidget.cpp index 3de8e88e3..5f23d8070 100644 --- a/src/gui/KeePass1OpenWidget.cpp +++ b/src/gui/KeePass1OpenWidget.cpp @@ -19,12 +19,12 @@ #include #include -#include #include "ui_DatabaseOpenWidget.h" #include "core/Database.h" #include "core/Metadata.h" #include "format/KeePass1Reader.h" +#include "gui/MessageBox.h" KeePass1OpenWidget::KeePass1OpenWidget(QWidget* parent) : DatabaseOpenWidget(parent) @@ -64,8 +64,8 @@ void KeePass1OpenWidget::openDatabase() Q_EMIT editFinished(true); } else { - QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1") - .arg(reader.errorString())); + MessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1") + .arg(reader.errorString())); m_ui->editPassword->clear(); } } diff --git a/src/gui/MessageBox.cpp b/src/gui/MessageBox.cpp new file mode 100644 index 000000000..643ceb0e5 --- /dev/null +++ b/src/gui/MessageBox.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2013 Felix Geyer + * + * This program 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 or (at your option) + * version 3 of the License. + * + * This program 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 this program. If not, see . + */ + +#include "MessageBox.h" + +QMessageBox::StandardButton MessageBox::m_nextAnswer(QMessageBox::NoButton); + +QMessageBox::StandardButton MessageBox::critical(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons, + QMessageBox::StandardButton defaultButton) +{ + if (m_nextAnswer == QMessageBox::NoButton) { + return QMessageBox::critical(parent, title, text, buttons, defaultButton); + } + else { + QMessageBox::StandardButton returnButton = m_nextAnswer; + m_nextAnswer = QMessageBox::NoButton; + return returnButton; + } +} + +QMessageBox::StandardButton MessageBox::information(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons, + QMessageBox::StandardButton defaultButton) +{ + if (m_nextAnswer == QMessageBox::NoButton) { + return QMessageBox::information(parent, title, text, buttons, defaultButton); + } + else { + QMessageBox::StandardButton returnButton = m_nextAnswer; + m_nextAnswer = QMessageBox::NoButton; + return returnButton; + } +} + +QMessageBox::StandardButton MessageBox::question(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons, + QMessageBox::StandardButton defaultButton) +{ + if (m_nextAnswer == QMessageBox::NoButton) { + return QMessageBox::question(parent, title, text, buttons, defaultButton); + } + else { + QMessageBox::StandardButton returnButton = m_nextAnswer; + m_nextAnswer = QMessageBox::NoButton; + return returnButton; + } +} + +QMessageBox::StandardButton MessageBox::warning(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons, + QMessageBox::StandardButton defaultButton) +{ + if (m_nextAnswer == QMessageBox::NoButton) { + return QMessageBox::warning(parent, title, text, buttons, defaultButton); + } + else { + QMessageBox::StandardButton returnButton = m_nextAnswer; + m_nextAnswer = QMessageBox::NoButton; + return returnButton; + } +} + +void MessageBox::setNextAnswer(QMessageBox::StandardButton button) +{ + m_nextAnswer = button; +} + diff --git a/src/gui/MessageBox.h b/src/gui/MessageBox.h new file mode 100644 index 000000000..abb12c072 --- /dev/null +++ b/src/gui/MessageBox.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2013 Felix Geyer + * + * This program 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 or (at your option) + * version 3 of the License. + * + * This program 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 this program. If not, see . + */ + +#ifndef KEEPASSX_MESSAGEBOX_H +#define KEEPASSX_MESSAGEBOX_H + +#include + +#include "core/Global.h" + +class MessageBox +{ +public: + static QMessageBox::StandardButton critical(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons = QMessageBox::Ok, + QMessageBox::StandardButton defaultButton = QMessageBox::NoButton); + static QMessageBox::StandardButton information(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons = QMessageBox::Ok, + QMessageBox::StandardButton defaultButton = QMessageBox::NoButton); + static QMessageBox::StandardButton question(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons = QMessageBox::Ok, + QMessageBox::StandardButton defaultButton = QMessageBox::NoButton); + static QMessageBox::StandardButton warning(QWidget* parent, + const QString& title, const QString& text, + QMessageBox::StandardButtons buttons = QMessageBox::Ok, + QMessageBox::StandardButton defaultButton = QMessageBox::NoButton); + + static void setNextAnswer(QMessageBox::StandardButton button); + +private: + static QMessageBox::StandardButton m_nextAnswer; +}; + +#endif // KEEPASSX_MESSAGEBOX_H diff --git a/src/gui/UnlockDatabaseWidget.cpp b/src/gui/UnlockDatabaseWidget.cpp index 133dd4216..7ad089a94 100644 --- a/src/gui/UnlockDatabaseWidget.cpp +++ b/src/gui/UnlockDatabaseWidget.cpp @@ -17,10 +17,9 @@ #include "UnlockDatabaseWidget.h" -#include - #include "ui_DatabaseOpenWidget.h" #include "core/Database.h" +#include "gui/MessageBox.h" UnlockDatabaseWidget::UnlockDatabaseWidget(QWidget* parent) : DatabaseOpenWidget(parent) @@ -50,7 +49,7 @@ void UnlockDatabaseWidget::openDatabase() Q_EMIT editFinished(true); } else { - QMessageBox::warning(this, tr("Error"), tr("Wrong key.")); + MessageBox::warning(this, tr("Error"), tr("Wrong key.")); m_ui->editPassword->clear(); } } diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index e043e3561..7bf4c62ed 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include "core/Database.h" @@ -35,6 +34,7 @@ #include "gui/EditWidgetIcons.h" #include "gui/EditWidgetProperties.h" #include "gui/FileDialog.h" +#include "gui/MessageBox.h" #include "gui/entry/AutoTypeAssociationsModel.h" #include "gui/entry/EntryAttachmentsModel.h" #include "gui/entry/EntryAttributesModel.h" @@ -381,7 +381,7 @@ void EditEntryWidget::saveEntry() } if (!passwordsEqual()) { - QMessageBox::warning(this, tr("Error"), tr("Different passwords supplied.")); + MessageBox::warning(this, tr("Error"), tr("Different passwords supplied.")); return; } @@ -614,14 +614,14 @@ void EditEntryWidget::insertAttachment() QFile file(filename); if (!file.open(QIODevice::ReadOnly)) { - QMessageBox::warning(this, tr("Error"), + MessageBox::warning(this, tr("Error"), tr("Unable to open file:\n").append(file.errorString())); return; } QByteArray data; if (!Tools::readAllFromDevice(&file, data)) { - QMessageBox::warning(this, tr("Error"), + MessageBox::warning(this, tr("Error"), tr("Unable to open file:\n").append(file.errorString())); return; } @@ -646,12 +646,12 @@ void EditEntryWidget::saveCurrentAttachment() QFile file(savePath); if (!file.open(QIODevice::WriteOnly)) { - QMessageBox::warning(this, tr("Error"), + MessageBox::warning(this, tr("Error"), tr("Unable to save the attachment:\n").append(file.errorString())); return; } if (file.write(attachmentData) != attachmentData.size()) { - QMessageBox::warning(this, tr("Error"), + MessageBox::warning(this, tr("Error"), tr("Unable to save the attachment:\n").append(file.errorString())); return; }