diff options
-rw-r--r-- | src/plugins/accessible/widgets/qaccessiblewidgets.cpp | 7 | ||||
-rw-r--r-- | tests/auto/qaccessibility/tst_qaccessibility.cpp | 69 |
2 files changed, 68 insertions, 8 deletions
diff --git a/src/plugins/accessible/widgets/qaccessiblewidgets.cpp b/src/plugins/accessible/widgets/qaccessiblewidgets.cpp index 590f603..0360e0c 100644 --- a/src/plugins/accessible/widgets/qaccessiblewidgets.cpp +++ b/src/plugins/accessible/widgets/qaccessiblewidgets.cpp @@ -1433,8 +1433,13 @@ QRect QAccessibleTextWidget::characterRect(int offset, CoordinateType coordType) r.setWidth(averageCharWidth); } + int height = line.height(); + + // make sure that height does not include leading. (only ascent + descent + 1) + if (line.leadingIncluded()) + height -= qRound(line.leading()); r = QRect(layoutPosition.x() + x, layoutPosition.y() + line.y(), - w, line.height()); + w, height); if (coordType == RelativeToScreen) { r.moveTo(viewport()->mapToGlobal(r.topLeft())); diff --git a/tests/auto/qaccessibility/tst_qaccessibility.cpp b/tests/auto/qaccessibility/tst_qaccessibility.cpp index ed69901..d47eb06 100644 --- a/tests/auto/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/qaccessibility/tst_qaccessibility.cpp @@ -2720,6 +2720,27 @@ void tst_QAccessibility::doubleSpinBoxTest() QTestAccessibility::clearEvents(); } +static void dumpTextDiagnostics(const QTextEdit &edit, int offset) +{ + qDebug() << "Incorrect result, font:" << edit.currentFont().toString(); + QFontMetricsF fm(edit.currentFont()); + qDebug() << "QFontMetricsF: " << fm.ascent() << fm.descent() << fm.height(); + + QTextBlock block = edit.document()->findBlock(offset); + QVERIFY(block.isValid()); + QTextLayout *layout = block.layout(); + QPointF layoutPosition = layout->position(); + QTextLine line = layout->lineForTextPosition(offset); + qDebug() << "QTextLine: " << line.ascent() << line.descent() << line.height() << line.leading(); + qDebug() << block.text(); + + // Reported to only be a problem on Ubuntu Oneiric. Verify that. +#if defined(Q_WS_X11) && defined(UBUNTU_ONEIRIC) + qDebug() << "UBUNTU_ONEIRIC"; +#endif + +} + void tst_QAccessibility::textEditTest() { { @@ -2743,13 +2764,47 @@ void tst_QAccessibility::textEditTest() QCOMPARE(endOffset, 30); QCOMPARE(iface->text(QAccessible::Value, 6), QString()); QCOMPARE(iface->textInterface()->characterCount(), 31); - QFontMetrics fm(edit.font()); -#if defined(Q_WS_X11) && defined(UBUNTU_ONEIRIC) - QEXPECT_FAIL("", "QTBUG-26499", Abort); -#endif - QCOMPARE(iface->textInterface()->characterRect(0, QAccessible2::RelativeToParent).size(), QSize(fm.width("h"), fm.height())); - QCOMPARE(iface->textInterface()->characterRect(5, QAccessible2::RelativeToParent).size(), QSize(fm.width(" "), fm.height())); - QCOMPARE(iface->textInterface()->characterRect(6, QAccessible2::RelativeToParent).size(), QSize(fm.width("w"), fm.height())); + QFontMetrics fm(edit.currentFont()); + + // Test for + // QAccessibleTextInterface::characterRect() and + ///QAccessibleTextInterface::offsetAtPoint() + struct { + int offset; + char ch; + } testdata[] = { + {0, 'h'}, + {4, 'o'}, + // skip space, it might be too narrow to reliably hit it + {6, 'w'} + }; + + const int expectedHeight = fm.height(); //ascent + descent + 1 + + for (int i = 0; i < 3; ++i) { + int offset = testdata[i].offset; + QRect rect = iface->textInterface()->characterRect(offset, QAccessible2::RelativeToParent); + QVERIFY(rect.isValid()); + const QSize actualSize = rect.size(); + const int widthDelta = actualSize.width() - fm.width(QChar(testdata[i].ch)); + const int heightDelta = actualSize.height() - expectedHeight; + // The deltas should really be 0, but it seems that it fails for some fonts + // Dump some diagnostics if that is the case + if (heightDelta || widthDelta) + dumpTextDiagnostics(edit, offset); + + QVERIFY(qAbs(widthDelta) <= 1); + + if (qAbs(heightDelta) == 1) { + qDebug() << "Result is off by one, accepted. (" << actualSize.height() << expectedHeight << ")"; + } else { + QCOMPARE(actualSize.height(), expectedHeight); + } + // Width must be >= 3 in order for rect.center() to not end up on one of the edges. They are not reliable. + if (rect.width() >= 3) { + QCOMPARE(iface->textInterface()->offsetAtPoint(rect.center(), QAccessible2::RelativeToParent), offset); + } + } iface->editableTextInterface()->copyText(6, 11); QCOMPARE(QApplication::clipboard()->text(), QLatin1String("world")); |