diff options
author | Jan-Arve Saether <jan-arve.saether@nokia.com> | 2012-08-08 16:06:33 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-08-14 03:56:55 +0200 |
commit | 2cb9ded6eca80aa0852ddfefaf6899ccb913c556 (patch) | |
tree | b0043d91d9d602c449341a271681630bb670c097 /tests/auto/other | |
parent | 59117012f27355830ceda4fc7f9e5f637d07fcc9 (diff) |
Implemented QAccessibleTextWidget
A new class called QAccessibleTextWidget was added.
This class should implement all methods of QAccessibleTextInterface and
QAccessibleEditableTextInterface which only need a QTextCursor, and it
defines two pure virtual methods, to obtain and set the text cursor, so
accessible implementations of widgets which use a text cursor can implement
these two methods.
QAccessibleTextEdit is now a subclass of QAccessibleTextWidget and most of
its methods were moved to QAccessibleTextWidget.
This is a forward port of ba5d7d608cc31fc63354fd74d85a1bad7780fc45 from
Qt 4.8, and is a prerequisite for forward-porting QPlainTextEdit
Change-Id: I6093c4fa7e0a77b84de779479c6074db006efec1
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@nokia.com>
Diffstat (limited to 'tests/auto/other')
-rw-r--r-- | tests/auto/other/qaccessibility/tst_qaccessibility.cpp | 112 |
1 files changed, 77 insertions, 35 deletions
diff --git a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp index 90dc4053e7..3e937fefd4 100644 --- a/tests/auto/other/qaccessibility/tst_qaccessibility.cpp +++ b/tests/auto/other/qaccessibility/tst_qaccessibility.cpp @@ -1544,47 +1544,89 @@ void tst_QAccessibility::doubleSpinBoxTest() QTestAccessibility::clearEvents(); } -void tst_QAccessibility::textEditTest() +static QRect characterRect(const QTextEdit &edit, int offset) { - { - QTextEdit edit; - int startOffset; - int endOffset; - QString text = "hello world\nhow are you today?\n"; - edit.setText(text); - edit.show(); - - QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&edit); - QCOMPARE(iface->text(QAccessible::Value), text); - QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world")); - QCOMPARE(startOffset, 6); - QCOMPARE(endOffset, 11); - QCOMPARE(iface->textInterface()->textAtOffset(14, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("how are you today?")); - QCOMPARE(startOffset, 12); - QCOMPARE(endOffset, 30); - QCOMPARE(iface->textInterface()->characterCount(), 31); + QTextBlock block = edit.document()->findBlock(offset); + QTextLayout *layout = block.layout(); + QPointF layoutPosition = layout->position(); + int relativeOffset = offset - block.position(); + QTextLine line = layout->lineForTextPosition(relativeOffset); QFontMetrics fm(edit.font()); - QCOMPARE(iface->textInterface()->characterRect(0).size(), QSize(fm.width("h"), fm.height())); - QCOMPARE(iface->textInterface()->characterRect(5).size(), QSize(fm.width(" "), fm.height())); - QCOMPARE(iface->textInterface()->characterRect(6).size(), QSize(fm.width("w"), fm.height())); + QChar ch = edit.document()->characterAt(offset); + int w = fm.width(ch); + int h = fm.height(); - QTestAccessibility::clearEvents(); + qreal x = line.cursorToX(relativeOffset); + QRect r(layoutPosition.x() + x, layoutPosition.y() + line.y(), w, h); + r.moveTo(edit.viewport()->mapToGlobal(r.topLeft())); - // select text - QTextCursor c = edit.textCursor(); - c.setPosition(2); - c.setPosition(4, QTextCursor::KeepAnchor); - edit.setTextCursor(c); - QAccessibleTextSelectionEvent sel(&edit, 2, 4); - QVERIFY_EVENT(&sel); + return r; +} - edit.selectAll(); - int end = edit.textCursor().position(); - sel.setCursorPosition(end); - sel.setSelection(0, end); - QVERIFY_EVENT(&sel); +void tst_QAccessibility::textEditTest() +{ + for (int pass = 0; pass < 2; ++pass) { + { + QTextEdit edit; + int startOffset; + int endOffset; + // create two blocks of text. The first block has two lines. + QString text = "<p>hello world.<br/>How are you today?</p><p>I'm fine, thanks</p>"; + edit.setHtml(text); + if (pass == 1) { + QFont font("Helvetica"); + font.setPointSizeF(12.5); + font.setWordSpacing(1.1); + edit.setFont(font); + } + + edit.show(); + QTest::qWaitForWindowShown(&edit); + QAccessibleInterface *iface = QAccessible::queryAccessibleInterface(&edit); + QCOMPARE(iface->text(QAccessible::Value), edit.toPlainText()); + QCOMPARE(iface->textInterface()->textAtOffset(8, QAccessible2::WordBoundary, &startOffset, &endOffset), QString("world")); + QCOMPARE(startOffset, 6); + QCOMPARE(endOffset, 11); + QCOMPARE(iface->textInterface()->textAtOffset(15, QAccessible2::LineBoundary, &startOffset, &endOffset), QString("How are you today?")); + QCOMPARE(startOffset, 13); + QCOMPARE(endOffset, 31); + QCOMPARE(iface->textInterface()->characterCount(), 48); + QFontMetrics fm(edit.font()); + QCOMPARE(iface->textInterface()->characterRect(0).size(), QSize(fm.width("h"), fm.height())); + QCOMPARE(iface->textInterface()->characterRect(5).size(), QSize(fm.width(" "), fm.height())); + QCOMPARE(iface->textInterface()->characterRect(6).size(), QSize(fm.width("w"), fm.height())); + + int offset = 10; + QCOMPARE(iface->textInterface()->text(offset, offset + 1), QStringLiteral("d")); + QCOMPARE(iface->textInterface()->characterRect(offset), characterRect(edit, offset)); + offset = 13; + QCOMPARE(iface->textInterface()->text(offset, offset + 1), QStringLiteral("H")); + QCOMPARE(iface->textInterface()->characterRect(offset), characterRect(edit, offset)); + offset = 21; + QCOMPARE(iface->textInterface()->text(offset, offset + 1), QStringLiteral("y")); + QCOMPARE(iface->textInterface()->characterRect(offset), characterRect(edit, offset)); + offset = 32; + QCOMPARE(iface->textInterface()->text(offset, offset + 1), QStringLiteral("I")); + QCOMPARE(iface->textInterface()->characterRect(offset), characterRect(edit, offset)); + + QTestAccessibility::clearEvents(); + + // select text + QTextCursor c = edit.textCursor(); + c.setPosition(2); + c.setPosition(4, QTextCursor::KeepAnchor); + edit.setTextCursor(c); + QAccessibleTextSelectionEvent sel(&edit, 2, 4); + QVERIFY_EVENT(&sel); + + edit.selectAll(); + int end = edit.textCursor().position(); + sel.setCursorPosition(end); + sel.setSelection(0, end); + QVERIFY_EVENT(&sel); + } + QTestAccessibility::clearEvents(); } - QTestAccessibility::clearEvents(); } void tst_QAccessibility::textBrowserTest() |