Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions tests/test_attrwidgets_lambdas_ext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

/*
* Unit tests for attribute panel remaining lambdas (issue V-8626)
*
* Existing test_colorpanel_ext2.cpp covers button toggle, lineEdit textChanged,
* sliderPressed, valueChanged, but NOT:
* - ColorPanel::initUI() returnPressed lambda (hides parent)
* - ColorPanel::initConnection() m_colorfulBtn buttonClick lambda
* - CAlphaControlWidget::initConnection() sliderReleased lambda
* - CColorPickWidget::mousePressEvent delegation
*/

#include <gtest/gtest.h>

Check warning on line 16 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gtest/gtest.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QSignalSpy>

Check warning on line 18 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QSignalSpy> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QColor>

Check warning on line 19 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QColor> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QButtonGroup>

Check warning on line 20 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QButtonGroup> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QMouseEvent>

Check warning on line 21 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMouseEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QCoreApplication>

Check warning on line 22 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCoreApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#define protected public
#define private public
#include "widgets/ciconbutton.h"

Check warning on line 26 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "widgets/ciconbutton.h" not found.
#include "colorpanel.h"

Check warning on line 27 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "colorpanel.h" not found.
#include "calphacontrolwidget.h"

Check warning on line 28 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "calphacontrolwidget.h" not found.
#include "ccolorpickwidget.h"

Check warning on line 29 in tests/test_attrwidgets_lambdas_ext.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "ccolorpickwidget.h" not found.
#include "drawshape/globaldefine.h"
#undef protected
#undef private

#if TEST_COLORPANEL_ITEM

// ===========================================================================
// ColorPanel::initUI — returnPressed lambda (hides parent widget)
// ===========================================================================

TEST(ColorPanelLambdasExt, ReturnPressedWithParentHidesParent)
{
// parentColorWidget() returns qobject_cast<CColorPickWidget*>(parent()).
// The returnPressed lambda calls parentColorWidget()->hide().
// We must provide a CColorPickWidget parent to avoid null dereference.
CColorPickWidget pickWidget;
ColorPanel *panel = new ColorPanel();
panel->setParent(&pickWidget);

// Emit returnPressed — the lambda should call pickWidget.hide()
emit panel->m_colLineEdit->returnPressed();
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);

// No crash means success
delete panel;
SUCCEED();
}

// ===========================================================================
// ColorPanel::initConnection — m_colorfulBtn buttonClick lambda
// ===========================================================================

TEST(ColorPanelLambdasExt, ColorfulBtnButtonClickTogglesExpand)
{
ColorPanel panel;

// The colorfulBtn buttonClick lambda toggles s_expand and calls updateExpendArea.
emit panel.m_colorfulBtn->buttonClick();
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);

// Emitting again should toggle back
emit panel.m_colorfulBtn->buttonClick();
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);

SUCCEED();
}

// ===========================================================================
// CAlphaControlWidget::initConnection — sliderReleased lambda
// ===========================================================================

TEST(CAlphaControlWidgetLambdasExt, SliderReleased_EmitsAlphaChangedFinished)
{
CAlphaControlWidget w;
QSignalSpy spy(&w, &CAlphaControlWidget::alphaChanged);

// Set a known value first
w.setAlpha(100, false);

// Emit sliderReleased to trigger the third lambda
emit w.m_alphaSlider->sliderReleased();
QCoreApplication::processEvents(QEventLoop::AllEvents, 200);

EXPECT_GE(spy.count(), 1);
if (spy.count() > 0) {
QList<QVariant> args = spy.takeLast();
// sliderReleased lambda emits with EChangedFinished
EXPECT_EQ(args.at(0).toInt(), 100);
}
}

// ===========================================================================
// CColorPickWidget::mousePressEvent — delegates to DArrowRectangle
// ===========================================================================

TEST(CColorPickWidgetExt, MousePressEventDoesNotCrash)
{
CColorPickWidget pickWidget;

QMouseEvent event(QMouseEvent::MouseButtonPress,
QPointF(5, 5),
QPointF(5, 5),
Qt::LeftButton, Qt::LeftButton,
Qt::NoModifier);

pickWidget.mousePressEvent(&event);

SUCCEED();
}

#endif // TEST_COLORPANEL_ITEM
107 changes: 107 additions & 0 deletions tests/test_cgraphicsitem_register.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

/*
* Unit tests for CPictureItem factory registration and
* CGraphicsItemGroup::rasterToSelfLayer (issue V-8626)
*
* Existing test_cgraphicsitem_misc2.cpp covers CGraphicsItem::rasterToSelfLayer
* basic case, but the group version was skipped. These tests cover:
* - CPictureItemRegisterNameSpace::creatInstance() (via factory)
* - CGraphicsItemGroup::rasterToSelfLayer(bool) with child items
*
* Source:
* src/drawshape/drawItems/bzItems/cpictureitem.cpp
* src/drawshape/drawItems/cgraphicsitemselectedmgr.cpp
*/

#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>

#include <QRectF>
#include <QImage>

#define protected public
#define private public
#include "drawshape/drawItems/bzItems/cpictureitem.h"
#include "drawshape/drawItems/bzItems/cgraphicsrectitem.h"
#include "drawshape/drawItems/bzItems/cgraphicsitem.h"
#include "drawshape/drawItems/cgraphicsitemselectedmgr.h"
#include "drawshape/drawItems/cgraphicslayer.h"
#include "service/dyncreatobject.h"
#include "drawshape/globaldefine.h"
#undef protected
#undef private

// ===========================================================================
// CPictureItemRegisterNameSpace::creatInstance (via ClassObjectFactory)
// ===========================================================================

TEST(CPictureItemRegisterTest, FactoryCreatesPictureItem)
{
// REGISTITEMCLASS in cgraphicsitem.cpp registers CPictureItem at static init
auto *obj = ClassObjectFactory::creatObject<CPictureItem>("CPictureItem");
ASSERT_NE(obj, nullptr);
EXPECT_GE(obj->type(), 0);
delete obj;
}

TEST(CPictureItemRegisterTest, FactoryReturnsNullForUnknownName)
{
auto *obj = ClassObjectFactory::creatObject<CPictureItem>("NonExistentPicture");
EXPECT_EQ(obj, nullptr);
}

// ===========================================================================
// CGraphicsItemGroup::rasterToSelfLayer
// ===========================================================================

TEST(CGraphicsItemGroupRasterTest, RasterToSelfLayerWithChildItemsNoCrash)
{
// Create a group with a child rect item that has a valid layer
CGraphicsItemGroup group;

CGraphicsRectItem *rectItem = new CGraphicsRectItem(QRectF(0, 0, 50, 50));

CGraphicsLayer layer;
layer.setRect(QRectF(0, 0, 100, 100));
rectItem->setLayer(&layer);

group.add(rectItem);

// Call rasterToSelfLayer with deleteSelf=false to avoid scene removal
group.rasterToSelfLayer(false);

// The group should not crash and items should still be accessible
EXPECT_FALSE(group.getBzItems(true).isEmpty());
}

TEST(CGraphicsItemGroupRasterTest, RasterToSelfLayerEmptyGroupNoCrash)
{
CGraphicsItemGroup group;

// An empty group should not crash
group.rasterToSelfLayer(false);
SUCCEED();
}

TEST(CGraphicsItemGroupRasterTest, RasterToSelfLayerMultipleChildren)
{
CGraphicsItemGroup group;

CGraphicsRectItem *rect1 = new CGraphicsRectItem(QRectF(0, 0, 30, 30));
CGraphicsRectItem *rect2 = new CGraphicsRectItem(QRectF(40, 40, 30, 30));

CGraphicsLayer layer;
layer.setRect(QRectF(0, 0, 100, 100));
rect1->setLayer(&layer);
rect2->setLayer(&layer);

group.add(rect1);
group.add(rect2);

group.rasterToSelfLayer(false);

EXPECT_EQ(group.getBzItems(true).size(), 2);
}
Loading
Loading