Skip to content
Open
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
20 changes: 20 additions & 0 deletions tests/test_application_slots.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,23 @@ TEST(ApplicationSlots, onFocusChangedBothValidNoCrash)
drawApp->onFocusChanged(&w1, &w2);
SUCCEED();
}

// =========================================================================
// Application::defaultAttriVar() – fallback to attributionWidget (line 608)
// =========================================================================
#include "publicApi.h"

TEST(ApplicationSlots, DefaultAttrivar_FallbackToAttributionWidget)
{
// Ensure _defaultAttriVars doesn't contain the current scene key.
// Clear it so the lookup misses and we hit the fallback at line 608.
drawApp->_defaultAttriVars.clear();

// Call defaultAttriVar with currentDrawScence() and an arbitrary attri.
QVariant result = drawApp->defaultAttriVar(drawApp->currentDrawScence(), 999992);

if (drawApp->drawBoard() == nullptr || drawApp->drawBoard()->attributionWidget() == nullptr) {
GTEST_SKIP() << "attributionWidget() is null in offscreen environment";
}
SUCCEED();
}
33 changes: 33 additions & 0 deletions tests/test_cattributemanagerwgt_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,37 @@ TEST(CAttributeManagerWgtTest, AttributeWgtsEmptyWhenNoInstall)
// }
// =========================================================================


// =========================================================================
// removeComAttributeWgt – found branch (lines 290-295)
// Avoids the UB bug at line 302 by NOT inserting into s_allInstalledDefaultVar.
// =========================================================================
#include "publicApi.h"
#include "cattributeitemwidget.h"

TEST(CAttributeManagerWgtTest, RemoveComAttributeWgt_FoundBranch_NoUB)
{
MainWindow *mw = drawApp->topMainWindow();
ASSERT_NE(mw, nullptr);
DrawBoard *board = mw->drawBoard();
ASSERT_NE(board, nullptr);

auto *mgr = board->attributionWidget();
if (mgr == nullptr) {
GTEST_SKIP() << "attributionWidget() is null in offscreen environment";
}

// Insert ONLY into s_allInstalledAttriWgts, NOT s_allInstalledDefaultVar.
// This ensures itfV == end() at line 300, avoiding the UB at line 302.
auto *w = new CAttributeWgt(900001, mgr);
mgr->s_allInstalledAttriWgts.insert(900001, w);

// Call removeComAttributeWgt → hits lines 290-295 (found: disconnect + erase).
mgr->removeComAttributeWgt(900001);

// Verify the key is removed from s_allInstalledAttriWgts.
EXPECT_FALSE(mgr->s_allInstalledAttriWgts.contains(900001));

delete w;
}
#include "test_cattributemanagerwgt_funcs.moc"
26 changes: 26 additions & 0 deletions tests/test_ccentralwidget_lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,29 @@ TEST(TabBarWgtSizeModeLambdaTest, SizeModeChangedKeepsBarHidden)
qApp->processEvents();
EXPECT_FALSE(bar.isVisible());
}

// =========================================================================
// Page::signalSetScale lambda – emit zoomValueChanged (line 344)
// =========================================================================
#include <QSignalSpy>

TEST(CCentralWidgetLambdas, SignalSetScale_TriggersZoomValueChanged)
{
createNewViewByShortcutKey();
MainWindow *mw = drawApp->topMainWindow();
ASSERT_NE(mw, nullptr);
DrawBoard *board = mw->drawBoard();
ASSERT_NE(board, nullptr);
PageView *view = getCurView();
ASSERT_NE(view, nullptr);

// The lambda at line 344 checks `this == borad()->currentPage()`.
// getCurView() returns the current page's _view, so the lambda is connected
// and the condition should be true.
QSignalSpy spy(board, &DrawBoard::zoomValueChanged);

emit view->signalSetScale(1.5);
qApp->processEvents();

EXPECT_GE(spy.count(), 1);
}
33 changes: 33 additions & 0 deletions tests/test_ccutwidget_lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,36 @@ TEST(CCutWidgetLambdas, SizeReduceAction_HeightAtMin_DoesNotDecrement)
int height = w.m_heightEdit->text().trimmed().toInt();
EXPECT_EQ(height, 10);
}

// =========================================================================
// m_doneBtn clicked – clearFocus branches (lines 603/607)
// =========================================================================
#include <QSignalSpy>

TEST(CCutWidgetLambdas, DoneBtn_ClearsWidthEditFocus)
{
CCutWidget w;
w.show();
qApp->processEvents();

w.m_widthEdit->lineEdit()->setFocus(Qt::OtherFocusReason);
qApp->processEvents();
if (!w.m_widthEdit->lineEdit()->hasFocus()) {
GTEST_SKIP() << "Cannot set focus on widthEdit in offscreen environment";
}

w.m_heightEdit->lineEdit()->setFocus(Qt::OtherFocusReason);
qApp->processEvents();
if (!w.m_heightEdit->lineEdit()->hasFocus()) {
GTEST_SKIP() << "Cannot set focus on heightEdit in offscreen environment";
}

QSignalSpy spy(&w, &CCutWidget::finshed);
emit w.m_doneBtn->clicked();

// Lines 603/607: clearFocus on both edits should be hit.
EXPECT_FALSE(w.m_widthEdit->lineEdit()->hasFocus());
EXPECT_FALSE(w.m_heightEdit->lineEdit()->hasFocus());
// Line 609: emit finshed(true)
EXPECT_EQ(spy.count(), 1);
}
37 changes: 37 additions & 0 deletions tests/test_cdrawscene_cgraphicsview_coverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,40 @@ TEST(CDrawSceneGraphicsViewCoverageTest, ContextMenuEvent_IsShowContextFalse_NoP

EXPECT_EQ(qApp->activePopupWidget(), nullptr);
}

// ---------------------------------------------------------------------------
// getGroupTreeInfo with real nested group (lines 466-470)
// ---------------------------------------------------------------------------

TEST(CDrawSceneGraphicsViewCoverageTest, GetGroupTreeInfo_NestedGroup_ProcessEvents)
{
createNewViewByShortcutKey();
PageView *view = CManageViewSigleton::GetInstance()->getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *r1 = new CGraphicsRectItem(QRectF(0, 0, 40, 40));
auto *r2 = new CGraphicsRectItem(QRectF(50, 0, 40, 40));
auto *r3 = new CGraphicsRectItem(QRectF(0, 60, 40, 40));
scene->addCItem(r1);
scene->addCItem(r2);
scene->addCItem(r3);

auto *inner = scene->creatGroup({r1, r2});
ASSERT_NE(inner, nullptr);
auto *outer = scene->creatGroup({inner, r3});
ASSERT_NE(outer, nullptr);

// outer->getGroups() contains inner → 466 foreach → 467 processEvent=true
// → 468 qApp->processEvents() is hit
auto info = scene->getGroupTreeInfo(outer, EDuplicate, true);
PageScene::releaseBzItemsTreeInfo(info);

// Cleanup: clear selection, remove items
scene->clearSelectGroup();
scene->removeCItem(r1, true);
scene->removeCItem(r2, true);
scene->removeCItem(r3, true);
drawApp->setCurrentTool(selection);
}
52 changes: 52 additions & 0 deletions tests/test_cdrawscene_touchevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,55 @@ TEST_F(PageStateGuard, RecordSecenInfoToCmd_EItemAdded_ReturnsEarly)

EXPECT_EQ(undoBefore, undoAfter);
}

// ===========================================================================
// Touch event: double TouchBegin → accept=false (line 335)
// ===========================================================================

TEST_F(PageStateGuard, TouchEvent_DoubleTouchBegin_AcceptFalse)
{
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);
Page *page = view->page();
ASSERT_NE(page, nullptr);

// Verify page() is accessible (non-null) before sending touch events.
if (scene->page() == nullptr) {
GTEST_SKIP() << "scene->page() is null in offscreen environment";
}

page->setCurrentTool(selection);

QPointF scenePos1(50, 50);
QPointF scenePos2(53, 50); // within ±3px of scenePos1

// First TouchBegin: starts the double-click timer in the tool.
{
QEventPoint ep(0, QEventPoint::Pressed, scenePos1,
view->mapToGlobal(view->mapFromScene(scenePos1)));
QTouchEvent te1(QEvent::TouchBegin, QPointingDevice::primaryPointingDevice(),
Qt::NoModifier, {ep});
scene->event(&te1);
}

// Second TouchBegin within ±3px: triggers dueTouchDoubleClickedStart
// → setAccepted(false) + interrupt() → accept becomes false → line 335.
{
QEventPoint ep(0, QEventPoint::Pressed, scenePos2,
view->mapToGlobal(view->mapFromScene(scenePos2)));
QTouchEvent te2(QEvent::TouchBegin, QPointingDevice::primaryPointingDevice(),
Qt::NoModifier, {ep});
scene->event(&te2);
}

// Cleanup: interrupt any working tool and clear undo stack.
auto *tool = page->currentTool_p();
if (tool != nullptr && tool->isWorking()) {
tool->interrupt();
qApp->processEvents();
}
CUndoRedoCommand::clearCommand();
drawApp->setCurrentTool(selection);
}
122 changes: 122 additions & 0 deletions tests/test_cgraphicslineitem_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,125 @@ TEST(CGraphicsLineItemFuncTest, LoadGraphicsUnit_RoundTrip_PreservesData)
unit.release();
unit2.release();
}

// =========================================================================
// updateHandlesGeometry – Rotation handle branches (lines 279-306)
// =========================================================================
#include "publicApi.h"

TEST(CGraphicsLineItemFuncTest, UpdateHandlesGeometry_Rotation_DxPos_DyNeg)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *line = new CGraphicsLineItem(QPointF(0, 100), QPointF(100, 0));
scene->addCItem(line);
scene->selectItem(line);
ASSERT_TRUE(line->isSelected());
ASSERT_FALSE(line->isMutiSelected());
ASSERT_EQ(line->bzGroup(), nullptr);

line->m_handles.push_back(new CSizeHandleRect(line, CSizeHandleRect::Rotation));
line->updateHandlesGeometry();
SUCCEED();

line->clearHandle();
scene->clearSelectGroup();
scene->removeCItem(line, true);
drawApp->setCurrentTool(selection);
}

TEST(CGraphicsLineItemFuncTest, UpdateHandlesGeometry_Rotation_DxPos_DyPos)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *line = new CGraphicsLineItem(QPointF(0, 0), QPointF(100, 100));
scene->addCItem(line);
scene->selectItem(line);
ASSERT_TRUE(line->isSelected());

line->m_handles.push_back(new CSizeHandleRect(line, CSizeHandleRect::Rotation));
line->updateHandlesGeometry();
SUCCEED();

line->clearHandle();
scene->clearSelectGroup();
scene->removeCItem(line, true);
drawApp->setCurrentTool(selection);
}

TEST(CGraphicsLineItemFuncTest, UpdateHandlesGeometry_Rotation_DxNeg_DyPos)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *line = new CGraphicsLineItem(QPointF(100, 0), QPointF(0, 100));
scene->addCItem(line);
scene->selectItem(line);
ASSERT_TRUE(line->isSelected());

line->m_handles.push_back(new CSizeHandleRect(line, CSizeHandleRect::Rotation));
line->updateHandlesGeometry();
SUCCEED();

line->clearHandle();
scene->clearSelectGroup();
scene->removeCItem(line, true);
drawApp->setCurrentTool(selection);
}

TEST(CGraphicsLineItemFuncTest, UpdateHandlesGeometry_Rotation_DxNeg_DyNeg)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *line = new CGraphicsLineItem(QPointF(100, 100), QPointF(0, 0));
scene->addCItem(line);
scene->selectItem(line);
ASSERT_TRUE(line->isSelected());

line->m_handles.push_back(new CSizeHandleRect(line, CSizeHandleRect::Rotation));
line->updateHandlesGeometry();
SUCCEED();

line->clearHandle();
scene->clearSelectGroup();
scene->removeCItem(line, true);
drawApp->setCurrentTool(selection);
}

TEST(CGraphicsLineItemFuncTest, UpdateHandlesGeometry_Rotation_VerticalLine)
{
createNewViewByShortcutKey();
PageView *view = getCurView();
ASSERT_NE(view, nullptr);
PageScene *scene = view->drawScene();
ASSERT_NE(scene, nullptr);

auto *line = new CGraphicsLineItem(QPointF(50, 0), QPointF(50, 100));
scene->addCItem(line);
scene->selectItem(line);
ASSERT_TRUE(line->isSelected());

line->m_handles.push_back(new CSizeHandleRect(line, CSizeHandleRect::Rotation));
line->updateHandlesGeometry();
SUCCEED();

line->clearHandle();
scene->clearSelectGroup();
scene->removeCItem(line, true);
drawApp->setCurrentTool(selection);
}
22 changes: 22 additions & 0 deletions tests/test_cgraphicstextitem_ext2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,25 @@ TEST(CGraphicsTextItemExt2, MouseDoubleClickEvent_NoCrash)
// mouseDoubleClickEvent is a no-op (Q_UNUSED), should not crash
SUCCEED();
}

// =========================================================================
// currentCharFormat() – empty editor branch (line 134)
// =========================================================================

TEST(CGraphicsTextItemExt2, CurrentCharFormat_EmptyEditor_ReturnsDefault)
{
// Create a text item that has NOT entered edit state (m_pTextEdit is null).
CGraphicsTextItem item("Hello draw");
// Do not call changToEditState, so m_pTextEdit remains nullptr.
// currentCharFormat() should hit the `return QTextCharFormat()` branch.
QTextCharFormat fmt = item.currentCharFormat();
// Default-constructed QTextCharFormat has no properties set.
EXPECT_FALSE(fmt.hasProperty(QTextFormat::FontWeight));
}

TEST(CGraphicsTextItemExt2, CurrentCharFormat_EmptyEditor_DefaultConstructor)
{
CGraphicsTextItem item;
QTextCharFormat fmt = item.currentCharFormat();
SUCCEED(); // Default QTextCharFormat is valid but has no properties
}
Loading