From 50772544c6aa62b43a2c2826186f4110dbfd5c3f Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Mon, 5 Dec 2011 11:01:15 +1000 Subject: [PATCH] Fix test failures due to invalid font metrics. Don't use QFontMetrics to determine expected text dimensions or cursor positions in TextInput and TextEdit tests. Instead Layout the text with the same options as used by the item so the only error should be due to error. Task-number: QTBUG-21689 Change-Id: I7ba008d92f1ebb14c37ae7df06fdb11465c3225d Reviewed-by: Michael Brasser --- .../qtquick2/qquicktextedit/tst_qquicktextedit.cpp | 84 ++++++------- .../qquicktextinput/tst_qquicktextinput.cpp | 140 ++++++++++----------- 2 files changed, 102 insertions(+), 122 deletions(-) diff --git a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp index 97d7531..fe9791a 100644 --- a/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp @@ -365,42 +365,34 @@ void tst_qquicktextedit::width() for (int i = 0; i < standard.size(); i++) { - QFont f; - qreal metricWidth = 0.0; - - if (requiresUnhintedMetrics) { - QString s = standard.at(i); - s.replace(QLatin1Char('\n'), QChar::LineSeparator); - - QTextLayout layout(s); - layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic); - { - QTextOption option; - option.setUseDesignMetrics(true); - layout.setTextOption(option); - } - - layout.beginLayout(); - forever { - QTextLine line = layout.createLine(); - if (!line.isValid()) - break; - } - - layout.endLayout(); - - metricWidth = ceil(layout.boundingRect().width()); - } else { - QFontMetricsF fm(f); - metricWidth = fm.size(Qt::TextExpandTabs | Qt::TextShowMnemonic, standard.at(i)).width(); - metricWidth = ceil(metricWidth); - } - QString componentStr = "import QtQuick 2.0\nTextEdit { text: \"" + standard.at(i) + "\" }"; QDeclarativeComponent texteditComponent(&engine); texteditComponent.setData(componentStr.toLatin1(), QUrl()); QQuickTextEdit *textEditObject = qobject_cast(texteditComponent.create()); + QString s = standard.at(i); + s.replace(QLatin1Char('\n'), QChar::LineSeparator); + + QTextLayout layout(s); + layout.setFont(textEditObject->font()); + layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic); + if (requiresUnhintedMetrics) { + QTextOption option; + option.setUseDesignMetrics(true); + layout.setTextOption(option); + } + + layout.beginLayout(); + forever { + QTextLine line = layout.createLine(); + if (!line.isValid()) + break; + } + + layout.endLayout(); + + qreal metricWidth = ceil(layout.boundingRect().width()); + QVERIFY(textEditObject != 0); QCOMPARE(textEditObject->width(), qreal(metricWidth)); } @@ -1604,30 +1596,26 @@ void tst_qquicktextedit::positionAt() QQuickTextEdit *texteditObject = qobject_cast(canvas.rootObject()); QVERIFY(texteditObject != 0); - QFontMetrics fm(texteditObject->font()); - const int y0 = fm.height() / 2; - const int y1 = fm.height() * 3 / 2; + QTextLayout layout(texteditObject->text()); + layout.setFont(texteditObject->font()); - int pos = texteditObject->positionAt(texteditObject->width()/2, y0); - int widthBegin = 0; - int widthEnd = 0; if (!qmlDisableDistanceField()) { - QTextLayout layout(texteditObject->text()); - QTextOption option; option.setUseDesignMetrics(true); layout.setTextOption(option); + } - layout.beginLayout(); - QTextLine line = layout.createLine(); - layout.endLayout(); + layout.beginLayout(); + QTextLine line = layout.createLine(); + layout.endLayout(); - widthBegin = floor(line.cursorToX(pos - 1)); - widthEnd = ceil(line.cursorToX(pos + 1)); - } else { - widthBegin = fm.width(texteditObject->text().left(pos - 1)); - widthEnd = fm.width(texteditObject->text().left(pos + 1)); - } + const int y0 = line.height() / 2; + const int y1 = line.height() * 3 / 2; + + int pos = texteditObject->positionAt(texteditObject->width()/2, y0); + + int widthBegin = floor(line.cursorToX(pos - 1)); + int widthEnd = ceil(line.cursorToX(pos + 1)); QVERIFY(widthBegin <= texteditObject->width() / 2); QVERIFY(widthEnd >= texteditObject->width() / 2); diff --git a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp index 60c5eeb..6344768 100644 --- a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp @@ -315,40 +315,34 @@ void tst_qquicktextinput::width() for (int i = 0; i < standard.size(); i++) { - QFont f; - qreal metricWidth = 0.0; - if (requiresUnhintedMetrics) { - QString s = standard.at(i); - s.replace(QLatin1Char('\n'), QChar::LineSeparator); - - QTextLayout layout(s); - layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic); - { - QTextOption option; - option.setUseDesignMetrics(true); - layout.setTextOption(option); - } - - layout.beginLayout(); - forever { - QTextLine line = layout.createLine(); - if (!line.isValid()) - break; - } - - layout.endLayout(); - - metricWidth = ceil(layout.boundingRect().width()); - } else { - QFontMetricsF fm(f); - metricWidth = fm.width(standard.at(i)); - } - QString componentStr = "import QtQuick 2.0\nTextInput { text: \"" + standard.at(i) + "\" }"; QDeclarativeComponent textinputComponent(&engine); textinputComponent.setData(componentStr.toLatin1(), QUrl()); QQuickTextInput *textinputObject = qobject_cast(textinputComponent.create()); + QString s = standard.at(i); + s.replace(QLatin1Char('\n'), QChar::LineSeparator); + + QTextLayout layout(s); + layout.setFont(textinputObject->font()); + layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic); + if (requiresUnhintedMetrics) { + QTextOption option; + option.setUseDesignMetrics(true); + layout.setTextOption(option); + } + + layout.beginLayout(); + forever { + QTextLine line = layout.createLine(); + if (!line.isValid()) + break; + } + + layout.endLayout(); + + qreal metricWidth = ceil(layout.boundingRect().width()); + QVERIFY(textinputObject != 0); int delta = abs(int(int(textinputObject->width()) - metricWidth)); QVERIFY(delta <= 3.0); // As best as we can hope for cross-platform. @@ -1295,31 +1289,24 @@ void tst_qquicktextinput::positionAt() QVERIFY(textinputObject != 0); // Check autoscrolled... - QFontMetrics fm(textinputObject->font()); int pos = textinputObject->positionAt(textinputObject->width()/2); - int textWidth = 0; - int textLeftWidthBegin = 0; - int textLeftWidthEnd = 0; - if (!qmlDisableDistanceField()) { - QTextLayout layout(textinputObject->text()); + QTextLayout layout(textinputObject->text()); + layout.setFont(textinputObject->font()); + + if (!qmlDisableDistanceField()) { QTextOption option; option.setUseDesignMetrics(true); layout.setTextOption(option); - - layout.beginLayout(); - QTextLine line = layout.createLine(); - layout.endLayout(); - - textLeftWidthBegin = floor(line.cursorToX(pos - 1)); - textLeftWidthEnd = ceil(line.cursorToX(pos + 1)); - textWidth = floor(line.horizontalAdvance()); - } else { - textWidth = fm.width(textinputObject->text()); - textLeftWidthBegin = fm.width(textinputObject->text().left(pos - 1)); - textLeftWidthEnd = fm.width(textinputObject->text().left(pos + 1)); } + layout.beginLayout(); + QTextLine line = layout.createLine(); + layout.endLayout(); + + int textLeftWidthBegin = floor(line.cursorToX(pos - 1)); + int textLeftWidthEnd = ceil(line.cursorToX(pos + 1)); + int textWidth = floor(line.horizontalAdvance()); QVERIFY(textLeftWidthBegin <= textWidth - textinputObject->width() / 2); QVERIFY(textLeftWidthEnd >= textWidth - textinputObject->width() / 2); @@ -1332,23 +1319,8 @@ void tst_qquicktextinput::positionAt() textinputObject->setAutoScroll(false); pos = textinputObject->positionAt(textinputObject->width()/2); - if (!qmlDisableDistanceField()) { - QTextLayout layout(textinputObject->text()); - - QTextOption option; - option.setUseDesignMetrics(true); - layout.setTextOption(option); - - layout.beginLayout(); - QTextLine line = layout.createLine(); - layout.endLayout(); - - textLeftWidthBegin = floor(line.cursorToX(pos - 1)); - textLeftWidthEnd = ceil(line.cursorToX(pos + 1)); - } else { - textLeftWidthBegin = fm.width(textinputObject->text().left(pos - 1)); - textLeftWidthEnd = fm.width(textinputObject->text().left(pos + 1)); - } + textLeftWidthBegin = floor(line.cursorToX(pos - 1)); + textLeftWidthEnd = ceil(line.cursorToX(pos + 1)); QVERIFY(textLeftWidthBegin <= textinputObject->width() / 2); QVERIFY(textLeftWidthEnd >= textinputObject->width() / 2); @@ -1974,14 +1946,24 @@ void tst_qquicktextinput::cursorVisible() void tst_qquicktextinput::cursorRectangle() { - QSKIP("QTBUG-21689"); QString text = "Hello World!"; QQuickTextInput input; input.setText(text); - QFontMetricsF fm(input.font()); - input.setWidth(fm.width(text.mid(0, 5))); + + QTextLayout layout(text); + layout.setFont(input.font()); + if (!qmlDisableDistanceField()) { + QTextOption option; + option.setUseDesignMetrics(true); + layout.setTextOption(option); + } + layout.beginLayout(); + QTextLine line = layout.createLine(); + layout.endLayout(); + + input.setWidth(line.cursorToX(5, QTextLine::Leading)); QRect r; @@ -1992,14 +1974,12 @@ void tst_qquicktextinput::cursorRectangle() const int error = 5; #endif - for (int i = 0; i <= 5; ++i) { input.setCursorPosition(i); r = input.cursorRectangle(); - int textWidth = fm.width(text.mid(0, i)); - QVERIFY(r.left() < textWidth + error); - QVERIFY(r.right() > textWidth - error); + QVERIFY(r.left() < qCeil(line.cursorToX(i, QTextLine::Trailing))); + QVERIFY(r.right() >= qFloor(line.cursorToX(i , QTextLine::Leading))); QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRect(), r); } @@ -2453,6 +2433,7 @@ void tst_qquicktextinput::preeditAutoScroll() QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges); QTextLayout layout(preeditText); + layout.setFont(input->font()); if (!qmlDisableDistanceField()) { QTextOption option; option.setUseDesignMetrics(true); @@ -2578,9 +2559,20 @@ void tst_qquicktextinput::inputContextMouseHandler() QTest::qWaitForWindowShown(&view); QTRY_COMPARE(&view, qGuiApp->focusWindow()); - QFontMetricsF fm(input->font()); - const qreal y = fm.height() / 2; - QPoint position = QPointF(fm.width(text.mid(0, 2)), y).toPoint(); + QTextLayout layout(text); + layout.setFont(input->font()); + if (!qmlDisableDistanceField()) { + QTextOption option; + option.setUseDesignMetrics(true); + layout.setTextOption(option); + } + layout.beginLayout(); + QTextLine line = layout.createLine(); + layout.endLayout(); + + const qreal x = line.cursorToX(2, QTextLine::Leading); + const qreal y = line.height() / 2; + QPoint position = QPointF(x, y).toPoint(); QInputMethodEvent inputEvent(text.mid(0, 5), QList()); QApplication::sendEvent(input, &inputEvent); -- 2.7.4