Skip to content

test: add unit tests for colormenu, clipboard and animation - #60

Closed
add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:ut-colormenu-clipboard-animation
Closed

add-uos wants to merge 1 commit into
linuxdeepin:masterfrom
add-uos:ut-colormenu-clipboard-animation

Conversation

@add-uos

@add-uos add-uos commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

What

Add GoogleTest unit tests for the UI helper classes:

  • autotests/src/test_ColorMenu.cpp — 35 cases
  • autotests/src/test_clipboard.cpp — 14 cases
  • autotests/src/test_Animation.cpp — 6 cases

Part of a 3-part series upstreaming unit tests for deepin-picker.

Why

Extend test coverage to the color menu, clipboard and animation classes.

Notes

  • These sources are discovered automatically by the CMake test framework (file(GLOB test_*.cpp), BUILD_TESTS=ON) introduced in the framework PR (test: add CMake unit test framework and tests for utils/settings/desktopinfo #59).
  • Under the default qmake build (deepin-picker.pro) these files are not referenced, so this PR does not affect the existing build and introduces no compile errors.
  • Each PR in the series is independent and can be merged in any order; the tests build once the opt-in CMake framework is present.

Verification

  • qmake build (qmake6 && make) succeeds; the test sources are not compiled by it (no test_*.o produced).
  • With the CMake framework present, all 55 cases pass with Qt6/DTK6.

Summary by Sourcery

Tests:

  • Add GoogleTest coverage for ColorMenu, Clipboard, and Animation behavior, including initialization, formatting, signals, rendering, lifecycle, and boundary cases.

Add GoogleTest unit tests for the UI helper classes:
- ColorMenu: 35 cases (construction, color actions, signal emission)
- clipboard: 14 cases (clipboard copy and color format handling)
- Animation: 6 cases (animation state and transitions)

These sources are picked up automatically by the opt-in CMake test
framework (BUILD_TESTS=ON, file(GLOB test_*.cpp)). Under the default
qmake build they are not referenced by deepin-picker.pro, so this PR
does not affect the existing build.

Verified: all 55 cases pass with Qt6/DTK6 once the framework is present.
@sourcery-ai

sourcery-ai Bot commented Aug 25, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds 55 GoogleTest cases covering ColorMenu, Clipboard, and Animation behavior using Qt fixtures and stubbed platform/settings dependencies; the tests are picked up by the opt-in CMake framework while remaining outside the default qmake build.

File-Level Changes

Change Details Files
Add GoogleTest coverage for ColorMenu construction, color-format selection, copy actions, signals, painting, and destruction.
  • Introduce Qt application setup and stub QWidget/Settings behavior to isolate menu tests.
  • Cover all supported and unknown color-type branches, state initialization, boundary colors/sizes/coordinates, copy signals, and paint/destructor paths.
  • Access protected/private implementation state for direct assertions.
autotests/src/test_ColorMenu.cpp
Add GoogleTest coverage for Clipboard construction, ownership, destruction, and color-format clipboard conversion.
  • Set up a Qt core application and stub clipboard, settings, and application-exit interactions.
  • Exercise all color-format branches plus unknown/empty types and black/white boundary colors.
  • Assert copied text and persisted color-type settings.
autotests/src/test_clipboard.cpp
Add GoogleTest coverage for Animation initialization, frame progression, painting, and cleanup.
  • Create a test subclass to invoke protected paintEvent and initialize a QApplication test fixture.
  • Stub timers and window operations to make animation behavior deterministic.
  • Verify constructor geometry and flags, repaint behavior before the frame limit, timer stop/hide/finish behavior at the limit, painting, and destruction.
autotests/src/test_Animation.cpp
Integrate the new tests through the existing opt-in CMake test discovery without changing the default qmake build.
  • Rely on the framework's file(GLOB test_*.cpp) discovery when BUILD_TESTS=ON.
  • Keep the new sources unreferenced by deepin-picker.pro.
autotests/src/test_ColorMenu.cpp
autotests/src/test_clipboard.cpp
autotests/src/test_Animation.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 3 issues

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

## Individual Comments

### Comment 1
<location path="autotests/src/test_ColorMenu.cpp" line_range="54" />
<code_context>
+class ColorMenuTest : public ::testing::Test {
+protected:
+    static void SetUpTestSuite() {
+        argc_ = 1;
+        app_ = new QApplication(argc_, nullptr);
+    }
+    static void TearDownTestSuite() {
</code_context>
<issue_to_address>
**issue (testing):** `QApplication` is constructed with `argc_ == 1` and a null `argv`; Qt expects a valid argument vector when argc is nonzero, so test-suite setup dereferences invalid argument storage and crashes before any ColorMenu test runs.

**Triggers:** When the CMake test target executes the ColorMenu test suite.

**Suggested fix:** Provide a real `argv` array, as done in `test_Animation.cpp`, or construct the application with argc set to zero.

```suggestion
        argc_ = 0;
```
</issue_to_address>

### Comment 2
<location path="autotests/src/test_Animation.cpp" line_range="82" />
<code_context>
+        stub.set_lamda(&QWidget::hide, [](QWidget *) {
+            __DBG_STUB_INVOKE__
+        });
+        // Stub QApplication::devicePixelRatio for paintEvent
+        stub.set_lamda(VADDR(QApplication, devicePixelRatio), []() -> qreal {
+            __DBG_STUB_INVOKE__
+            return 1.0;
+        });
+    }
</code_context>
<issue_to_address>
**issue (bug_risk):** The `VADDR(QApplication, devicePixelRatio)` stub is installed with a zero-argument lambda even though `devicePixelRatio` is an instance member call and receives a `QApplication *` argument; the stub therefore has an incompatible replacement signature and the test target fails to compile or invokes the replacement incorrectly.

**Triggers:** When `PaintEvent_WithValidWidget_DrawsCircleClip` calls `qApp->devicePixelRatio()`.

**Suggested fix:** Define the replacement as `[](QApplication *, ...) -> qreal` with the signature expected by `VADDR`/`set_lamda`.

```suggestion
        stub.set_lamda(VADDR(QApplication, devicePixelRatio), [](QApplication *, ...) -> qreal {
```
</issue_to_address>

### Comment 3
<location path="autotests/src/test_clipboard.cpp" line_range="73-78" />
<code_context>
+
+        obj = new Clipboard();
+
+        // Stub QApplication::clipboard() to return nullptr;
+        // QClipboard::setText will be separately stubbed at call site
+        stub.set_lamda(QApplication::clipboard, []() -> QClipboard* {
+            __DBG_STUB_INVOKE__
+            return nullptr;
+        });
+
+        // Stub QClipboard::setText(const QString&, QClipboard::Mode) to capture text
</code_context>
<issue_to_address>
**issue (testing):** The test forces `QApplication::clipboard()` to return `nullptr` and then exercises production code that unconditionally calls `clipboard->setText(colorString)`; calling an instance method through a null `QClipboard *` is undefined behavior, so the tests depend on an unsafe stub-extension implementation detail and can crash instead of testing clipboard copying.

**Triggers:** When any `copyToClipboard` test reaches the clipboard write.

**Suggested fix:** Return a real test `QClipboard` object from the `QApplication::clipboard()` stub, or avoid stubbing the accessor and install a GUI application with a usable clipboard.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

class ColorMenuTest : public ::testing::Test {
protected:
static void SetUpTestSuite() {
argc_ = 1;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (testing): QApplication is constructed with argc_ == 1 and a null argv; Qt expects a valid argument vector when argc is nonzero, so test-suite setup dereferences invalid argument storage and crashes before any ColorMenu test runs.

Triggers: When the CMake test target executes the ColorMenu test suite.

Suggested fix: Provide a real argv array, as done in test_Animation.cpp, or construct the application with argc set to zero.

Suggested change
argc_ = 1;
argc_ = 0;

__DBG_STUB_INVOKE__
});
// Stub QApplication::devicePixelRatio for paintEvent
stub.set_lamda(VADDR(QApplication, devicePixelRatio), []() -> qreal {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): The VADDR(QApplication, devicePixelRatio) stub is installed with a zero-argument lambda even though devicePixelRatio is an instance member call and receives a QApplication * argument; the stub therefore has an incompatible replacement signature and the test target fails to compile or invokes the replacement incorrectly.

Triggers: When PaintEvent_WithValidWidget_DrawsCircleClip calls qApp->devicePixelRatio().

Suggested fix: Define the replacement as [](QApplication *, ...) -> qreal with the signature expected by VADDR/set_lamda.

Suggested change
stub.set_lamda(VADDR(QApplication, devicePixelRatio), []() -> qreal {
stub.set_lamda(VADDR(QApplication, devicePixelRatio), [](QApplication *, ...) -> qreal {

Comment on lines +73 to +78
// Stub QApplication::clipboard() to return nullptr;
// QClipboard::setText will be separately stubbed at call site
stub.set_lamda(QApplication::clipboard, []() -> QClipboard* {
__DBG_STUB_INVOKE__
return nullptr;
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (testing): The test forces QApplication::clipboard() to return nullptr and then exercises production code that unconditionally calls clipboard->setText(colorString); calling an instance method through a null QClipboard * is undefined behavior, so the tests depend on an unsafe stub-extension implementation detail and can crash instead of testing clipboard copying.

Triggers: When any copyToClipboard test reaches the clipboard write.

Suggested fix: Return a real test QClipboard object from the QApplication::clipboard() stub, or avoid stubbing the accessor and install a GUI application with a usable clipboard.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos, lzwind

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

@add-uos add-uos closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants