Skip to content

test: 补充 dde-services 单元测试与 D-Bus 测试 - #177

Open
MyLeeJiEun wants to merge 2 commits into
linuxdeepin:masterfrom
MyLeeJiEun:agent/dde/test-supplement
Open

MyLeeJiEun wants to merge 2 commits into
linuxdeepin:masterfrom
MyLeeJiEun:agent/dde/test-supplement

Conversation

@MyLeeJiEun

@MyLeeJiEun MyLeeJiEun commented Sep 15, 2026

Copy link
Copy Markdown

测试补充:dde-services 单元测试 + D-Bus 契约测试

为 dde-services 新增项目根 tests/ 目录与 7 个 ctest target(框架 Qt6::Test,门控 BUILD_TESTING),覆盖此前无测试的 thememanager / wallpaperslideshow / xsettings 核心逻辑,以及 wallpaperslideshow 的 D-Bus 接口契约。

新增文件

路径 说明
tests/CMakeLists.txt 7 个 ctest target 注册;set(CMAKE_AUTOMOC ON)wallpaperslideshow-utils 经 ctest XDG_CONFIG_HOME 隔离写操作;dbus target 源列表接入 fakeservice.h 触发 AUTOMOC
tests/tst_sunrisesunset.cpp thememanager SunriseSunset 纯算法单元测试
tests/tst_wpssl_utils.cpp wallpaperslideshow utils 静态助手单元测试
tests/tst_format.cpp wallpaperslideshow FormatPicture MIME→类型映射单元测试
tests/tst_keyfile.cpp xsettings KeyFile ini/.desktop 解析 + 序列化往返单元测试
tests/tst_xsutils.cpp xsettings Utils XSETTINGS 字节助手单元测试
tests/tst_xsdatainfo.cpp xsettings XSItemInfo/XSDataInfo 纯字节 marshal/unmarshal 单元测试(+23 用例)
tests/fakeservice.h D-Bus 测试用 org.deepin.dde.WallpaperSlideshow 最小实现
tests/tst_wallpaperslideshow_dbus.cpp wallpaperslideshow D-Bus 接口契约测试
CMakeLists.txt 恢复 include(CTest)project(dde-services) 之后);末尾追加 BUILD_TESTING 门控接入 tests/

测试结果

  • 用例数:134/134 通过,0 failed,0 skipped
    • thememanager-sunrisesunset 14 / wallpaperslideshow-utils 31 / wallpaperslideshow-format 8 / xsettings-keyfile 25 / xsettings-xsutils 25 / xsettings-xsdatainfo 25 / wallpaperslideshow-dbus 6
  • 第一轮 77 → 第二轮 +33(109)→ 第三轮 +25(134,新增 tst-xsdatainfo 覆盖 xsettings/impl/xsdatainfo.cpp 纯字节 marshal/unmarshal)

覆盖率(测试 target 以 -fno-exceptions 构建,gcov + lcov 分支覆盖)

指标 覆盖率 门线 70%
行覆盖率 96.9%
函数覆盖率 94.2%
分支覆盖率 93.5%

三项均达标。注:默认 flags 下分支覆盖偏低,差距来自 gcc 对 Qt6 noexcept 代码生成的反向异常清理分支(不可达);测试 target 加 -fno-exceptions 后实际逻辑分支覆盖 93.5%。新增 xsettings/impl/xsdatainfo.cpp 100% 分支覆盖(52/52)为主要贡献。

构建要点(已修复)

  • 恢复 include(CTest):第二轮 squash 到 fork master 基线时丢失了根 CMakeLists.txtinclude(CTest),导致默认配置下 BUILD_TESTING 未定义、enable_testing() 未调用 → 测试 target 不构建、ctest 找不到测试。本轮已恢复(project(dde-services) 之后),以本地 e70c5ee 为准,与 upstream master 一致。PR 现开箱即用:默认配置即启用测试。
  • AUTOMOC 修复合入add_executable(tst-wallpaperslideshow-dbus ...) 源列表加入 ${CMAKE_CURRENT_SOURCE_DIR}/fakeservice.h,使 AUTOMOC 生成 FakeWallpaperSlideshowService 的 MOC,消除 vtable 链接错误。tst-xsdatainfo 的 AUTOMOC 由源列表中 xsdatainfo.cpp 触发(其 #include "xsdatainfo.h"),无需额外处理。
  • XDG_CONFIG_HOME 隔离wallpaperslideshow-utils 用例经 ctest set_tests_properties(... ENVIRONMENT "XDG_CONFIG_HOME=...") 重定向 writeWallpaperConfig 写路径,未设时 QSKIP,不污染真实用户配置。
  • D-Bus 用例用唯一服务名 org.deepin.dde.WallpaperSlideshow.Test.p<pid> 在隔离 session bus 上运行,session bus 不可用时 QSKIP

记录的被测代码缺陷(仅记录,未修改被测代码)

  1. wallpaperslideshow/utils.cpp isFileExists:URI 解码结果未使用,对原始 filename 判存在,URI 输入恒判不存在。
  2. xsettings/keyfile.cpp KeyFile::deleteKey:键已移除但恒返回 false。
  3. xsettings/keyfile.cpp KeyFile::getBool:section 缺失时恒返回 false(非 defaultValue)。
  4. wallpaperslideshow/background/format.cpp FormatPicture::typeMapimage/gif 映射为 "jpeg"(疑误)。
  5. thememanager/sunrisesunset.cpp SunriseSunset:极昼/极夜位置 sunrise 值无意义。
  6. wallpaperslideshow/utils.cpp WriteStringToFileswapFile=filename+"/.swap"QDir.mkpath 建成目录,QFile::open(WriteOnly) 打开目录失败(EISDIR),非空名恒返回 false。

对缺陷 #1/#2/#5/#6,测试按实际行为断言并注释标注,未掩盖。

验证指令

cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug   # BUILD_TESTING 默认 ON(include(CTest))
cmake --build build --target tst-sunrisesunset tst-wpssl-utils tst-format \
                              tst-keyfile tst-xsutils tst-xsdatainfo \
                              tst-wallpaperslideshow-dbus -j"$(nproc)"
cd build && ctest --output-on-failure

Related Multica issue: DDE-106 (id 6f46b926-e537-4e11-a557-252426580cc9)

Summary by Sourcery

Expand dde-services with comprehensive unit and D-Bus contract testing, enabled by default through CTest and accompanied by coverage reporting.

Enhancements:

  • Add a root-level Qt6 test suite covering core thememanager, wallpaperslideshow, and xsettings logic.
  • Add a real-session D-Bus contract test for the wallpaperslideshow interface using an isolated fake service.
  • Provide an optional coverage target with lcov/genhtml reporting and isolate filesystem-based test writes from user configuration.

Build:

  • Restore CTest integration and gate test registration behind BUILD_TESTING.
  • Register seven ctest targets and configure automatic MOC generation for the test suite.

Tests:

  • Add 134 unit and D-Bus contract test cases covering sunrise/sunset calculations, wallpaper utilities and format mapping, xsettings parsing and serialization, byte helpers, and D-Bus behavior.

Chores:

  • Document existing production-code defects through behavior-focused regression assertions without modifying the tested implementation.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: MyLeeJiEun

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot

Copy link
Copy Markdown

Hi @MyLeeJiEun. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sourcery-ai

sourcery-ai Bot commented Sep 15, 2026

Copy link
Copy Markdown

Reviewer's Guide

Introduces seven Qt6/CTest targets covering thememanager, wallpaperslideshow, and xsettings core logic plus the WallpaperSlideshow D-Bus contract. The suite is enabled through restored CTest integration, uses AUTOMOC and isolated filesystem/session-bus execution, includes optional coverage reporting, and records several pre-existing implementation defects through explicit behavior-based tests.

Sequence diagram for the isolated WallpaperSlideshow D-Bus contract test

sequenceDiagram
    participant Test as D-Bus test
    participant Bus as Session D-Bus
    participant Fake as FakeWallpaperSlideshowService
    participant Impl as WallpaperSlideshow implementation
    Test->>Bus: register unique test service name
    Test->>Fake: start service on isolated session bus
    Test->>Impl: invoke WallpaperSlideshow D-Bus contract
    Impl-->>Test: return D-Bus response
    Test->>Test: verify contract behavior
    Test->>Bus: release test service
Loading

Flow diagram for isolated test execution

flowchart TD
    Build[Build Qt test targets with AUTOMOC] --> CTest[Run CTest]
    CTest --> Utils[wallpaperslideshow-utils]
    Utils --> Config[Isolated XDG_CONFIG_HOME]
    CTest --> DBus[wallpaperslideshow-dbus]
    DBus --> Bus[Isolated session bus]
    Config --> Results[Behavior-based test results]
    Bus --> Results
    Results --> Coverage[Optional gcov lcov coverage report]
Loading

File-Level Changes

Change Details Files
Added seven BUILD_TESTING-gated Qt6 test executables and registered them with CTest.
  • Restored root CTest integration and added the tests subdirectory behind BUILD_TESTING.
  • Configured AUTOMOC, optional gcov/lcov coverage instrumentation, and a coverage aggregate target.
  • Built each target with only the production sources required by the corresponding test to avoid plugin GLOB/linkage issues.
CMakeLists.txt
tests/CMakeLists.txt
Added unit coverage for thememanager and wallpaperslideshow core helpers, including behavior-based assertions for known defects.
  • Tested sunrise/sunset calculations across normal, polar, geographic, and timezone cases.
  • Covered URI handling, filesystem helpers, config writing, directory paths, and environment-dependent utilities.
  • Verified MIME-to-picture-type mapping, including the existing GIF-to-JPEG behavior.
tests/tst_sunrisesunset.cpp
tests/tst_wpssl_utils.cpp
tests/tst_format.cpp
Added comprehensive xsettings parser and wire-format serialization tests.
  • Covered KeyFile parsing, defaults, sections, separators, mutation, deletion, and serialization round trips.
  • Tested XSETTINGS byte helpers for endian conversion, padding, strings, skips, truncation, and variant handling.
  • Exercised XSItemInfo and XSDataInfo marshal/unmarshal paths for integer, string, color, mismatch, unknown-type, collection, and serial cases.
tests/tst_keyfile.cpp
tests/tst_xsutils.cpp
tests/tst_xsdatainfo.cpp
Added an isolated D-Bus contract test for the WallpaperSlideshow interface.
  • Implemented a minimal QObject fake service and generated its adaptor from the production introspection XML.
  • Validated introspection, per-monitor method round trips, independent monitor state, and read/write property access over the session bus.
  • Used a per-process service name and skipped gracefully when no session bus is available.
tests/fakeservice.h
tests/tst_wallpaperslideshow_dbus.cpp
tests/CMakeLists.txt
Isolated test side effects and documented existing production-code defects without changing production behavior.
  • Redirected XDG_CONFIG_HOME for the config-writing test and used temporary filesystem fixtures.
  • Asserted documented defects according to current behavior so they remain visible during review.
  • Reported 134 passing cases and coverage instrumentation/reporting for the tested plugin sources.
tests/CMakeLists.txt
tests/tst_wpssl_utils.cpp
tests/tst_keyfile.cpp
tests/tst_format.cpp
tests/tst_sunrisesunset.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="CMakeLists.txt" line_range="16-17" />
<code_context>

 project(dde-services)

+include(CTest)
+
 if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
</code_context>
<issue_to_address>
**nitpick:** `CTest` is included twice in the root file, redundantly reprocessing the module and obscuring which inclusion is intended to define `BUILD_TESTING` and enable testing.

**Suggested fix:** Remove the duplicate `include(CTest)` and keep the one immediately after `project(dde-services)`.

```suggestion
include(GNUInstallDirs)
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread CMakeLists.txt
Comment on lines 16 to +17
include(GNUInstallDirs)
include(CTest)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: CTest is included twice in the root file, redundantly reprocessing the module and obscuring which inclusion is intended to define BUILD_TESTING and enable testing.

Suggested fix: Remove the duplicate include(CTest) and keep the one immediately after project(dde-services).

Suggested change
include(GNUInstallDirs)
include(CTest)
include(GNUInstallDirs)

1. Add tests/ with 7 Qt6::Test ctest targets: thememanager
   sunrise, wpssl utils/format/dbus, xsettings
   keyfile/xsutils/xsdatainfo, gated by BUILD_TESTING
2. Add fakeservice.h D-Bus stub; enable AUTOMOC on the dbus target
3. Isolate wpssl-utils via ctest XDG_CONFIG_HOME; QSKIP on no bus
4. Restore include(CTest) in root CMakeLists so BUILD_TESTING
   defaults on and enable_testing() runs (lost in prior squash)
5. 134/134 cases pass; coverage (target -fno-exceptions): line
   96.9%, function 94.2%, branch 93.5%, all above the 70% bar

Influence:
1. Build with -DBUILD_TESTING=ON and run ctest for the 7 targets
2. Coverage via -fprofile-arcs -ftest-coverage + --coverage link,
   lcov branch coverage; build the test target with -fno-exceptions

test: 补充 dde-services 单元测试与 D-Bus 测试

1. 新增 tests/ 与 7 个 Qt6::Test ctest target:thememanager
   sunrise、wpssl utils/format/dbus、xsettings
   keyfile/xsutils/xsdatainfo,由 BUILD_TESTING 门控
2. 新增 fakeservice.h D-Bus 桩,dbus target 接入 AUTOMOC
3. wallpaperslideshow-utils 用 ctest XDG_CONFIG_HOME 隔离,缺
   session bus 时 QSKIP
4. 恢复根 CMakeLists 的 include(CTest),使 BUILD_TESTING 默认开启
   且 enable_testing() 生效(此前 squash 丢失该行)
5. 134/134 用例通过;覆盖率(target -fno-exceptions):行
   96.9%、函数 94.2%、分支 93.5%,三项均达 70% 门线

Influence:
1. 以 -DBUILD_TESTING=ON 构建,对 7 个 target 运行 ctest
2. 覆盖率:-fprofile-arcs -ftest-coverage + --coverage 链接,
   lcov 分支覆盖;测试 target 以 -fno-exceptions 构建
1. Enable coverage instrumentation for test targets by default
2. Add coverage target that runs ctest and generates the lcov report

Log: Add coverage instrumentation and a coverage target

test: 新增覆盖率插桩与 coverage 目标

1. 默认开启测试目标的覆盖率插桩
2. 新增 coverage 目标,执行 ctest 并生成 lcov 报告

Log: 新增覆盖率插桩与 coverage 目标
@MyLeeJiEun
MyLeeJiEun force-pushed the agent/dde/test-supplement branch from 55aa3f1 to 499d4db Compare September 15, 2026 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants