Skip to content

Commit

Permalink
tests: add some unit tests
Browse files Browse the repository at this point in the history
Add some unit tests. Mostly for platformthemeplugin.

Log: add some unit tests
  • Loading branch information
asterwyx committed Aug 10, 2023
1 parent 3abe01b commit 0bcaf18
Show file tree
Hide file tree
Showing 14 changed files with 1,111 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ License: CC0-1.0
# png svg dci
Files: platformthemeplugin/icons/* styleplugins/chameleon/*.svg styles/images/*.png
tests/*/icons/*/*.svg tests/*/*.svg tests/*/icons/*/*.dci
tests/imageformats/dci/*.dci
tests/imageformats/dci/*.dci tests/platformtheme/qt-theme.ini
Copyright: None
License: CC0-1.0

Expand Down
2 changes: 2 additions & 0 deletions platformthemeplugin/qdeepinfiledialoghelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
DGUI_USE_NAMESPACE

QT_BEGIN_NAMESPACE
#ifndef DIALOG_SERVICE
#define DIALOG_SERVICE "com.deepin.filemanager.filedialog"
#endif
Q_LOGGING_CATEGORY(fileDialogHelper, "dtk.qpa.fileDialogHelper");

QList<QUrl> stringList2UrlList(const QStringList &list)
Expand Down
54 changes: 54 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,74 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later

if(QT_VERSION_MAJOR EQUAL 5)
find_package(Qt5X11Extras REQUIRED)
list(APPEND QT_LIBS Qt5::X11Extras)
if(QT_VERSION_MINOR GREATER_EQUAL 8) # Qt5.8
find_package(Qt5ThemeSupport REQUIRED)
list(APPEND THEME_LIBS Qt5::ThemeSupportPrivate)
else()
list(APPEND THEME_LIBS Qt5::PlatformSupportPrivate) # TODO to be verified
endif()
set(3RD_PARTY_SOURCES
${CMAKE_SOURCE_DIR}/3rdparty/qdbustrayicon.cpp
${CMAKE_SOURCE_DIR}/3rdparty/qstatusnotifieritemadaptor.cpp
${CMAKE_SOURCE_DIR}/3rdparty/qdbusmenuconnection.cpp
)
set(3RD_PARTY_HEADERS
${CMAKE_SOURCE_DIR}/3rdparty/qdbustrayicon_p.h
${CMAKE_SOURCE_DIR}/3rdparty/qstatusnotifieritemadaptor_p.h
${CMAKE_SOURCE_DIR}/3rdparty/qdbusmenuconnection_p.h
)
qt5_add_dbus_interface(DBUS_INTERFACES ${CMAKE_SOURCE_DIR}/platformthemeplugin/xmls/com.deepin.filemanager.filedialog.xml filedialog_interface)
qt5_add_dbus_interface(DBUS_INTERFACES ${CMAKE_SOURCE_DIR}/platformthemeplugin/xmls/com.deepin.filemanager.filedialogmanager.xml filedialogmanager_interface)
else()
qt6_add_dbus_interface(DBUS_INTERFACES ${CMAKE_SOURCE_DIR}/platformthemeplugin/xmls/com.deepin.filemanager.filedialog.xml filedialog_interface)
qt6_add_dbus_interface(DBUS_INTERFACES ${CMAKE_SOURCE_DIR}/platformthemeplugin/xmls/com.deepin.filemanager.filedialogmanager.xml filedialogmanager_interface)
endif()
add_executable(unit-tests
${CMAKE_SOURCE_DIR}/iconengineplugins/svgiconengine/qsvgiconengine.h
${CMAKE_SOURCE_DIR}/imageformatplugins/svg/qsvgiohandler.h
${CMAKE_SOURCE_DIR}/imageformatplugins/dci/qdciiohandler.h
${CMAKE_SOURCE_DIR}/styleplugins/chameleon/chameleonstyle.h
${CMAKE_SOURCE_DIR}/styleplugins/chameleon/common.h
${CMAKE_SOURCE_DIR}/styleplugins/chameleon/dstyleanimation.h
${CMAKE_SOURCE_DIR}/platformthemeplugin/dthemesettings.h
${CMAKE_SOURCE_DIR}/platformthemeplugin/qdeepintheme.h
${CMAKE_SOURCE_DIR}/platformthemeplugin/qdeepinfiledialoghelper.h
${CMAKE_SOURCE_DIR}/iconengineplugins/svgiconengine/qsvgiconengine.cpp
${CMAKE_SOURCE_DIR}/imageformatplugins/svg/qsvgiohandler.cpp
${CMAKE_SOURCE_DIR}/imageformatplugins/dci/qdciiohandler.cpp
${CMAKE_SOURCE_DIR}/styleplugins/chameleon/chameleonstyle.cpp
${CMAKE_SOURCE_DIR}/styleplugins/chameleon/dstyleanimation.cpp
${CMAKE_SOURCE_DIR}/platformthemeplugin/dthemesettings.cpp
${CMAKE_SOURCE_DIR}/platformthemeplugin/qdeepintheme.cpp
${CMAKE_SOURCE_DIR}/platformthemeplugin/qdeepinfiledialoghelper.cpp
iconengines/svgiconengine/ut_qsvgiconengine.cpp
iconengines/svgiconengine/icons.qrc
imageformats/dci/ut_qdciiohandler.cpp
imageformats/dci/dci_test_files.qrc
imageformats/svg/ut_qsvgiohandler.cpp
styleplugins/chameleon/ut_chameleonstyle.cpp
styleplugins/chameleon/icons/theme-icons.qrc
platformtheme/ut_dthemesettings.cpp
platformtheme/ut_qdeepintheme.cpp
platformtheme/ut_qdeepinfiledialoghelper.cpp
platformtheme/test-files.qrc
platformtheme/filedialogservice.h
platformtheme/filedialogservice.cpp
platformtheme/filedialogmanagerservice.h
platformtheme/filedialogmanagerservice.cpp
ut_pluginloading.cpp
main.cpp
${3RD_PARTY_SOURCES}
${3RD_PARTY_HEADERS}
${DBUS_INTERFACES}
)
find_package(GTest REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Concurrent REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11)
target_compile_options(unit-tests PRIVATE
-g
-fno-access-control
Expand All @@ -46,9 +91,17 @@ target_link_libraries(unit-tests PRIVATE
Qt${QT_VERSION_MAJOR}::Concurrent
Qt${QT_VERSION_MAJOR}::CorePrivate
Qt${QT_VERSION_MAJOR}::GuiPrivate
${THEME_LIBS}
${QT_LIBS}
Qt${QT_VERSION_MAJOR}::WidgetsPrivate
asan
gcov
PkgConfig::X11
)
target_compile_definitions(unit-tests PRIVATE
DIALOG_SERVICE="org.deepin.fakefilemanager.filedialog"
META_FILE_BASE_DIR="${CMAKE_SOURCE_DIR}"
PLUGIN_OUTPUT_BASE_DIR="${PLUGIN_OUTPUT_BASE_DIR}"
)
target_include_directories(unit-tests PRIVATE
${CMAKE_SOURCE_DIR}/iconengineplugins/builtinengine
Expand All @@ -57,6 +110,7 @@ target_include_directories(unit-tests PRIVATE
${CMAKE_SOURCE_DIR}/imageformatplugins/svg
${CMAKE_SOURCE_DIR}/imageformatplugins/dci
${CMAKE_SOURCE_DIR}/styleplugins/chameleon
${CMAKE_SOURCE_DIR}/platformthemeplugin
)
target_compile_definitions(unit-tests PRIVATE
-DUT_PLATFORMPLUGIN_PATH="${CMAKE_BINARY_DIR}/plugins"
Expand Down
81 changes: 81 additions & 0 deletions tests/platformtheme/filedialogmanagerservice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "filedialogmanagerservice.h"
#include <QUuid>
#include <QDBusConnection>
FileDialogManagerService::FileDialogManagerService(const QString &service,
const QString &path)
: m_service{service},
m_path{path},
m_useFileChooserDialog{true},
m_errorMsg{}
{
registerAll();
}

FileDialogManagerService::~FileDialogManagerService()
{
unregisterAll();
}

bool FileDialogManagerService::registerAll()
{
QDBusConnection conn = QDBusConnection::sessionBus();
if (!conn.registerService(m_service)) {
m_errorMsg = QString("Cannot register service %1").arg(m_service);
return false;
}
if (!conn.registerObject(m_path, this, QDBusConnection::ExportAllContents)) {
m_errorMsg = QString("Cannot register object %1").arg(m_path);
return false;
}
return true;
}

bool FileDialogManagerService::unregisterAll()
{
QDBusConnection conn = QDBusConnection::sessionBus();
conn.unregisterObject(m_path);
if (!conn.unregisterService(m_service)) {
m_errorMsg = QString("Cannot unregister service %1").arg(m_service);
return false;
}
return true;
}

QList<QDBusObjectPath> FileDialogManagerService::dialogs()

Check warning on line 48 in tests/platformtheme/filedialogmanagerservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'dialogs' is never used.
{
return m_dialogMap.keys();
}

QDBusObjectPath FileDialogManagerService::createDialog(const QString key)

Check warning on line 53 in tests/platformtheme/filedialogmanagerservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'key' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
QString path{"/com/deepin/filemanager/filedialog/"};
if (key.isEmpty()) {
path.append(QUuid::createUuid().toRfc4122().toHex());
} else {
path.append(key);
}
FileDialogService *dialogService = new FileDialogService(path);
m_dialogMap.insert(QDBusObjectPath{path}, dialogService);
return QDBusObjectPath{path};
}

void FileDialogManagerService::destroyDialog(const QDBusObjectPath &path)

Check warning on line 66 in tests/platformtheme/filedialogmanagerservice.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'destroyDialog' is never used.
{
auto dialog = m_dialogMap.find(path);
dialog.value()->deleteLater();
m_dialogMap.erase(dialog);
}

bool FileDialogManagerService::isUseFileChooserDialog()
{
return m_useFileChooserDialog;
}

QString FileDialogManagerService::errorString()
{
return m_errorMsg;
}
58 changes: 58 additions & 0 deletions tests/platformtheme/filedialogmanagerservice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#ifndef FILEDIALOGMANAGERSERVICE_H
#define FILEDIALOGMANAGERSERVICE_H
#include <QDBusContext>
#include <QDBusObjectPath>
#include "filedialogservice.h"
#include <QDBusConnection>

class FileDialogManagerService : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "com.deepin.filemanager.filedialogmanager")
Q_CLASSINFO("D-Bus Introspection", ""
" <interface name=\"com.deepin.filemanager.filedialogmanager\">\n"
" <method name=\"createDialog\">\n"
" <arg direction=\"in\" type=\"s\" name=\"key\"/>\n"
" <arg direction=\"out\" type=\"o\" name=\"path\"/>\n"
" </method>\n"
" <method name=\"destroyDialog\">\n"
" <arg direction=\"in\" type=\"o\" name=\"path\"/>\n"
" </method>\n"
" <method name=\"dialogs\">\n"
" <arg direction=\"out\" type=\"ao\" name=\"dialogObjectList\"/>\n"
" </method>\n"
" <method name=\"errorString\">\n"
" <arg direction=\"out\" type=\"s\" name=\"error\"/>\n"
" </method>\n"
" <method name=\"isUseFileChooserDialog\">\n"
" <arg direction=\"out\" type=\"b\" name=\"ok\"/>\n"
" </method>\n"
" </interface>\n"
"")

public:
FileDialogManagerService(const QString &service, const QString &path);
~FileDialogManagerService();

public Q_SLOTS:
Q_SCRIPTABLE QDBusObjectPath createDialog(const QString key);

Check warning on line 42 in tests/platformtheme/filedialogmanagerservice.h

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'key' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
void destroyDialog(const QDBusObjectPath &path);
QList<QDBusObjectPath> dialogs();
QString errorString();
bool isUseFileChooserDialog();

private:
bool registerAll();
bool unregisterAll();
QMap<QDBusObjectPath, FileDialogService*> m_dialogMap;
QString m_service;
QString m_path;
bool m_useFileChooserDialog;
QString m_errorMsg;
};

#endif // FILEDIALOGMANAGERSERVICE_H
Loading

0 comments on commit 0bcaf18

Please sign in to comment.