summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-12-21 14:03:12 +0100
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2022-12-22 23:20:24 +0100
commit1a37fd3148fa623b6855992ffe67a4d84671e242 (patch)
tree1e1f849b71b5841a1a18f3a1e4d90916e5f6ea8c /tests
parentfafe0dec56fb2f5210a3043ee8a811bdc034c584 (diff)
Enable and fix the test for QTextImageHandler
Amends 52ce0c177e80c2d5b70b38d429abb3689b3da51e, which added the test without adding it to the parent directory. Refactor the test code to be data driven, add the image files as external test data files, and adjust the test code to find the files. Use the QTextImageFormat from the document rather than a manually crafted one, as otherwise we don't test a real usecase. This also makes the test more flexible for adding qrc, resources, and file URLs. Task-number: QTBUG-109212 Change-Id: Id0771037b961d95ec3cadd0cd6467d2448f22884 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io> (cherry picked from commit d8e213a9e6b3013f768cbf4d94878cd2a0f7bb79) Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/gui/text/CMakeLists.txt1
-rw-r--r--tests/auto/gui/text/qtextimagehandler/CMakeLists.txt16
-rw-r--r--tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp28
3 files changed, 38 insertions, 7 deletions
diff --git a/tests/auto/gui/text/CMakeLists.txt b/tests/auto/gui/text/CMakeLists.txt
index 5040060a33..536870bada 100644
--- a/tests/auto/gui/text/CMakeLists.txt
+++ b/tests/auto/gui/text/CMakeLists.txt
@@ -13,6 +13,7 @@ add_subdirectory(qtextcursor)
add_subdirectory(qtextdocumentfragment)
add_subdirectory(qtextdocumentlayout)
add_subdirectory(qtextformat)
+add_subdirectory(qtextimagehandler)
add_subdirectory(qtextlist)
add_subdirectory(qtextobject)
# add_subdirectory(qtextscriptengine) # disable until system_harfbuzz feature is available # special case
diff --git a/tests/auto/gui/text/qtextimagehandler/CMakeLists.txt b/tests/auto/gui/text/qtextimagehandler/CMakeLists.txt
new file mode 100644
index 0000000000..c9d20fe956
--- /dev/null
+++ b/tests/auto/gui/text/qtextimagehandler/CMakeLists.txt
@@ -0,0 +1,16 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+list(APPEND test_data "data/image.png")
+list(APPEND test_data "data/image@2x.png")
+
+qt_internal_add_test(tst_qtextimagehandler
+ SOURCES
+ tst_qtextimagehandler.cpp
+ LIBRARIES
+ Qt::CorePrivate
+ Qt::Gui
+ Qt::GuiPrivate
+ TESTDATA
+ ${test_data}
+)
diff --git a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
index d5dde13770..1863c3eeb7 100644
--- a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
+++ b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp
@@ -42,6 +42,7 @@ private slots:
void init();
void cleanup();
void cleanupTestCase();
+ void loadAtNImages_data();
void loadAtNImages();
};
@@ -61,24 +62,37 @@ void tst_QTextImageHandler::cleanupTestCase()
{
}
+void tst_QTextImageHandler::loadAtNImages_data()
+{
+ QTest::addColumn<QString>("imageFile");
+
+ QTest::addRow("file") << QFINDTESTDATA("data/image.png");
+}
+
void tst_QTextImageHandler::loadAtNImages()
{
+ QFETCH(QString, imageFile);
+
QTextDocument doc;
QTextCursor c(&doc);
- c.insertHtml("<img src=\"data/image.png\">");
+ c.insertHtml("<img src=\"" + imageFile + "\">");
+ const auto formats = doc.allFormats();
+ const auto it = std::find_if(formats.begin(), formats.end(), [](const auto &format){
+ return format.objectType() == QTextFormat::ImageObject;
+ });
+ QVERIFY(it != formats.end());
+ const QTextImageFormat format = (*it).toImageFormat();
QTextImageHandler handler;
- QTextImageFormat fmt;
- fmt.setName("data/image.png");
- for (int i = 1; i < 3; ++i) {
+ for (const auto &dpr : {1, 2}) {
QImage img(20, 20, QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::white);
- img.setDevicePixelRatio(i);
+ img.setDevicePixelRatio(dpr);
QPainter p(&img);
- handler.drawObject(&p, QRect(0, 0, 20, 20), &doc, 0, fmt);
+ handler.drawObject(&p, QRect(0, 0, 20, 20), &doc, 0, format);
p.end();
QVERIFY(!img.isNull());
- const auto expectedColor = i == 1 ? Qt::red : Qt::green;
+ const auto expectedColor = dpr == 1 ? Qt::red : Qt::green;
QCOMPARE(img.pixelColor(0, 0), expectedColor);
}
}