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.
This commit is contained in:
Felix Geyer 2013-10-08 22:09:20 +02:00
parent 551637f0c2
commit d5b70b1bef
11 changed files with 176 additions and 41 deletions

View File

@ -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

View File

@ -18,11 +18,10 @@
#include "ChangeMasterKeyWidget.h"
#include "ui_ChangeMasterKeyWidget.h"
#include <QMessageBox>
#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,7 +104,7 @@ 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"),
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;
@ -114,7 +113,7 @@ void ChangeMasterKeyWidget::generateKey()
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;

View File

@ -18,11 +18,10 @@
#include "DatabaseOpenWidget.h"
#include "ui_DatabaseOpenWidget.h"
#include <QMessageBox>
#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,7 +114,7 @@ void DatabaseOpenWidget::openDatabase()
Q_EMIT editFinished(true);
}
else {
QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1")
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);

View File

@ -18,7 +18,6 @@
#include "DatabaseTabWidget.h"
#include <QFileInfo>
#include <QMessageBox>
#include <QTabWidget>
#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,7 +270,7 @@ void DatabaseTabWidget::saveDatabase(Database* db)
updateTabName(db);
}
else {
QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
}
}
@ -309,7 +309,7 @@ void DatabaseTabWidget::saveDatabaseAs(Database* db)
updateLastDatabases(dbStruct.filePath);
}
else {
QMessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
MessageBox::critical(this, tr("Error"), tr("Writing the database failed.") + "\n\n"
+ saveFile.errorString());
}
}

View File

@ -23,7 +23,6 @@
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QSplitter>
#include <QTimer>
@ -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()),

View File

@ -19,12 +19,12 @@
#include "ui_EditWidgetIcons.h"
#include <QFileDialog>
#include <QMessageBox>
#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,7 +192,7 @@ void EditWidgetIcons::removeCustomIcon()
}
}
else {
QMessageBox::information(this, tr("Can't delete icon!"),
MessageBox::information(this, tr("Can't delete icon!"),
tr("Can't delete icon. Still used by %1 items.")
.arg(iconUsedCount));
}

View File

@ -19,12 +19,12 @@
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#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,7 +64,7 @@ void KeePass1OpenWidget::openDatabase()
Q_EMIT editFinished(true);
}
else {
QMessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1")
MessageBox::warning(this, tr("Error"), tr("Unable to open the database.\n%1")
.arg(reader.errorString()));
m_ui->editPassword->clear();
}

86
src/gui/MessageBox.cpp Normal file
View File

@ -0,0 +1,86 @@
/*
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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;
}

51
src/gui/MessageBox.h Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2013 Felix Geyer <debfx@fobos.de>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSX_MESSAGEBOX_H
#define KEEPASSX_MESSAGEBOX_H
#include <QMessageBox>
#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

View File

@ -17,10 +17,9 @@
#include "UnlockDatabaseWidget.h"
#include <QMessageBox>
#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();
}
}

View File

@ -24,7 +24,6 @@
#include <QDesktopServices>
#include <QStackedLayout>
#include <QMenu>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#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;
}