From 5e8ab9510cc1a35eccdf7e1b0b9506b2d3daf7e3 Mon Sep 17 00:00:00 2001 From: xiepengfei Date: Fri, 11 Sep 2026 11:28:07 +0800 Subject: [PATCH] fix(test): adapt night mode tests to async NightFilter refactor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BrowserPage::applyNightMode was removed in the night mode refactoring (async EyeProtectionManager + NightFilter + background thread). Update tests to match the new API: 1. ut_browserpage.cpp: UT_BrowserPage_applyNightMode_001 — rewrite to test NightFilter::apply() pure filter logic directly (null input, white→dark inversion, dark→white boost) instead of the removed synchronous applyNightMode method. 2. ut_pagerenderthread.cpp: onDocPageNormalImageTaskFinished_002 and onDocPageBigImageTaskFinished_002 — add setImageObjectRects stub. The refactoring added task.page->setImageObjectRects() before handleRenderFinished() in these slots; without the stub, nullptr task.page caused a segfault that prevented the full suite from completing and coverage from being generated. No production code changes. All 1120 tests pass (0 failures). --- tests/browser/ut_browserpage.cpp | 34 +++++++++++++++++++++------ tests/browser/ut_pagerenderthread.cpp | 9 +++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/tests/browser/ut_browserpage.cpp b/tests/browser/ut_browserpage.cpp index 05d3260d7..f098ff600 100644 --- a/tests/browser/ut_browserpage.cpp +++ b/tests/browser/ut_browserpage.cpp @@ -4,6 +4,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "BrowserPage.h" +#include "NightFilter.h" #include "PageRenderThread.h" #include "BrowserAnnotation.h" #include "PDFModel.h" @@ -837,11 +838,30 @@ TEST_F(TestBrowserPage, UT_BrowserPage_isBigDoc_001) TEST_F(TestBrowserPage, UT_BrowserPage_applyNightMode_001) { - QPixmap nullPix; - EXPECT_TRUE(m_tester->applyNightMode(nullPix).isNull()); - - QPixmap src(16, 16); - src.fill(Qt::white); - QPixmap dst = m_tester->applyNightMode(src); - EXPECT_FALSE(dst.isNull()); + // applyNightMode 已重构为异步实现(NightFilter + 后台线程), + // 此处直接测试 NightFilter::apply 纯滤镜逻辑的输入/输出断言 + + // 空图像输入返回空图像 + QImage nullImg; + EXPECT_TRUE(NightFilter::apply(nullImg).isNull()); + + // 白色图像经夜间滤镜反色后应为深色背景 + QImage whiteImg(16, 16, QImage::Format_ARGB32); + whiteImg.fill(Qt::white); + QImage nightImg = NightFilter::apply(whiteImg); + EXPECT_FALSE(nightImg.isNull()); + QRgb pixel = nightImg.pixel(0, 0); + EXPECT_LT(qRed(pixel), 128); + EXPECT_LT(qGreen(pixel), 128); + EXPECT_LT(qBlue(pixel), 128); + + // 深色像素(boostSourceBelow 以下)反色后应提纯白 + QImage darkImg(16, 16, QImage::Format_ARGB32); + darkImg.fill(Qt::black); + QImage darkNight = NightFilter::apply(darkImg); + EXPECT_FALSE(darkNight.isNull()); + QRgb darkPixel = darkNight.pixel(0, 0); + EXPECT_EQ(qRed(darkPixel), 255); + EXPECT_EQ(qGreen(darkPixel), 255); + EXPECT_EQ(qBlue(darkPixel), 255); } diff --git a/tests/browser/ut_pagerenderthread.cpp b/tests/browser/ut_pagerenderthread.cpp index 88a201d1c..ee5f853f6 100644 --- a/tests/browser/ut_pagerenderthread.cpp +++ b/tests/browser/ut_pagerenderthread.cpp @@ -65,6 +65,13 @@ static void handleRenderFinished_stub(const int &, const QPixmap &, const QRect g_funcName = __FUNCTION__; } +// setImageObjectRects stub: night mode refactoring added this call before +// handleRenderFinished in onDocPageNormalImageTaskFinished / onDocPageBigImageTaskFinished. +// Stub prevents null-pointer dereference when task.page is nullptr. +static void setImageObjectRects_stub(const QVector &, int, int) +{ +} + static void handleWordLoaded_stub(const QList &) { g_funcName = __FUNCTION__; @@ -228,6 +235,7 @@ TEST_F(TestPageRenderThread, UT_PageRenderThread_onDocPageNormalImageTaskFinishe Stub s; s.set(ADDR(DocSheet, existSheet), existSheet_true_stub); s.set(ADDR(BrowserPage, handleRenderFinished), handleRenderFinished_stub); + s.set(ADDR(BrowserPage, setImageObjectRects), setImageObjectRects_stub); DocPageNormalImageTask task; task.sheet = nullptr; // existSheet is stubbed to return true anyway @@ -263,6 +271,7 @@ TEST_F(TestPageRenderThread, UT_PageRenderThread_onDocPageBigImageTaskFinished_0 Stub s; s.set(ADDR(DocSheet, existSheet), existSheet_true_stub); s.set(ADDR(BrowserPage, handleRenderFinished), handleRenderFinished_stub); + s.set(ADDR(BrowserPage, setImageObjectRects), setImageObjectRects_stub); DocPageBigImageTask task; task.sheet = nullptr;