Skip to content

Commit

Permalink
Initial Catch2 integration for PassphraseGenerator GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaslenko committed Mar 8, 2023
1 parent cb4fbbc commit 8361b22
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests2/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,7 @@ target_link_libraries(testsupport2 Qt5::Core Catch2::Catch2WithMain)
add_unit_test(NAME pass-phrase-generator
SOURCES core/TestPassphraseGenerator.cpp
LIBS testsupport2 ${TEST_LIBRARIES})

#if(WITH_GUI_TESTS)
add_subdirectory(gui)
#endif(WITH_GUI_TESTS)
31 changes: 31 additions & 0 deletions tests2/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
#
# 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_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/src
${CMAKE_CURRENT_BINARY_DIR}/../src)

set(TEST_LIBRARIES keepassx_core Catch2::Catch2WithMain)

set(testsupport_SOURCES main.cpp GuiTestFixture.cpp)

add_library(testsupport3 STATIC ${testsupport_SOURCES})
target_link_libraries(testsupport3 Qt5::Core Qt5::Concurrent Qt5::Widgets Catch2::Catch2WithMain)

add_unit_test(NAME pass-phrase-generator-gui
SOURCES TestPassphraseGeneratorGui.cpp
LIBS testsupport3 ${TEST_LIBRARIES})
93 changes: 93 additions & 0 deletions tests2/gui/GuiTestFixture.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
*
* 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 "GuiTestFixture.h"
#include "catch2/catch_all.hpp"

#include <QApplication>
#include <QToolBar>

GuiTestFixture::GuiTestFixture() {
m_mainWindow.reset(new MainWindow());
m_mainWindow->show();
m_mainWindow->resize(1024, 768);
m_mainWindow->activateWindow();

QApplication::processEvents();

init();
}

// Every test starts with resetting config settings and opening the temp database
void GuiTestFixture::init()
{
// Reset config to defaults
config()->resetToDefaults();
// Disable autosave so we can test the modified file indicator
config()->set(Config::AutoSaveAfterEveryChange, false);
config()->set(Config::AutoSaveOnExit, false);
// Enable the tray icon so we can test hiding/restoring the windowQByteArray
config()->set(Config::GUI_ShowTrayIcon, true);
// Disable the update check first time alert
config()->set(Config::UpdateCheckMessageShown, true);
// Disable quick unlock
config()->set(Config::Security_QuickUnlock, false);
// Disable atomic saves to prevent transient errors on some platforms
config()->set(Config::UseAtomicSaves, false);
// Disable showing expired entries on unlock
config()->set(Config::GUI_ShowExpiredEntriesOnDatabaseUnlock, false);
/*
// Copy the test database file to the temporary file
auto origFilePath = QDir(KEEPASSX_TEST_DATA_DIR).absoluteFilePath("NewDatabase.kdbx");
QVERIFY(m_dbFile.copyFromFile(origFilePath));
m_dbFileName = QFileInfo(m_dbFile.fileName()).fileName();
m_dbFilePath = m_dbFile.fileName();
// make sure window is activated or focus tests may fail
m_mainWindow->activateWindow();
QApplication::processEvents();
fileDialog()->setNextFileName(m_dbFilePath);
triggerAction("actionDatabaseOpen");
QApplication::processEvents();
m_dbWidget = m_tabWidget->currentDatabaseWidget();
auto* databaseOpenWidget = m_tabWidget->currentDatabaseWidget()->findChild<QWidget*>("databaseOpenWidget");
QVERIFY(databaseOpenWidget);
// editPassword is not QLineEdit anymore but PasswordWidget
auto* editPassword =
databaseOpenWidget->findChild<PasswordWidget*>("editPassword")->findChild<QLineEdit*>("passwordEdit");
QVERIFY(editPassword);
editPassword->setFocus();
QTest::keyClicks(editPassword, "a");
QTest::keyClick(editPassword, Qt::Key_Enter);
QTRY_VERIFY(!m_dbWidget->isLocked());
m_db = m_dbWidget->database();*/

QApplication::processEvents();
}

QToolBar* GuiTestFixture::findToolBar()
{
auto* toolBar = m_mainWindow->findChild<QToolBar*>("toolBar");
REQUIRE(toolBar);

return toolBar;
}
42 changes: 42 additions & 0 deletions tests2/gui/GuiTestFixture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
*
* 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 KEEPASSXC_GUITESTFIXTURE_H
#define KEEPASSXC_GUITESTFIXTURE_H

#include "gui/MainWindow.h"
#include "core/Tools.h"

class GuiTestFixture
{
public:
GuiTestFixture();

protected:
QToolBar* findToolBar();

static void wait(int ms = 1000) {
Tools::wait(ms);
}

protected:
QScopedPointer<MainWindow> m_mainWindow;

private:
void init();
};

#endif // KEEPASSXC_GUITESTFIXTURE_H
34 changes: 34 additions & 0 deletions tests2/gui/TestPassphraseGeneratorGui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
*
* 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 "../streams.h"
#include "catch2/catch_all.hpp"
#include "GuiTestFixture.h"

#include <QApplication>

TEST_CASE_METHOD(GuiTestFixture, "Testing PassphraseGenerator GUI", "[gui]")
{
// wait();

auto* toolBar = findToolBar();
REQUIRE(toolBar);

SECTION("TODO")
{
REQUIRE(1 == 1);
}
}
48 changes: 48 additions & 0 deletions tests2/gui/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 KeePassXC Team <team@keepassxc.org>
*
* 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 "catch2/catch_all.hpp"
#include "crypto/Crypto.h"
#include "gui/Application.h"
#include "core/Config.h"

// TODO: use from the config-keepassx.h
#define KEEPASSXC_VERSION "2.8.0-snapshot"

int main(int argc, char** argv)
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
Application app(argc, argv);
Application::setApplicationName("KeePassXC");
Application::setApplicationVersion(KEEPASSXC_VERSION);
Application::setQuitOnLastWindowClosed(false);
Application::setAttribute(Qt::AA_Use96Dpi, true);
app.applyTheme();

// Add your common setup for all unit tests here...
Crypto::init();
Config::createTempFileInstance();
Application::bootstrap();

int result = Catch::Session().run(argc, argv);

// Add your clean-up here...

return result;
}

0 comments on commit 8361b22

Please sign in to comment.