Skip to content

Commit

Permalink
Remove expected failures from TextInput pre-edit tests.
Browse files Browse the repository at this point in the history
Use the cursorRectangleChanged signal of QInputPanel to determine
if an update has been sent instead of a custom input context.

Change-Id: I500ef71a3ea76e8b4271560d7aa5cc4fc6737cb0
Reviewed-by: Martin Jones <martin.jones@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Nov 10, 2011
1 parent fbbc093 commit 32bbe6d
Showing 1 changed file with 57 additions and 60 deletions.
117 changes: 57 additions & 60 deletions tests/auto/declarative/qquicktextinput/tst_qquicktextinput.cpp
Expand Up @@ -146,8 +146,7 @@ private slots:
void testQtQuick11Attributes_data();

void preeditAutoScroll();
void preeditMicroFocus();

void preeditCursorRectangle();
void inputMethodComposing();
void cursorRectangleSize();

Expand Down Expand Up @@ -2273,112 +2272,104 @@ void tst_qquicktextinput::testQtQuick11Attributes_data()
<< "";
}

static void sendPreeditText(const QString &text, int cursor)
{
QInputMethodEvent event(text, QList<QInputMethodEvent::Attribute>()
<< QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor, text.length(), QVariant()));
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &event);
}

void tst_qquicktextinput::preeditAutoScroll()
{
#ifdef QTBUG_21691
QEXPECT_FAIL("", QTBUG_21691_MESSAGE, Abort);
QVERIFY(false);
#else
QString preeditText = "califragisiticexpialidocious!";

QQuickView view(QUrl::fromLocalFile(TESTDATA("preeditAutoScroll.qml")));
MyInputContext ic;
// QQuickCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has active focus
// and QWidget won't allow an input context to be set when the flag is not set.
view.setAttribute(Qt::WA_InputMethodEnabled, true);
view.setInputContext(&ic);
view.setAttribute(Qt::WA_InputMethodEnabled, false);
view.show();
view.requestActivateWindow();
QTest::qWaitForWindowShown(&view);
QTRY_COMPARE(&view, qGuiApp->focusWindow());
QQuickTextInput *input = qobject_cast<QQuickTextInput *>(view.rootObject());
QVERIFY(input);
QVERIFY(input->hasActiveFocus());

input->setWidth(input->implicitWidth());

QSignalSpy cursorRectangleSpy(input, SIGNAL(cursorRectangleChanged()));
int cursorRectangleChanges = 0;

QFontMetricsF fm(input->font());
input->setWidth(fm.width(input->text()));

// test the text is scrolled so the preedit is visible.
ic.sendPreeditText(preeditText.mid(0, 3), 1);
sendPreeditText(preeditText.mid(0, 3), 1);
QVERIFY(input->positionAt(0) != 0);
QVERIFY(input->cursorRectangle().left() < input->boundingRect().width());
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);

// test the text is scrolled back when the preedit is removed.
ic.sendEvent(QInputMethodEvent());
QInputMethodEvent imEvent;
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);
QCOMPARE(input->positionAt(0), 0);
QCOMPARE(input->positionAt(input->width()), 5);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);

// some tolerance for different fonts.
#ifdef Q_OS_LINUX
const int error = 2;
#else
const int error = 5;
#endif
QTextLayout layout(preeditText);
if (!qmlDisableDistanceField()) {
QTextOption option;
option.setUseDesignMetrics(true);
layout.setTextOption(option);
}
layout.beginLayout();
QTextLine line = layout.createLine();
layout.endLayout();

// test if the preedit is larger than the text input that the
// character preceding the cursor is still visible.
qreal x = input->positionToRectangle(0).x();
for (int i = 0; i < 3; ++i) {
ic.sendPreeditText(preeditText, i + 1);
QVERIFY(input->cursorRectangle().right() >= fm.width(preeditText.at(i)) - error);
sendPreeditText(preeditText, i + 1);
int width = ceil(line.cursorToX(i, QTextLine::Trailing)) - floor(line.cursorToX(i));
QVERIFY(input->cursorRectangle().right() >= width - 3);
QVERIFY(input->positionToRectangle(0).x() < x);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
x = input->positionToRectangle(0).x();
}
for (int i = 1; i >= 0; --i) {
ic.sendPreeditText(preeditText, i + 1);
QVERIFY(input->cursorRectangle().right() >= fm.width(preeditText.at(i)) - error);
sendPreeditText(preeditText, i + 1);
int width = ceil(line.cursorToX(i, QTextLine::Trailing)) - floor(line.cursorToX(i));
QVERIFY(input->cursorRectangle().right() >= width - 3);
QVERIFY(input->positionToRectangle(0).x() > x);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
x = input->positionToRectangle(0).x();
}

// Test incrementing the preedit cursor doesn't cause further
// scrolling when right most text is visible.
ic.sendPreeditText(preeditText, preeditText.length() - 3);
sendPreeditText(preeditText, preeditText.length() - 3);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
x = input->positionToRectangle(0).x();
for (int i = 2; i >= 0; --i) {
ic.sendPreeditText(preeditText, preeditText.length() - i);
sendPreeditText(preeditText, preeditText.length() - i);
QCOMPARE(input->positionToRectangle(0).x(), x);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
}
for (int i = 1; i < 3; ++i) {
ic.sendPreeditText(preeditText, preeditText.length() - i);
sendPreeditText(preeditText, preeditText.length() - i);
QCOMPARE(input->positionToRectangle(0).x(), x);
QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
}

// Test disabling auto scroll.
ic.sendEvent(QInputMethodEvent());
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);

input->setAutoScroll(false);
ic.sendPreeditText(preeditText.mid(0, 3), 1);
sendPreeditText(preeditText.mid(0, 3), 1);
QCOMPARE(input->positionAt(0), 0);
QCOMPARE(input->positionAt(input->width()), 5);
#endif
}

void tst_qquicktextinput::preeditMicroFocus()
void tst_qquicktextinput::preeditCursorRectangle()
{
#ifdef QTBUG_21691
QEXPECT_FAIL("", QTBUG_21691_MESSAGE, Abort);
QVERIFY(false);
#else
QString preeditText = "super";

QQuickView view(QUrl::fromLocalFile(TESTDATA("inputMethodEvent.qml")));
MyInputContext ic;
// QQuickCanvas won't set the Qt::WA_InputMethodEnabled flag unless a suitable item has active focus
// and QWidget won't allow an input context to be set when the flag is not set.
view.setAttribute(Qt::WA_InputMethodEnabled, true);
view.setInputContext(&ic);
view.setAttribute(Qt::WA_InputMethodEnabled, false);
view.show();
view.requestActivateWindow();
QTest::qWaitForWindowShown(&view);
Expand All @@ -2387,37 +2378,43 @@ void tst_qquicktextinput::preeditMicroFocus()
QVERIFY(input);

QRect currentRect;
QRect previousRect = input->inputMethodQuery(Qt::ImCursorRectangle).toRect();

QInputMethodQueryEvent query(Qt::ImCursorRectangle);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
QRect previousRect = query.value(Qt::ImCursorRectangle).toRect();

// Verify that the micro focus rect is positioned the same for position 0 as
// it would be if there was no preedit text.
ic.updateReceived = false;
ic.sendPreeditText(preeditText, 0);
currentRect = input->inputMethodQuery(Qt::ImCursorRectangle).toRect();
sendPreeditText(preeditText, 0);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QCOMPARE(currentRect, previousRect);
QCOMPARE(ic.updateReceived, true);

QSignalSpy inputSpy(input, SIGNAL(cursorRectangleChanged()));
QSignalSpy panelSpy(qGuiApp->inputPanel(), SIGNAL(cursorRectangleChanged()));

// Verify that the micro focus rect moves to the left as the cursor position
// is incremented.
for (int i = 1; i <= 5; ++i) {
ic.updateReceived = false;
ic.sendPreeditText(preeditText, i);
currentRect = input->inputMethodQuery(Qt::ImCursorRectangle).toRect();
sendPreeditText(preeditText, i);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QVERIFY(previousRect.left() < currentRect.left());

QCOMPARE(ic.updateReceived, true);
QVERIFY(inputSpy.count() > 0); inputSpy.clear();
QVERIFY(panelSpy.count() > 0); panelSpy.clear();
previousRect = currentRect;
}

// Verify that if there is no preedit cursor then the micro focus rect is the
// same as it would be if it were positioned at the end of the preedit text.
ic.sendPreeditText(preeditText, 0);
ic.updateReceived = false;
ic.sendEvent(QInputMethodEvent(preeditText, QList<QInputMethodEvent::Attribute>()));
currentRect = input->inputMethodQuery(Qt::ImCursorRectangle).toRect();
sendPreeditText(preeditText, 0);
QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &imEvent);
QCoreApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &query);
currentRect = query.value(Qt::ImCursorRectangle).toRect();
QCOMPARE(currentRect, previousRect);
QCOMPARE(ic.updateReceived, true);
#endif
QVERIFY(inputSpy.count() > 0);
QVERIFY(panelSpy.count() > 0);
}

void tst_qquicktextinput::inputMethodComposing()
Expand Down

0 comments on commit 32bbe6d

Please sign in to comment.