diff options
Diffstat (limited to 'tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp')
-rw-r--r-- | tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp | 249 |
1 files changed, 231 insertions, 18 deletions
diff --git a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp index ef32ee3..1720c33 100644 --- a/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp +++ b/tests/auto/declarative/qdeclarativetextinput/tst_qdeclarativetextinput.cpp @@ -7,29 +7,29 @@ ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** ** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 2.1 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 2.1 requirements -** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception +** rights. These rights are described in the Nokia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. ** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. ** ** ** @@ -95,6 +95,12 @@ private slots: void moveCursorSelection(); void moveCursorSelectionSequence_data(); void moveCursorSelectionSequence(); + void mouseSelection_data(); + void mouseSelection(); + void deferEnableSelectByMouse_data(); + void deferEnableSelectByMouse(); + void deferDisableSelectByMouse_data(); + void deferDisableSelectByMouse(); void dragMouseSelection(); void mouseSelectionMode_data(); void mouseSelectionMode(); @@ -375,6 +381,13 @@ void tst_qdeclarativetextinput::selection() QCOMPARE(textinputObject->selectionEnd(), i); QVERIFY(textinputObject->selectedText().isNull()); } + //Test cursor follows selection + for(int i=0; i<= testStr.size(); i++) { + textinputObject->select(i,i); + QCOMPARE(textinputObject->cursorPosition(), i); + QCOMPARE(textinputObject->selectionStart(), i); + QCOMPARE(textinputObject->selectionEnd(), i); + } textinputObject->setCursorPosition(0); QVERIFY(textinputObject->cursorPosition() == 0); @@ -399,10 +412,12 @@ void tst_qdeclarativetextinput::selection() for(int i=0; i<= testStr.size(); i++) { textinputObject->select(0,i); QCOMPARE(testStr.mid(0,i), textinputObject->selectedText()); + QCOMPARE(textinputObject->cursorPosition(), i); } for(int i=0; i<= testStr.size(); i++) { textinputObject->select(i,testStr.size()); QCOMPARE(testStr.mid(i,testStr.size()-i), textinputObject->selectedText()); + QCOMPARE(textinputObject->cursorPosition(), testStr.size()); } textinputObject->setCursorPosition(0); @@ -902,6 +917,141 @@ void tst_qdeclarativetextinput::moveCursorSelectionSequence() QCOMPARE(textinputObject->selectionEnd(), selection2End); } +void tst_qdeclarativetextinput::mouseSelection_data() +{ + QTest::addColumn<QString>("qmlfile"); + QTest::addColumn<bool>("expectSelection"); + + // import installed + QTest::newRow("on") << SRCDIR "/data/mouseselection_true.qml" << true; + QTest::newRow("off") << SRCDIR "/data/mouseselection_false.qml" << false; + QTest::newRow("default") << SRCDIR "/data/mouseselection_default.qml" << false; + QTest::newRow("on word selection") << SRCDIR "/data/mouseselection_true_words.qml" << true; + QTest::newRow("off word selection") << SRCDIR "/data/mouseselection_false_words.qml" << false; + QTest::newRow("on read only") << SRCDIR "/data/mouseselection_true_readonly.qml" << true; + QTest::newRow("off read only") << SRCDIR "/data/mouseselection_false_readonly.qml" << false; +} + +void tst_qdeclarativetextinput::mouseSelection() +{ + QFETCH(QString, qmlfile); + QFETCH(bool, expectSelection); + + QDeclarativeView *canvas = createView(qmlfile); + + canvas->show(); + QApplication::setActiveWindow(canvas); + QTest::qWaitForWindowShown(canvas); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); + + QVERIFY(canvas->rootObject() != 0); + QDeclarativeTextInput *textInputObject = qobject_cast<QDeclarativeTextInput *>(canvas->rootObject()); + QVERIFY(textInputObject != 0); + + // press-and-drag-and-release from x1 to x2 + int x1 = 10; + int x2 = 70; + int y = textInputObject->height()/2; + QTest::mousePress(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x1,y))); + //QTest::mouseMove(canvas->viewport(), canvas->mapFromScene(QPoint(x2,y))); // doesn't work + QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(QPoint(x2,y)), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); + QApplication::sendEvent(canvas->viewport(), &mv); + QTest::mouseRelease(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x2,y))); + QString str = textInputObject->selectedText(); + if (expectSelection) + QVERIFY(str.length() > 3); // don't reallly care *what* was selected (and it's too sensitive to platform) + else + QVERIFY(str.isEmpty()); + + // Clicking and shift to clicking between the same points should select the same text. + textInputObject->setCursorPosition(0); + QTest::mouseClick(canvas->viewport(), Qt::LeftButton, Qt::NoModifier, canvas->mapFromScene(QPoint(x1,y))); + QTest::mouseClick(canvas->viewport(), Qt::LeftButton, Qt::ShiftModifier, canvas->mapFromScene(QPoint(x2,y))); + QCOMPARE(textInputObject->selectedText(), str); + + delete canvas; +} + +void tst_qdeclarativetextinput::deferEnableSelectByMouse_data() +{ + QTest::addColumn<QString>("qmlfile"); + + QTest::newRow("writable") << SRCDIR "/data/mouseselection_false.qml"; + QTest::newRow("read only") << SRCDIR "/data/mouseselection_false_readonly.qml"; +} + +void tst_qdeclarativetextinput::deferEnableSelectByMouse() +{ + // Verify text isn't selected if selectByMouse is enabled after the mouse button has been pressed. + QFETCH(QString, qmlfile); + + QDeclarativeView *canvas = createView(qmlfile); + + canvas->show(); + QApplication::setActiveWindow(canvas); + QTest::qWaitForWindowShown(canvas); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); + + QVERIFY(canvas->rootObject() != 0); + QDeclarativeTextInput *textInputObject = qobject_cast<QDeclarativeTextInput *>(canvas->rootObject()); + QVERIFY(textInputObject != 0); + + // press-and-drag-and-release from x1 to x2 + int x1 = 10; + int x2 = 70; + int y = textInputObject->height()/2; + + QTest::mousePress(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x1,y))); + textInputObject->setSelectByMouse(true); + //QTest::mouseMove(canvas->viewport(), canvas->mapFromScene(QPoint(x2,y))); // doesn't work + QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(QPoint(x2,y)), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); + QApplication::sendEvent(canvas->viewport(), &mv); + QTest::mouseRelease(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x2,y))); + QVERIFY(textInputObject->selectedText().isEmpty()); + + delete canvas; +} + +void tst_qdeclarativetextinput::deferDisableSelectByMouse_data() +{ + QTest::addColumn<QString>("qmlfile"); + + QTest::newRow("writable") << SRCDIR "/data/mouseselection_true.qml"; + QTest::newRow("read only") << SRCDIR "/data/mouseselection_true_readonly.qml"; +} + +void tst_qdeclarativetextinput::deferDisableSelectByMouse() +{ + // Verify text isn't selected if selectByMouse is enabled after the mouse button has been pressed. + QFETCH(QString, qmlfile); + + QDeclarativeView *canvas = createView(qmlfile); + + canvas->show(); + QApplication::setActiveWindow(canvas); + QTest::qWaitForWindowShown(canvas); + QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); + + QVERIFY(canvas->rootObject() != 0); + QDeclarativeTextInput *textInputObject = qobject_cast<QDeclarativeTextInput *>(canvas->rootObject()); + QVERIFY(textInputObject != 0); + + // press-and-drag-and-release from x1 to x2 + int x1 = 10; + int x2 = 70; + int y = textInputObject->height()/2; + + QTest::mousePress(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x1,y))); + textInputObject->setSelectByMouse(false); + //QTest::mouseMove(canvas->viewport(), canvas->mapFromScene(QPoint(x2,y))); // doesn't work + QMouseEvent mv(QEvent::MouseMove, canvas->mapFromScene(QPoint(x2,y)), Qt::LeftButton, Qt::LeftButton,Qt::NoModifier); + QApplication::sendEvent(canvas->viewport(), &mv); + QTest::mouseRelease(canvas->viewport(), Qt::LeftButton, 0, canvas->mapFromScene(QPoint(x2,y))); + QVERIFY(textInputObject->selectedText().length() > 3); + + delete canvas; +} + void tst_qdeclarativetextinput::dragMouseSelection() { QString qmlfile = SRCDIR "/data/mouseselection_true.qml"; @@ -1364,8 +1514,10 @@ void tst_qdeclarativetextinput::inputMethods() QVERIFY(canvas->rootObject() != 0); QDeclarativeTextInput *input = qobject_cast<QDeclarativeTextInput *>(canvas->rootObject()); QVERIFY(input != 0); + QVERIFY(input->imHints() & Qt::ImhNoPredictiveText); QVERIFY(input->inputMethodHints() & Qt::ImhNoPredictiveText); - input->setInputMethodHints(Qt::ImhUppercaseOnly); + input->setIMHints(Qt::ImhUppercaseOnly); + QVERIFY(input->imHints() & Qt::ImhUppercaseOnly); QVERIFY(input->inputMethodHints() & Qt::ImhUppercaseOnly); QVERIFY(canvas->rootObject() != 0); @@ -1632,6 +1784,15 @@ void tst_qdeclarativetextinput::cursorDelegate() QCOMPARE(textInputObject->cursorRectangle().x(), qRound(delegateObject->x())); QCOMPARE(textInputObject->cursorRectangle().y(), qRound(delegateObject->y())); } + const QString preedit = "preedit"; + for (int i = 0; i <= preedit.length(); i++) { + QInputMethodEvent event(preedit, QList<QInputMethodEvent::Attribute>() + << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, i, 1, QVariant())); + QApplication::sendEvent(view, &event); + + QCOMPARE(textInputObject->cursorRectangle().x(), qRound(delegateObject->x())); + QCOMPARE(textInputObject->cursorRectangle().y(), qRound(delegateObject->y())); + } textInputObject->setCursorPosition(0); QCOMPARE(textInputObject->cursorRectangle().x(), qRound(delegateObject->x())); QCOMPARE(textInputObject->cursorRectangle().y(), qRound(delegateObject->y())); @@ -1805,6 +1966,7 @@ void tst_qdeclarativetextinput::echoMode() ref &= ~Qt::ImhHiddenText; ref &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhNone); input->setEchoMode(QDeclarativeTextInput::NoEcho); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("")); @@ -1813,6 +1975,7 @@ void tst_qdeclarativetextinput::echoMode() ref |= Qt::ImhHiddenText; ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhNone); input->setEchoMode(QDeclarativeTextInput::Password); //Password ref |= Qt::ImhHiddenText; @@ -1820,6 +1983,7 @@ void tst_qdeclarativetextinput::echoMode() QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("********")); QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhNone); input->setPasswordCharacter(QChar('Q')); QCOMPARE(input->passwordCharacter(), QLatin1String("Q")); QCOMPARE(input->text(), initial); @@ -1829,6 +1993,7 @@ void tst_qdeclarativetextinput::echoMode() ref &= ~Qt::ImhHiddenText; ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhNone); QCOMPARE(input->text(), initial); QCOMPARE(input->displayText(), QLatin1String("QQQQQQQQ")); QCOMPARE(input->inputMethodQuery(Qt::ImSurroundingText).toString(), QLatin1String("QQQQQQQQ")); @@ -1849,6 +2014,40 @@ void tst_qdeclarativetextinput::echoMode() QCOMPARE(input->displayText(), initial); QCOMPARE(input->inputMethodQuery(Qt::ImSurroundingText).toString(), initial); + // Test echo mode doesn't override imHints. + input->setIMHints(Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + ref |= Qt::ImhDialableCharactersOnly; + //Normal + input->setEchoMode(QDeclarativeTextInput::Normal); + ref |= Qt::ImhHiddenText; + ref &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + //NoEcho + input->setEchoMode(QDeclarativeTextInput::NoEcho); + ref |= Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + //Password + input->setEchoMode(QDeclarativeTextInput::Password); + ref |= Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + //PasswordEchoOnEdit + input->setEchoMode(QDeclarativeTextInput::PasswordEchoOnEdit); + ref &= ~Qt::ImhHiddenText; + ref |= (Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + //Normal + input->setEchoMode(QDeclarativeTextInput::Normal); + ref |= Qt::ImhHiddenText; + ref &= ~(Qt::ImhNoAutoUppercase | Qt::ImhNoPredictiveText); + QCOMPARE(input->inputMethodHints(), ref); + QCOMPARE(input->imHints(), Qt::ImhHiddenText | Qt::ImhDialableCharactersOnly); + delete canvas; } @@ -2285,6 +2484,20 @@ void tst_qdeclarativetextinput::preeditAutoScroll() ic.sendPreeditText(preeditText.mid(0, 3), 1); QCOMPARE(input.positionAt(0), 0); QCOMPARE(input.positionAt(input.width()), 5); + + ic.sendEvent(QInputMethodEvent()); + input.setAutoScroll(true); + // Test committing pre-edit text at the start of the string. QTBUG-18789 + input.setCursorPosition(0); + ic.sendPreeditText(input.text(), 5); + QCOMPARE(input.positionAt(0), 0); + + QInputMethodEvent event; + event.setCommitString(input.text()); + ic.sendEvent(event); + + QCOMPARE(input.positionAt(0), 0); + QCOMPARE(input.positionAt(input.width()), 5); } void tst_qdeclarativetextinput::preeditMicroFocus() |