diff --git a/tests/auto/gui/text/CMakeLists.txt b/tests/auto/gui/text/CMakeLists.txt index 856551bf52..43c28ec768 100644 --- a/tests/auto/gui/text/CMakeLists.txt +++ b/tests/auto/gui/text/CMakeLists.txt @@ -16,6 +16,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 a1d9b2c6cf..ed01db4e2d 100644 --- a/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp +++ b/tests/auto/gui/text/qtextimagehandler/tst_qtextimagehandler.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include @@ -17,6 +17,7 @@ private slots: void init(); void cleanup(); void cleanupTestCase(); + void loadAtNImages_data(); void loadAtNImages(); }; @@ -36,24 +37,37 @@ void tst_QTextImageHandler::cleanupTestCase() { } +void tst_QTextImageHandler::loadAtNImages_data() +{ + QTest::addColumn("imageFile"); + + QTest::addRow("file") << QFINDTESTDATA("data/image.png"); +} + void tst_QTextImageHandler::loadAtNImages() { + QFETCH(QString, imageFile); + QTextDocument doc; QTextCursor c(&doc); - c.insertHtml(""); + c.insertHtml(""); + 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); } }