Skip to content

Commit

Permalink
feat: Add UT for DWaterMarkWidget
Browse files Browse the repository at this point in the history
  Add UT.
  • Loading branch information
18202781743 committed Aug 23, 2023
1 parent edd36a9 commit faa9643
Show file tree
Hide file tree
Showing 5 changed files with 71 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 @@ -112,6 +112,7 @@ set(WIDGET_TEST
testcases/widgets/ut_dwindowoptionbutton.cpp
testcases/widgets/ut_dwindowquitfullbutton.cpp
testcases/widgets/ut_dcombobox.cpp
testcases/widgets/ut_dwatermarkwidget.cpp
)

include(${PROJECT_SOURCE_DIR}/src/util/util.cmake)
Expand Down
2 changes: 2 additions & 0 deletions tests/data.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<file>data/titlebar-settings.json</file>
<file>data/example-license.json</file>
<file>data/test.png</file>
<file>data/watermarks/text.png</file>
<file>data/watermarks/image.png</file>
</qresource>
</RCC>
Binary file added tests/data/watermarks/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/watermarks/text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions tests/testcases/widgets/ut_dwatermarkwidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>
#include <QLabel>
#include <QTest>

#include "dwatermarkwidget.h"
DWIDGET_USE_NAMESPACE
class ut_DWaterMarkWidget : public testing::Test
{
protected:
void SetUp() override
{
root = new QLabel();
target = new DWaterMarkWidget(root);
root->resize(600, 600);

QImage img(":/data/test.png");
QPixmap pixmap = QPixmap::fromImage(img.scaled(root->size()));
root->setPixmap(pixmap);
}
void TearDown() override
{
if (root)
delete root;
}
inline bool equalImage(const QImage &source)
{
QPixmap result = root->grab();
return result.toImage() == source;
}

DWaterMarkWidget *target = nullptr;
QLabel *root = nullptr;
};

TEST_F(ut_DWaterMarkWidget, paintText)
{
WaterMarkData data = target->data();
data.setType(WaterMarkData::Text);
data.setText("deepin water mark");
data.setLineSpacing(200);
data.setScaleFactor(1.5);
auto font = data.font();
font.setBold(true);
font.setPointSize(20);
data.setFont(font);
data.setColor(Qt::red);
data.setOpacity(0.5);
target->setData(data);

EXPECT_TRUE(equalImage(QImage(":/data/watermarks/text.png")));
}

TEST_F(ut_DWaterMarkWidget, paintImage)
{
WaterMarkData data = target->data();
data.setType(WaterMarkData::Image);
QImage img(":/assets/images/uos.svg");
data.setImage(img);
data.setLineSpacing(200);
data.setGrayScale(true);
target->setData(data);

EXPECT_TRUE(equalImage(QImage(":/data/watermarks/image.png")));
}

0 comments on commit faa9643

Please sign in to comment.