From 1b21b73e89942d567c90a17a3bf7a7ecae3de258 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Tue, 25 Jul 2017 12:48:33 +0200 Subject: QQuickTextInput: Allow going from an Acceptable to an Intermediate state When editing text with a validator set then it can happen that it would no longer be in the acceptable state. This ensures that it does not prevent editing the text when an input mask is used to go back to an Intermediate state. Change-Id: I6da533d18035e9da468939c28a961bc8f2f3090b Reviewed-by: Robin Burchell Reviewed-by: Mitch Curtis --- .../quick/qquicktextinput/tst_qquicktextinput.cpp | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp') diff --git a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp index 2e20275193..9b526f80c5 100644 --- a/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp @@ -216,6 +216,8 @@ private slots: void keypress_inputMask_data(); void keypress_inputMask(); void keypress_inputMethod_inputMask(); + void keypress_inputMask_withValidator_data(); + void keypress_inputMask_withValidator(); void hasAcceptableInputMask_data(); void hasAcceptableInputMask(); void maskCharacter_data(); @@ -6112,6 +6114,79 @@ void tst_qquicktextinput::negativeDimensions() QCOMPARE(input->height(), qreal(-1)); } +void tst_qquicktextinput::keypress_inputMask_withValidator_data() +{ + QTest::addColumn("mask"); + QTest::addColumn("validatorMinimum"); + QTest::addColumn("validatorMaximum"); + QTest::addColumn("decimals"); + QTest::addColumn("validatorRegExp"); + QTest::addColumn("keys"); + QTest::addColumn("expectedText"); + QTest::addColumn("expectedDisplayText"); + + { + KeyList keys; + // inserting '1212' then two backspaces + keys << Qt::Key_Home << "1212" << Qt::Key_Backspace << Qt::Key_Backspace; + QTest::newRow("backspaceWithInt") << QString("9999;_") << 1.0 << 9999.00 << 0 << QString() + << keys << QString("12") << QString("12__"); + } + { + KeyList keys; + // inserting '12.12' then two backspaces + keys << Qt::Key_Home << "12.12" << Qt::Key_Backspace << Qt::Key_Backspace; + QTest::newRow("backspaceWithDouble") << QString("99.99;_") << 1.0 << 99.99 << 2 << QString() + << keys << QString("12.") << QString("12.__"); + } + { + KeyList keys; + // inserting '1111.11' then two backspaces + keys << Qt::Key_Home << "1111.11" << Qt::Key_Backspace << Qt::Key_Backspace; + QTest::newRow("backspaceWithRegExp") << QString("9999.99;_") << 0.0 << 0.0 << 0 + << QString("/^[-]?((\\.\\d+)|(\\d+(\\.\\d+)?))$/") + << keys << QString("1111.") << QString("1111.__"); + } +} + +void tst_qquicktextinput::keypress_inputMask_withValidator() +{ + QFETCH(QString, mask); + QFETCH(qreal, validatorMinimum); + QFETCH(qreal, validatorMaximum); + QFETCH(int, decimals); + QFETCH(QString, validatorRegExp); + QFETCH(KeyList, keys); + QFETCH(QString, expectedText); + QFETCH(QString, expectedDisplayText); + + QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + mask + "\"\n"; + if (!validatorRegExp.isEmpty()) + componentStr += "validator: RegExpValidator { regExp: " + validatorRegExp + " }\n}"; + else if (decimals > 0) + componentStr += QString("validator: DoubleValidator { bottom: %1; decimals: %2; top: %3 }\n}"). + arg(validatorMinimum).arg(decimals).arg(validatorMaximum); + else + componentStr += QString("validator: IntValidator { bottom: %1; top: %2 }\n}"). + arg((int)validatorMinimum).arg((int)validatorMaximum); + + QQmlComponent textInputComponent(&engine); + textInputComponent.setData(componentStr.toLatin1(), QUrl()); + QQuickTextInput *textInput = qobject_cast(textInputComponent.create()); + QVERIFY(textInput != 0); + + QQuickWindow window; + textInput->setParentItem(window.contentItem()); + window.show(); + window.requestActivate(); + QTest::qWaitForWindowActive(&window); + QVERIFY(textInput->hasActiveFocus()); + + simulateKeys(&window, keys); + + QCOMPARE(textInput->text(), expectedText); + QCOMPARE(textInput->displayText(), expectedDisplayText); +} void tst_qquicktextinput::setInputMask_data() { -- cgit v1.2.3