test: Coverage for AboutDialog.cpp

This commit is contained in:
Nick Bolton
2024-10-12 23:03:09 +01:00
parent ce35234f18
commit c630ad0952
7 changed files with 132 additions and 11 deletions

View File

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

View File

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

View File

@ -18,25 +18,32 @@
#pragma once
#include "MainWindow.h"
#include "gui/VersionChecker.h"
#include "ui_AboutDialogBase.h"
#include <QDialog>
#include <QMainWindow>
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> deps = std::make_shared<Deps>());
int exec() override;
private:
VersionChecker m_versionChecker;
std::shared_ptr<Deps> m_pDeps;
void updateLogo() const;
void setLogo(const char *const &filename) const;
virtual QString importantDevelopers() const;
QString importantDevelopers() const;
};

View File

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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
// `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 <QPixmap>
#include <QTimer>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
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<NiceMock<DepsMock>>();
AboutDialog aboutDialog(nullptr, deps);
QTimer::singleShot(0, &aboutDialog, &QDialog::accept);
aboutDialog.exec();
const auto label = aboutDialog.findChild<QLabel *>("m_pDevelopersLabel");
EXPECT_TRUE(label->text().contains("Chris&nbsp;Schoeneman"));
}
TEST(AboutDialogTests, exec_setsCopyrightLabel) {
TestQtFullApp app;
const auto deps = std::make_shared<NiceMock<DepsMock>>();
AboutDialog aboutDialog(nullptr, deps);
QTimer::singleShot(0, &aboutDialog, &QDialog::accept);
aboutDialog.exec();
EXPECT_EQ(
aboutDialog.findChild<QLabel *>("m_pCopyrightLabel")->text(),
QString::fromStdString(deskflow::copyright()));
}
TEST(AboutDialogTests, exec_inDarkMode_usesDarkLogo) {
TestQtFullApp app;
const auto deps = std::make_shared<NiceMock<DepsMock>>();
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<QLabel *>("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<NiceMock<DepsMock>>();
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<QLabel *>("m_pLabel_Logo")->pixmap();
EXPECT_FALSE(actualLogo.isNull());
EXPECT_EQ(actualLogo.toImage(), expectedLogo.toImage());
}
#endif

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// `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"