Skip to content

Commit

Permalink
chore: add unit test ut-iconlabel
Browse files Browse the repository at this point in the history
unit test

Log: add unit test ut-iconlabel
  • Loading branch information
FeiWang1119 committed Aug 2, 2023
1 parent 90fc4ec commit ceca1b1
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ file(GLOB TEST_SOURCES
ut_dquickglow.cpp
ut_dsoftwareroundedimagenode.cpp
ut_dblitframebuffernode.cpp
ut_dquickiconlabel.cpp
)
if (EnableDtk5)
list(APPEND TEST_SOURCES
Expand Down
1 change: 1 addition & 0 deletions tests/data.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<file>qml/GlowEffect.qml</file>
<file>qml/DSoftwareRoundedImageNodeItem.qml</file>
<file>qml/DBlitFramebufferNodeItem.qml</file>
<file>qml/IconLabel.qml</file>
</qresource>
</RCC>
35 changes: 35 additions & 0 deletions tests/qml/IconLabel.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

import QtQuick 2.11
import org.deepin.dtk 1.0 as D

Rectangle {
id: control

width: 100; height: 100

property QtObject icon: QtObject{
property string name: "switch_button"
property int width: 20
property int height: 20
}

D.IconLabel {
anchors.centerIn: parent
width: 50; height: 50
text: "test"
font: D.DTK.fontManager.t5
color: "red"
display: D.IconLabel.IconBesideText
spacing: 2
mirrored: false
alignment: Qt.AlignLeft
topPadding: 1
leftPadding: 1
rightPadding: 1
bottomPadding: 1
icon: D.DTK.makeIcon(control.icon, control.D.DciIcon)
}
}
110 changes: 110 additions & 0 deletions tests/ut_dquickiconlabel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>

#include "test_helper.hpp"
#include "dquickiconlabel_p.h"

#include <QQuickItem>
#include <private/qquicktext_p.h>

DQUICK_USE_NAMESPACE

class ut_DQuickIconLabel : public ::testing::Test
{
public:
virtual void SetUp()
{
ASSERT_TRUE(helper.load("qrc:/qml/IconLabel.qml"));

target = helper.object->findChild<DQuickIconLabel *>();
ASSERT_TRUE(target);
}

ControlHelper<QQuickItem> helper;
DQuickIconLabel *target = nullptr;
};

TEST_F(ut_DQuickIconLabel, properties)
{
EXPECT_EQ(target->icon().width(), 20);
EXPECT_EQ(target->icon().height(), 20);
EXPECT_EQ(target->icon().name(), "switch_button");
EXPECT_EQ(target->text(), "test");
EXPECT_EQ(target->font().pixelSize(), 17);
EXPECT_EQ(target->color(), Qt::red);
EXPECT_EQ(target->display(), DQuickIconLabel::IconBesideText);
EXPECT_EQ(target->spacing(), 2);
EXPECT_EQ(target->isMirrored(), false);
EXPECT_EQ(target->alignment(), 129);
EXPECT_EQ(target->topPadding(), 1);
target->resetTopPadding();
EXPECT_EQ(target->topPadding(), 0);
EXPECT_EQ(target->leftPadding(), 1);
target->resetLeftPadding();
EXPECT_EQ(target->leftPadding(), 0);
EXPECT_EQ(target->rightPadding(), 1);
target->resetRightPadding();
EXPECT_EQ(target->rightPadding(), 0);
EXPECT_EQ(target->bottomPadding(), 1);
target->resetBottomPadding();
EXPECT_EQ(target->bottomPadding(), 0);
}

TEST_F(ut_DQuickIconLabel, displayIconOnly)
{
target->setDisplay(DQuickIconLabel::IconOnly);

auto image = findItem<DQuickDciIconImage>(helper.object);
EXPECT_EQ(image->x(), 1);
auto text = findItem<QQuickText>(helper.object);
EXPECT_FALSE(text);
}

TEST_F(ut_DQuickIconLabel, displayTextOnly)
{
target->setDisplay(DQuickIconLabel::TextOnly);

auto image = findItem<DQuickDciIconImage>(helper.object);
EXPECT_FALSE(image);
auto text = findItem<QQuickText>(helper.object);
EXPECT_EQ(text->x(), 1);
}

TEST_F(ut_DQuickIconLabel, displayIconBesideText)
{
target->setDisplay(DQuickIconLabel::IconBesideText);

auto image = findItem<DQuickDciIconImage>(helper.object);
ASSERT_TRUE(image);
auto text = findItem<QQuickText>(helper.object);
ASSERT_TRUE(text);

EXPECT_GT(image->position().x(), text->position().x());
}

TEST_F(ut_DQuickIconLabel, displayTextBesideIcon)
{
target->setDisplay(DQuickIconLabel::TextBesideIcon);

auto image = findItem<DQuickDciIconImage>(helper.object);
ASSERT_TRUE(image);
auto text = findItem<QQuickText>(helper.object);
ASSERT_TRUE(text);

EXPECT_LT(image->position().x(), text->position().x());
}

TEST_F(ut_DQuickIconLabel, displayTextUnderIcon)
{
target->setDisplay(DQuickIconLabel::TextUnderIcon);

auto image = findItem<DQuickDciIconImage>(helper.object);
EXPECT_TRUE(image);
auto text = findItem<QQuickText>(helper.object);
EXPECT_TRUE(text);

EXPECT_LT(image->position().y(), text->position().y());
}

0 comments on commit ceca1b1

Please sign in to comment.