Don't overwrite text selections made in focus handlers.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Mon, 12 Mar 2012 05:49:26 +0000 (15:49 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 21 Mar 2012 05:52:37 +0000 (06:52 +0100)
Defer giving focus to TextInput or TextEdit until after other mouse
press handling has been performed, otherwise setting the cursor position
will overwrite any selection or cursor position set in the
on(Active)FocusChanged handler.

Change-Id: I6332642484de65138a444987b5a81be69c6f835f
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
src/quick/items/qquicktextedit.cpp
src/quick/items/qquicktextinput.cpp
tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp
tests/auto/quick/qquicktextinput/tst_qquicktextinput.cpp

index 5456d35..79df5ab 100644 (file)
@@ -1477,6 +1477,7 @@ Handles the given mouse \a event.
 void QQuickTextEdit::mousePressEvent(QMouseEvent *event)
 {
     Q_D(QQuickTextEdit);
+    d->control->processEvent(event, QPointF(0, -d->yoff));
     if (d->focusOnPress){
         bool hadActiveFocus = hasActiveFocus();
         forceActiveFocus();
@@ -1484,7 +1485,6 @@ void QQuickTextEdit::mousePressEvent(QMouseEvent *event)
         if (hasActiveFocus() && hadActiveFocus && !isReadOnly())
             openSoftwareInputPanel();
     }
-    d->control->processEvent(event, QPointF(0, -d->yoff));
     if (!event->isAccepted())
         QQuickImplicitSizeItem::mousePressEvent(event);
 }
index 30271c7..c9b2e7b 100644 (file)
@@ -1444,13 +1444,9 @@ void QQuickTextInput::mousePressEvent(QMouseEvent *event)
 
     d->pressPos = event->localPos();
 
-    if (d->focusOnPress) {
-        bool hadActiveFocus = hasActiveFocus();
-        forceActiveFocus();
-        // re-open input panel on press if already focused
-        if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
-            openSoftwareInputPanel();
-    }
+    if (d->sendMouseEventToInputContext(event))
+        return;
+
     if (d->selectByMouse) {
         setKeepMouseGrab(false);
         d->selectPressed = true;
@@ -1463,12 +1459,18 @@ void QQuickTextInput::mousePressEvent(QMouseEvent *event)
         }
     }
 
-    if (d->sendMouseEventToInputContext(event))
-        return;
-
     bool mark = (event->modifiers() & Qt::ShiftModifier) && d->selectByMouse;
     int cursor = d->positionAt(event->localPos());
     d->moveCursor(cursor, mark);
+
+    if (d->focusOnPress) {
+        bool hadActiveFocus = hasActiveFocus();
+        forceActiveFocus();
+        // re-open input panel on press if already focused
+        if (hasActiveFocus() && hadActiveFocus && !d->m_readOnly)
+            openSoftwareInputPanel();
+    }
+
     event->setAccepted(true);
 }
 
index 7b3c846..46a4ea4 100644 (file)
@@ -1013,23 +1013,88 @@ void tst_qquicktextedit::persistentSelection()
 
 void tst_qquicktextedit::focusOnPress()
 {
-    {
-        QString componentStr = "import QtQuick 2.0\nTextEdit {  activeFocusOnPress: true; text: \"Hello World\" }";
-        QQmlComponent texteditComponent(&engine);
-        texteditComponent.setData(componentStr.toLatin1(), QUrl());
-        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
-        QVERIFY(textEditObject != 0);
-        QCOMPARE(textEditObject->focusOnPress(), true);
-    }
+    QString componentStr =
+            "import QtQuick 2.0\n"
+            "TextEdit {\n"
+                "property bool selectOnFocus: false\n"
+                "width: 100; height: 50\n"
+                "activeFocusOnPress: true\n"
+                "text: \"Hello World\"\n"
+                "onFocusChanged: { if (focus && selectOnFocus) selectAll() }"
+            " }";
+    QQmlComponent texteditComponent(&engine);
+    texteditComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
+    QVERIFY(textEditObject != 0);
+    QCOMPARE(textEditObject->focusOnPress(), true);
+    QCOMPARE(textEditObject->hasFocus(), false);
 
-    {
-        QString componentStr = "import QtQuick 2.0\nTextEdit {  activeFocusOnPress: false; text: \"Hello World\" }";
-        QQmlComponent texteditComponent(&engine);
-        texteditComponent.setData(componentStr.toLatin1(), QUrl());
-        QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit*>(texteditComponent.create());
-        QVERIFY(textEditObject != 0);
-        QCOMPARE(textEditObject->focusOnPress(), false);
-    }
+    QSignalSpy focusSpy(textEditObject, SIGNAL(focusChanged(bool)));
+    QSignalSpy activeFocusSpy(textEditObject, SIGNAL(focusChanged(bool)));
+    QSignalSpy activeFocusOnPressSpy(textEditObject, SIGNAL(activeFocusOnPressChanged(bool)));
+
+    textEditObject->setFocusOnPress(true);
+    QCOMPARE(textEditObject->focusOnPress(), true);
+    QCOMPARE(activeFocusOnPressSpy.count(), 0);
+
+    QQuickCanvas canvas;
+    canvas.resize(100, 50);
+    textEditObject->setParentItem(canvas.rootItem());
+    canvas.show();
+    canvas.requestActivateWindow();
+    QTest::qWaitForWindowShown(&canvas);
+    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+
+    QCOMPARE(textEditObject->hasFocus(), false);
+    QCOMPARE(textEditObject->hasActiveFocus(), false);
+
+    QPoint centerPoint(canvas.width()/2, canvas.height()/2);
+    Qt::KeyboardModifiers noModifiers = 0;
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textEditObject->hasFocus(), true);
+    QCOMPARE(textEditObject->hasActiveFocus(), true);
+    QCOMPARE(focusSpy.count(), 1);
+    QCOMPARE(activeFocusSpy.count(), 1);
+    QCOMPARE(textEditObject->selectedText(), QString());
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+
+    textEditObject->setFocusOnPress(false);
+    QCOMPARE(textEditObject->focusOnPress(), false);
+    QCOMPARE(activeFocusOnPressSpy.count(), 1);
+
+    textEditObject->setFocus(false);
+    QCOMPARE(textEditObject->hasFocus(), false);
+    QCOMPARE(textEditObject->hasActiveFocus(), false);
+    QCOMPARE(focusSpy.count(), 2);
+    QCOMPARE(activeFocusSpy.count(), 2);
+
+    // Wait for double click timeout to expire before clicking again.
+    QTest::qWait(400);
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textEditObject->hasFocus(), false);
+    QCOMPARE(textEditObject->hasActiveFocus(), false);
+    QCOMPARE(focusSpy.count(), 2);
+    QCOMPARE(activeFocusSpy.count(), 2);
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+
+    textEditObject->setFocusOnPress(true);
+    QCOMPARE(textEditObject->focusOnPress(), true);
+    QCOMPARE(activeFocusOnPressSpy.count(), 2);
+
+    // Test a selection made in the on(Active)FocusChanged handler isn't overwritten.
+    textEditObject->setProperty("selectOnFocus", true);
+
+    QTest::qWait(400);
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textEditObject->hasFocus(), true);
+    QCOMPARE(textEditObject->hasActiveFocus(), true);
+    QCOMPARE(focusSpy.count(), 3);
+    QCOMPARE(activeFocusSpy.count(), 3);
+    QCOMPARE(textEditObject->selectedText(), textEditObject->text());
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
 }
 
 void tst_qquicktextedit::selection()
@@ -1563,32 +1628,41 @@ void tst_qquicktextedit::mouseSelection_data()
     QTest::addColumn<int>("from");
     QTest::addColumn<int>("to");
     QTest::addColumn<QString>("selectedText");
+    QTest::addColumn<bool>("focus");
+    QTest::addColumn<bool>("focusOnPress");
     QTest::addColumn<bool>("doubleClick");
 
     // import installed
-    QTest::newRow("on") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << false;
-    QTest::newRow("off") << testFile("mouseselection_false.qml") << 4 << 9 << QString() << false;
-    QTest::newRow("default") << testFile("mouseselection_default.qml") << 4 << 9 << QString() << false;
-    QTest::newRow("off word selection") << testFile("mouseselection_false_words.qml") << 4 << 9 << QString() << false;
-    QTest::newRow("on word selection (4,9)") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << false;
-    QTest::newRow("on word selection (2,13)") << testFile("mouseselection_true_words.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (2,30)") << testFile("mouseselection_true_words.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (9,13)") << testFile("mouseselection_true_words.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (9,30)") << testFile("mouseselection_true_words.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (13,2)") << testFile("mouseselection_true_words.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (20,2)") << testFile("mouseselection_true_words.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (12,9)") << testFile("mouseselection_true_words.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-    QTest::newRow("on word selection (30,9)") << testFile("mouseselection_true_words.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << false;
-
-    QTest::newRow("off double click (4,9)") << testFile("mouseselection_true.qml") << 4 << 9 << "0123456789" << true;
-    QTest::newRow("off double click (2,13)") << testFile("mouseselection_true.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (2,30)") << testFile("mouseselection_true.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (9,13)") << testFile("mouseselection_true.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (9,30)") << testFile("mouseselection_true.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (13,2)") << testFile("mouseselection_true.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (20,2)") << testFile("mouseselection_true.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (12,9)") << testFile("mouseselection_true.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
-    QTest::newRow("off double click (30,9)") << testFile("mouseselection_true.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true;
+    QTest::newRow("on") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << true << true << false;
+    QTest::newRow("off") << testFile("mouseselection_false.qml") << 4 << 9 << QString() << true << true << false;
+    QTest::newRow("default") << testFile("mouseselection_default.qml") << 4 << 9 << QString() << true << true << false;
+    QTest::newRow("off word selection") << testFile("mouseselection_false_words.qml") << 4 << 9 << QString() << true << true << false;
+    QTest::newRow("on word selection (4,9)") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << true << true << false;
+
+    QTest::newRow("on unfocused") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << false << false << false;
+    QTest::newRow("on word selection (4,9) unfocused") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << false << false << false;
+
+    QTest::newRow("on focus on press") << testFile("mouseselection_true.qml") << 4 << 9 << "45678" << false << true << false;
+    QTest::newRow("on word selection (4,9) focus on press") << testFile("mouseselection_true_words.qml") << 4 << 9 << "0123456789" << false << true << false;
+
+    QTest::newRow("on word selection (2,13)") << testFile("mouseselection_true_words.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (2,30)") << testFile("mouseselection_true_words.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (9,13)") << testFile("mouseselection_true_words.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (9,30)") << testFile("mouseselection_true_words.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (13,2)") << testFile("mouseselection_true_words.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (20,2)") << testFile("mouseselection_true_words.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (12,9)") << testFile("mouseselection_true_words.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+    QTest::newRow("on word selection (30,9)") << testFile("mouseselection_true_words.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << false;
+
+    QTest::newRow("off double click (4,9)") << testFile("mouseselection_true.qml") << 4 << 9 << "0123456789" << true << true << true;
+    QTest::newRow("off double click (2,13)") << testFile("mouseselection_true.qml") << 2 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (2,30)") << testFile("mouseselection_true.qml") << 2 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (9,13)") << testFile("mouseselection_true.qml") << 9 << 13 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (9,30)") << testFile("mouseselection_true.qml") << 9 << 30 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (13,2)") << testFile("mouseselection_true.qml") << 13 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (20,2)") << testFile("mouseselection_true.qml") << 20 << 2 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (12,9)") << testFile("mouseselection_true.qml") << 12 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
+    QTest::newRow("off double click (30,9)") << testFile("mouseselection_true.qml") << 30 << 9 << "0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ" << true << true << true;
 }
 
 void tst_qquicktextedit::mouseSelection()
@@ -1597,6 +1671,8 @@ void tst_qquicktextedit::mouseSelection()
     QFETCH(int, from);
     QFETCH(int, to);
     QFETCH(QString, selectedText);
+    QFETCH(bool, focus);
+    QFETCH(bool, focusOnPress);
     QFETCH(bool, doubleClick);
 
     QQuickView canvas(QUrl::fromLocalFile(qmlfile));
@@ -1610,6 +1686,9 @@ void tst_qquicktextedit::mouseSelection()
     QQuickTextEdit *textEditObject = qobject_cast<QQuickTextEdit *>(canvas.rootObject());
     QVERIFY(textEditObject != 0);
 
+    textEditObject->setFocus(focus);
+    textEditObject->setFocusOnPress(focusOnPress);
+
     // press-and-drag-and-release from x1 to x2
     QPoint p1 = textEditObject->positionToRectangle(from).center().toPoint();
     QPoint p2 = textEditObject->positionToRectangle(to).center().toPoint();
index 35d1eba..e3a9bfc 100644 (file)
@@ -151,6 +151,7 @@ private slots:
     void canPasteEmpty();
     void canPaste();
     void readOnly();
+    void focusOnPress();
 
     void openInputPanel();
     void setHAlignClearCache();
@@ -1190,17 +1191,27 @@ void tst_qquicktextinput::mouseSelectionMode_data()
 {
     QTest::addColumn<QString>("qmlfile");
     QTest::addColumn<bool>("selectWords");
+    QTest::addColumn<bool>("focus");
+    QTest::addColumn<bool>("focusOnPress");
 
     // import installed
-    QTest::newRow("SelectWords") << testFile("mouseselectionmode_words.qml") << true;
-    QTest::newRow("SelectCharacters") << testFile("mouseselectionmode_characters.qml") << false;
-    QTest::newRow("default") << testFile("mouseselectionmode_default.qml") << false;
+    QTest::newRow("SelectWords focused") << testFile("mouseselectionmode_words.qml") << true << true << true;
+    QTest::newRow("SelectCharacters focused") << testFile("mouseselectionmode_characters.qml") << false << true << true;
+    QTest::newRow("default focused") << testFile("mouseselectionmode_default.qml") << false << true << true;
+    QTest::newRow("SelectWords unfocused") << testFile("mouseselectionmode_words.qml") << true << false << false;
+    QTest::newRow("SelectCharacters unfocused") << testFile("mouseselectionmode_characters.qml") << false << false << false;
+    QTest::newRow("default unfocused") << testFile("mouseselectionmode_default.qml") << false << true << false;
+    QTest::newRow("SelectWords focuss on press") << testFile("mouseselectionmode_words.qml") << true << false << true;
+    QTest::newRow("SelectCharacters focus on press") << testFile("mouseselectionmode_characters.qml") << false << false << true;
+    QTest::newRow("default focus on press") << testFile("mouseselectionmode_default.qml") << false << false << true;
 }
 
 void tst_qquicktextinput::mouseSelectionMode()
 {
     QFETCH(QString, qmlfile);
     QFETCH(bool, selectWords);
+    QFETCH(bool, focus);
+    QFETCH(bool, focusOnPress);
 
     QString text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
@@ -1215,6 +1226,9 @@ void tst_qquicktextinput::mouseSelectionMode()
     QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
     QVERIFY(textInputObject != 0);
 
+    textInputObject->setFocus(focus);
+    textInputObject->setFocusOnPress(focusOnPress);
+
     // press-and-drag-and-release from x1 to x2
     int x1 = 10;
     int x2 = 70;
@@ -2775,6 +2789,92 @@ void tst_qquicktextinput::simulateKey(QWindow *view, int key)
 }
 
 
+void tst_qquicktextinput::focusOnPress()
+{
+    QString componentStr =
+            "import QtQuick 2.0\n"
+            "TextInput {\n"
+                "property bool selectOnFocus: false\n"
+                "width: 100; height: 50\n"
+                "activeFocusOnPress: true\n"
+                "text: \"Hello World\"\n"
+                "onFocusChanged: { if (focus && selectOnFocus) selectAll() }"
+            " }";
+    QQmlComponent texteditComponent(&engine);
+    texteditComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput*>(texteditComponent.create());
+    QVERIFY(textInputObject != 0);
+    QCOMPARE(textInputObject->focusOnPress(), true);
+    QCOMPARE(textInputObject->hasFocus(), false);
+
+    QSignalSpy focusSpy(textInputObject, SIGNAL(focusChanged(bool)));
+    QSignalSpy activeFocusSpy(textInputObject, SIGNAL(focusChanged(bool)));
+    QSignalSpy activeFocusOnPressSpy(textInputObject, SIGNAL(activeFocusOnPressChanged(bool)));
+
+    textInputObject->setFocusOnPress(true);
+    QCOMPARE(textInputObject->focusOnPress(), true);
+    QCOMPARE(activeFocusOnPressSpy.count(), 0);
+
+    QQuickCanvas canvas;
+    canvas.resize(100, 50);
+    textInputObject->setParentItem(canvas.rootItem());
+    canvas.show();
+    canvas.requestActivateWindow();
+    QTest::qWaitForWindowShown(&canvas);
+    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+
+    QCOMPARE(textInputObject->hasFocus(), false);
+    QCOMPARE(textInputObject->hasActiveFocus(), false);
+
+    QPoint centerPoint(canvas.width()/2, canvas.height()/2);
+    Qt::KeyboardModifiers noModifiers = 0;
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textInputObject->hasFocus(), true);
+    QCOMPARE(textInputObject->hasActiveFocus(), true);
+    QCOMPARE(focusSpy.count(), 1);
+    QCOMPARE(activeFocusSpy.count(), 1);
+    QCOMPARE(textInputObject->selectedText(), QString());
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+
+    textInputObject->setFocusOnPress(false);
+    QCOMPARE(textInputObject->focusOnPress(), false);
+    QCOMPARE(activeFocusOnPressSpy.count(), 1);
+
+    textInputObject->setFocus(false);
+    QCOMPARE(textInputObject->hasFocus(), false);
+    QCOMPARE(textInputObject->hasActiveFocus(), false);
+    QCOMPARE(focusSpy.count(), 2);
+    QCOMPARE(activeFocusSpy.count(), 2);
+
+    // Wait for double click timeout to expire before clicking again.
+    QTest::qWait(400);
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textInputObject->hasFocus(), false);
+    QCOMPARE(textInputObject->hasActiveFocus(), false);
+    QCOMPARE(focusSpy.count(), 2);
+    QCOMPARE(activeFocusSpy.count(), 2);
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+
+    textInputObject->setFocusOnPress(true);
+    QCOMPARE(textInputObject->focusOnPress(), true);
+    QCOMPARE(activeFocusOnPressSpy.count(), 2);
+
+    // Test a selection made in the on(Active)FocusChanged handler isn't overwritten.
+    textInputObject->setProperty("selectOnFocus", true);
+
+    QTest::qWait(400);
+    QTest::mousePress(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textInputObject->hasFocus(), true);
+    QCOMPARE(textInputObject->hasActiveFocus(), true);
+    QCOMPARE(focusSpy.count(), 3);
+    QCOMPARE(activeFocusSpy.count(), 3);
+    QCOMPARE(textInputObject->selectedText(), textInputObject->text());
+    QTest::mouseRelease(&canvas, Qt::LeftButton, noModifiers, centerPoint);
+}
+
 void tst_qquicktextinput::openInputPanel()
 {
     PlatformInputContext platformInputContext;