From c630ad0952df6e86f98c83ec09af7abde8fef5fb Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Sat, 12 Oct 2024 23:03:09 +0100 Subject: [PATCH] test: Coverage for `AboutDialog.cpp` --- src/gui/src/MainWindow.cpp | 2 +- .../src => lib/gui/dialogs}/AboutDialog.cpp | 17 +++- .../src => lib/gui/dialogs}/AboutDialog.h | 19 ++-- .../gui/dialogs}/AboutDialogBase.ui | 0 src/test/CMakeLists.txt | 5 + .../gui/dialogs/AboutDialogTests.cpp | 98 +++++++++++++++++++ .../gui/dialogs/SettingsDialogTests.cpp | 2 +- 7 files changed, 132 insertions(+), 11 deletions(-) rename src/{gui/src => lib/gui/dialogs}/AboutDialog.cpp (92%) rename src/{gui/src => lib/gui/dialogs}/AboutDialog.h (77%) rename src/{gui/src => lib/gui/dialogs}/AboutDialogBase.ui (100%) create mode 100644 src/test/unittests/gui/dialogs/AboutDialogTests.cpp diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index d8312999f..773918a9e 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -18,7 +18,6 @@ #include "MainWindow.h" -#include "AboutDialog.h" #include "ServerConfigDialog.h" #include "common/constants.h" #include "gui/Logger.h" @@ -26,6 +25,7 @@ #include "gui/constants.h" #include "gui/core/CoreProcess.h" #include "gui/diagnostic.h" +#include "gui/dialogs/AboutDialog.h" #include "gui/dialogs/SettingsDialog.h" #include "gui/messages.h" #include "gui/string_utils.h" diff --git a/src/gui/src/AboutDialog.cpp b/src/lib/gui/dialogs/AboutDialog.cpp similarity index 92% rename from src/gui/src/AboutDialog.cpp rename to src/lib/gui/dialogs/AboutDialog.cpp index 1eedb9017..856a3728f 100644 --- a/src/gui/src/AboutDialog.cpp +++ b/src/lib/gui/dialogs/AboutDialog.cpp @@ -28,9 +28,20 @@ using namespace deskflow::gui; -AboutDialog::AboutDialog(MainWindow *parent) +// +// AboutDialog::Deps +// + +bool AboutDialog::Deps::isDarkMode() const { return ::isDarkMode(); } + +// +// AboutDialog +// + +AboutDialog::AboutDialog(QMainWindow *parent, std::shared_ptr deps) : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint), - Ui::AboutDialogBase() { + Ui::AboutDialogBase(), + m_pDeps(deps) { setupUi(this); @@ -65,7 +76,7 @@ void AboutDialog::setLogo(const char *const &filename) const { } void AboutDialog::updateLogo() const { - if (isDarkMode()) { + if (m_pDeps->isDarkMode()) { qDebug("showing dark logo"); setLogo(":/image/logo-dark.png"); } else { diff --git a/src/gui/src/AboutDialog.h b/src/lib/gui/dialogs/AboutDialog.h similarity index 77% rename from src/gui/src/AboutDialog.h rename to src/lib/gui/dialogs/AboutDialog.h index 32a9386ed..53d7c9df7 100644 --- a/src/gui/src/AboutDialog.h +++ b/src/lib/gui/dialogs/AboutDialog.h @@ -18,25 +18,32 @@ #pragma once -#include "MainWindow.h" -#include "gui/VersionChecker.h" #include "ui_AboutDialogBase.h" #include +#include class QWidget; class QString; class AboutDialog : public QDialog, public Ui::AboutDialogBase { Q_OBJECT + public: - explicit AboutDialog(MainWindow *parent); + struct Deps { + virtual ~Deps() = default; + virtual bool isDarkMode() const; + }; + + explicit AboutDialog( + QMainWindow *parent, + std::shared_ptr deps = std::make_shared()); int exec() override; private: - VersionChecker m_versionChecker; + std::shared_ptr m_pDeps; + void updateLogo() const; void setLogo(const char *const &filename) const; - - virtual QString importantDevelopers() const; + QString importantDevelopers() const; }; diff --git a/src/gui/src/AboutDialogBase.ui b/src/lib/gui/dialogs/AboutDialogBase.ui similarity index 100% rename from src/gui/src/AboutDialogBase.ui rename to src/lib/gui/dialogs/AboutDialogBase.ui diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 808d99c9e..fd584b270 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -16,6 +16,9 @@ macro(config_all_tests) + # Required to load images from .qrc file. + set(CMAKE_AUTORCC ON) + set(base_dir ${PROJECT_SOURCE_DIR}) set(src_dir ${base_dir}/src) set(test_base_dir ${src_dir}/test) @@ -66,6 +69,8 @@ macro(set_sources) list(APPEND sources ${PROJECT_BINARY_DIR}/src/version.rc) endif() + list(APPEND sources ${GUI_QRC_FILE}) + replace_platform_sources() replace_arch_sources() diff --git a/src/test/unittests/gui/dialogs/AboutDialogTests.cpp b/src/test/unittests/gui/dialogs/AboutDialogTests.cpp new file mode 100644 index 000000000..065694a02 --- /dev/null +++ b/src/test/unittests/gui/dialogs/AboutDialogTests.cpp @@ -0,0 +1,98 @@ +/* + * Deskflow -- mouse and keyboard sharing utility + * Copyright (C) 2024 Symless Ltd. + * + * This package is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * found in the file LICENSE that should have accompanied this file. + * + * This package 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 . + */ + +// `TestQtFullApp` freezes on Windows CI, so exclude this test for now. +#ifndef WIN32 + +#include "common/copyright.h" +#include "gui/dialogs/AboutDialog.h" +#include "shared/gui/TestQtFullApp.h" + +#include +#include +#include +#include + +using namespace testing; + +namespace { + +class DepsMock : public AboutDialog::Deps { +public: + MOCK_METHOD(bool, isDarkMode, (), (const, override)); +}; + +} // namespace + +TEST(AboutDialogTests, exec_setsDevelopersLabel) { + TestQtFullApp app; + const auto deps = std::make_shared>(); + AboutDialog aboutDialog(nullptr, deps); + QTimer::singleShot(0, &aboutDialog, &QDialog::accept); + + aboutDialog.exec(); + + const auto label = aboutDialog.findChild("m_pDevelopersLabel"); + EXPECT_TRUE(label->text().contains("Chris Schoeneman")); +} + +TEST(AboutDialogTests, exec_setsCopyrightLabel) { + TestQtFullApp app; + const auto deps = std::make_shared>(); + AboutDialog aboutDialog(nullptr, deps); + QTimer::singleShot(0, &aboutDialog, &QDialog::accept); + + aboutDialog.exec(); + + EXPECT_EQ( + aboutDialog.findChild("m_pCopyrightLabel")->text(), + QString::fromStdString(deskflow::copyright())); +} + +TEST(AboutDialogTests, exec_inDarkMode_usesDarkLogo) { + TestQtFullApp app; + const auto deps = std::make_shared>(); + AboutDialog aboutDialog(nullptr, deps); + QTimer::singleShot(0, &aboutDialog, &QDialog::accept); + EXPECT_CALL(*deps, isDarkMode()).WillOnce(Return(true)); + + aboutDialog.exec(); + + const QPixmap expectedLogo(":/image/logo-dark.png"); + const auto actualLogo = + aboutDialog.findChild("m_pLabel_Logo")->pixmap(); + EXPECT_FALSE(actualLogo.isNull()); + EXPECT_EQ(actualLogo.toImage(), expectedLogo.toImage()); +} + +TEST(AboutDialogTests, exec_notInDarkMode_usesLightLogo) { + TestQtFullApp app; + const auto deps = std::make_shared>(); + AboutDialog aboutDialog(nullptr, deps); + QTimer::singleShot(0, &aboutDialog, &QDialog::accept); + EXPECT_CALL(*deps, isDarkMode()).WillOnce(Return(false)); + + aboutDialog.exec(); + + const QPixmap expectedLogo(":/image/logo-light.png"); + const auto actualLogo = + aboutDialog.findChild("m_pLabel_Logo")->pixmap(); + EXPECT_FALSE(actualLogo.isNull()); + EXPECT_EQ(actualLogo.toImage(), expectedLogo.toImage()); +} + +#endif diff --git a/src/test/unittests/gui/dialogs/SettingsDialogTests.cpp b/src/test/unittests/gui/dialogs/SettingsDialogTests.cpp index b9a190ac4..6f09741f3 100644 --- a/src/test/unittests/gui/dialogs/SettingsDialogTests.cpp +++ b/src/test/unittests/gui/dialogs/SettingsDialogTests.cpp @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -// `TestQtFullApp` freezes on windows ci, so exclude this test for now. +// `TestQtFullApp` freezes on Windows CI, so exclude this test for now. #ifndef WIN32 #include "gui/core/CoreProcess.h"