From c97dccf652e7ecc786eb99ec487cb6286ee141b3 Mon Sep 17 00:00:00 2001 From: wangfei Date: Mon, 14 Aug 2023 16:06:45 +0800 Subject: [PATCH] chore: link to target directly on unit test link to target directly, instead of compiling the source code. Log: Link to target directly on unit test --- CMakeLists.txt | 1 + tests/CMakeLists.txt | 24 ++++++++++-------------- tests/main.cpp | 5 +++-- tests/src/ut_dbackingstoreproxy.cpp | 5 +++-- tests/src/ut_dnotitlebarwindowhelper.cpp | 14 -------------- xcb/CMakeLists.txt | 12 ++++++++---- 6 files changed, 25 insertions(+), 36 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 63011a03..51626bc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug") else () set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Ofast") endif () +set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin/plugins/platforms) add_subdirectory(xcb) if("${PROJECT_VERSION_MAJOR}" STREQUAL "5") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 487cb069..ff9119d9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -12,23 +12,20 @@ else() find_package(Qt6 REQUIRED COMPONENTS OpenGL XcbQpaPrivate) endif() -# NOTE(sbw): 禁止语法树上的 vrp 优化,-O2/-O3 默认开启,会导致测试虚析构函数 HOOK 失败 -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-tree-vrp" CACHE STRING "disable vrp optimization" FORCE) - add_definitions(-DDXCB_VERSION=\"${DTK_VERSION}\") if(CMAKE_BUILD_TYPE STREQUAL "Release") add_definitions(-DQT_NO_DEBUG_OUTPUT=TRUE) endif() -include(${CMAKE_SOURCE_DIR}/xcb/xcb.cmake) -include(${CMAKE_SOURCE_DIR}/src/src.cmake) file(GLOB test_SRC test.h main.cpp src/*.cpp) -add_executable(${PROJECT_NAME} ${GLOBAL_HEADERS} ${GLOBAL_SOURCES} ${test_SRC} ${xcb_SRC}) +add_executable(${PROJECT_NAME} ${test_SRC}) include(${CMAKE_SOURCE_DIR}/xcb/linux.cmake) +add_definitions(-DPLUGIN_OUTPUT_PATH=\"${LIBRARY_OUTPUT_PATH}/..\") + target_compile_options(${PROJECT_NAME} PRIVATE -fno-access-control -fsanitize=address) target_link_options(${PROJECT_NAME} PRIVATE -fsanitize=address) if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") @@ -38,9 +35,15 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") target_compile_options(${PROJECT_NAME} PRIVATE -fprofile-arcs -ftest-coverage) endif() +target_include_directories(${PROJECT_NAME} PRIVATE + ${CMAKE_SOURCE_DIR}/src + ${CMAKE_SOURCE_DIR}/xcb +) + target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::GuiPrivate + Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Test GTest::GTest gmock @@ -48,16 +51,9 @@ target_link_libraries(${PROJECT_NAME} PRIVATE m gcov -ldl + dxcb ) -target_link_libraries(${PROJECT_NAME} -PRIVATE - Qt${QT_VERSION_MAJOR}::Core - Qt${QT_VERSION_MAJOR}::CorePrivate - Qt${QT_VERSION_MAJOR}::Gui - Qt${QT_VERSION_MAJOR}::GuiPrivate - Qt${QT_VERSION_MAJOR}::Widgets -) if(${QT_VERSION_MAJOR} STREQUAL "5") target_link_libraries(${PROJECT_NAME} PRIVATE diff --git a/tests/main.cpp b/tests/main.cpp index 18c4ae09..53f6fef3 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,9 +2,8 @@ // // SPDX-License-Identifier: LGPL-3.0-or-later -#include "test.h" - #include +#include #include #ifdef QT_DEBUG @@ -13,6 +12,8 @@ int main(int argc, char *argv[]) { + qputenv("QT_PLUGIN_PATH", PLUGIN_OUTPUT_PATH); + // gerrit编译时没有显示器,需要指定环境变量 if (!qEnvironmentVariableIsSet("DISPLAY")) qputenv("QT_QPA_PLATFORM", "offscreen"); diff --git a/tests/src/ut_dbackingstoreproxy.cpp b/tests/src/ut_dbackingstoreproxy.cpp index 0c470254..0539fa4a 100644 --- a/tests/src/ut_dbackingstoreproxy.cpp +++ b/tests/src/ut_dbackingstoreproxy.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "dbackingstoreproxy.h" @@ -32,13 +33,13 @@ TEST_F(TDBackingStoreProxy, useGLPaint) { ASSERT_FALSE(DBackingStoreProxy::useGLPaint(window)); window->setSurfaceType(QSurface::OpenGLSurface); - window->setProperty("_d_enableGLPaint", true); + window->setProperty("_d_enableGLPaint", QVariant::fromValue(true)); ASSERT_TRUE(DBackingStoreProxy::useGLPaint(window)); } TEST_F(TDBackingStoreProxy, useWallpaperPaint) { ASSERT_FALSE(DBackingStoreProxy::useWallpaperPaint(window)); - window->setProperty("_d_dxcb_wallpaper", true); + window->setProperty("_d_dxcb_wallpaper", QVariant::fromValue(true)); ASSERT_TRUE(DBackingStoreProxy::useWallpaperPaint(window)); } diff --git a/tests/src/ut_dnotitlebarwindowhelper.cpp b/tests/src/ut_dnotitlebarwindowhelper.cpp index 2fc429aa..4612dbec 100644 --- a/tests/src/ut_dnotitlebarwindowhelper.cpp +++ b/tests/src/ut_dnotitlebarwindowhelper.cpp @@ -45,56 +45,42 @@ TEST_F(TDNoTitlebarWindowHelper, theme) TEST_F(TDNoTitlebarWindowHelper, windowRadius) { - QPointF defaultValue(0.0, 0.0); - ASSERT_EQ(defaultValue, helper->windowRadius()); helper->setWindowRadius(QPointF(0.1, 0.1)); ASSERT_EQ(QPointF(0.1, 0.1), helper->windowRadius()); } TEST_F(TDNoTitlebarWindowHelper, borderWidth) { - int defaultValue = 0; - ASSERT_EQ(defaultValue, helper->borderWidth()); helper->setBorderWidth(2.0); ASSERT_EQ(2.0, helper->borderWidth()); } TEST_F(TDNoTitlebarWindowHelper, borderColor) { - QColor defaultValue; - ASSERT_EQ(defaultValue, helper->borderColor()); helper->setBorderColor(Qt::red); ASSERT_EQ(QColor(Qt::red), helper->borderColor()); } TEST_F(TDNoTitlebarWindowHelper, shadowRadius) { - qreal defaultValue; - ASSERT_EQ(defaultValue, helper->shadowRadius()); helper->setShadowRadius(2.0); ASSERT_EQ(2.0, helper->shadowRadius()); } TEST_F(TDNoTitlebarWindowHelper, shadowOffset) { - QPointF defaultValue; - ASSERT_EQ(defaultValue, helper->shadowOffset()); helper->setShadowOffect(QPointF(1.0, 1.0)); ASSERT_EQ(QPointF(1.0, 1.0), helper->shadowOffset()); } TEST_F(TDNoTitlebarWindowHelper, shadowColor) { - QColor defaultValue; - ASSERT_EQ(defaultValue, helper->shadowColor()); helper->setShadowColor(Qt::blue); ASSERT_EQ(QColor(Qt::blue), helper->shadowColor()); } TEST_F(TDNoTitlebarWindowHelper, mouseInputAreaMargins) { - QMarginsF defaultValue; - ASSERT_EQ(defaultValue, helper->mouseInputAreaMargins()); helper->setMouseInputAreaMargins(QMarginsF(1.0, 1.0, 1.0 ,1.0)); ASSERT_EQ(QMarginsF(1.0, 1.0, 1.0 ,1.0), helper->mouseInputAreaMargins()); } diff --git a/xcb/CMakeLists.txt b/xcb/CMakeLists.txt index 38d81eec..31c0d587 100644 --- a/xcb/CMakeLists.txt +++ b/xcb/CMakeLists.txt @@ -4,7 +4,8 @@ project(dxcb) -find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Widgets DBus) +find_package(Qt${QT_VERSION_MAJOR} ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Widgets) +find_package(DtkBuildHelper REQUIRED) if(${QT_VERSION_MAJOR} STREQUAL "5") find_package(Qt5 REQUIRED COMPONENTS XcbQpa X11Extras EdidSupport XkbCommonSupport) else() @@ -22,12 +23,15 @@ endif() include(${CMAKE_CURRENT_LIST_DIR}/xcb.cmake) include(${CMAKE_SOURCE_DIR}/src/src.cmake) -set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin/plugins/platforms) - -add_library(${PROJECT_NAME} MODULE ${GLOBAL_HEADERS} ${GLOBAL_SOURCES} ${xcb_SRC} ${DBUS_INTERFACE_XMLS}) +add_library(${PROJECT_NAME} SHARED ${GLOBAL_HEADERS} ${GLOBAL_SOURCES} ${xcb_SRC} ${DBUS_INTERFACE_XMLS}) include(${CMAKE_CURRENT_LIST_DIR}/linux.cmake) +set(EnableCov CACHE BOOL FALSE) +if (EnableCov) + dtk_setup_code_coverage(${PROJECT_NAME}) +endif() + target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Core