From: Andrew den Exter Date: Fri, 13 Jul 2012 01:11:31 +0000 (+1000) Subject: Fix TextEdit and TextInput test failures. X-Git-Tag: upstream/5.2.1~1444 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=867704b37cfbe055ba947291cde343204ce8e52a;p=platform%2Fupstream%2Fqtdeclarative.git Fix TextEdit and TextInput test failures. Use qWaitForWindowActive and instead of qWaitForWindowShown where focus is required. Also send events directly to the tested item, instead of getting a pointer to the same object from QGuiApplication::focusObject() which can return null and crash the test if focus is stolen. Change-Id: I6b8956a3afc8ffa58394a957a35b64b1c0924596 Reviewed-by: Damian Jansen Reviewed-by: Michael Brasser --- diff --git a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp index 26d64e7..562f24e 100644 --- a/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp +++ b/tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp @@ -583,9 +583,7 @@ void tst_qquicktextedit::alignments() QQuickView canvas(testFileUrl("alignments.qml")); canvas.show(); - canvas.requestActivateWindow(); QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); QObject *ob = canvas.rootObject(); QVERIFY(ob != 0); @@ -727,18 +725,18 @@ void tst_qquicktextedit::hAlign_RightToLeft() QVERIFY(textEdit->positionToRectangle(0).x() < canvas.width()/2); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textEdit->hasActiveFocus()); textEdit->setText(QString()); - { QInputMethodEvent ev(rtlText, QList()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev(rtlText, QList()); QGuiApplication::sendEvent(textEdit, &ev); } QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignRight); - { QInputMethodEvent ev("Hello world!", QList()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev("Hello world!", QList()); QGuiApplication::sendEvent(textEdit, &ev); } QCOMPARE(textEdit->hAlign(), QQuickTextEdit::AlignLeft); // Clear pre-edit text. TextEdit should maybe do this itself on setText, but that may be // redundant as an actual input method may take care of it. - { QInputMethodEvent ev; QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev; QGuiApplication::sendEvent(textEdit, &ev); } // empty text with implicit alignment follows the system locale-based // keyboard input direction from qApp->inputMethod()->inputDirection @@ -814,7 +812,6 @@ void tst_qquicktextedit::hAlignVisual() { QQuickView view(testFileUrl("hAlignVisual.qml")); view.show(); - view.requestActivateWindow(); QTest::qWaitForWindowShown(&view); QQuickText *text = view.rootObject()->findChild("textItem"); @@ -1086,9 +1083,7 @@ void tst_qquicktextedit::persistentSelection() QQuickView canvas(testFileUrl("persistentSelection.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); - canvas.requestActivateWindow(); + QTest::qWaitForWindowActive(&canvas); QQuickTextEdit *edit = qobject_cast(canvas.rootObject()); QVERIFY(edit); @@ -1157,8 +1152,7 @@ void tst_qquicktextedit::focusOnPress() textEditObject->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); QCOMPARE(textEditObject->hasFocus(), false); QCOMPARE(textEditObject->hasActiveFocus(), false); @@ -1358,16 +1352,14 @@ void tst_qquicktextedit::keySelection() QQuickView canvas(testFileUrl("navigation.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); - canvas.requestActivateWindow(); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextEdit *input = qobject_cast(qvariant_cast(canvas.rootObject()->property("myInput"))); QVERIFY(input != 0); - QTRY_VERIFY(input->hasActiveFocus() == true); + QVERIFY(input->hasActiveFocus()); QSignalSpy spy(input, SIGNAL(selectedTextChanged())); @@ -1814,7 +1806,6 @@ void tst_qquicktextedit::mouseSelection() canvas.show(); canvas.requestActivateWindow(); QTest::qWaitForWindowActive(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); QVERIFY(canvas.rootObject() != 0); QQuickTextEdit *textEditObject = qobject_cast(canvas.rootObject()); @@ -1856,8 +1847,7 @@ void tst_qquicktextedit::dragMouseSelection() canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextEdit *textEditObject = qobject_cast(canvas.rootObject()); @@ -1910,8 +1900,7 @@ void tst_qquicktextedit::mouseSelectionMode() canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextEdit *textEditObject = qobject_cast(canvas.rootObject()); @@ -1997,7 +1986,7 @@ void tst_qquicktextedit::positionAt() texteditObject->setCursorPosition(0); QInputMethodEvent inputEvent(preeditText, QList()); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &inputEvent); + QGuiApplication::sendEvent(texteditObject, &inputEvent); // Check all points within the preedit text return the same position. QCOMPARE(texteditObject->positionAt(0, y0), 0); @@ -2017,7 +2006,7 @@ void tst_qquicktextedit::linkActivated() QVERIFY(canvas.rootObject() != 0); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QQuickTextEdit *texteditObject = qobject_cast(canvas.rootObject()); QVERIFY(texteditObject != 0); @@ -2205,8 +2194,8 @@ void tst_qquicktextedit::cursorVisible() QQuickView view(testFileUrl("cursorVisible.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); + QCOMPARE(&view, qGuiApp->focusWindow()); QCOMPARE(edit.isCursorVisible(), false); @@ -2237,7 +2226,7 @@ void tst_qquicktextedit::cursorVisible() QWindow alternateView; alternateView.show(); alternateView.requestActivateWindow(); - QTest::qWaitForWindowShown(&alternateView); + QTest::qWaitForWindowActive(&alternateView); QCOMPARE(edit.isCursorVisible(), false); QCOMPARE(spy.count(), 6); @@ -2518,8 +2507,7 @@ void tst_qquicktextedit::textInput() QQuickView view(testFileUrl("inputMethodEvent.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextEdit *edit = qobject_cast(view.rootObject()); QVERIFY(edit); QVERIFY(edit->hasActiveFocus() == true); @@ -2528,7 +2516,7 @@ void tst_qquicktextedit::textInput() QSignalSpy spy(edit, SIGNAL(textChanged())); QInputMethodEvent event; event.setCommitString( "Hello world!", 0, 0); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(edit, &event); QCOMPARE(edit->text(), QString("Hello world!")); QCOMPARE(spy.count(), 1); @@ -2538,7 +2526,7 @@ void tst_qquicktextedit::textInput() QCOMPARE(editPrivate->text, QString("Hello world!")); QInputMethodQueryEvent queryEvent(Qt::ImEnabled); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &queryEvent); + QGuiApplication::sendEvent(edit, &queryEvent); QCOMPARE(queryEvent.value(Qt::ImEnabled).toBool(), true); edit->setReadOnly(true); @@ -2555,8 +2543,7 @@ void tst_qquicktextedit::inputMethodUpdate() QQuickView view(testFileUrl("inputMethodEvent.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextEdit *edit = qobject_cast(view.rootObject()); QVERIFY(edit); QVERIFY(edit->hasActiveFocus() == true); @@ -2646,8 +2633,7 @@ void tst_qquicktextedit::openInputPanel() QQuickView view(testFileUrl("openInputPanel.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextEdit *edit = qobject_cast(view.rootObject()); QVERIFY(edit); @@ -2655,7 +2641,6 @@ void tst_qquicktextedit::openInputPanel() // check default values QVERIFY(edit->focusOnPress()); QVERIFY(!edit->hasActiveFocus()); - qDebug() << &edit << qApp->focusObject(); QVERIFY(qApp->focusObject() != edit); QCOMPARE(qApp->inputMethod()->isVisible(), false); @@ -2987,9 +2972,8 @@ void tst_qquicktextedit::preeditCursorRectangle() QQuickView view(testFileUrl("inputMethodEvent.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); + QTest::qWaitForWindowActive(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); QQuickTextEdit *edit = qobject_cast(view.rootObject()); QVERIFY(edit); @@ -3001,16 +2985,17 @@ void tst_qquicktextedit::preeditCursorRectangle() QRectF currentRect; + QCOMPARE(QGuiApplication::focusObject(), static_cast(edit)); QInputMethodQueryEvent query(Qt::ImCursorRectangle); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(edit, &query); QRectF previousRect = query.value(Qt::ImCursorRectangle).toRectF(); // Verify that the micro focus rect is positioned the same for position 0 as // it would be if there was no preedit text. QInputMethodEvent imEvent(preeditText, QList() << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, preeditText.length(), QVariant())); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(edit, &imEvent); + QCoreApplication::sendEvent(edit, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(edit->cursorRectangle(), currentRect); QCOMPARE(cursor->pos(), currentRect.topLeft()); @@ -3023,8 +3008,8 @@ void tst_qquicktextedit::preeditCursorRectangle() for (int i = 1; i <= 5; ++i) { QInputMethodEvent imEvent(preeditText, QList() << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, i, preeditText.length(), QVariant())); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(edit, &imEvent); + QCoreApplication::sendEvent(edit, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(edit->cursorRectangle(), currentRect); QCOMPARE(cursor->pos(), currentRect.topLeft()); @@ -3040,8 +3025,8 @@ void tst_qquicktextedit::preeditCursorRectangle() panelSpy.clear(); { QInputMethodEvent imEvent("wwwww", QList() << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 5, 1, QVariant())); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); } - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(edit, &imEvent); } + QCoreApplication::sendEvent(edit, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(edit->cursorRectangle(), currentRect); QCOMPARE(cursor->pos(), currentRect.topLeft()); @@ -3053,8 +3038,8 @@ void tst_qquicktextedit::preeditCursorRectangle() editSpy.clear(); panelSpy.clear(); { QInputMethodEvent imEvent(preeditText, QList()); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); } - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(edit, &imEvent); } + QCoreApplication::sendEvent(edit, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(edit->cursorRectangle(), currentRect); QCOMPARE(cursor->pos(), currentRect.topLeft()); @@ -3070,10 +3055,12 @@ void tst_qquicktextedit::inputMethodComposing() QQuickView view(testFileUrl("inputContext.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); + QQuickTextEdit *edit = qobject_cast(view.rootObject()); QVERIFY(edit); + QCOMPARE(QGuiApplication::focusObject(), static_cast(edit)); + QSignalSpy spy(edit, SIGNAL(inputMethodComposingChanged())); edit->setCursorPosition(12); @@ -3183,10 +3170,10 @@ void tst_qquicktextedit::cursorRectangleSize() textEdit->setFocus(true); canvas->show(); canvas->requestActivateWindow(); - QTest::qWaitForWindowShown(canvas); + QTest::qWaitForWindowActive(canvas); QInputMethodQueryEvent event(Qt::ImCursorRectangle); - qApp->sendEvent(qApp->focusObject(), &event); + qApp->sendEvent(textEdit, &event); QRectF cursorRectFromQuery = event.value(Qt::ImCursorRectangle).toRectF(); QRectF cursorRectFromItem = textEdit->cursorRectangle(); @@ -4041,8 +4028,9 @@ void tst_qquicktextedit::keySequence() textEdit->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + + QVERIFY(textEdit->hasActiveFocus()); simulateKey(&canvas, layoutDirection); @@ -4204,8 +4192,8 @@ void tst_qquicktextedit::undo() canvas.show(); canvas.requestActivateWindow(); QTest::qWaitForWindowActive(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QVERIFY(textEdit->hasActiveFocus()); QVERIFY(!textEdit->canUndo()); QSignalSpy spy(textEdit, SIGNAL(canUndoChanged())); @@ -4289,8 +4277,8 @@ void tst_qquicktextedit::redo() textEdit->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textEdit->hasActiveFocus()); QVERIFY(!textEdit->canUndo()); QVERIFY(!textEdit->canRedo()); @@ -4482,8 +4470,8 @@ void tst_qquicktextedit::undo_keypressevents() textEdit->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textEdit->hasActiveFocus()); simulateKeys(&canvas, keys); @@ -4571,7 +4559,7 @@ void tst_qquicktextedit::emptytags_QTBUG_22058() canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QQuickTextEdit *input = qobject_cast(qvariant_cast(canvas.rootObject()->property("inputField"))); QVERIFY(input->hasActiveFocus()); diff --git a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp index 63fd658..eec9faf 100644 --- a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp @@ -661,9 +661,7 @@ void tst_qquicktextinput::persistentSelection() QQuickView canvas(testFileUrl("persistentSelection.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); - canvas.requestActivateWindow(); + QTest::qWaitForWindowActive(&canvas); QQuickTextInput *input = qobject_cast(canvas.rootObject()); QVERIFY(input); @@ -1176,8 +1174,6 @@ void tst_qquicktextinput::dragMouseSelection() canvas.requestActivateWindow(); QTest::qWaitForWindowActive(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); - QVERIFY(canvas.rootObject() != 0); QQuickTextInput *textInputObject = qobject_cast(canvas.rootObject()); QVERIFY(textInputObject != 0); @@ -1240,7 +1236,6 @@ void tst_qquicktextinput::mouseSelectionMode() canvas.show(); canvas.requestActivateWindow(); QTest::qWaitForWindowActive(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); QVERIFY(canvas.rootObject() != 0); QQuickTextInput *textInputObject = qobject_cast(canvas.rootObject()); @@ -1285,9 +1280,8 @@ void tst_qquicktextinput::horizontalAlignment() QQuickView canvas(testFileUrl("horizontalAlignment.qml")); canvas.show(); - canvas.requestActivateWindow(); QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QObject *ob = canvas.rootObject(); QVERIFY(ob != 0); ob->setProperty("horizontalAlignment",hAlign); @@ -1382,19 +1376,19 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft() QCOMPARE(textInput->boundingRect().left(), qreal(0)); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); // If there is no commited text, the preedit text should determine the alignment. textInput->setText(QString()); - { QInputMethodEvent ev(rtlText, QList()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev(rtlText, QList()); QGuiApplication::sendEvent(textInput, &ev); } QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight); - { QInputMethodEvent ev("Hello world!", QList()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev("Hello world!", QList()); QGuiApplication::sendEvent(textInput, &ev); } QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignLeft); // Clear pre-edit text. TextInput should maybe do this itself on setText, but that may be // redundant as an actual input method may take care of it. - { QInputMethodEvent ev; QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); } + { QInputMethodEvent ev; QGuiApplication::sendEvent(textInput, &ev); } // empty text with implicit alignment follows the system locale-based // keyboard input direction from QInputMethod::inputDirection() @@ -1645,7 +1639,7 @@ void tst_qquicktextinput::positionAt() QVERIFY(canvas.rootObject() != 0); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QQuickTextInput *textinputObject = qobject_cast(canvas.rootObject()); QVERIFY(textinputObject != 0); @@ -1700,7 +1694,7 @@ void tst_qquicktextinput::positionAt() { QInputMethodEvent inputEvent(preeditText, QList()); QVERIFY(qGuiApp->focusObject()); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &inputEvent); } + QGuiApplication::sendEvent(textinputObject, &inputEvent); } // Check all points within the preedit text return the same position. QCOMPARE(evaluate(textinputObject, QString("positionAt(%1)").arg(0)), 0); @@ -1713,7 +1707,7 @@ void tst_qquicktextinput::positionAt() { QInputMethodEvent inputEvent; QVERIFY(qGuiApp->focusObject()); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &inputEvent); } + QGuiApplication::sendEvent(textinputObject, &inputEvent); } // With wrapping. textinputObject->setWrapMode(QQuickTextInput::WrapAnywhere); @@ -1735,7 +1729,7 @@ void tst_qquicktextinput::maxLength() QVERIFY(canvas.rootObject() != 0); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QQuickTextInput *textinputObject = qobject_cast(canvas.rootObject()); QVERIFY(textinputObject != 0); @@ -2123,7 +2117,7 @@ void tst_qquicktextinput::inputMethods() QQuickView canvas(testFileUrl("inputmethods.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); // test input method hints QVERIFY(canvas.rootObject() != 0); @@ -2147,24 +2141,24 @@ void tst_qquicktextinput::inputMethods() QInputMethodEvent event; event.setCommitString( "My ", -12, 0); QTRY_COMPARE(qGuiApp->focusObject(), input); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(input, &event); QCOMPARE(input->text(), QString("My Hello world!")); input->setCursorPosition(2); event.setCommitString("Your", -2, 2); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(input, &event); QCOMPARE(input->text(), QString("Your Hello world!")); QCOMPARE(input->cursorPosition(), 4); input->setCursorPosition(7); event.setCommitString("Goodbye", -2, 5); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(input, &event); QCOMPARE(input->text(), QString("Your Goodbye world!")); QCOMPARE(input->cursorPosition(), 12); input->setCursorPosition(8); event.setCommitString("Our", -8, 4); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(input, &event); QCOMPARE(input->text(), QString("Our Goodbye world!")); QCOMPARE(input->cursorPosition(), 7); @@ -2173,7 +2167,7 @@ void tst_qquicktextinput::inputMethods() input->setCursorPosition(0); input->moveCursorSelection(input->text().length()); event.setCommitString("replacement", -input->text().length(), input->text().length()); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &event); + QGuiApplication::sendEvent(input, &event); QCOMPARE(input->selectionStart(), input->selectionEnd()); QInputMethodQueryEvent enabledQueryEvent(Qt::ImEnabled); @@ -2381,8 +2375,7 @@ void tst_qquicktextinput::copyAndPasteKeySequence() { textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); // copy and paste QVERIFY(textInput->hasActiveFocus()); @@ -2647,8 +2640,7 @@ void tst_qquicktextinput::cursorVisible() QQuickView view(testFileUrl("cursorVisible.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QCOMPARE(input.isCursorVisible(), false); @@ -2679,7 +2671,7 @@ void tst_qquicktextinput::cursorVisible() QWindow alternateView; alternateView.show(); alternateView.requestActivateWindow(); - QTest::qWaitForWindowShown(&alternateView); + QTest::qWaitForWindowActive(&alternateView); QCOMPARE(input.isCursorVisible(), false); QCOMPARE(spy.count(), 6); @@ -2957,8 +2949,7 @@ void tst_qquicktextinput::echoMode() QQuickView canvas(testFileUrl("echoMode.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); @@ -3032,13 +3023,13 @@ void tst_qquicktextinput::passwordEchoDelay() QQuickView canvas(testFileUrl("echoMode.qml")); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(&canvas, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextInput *input = qobject_cast(qvariant_cast(canvas.rootObject()->property("myInput"))); QVERIFY(input); + QVERIFY(input->hasActiveFocus()); QQuickItem *cursor = input->findChild("cursor"); QVERIFY(cursor); @@ -3085,7 +3076,7 @@ void tst_qquicktextinput::passwordEchoDelay() QInputMethodEvent ev; ev.setCommitString(QLatin1String("7")); - QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); + QGuiApplication::sendEvent(input, &ev); QCOMPARE(input->displayText(), QString(7, fillChar) + QLatin1Char('7')); input->setCursorPosition(3); @@ -3138,8 +3129,7 @@ void tst_qquicktextinput::focusOnPress() textInputObject->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); QCOMPARE(textInputObject->hasFocus(), false); QCOMPARE(textInputObject->hasActiveFocus(), false); @@ -3202,8 +3192,7 @@ void tst_qquicktextinput::openInputPanel() QQuickView view(testFileUrl("openInputPanel.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextInput *input = qobject_cast(view.rootObject()); QVERIFY(input); @@ -3292,7 +3281,7 @@ void tst_qquicktextinput::setHAlignClearCache() input.setParentItem(view.rootItem()); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); + QTest::qWaitForWindowActive(&view); #ifdef Q_OS_MAC QEXPECT_FAIL("", "QTBUG-23485", Abort); #endif @@ -3315,7 +3304,8 @@ void tst_qquicktextinput::focusOutClearSelection() input2.componentComplete(); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); + QTest::qWaitForWindowActive(&view); + QVERIFY(input.hasActiveFocus()); input.select(2,5); //The selection should work QTRY_COMPARE(input.selectedText(), QLatin1String("llo")); @@ -3370,11 +3360,11 @@ void tst_qquicktextinput::contentSize() } } -static void sendPreeditText(const QString &text, int cursor) +static void sendPreeditText(QQuickItem *item, const QString &text, int cursor) { QInputMethodEvent event(text, QList() << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, cursor, text.length(), QVariant())); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &event); + QCoreApplication::sendEvent(item, &event); } void tst_qquicktextinput::preeditAutoScroll() @@ -3384,8 +3374,7 @@ void tst_qquicktextinput::preeditAutoScroll() QQuickView view(testFileUrl("preeditAutoScroll.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextInput *input = qobject_cast(view.rootObject()); QVERIFY(input); QVERIFY(input->hasActiveFocus()); @@ -3396,14 +3385,14 @@ void tst_qquicktextinput::preeditAutoScroll() int cursorRectangleChanges = 0; // test the text is scrolled so the preedit is visible. - sendPreeditText(preeditText.mid(0, 3), 1); + sendPreeditText(input, preeditText.mid(0, 3), 1); QVERIFY(evaluate(input, QString("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. QInputMethodEvent imEvent; - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); + QCoreApplication::sendEvent(input, &imEvent); QCOMPARE(evaluate(input, QString("positionAt(%1)").arg(0)), 0); QCOMPARE(evaluate(input, QString("positionAt(%1)").arg(input->width())), 5); QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges); @@ -3423,7 +3412,7 @@ void tst_qquicktextinput::preeditAutoScroll() // character preceding the cursor is still visible. qreal x = input->positionToRectangle(0).x(); for (int i = 0; i < 3; ++i) { - sendPreeditText(preeditText, i + 1); + sendPreeditText(input, 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); @@ -3431,7 +3420,7 @@ void tst_qquicktextinput::preeditAutoScroll() x = input->positionToRectangle(0).x(); } for (int i = 1; i >= 0; --i) { - sendPreeditText(preeditText, i + 1); + sendPreeditText(input, 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); @@ -3441,25 +3430,25 @@ void tst_qquicktextinput::preeditAutoScroll() // Test incrementing the preedit cursor doesn't cause further // scrolling when right most text is visible. - sendPreeditText(preeditText, preeditText.length() - 3); + sendPreeditText(input, preeditText, preeditText.length() - 3); QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges); x = input->positionToRectangle(0).x(); for (int i = 2; i >= 0; --i) { - sendPreeditText(preeditText, preeditText.length() - i); + sendPreeditText(input, preeditText, preeditText.length() - i); QCOMPARE(input->positionToRectangle(0).x(), x); QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges); } for (int i = 1; i < 3; ++i) { - sendPreeditText(preeditText, preeditText.length() - i); + sendPreeditText(input, preeditText, preeditText.length() - i); QCOMPARE(input->positionToRectangle(0).x(), x); QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges); } // Test disabling auto scroll. - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); + QCoreApplication::sendEvent(input, &imEvent); input->setAutoScroll(false); - sendPreeditText(preeditText.mid(0, 3), 1); + sendPreeditText(input, preeditText.mid(0, 3), 1); QCOMPARE(evaluate(input, QString("positionAt(%1)").arg(0)), 0); QCOMPARE(evaluate(input, QString("positionAt(%1)").arg(input->width())), 5); } @@ -3471,10 +3460,10 @@ void tst_qquicktextinput::preeditCursorRectangle() QQuickView view(testFileUrl("inputMethodEvent.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextInput *input = qobject_cast(view.rootObject()); QVERIFY(input); + QVERIFY(input->hasActiveFocus()); QQuickItem *cursor = input->findChild("cursor"); QVERIFY(cursor); @@ -3482,13 +3471,13 @@ void tst_qquicktextinput::preeditCursorRectangle() QRectF currentRect; QInputMethodQueryEvent query(Qt::ImCursorRectangle); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(input, &query); QRectF previousRect = query.value(Qt::ImCursorRectangle).toRectF(); // Verify that the micro focus rect is positioned the same for position 0 as // it would be if there was no preedit text. - sendPreeditText(preeditText, 0); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + sendPreeditText(input, preeditText, 0); + QCoreApplication::sendEvent(input, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(currentRect, previousRect); QCOMPARE(input->cursorRectangle(), currentRect); @@ -3500,8 +3489,8 @@ void tst_qquicktextinput::preeditCursorRectangle() // Verify that the micro focus rect moves to the left as the cursor position // is incremented. for (int i = 1; i <= 5; ++i) { - sendPreeditText(preeditText, i); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + sendPreeditText(input, preeditText, i); + QCoreApplication::sendEvent(input, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QVERIFY(previousRect.left() < currentRect.left()); QCOMPARE(input->cursorRectangle(), currentRect); @@ -3515,8 +3504,8 @@ void tst_qquicktextinput::preeditCursorRectangle() // but the (non-zero) cursor position is the same. inputSpy.clear(); panelSpy.clear(); - sendPreeditText("wwwww", 5); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + sendPreeditText(input, "wwwww", 5); + QCoreApplication::sendEvent(input, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(input->cursorRectangle(), currentRect); QCOMPARE(cursor->pos(), currentRect.topLeft()); @@ -3528,8 +3517,8 @@ void tst_qquicktextinput::preeditCursorRectangle() inputSpy.clear(); panelSpy.clear(); { QInputMethodEvent imEvent(preeditText, QList()); - QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent); } - QCoreApplication::sendEvent(qGuiApp->focusObject(), &query); + QCoreApplication::sendEvent(input, &imEvent); } + QCoreApplication::sendEvent(input, &query); currentRect = query.value(Qt::ImCursorRectangle).toRectF(); QCOMPARE(currentRect, previousRect); QCOMPARE(input->cursorRectangle(), currentRect); @@ -3554,8 +3543,7 @@ void tst_qquicktextinput::inputContextMouseHandler() view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QTextLayout layout(text); layout.setFont(input->font()); @@ -3591,10 +3579,10 @@ void tst_qquicktextinput::inputMethodComposing() QQuickView view(testFileUrl("inputContext.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextInput *input = qobject_cast(view.rootObject()); QVERIFY(input); + QVERIFY(input->hasActiveFocus()); QSignalSpy spy(input, SIGNAL(inputMethodComposingChanged())); QCOMPARE(input->isInputMethodComposing(), false); @@ -3697,10 +3685,10 @@ void tst_qquicktextinput::inputMethodUpdate() QQuickView view(testFileUrl("inputContext.qml")); view.show(); view.requestActivateWindow(); - QTest::qWaitForWindowShown(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); + QTest::qWaitForWindowActive(&view); QQuickTextInput *input = qobject_cast(view.rootObject()); QVERIFY(input); + QVERIFY(input->hasActiveFocus()); // text change even without cursor position change needs to trigger update input->setText("test"); @@ -3787,11 +3775,11 @@ void tst_qquicktextinput::cursorRectangleSize() textInput->setFocus(true); canvas->show(); canvas->requestActivateWindow(); - QTest::qWaitForWindowShown(canvas); - QTRY_VERIFY(qApp->focusObject()); + QTest::qWaitForWindowActive(canvas); + QVERIFY(textInput->hasActiveFocus()); QInputMethodQueryEvent event(Qt::ImCursorRectangle); - qApp->sendEvent(qApp->focusObject(), &event); + qApp->sendEvent(textInput, &event); QRectF cursorRectFromQuery = event.value(Qt::ImCursorRectangle).toRectF(); QRectF cursorRectFromItem = textInput->cursorRectangle(); @@ -3821,8 +3809,6 @@ void tst_qquicktextinput::tripleClickSelectsAll() view.requestActivateWindow(); QTest::qWaitForWindowActive(&view); - QTRY_COMPARE(&view, qGuiApp->focusWindow()); - QQuickTextInput* input = qobject_cast(view.rootObject()); QVERIFY(input); @@ -4813,8 +4799,8 @@ void tst_qquicktextinput::keySequence() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); simulateKey(&canvas, layoutDirection); @@ -4974,8 +4960,8 @@ void tst_qquicktextinput::undo() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); QVERIFY(!textInput->canUndo()); @@ -5060,9 +5046,9 @@ void tst_qquicktextinput::redo() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); QVERIFY(!textInput->canUndo()); QVERIFY(!textInput->canRedo()); @@ -5262,8 +5248,8 @@ void tst_qquicktextinput::undo_keypressevents() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); simulateKeys(&canvas, keys); @@ -5281,7 +5267,7 @@ void tst_qquicktextinput::QTBUG_19956() QQuickView canvas(testFileUrl(url)); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextInput *input = qobject_cast(canvas.rootObject()); QVERIFY(input); @@ -5320,7 +5306,7 @@ void tst_qquicktextinput::QTBUG_19956_regexp() QQuickView canvas(url); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); + QTest::qWaitForWindowActive(&canvas); QVERIFY(canvas.rootObject() != 0); QQuickTextInput *input = qobject_cast(canvas.rootObject()); QVERIFY(input); @@ -5632,8 +5618,8 @@ void tst_qquicktextinput::setInputMask() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); simulateKey(&canvas, Qt::Key_Home); for (int i = 0; i < input.length(); i++) @@ -5762,8 +5748,8 @@ void tst_qquicktextinput::keypress_inputMask() textInput->setParentItem(canvas.rootItem()); canvas.show(); canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); + QTest::qWaitForWindowActive(&canvas); + QVERIFY(textInput->hasActiveFocus()); simulateKeys(&canvas, keys); @@ -5798,19 +5784,12 @@ void tst_qquicktextinput::hasAcceptableInputMask() QFETCH(QString, invalid); QFETCH(QString, valid); - QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + optionalMask + "\" }"; + QString componentStr = "import QtQuick 2.0\nTextInput { inputMask: \"" + optionalMask + "\" }"; QQmlComponent textInputComponent(&engine); textInputComponent.setData(componentStr.toLatin1(), QUrl()); QQuickTextInput *textInput = qobject_cast(textInputComponent.create()); QVERIFY(textInput != 0); - QQuickCanvas canvas; - textInput->setParentItem(canvas.rootItem()); - canvas.show(); - canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); - // test that invalid input (for required) work for optionalMask textInput->setText(invalid); QVERIFY(textInput->hasAcceptableInput()); @@ -5861,19 +5840,12 @@ void tst_qquicktextinput::maskCharacter() QFETCH(QString, input); QFETCH(bool, expectedValid); - QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + mask + "\" }"; + QString componentStr = "import QtQuick 2.0\nTextInput { inputMask: \"" + mask + "\" }"; QQmlComponent textInputComponent(&engine); textInputComponent.setData(componentStr.toLatin1(), QUrl()); QQuickTextInput *textInput = qobject_cast(textInputComponent.create()); QVERIFY(textInput != 0); - QQuickCanvas canvas; - textInput->setParentItem(canvas.rootItem()); - canvas.show(); - canvas.requestActivateWindow(); - QTest::qWaitForWindowShown(&canvas); - QTRY_COMPARE(QGuiApplication::focusWindow(), &canvas); - for (int i = 0; i < input.size(); ++i) { QString in = QString(input.at(i)); QString expected = expectedValid ? in : QString();