diff options
Diffstat (limited to 'tests')
3 files changed, 59 insertions, 6 deletions
diff --git a/tests/auto/quick/qquicktext/tst_qquicktext.cpp b/tests/auto/quick/qquicktext/tst_qquicktext.cpp index bf90a8efd4..5f3679ca00 100644 --- a/tests/auto/quick/qquicktext/tst_qquicktext.cpp +++ b/tests/auto/quick/qquicktext/tst_qquicktext.cpp @@ -4049,7 +4049,11 @@ static qreal expectedBaselineScaled(QQuickText *item) { QFont font = item->font(); QTextLayout layout(item->text().replace(QLatin1Char('\n'), QChar::LineSeparator)); - do { + + qreal low = 0; + qreal high = 10000; + + while (low < high) { layout.setFont(font); qreal width = 0; layout.beginLayout(); @@ -4059,12 +4063,23 @@ static qreal expectedBaselineScaled(QQuickText *item) } layout.endLayout(); - if (width < item->width()) { - QFontMetricsF fm(layout.font()); - return fm.ascent() + item->topPadding(); + if (width > item->width()) { + high = font.pointSizeF(); + font.setPointSizeF((high + low) / 2); + } else { + low = font.pointSizeF(); + + // When fontSizeMode != FixedSize, the font size will be scaled to a value + // The goal is to find a pointSize that uses as much space as possible while + // still fitting inside the available space. 0.01 is chosen as the threshold. + if ((high - low) < qreal(0.01)) { + QFontMetricsF fm(layout.font()); + return fm.ascent() + item->topPadding(); + } + + font.setPointSizeF((high + low) / 2); } - font.setPointSize(font.pointSize() - 1); - } while (font.pointSize() > 0); + } return item->topPadding(); } diff --git a/tests/auto/quickcontrols2/qquickcontrol/data/fractionalFontSize.qml b/tests/auto/quickcontrols2/qquickcontrol/data/fractionalFontSize.qml new file mode 100644 index 0000000000..a4574e460e --- /dev/null +++ b/tests/auto/quickcontrols2/qquickcontrol/data/fractionalFontSize.qml @@ -0,0 +1,18 @@ +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + width: 400 + height: 400 + + property alias control: ctrl + + Control { + id: ctrl + contentItem: Text { + font.pointSize: 10.5 + elide: Text.ElideRight + text: "This is some sample text" + } + } +} diff --git a/tests/auto/quickcontrols2/qquickcontrol/tst_qquickcontrol.cpp b/tests/auto/quickcontrols2/qquickcontrol/tst_qquickcontrol.cpp index 94dccf6115..3ee9cc2328 100644 --- a/tests/auto/quickcontrols2/qquickcontrol/tst_qquickcontrol.cpp +++ b/tests/auto/quickcontrols2/qquickcontrol/tst_qquickcontrol.cpp @@ -33,6 +33,7 @@ #include <QtQuickTestUtils/private/visualtestutils_p.h> #include <QtQuickTemplates2/private/qquickbutton_p.h> #include <QtQuickControlsTestUtils/private/qtest_quickcontrols_p.h> +#include <QtQuick/private/qquicktext_p_p.h> using namespace QQuickVisualTestUtils; @@ -46,6 +47,7 @@ public: private slots: void initTestCase() override; void flickable(); + void fractionalFontSize(); private: QScopedPointer<QPointingDevice> touchDevice; @@ -93,6 +95,24 @@ void tst_QQuickControl::flickable() QTRY_COMPARE(buttonClickedSpy.count(), 1); } +void tst_QQuickControl::fractionalFontSize() +{ + QQuickApplicationHelper helper(this, QStringLiteral("fractionalFontSize.qml")); + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + const QQuickControl *control = window->property("control").value<QQuickControl *>(); + QVERIFY(control); + QQuickText *contentItem = qobject_cast<QQuickText *>(control->contentItem()); + QVERIFY(contentItem); + + QVERIFY(!contentItem->truncated()); + + QVERIFY2(qFuzzyCompare(contentItem->contentWidth(), + QQuickTextPrivate::get(contentItem)->layout.boundingRect().width()), + "The QQuickText::contentWidth() doesn't match the layout's preferred text width"); +} + QTEST_QUICKCONTROLS_MAIN(tst_QQuickControl) #include "tst_qquickcontrol.moc" |