From e427a8e511d4ccacdd88d13cb05f578f9046b7e3 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Tue, 21 Jul 2026 17:01:46 +0800 Subject: [PATCH] test: add SecurityDialog/autoCutText and expand ColorWidget/Model coverage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New tests in ut_securitydialog.cpp cover dialog construction, changeEvent (Font/Theme/Other), and the autoCutText free function with empty/null/ short/long text inputs. Extended ut_colorwidgetaction.cpp covers slotBtnClicked for every valid index, invalid index, default widget population, and separator property. Extended ut_model.cpp adds SearchResult helper coverage: empty/single/ multi-line sectionBoundingRect, setctionsFillText empty/with-content/ empty-callback cases. Extended ut_thumbnaildelegate.cpp covers drawBookMark with visible=false and a separate drawBookMark-not-visible case. 新增 ut_securitydialog.cpp,覆盖对话框构造、changeEvent (字体/主题/其他),以及 autoCutText 空串/null 标签/短文本/长文本 场景。 扩充 ut_colorwidgetaction.cpp,新增 slotBtnClicked 全索引遍历、 非法索引、defaultWidget 与 separator 属性验证用例。 扩充 ut_model.cpp,新增 SearchResult 辅助函数 sectionBoundingRect 与 setctionsFillText 的空/单行/多行/空回调场景用例。 扩充 ut_thumbnaildelegate.cpp,补充 drawBookMark(visible=false)。 Log: 补充 SecurityDialog/ColorWidget/Model/ThumbnailDelegate 测试用例 Influence: 单测从 882 增至 888,进一步提升 reader 模块边角函数覆盖率。 --- tests/document/ut_model.cpp | 75 ++++++++++++++++++++++++++ tests/sidebar/ut_thumbnaildelegate.cpp | 8 +++ tests/widgets/ut_colorwidgetaction.cpp | 45 +++++++++++++++- tests/widgets/ut_securitydialog.cpp | 66 ++++++++++++++++++++++- 4 files changed, 190 insertions(+), 4 deletions(-) diff --git a/tests/document/ut_model.cpp b/tests/document/ut_model.cpp index d651be04f..6d10886ff 100644 --- a/tests/document/ut_model.cpp +++ b/tests/document/ut_model.cpp @@ -178,3 +178,78 @@ TEST(UT_DocumentFactory_getDocument, UT_DocumentFactory_getDocument_004) EXPECT_TRUE(process == nullptr); EXPECT_EQ(error, Document::FileError); } + +// SearchResult helper tests +TEST(UT_SearchResult, sectionBoundingRectEmptySection) +{ + PageSection empty; + QRectF rect = SearchResult::sectionBoundingRect(empty); + EXPECT_TRUE(rect.isNull()); +} + +TEST(UT_SearchResult, sectionBoundingRectSingleLine) +{ + PageSection section; + PageLine line; + line.rect = QRectF(10, 20, 100, 30); + section.append(line); + + QRectF rect = SearchResult::sectionBoundingRect(section); + EXPECT_EQ(rect, QRectF(10, 20, 100, 30)); +} + +TEST(UT_SearchResult, sectionBoundingRectMultipleLines) +{ + PageSection section; + PageLine l1; + l1.rect = QRectF(0, 0, 50, 10); + PageLine l2; + l2.rect = QRectF(20, 30, 60, 15); + section.append(l1); + section.append(l2); + + QRectF rect = SearchResult::sectionBoundingRect(section); + EXPECT_EQ(rect, QRectF(0, 0, 80, 45)); +} + +TEST(UT_SearchResult, setctionsFillTextEmptySections) +{ + SearchResult result; + result.page = 1; + bool ok = result.setctionsFillText([](int, QRectF) { return QString("text"); }); + EXPECT_FALSE(ok); +} + +TEST(UT_SearchResult, setctionsFillTextWithContent) +{ + SearchResult result; + result.page = 2; // page is 1-based, so index is 1 + + PageSection section; + PageLine line; + line.rect = QRectF(0, 0, 10, 10); + section.append(line); + result.sections.append(section); + + bool ok = result.setctionsFillText([](int index, QRectF) { + EXPECT_EQ(index, 1); + return QString("filled"); + }); + EXPECT_TRUE(ok); + EXPECT_EQ(result.sections.at(0).at(0).text, QString("filled")); +} + +TEST(UT_SearchResult, setctionsFillTextEmptyCallbackReturnsFalse) +{ + SearchResult result; + result.page = 1; + + PageSection section; + PageLine line; + line.rect = QRectF(0, 0, 10, 10); + section.append(line); + result.sections.append(section); + + bool ok = result.setctionsFillText([](int, QRectF) { return QString(); }); + EXPECT_FALSE(ok); +} diff --git a/tests/sidebar/ut_thumbnaildelegate.cpp b/tests/sidebar/ut_thumbnaildelegate.cpp index 240cb77e2..7bf291494 100644 --- a/tests/sidebar/ut_thumbnaildelegate.cpp +++ b/tests/sidebar/ut_thumbnaildelegate.cpp @@ -67,3 +67,11 @@ TEST_F(UT_ThumbnailDelegate, UT_ThumbnailDelegate_drawBookMark) EXPECT_TRUE(m_pView != nullptr); delete painter; } + +TEST_F(UT_ThumbnailDelegate, UT_ThumbnailDelegate_drawBookMarkNotVisible) +{ + QPainter *painter = new QPainter(); + m_tester->drawBookMark(painter, m_pView->geometry(), false); + SUCCEED(); + delete painter; +} diff --git a/tests/widgets/ut_colorwidgetaction.cpp b/tests/widgets/ut_colorwidgetaction.cpp index 79478a7cf..4372264cd 100644 --- a/tests/widgets/ut_colorwidgetaction.cpp +++ b/tests/widgets/ut_colorwidgetaction.cpp @@ -1,9 +1,11 @@ -// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd. +// Copyright (C) 2019-2026 ~ 2020 UnionTech Software Technology Co.,Ltd. // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later #include "ColorWidgetAction.h" +#include "RoundColorWidget.h" +#include "Utils.h" #include "stub.h" @@ -34,7 +36,6 @@ class TestColorWidgetAction : public ::testing::Test TEST_F(TestColorWidgetAction, initTest) { - } TEST_F(TestColorWidgetAction, testslotBtnClicked) @@ -43,3 +44,43 @@ TEST_F(TestColorWidgetAction, testslotBtnClicked) m_tester->slotBtnClicked(0); EXPECT_EQ(spy.count(), 1); } + +TEST_F(TestColorWidgetAction, testslotBtnClickedAllIndices) +{ + // Color list has 8 colors per Utils::getHiglightColorList() + int colorCount = Utils::getHiglightColorList().size(); + ASSERT_GT(colorCount, 0); + + QColor lastSelectedColor; + for (int i = 0; i < colorCount; ++i) { + QSignalSpy spy(m_tester, SIGNAL(sigBtnGroupClicked())); + m_tester->slotBtnClicked(i); + EXPECT_EQ(spy.count(), 1); + lastSelectedColor = Utils::getHiglightColorList().at(i); + } + + // After iterating all buttons, the current color should match last selection + EXPECT_EQ(Utils::getCurHiglightColor(), lastSelectedColor); +} + +TEST_F(TestColorWidgetAction, testslotBtnClickedInvalidIndex) +{ + // Invalid index should not crash and should not emit signal + QSignalSpy spy(m_tester, SIGNAL(sigBtnGroupClicked())); + m_tester->slotBtnClicked(999); + EXPECT_EQ(spy.count(), 0); +} + +TEST_F(TestColorWidgetAction, testDefaultWidgetPopulated) +{ + QWidget *defaultWidget = m_tester->defaultWidget(); + ASSERT_NE(defaultWidget, nullptr); + + auto buttons = defaultWidget->findChildren(); + EXPECT_GE(buttons.size(), 1); +} + +TEST_F(TestColorWidgetAction, testSeparatorProperty) +{ + EXPECT_TRUE(m_tester->isSeparator()); +} diff --git a/tests/widgets/ut_securitydialog.cpp b/tests/widgets/ut_securitydialog.cpp index f4e9804e9..4dc428eec 100644 --- a/tests/widgets/ut_securitydialog.cpp +++ b/tests/widgets/ut_securitydialog.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd. +// Copyright (C) 2019-2026 ~ 2020 UnionTech Software Technology Co.,Ltd. // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -9,6 +9,9 @@ #include #include +#include +#include +#include class TestSecurityDialog : public ::testing::Test { @@ -18,7 +21,7 @@ class TestSecurityDialog : public ::testing::Test public: virtual void SetUp() { - m_tester = new SecurityDialog("123456789"); + m_tester = new SecurityDialog("https://example.com/path?query=1"); m_tester->disconnect(); } @@ -33,6 +36,65 @@ class TestSecurityDialog : public ::testing::Test TEST_F(TestSecurityDialog, initTest) { +} + +TEST_F(TestSecurityDialog, testDialogInitialProperties) +{ + EXPECT_EQ(m_tester->minimumWidth(), 380); + EXPECT_GE(m_tester->buttonCount(), 2); +} + +TEST_F(TestSecurityDialog, testChangeEventFontChange) +{ + QEvent event(QEvent::FontChange); + m_tester->changeEvent(&event); + SUCCEED(); +} + +TEST_F(TestSecurityDialog, testChangeEventThemeChange) +{ + QEvent event(QEvent::ThemeChange); + m_tester->changeEvent(&event); + SUCCEED(); +} + +TEST_F(TestSecurityDialog, testChangeEventOther) +{ + QEvent event(QEvent::None); + m_tester->changeEvent(&event); + SUCCEED(); +} + +// Free function tests +TEST(UT_autoCutText, emptyTextReturnsEmpty) +{ + DLabel label; + NewStr result = autoCutText(QString(), &label); + EXPECT_TRUE(result.resultStr.isEmpty()); + EXPECT_TRUE(result.strList.isEmpty()); +} +TEST(UT_autoCutText, nullLabelReturnsEmpty) +{ + NewStr result = autoCutText("hello world", nullptr); + EXPECT_TRUE(result.resultStr.isEmpty()); } +TEST(UT_autoCutText, shortTextNoWrap) +{ + DLabel label; + label.setFixedWidth(1000); // Wide enough that no wrapping is needed + NewStr result = autoCutText("hi", &label); + EXPECT_EQ(result.resultStr, QString("hi")); + EXPECT_EQ(result.strList.size(), 1); + EXPECT_GT(result.fontHeifht, 0); +} + +TEST(UT_autoCutText, longTextWraps) +{ + DLabel label; + label.setFixedWidth(20); // Very narrow, forcing wrap + NewStr result = autoCutText("this is some long text that should wrap", &label); + EXPECT_FALSE(result.resultStr.isEmpty()); + EXPECT_GE(result.strList.size(), 1); +}