Fix QQuickTextInput::moveCursorSelection()
[profile/ivi/qtdeclarative.git] / tests / auto / quick / qquicktextinput / tst_qquicktextinput.cpp
index 9e689a8..249755b 100644 (file)
@@ -1,38 +1,38 @@
 /****************************************************************************
 **
-** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: http://www.qt-project.org/
+** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
 **
 ** This file is part of the test suite of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.  For licensing terms and
+** conditions see http://qt.digia.com/licensing.  For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
 ** GNU Lesser General Public License Usage
-** 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.
+** 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.
 **
-** In addition, as a special exception, Nokia gives you certain additional
-** rights. These rights are described in the Nokia Qt LGPL Exception
+** In addition, as a special exception, Digia gives you certain additional
+** rights.  These rights are described in the Digia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
 ** 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.
-**
-**
-**
-**
+** 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.
 **
 **
 ** $QT_END_LICENSE$
@@ -41,6 +41,7 @@
 #include <qtest.h>
 #include <QtTest/QSignalSpy>
 #include "../../shared/util.h"
+#include "../../shared/testhttpserver.h"
 #include <private/qinputmethod_p.h>
 #include <QtQml/qqmlengine.h>
 #include <QtQml/qqmlexpression.h>
 #endif
 
 #include "qplatformdefs.h"
+#include "../../shared/platformquirks.h"
 #include "../../shared/platforminputcontext.h"
 
+#define SERVER_PORT 14460
+#define SERVER_ADDR "http://localhost:14460"
+
 Q_DECLARE_METATYPE(QQuickTextInput::SelectionMode)
+Q_DECLARE_METATYPE(QQuickTextInput::EchoMode)
+Q_DECLARE_METATYPE(Qt::Key)
+
 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
 
 QString createExpectedFileIfNotFound(const QString& filebasename, const QImage& actual)
@@ -90,6 +98,8 @@ template <typename T> static T evaluate(QObject *scope, const QString &expressio
     return result;
 }
 
+template<typename T, int N> int lengthOf(const T (&)[N]) { return N; }
+
 typedef QPair<int, QChar> Key;
 
 class tst_qquicktextinput : public QQmlDataTest
@@ -117,13 +127,15 @@ private slots:
     void dragMouseSelection();
     void mouseSelectionMode_data();
     void mouseSelectionMode();
+    void mouseSelectionMode_accessors();
+    void selectByMouse();
+    void renderType();
     void tripleClickSelectsAll();
 
-    void horizontalAlignment_data();
-    void horizontalAlignment();
     void horizontalAlignment_RightToLeft();
     void verticalAlignment();
 
+    void clipRect();
     void boundingRect();
 
     void positionAt();
@@ -136,24 +148,28 @@ private slots:
     void passwordCharacter();
     void cursorDelegate_data();
     void cursorDelegate();
+    void remoteCursorDelegate();
     void cursorVisible();
+    void cursorRectangle_data();
     void cursorRectangle();
     void navigation();
     void navigation_RTL();
+#ifndef QT_NO_CLIPBOARD
     void copyAndPaste();
     void copyAndPasteKeySequence();
     void canPasteEmpty();
     void canPaste();
+    void middleClickPaste();
+#endif
     void readOnly();
+    void focusOnPress();
 
     void openInputPanel();
     void setHAlignClearCache();
     void focusOutClearSelection();
 
     void echoMode();
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
     void passwordEchoDelay();
-#endif
     void geometrySignals();
     void contentSize();
 
@@ -181,14 +197,34 @@ private slots:
     void undo_keypressevents_data();
     void undo_keypressevents();
 
+    void backspaceSurrogatePairs();
+
     void QTBUG_19956();
     void QTBUG_19956_data();
     void QTBUG_19956_regexp();
 
+    void implicitSize_data();
+    void implicitSize();
+    void implicitSizeBinding_data();
+    void implicitSizeBinding();
+
     void negativeDimensions();
 
+
+    void setInputMask_data();
+    void setInputMask();
+    void inputMask_data();
+    void inputMask();
+    void clearInputMask();
+    void keypress_inputMask_data();
+    void keypress_inputMask();
+    void hasAcceptableInputMask_data();
+    void hasAcceptableInputMask();
+    void maskCharacter_data();
+    void maskCharacter();
+
 private:
-    void simulateKey(QQuickView *, int key);
+    void simulateKey(QWindow *, int key);
 
     void simulateKeys(QWindow *window, const QList<Key> &keys);
     void simulateKeys(QWindow *window, const QKeySequence &sequence);
@@ -207,8 +243,8 @@ Q_DECLARE_METATYPE(KeyList)
 void tst_qquicktextinput::simulateKeys(QWindow *window, const QList<Key> &keys)
 {
     for (int i = 0; i < keys.count(); ++i) {
-        const int key = keys.at(i).first;
-        const int modifiers = key & Qt::KeyboardModifierMask;
+        const int key = keys.at(i).first & ~Qt::KeyboardModifierMask;
+        const int modifiers = keys.at(i).first & Qt::KeyboardModifierMask;
         const QString text = !keys.at(i).second.isNull() ? QString(keys.at(i).second) : QString();
 
         QKeyEvent press(QEvent::KeyPress, Qt::Key(key), Qt::KeyboardModifiers(modifiers), text);
@@ -437,6 +473,57 @@ void tst_qquicktextinput::font()
 
 void tst_qquicktextinput::color()
 {
+    //test initial color
+    {
+        QString componentStr = "import QtQuick 2.0\nTextInput { text: \"Hello World\" }";
+        QQmlComponent texteditComponent(&engine);
+        texteditComponent.setData(componentStr.toLatin1(), QUrl());
+        QScopedPointer<QObject> object(texteditComponent.create());
+        QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput*>(object.data());
+
+        QVERIFY(textInputObject);
+        QCOMPARE(textInputObject->color(), QColor("black"));
+        QCOMPARE(textInputObject->selectionColor(), QColor::fromRgba(0xFF000080));
+        QCOMPARE(textInputObject->selectedTextColor(), QColor("white"));
+
+        QSignalSpy colorSpy(textInputObject, SIGNAL(colorChanged()));
+        QSignalSpy selectionColorSpy(textInputObject, SIGNAL(selectionColorChanged()));
+        QSignalSpy selectedTextColorSpy(textInputObject, SIGNAL(selectedTextColorChanged()));
+
+        textInputObject->setColor(QColor("white"));
+        QCOMPARE(textInputObject->color(), QColor("white"));
+        QCOMPARE(colorSpy.count(), 1);
+
+        textInputObject->setSelectionColor(QColor("black"));
+        QCOMPARE(textInputObject->selectionColor(), QColor("black"));
+        QCOMPARE(selectionColorSpy.count(), 1);
+
+        textInputObject->setSelectedTextColor(QColor("blue"));
+        QCOMPARE(textInputObject->selectedTextColor(), QColor("blue"));
+        QCOMPARE(selectedTextColorSpy.count(), 1);
+
+        textInputObject->setColor(QColor("white"));
+        QCOMPARE(colorSpy.count(), 1);
+
+        textInputObject->setSelectionColor(QColor("black"));
+        QCOMPARE(selectionColorSpy.count(), 1);
+
+        textInputObject->setSelectedTextColor(QColor("blue"));
+        QCOMPARE(selectedTextColorSpy.count(), 1);
+
+        textInputObject->setColor(QColor("black"));
+        QCOMPARE(textInputObject->color(), QColor("black"));
+        QCOMPARE(colorSpy.count(), 2);
+
+        textInputObject->setSelectionColor(QColor("blue"));
+        QCOMPARE(textInputObject->selectionColor(), QColor("blue"));
+        QCOMPARE(selectionColorSpy.count(), 2);
+
+        textInputObject->setSelectedTextColor(QColor("white"));
+        QCOMPARE(textInputObject->selectedTextColor(), QColor("white"));
+        QCOMPARE(selectedTextColorSpy.count(), 2);
+    }
+
     //test color
     for (int i = 0; i < colorStrings.size(); i++)
     {
@@ -526,6 +613,29 @@ void tst_qquicktextinput::wrap()
 
         delete textObject;
     }
+
+    {
+        QQmlComponent component(&engine);
+        component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
+        QScopedPointer<QObject> object(component.create());
+        QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
+        QVERIFY(input);
+
+        QSignalSpy spy(input, SIGNAL(wrapModeChanged()));
+
+        QCOMPARE(input->wrapMode(), QQuickTextInput::NoWrap);
+
+        input->setWrapMode(QQuickTextInput::Wrap);
+        QCOMPARE(input->wrapMode(), QQuickTextInput::Wrap);
+        QCOMPARE(spy.count(), 1);
+
+        input->setWrapMode(QQuickTextInput::Wrap);
+        QCOMPARE(spy.count(), 1);
+
+        input->setWrapMode(QQuickTextInput::NoWrap);
+        QCOMPARE(input->wrapMode(), QQuickTextInput::NoWrap);
+        QCOMPARE(spy.count(), 2);
+    }
 }
 
 void tst_qquicktextinput::selection()
@@ -629,14 +739,12 @@ void tst_qquicktextinput::selection()
 
 void tst_qquicktextinput::persistentSelection()
 {
-    QQuickView canvas(testFileUrl("persistentSelection.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
-    canvas.requestActivateWindow();
-
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QQuickView window(testFileUrl("persistentSelection.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(window.rootObject());
     QVERIFY(input);
     QVERIFY(input->hasActiveFocus());
 
@@ -803,10 +911,12 @@ void tst_qquicktextinput::moveCursorSelection_data()
     QTest::newRow("jum<()>ped|words")
             << standard[0] << 23 << 23 << QQuickTextInput::SelectWords << 23 << 23 << true;
 
-    QTest::newRow("Hello<(,)> |words")
-            << standard[2] << 5 << 6 << QQuickTextInput::SelectWords << 5 << 6 << true;
-    QTest::newRow("Hello<(, )>world|words,ltr")
-            << standard[2] << 5 << 7 << QQuickTextInput::SelectWords << 5 << 7 << false;
+    QTest::newRow("<Hello(,)> |words,ltr")
+            << standard[2] << 5 << 6 << QQuickTextInput::SelectWords << 0 << 6 << false;
+    QTest::newRow("Hello<(,)> |words,rtl")
+            << standard[2] << 6 << 5 << QQuickTextInput::SelectWords << 5 << 6 << false;
+    QTest::newRow("<Hello(, )>world|words,ltr")
+            << standard[2] << 5 << 7 << QQuickTextInput::SelectWords << 0 << 7 << false;
     QTest::newRow("Hello<(, )world>|words,rtl")
             << standard[2] << 7 << 5 << QQuickTextInput::SelectWords << 5 << 12 << false;
     QTest::newRow("<Hel(lo, )>world|words,ltr")
@@ -819,35 +929,35 @@ void tst_qquicktextinput::moveCursorSelection_data()
             << standard[2] << 5 << 5 << QQuickTextInput::SelectWords << 5 << 5 << true;
     QTest::newRow("Hello,<()>|words")
             << standard[2] << 6 << 6 << QQuickTextInput::SelectWords << 6 << 6 << true;
-    QTest::newRow("Hello<,( )>world|words,ltr")
-            << standard[2] << 6 << 7 << QQuickTextInput::SelectWords << 5 << 7 << false;
+    QTest::newRow("Hello,<( )>world|words,ltr")
+            << standard[2] << 6 << 7 << QQuickTextInput::SelectWords << 6 << 7 << false;
     QTest::newRow("Hello,<( )world>|words,rtl")
             << standard[2] << 7 << 6 << QQuickTextInput::SelectWords << 6 << 12 << false;
-    QTest::newRow("Hello<,( world)>|words,ltr")
-            << standard[2] << 6 << 12 << QQuickTextInput::SelectWords << 5 << 12 << false;
-    QTest::newRow("Hello,<( world)>|words,rtl")
-            << standard[2] << 12 << 6 << QQuickTextInput::SelectWords << 6 << 12 << false;
-    QTest::newRow("Hello<,( world!)>|words,ltr")
-            << standard[2] << 6 << 13 << QQuickTextInput::SelectWords << 5 << 13 << false;
-    QTest::newRow("Hello,<( world!)>|words,rtl")
-            << standard[2] << 13 << 6 << QQuickTextInput::SelectWords << 6 << 13 << false;
-    QTest::newRow("Hello<(, world!)>|words")
-            << standard[2] << 5 << 13 << QQuickTextInput::SelectWords << 5 << 13 << true;
-    // Fails due to an issue with QTextBoundaryFinder and punctuation at the end of strings.
-    // QTBUG-11365
-    // QTest::newRow("world<(!)>|words")
-    //         << standard[2] << 12 << 13 << QQuickTextInput::SelectWords << 12 << 13 << true;
+    QTest::newRow("Hello,<( world)>|words")
+            << standard[2] << 6 << 12 << QQuickTextInput::SelectWords << 6 << 12 << true;
+    QTest::newRow("Hello,<( world!)>|words")
+            << standard[2] << 6 << 13 << QQuickTextInput::SelectWords << 6 << 13 << true;
+    QTest::newRow("<Hello(, world!)>|words,ltr")
+            << standard[2] << 5 << 13 << QQuickTextInput::SelectWords << 0 << 13 << false;
+    QTest::newRow("Hello<(, world!)>|words,rtl")
+            << standard[2] << 13 << 5 << QQuickTextInput::SelectWords << 5 << 13 << false;
+    QTest::newRow("<world(!)>|words,ltr")
+            << standard[2] << 12 << 13 << QQuickTextInput::SelectWords << 7 << 13 << false;
+    QTest::newRow("world<(!)>|words,rtl")
+            << standard[2] << 13 << 12 << QQuickTextInput::SelectWords << 12 << 13 << false;
     QTest::newRow("world!<()>)|words")
             << standard[2] << 13 << 13 << QQuickTextInput::SelectWords << 13 << 13 << true;
     QTest::newRow("world<()>!)|words")
             << standard[2] << 12 << 12 << QQuickTextInput::SelectWords << 12 << 12 << true;
 
-    QTest::newRow("<(,)>olleH |words")
-            << standard[3] << 7 << 8 << QQuickTextInput::SelectWords << 7 << 8 << true;
+    QTest::newRow("<(,)>olleH |words,ltr")
+            << standard[3] << 7 << 8 << QQuickTextInput::SelectWords << 7 << 8 << false;
+    QTest::newRow("<(,)olleH> |words,rtl")
+            << standard[3] << 8 << 7 << QQuickTextInput::SelectWords << 7 << 13 << false;
     QTest::newRow("<dlrow( ,)>olleH|words,ltr")
             << standard[3] << 6 << 8 << QQuickTextInput::SelectWords << 1 << 8 << false;
-    QTest::newRow("dlrow<( ,)>olleH|words,rtl")
-            << standard[3] << 8 << 6 << QQuickTextInput::SelectWords << 6 << 8 << false;
+    QTest::newRow("dlrow<( ,)olleH>|words,rtl")
+            << standard[3] << 8 << 6 << QQuickTextInput::SelectWords << 6 << 13 << false;
     QTest::newRow("<dlrow( ,ol)leH>|words,ltr")
             << standard[3] << 6 << 10 << QQuickTextInput::SelectWords << 1 << 13 << false;
     QTest::newRow("dlrow<( ,ol)leH>|words,rtl")
@@ -860,20 +970,20 @@ void tst_qquicktextinput::moveCursorSelection_data()
             << standard[3] << 7 << 7 << QQuickTextInput::SelectWords << 7 << 7 << true;
     QTest::newRow("<dlrow( )>,olleH|words,ltr")
             << standard[3] << 6 << 7 << QQuickTextInput::SelectWords << 1 << 7 << false;
-    QTest::newRow("dlrow<( ),>olleH|words,rtl")
-            << standard[3] << 7 << 6 << QQuickTextInput::SelectWords << 6 << 8 << false;
-    QTest::newRow("<(dlrow )>,olleH|words,ltr")
-            << standard[3] << 1 << 7 << QQuickTextInput::SelectWords << 1 << 7 << false;
-    QTest::newRow("<(dlrow ),>olleH|words,rtl")
-            << standard[3] << 7 << 1 << QQuickTextInput::SelectWords << 1 << 8 << false;
-    QTest::newRow("<(!dlrow )>,olleH|words,ltr")
-            << standard[3] << 0 << 7 << QQuickTextInput::SelectWords << 0 << 7 << false;
-    QTest::newRow("<(!dlrow ),>olleH|words,rtl")
-            << standard[3] << 7 << 0 << QQuickTextInput::SelectWords << 0 << 8 << false;
-    QTest::newRow("(!dlrow ,)olleH|words")
-            << standard[3] << 0 << 8 << QQuickTextInput::SelectWords << 0 << 8 << true;
-    QTest::newRow("<(!)>dlrow|words")
-            << standard[3] << 0 << 1 << QQuickTextInput::SelectWords << 0 << 1 << true;
+    QTest::newRow("dlrow<( )>,olleH|words,rtl")
+            << standard[3] << 7 << 6 << QQuickTextInput::SelectWords << 6 << 7 << false;
+    QTest::newRow("<(dlrow )>,olleH|words")
+            << standard[3] << 1 << 7 << QQuickTextInput::SelectWords << 1 << 7 << true;
+    QTest::newRow("<(!dlrow )>,olleH|words")
+            << standard[3] << 0 << 7 << QQuickTextInput::SelectWords << 0 << 7 << true;
+    QTest::newRow("<(!dlrow ,)>olleH|words,ltr")
+            << standard[3] << 0 << 8 << QQuickTextInput::SelectWords << 0 << 8 << false;
+    QTest::newRow("<(!dlrow ,)olleH>|words,rtl")
+            << standard[3] << 8 << 0 << QQuickTextInput::SelectWords << 0 << 13 << false;
+    QTest::newRow("<(!)>dlrow|words,ltr")
+            << standard[3] << 0 << 1 << QQuickTextInput::SelectWords << 0 << 1 << false;
+    QTest::newRow("<(!)dlrow|words,rtl")
+            << standard[3] << 1 << 0 << QQuickTextInput::SelectWords << 0 << 6 << false;
     QTest::newRow("<()>!dlrow|words")
             << standard[3] << 0 << 0 << QQuickTextInput::SelectWords << 0 << 0 << true;
     QTest::newRow("!<()>dlrow|words")
@@ -882,16 +992,15 @@ void tst_qquicktextinput::moveCursorSelection_data()
     QTest::newRow(" <s(pac)ey>   text |words")
             << standard[4] << 1 << 4 << QQuickTextInput::SelectWords << 1 << 7 << true;
     QTest::newRow(" spacey   <t(ex)t> |words")
-            << standard[4] << 11 << 13 << QQuickTextInput::SelectWords << 10 << 14 << false; // Should be reversible. QTBUG-11365
+            << standard[4] << 11 << 13 << QQuickTextInput::SelectWords << 10 << 14 << true;
     QTest::newRow("<( )>spacey   text |words|ltr")
             << standard[4] << 0 << 1 << QQuickTextInput::SelectWords << 0 << 1 << false;
     QTest::newRow("<( )spacey>   text |words|rtl")
             << standard[4] << 1 << 0 << QQuickTextInput::SelectWords << 0 << 7 << false;
     QTest::newRow("spacey   <text( )>|words|ltr")
             << standard[4] << 14 << 15 << QQuickTextInput::SelectWords << 10 << 15 << false;
-//    QTBUG-11365
-//    QTest::newRow("spacey   text<( )>|words|rtl")
-//            << standard[4] << 15 << 14 << QQuickTextInput::SelectWords << 14 << 15 << false;
+    QTest::newRow("spacey   text<( )>|words|rtl")
+            << standard[4] << 15 << 14 << QQuickTextInput::SelectWords << 14 << 15 << false;
     QTest::newRow("<()> spacey   text |words")
             << standard[4] << 0 << 0 << QQuickTextInput::SelectWords << 0 << 0 << false;
     QTest::newRow(" spacey   text <()>|words")
@@ -1088,23 +1197,21 @@ void tst_qquicktextinput::moveCursorSelectionSequence_data()
             << 15 << 12 << 15
             << 10 << 15
             << 15 << 15;
-//    QTBUG-11365
-//    QTest::newRow(" spacey   <te(xt{^ )>}|rtl")
-//            << standard[4]
-//            << 15 << 12 << 14
-//            << 10 << 15
-//            << 14 << 15;
+    QTest::newRow(" spacey   <te(xt{^ )>}|rtl")
+            << standard[4]
+            << 15 << 12 << 14
+            << 10 << 15
+            << 14 << 15;
     QTest::newRow(" spacey   {<te(x^t} )>|ltr")
             << standard[4]
             << 12 << 15 << 13
             << 10 << 15
             << 10 << 14;
-//    QTBUG-11365
-//    QTest::newRow(" spacey   {<te(xt^} )>|ltr")
-//            << standard[4]
-//            << 12 << 15 << 14
-//            << 10 << 15
-//            << 10 << 14;
+    QTest::newRow(" spacey   {<te(xt^} )>|ltr")
+            << standard[4]
+            << 12 << 15 << 14
+            << 10 << 15
+            << 10 << 14;
 }
 
 void tst_qquicktextinput::moveCursorSelectionSequence()
@@ -1143,25 +1250,23 @@ void tst_qquicktextinput::dragMouseSelection()
 {
     QString qmlfile = testFile("mouseselection_true.qml");
 
-    QQuickView canvas(QUrl::fromLocalFile(qmlfile));
-
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
+    QQuickView window(QUrl::fromLocalFile(qmlfile));
 
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(window.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, Qt::LeftButton, 0, QPoint(x1,y));
-    QTest::mouseMove(&canvas, QPoint(x2, y));
-    QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(x2,y));
+    QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
+    QTest::mouseMove(&window, QPoint(x2, y));
+    QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
     QTest::qWait(100);
     QString str1;
     QVERIFY((str1 = textInputObject->selectedText()).length() > 3);
@@ -1170,9 +1275,9 @@ void tst_qquicktextinput::dragMouseSelection()
     // press and drag the current selection.
     x1 = 40;
     x2 = 100;
-    QTest::mousePress(&canvas, Qt::LeftButton, 0, QPoint(x1,y));
-    QTest::mouseMove(&canvas, QPoint(x2, y));
-    QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(x2,y));
+    QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
+    QTest::mouseMove(&window, QPoint(x2, y));
+    QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
     QTest::qWait(300);
     QString str2 = textInputObject->selectedText();
     QVERIFY(str2.length() > 3);
@@ -1184,38 +1289,50 @@ 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";
 
-    QQuickView canvas(QUrl::fromLocalFile(qmlfile));
+    QQuickView window(QUrl::fromLocalFile(qmlfile));
 
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(window.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;
     int y = textInputObject->height()/2;
-    QTest::mousePress(&canvas, Qt::LeftButton, 0, QPoint(x1,y));
-    QTest::mouseMove(&canvas, QPoint(x2,y)); // doesn't work
-    QTest::mouseRelease(&canvas, Qt::LeftButton, 0, QPoint(x2,y));
+    QTest::mousePress(&window, Qt::LeftButton, 0, QPoint(x1,y));
+    QTest::mouseMove(&window, QPoint(x2,y)); // doesn't work
+    QTest::mouseRelease(&window, Qt::LeftButton, 0, QPoint(x2,y));
     QTest::qWait(300);
     if (selectWords) {
         QTRY_COMPARE(textInputObject->selectedText(), text);
@@ -1225,39 +1342,78 @@ void tst_qquicktextinput::mouseSelectionMode()
     }
 }
 
-void tst_qquicktextinput::horizontalAlignment_data()
+void tst_qquicktextinput::mouseSelectionMode_accessors()
 {
-    QTest::addColumn<int>("hAlign");
-    QTest::addColumn<QString>("expectfile");
+    QQmlComponent component(&engine);
+    component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
+    QScopedPointer<QObject> object(component.create());
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
+    QVERIFY(input);
+
+    QSignalSpy spy(input, SIGNAL(mouseSelectionModeChanged(SelectionMode)));
+
+    QCOMPARE(input->mouseSelectionMode(), QQuickTextInput::SelectCharacters);
+
+    input->setMouseSelectionMode(QQuickTextInput::SelectWords);
+    QCOMPARE(input->mouseSelectionMode(), QQuickTextInput::SelectWords);
+    QCOMPARE(spy.count(), 1);
 
-    QTest::newRow("L") << int(Qt::AlignLeft) << "halign_left";
-    QTest::newRow("R") << int(Qt::AlignRight) << "halign_right";
-    QTest::newRow("C") << int(Qt::AlignHCenter) << "halign_center";
+    input->setMouseSelectionMode(QQuickTextInput::SelectWords);
+    QCOMPARE(spy.count(), 1);
+
+    input->setMouseSelectionMode(QQuickTextInput::SelectCharacters);
+    QCOMPARE(input->mouseSelectionMode(), QQuickTextInput::SelectCharacters);
+    QCOMPARE(spy.count(), 2);
 }
 
-void tst_qquicktextinput::horizontalAlignment()
+void tst_qquicktextinput::selectByMouse()
 {
-    QSKIP("Image comparison of text is almost guaranteed to fail during development");
+    QQmlComponent component(&engine);
+    component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
+    QScopedPointer<QObject> object(component.create());
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
+    QVERIFY(input);
 
-    QFETCH(int, hAlign);
-    QFETCH(QString, expectfile);
+    QSignalSpy spy(input, SIGNAL(selectByMouseChanged(bool)));
 
-    QQuickView canvas(testFileUrl("horizontalAlignment.qml"));
+    QCOMPARE(input->selectByMouse(), false);
 
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
-    QObject *ob = canvas.rootObject();
-    QVERIFY(ob != 0);
-    ob->setProperty("horizontalAlignment",hAlign);
-    QImage actual = canvas.grabFrameBuffer();
+    input->setSelectByMouse(true);
+    QCOMPARE(input->selectByMouse(), true);
+    QCOMPARE(spy.count(), 1);
+    QCOMPARE(spy.at(0).at(0).toBool(), true);
+
+    input->setSelectByMouse(true);
+    QCOMPARE(spy.count(), 1);
+
+    input->setSelectByMouse(false);
+    QCOMPARE(input->selectByMouse(), false);
+    QCOMPARE(spy.count(), 2);
+    QCOMPARE(spy.at(1).at(0).toBool(), false);
+}
+
+void tst_qquicktextinput::renderType()
+{
+    QQmlComponent component(&engine);
+    component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
+    QScopedPointer<QObject> object(component.create());
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
+    QVERIFY(input);
+
+    QSignalSpy spy(input, SIGNAL(renderTypeChanged()));
 
-    expectfile = createExpectedFileIfNotFound(expectfile, actual);
+    QCOMPARE(input->renderType(), QQuickTextInput::QtRendering);
 
-    QImage expect(expectfile);
+    input->setRenderType(QQuickTextInput::NativeRendering);
+    QCOMPARE(input->renderType(), QQuickTextInput::NativeRendering);
+    QCOMPARE(spy.count(), 1);
+
+    input->setRenderType(QQuickTextInput::NativeRendering);
+    QCOMPARE(spy.count(), 1);
 
-    QCOMPARE(actual,expect);
+    input->setRenderType(QQuickTextInput::QtRendering);
+    QCOMPARE(input->renderType(), QQuickTextInput::QtRendering);
+    QCOMPARE(spy.count(), 2);
 }
 
 void tst_qquicktextinput::horizontalAlignment_RightToLeft()
@@ -1266,50 +1422,48 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft()
     QInputMethodPrivate *inputMethodPrivate = QInputMethodPrivate::get(qApp->inputMethod());
     inputMethodPrivate->testContext = &platformInputContext;
 
-    QQuickView canvas(testFileUrl("horizontalAlignment_RightToLeft.qml"));
-    QQuickTextInput *textInput = canvas.rootObject()->findChild<QQuickTextInput*>("text");
+    QQuickView window(testFileUrl("horizontalAlignment_RightToLeft.qml"));
+    QQuickTextInput *textInput = window.rootObject()->findChild<QQuickTextInput*>("text");
     QVERIFY(textInput != 0);
-    canvas.show();
+    window.show();
 
     const QString rtlText = textInput->text();
 
-    QQuickTextInputPrivate *textInputPrivate = QQuickTextInputPrivate::get(textInput);
-    QVERIFY(textInputPrivate != 0);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // implicit alignment should follow the reading direction of RTL text
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // explicitly left aligned
     textInput->setHAlign(QQuickTextInput::AlignLeft);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignLeft);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
-    QCOMPARE(textInputPrivate->boundingRect.left() - textInputPrivate->hscroll, qreal(0));
+    QCOMPARE(textInput->boundingRect().left(), qreal(0));
 
     // explicitly right aligned
     textInput->setHAlign(QQuickTextInput::AlignRight);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // explicitly center aligned
     textInput->setHAlign(QQuickTextInput::AlignHCenter);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignHCenter);
-    QVERIFY(textInputPrivate->boundingRect.left() - textInputPrivate->hscroll > 0);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll < textInput->width());
+    QVERIFY(textInput->boundingRect().left() > 0);
+    QVERIFY(textInput->boundingRect().right() < textInput->width());
 
     // reseted alignment should go back to following the text reading direction
     textInput->resetHAlign();
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // mirror the text item
     QQuickItemPrivate::get(textInput)->setLayoutMirror(true);
@@ -1317,21 +1471,21 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft()
     // mirrored implicit alignment should continue to follow the reading direction of the text
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
     QCOMPARE(textInput->effectiveHAlign(), textInput->hAlign());
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // explicitly right aligned behaves as left aligned
     textInput->setHAlign(QQuickTextInput::AlignRight);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
     QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignLeft);
-    QCOMPARE(textInputPrivate->boundingRect.left() - textInputPrivate->hscroll, qreal(0));
+    QCOMPARE(textInput->boundingRect().left(), qreal(0));
 
     // mirrored explicitly left aligned behaves as right aligned
     textInput->setHAlign(QQuickTextInput::AlignLeft);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignLeft);
     QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // disable mirroring
     QQuickItemPrivate::get(textInput)->setLayoutMirror(false);
@@ -1341,22 +1495,22 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft()
     // English text should be implicitly left aligned
     textInput->setText("Hello world!");
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignLeft);
-    QCOMPARE(textInputPrivate->boundingRect.left() - textInputPrivate->hscroll, qreal(0));
+    QCOMPARE(textInput->boundingRect().left(), qreal(0));
 
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(textInput->hasActiveFocus());
 
-    // If there is no commited text, the preedit text should determine the alignment.
+    // If there is no committed text, the preedit text should determine the alignment.
     textInput->setText(QString());
-    { QInputMethodEvent ev(rtlText, QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); }
+    { QInputMethodEvent ev(rtlText, QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(textInput, &ev); }
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    { QInputMethodEvent ev("Hello world!", QList<QInputMethodEvent::Attribute>()); QGuiApplication::sendEvent(qGuiApp->focusObject(), &ev); }
+    { QInputMethodEvent ev("Hello world!", QList<QInputMethodEvent::Attribute>()); 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()
@@ -1364,25 +1518,25 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft()
     platformInputContext.setInputDirection(Qt::LeftToRight);
     QVERIFY(qApp->inputMethod()->inputDirection() == Qt::LeftToRight);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignLeft);
-    QCOMPARE(textInputPrivate->boundingRect.left() - textInputPrivate->hscroll, qreal(0));
+    QCOMPARE(textInput->boundingRect().left(), qreal(0));
 
     QSignalSpy cursorRectangleSpy(textInput, SIGNAL(cursorRectangleChanged()));
     platformInputContext.setInputDirection(Qt::RightToLeft);
     QVERIFY(qApp->inputMethod()->inputDirection() == Qt::RightToLeft);
     QCOMPARE(cursorRectangleSpy.count(), 1);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // set input direction while having content
     platformInputContext.setInputDirection(Qt::LeftToRight);
     textInput->setText("a");
     platformInputContext.setInputDirection(Qt::RightToLeft);
-    QTest::keyClick(&canvas, Qt::Key_Backspace);
+    QTest::keyClick(&window, Qt::Key_Backspace);
     QVERIFY(textInput->text().isEmpty());
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     // input direction changed while not having focus
     platformInputContext.setInputDirection(Qt::LeftToRight);
@@ -1390,49 +1544,60 @@ void tst_qquicktextinput::horizontalAlignment_RightToLeft()
     platformInputContext.setInputDirection(Qt::RightToLeft);
     textInput->setFocus(true);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
 
     textInput->setHAlign(QQuickTextInput::AlignRight);
     QCOMPARE(textInput->hAlign(), QQuickTextInput::AlignRight);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll >= textInput->width() - 1);
-    QVERIFY(textInputPrivate->boundingRect.right() - textInputPrivate->hscroll <= textInput->width() + 1);
+    QVERIFY(textInput->boundingRect().right() >= textInput->width() - 1);
+    QVERIFY(textInput->boundingRect().right() <= textInput->width() + 1);
+
+    // neutral text should fall back to input direction
+    textInput->setFocus(true);
+    textInput->resetHAlign();
+    textInput->setText(" ()((=<>");
+    platformInputContext.setInputDirection(Qt::LeftToRight);
+    QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignLeft);
+    platformInputContext.setInputDirection(Qt::RightToLeft);
+    QCOMPARE(textInput->effectiveHAlign(), QQuickTextInput::AlignRight);
+
+    // changing width keeps right aligned cursor on proper position
+    textInput->setText("");
+    textInput->setWidth(500);
+    QVERIFY(textInput->positionToRectangle(0).x() > textInput->width() / 2);
 }
 
 void tst_qquicktextinput::verticalAlignment()
 {
-    QQuickView canvas(testFileUrl("horizontalAlignment.qml"));
-    QQuickTextInput *textInput = canvas.rootObject()->findChild<QQuickTextInput*>("text");
+    QQuickView window(testFileUrl("horizontalAlignment.qml"));
+    QQuickTextInput *textInput = window.rootObject()->findChild<QQuickTextInput*>("text");
     QVERIFY(textInput != 0);
-    canvas.show();
-
-    QQuickTextInputPrivate *textInputPrivate = QQuickTextInputPrivate::get(textInput);
-    QVERIFY(textInputPrivate != 0);
+    window.show();
 
     QCOMPARE(textInput->vAlign(), QQuickTextInput::AlignTop);
-    QVERIFY(textInputPrivate->boundingRect.bottom() - textInputPrivate->vscroll < canvas.height() / 2);
-    QVERIFY(textInput->cursorRectangle().bottom() < canvas.height() / 2);
-    QVERIFY(textInput->positionToRectangle(0).bottom() < canvas.height() / 2);
+    QVERIFY(textInput->boundingRect().bottom() < window.height() / 2);
+    QVERIFY(textInput->cursorRectangle().bottom() < window.height() / 2);
+    QVERIFY(textInput->positionToRectangle(0).bottom() < window.height() / 2);
 
     // bottom aligned
     textInput->setVAlign(QQuickTextInput::AlignBottom);
     QCOMPARE(textInput->vAlign(), QQuickTextInput::AlignBottom);
-    QVERIFY(textInputPrivate->boundingRect.top() - textInputPrivate->vscroll > canvas.height() / 2);
-    QVERIFY(textInput->cursorRectangle().top() > canvas.height() / 2);
-    QVERIFY(textInput->positionToRectangle(0).top() > canvas.height() / 2);
+    QVERIFY(textInput->boundingRect().top() > window.height() / 2);
+    QVERIFY(textInput->cursorRectangle().top() > window.height() / 2);
+    QVERIFY(textInput->positionToRectangle(0).top() > window.height() / 2);
 
     // explicitly center aligned
     textInput->setVAlign(QQuickTextInput::AlignVCenter);
     QCOMPARE(textInput->vAlign(), QQuickTextInput::AlignVCenter);
-    QVERIFY(textInputPrivate->boundingRect.top() - textInputPrivate->vscroll < canvas.height() / 2);
-    QVERIFY(textInputPrivate->boundingRect.bottom() - textInputPrivate->vscroll > canvas.height() / 2);
-    QVERIFY(textInput->cursorRectangle().top() < canvas.height() / 2);
-    QVERIFY(textInput->cursorRectangle().bottom() > canvas.height() / 2);
-    QVERIFY(textInput->positionToRectangle(0).top() < canvas.height() / 2);
-    QVERIFY(textInput->positionToRectangle(0).bottom() > canvas.height() / 2);
+    QVERIFY(textInput->boundingRect().top() < window.height() / 2);
+    QVERIFY(textInput->boundingRect().bottom() > window.height() / 2);
+    QVERIFY(textInput->cursorRectangle().top() < window.height() / 2);
+    QVERIFY(textInput->cursorRectangle().bottom() > window.height() / 2);
+    QVERIFY(textInput->positionToRectangle(0).top() < window.height() / 2);
+    QVERIFY(textInput->positionToRectangle(0).bottom() > window.height() / 2);
 }
 
-void tst_qquicktextinput::boundingRect()
+void tst_qquicktextinput::clipRect()
 {
     QQmlComponent component(&engine);
     component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
@@ -1440,41 +1605,165 @@ void tst_qquicktextinput::boundingRect()
     QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
     QVERIFY(input);
 
-    QCOMPARE(input->width() + input->cursorRectangle().width(), input->boundingRect().width());
-    QCOMPARE(input->height(), input->boundingRect().height());
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + input->cursorRectangle().width());
+    QCOMPARE(input->clipRect().height(), input->height());
 
     input->setText("Hello World");
-    QCOMPARE(input->width() + input->cursorRectangle().width(), input->boundingRect().width());
-    QCOMPARE(input->height(), input->boundingRect().height());
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + input->cursorRectangle().width());
+    QCOMPARE(input->clipRect().height(), input->height());
 
-    // bounding rect shouldn't exceed the size of the item, expect for the cursor width;
+    // clip rect shouldn't exceed the size of the item, expect for the cursor width;
     input->setWidth(input->width() / 2);
-    QCOMPARE(input->width() + input->cursorRectangle().width(), input->boundingRect().width());
-    QCOMPARE(input->height(), input->boundingRect().height());
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + input->cursorRectangle().width());
+    QCOMPARE(input->clipRect().height(), input->height());
 
     input->setHeight(input->height() * 2);
-    QCOMPARE(input->width() + input->cursorRectangle().width(), input->boundingRect().width());
-    QCOMPARE(input->height(), input->boundingRect().height());
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + input->cursorRectangle().width());
+    QCOMPARE(input->clipRect().height(), input->height());
 
     QQmlComponent cursorComponent(&engine);
     cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
 
     input->setCursorDelegate(&cursorComponent);
+    input->setCursorVisible(true);
 
     // If a cursor delegate is used it's size should determine the excess width.
-    QCOMPARE(input->width() + 8, input->boundingRect().width());
-    QCOMPARE(input->height(), input->boundingRect().height());
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + 8);
+    QCOMPARE(input->clipRect().height(), input->height());
+
+    // Alignment, auto scroll, wrapping all don't affect the clip rect.
+    input->setAutoScroll(false);
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + 8);
+    QCOMPARE(input->clipRect().height(), input->height());
+
+    input->setHAlign(QQuickTextInput::AlignRight);
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + 8);
+    QCOMPARE(input->clipRect().height(), input->height());
+
+    input->setWrapMode(QQuickTextInput::Wrap);
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + 8);
+    QCOMPARE(input->clipRect().height(), input->height());
+
+    input->setVAlign(QQuickTextInput::AlignBottom);
+    QCOMPARE(input->clipRect().x(), qreal(0));
+    QCOMPARE(input->clipRect().y(), qreal(0));
+    QCOMPARE(input->clipRect().width(), input->width() + 8);
+    QCOMPARE(input->clipRect().height(), input->height());
+}
+
+void tst_qquicktextinput::boundingRect()
+{
+    QQmlComponent component(&engine);
+    component.setData("import QtQuick 2.0\n TextInput {}", QUrl());
+    QScopedPointer<QObject> object(component.create());
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(object.data());
+    QVERIFY(input);
+
+    QTextLayout layout;
+    layout.setFont(input->font());
+
+    if (!qmlDisableDistanceField()) {
+        QTextOption option;
+        option.setUseDesignMetrics(true);
+        layout.setTextOption(option);
+    }
+    layout.beginLayout();
+    QTextLine line = layout.createLine();
+    layout.endLayout();
+
+    QCOMPARE(input->boundingRect().x(), qreal(0));
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), input->cursorRectangle().width());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    input->setText("Hello World");
+
+    layout.setText(input->text());
+    layout.beginLayout();
+    line = layout.createLine();
+    layout.endLayout();
+
+    QCOMPARE(input->boundingRect().x(), qreal(0));
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth() + input->cursorRectangle().width());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    // the size of the bounding rect shouldn't be bounded by the size of item.
+    input->setWidth(input->width() / 2);
+    QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth() + input->cursorRectangle().width());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    input->setHeight(input->height() * 2);
+    QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth() + input->cursorRectangle().width());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    QQmlComponent cursorComponent(&engine);
+    cursorComponent.setData("import QtQuick 2.0\nRectangle { height: 20; width: 8 }", QUrl());
+
+    input->setCursorDelegate(&cursorComponent);
+    input->setCursorVisible(true);
+
+    // Don't include the size of a cursor delegate as it has its own bounding rect.
+    QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    // Bounding rect left aligned when auto scroll is disabled;
+    input->setAutoScroll(false);
+    QCOMPARE(input->boundingRect().x(), qreal(0));
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    input->setHAlign(QQuickTextInput::AlignRight);
+    QCOMPARE(input->boundingRect().x(), input->width() - line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QCOMPARE(input->boundingRect().width(), line.naturalTextWidth());
+    QCOMPARE(input->boundingRect().height(), line.height());
+
+    input->setWrapMode(QQuickTextInput::Wrap);
+    QCOMPARE(input->boundingRect().right(), input->width());
+    QCOMPARE(input->boundingRect().y(), qreal(0));
+    QVERIFY(input->boundingRect().width() < line.naturalTextWidth());
+    QVERIFY(input->boundingRect().height() > line.height());
+
+    input->setVAlign(QQuickTextInput::AlignBottom);
+    QCOMPARE(input->boundingRect().right(), input->width());
+    QCOMPARE(input->boundingRect().bottom(), input->height());
+    QVERIFY(input->boundingRect().width() < line.naturalTextWidth());
+    QVERIFY(input->boundingRect().height() > line.height());
 }
 
 void tst_qquicktextinput::positionAt()
 {
-    QQuickView canvas(testFileUrl("positionAt.qml"));
-    QVERIFY(canvas.rootObject() != 0);
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
+    QQuickView window(testFileUrl("positionAt.qml"));
+    QVERIFY(window.rootObject() != 0);
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(window.rootObject());
     QVERIFY(textinputObject != 0);
 
     // Check autoscrolled...
@@ -1527,7 +1816,7 @@ void tst_qquicktextinput::positionAt()
 
     {   QInputMethodEvent inputEvent(preeditText, QList<QInputMethodEvent::Attribute>());
         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<int>(textinputObject, QString("positionAt(%1)").arg(0)), 0);
@@ -1540,7 +1829,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);
@@ -1558,13 +1847,13 @@ void tst_qquicktextinput::positionAt()
 
 void tst_qquicktextinput::maxLength()
 {
-    QQuickView canvas(testFileUrl("maxLength.qml"));
-    QVERIFY(canvas.rootObject() != 0);
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
+    QQuickView window(testFileUrl("maxLength.qml"));
+    QVERIFY(window.rootObject() != 0);
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(window.rootObject());
     QVERIFY(textinputObject != 0);
     QVERIFY(textinputObject->text().isEmpty());
     QVERIFY(textinputObject->maxLength() == 10);
@@ -1578,9 +1867,9 @@ void tst_qquicktextinput::maxLength()
     QTRY_VERIFY(textinputObject->hasActiveFocus() == true);
     for (int i=0; i<20; i++) {
         QTRY_COMPARE(textinputObject->text().length(), qMin(i,10));
-        //simulateKey(&canvas, Qt::Key_A);
-        QTest::keyPress(&canvas, Qt::Key_A);
-        QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+        //simulateKey(&window, Qt::Key_A);
+        QTest::keyPress(&window, Qt::Key_A);
+        QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
         QTest::qWait(50);
     }
 }
@@ -1589,11 +1878,11 @@ void tst_qquicktextinput::masks()
 {
     //Not a comprehensive test of the possible masks, that's done elsewhere (QLineEdit)
     //QString componentStr = "import QtQuick 2.0\nTextInput {  inputMask: 'HHHHhhhh'; }";
-    QQuickView canvas(testFileUrl("masks.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QQuickView window(testFileUrl("masks.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *textinputObject = qobject_cast<QQuickTextInput *>(window.rootObject());
     QVERIFY(textinputObject != 0);
     QTRY_VERIFY(textinputObject->hasActiveFocus() == true);
     QVERIFY(textinputObject->text().length() == 0);
@@ -1605,9 +1894,9 @@ void tst_qquicktextinput::masks()
         QCOMPARE(textinputObject->getText(0, qMin(i, 8)), QString(qMin(i, 8), 'a'));
         QCOMPARE(textinputObject->getText(qMin(i, 8), 8), QString(8 - qMin(i, 8), ' '));
         QCOMPARE(i>=4, textinputObject->hasAcceptableInput());
-        //simulateKey(&canvas, Qt::Key_A);
-        QTest::keyPress(&canvas, Qt::Key_A);
-        QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+        //simulateKey(&window, Qt::Key_A);
+        QTest::keyPress(&window, Qt::Key_A);
+        QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
         QTest::qWait(50);
     }
 }
@@ -1620,17 +1909,18 @@ void tst_qquicktextinput::validators()
 
     QLocale::setDefault(QLocale(QStringLiteral("C")));
 
-    QQuickView canvas(testFileUrl("validators.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
+    QQuickView window(testFileUrl("validators.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QVERIFY(canvas.rootObject() != 0);
+    QVERIFY(window.rootObject() != 0);
 
     QLocale defaultLocale;
     QLocale enLocale("en");
     QLocale deLocale("de_DE");
 
-    QQuickTextInput *intInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("intInput")));
+    QQuickTextInput *intInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("intInput")));
     QVERIFY(intInput);
     QSignalSpy intSpy(intInput, SIGNAL(acceptableInputChanged()));
 
@@ -1649,63 +1939,63 @@ void tst_qquicktextinput::validators()
     QTRY_VERIFY(intInput->hasActiveFocus());
     QCOMPARE(intInput->hasAcceptableInput(), false);
     QCOMPARE(intInput->property("acceptable").toBool(), false);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
     QCOMPARE(intInput->property("acceptable").toBool(), false);
     QCOMPARE(intSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_2);
-    QTest::keyRelease(&canvas, Qt::Key_2, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_2);
+    QTest::keyRelease(&window, Qt::Key_2, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
     QCOMPARE(intInput->property("acceptable").toBool(), false);
     QCOMPARE(intSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_Period);
-    QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Period);
+    QTest::keyRelease(&window, Qt::Key_Period, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
-    QTest::keyPress(&canvas, Qt::Key_Comma);
-    QTest::keyRelease(&canvas, Qt::Key_Comma, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Comma);
+    QTest::keyRelease(&window, Qt::Key_Comma, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1,"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
     intValidator->setLocaleName(deLocale.name());
-    QTest::keyPress(&canvas, Qt::Key_Period);
-    QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Period);
+    QTest::keyRelease(&window, Qt::Key_Period, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1."));
     QCOMPARE(intInput->hasAcceptableInput(), false);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(intInput->text(), QLatin1String("1"));
     QCOMPARE(intInput->hasAcceptableInput(), false);
     intValidator->resetLocaleName();
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QCOMPARE(intInput->text(), QLatin1String("11"));
     QCOMPARE(intInput->hasAcceptableInput(), true);
     QCOMPARE(intInput->property("acceptable").toBool(), true);
     QCOMPARE(intSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_0);
-    QTest::keyRelease(&canvas, Qt::Key_0, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_0);
+    QTest::keyRelease(&window, Qt::Key_0, Qt::NoModifier ,10);
     QTest::qWait(50);
     QCOMPARE(intInput->text(), QLatin1String("11"));
     QCOMPARE(intInput->hasAcceptableInput(), true);
     QCOMPARE(intInput->property("acceptable").toBool(), true);
     QCOMPARE(intSpy.count(), 1);
 
-    QQuickTextInput *dblInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("dblInput")));
+    QQuickTextInput *dblInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("dblInput")));
     QVERIFY(dblInput);
     QSignalSpy dblSpy(dblInput, SIGNAL(acceptableInputChanged()));
 
@@ -1724,81 +2014,81 @@ void tst_qquicktextinput::validators()
     QVERIFY(dblInput->hasActiveFocus() == true);
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("1"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_2);
-    QTest::keyRelease(&canvas, Qt::Key_2, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_2);
+    QTest::keyRelease(&window, Qt::Key_2, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblInput->property("acceptable").toBool(), true);
     QCOMPARE(dblSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_Comma);
-    QTest::keyRelease(&canvas, Qt::Key_Comma, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Comma);
+    QTest::keyRelease(&window, Qt::Key_Comma, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     dblValidator->setLocaleName(deLocale.name());
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,1"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,11"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,1"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12,"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     dblValidator->resetLocaleName();
-    QTest::keyPress(&canvas, Qt::Key_Period);
-    QTest::keyRelease(&canvas, Qt::Key_Period, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Period);
+    QTest::keyRelease(&window, Qt::Key_Period, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12."));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblInput->property("acceptable").toBool(), true);
     QCOMPARE(dblSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.1"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblInput->property("acceptable").toBool(), true);
     QCOMPARE(dblSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.11"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblInput->property("acceptable").toBool(), true);
     QCOMPARE(dblSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.11"));
     QCOMPARE(dblInput->hasAcceptableInput(), true);
@@ -1811,122 +2101,132 @@ void tst_qquicktextinput::validators()
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.1"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
     // Once unacceptable input is in anything goes until it reaches an acceptable state again.
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.11"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12.1"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12."));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("12"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
-    QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_Backspace);
+    QTest::keyRelease(&window, Qt::Key_Backspace, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(dblInput->text(), QLatin1String("1"));
     QCOMPARE(dblInput->hasAcceptableInput(), false);
     QCOMPARE(dblInput->property("acceptable").toBool(), false);
     QCOMPARE(dblSpy.count(), 2);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QCOMPARE(dblInput->text(), QLatin1String("11"));
     QCOMPARE(dblInput->property("acceptable").toBool(), true);
     QCOMPARE(dblInput->hasAcceptableInput(), true);
     QCOMPARE(dblSpy.count(), 3);
 
-    QQuickTextInput *strInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("strInput")));
+    // Changing the validator properties will re-evaluate whether the input is acceptable.
+    intValidator->setTop(10);
+    QCOMPARE(dblInput->property("acceptable").toBool(), false);
+    QCOMPARE(dblInput->hasAcceptableInput(), false);
+    QCOMPARE(dblSpy.count(), 4);
+    intValidator->setTop(12);
+    QCOMPARE(dblInput->property("acceptable").toBool(), true);
+    QCOMPARE(dblInput->hasAcceptableInput(), true);
+    QCOMPARE(dblSpy.count(), 5);
+
+    QQuickTextInput *strInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("strInput")));
     QVERIFY(strInput);
     QSignalSpy strSpy(strInput, SIGNAL(acceptableInputChanged()));
     strInput->setFocus(true);
     QVERIFY(strInput->hasActiveFocus() == true);
     QCOMPARE(strInput->hasAcceptableInput(), false);
     QCOMPARE(strInput->property("acceptable").toBool(), false);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String(""));
     QCOMPARE(strInput->hasAcceptableInput(), false);
     QCOMPARE(strInput->property("acceptable").toBool(), false);
     QCOMPARE(strSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String("a"));
     QCOMPARE(strInput->hasAcceptableInput(), false);
     QCOMPARE(strInput->property("acceptable").toBool(), false);
     QCOMPARE(strSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String("aa"));
     QCOMPARE(strInput->hasAcceptableInput(), true);
     QCOMPARE(strInput->property("acceptable").toBool(), true);
     QCOMPARE(strSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String("aaa"));
     QCOMPARE(strInput->hasAcceptableInput(), true);
     QCOMPARE(strInput->property("acceptable").toBool(), true);
     QCOMPARE(strSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String("aaaa"));
     QCOMPARE(strInput->hasAcceptableInput(), true);
     QCOMPARE(strInput->property("acceptable").toBool(), true);
     QCOMPARE(strSpy.count(), 1);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(strInput->text(), QLatin1String("aaaa"));
     QCOMPARE(strInput->hasAcceptableInput(), true);
     QCOMPARE(strInput->property("acceptable").toBool(), true);
     QCOMPARE(strSpy.count(), 1);
 
-    QQuickTextInput *unvalidatedInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("unvalidatedInput")));
+    QQuickTextInput *unvalidatedInput = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("unvalidatedInput")));
     QVERIFY(unvalidatedInput);
     QSignalSpy unvalidatedSpy(unvalidatedInput, SIGNAL(acceptableInputChanged()));
     unvalidatedInput->setFocus(true);
     QVERIFY(unvalidatedInput->hasActiveFocus() == true);
     QCOMPARE(unvalidatedInput->hasAcceptableInput(), true);
     QCOMPARE(unvalidatedInput->property("acceptable").toBool(), true);
-    QTest::keyPress(&canvas, Qt::Key_1);
-    QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_1);
+    QTest::keyRelease(&window, Qt::Key_1, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(unvalidatedInput->text(), QLatin1String("1"));
     QCOMPARE(unvalidatedInput->hasAcceptableInput(), true);
     QCOMPARE(unvalidatedInput->property("acceptable").toBool(), true);
     QCOMPARE(unvalidatedSpy.count(), 0);
-    QTest::keyPress(&canvas, Qt::Key_A);
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QTest::qWait(50);
     QTRY_COMPARE(unvalidatedInput->text(), QLatin1String("1a"));
     QCOMPARE(unvalidatedInput->hasAcceptableInput(), true);
@@ -1936,14 +2236,14 @@ void tst_qquicktextinput::validators()
 
 void tst_qquicktextinput::inputMethods()
 {
-    QQuickView canvas(testFileUrl("inputmethods.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
+    QQuickView window(testFileUrl("inputmethods.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
     // test input method hints
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(canvas.rootObject());
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(window.rootObject());
     QVERIFY(input != 0);
     QVERIFY(input->inputMethodHints() & Qt::ImhNoPredictiveText);
     QSignalSpy inputMethodHintSpy(input, SIGNAL(inputMethodHintsChanged()));
@@ -1963,55 +2263,33 @@ 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);
 
-    // test that basic tentative commit gets to text property on preedit state
-    input->setText("");
-    QList<QInputMethodEvent::Attribute> attributes;
-    QInputMethodEvent preeditEvent("test", attributes);
-    preeditEvent.setTentativeCommitString("test");
-    QGuiApplication::sendEvent(input, &preeditEvent);
-    QCOMPARE(input->text(), QString("test"));
-
-    // tentative commit not allowed present in surrounding text
-    QInputMethodQueryEvent queryEvent(Qt::ImSurroundingText);
-    QGuiApplication::sendEvent(input, &queryEvent);
-    QCOMPARE(queryEvent.value(Qt::ImSurroundingText).toString(), QString(""));
-
-    // if text with tentative commit does not validate, not allowed to be part of text property
-    input->setText(""); // ensure input state is reset
-    QValidator *validator = new QIntValidator(0, 100);
-    input->setValidator(validator);
-    QGuiApplication::sendEvent(input, &preeditEvent);
-    QCOMPARE(input->text(), QString(""));
-    input->setValidator(0);
-    delete validator;
-
     // input should reset selection even if replacement parameters are out of bounds
     input->setText("text");
     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);
@@ -2030,52 +2308,62 @@ the extent of the text, then they should ignore the keys.
 */
 void tst_qquicktextinput::navigation()
 {
-    QQuickView canvas(testFileUrl("navigation.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
+    QQuickView window(testFileUrl("navigation.qml"));
+    window.show();
+    window.requestActivateWindow();
 
-    QVERIFY(canvas.rootObject() != 0);
+    QVERIFY(window.rootObject() != 0);
 
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("myInput")));
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
 
     QVERIFY(input != 0);
     input->setCursorPosition(0);
     QTRY_VERIFY(input->hasActiveFocus() == true);
-    simulateKey(&canvas, Qt::Key_Left);
+    simulateKey(&window, Qt::Key_Left);
     QVERIFY(input->hasActiveFocus() == false);
-    simulateKey(&canvas, Qt::Key_Right);
+    simulateKey(&window, Qt::Key_Right);
     QVERIFY(input->hasActiveFocus() == true);
     //QT-2944: If text is selected, ensure we deselect upon cursor motion
     input->setCursorPosition(input->text().length());
     input->select(0,input->text().length());
     QVERIFY(input->selectionStart() != input->selectionEnd());
-    simulateKey(&canvas, Qt::Key_Right);
+    simulateKey(&window, Qt::Key_Right);
     QVERIFY(input->selectionStart() == input->selectionEnd());
     QVERIFY(input->selectionStart() == input->text().length());
     QVERIFY(input->hasActiveFocus() == true);
-    simulateKey(&canvas, Qt::Key_Right);
+    simulateKey(&window, Qt::Key_Right);
     QVERIFY(input->hasActiveFocus() == false);
-    simulateKey(&canvas, Qt::Key_Left);
+    simulateKey(&window, Qt::Key_Left);
     QVERIFY(input->hasActiveFocus() == true);
 
     // Up and Down should NOT do Home/End, even on Mac OS X (QTBUG-10438).
     input->setCursorPosition(2);
     QCOMPARE(input->cursorPosition(),2);
-    simulateKey(&canvas, Qt::Key_Up);
+    simulateKey(&window, Qt::Key_Up);
     QCOMPARE(input->cursorPosition(),2);
-    simulateKey(&canvas, Qt::Key_Down);
+    simulateKey(&window, Qt::Key_Down);
     QCOMPARE(input->cursorPosition(),2);
+
+    // Test left and right navigation works if the TextInput is empty (QTBUG-25447).
+    input->setText(QString());
+    QCOMPARE(input->cursorPosition(), 0);
+    simulateKey(&window, Qt::Key_Right);
+    QCOMPARE(input->hasActiveFocus(), false);
+    simulateKey(&window, Qt::Key_Left);
+    QCOMPARE(input->hasActiveFocus(), true);
+    simulateKey(&window, Qt::Key_Left);
+    QCOMPARE(input->hasActiveFocus(), false);
 }
 
 void tst_qquicktextinput::navigation_RTL()
 {
-    QQuickView canvas(testFileUrl("navigation.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
+    QQuickView window(testFileUrl("navigation.qml"));
+    window.show();
+    window.requestActivateWindow();
 
-    QVERIFY(canvas.rootObject() != 0);
+    QVERIFY(window.rootObject() != 0);
 
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("myInput")));
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
 
     QVERIFY(input != 0);
     const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647};
@@ -2085,38 +2373,30 @@ void tst_qquicktextinput::navigation_RTL()
     QTRY_VERIFY(input->hasActiveFocus() == true);
 
     // move off
-    simulateKey(&canvas, Qt::Key_Right);
+    simulateKey(&window, Qt::Key_Right);
     QVERIFY(input->hasActiveFocus() == false);
 
     // move back
-    simulateKey(&canvas, Qt::Key_Left);
+    simulateKey(&window, Qt::Key_Left);
     QVERIFY(input->hasActiveFocus() == true);
 
     input->setCursorPosition(input->text().length());
     QVERIFY(input->hasActiveFocus() == true);
 
     // move off
-    simulateKey(&canvas, Qt::Key_Left);
+    simulateKey(&window, Qt::Key_Left);
     QVERIFY(input->hasActiveFocus() == false);
 
     // move back
-    simulateKey(&canvas, Qt::Key_Right);
+    simulateKey(&window, Qt::Key_Right);
     QVERIFY(input->hasActiveFocus() == true);
 }
 
-void tst_qquicktextinput::copyAndPaste() {
 #ifndef QT_NO_CLIPBOARD
-
-#ifdef Q_OS_MAC
-    {
-        PasteboardRef pasteboard;
-        OSStatus status = PasteboardCreate(0, &pasteboard);
-        if (status == noErr)
-            CFRelease(pasteboard);
-        else
-            QSKIP("This machine doesn't support the clipboard");
-    }
-#endif
+void tst_qquicktextinput::copyAndPaste()
+{
+    if (!PlatformQuirks::isClipboardAvailable())
+        QSKIP("This machine doesn't support the clipboard");
 
     QString componentStr = "import QtQuick 2.0\nTextInput { text: \"Hello world!\" }";
     QQmlComponent textInputComponent(&engine);
@@ -2126,7 +2406,7 @@ void tst_qquicktextinput::copyAndPaste() {
 
     // copy and paste
     QCOMPARE(textInput->text().length(), 12);
-    textInput->select(0, textInput->text().length());;
+    textInput->select(0, textInput->text().length());
     textInput->copy();
     QCOMPARE(textInput->selectedText(), QString("Hello world!"));
     QCOMPARE(textInput->selectedText().length(), 12);
@@ -2140,14 +2420,27 @@ void tst_qquicktextinput::copyAndPaste() {
     QVERIFY(textInput->canPaste());
     textInput->setReadOnly(true);
     QVERIFY(!textInput->canPaste());
+    textInput->paste();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+    QCOMPARE(textInput->text().length(), 24);
     textInput->setReadOnly(false);
     QVERIFY(textInput->canPaste());
 
+    // cut: no selection
+    textInput->cut();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+
     // select word
     textInput->setCursorPosition(0);
     textInput->selectWord();
     QCOMPARE(textInput->selectedText(), QString("Hello"));
 
+    // cut: read only.
+    textInput->setReadOnly(true);
+    textInput->cut();
+    QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
+    textInput->setReadOnly(false);
+
     // select all and cut
     textInput->selectAll();
     textInput->cut();
@@ -2156,6 +2449,18 @@ void tst_qquicktextinput::copyAndPaste() {
     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
     QCOMPARE(textInput->text().length(), 24);
 
+    // Copy first word.
+    textInput->setCursorPosition(0);
+    textInput->selectWord();
+    textInput->copy();
+    // copy: no selection, previous copy retained;
+    textInput->setCursorPosition(0);
+    QCOMPARE(textInput->selectedText(), QString());
+    textInput->copy();
+    textInput->setText(QString());
+    textInput->paste();
+    QCOMPARE(textInput->text(), QString("Hello"));
+
     // clear copy buffer
     QClipboard *clipboard = QGuiApplication::clipboard();
     QVERIFY(clipboard);
@@ -2169,7 +2474,7 @@ void tst_qquicktextinput::copyAndPaste() {
         QQuickTextInput::EchoMode echoMode = QQuickTextInput::EchoMode(index);
         textInput->setEchoMode(echoMode);
         textInput->setText("My password");
-        textInput->select(0, textInput->text().length());;
+        textInput->select(0, textInput->text().length());
         textInput->copy();
         if (echoMode == QQuickTextInput::Normal) {
             QVERIFY(!clipboard->text().isEmpty());
@@ -2182,22 +2487,14 @@ void tst_qquicktextinput::copyAndPaste() {
     }
 
     delete textInput;
-#endif
 }
+#endif
 
-void tst_qquicktextinput::copyAndPasteKeySequence() {
 #ifndef QT_NO_CLIPBOARD
-
-#ifdef Q_OS_MAC
-    {
-        PasteboardRef pasteboard;
-        OSStatus status = PasteboardCreate(0, &pasteboard);
-        if (status == noErr)
-            CFRelease(pasteboard);
-        else
-            QSKIP("This machine doesn't support the clipboard");
-    }
-#endif
+void tst_qquicktextinput::copyAndPasteKeySequence()
+{
+    if (!PlatformQuirks::isClipboardAvailable())
+        QSKIP("This machine doesn't support the clipboard");
 
     QString componentStr = "import QtQuick 2.0\nTextInput { text: \"Hello world!\"; focus: true }";
     QQmlComponent textInputComponent(&engine);
@@ -2205,31 +2502,30 @@ void tst_qquicktextinput::copyAndPasteKeySequence() {
     QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
     QVERIFY(textInput != 0);
 
-    QQuickCanvas canvas;
-    textInput->setParentItem(canvas.rootItem());
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
     // copy and paste
     QVERIFY(textInput->hasActiveFocus());
     QCOMPARE(textInput->text().length(), 12);
     textInput->select(0, textInput->text().length());
-    simulateKeys(&canvas, QKeySequence::Copy);
+    simulateKeys(&window, QKeySequence::Copy);
     QCOMPARE(textInput->selectedText(), QString("Hello world!"));
     QCOMPARE(textInput->selectedText().length(), 12);
     textInput->setCursorPosition(0);
     QVERIFY(textInput->canPaste());
-    simulateKeys(&canvas, QKeySequence::Paste);
+    simulateKeys(&window, QKeySequence::Paste);
     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
     QCOMPARE(textInput->text().length(), 24);
 
     // select all and cut
-    simulateKeys(&canvas, QKeySequence::SelectAll);
-    simulateKeys(&canvas, QKeySequence::Cut);
+    simulateKeys(&window, QKeySequence::SelectAll);
+    simulateKeys(&window, QKeySequence::Cut);
     QCOMPARE(textInput->text().length(), 0);
-    simulateKeys(&canvas, QKeySequence::Paste);
+    simulateKeys(&window, QKeySequence::Paste);
     QCOMPARE(textInput->text(), QString("Hello world!Hello world!"));
     QCOMPARE(textInput->text().length(), 24);
 
@@ -2246,8 +2542,8 @@ void tst_qquicktextinput::copyAndPasteKeySequence() {
         QQuickTextInput::EchoMode echoMode = QQuickTextInput::EchoMode(index);
         textInput->setEchoMode(echoMode);
         textInput->setText("My password");
-        textInput->select(0, textInput->text().length());;
-        simulateKeys(&canvas, QKeySequence::Copy);
+        textInput->select(0, textInput->text().length());
+        simulateKeys(&window, QKeySequence::Copy);
         if (echoMode == QQuickTextInput::Normal) {
             QVERIFY(!clipboard->text().isEmpty());
             QCOMPARE(clipboard->text(), QString("My password"));
@@ -2259,12 +2555,12 @@ void tst_qquicktextinput::copyAndPasteKeySequence() {
     }
 
     delete textInput;
-#endif
 }
+#endif
 
-void tst_qquicktextinput::canPasteEmpty() {
 #ifndef QT_NO_CLIPBOARD
-
+void tst_qquicktextinput::canPasteEmpty()
+{
     QGuiApplication::clipboard()->clear();
 
     QString componentStr = "import QtQuick 2.0\nTextInput { text: \"Hello world!\" }";
@@ -2275,13 +2571,12 @@ void tst_qquicktextinput::canPasteEmpty() {
 
     bool cp = !textInput->isReadOnly() && QGuiApplication::clipboard()->text().length() != 0;
     QCOMPARE(textInput->canPaste(), cp);
-
-#endif
 }
+#endif
 
-void tst_qquicktextinput::canPaste() {
 #ifndef QT_NO_CLIPBOARD
-
+void tst_qquicktextinput::canPaste()
+{
     QGuiApplication::clipboard()->setText("Some text");
 
     QString componentStr = "import QtQuick 2.0\nTextInput { text: \"Hello world!\" }";
@@ -2292,9 +2587,51 @@ void tst_qquicktextinput::canPaste() {
 
     bool cp = !textInput->isReadOnly() && QGuiApplication::clipboard()->text().length() != 0;
     QCOMPARE(textInput->canPaste(), cp);
-
+}
 #endif
+
+#ifndef QT_NO_CLIPBOARD
+void tst_qquicktextinput::middleClickPaste()
+{
+    if (!PlatformQuirks::isClipboardAvailable())
+        QSKIP("This machine doesn't support the clipboard");
+
+    QQuickView window(testFileUrl("mouseselection_true.qml"));
+
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *textInputObject = qobject_cast<QQuickTextInput *>(window.rootObject());
+    QVERIFY(textInputObject != 0);
+
+    textInputObject->setFocus(true);
+
+    QString originalText = textInputObject->text();
+    QString selectedText = "234567";
+
+    // press-and-drag-and-release from x1 to x2
+    const QPoint p1 = textInputObject->positionToRectangle(2).center().toPoint();
+    const QPoint p2 = textInputObject->positionToRectangle(8).center().toPoint();
+    const QPoint p3 = textInputObject->positionToRectangle(1).center().toPoint();
+    QTest::mousePress(&window, Qt::LeftButton, Qt::NoModifier, p1);
+    QTest::mouseMove(&window, p2);
+    QTest::mouseRelease(&window, Qt::LeftButton, Qt::NoModifier, p2);
+    QTRY_COMPARE(textInputObject->selectedText(), selectedText);
+
+    // Middle click pastes the selected text, assuming the platform supports it.
+    QTest::mouseClick(&window, Qt::MiddleButton, Qt::NoModifier, p3);
+
+    // ### This is to prevent double click detection from carrying over to the next test.
+    QTest::qWait(QGuiApplication::styleHints()->mouseDoubleClickInterval() + 10);
+
+    if (QGuiApplication::clipboard()->supportsSelection())
+        QCOMPARE(textInputObject->text().mid(1, selectedText.length()), selectedText);
+    else
+        QCOMPARE(textInputObject->text(), originalText);
 }
+#endif
 
 void tst_qquicktextinput::passwordCharacter()
 {
@@ -2330,9 +2667,13 @@ void tst_qquicktextinput::cursorDelegate()
     view.requestActivateWindow();
     QQuickTextInput *textInputObject = view.rootObject()->findChild<QQuickTextInput*>("textInputObject");
     QVERIFY(textInputObject != 0);
-    QVERIFY(textInputObject->findChild<QQuickItem*>("cursorInstance"));
+    // Delegate is created on demand, and so won't be available immediately.  Focus in or
+    // setCursorVisible(true) will trigger creation.
+    QTRY_VERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
+    QVERIFY(!textInputObject->isCursorVisible());
     //Test Delegate gets created
     textInputObject->setFocus(true);
+    QVERIFY(textInputObject->isCursorVisible());
     QQuickItem* delegateObject = textInputObject->findChild<QQuickItem*>("cursorInstance");
     QVERIFY(delegateObject);
     QCOMPARE(delegateObject->property("localProperty").toString(), QString("Hello"));
@@ -2345,24 +2686,135 @@ void tst_qquicktextinput::cursorDelegate()
     textInputObject->setCursorPosition(0);
     QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
     QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    // Test delegate gets moved on mouse press.
+    textInputObject->setSelectByMouse(true);
+    textInputObject->setCursorPosition(0);
+    const QPoint point1 = textInputObject->positionToRectangle(5).center().toPoint();
+    QTest::qWait(400);  //ensure this isn't treated as a double-click
+    QTest::mouseClick(&view, Qt::LeftButton, 0, point1);
+    QTest::qWait(50);
+    QTRY_VERIFY(textInputObject->cursorPosition() != 0);
+    QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+    QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    // Test delegate gets moved on mouse drag
+    textInputObject->setCursorPosition(0);
+    const QPoint point2 = textInputObject->positionToRectangle(10).center().toPoint();
+    QTest::qWait(400);  //ensure this isn't treated as a double-click
+    QTest::mousePress(&view, Qt::LeftButton, 0, point1);
+    QMouseEvent mv(QEvent::MouseMove, point2, Qt::LeftButton, Qt::LeftButton,Qt::NoModifier);
+    QGuiApplication::sendEvent(&view, &mv);
+    QTest::mouseRelease(&view, Qt::LeftButton, 0, point2);
+    QTest::qWait(50);
+    QTRY_COMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+    QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    textInputObject->setReadOnly(true);
+    textInputObject->setCursorPosition(0);
+    QTest::qWait(400);  //ensure this isn't treated as a double-click
+    QTest::mouseClick(&view, Qt::LeftButton, 0, textInputObject->positionToRectangle(5).center().toPoint());
+    QTest::qWait(50);
+    QTRY_VERIFY(textInputObject->cursorPosition() != 0);
+    QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+    QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    textInputObject->setCursorPosition(0);
+    QTest::qWait(400);  //ensure this isn't treated as a double-click
+    QTest::mouseClick(&view, Qt::LeftButton, 0, textInputObject->positionToRectangle(5).center().toPoint());
+    QTest::qWait(50);
+    QTRY_VERIFY(textInputObject->cursorPosition() != 0);
+    QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+    QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    textInputObject->setCursorPosition(0);
+    QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+    QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+
+    textInputObject->setReadOnly(false);
+
+    // Delegate moved when text is entered
+    textInputObject->setText(QString());
+    for (int i = 0; i < 20; ++i) {
+        QTest::keyClick(&view, Qt::Key_A);
+        QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+        QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+    }
+
+    // Delegate moved when text is entered by im.
+    textInputObject->setText(QString());
+    for (int i = 0; i < 20; ++i) {
+        QInputMethodEvent event;
+        event.setCommitString("w");
+        QGuiApplication::sendEvent(textInputObject, &event);
+        QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+        QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+    }
+    // Delegate moved when text is removed by im.
+    for (int i = 19; i > 1; --i) {
+        QInputMethodEvent event;
+        event.setCommitString(QString(), -1, 1);
+        QGuiApplication::sendEvent(textInputObject, &event);
+        QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+        QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+    }
+    {   // Delegate moved the text is changed in place by im.
+        QInputMethodEvent event;
+        event.setCommitString("i", -1, 1);
+        QGuiApplication::sendEvent(textInputObject, &event);
+        QCOMPARE(textInputObject->cursorRectangle().x(), delegateObject->x());
+        QCOMPARE(textInputObject->cursorRectangle().y(), delegateObject->y());
+    }
+
     //Test Delegate gets deleted
     textInputObject->setCursorDelegate(0);
     QVERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
 }
 
-void tst_qquicktextinput::cursorVisible()
+void tst_qquicktextinput::remoteCursorDelegate()
 {
-    QQuickView view(testFileUrl("cursorVisible.qml"));
-    view.show();
-    view.requestActivateWindow();
-    QTest::qWaitForWindowShown(&view);
-    QTRY_COMPARE(&view, qGuiApp->focusWindow());
+    TestHTTPServer server(SERVER_PORT);
+    server.serveDirectory(dataDirectory());
 
-    QQuickTextInput input;
-    input.componentComplete();
-    QSignalSpy spy(&input, SIGNAL(cursorVisibleChanged(bool)));
+    QQuickView view;
 
-    QCOMPARE(input.isCursorVisible(), false);
+    QQmlComponent component(view.engine(), QUrl(SERVER_ADDR "/RemoteCursor.qml"));
+
+    view.rootContext()->setContextProperty("contextDelegate", &component);
+    view.setSource(testFileUrl("cursorTestRemote.qml"));
+    view.show();
+    view.requestActivateWindow();
+    QQuickTextInput *textInputObject = view.rootObject()->findChild<QQuickTextInput*>("textInputObject");
+    QVERIFY(textInputObject != 0);
+
+    // Delegate is created on demand, and so won't be available immediately.  Focus in or
+    // setCursorVisible(true) will trigger creation.
+    QTRY_VERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
+    QVERIFY(!textInputObject->isCursorVisible());
+
+    textInputObject->setFocus(true);
+    QVERIFY(textInputObject->isCursorVisible());
+
+    QCOMPARE(component.status(), QQmlComponent::Loading);
+    QVERIFY(!textInputObject->findChild<QQuickItem*>("cursorInstance"));
+
+    // Wait for component to load.
+    QTRY_COMPARE(component.status(), QQmlComponent::Ready);
+    QVERIFY(textInputObject->findChild<QQuickItem*>("cursorInstance"));
+}
+
+void tst_qquicktextinput::cursorVisible()
+{
+    QQuickTextInput input;
+    input.componentComplete();
+    QSignalSpy spy(&input, SIGNAL(cursorVisibleChanged(bool)));
+
+    QQuickView view(testFileUrl("cursorVisible.qml"));
+    view.show();
+    view.requestActivateWindow();
+    QTest::qWaitForWindowActive(&view);
+
+    QCOMPARE(input.isCursorVisible(), false);
 
     input.setCursorVisible(true);
     QCOMPARE(input.isCursorVisible(), true);
@@ -2391,21 +2843,84 @@ void tst_qquicktextinput::cursorVisible()
     QQuickView alternateView;
     alternateView.show();
     alternateView.requestActivateWindow();
-    QTest::qWaitForWindowShown(&alternateView);
+    QTest::qWaitForWindowActive(&alternateView);
 
     QCOMPARE(input.isCursorVisible(), false);
     QCOMPARE(spy.count(), 6);
 
     view.requestActivateWindow();
-    QTest::qWaitForWindowShown(&view);
+    QTest::qWaitForWindowActive(&view);
     QCOMPARE(input.isCursorVisible(), true);
     QCOMPARE(spy.count(), 7);
+
+    {   // Cursor attribute with 0 length hides cursor.
+        QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
+        QCoreApplication::sendEvent(&input, &ev);
+    }
+    QCOMPARE(input.isCursorVisible(), false);
+    QCOMPARE(spy.count(), 8);
+
+    {   // Cursor attribute with non zero length shows cursor.
+        QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 1, QVariant()));
+        QCoreApplication::sendEvent(&input, &ev);
+    }
+    QCOMPARE(input.isCursorVisible(), true);
+    QCOMPARE(spy.count(), 9);
+
+    {   // If the cursor is hidden by the input method and the text is changed it should be visible again.
+        QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
+        QCoreApplication::sendEvent(&input, &ev);
+    }
+    QCOMPARE(input.isCursorVisible(), false);
+    QCOMPARE(spy.count(), 10);
+
+    input.setText("something");
+    QCOMPARE(input.isCursorVisible(), true);
+    QCOMPARE(spy.count(), 11);
+
+    {   // If the cursor is hidden by the input method and the cursor position is changed it should be visible again.
+        QInputMethodEvent ev(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
+        QCoreApplication::sendEvent(&input, &ev);
+    }
+    QCOMPARE(input.isCursorVisible(), false);
+    QCOMPARE(spy.count(), 12);
+
+    input.setCursorPosition(5);
+    QCOMPARE(input.isCursorVisible(), true);
+    QCOMPARE(spy.count(), 13);
 }
 
-void tst_qquicktextinput::cursorRectangle()
+void tst_qquicktextinput::cursorRectangle_data()
 {
+    const quint16 arabic_str[] = { 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0638, 0x0643, 0x00646, 0x0647, 0x0633, 0x0647};
 
-    QString text = "Hello World!";
+    QTest::addColumn<QString>("text");
+    QTest::addColumn<int>("positionAtWidth");
+    QTest::addColumn<int>("wrapPosition");
+    QTest::addColumn<QString>("shortText");
+    QTest::addColumn<bool>("leftToRight");
+
+    QTest::newRow("left to right")
+            << "Hello      World!" << 5 << 11
+            << "Hi"
+            << true;
+    QTest::newRow("right to left")
+            << QString::fromUtf16(arabic_str, lengthOf(arabic_str)) << 5 << 11
+            << QString::fromUtf16(arabic_str, 3)
+            << false;
+}
+
+void tst_qquicktextinput::cursorRectangle()
+{
+    QFETCH(QString, text);
+    QFETCH(int, positionAtWidth);
+    QFETCH(int, wrapPosition);
+    QFETCH(QString, shortText);
+    QFETCH(bool, leftToRight);
 
     QQuickTextInput input;
     input.setText(text);
@@ -2422,33 +2937,30 @@ void tst_qquicktextinput::cursorRectangle()
     QTextLine line = layout.createLine();
     layout.endLayout();
 
-    input.setWidth(line.cursorToX(5, QTextLine::Leading));
+    qreal offset = 0;
+    if (leftToRight) {
+        input.setWidth(line.cursorToX(positionAtWidth, QTextLine::Leading));
+    } else {
+        input.setWidth(line.horizontalAdvance() - line.cursorToX(positionAtWidth, QTextLine::Leading));
+        offset = line.horizontalAdvance() - input.width();
+    }
     input.setHeight(qCeil(line.height() * 3 / 2));
 
     QRectF r;
 
-    // some tolerance for different fonts.
-#ifdef Q_OS_LINUX
-    const int error = 2;
-#else
-    const int error = 5;
-#endif
-
-    for (int i = 0; i <= 5; ++i) {
+    for (int i = 0; i <= positionAtWidth; ++i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
 
-        QVERIFY(r.left() < qCeil(line.cursorToX(i, QTextLine::Trailing)));
-        QVERIFY(r.right() >= qFloor(line.cursorToX(i , QTextLine::Leading)));
+        QCOMPARE(r.left(), line.cursorToX(i, QTextLine::Leading) - offset);
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
     // Check the cursor rectangle remains within the input bounding rect when auto scrolling.
-    QVERIFY(r.left() < input.width() + error);
-    QVERIFY(r.right() >= input.width() - error);
+    QCOMPARE(r.left(), leftToRight ? input.width() : 0);
 
-    for (int i = 6; i < text.length(); ++i) {
+    for (int i = positionAtWidth + 1; i < text.length(); ++i) {
         input.setCursorPosition(i);
         QCOMPARE(r, input.cursorRectangle());
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
@@ -2459,7 +2971,11 @@ void tst_qquicktextinput::cursorRectangle()
         input.setCursorPosition(i);
         r = input.cursorRectangle();
         QCOMPARE(r.top(), 0.);
-        QVERIFY(r.right() >= 0);
+        if (leftToRight) {
+            QVERIFY(r.right() >= 0);
+        } else {
+            QVERIFY(r.left() <= input.width());
+        }
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
@@ -2467,69 +2983,99 @@ void tst_qquicktextinput::cursorRectangle()
     // Check position with word wrap.
     input.setWrapMode(QQuickTextInput::WordWrap);
     input.setAutoScroll(false);
-    for (int i = 0; i <= 5; ++i) {
+    for (int i = 0; i < wrapPosition; ++i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
 
-        QVERIFY(r.left() < qCeil(line.cursorToX(i, QTextLine::Trailing)));
-        QVERIFY(r.right() >= qFloor(line.cursorToX(i , QTextLine::Leading)));
+        QCOMPARE(r.left(), line.cursorToX(i, QTextLine::Leading) - offset);
         QCOMPARE(r.top(), 0.);
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
-    input.setCursorPosition(6);
+    input.setCursorPosition(wrapPosition);
     r = input.cursorRectangle();
-    QCOMPARE(r.left(), 0.);
-    QVERIFY(r.top() > line.height() - error);
+    if (leftToRight) {
+        QCOMPARE(r.left(), 0.);
+    } else {
+        QCOMPARE(r.left(), input.width());
+    }
+    QVERIFY(r.top() >= line.height() - 1);
     QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
-    QCOMPARE(input.positionToRectangle(6), r);
+    QCOMPARE(input.positionToRectangle(11), r);
 
-    for (int i = 7; i < text.length(); ++i) {
+    for (int i = wrapPosition + 1; i < text.length(); ++i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
-        QVERIFY(r.top() > line.height() - error);
+        QVERIFY(r.top() >= line.height() - 1);
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
     // Check vertical scrolling with word wrap.
     input.setAutoScroll(true);
-    for (int i = 0; i <= 5; ++i) {
+    for (int i = 0; i <= positionAtWidth; ++i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
 
-        QVERIFY(r.left() < qCeil(line.cursorToX(i, QTextLine::Trailing)));
-        QVERIFY(r.right() >= qFloor(line.cursorToX(i , QTextLine::Leading)));
+        QCOMPARE(r.left(), line.cursorToX(i, QTextLine::Leading) - offset);
         QCOMPARE(r.top(), 0.);
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
-    input.setCursorPosition(6);
+    // Whitespace doesn't wrap, so scroll horizontally until the until the cursor
+    // reaches the next non-whitespace character.
+    QCOMPARE(r.left(), leftToRight ? input.width() : 0);
+    for (int i = positionAtWidth + 1; i < wrapPosition && leftToRight; ++i) {
+        input.setCursorPosition(i);
+        QCOMPARE(r, input.cursorRectangle());
+        QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
+        QCOMPARE(input.positionToRectangle(i), r);
+    }
+
+    input.setCursorPosition(wrapPosition);
     r = input.cursorRectangle();
-    QCOMPARE(r.left(), 0.);
-    QVERIFY(r.bottom() >= input.height() - error);
+    if (leftToRight) {
+        QCOMPARE(r.left(), 0.);
+    } else {
+        QCOMPARE(r.left(), input.width());
+    }
+    QVERIFY(r.bottom() >= input.height());
     QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
-    QCOMPARE(input.positionToRectangle(6), r);
+    QCOMPARE(input.positionToRectangle(11), r);
 
-    for (int i = 7; i < text.length(); ++i) {
+    for (int i = wrapPosition + 1; i < text.length(); ++i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
-        QVERIFY(r.bottom() >= input.height() - error);
+        QVERIFY(r.bottom() >= input.height());
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
-    for (int i = text.length() - 2; i >= 6; --i) {
+    for (int i = text.length() - 2; i >= wrapPosition; --i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
-        QVERIFY(r.bottom() >= input.height() - error);
+        QVERIFY(r.bottom() >= input.height());
         QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
-    for (int i = 5; i >= 0; --i) {
+    input.setCursorPosition(wrapPosition - 1);
+    r = input.cursorRectangle();
+    QCOMPARE(r.top(), 0.);
+    QCOMPARE(r.left(), leftToRight ? input.width() : 0);
+    QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
+    QCOMPARE(input.positionToRectangle(10), r);
+
+    for (int i = wrapPosition - 2; i >= positionAtWidth + 1; --i) {
+        input.setCursorPosition(i);
+        QCOMPARE(r, input.cursorRectangle());
+        QCOMPARE(input.inputMethodQuery(Qt::ImCursorRectangle).toRectF(), r);
+        QCOMPARE(input.positionToRectangle(i), r);
+    }
+
+    for (int i = positionAtWidth; i >= 0; --i) {
         input.setCursorPosition(i);
         r = input.cursorRectangle();
         QCOMPARE(r.top(), 0.);
@@ -2537,32 +3083,31 @@ void tst_qquicktextinput::cursorRectangle()
         QCOMPARE(input.positionToRectangle(i), r);
     }
 
-    input.setText("Hi!");
-    input.setHAlign(QQuickTextInput::AlignRight);
+    input.setText(shortText);
+    input.setHAlign(leftToRight ? QQuickTextInput::AlignRight : QQuickTextInput::AlignLeft);
     r = input.cursorRectangle();
-    QVERIFY(r.left() < input.width() + error);
-    QVERIFY(r.right() >= input.width() - error);
+    QCOMPARE(r.left(), leftToRight ? input.width() : 0);
 }
 
 void tst_qquicktextinput::readOnly()
 {
-    QQuickView canvas(testFileUrl("readOnly.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
+    QQuickView window(testFileUrl("readOnly.qml"));
+    window.show();
+    window.requestActivateWindow();
 
-    QVERIFY(canvas.rootObject() != 0);
+    QVERIFY(window.rootObject() != 0);
 
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("myInput")));
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
 
     QVERIFY(input != 0);
     QTRY_VERIFY(input->hasActiveFocus() == true);
     QVERIFY(input->isReadOnly() == true);
     QString initial = input->text();
     for (int k=Qt::Key_0; k<=Qt::Key_Z; k++)
-        simulateKey(&canvas, k);
-    simulateKey(&canvas, Qt::Key_Return);
-    simulateKey(&canvas, Qt::Key_Space);
-    simulateKey(&canvas, Qt::Key_Escape);
+        simulateKey(&window, k);
+    simulateKey(&window, Qt::Key_Return);
+    simulateKey(&window, Qt::Key_Space);
+    simulateKey(&window, Qt::Key_Escape);
     QCOMPARE(input->text(), initial);
 
     input->setCursorPosition(3);
@@ -2573,15 +3118,14 @@ void tst_qquicktextinput::readOnly()
 
 void tst_qquicktextinput::echoMode()
 {
-    QQuickView canvas(testFileUrl("echoMode.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
+    QQuickView window(testFileUrl("echoMode.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
-    QVERIFY(canvas.rootObject() != 0);
+    QVERIFY(window.rootObject() != 0);
 
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("myInput")));
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
 
     QVERIFY(input != 0);
     QTRY_VERIFY(input->hasActiveFocus() == true);
@@ -2624,8 +3168,8 @@ void tst_qquicktextinput::echoMode()
     QCOMPARE(input->text(), initial);
     QCOMPARE(input->displayText(), QLatin1String("QQQQQQQQ"));
     QCOMPARE(input->inputMethodQuery(Qt::ImSurroundingText).toString(), QLatin1String("QQQQQQQQ"));
-    QTest::keyPress(&canvas, Qt::Key_A);//Clearing previous entry is part of PasswordEchoOnEdit
-    QTest::keyRelease(&canvas, Qt::Key_A, Qt::NoModifier ,10);
+    QTest::keyPress(&window, Qt::Key_A);//Clearing previous entry is part of PasswordEchoOnEdit
+    QTest::keyRelease(&window, Qt::Key_A, Qt::NoModifier ,10);
     QCOMPARE(input->text(), QLatin1String("a"));
     QCOMPARE(input->displayText(), QLatin1String("a"));
     QCOMPARE(input->inputMethodQuery(Qt::ImSurroundingText).toString(), QLatin1String("a"));
@@ -2643,18 +3187,24 @@ void tst_qquicktextinput::echoMode()
     QCOMPARE(input->inputMethodQuery(Qt::ImSurroundingText).toString(), initial);
 }
 
-#ifdef QT_GUI_PASSWORD_ECHO_DELAY
 void tst_qquicktextinput::passwordEchoDelay()
 {
-    QQuickView canvas(testFileUrl("echoMode.qml"));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(&canvas, qGuiApp->focusWindow());
+    int maskDelay = qGuiApp->styleHints()->passwordMaskDelay();
+    if (maskDelay <= 0)
+        QSKIP("No mask delay in use");
+    QQuickView window(testFileUrl("echoMode.qml"));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+
+    QVERIFY(window.rootObject() != 0);
 
-    QVERIFY(canvas.rootObject() != 0);
+    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(window.rootObject()->property("myInput")));
+    QVERIFY(input);
+    QVERIFY(input->hasActiveFocus());
 
-    QQuickTextInput *input = qobject_cast<QQuickTextInput *>(qvariant_cast<QObject *>(canvas.rootObject()->property("myInput")));
+    QQuickItem *cursor = input->findChild<QQuickItem *>("cursor");
+    QVERIFY(cursor);
 
     QChar fillChar = QLatin1Char('*');
 
@@ -2663,20 +3213,29 @@ void tst_qquicktextinput::passwordEchoDelay()
     input->setText(QString());
     QCOMPARE(input->displayText(), QString());
 
-    QTest::keyPress(&canvas, '0');
-    QTest::keyPress(&canvas, '1');
-    QTest::keyPress(&canvas, '2');
+    QTest::keyPress(&window, '0');
+    QTest::keyPress(&window, '1');
+    QTest::keyPress(&window, '2');
     QCOMPARE(input->displayText(), QString(2, fillChar) + QLatin1Char('2'));
-    QTest::keyPress(&canvas, '3');
-    QTest::keyPress(&canvas, '4');
+    QTest::keyPress(&window, '3');
+    QTest::keyPress(&window, '4');
     QCOMPARE(input->displayText(), QString(4, fillChar) + QLatin1Char('4'));
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
+    QTest::keyPress(&window, Qt::Key_Backspace);
     QCOMPARE(input->displayText(), QString(4, fillChar));
-    QTest::keyPress(&canvas, '4');
+    QTest::keyPress(&window, '4');
     QCOMPARE(input->displayText(), QString(4, fillChar) + QLatin1Char('4'));
-    QTest::qWait(QT_GUI_PASSWORD_ECHO_DELAY);
+    QCOMPARE(input->cursorRectangle().topLeft(), cursor->pos());
+
+    // Verify the last character entered is replaced by the fill character after a delay.
+    // Also check the cursor position is updated to accomdate a size difference between
+    // the fill character and the replaced character.
+    QSignalSpy cursorSpy(input, SIGNAL(cursorRectangleChanged()));
+    QTest::qWait(maskDelay);
     QTRY_COMPARE(input->displayText(), QString(5, fillChar));
-    QTest::keyPress(&canvas, '5');
+    QCOMPARE(cursorSpy.count(), 1);
+    QCOMPARE(input->cursorRectangle().topLeft(), cursor->pos());
+
+    QTest::keyPress(&window, '5');
     QCOMPARE(input->displayText(), QString(5, fillChar) + QLatin1Char('5'));
     input->setFocus(false);
     QVERIFY(!input->hasFocus());
@@ -2684,25 +3243,24 @@ void tst_qquicktextinput::passwordEchoDelay()
     input->setFocus(true);
     QTRY_VERIFY(input->hasFocus());
     QCOMPARE(input->displayText(), QString(6, fillChar));
-    QTest::keyPress(&canvas, '6');
+    QTest::keyPress(&window, '6');
     QCOMPARE(input->displayText(), QString(6, fillChar) + QLatin1Char('6'));
 
     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);
     QCOMPARE(input->displayText(), QString(7, fillChar) + QLatin1Char('7'));
-    QTest::keyPress(&canvas, 'a');
+    QTest::keyPress(&window, 'a');
     QCOMPARE(input->displayText(), QString(3, fillChar) + QLatin1Char('a') + QString(5, fillChar));
-    QTest::keyPress(&canvas, Qt::Key_Backspace);
+    QTest::keyPress(&window, Qt::Key_Backspace);
     QCOMPARE(input->displayText(), QString(8, fillChar));
 }
-#endif
 
 
-void tst_qquicktextinput::simulateKey(QQuickView *view, int key)
+void tst_qquicktextinput::simulateKey(QWindow *view, int key)
 {
     QKeyEvent press(QKeyEvent::KeyPress, key, 0);
     QKeyEvent release(QKeyEvent::KeyRelease, key, 0);
@@ -2712,6 +3270,91 @@ void tst_qquicktextinput::simulateKey(QQuickView *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);
+
+    QQuickWindow window;
+    window.resize(100, 50);
+    textInputObject->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+
+    QCOMPARE(textInputObject->hasFocus(), false);
+    QCOMPARE(textInputObject->hasActiveFocus(), false);
+
+    QPoint centerPoint(window.width()/2, window.height()/2);
+    Qt::KeyboardModifiers noModifiers = 0;
+    QTest::mousePress(&window, 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(&window, 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(&window, Qt::LeftButton, noModifiers, centerPoint);
+    QGuiApplication::processEvents();
+    QCOMPARE(textInputObject->hasFocus(), false);
+    QCOMPARE(textInputObject->hasActiveFocus(), false);
+    QCOMPARE(focusSpy.count(), 2);
+    QCOMPARE(activeFocusSpy.count(), 2);
+    QTest::mouseRelease(&window, 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(&window, 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(&window, Qt::LeftButton, noModifiers, centerPoint);
+}
+
 void tst_qquicktextinput::openInputPanel()
 {
     PlatformInputContext platformInputContext;
@@ -2721,8 +3364,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<QQuickTextInput *>(view.rootObject());
     QVERIFY(input);
@@ -2731,7 +3373,7 @@ void tst_qquicktextinput::openInputPanel()
     QVERIFY(input->focusOnPress());
     QVERIFY(!input->hasActiveFocus());
     QVERIFY(qApp->focusObject() != input);
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
 
     // input panel should open on focus
     QPoint centerPoint(view.width()/2, view.height()/2);
@@ -2740,16 +3382,16 @@ void tst_qquicktextinput::openInputPanel()
     QGuiApplication::processEvents();
     QVERIFY(input->hasActiveFocus());
     QCOMPARE(qApp->focusObject(), input);
-    QCOMPARE(qApp->inputMethod()->visible(), true);
+    QCOMPARE(qApp->inputMethod()->isVisible(), true);
     QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
 
     // input panel should be re-opened when pressing already focused TextInput
     qApp->inputMethod()->hide();
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
     QVERIFY(input->hasActiveFocus());
     QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
     QGuiApplication::processEvents();
-    QCOMPARE(qApp->inputMethod()->visible(), true);
+    QCOMPARE(qApp->inputMethod()->isVisible(), true);
     QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
 
     // input panel should stay visible if focus is lost to another text inputor
@@ -2758,13 +3400,13 @@ void tst_qquicktextinput::openInputPanel()
     anotherInput.componentComplete();
     anotherInput.setParentItem(view.rootObject());
     anotherInput.setFocus(true);
-    QCOMPARE(qApp->inputMethod()->visible(), true);
+    QCOMPARE(qApp->inputMethod()->isVisible(), true);
     QCOMPARE(qApp->focusObject(), qobject_cast<QObject*>(&anotherInput));
     QCOMPARE(inputPanelVisibilitySpy.count(), 0);
 
     anotherInput.setFocus(false);
     QVERIFY(qApp->focusObject() != &anotherInput);
-    QCOMPARE(view.activeFocusItem(), view.rootItem());
+    QCOMPARE(view.activeFocusItem(), view.contentItem());
     anotherInput.setFocus(true);
 
     qApp->inputMethod()->hide();
@@ -2772,28 +3414,20 @@ void tst_qquicktextinput::openInputPanel()
     // input panel should not be opened if TextInput is read only
     input->setReadOnly(true);
     input->setFocus(true);
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
     QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
     QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
     QGuiApplication::processEvents();
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
 
     // input panel should not be opened if focusOnPress is set to false
     input->setFocusOnPress(false);
     input->setFocus(false);
     input->setFocus(true);
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
     QTest::mousePress(&view, Qt::LeftButton, noModifiers, centerPoint);
     QTest::mouseRelease(&view, Qt::LeftButton, noModifiers, centerPoint);
-    QCOMPARE(qApp->inputMethod()->visible(), false);
-
-    // input panel should open when openSoftwareInputPanel is called
-    input->openSoftwareInputPanel();
-    QCOMPARE(qApp->inputMethod()->visible(), true);
-
-    // input panel should close when closeSoftwareInputPanel is called
-    input->closeSoftwareInputPanel();
-    QCOMPARE(qApp->inputMethod()->visible(), false);
+    QCOMPARE(qApp->inputMethod()->isVisible(), false);
 }
 
 class MyTextInput : public QQuickTextInput
@@ -2816,10 +3450,10 @@ void tst_qquicktextinput::setHAlignClearCache()
     QQuickView view;
     MyTextInput input;
     input.setText("Hello world");
-    input.setParentItem(view.rootItem());
+    input.setParentItem(view.contentItem());
     view.show();
     view.requestActivateWindow();
-    QTest::qWaitForWindowShown(&view);
+    QTest::qWaitForWindowActive(&view);
 #ifdef Q_OS_MAC
     QEXPECT_FAIL("", "QTBUG-23485", Abort);
 #endif
@@ -2836,13 +3470,14 @@ void tst_qquicktextinput::focusOutClearSelection()
     QQuickTextInput input2;
     input.setText(QLatin1String("Hello world"));
     input.setFocus(true);
-    input2.setParentItem(view.rootItem());
-    input.setParentItem(view.rootItem());
+    input2.setParentItem(view.contentItem());
+    input.setParentItem(view.contentItem());
     input.componentComplete();
     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"));
@@ -2888,13 +3523,20 @@ void tst_qquicktextinput::contentSize()
     QVERIFY(textObject->contentWidth() > textObject->width());
     QVERIFY(textObject->contentHeight() > textObject->height());
     QCOMPARE(spy.count(), 3);
+
+    textObject->setText("The quick red fox jumped over the lazy brown dog");
+    for (int w = 60; w < 120; ++w) {
+        textObject->setWidth(w);
+        QVERIFY(textObject->contentWidth() <= textObject->width());
+        QVERIFY(textObject->contentHeight() > textObject->height());
+    }
 }
 
-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::Attribute(QInputMethodEvent::Cursor, cursor, text.length(), QVariant()));
-    QCoreApplication::sendEvent(qGuiApp->focusObject(), &event);
+    QCoreApplication::sendEvent(item, &event);
 }
 
 void tst_qquicktextinput::preeditAutoScroll()
@@ -2904,8 +3546,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<QQuickTextInput *>(view.rootObject());
     QVERIFY(input);
     QVERIFY(input->hasActiveFocus());
@@ -2916,14 +3557,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<int>(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<int>(input, QString("positionAt(%1)").arg(0)), 0);
     QCOMPARE(evaluate<int>(input, QString("positionAt(%1)").arg(input->width())), 5);
     QCOMPARE(cursorRectangleSpy.count(), ++cursorRectangleChanges);
@@ -2943,7 +3584,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);
@@ -2951,7 +3592,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);
@@ -2961,25 +3602,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<int>(input, QString("positionAt(%1)").arg(0)), 0);
     QCOMPARE(evaluate<int>(input, QString("positionAt(%1)").arg(input->width())), 5);
 }
@@ -2991,23 +3632,28 @@ 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<QQuickTextInput *>(view.rootObject());
     QVERIFY(input);
+    QVERIFY(input->hasActiveFocus());
+
+    QQuickItem *cursor = input->findChild<QQuickItem *>("cursor");
+    QVERIFY(cursor);
 
-    QRect currentRect;
+    QRectF currentRect;
 
     QInputMethodQueryEvent query(Qt::ImCursorRectangle);
-    QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
-    QRect previousRect = query.value(Qt::ImCursorRectangle).toRect();
+    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);
-    currentRect = query.value(Qt::ImCursorRectangle).toRect();
+    sendPreeditText(input, preeditText, 0);
+    QCoreApplication::sendEvent(input, &query);
+    currentRect = query.value(Qt::ImCursorRectangle).toRectF();
     QCOMPARE(currentRect, previousRect);
+    QCOMPARE(input->cursorRectangle(), currentRect);
+    QCOMPARE(cursor->pos(), currentRect.topLeft());
 
     QSignalSpy inputSpy(input, SIGNAL(cursorRectangleChanged()));
     QSignalSpy panelSpy(qGuiApp->inputMethod(), SIGNAL(cursorRectangleChanged()));
@@ -3015,25 +3661,42 @@ 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);
-        currentRect = query.value(Qt::ImCursorRectangle).toRect();
+        sendPreeditText(input, preeditText, i);
+        QCoreApplication::sendEvent(input, &query);
+        currentRect = query.value(Qt::ImCursorRectangle).toRectF();
         QVERIFY(previousRect.left() < currentRect.left());
+        QCOMPARE(input->cursorRectangle(), currentRect);
+        QCOMPARE(cursor->pos(), currentRect.topLeft());
         QVERIFY(inputSpy.count() > 0); inputSpy.clear();
         QVERIFY(panelSpy.count() > 0); panelSpy.clear();
         previousRect = currentRect;
     }
 
+    // Verify that if the cursor rectangle is updated if the pre-edit text changes
+    // but the (non-zero) cursor position is the same.
+    inputSpy.clear();
+    panelSpy.clear();
+    sendPreeditText(input, "wwwww", 5);
+    QCoreApplication::sendEvent(input, &query);
+    currentRect = query.value(Qt::ImCursorRectangle).toRectF();
+    QCOMPARE(input->cursorRectangle(), currentRect);
+    QCOMPARE(cursor->pos(), currentRect.topLeft());
+    QCOMPARE(inputSpy.count(), 1);
+    QCOMPARE(panelSpy.count(), 1);
+
     // 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.
-    sendPreeditText(preeditText, 0);
-    QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
-    QCoreApplication::sendEvent(qGuiApp->focusObject(), &imEvent);
-    QCoreApplication::sendEvent(qGuiApp->focusObject(), &query);
-    currentRect = query.value(Qt::ImCursorRectangle).toRect();
+    inputSpy.clear();
+    panelSpy.clear();
+    {   QInputMethodEvent imEvent(preeditText, QList<QInputMethodEvent::Attribute>());
+        QCoreApplication::sendEvent(input, &imEvent); }
+    QCoreApplication::sendEvent(input, &query);
+    currentRect = query.value(Qt::ImCursorRectangle).toRectF();
     QCOMPARE(currentRect, previousRect);
-    QVERIFY(inputSpy.count() > 0);
-    QVERIFY(panelSpy.count() > 0);
+    QCOMPARE(input->cursorRectangle(), currentRect);
+    QCOMPARE(cursor->pos(), currentRect.topLeft());
+    QCOMPARE(inputSpy.count(), 1);
+    QCOMPARE(panelSpy.count(), 1);
 }
 
 void tst_qquicktextinput::inputContextMouseHandler()
@@ -3052,8 +3715,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());
@@ -3089,10 +3751,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<QQuickTextInput *>(view.rootObject());
     QVERIFY(input);
+    QVERIFY(input->hasActiveFocus());
     QSignalSpy spy(input, SIGNAL(inputMethodComposingChanged()));
 
     QCOMPARE(input->isInputMethodComposing(), false);
@@ -3115,6 +3777,75 @@ void tst_qquicktextinput::inputMethodComposing()
     }
     QCOMPARE(input->isInputMethodComposing(), false);
     QCOMPARE(spy.count(), 2);
+
+    // Changing the text while not composing doesn't alter the composing state.
+    input->setText(text.mid(0, 16));
+    QCOMPARE(input->isInputMethodComposing(), false);
+    QCOMPARE(spy.count(), 2);
+
+    {
+        QInputMethodEvent event(text.mid(16), QList<QInputMethodEvent::Attribute>());
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), true);
+    QCOMPARE(spy.count(), 3);
+
+    // Changing the text while composing cancels composition.
+    input->setText(text.mid(0, 12));
+    QCOMPARE(input->isInputMethodComposing(), false);
+    QCOMPARE(spy.count(), 4);
+
+    {   // Preedit cursor positioned outside (empty) preedit; composing.
+        QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, -2, 1, QVariant()));
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), true);
+    QCOMPARE(spy.count(), 5);
+
+
+    {   // Cursor hidden; composing
+        QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 0, QVariant()));
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), true);
+    QCOMPARE(spy.count(), 5);
+
+    {   // Default cursor attributes; composing.
+        QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, 0, 1, QVariant()));
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), true);
+    QCOMPARE(spy.count(), 5);
+
+    {   // Selections are persisted: not composing
+        QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::Selection, -5, 4, QVariant()));
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), false);
+    QCOMPARE(spy.count(), 6);
+
+    input->setCursorPosition(12);
+
+    {   // Formatting applied; composing.
+        QTextCharFormat format;
+        format.setUnderlineStyle(QTextCharFormat::SingleUnderline);
+        QInputMethodEvent event(QString(), QList<QInputMethodEvent::Attribute>()
+                << QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, -5, 4, format));
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), true);
+    QCOMPARE(spy.count(), 7);
+
+    {
+        QInputMethodEvent event;
+        QGuiApplication::sendEvent(input, &event);
+    }
+    QCOMPARE(input->isInputMethodComposing(), false);
+    QCOMPARE(spy.count(), 8);
 }
 
 void tst_qquicktextinput::inputMethodUpdate()
@@ -3126,10 +3857,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<QQuickTextInput *>(view.rootObject());
     QVERIFY(input);
+    QVERIFY(input->hasActiveFocus());
 
     // text change even without cursor position change needs to trigger update
     input->setText("test");
@@ -3204,9 +3935,9 @@ void tst_qquicktextinput::inputMethodUpdate()
 
 void tst_qquicktextinput::cursorRectangleSize()
 {
-    QQuickView *canvas = new QQuickView(testFileUrl("positionAt.qml"));
-    QVERIFY(canvas->rootObject() != 0);
-    QQuickTextInput *textInput = qobject_cast<QQuickTextInput *>(canvas->rootObject());
+    QQuickView *window = new QQuickView(testFileUrl("positionAt.qml"));
+    QVERIFY(window->rootObject() != 0);
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput *>(window->rootObject());
 
     // make sure cursor rectangle is not at (0,0)
     textInput->setX(10);
@@ -3214,13 +3945,13 @@ void tst_qquicktextinput::cursorRectangleSize()
     textInput->setCursorPosition(3);
     QVERIFY(textInput != 0);
     textInput->setFocus(true);
-    canvas->show();
-    canvas->requestActivateWindow();
-    QTest::qWaitForWindowShown(canvas);
-    QTRY_VERIFY(qApp->focusObject());
+    window->show();
+    window->requestActivateWindow();
+    QTest::qWaitForWindowActive(window);
+    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();
@@ -3232,14 +3963,14 @@ void tst_qquicktextinput::cursorRectangleSize()
     // item cursor rectangle and positionToRectangle calculations match
     QCOMPARE(cursorRectFromItem, cursorRectFromPositionToRectangle);
 
-    // item-canvas transform and input item transform match
-    QCOMPARE(QQuickItemPrivate::get(textInput)->itemToCanvasTransform(), qApp->inputMethod()->inputItemTransform());
+    // item-window transform and input item transform match
+    QCOMPARE(QQuickItemPrivate::get(textInput)->itemToWindowTransform(), qApp->inputMethod()->inputItemTransform());
 
     // input panel cursorRectangle property and tranformed item cursor rectangle match
-    QRectF sceneCursorRect = QQuickItemPrivate::get(textInput)->itemToCanvasTransform().mapRect(cursorRectFromItem);
+    QRectF sceneCursorRect = QQuickItemPrivate::get(textInput)->itemToWindowTransform().mapRect(cursorRectFromItem);
     QCOMPARE(sceneCursorRect, qApp->inputMethod()->cursorRectangle());
 
-    delete canvas;
+    delete window;
 }
 
 void tst_qquicktextinput::tripleClickSelectsAll()
@@ -3248,9 +3979,7 @@ void tst_qquicktextinput::tripleClickSelectsAll()
     QQuickView view(QUrl::fromLocalFile(qmlfile));
     view.show();
     view.requestActivateWindow();
-    QTest::qWaitForWindowShown(&view);
-
-    QTRY_COMPARE(&view, qGuiApp->focusWindow());
+    QTest::qWaitForWindowActive(&view);
 
     QQuickTextInput* input = qobject_cast<QQuickTextInput*>(view.rootObject());
     QVERIFY(input);
@@ -4084,63 +4813,135 @@ void tst_qquicktextinput::keySequence_data()
     QTest::addColumn<int>("cursorPosition");
     QTest::addColumn<QString>("expectedText");
     QTest::addColumn<QString>("selectedText");
+    QTest::addColumn<QQuickTextInput::EchoMode>("echoMode");
+    QTest::addColumn<Qt::Key>("layoutDirection");
 
     // standard[0] == "the [4]quick [10]brown [16]fox [20]jumped [27]over [32]the [36]lazy [41]dog"
 
     QTest::newRow("select all")
             << standard.at(0) << QKeySequence(QKeySequence::SelectAll) << 0 << 0
-            << 44 << standard.at(0) << standard.at(0);
+            << 44 << standard.at(0) << standard.at(0)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("select start of line")
+            << standard.at(0) << QKeySequence(QKeySequence::SelectStartOfLine) << 5 << 5
+            << 0 << standard.at(0) << standard.at(0).mid(0, 5)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("select start of block")
+            << standard.at(0) << QKeySequence(QKeySequence::SelectStartOfBlock) << 5 << 5
+            << 0 << standard.at(0) << standard.at(0).mid(0, 5)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("select end of line")
             << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfLine) << 5 << 5
-            << 44 << standard.at(0) << standard.at(0).mid(5);
+            << 44 << standard.at(0) << standard.at(0).mid(5)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("select end of document") // ### Not handled.
             << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfDocument) << 3 << 3
-            << 3 << standard.at(0) << QString();
+            << 3 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("select end of block")
             << standard.at(0) << QKeySequence(QKeySequence::SelectEndOfBlock) << 18 << 18
-            << 44 << standard.at(0) << standard.at(0).mid(18);
+            << 44 << standard.at(0) << standard.at(0).mid(18)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("delete end of line")
             << standard.at(0) << QKeySequence(QKeySequence::DeleteEndOfLine) << 24 << 24
-            << 24 << standard.at(0).mid(0, 24) << QString();
+            << 24 << standard.at(0).mid(0, 24) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("move to start of line")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToStartOfLine) << 31 << 31
-            << 0 << standard.at(0) << QString();
+            << 0 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("move to start of block")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToStartOfBlock) << 25 << 25
-            << 0 << standard.at(0) << QString();
+            << 0 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("move to next char")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToNextChar) << 12 << 12
-            << 13 << standard.at(0) << QString();
-    QTest::newRow("move to previous char")
+            << 13 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("move to previous char (ltr)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 3
+            << 2 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("move to previous char (rtl)")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 3
-            << 2 << standard.at(0) << QString();
-    QTest::newRow("select next char")
+            << 4 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_R;
+    QTest::newRow("move to previous char with selection")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousChar) << 3 << 7
+            << 3 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("select next char (ltr)")
+            << standard.at(0) << QKeySequence(QKeySequence::SelectNextChar) << 23 << 23
+            << 24 << standard.at(0) << standard.at(0).mid(23, 1)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("select next char (rtl)")
             << standard.at(0) << QKeySequence(QKeySequence::SelectNextChar) << 23 << 23
-            << 24 << standard.at(0) << standard.at(0).mid(23, 1);
-    QTest::newRow("select previous char")
+            << 22 << standard.at(0) << standard.at(0).mid(22, 1)
+            << QQuickTextInput::Normal << Qt::Key_Direction_R;
+    QTest::newRow("select previous char (ltr)")
             << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousChar) << 19 << 19
-            << 18 << standard.at(0) << standard.at(0).mid(18, 1);
-    QTest::newRow("move to next word")
+            << 18 << standard.at(0) << standard.at(0).mid(18, 1)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("select previous char (rtl)")
+            << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousChar) << 19 << 19
+            << 20 << standard.at(0) << standard.at(0).mid(19, 1)
+            << QQuickTextInput::Normal << Qt::Key_Direction_R;
+    QTest::newRow("move to next word (ltr)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
+            << 10 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("move to next word (rtl)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
+            << 4 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_R;
+    QTest::newRow("move to next word (password,ltr)")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
-            << 10 << standard.at(0) << QString();
-    QTest::newRow("move to previous word")
+            << 44 << standard.at(0) << QString()
+            << QQuickTextInput::Password << Qt::Key_Direction_L;
+    QTest::newRow("move to next word (password,rtl)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToNextWord) << 7 << 7
+            << 0 << standard.at(0) << QString()
+            << QQuickTextInput::Password << Qt::Key_Direction_R;
+    QTest::newRow("move to previous word (ltr)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
+            << 4 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
+    QTest::newRow("move to previous word (rlt)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
+            << 10 << standard.at(0) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_R;
+    QTest::newRow("move to previous word (password,ltr)")
             << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
-            << 4 << standard.at(0) << QString();
+            << 0 << standard.at(0) << QString()
+            << QQuickTextInput::Password << Qt::Key_Direction_L;
+    QTest::newRow("move to previous word (password,rtl)")
+            << standard.at(0) << QKeySequence(QKeySequence::MoveToPreviousWord) << 7 << 7
+            << 44 << standard.at(0) << QString()
+            << QQuickTextInput::Password << Qt::Key_Direction_R;
+    QTest::newRow("select next word")
+            << standard.at(0) << QKeySequence(QKeySequence::SelectNextWord) << 11 << 11
+            << 16 << standard.at(0) << standard.at(0).mid(11, 5)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("select previous word")
             << standard.at(0) << QKeySequence(QKeySequence::SelectPreviousWord) << 11 << 11
-            << 10 << standard.at(0) << standard.at(0).mid(10, 1);
+            << 10 << standard.at(0) << standard.at(0).mid(10, 1)
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("delete (selection)")
             << standard.at(0) << QKeySequence(QKeySequence::Delete) << 12 << 15
-            << 12 << (standard.at(0).mid(0, 12) + standard.at(0).mid(15)) << QString();
+            << 12 << (standard.at(0).mid(0, 12) + standard.at(0).mid(15)) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("delete (no selection)")
             << standard.at(0) << QKeySequence(QKeySequence::Delete) << 15 << 15
-            << 15 << (standard.at(0).mid(0, 15) + standard.at(0).mid(16)) << QString();
+            << 15 << (standard.at(0).mid(0, 15) + standard.at(0).mid(16)) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("delete end of word")
             << standard.at(0) << QKeySequence(QKeySequence::DeleteEndOfWord) << 24 << 24
-            << 24 << (standard.at(0).mid(0, 24) + standard.at(0).mid(27)) << QString();
+            << 24 << (standard.at(0).mid(0, 24) + standard.at(0).mid(27)) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
     QTest::newRow("delete start of word")
             << standard.at(0) << QKeySequence(QKeySequence::DeleteStartOfWord) << 7 << 7
-            << 4 << (standard.at(0).mid(0, 4) + standard.at(0).mid(7)) << QString();
+            << 4 << (standard.at(0).mid(0, 4) + standard.at(0).mid(7)) << QString()
+            << QQuickTextInput::Normal << Qt::Key_Direction_L;
 }
 
 void tst_qquicktextinput::keySequence()
@@ -4152,6 +4953,8 @@ void tst_qquicktextinput::keySequence()
     QFETCH(int, cursorPosition);
     QFETCH(QString, expectedText);
     QFETCH(QString, selectedText);
+    QFETCH(QQuickTextInput::EchoMode, echoMode);
+    QFETCH(Qt::Key, layoutDirection);
 
     if (sequence.isEmpty()) {
         QSKIP("Key sequence is undefined");
@@ -4162,17 +4965,20 @@ void tst_qquicktextinput::keySequence()
     textInputComponent.setData(componentStr.toLatin1(), QUrl());
     QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
     QVERIFY(textInput != 0);
+    textInput->setEchoMode(echoMode);
+
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(textInput->hasActiveFocus());
 
-    QQuickCanvas canvas;
-    textInput->setParentItem(canvas.rootItem());
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+    simulateKey(&window, layoutDirection);
 
     textInput->select(selectionStart, selectionEnd);
 
-    simulateKeys(&canvas, sequence);
+    simulateKeys(&window, sequence);
 
     QCOMPARE(textInput->cursorPosition(), cursorPosition);
     QCOMPARE(textInput->text(), expectedText);
@@ -4322,12 +5128,12 @@ void tst_qquicktextinput::undo()
     QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
     QVERIFY(textInput != 0);
 
-    QQuickCanvas canvas;
-    textInput->setParentItem(canvas.rootItem());
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(textInput->hasActiveFocus());
 
     QVERIFY(!textInput->canUndo());
 
@@ -4349,7 +5155,7 @@ void tst_qquicktextinput::undo()
         }
 
         for (int j = 0; j < insertString.at(i).length(); j++)
-            QTest::keyClick(&canvas, insertString.at(i).at(j).toLatin1());
+            QTest::keyClick(&window, insertString.at(i).at(j).toLatin1());
     }
 
     QCOMPARE(spy.count(), 1);
@@ -4408,13 +5214,13 @@ void tst_qquicktextinput::redo()
     QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
     QVERIFY(textInput != 0);
 
-    QQuickCanvas canvas;
-    textInput->setParentItem(canvas.rootItem());
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
 
+    QVERIFY(textInput->hasActiveFocus());
     QVERIFY(!textInput->canUndo());
     QVERIFY(!textInput->canRedo());
 
@@ -4426,7 +5232,7 @@ void tst_qquicktextinput::redo()
         if (insertIndex[i] > -1)
             textInput->setCursorPosition(insertIndex[i]);
         for (int j = 0; j < insertString.at(i).length(); j++)
-            QTest::keyClick(&canvas, insertString.at(i).at(j).toLatin1());
+            QTest::keyClick(&window, insertString.at(i).at(j).toLatin1());
         QVERIFY(textInput->canUndo());
         QVERIFY(!textInput->canRedo());
     }
@@ -4504,7 +5310,7 @@ void tst_qquicktextinput::undo_keypressevents_data()
                 << QKeySequence::MoveToStartOfLine
                 // selecting 'AB'
                 << (Qt::Key_Right | Qt::ShiftModifier) << (Qt::Key_Right | Qt::ShiftModifier)
-                << Qt::Key_Delete
+                << Qt::Key_Backspace
                 << QKeySequence::Undo
                 << Qt::Key_Right
                 << (Qt::Key_Right | Qt::ShiftModifier) << (Qt::Key_Right | Qt::ShiftModifier)
@@ -4579,10 +5385,153 @@ void tst_qquicktextinput::undo_keypressevents_data()
              << "ABC";
 
         expectedString << "ABC";
-        // for versions previous to 3.2 we overwrite needed two undo operations
         expectedString << "123";
 
         QTest::newRow("Inserts,moving,selection and overwriting") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        // inserting '123'
+        keys << "123"
+                << QKeySequence::Undo
+                << QKeySequence::Redo;
+
+        expectedString << "123";
+        expectedString << QString();
+
+        QTest::newRow("Insert,undo,redo") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << (Qt::Key_Backspace | Qt::ControlModifier)
+             << QKeySequence::Undo
+             << QKeySequence::Redo
+             << "hello";
+
+        expectedString
+                << "hello hello"
+                << "hello "
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,delete previous word,undo,redo,insert") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << QKeySequence::SelectPreviousWord
+             << (Qt::Key_Backspace)
+             << QKeySequence::Undo
+             << "hello";
+
+        expectedString
+                << "hello hello"
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,select previous word,remove,undo,insert") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << QKeySequence::DeleteStartOfWord
+             << QKeySequence::Undo
+             << "hello";
+
+        expectedString
+                << "hello worldhello"
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,delete previous word,undo,insert") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << QKeySequence::MoveToPreviousWord
+             << QKeySequence::DeleteEndOfWord
+             << QKeySequence::Undo
+             << "hello";
+
+        expectedString
+                << "hello helloworld"
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,move,delete next word,undo,insert") << keys << expectedString;
+    }
+    if (!QKeySequence(QKeySequence::DeleteEndOfLine).isEmpty()) {   // X11 only.
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << QKeySequence::MoveToStartOfLine
+             << Qt::Key_Right
+             << QKeySequence::DeleteEndOfLine
+             << QKeySequence::Undo
+             << "hello";
+
+        expectedString
+                << "hhelloello world"
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,move,delete end of line,undo,insert") << keys << expectedString;
+    } {
+        KeyList keys;
+        QStringList expectedString;
+
+        keys << "hello world"
+             << QKeySequence::MoveToPreviousWord
+             << (Qt::Key_Left | Qt::ShiftModifier)
+             << (Qt::Key_Left | Qt::ShiftModifier)
+             << QKeySequence::DeleteEndOfWord
+             << QKeySequence::Undo
+             << "hello";
+
+        expectedString
+                << "hellhelloworld"
+                << "hello world"
+                << QString();
+
+        QTest::newRow("Insert,move,select,delete next word,undo,insert") << keys << expectedString;
+    }
+
+    bool canCopyPaste = PlatformQuirks::isClipboardAvailable();
+
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Cut)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                // TextEdit: ""
+                << "123";
+        QTest::newRow("Cut,paste") << keys << expectedString;
+    }
+    if (canCopyPaste) {
+        KeyList keys;
+        keys    << "123"
+                << QKeySequence(QKeySequence::SelectStartOfLine)
+                << QKeySequence(QKeySequence::Copy)
+                << "ABC"
+                << QKeySequence(QKeySequence::Paste);
+        QStringList expectedString = QStringList()
+                << "ABC123"
+                << "ABC"
+                // TextEdit: "A"
+                << "123";
+        QTest::newRow("Copy,paste") << keys << expectedString;
     }
 }
 
@@ -4597,14 +5546,14 @@ void tst_qquicktextinput::undo_keypressevents()
     QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
     QVERIFY(textInput != 0);
 
-    QQuickCanvas canvas;
-    textInput->setParentItem(canvas.rootItem());
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QTRY_COMPARE(QGuiApplication::activeWindow(), &canvas);
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(textInput->hasActiveFocus());
 
-    simulateKeys(&canvas, keys);
+    simulateKeys(&window, keys);
 
     for (int i = 0; i < expectedString.size(); ++i) {
         QCOMPARE(textInput->text() , expectedString[i]);
@@ -4613,40 +5562,77 @@ void tst_qquicktextinput::undo_keypressevents()
     QVERIFY(textInput->text().isEmpty());
 }
 
+void tst_qquicktextinput::backspaceSurrogatePairs()
+{
+    // Test backspace, and delete remove both characters in a surrogate pair.
+    static const quint16 textData[] = { 0xd800, 0xdf00, 0xd800, 0xdf01, 0xd800, 0xdf02, 0xd800, 0xdf03, 0xd800, 0xdf04 };
+    const QString text = QString::fromUtf16(textData, lengthOf(textData));
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { focus: true }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+    textInput->setText(text);
+    textInput->setCursorPosition(text.length());
+
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QVERIFY(QTest::qWaitForWindowActive(&window));
+    QCOMPARE(QGuiApplication::focusWindow(), &window);
+
+    for (int i = text.length(); i >= 0; i -= 2) {
+        QCOMPARE(textInput->text(), text.mid(0, i));
+        QTest::keyClick(&window, Qt::Key_Backspace, Qt::NoModifier);
+    }
+    QCOMPARE(textInput->text(), QString());
+
+    textInput->setText(text);
+    textInput->setCursorPosition(0);
+
+    for (int i = 0; i < text.length(); i += 2) {
+        QCOMPARE(textInput->text(), text.mid(i));
+        QTest::keyClick(&window, Qt::Key_Delete, Qt::NoModifier);
+    }
+    QCOMPARE(textInput->text(), QString());
+}
+
 void tst_qquicktextinput::QTBUG_19956()
 {
     QFETCH(QString, url);
 
-    QQuickView canvas(testFileUrl(url));
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *input = qobject_cast<QQuickTextInput*>(canvas.rootObject());
+    QQuickView window(testFileUrl(url));
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *input = qobject_cast<QQuickTextInput*>(window.rootObject());
     QVERIFY(input);
     input->setFocus(true);
     QVERIFY(input->hasActiveFocus());
 
-    QCOMPARE(canvas.rootObject()->property("topvalue").toInt(), 30);
-    QCOMPARE(canvas.rootObject()->property("bottomvalue").toInt(), 10);
-    QCOMPARE(canvas.rootObject()->property("text").toString(), QString("20"));
-    QVERIFY(canvas.rootObject()->property("acceptableInput").toBool());
+    QCOMPARE(window.rootObject()->property("topvalue").toInt(), 30);
+    QCOMPARE(window.rootObject()->property("bottomvalue").toInt(), 10);
+    QCOMPARE(window.rootObject()->property("text").toString(), QString("20"));
+    QVERIFY(window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("topvalue", 15);
-    QCOMPARE(canvas.rootObject()->property("topvalue").toInt(), 15);
-    QVERIFY(!canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("topvalue", 15);
+    QCOMPARE(window.rootObject()->property("topvalue").toInt(), 15);
+    QVERIFY(!window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("topvalue", 25);
-    QCOMPARE(canvas.rootObject()->property("topvalue").toInt(), 25);
-    QVERIFY(canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("topvalue", 25);
+    QCOMPARE(window.rootObject()->property("topvalue").toInt(), 25);
+    QVERIFY(window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("bottomvalue", 21);
-    QCOMPARE(canvas.rootObject()->property("bottomvalue").toInt(), 21);
-    QVERIFY(!canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("bottomvalue", 21);
+    QCOMPARE(window.rootObject()->property("bottomvalue").toInt(), 21);
+    QVERIFY(!window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("bottomvalue", 10);
-    QCOMPARE(canvas.rootObject()->property("bottomvalue").toInt(), 10);
-    QVERIFY(canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("bottomvalue", 10);
+    QCOMPARE(window.rootObject()->property("bottomvalue").toInt(), 10);
+    QVERIFY(window.rootObject()->property("acceptableInput").toBool());
 }
 
 void tst_qquicktextinput::QTBUG_19956_regexp()
@@ -4656,28 +5642,80 @@ void tst_qquicktextinput::QTBUG_19956_regexp()
     QString warning = url.toString() + ":11: Unable to assign [undefined] to QRegExp";
     QTest::ignoreMessage(QtWarningMsg, qPrintable(warning));
 
-    QQuickView canvas(url);
-    canvas.show();
-    canvas.requestActivateWindow();
-    QTest::qWaitForWindowShown(&canvas);
-    QVERIFY(canvas.rootObject() != 0);
-    QQuickTextInput *input = qobject_cast<QQuickTextInput*>(canvas.rootObject());
+    QQuickView window(url);
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(window.rootObject() != 0);
+    QQuickTextInput *input = qobject_cast<QQuickTextInput*>(window.rootObject());
     QVERIFY(input);
     input->setFocus(true);
     QVERIFY(input->hasActiveFocus());
 
-    canvas.rootObject()->setProperty("regexvalue", QRegExp("abc"));
-    QCOMPARE(canvas.rootObject()->property("regexvalue").toRegExp(), QRegExp("abc"));
-    QCOMPARE(canvas.rootObject()->property("text").toString(), QString("abc"));
-    QVERIFY(canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("regexvalue", QRegExp("abc"));
+    QCOMPARE(window.rootObject()->property("regexvalue").toRegExp(), QRegExp("abc"));
+    QCOMPARE(window.rootObject()->property("text").toString(), QString("abc"));
+    QVERIFY(window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("regexvalue", QRegExp("abcd"));
-    QCOMPARE(canvas.rootObject()->property("regexvalue").toRegExp(), QRegExp("abcd"));
-    QVERIFY(!canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("regexvalue", QRegExp("abcd"));
+    QCOMPARE(window.rootObject()->property("regexvalue").toRegExp(), QRegExp("abcd"));
+    QVERIFY(!window.rootObject()->property("acceptableInput").toBool());
 
-    canvas.rootObject()->setProperty("regexvalue", QRegExp("abc"));
-    QCOMPARE(canvas.rootObject()->property("regexvalue").toRegExp(), QRegExp("abc"));
-    QVERIFY(canvas.rootObject()->property("acceptableInput").toBool());
+    window.rootObject()->setProperty("regexvalue", QRegExp("abc"));
+    QCOMPARE(window.rootObject()->property("regexvalue").toRegExp(), QRegExp("abc"));
+    QVERIFY(window.rootObject()->property("acceptableInput").toBool());
+}
+
+void tst_qquicktextinput::implicitSize_data()
+{
+    QTest::addColumn<QString>("text");
+    QTest::addColumn<QString>("wrap");
+    QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "TextInput.NoWrap";
+    QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "TextInput.Wrap";
+}
+
+void tst_qquicktextinput::implicitSize()
+{
+    QFETCH(QString, text);
+    QFETCH(QString, wrap);
+    QString componentStr = "import QtQuick 2.0\nTextInput { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
+    QQmlComponent textComponent(&engine);
+    textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
+    QQuickTextInput *textObject = qobject_cast<QQuickTextInput*>(textComponent.create());
+
+    QVERIFY(textObject->width() < textObject->implicitWidth());
+    QVERIFY(textObject->height() == textObject->implicitHeight());
+
+    textObject->resetWidth();
+    QVERIFY(textObject->width() == textObject->implicitWidth());
+    QVERIFY(textObject->height() == textObject->implicitHeight());
+}
+
+void tst_qquicktextinput::implicitSizeBinding_data()
+{
+    implicitSize_data();
+}
+
+void tst_qquicktextinput::implicitSizeBinding()
+{
+    QFETCH(QString, text);
+    QFETCH(QString, wrap);
+    QString componentStr = "import QtQuick 2.0\nTextInput { text: \"" + text + "\"; width: implicitWidth; height: implicitHeight; wrapMode: " + wrap + " }";
+    QQmlComponent textComponent(&engine);
+    textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
+    QScopedPointer<QObject> object(textComponent.create());
+    QQuickTextInput *textObject = qobject_cast<QQuickTextInput *>(object.data());
+
+    QCOMPARE(textObject->width(), textObject->implicitWidth());
+    QCOMPARE(textObject->height(), textObject->implicitHeight());
+
+    textObject->resetWidth();
+    QCOMPARE(textObject->width(), textObject->implicitWidth());
+    QCOMPARE(textObject->height(), textObject->implicitHeight());
+
+    textObject->resetHeight();
+    QCOMPARE(textObject->width(), textObject->implicitWidth());
+    QCOMPARE(textObject->height(), textObject->implicitHeight());
 }
 
 
@@ -4693,6 +5731,468 @@ void tst_qquicktextinput::negativeDimensions()
     QCOMPARE(input->height(), qreal(-1));
 }
 
+
+void tst_qquicktextinput::setInputMask_data()
+{
+    QTest::addColumn<QString>("mask");
+    QTest::addColumn<QString>("input");
+    QTest::addColumn<QString>("expectedText");
+    QTest::addColumn<QString>("expectedDisplay");
+    QTest::addColumn<bool>("insert_text");
+
+    // both keyboard and insert()
+    for (int i=0; i<2; i++) {
+        bool insert_text = i==0 ? false : true;
+        QString insert_mode = "keys ";
+        if (insert_text)
+            insert_mode = "insert ";
+
+        QTest::newRow(QString(insert_mode + "ip_localhost").toLatin1())
+            << QString("000.000.000.000")
+            << QString("127.0.0.1")
+            << QString("127.0.0.1")
+            << QString("127.0  .0  .1  ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "mac").toLatin1())
+            << QString("HH:HH:HH:HH:HH:HH;#")
+            << QString("00:E0:81:21:9E:8E")
+            << QString("00:E0:81:21:9E:8E")
+            << QString("00:E0:81:21:9E:8E")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "mac2").toLatin1())
+            << QString("<HH:>HH:!HH:HH:HH:HH;#")
+            << QString("AAe081219E8E")
+            << QString("aa:E0:81:21:9E:8E")
+            << QString("aa:E0:81:21:9E:8E")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "byte").toLatin1())
+            << QString("BBBBBBBB;0")
+            << QString("11011001")
+            << QString("11111")
+            << QString("11011001")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "halfbytes").toLatin1())
+            << QString("bbbb.bbbb;-")
+            << QString("110. 0001")
+            << QString("110.0001")
+            << QString("110-.0001")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "blank char same type as content").toLatin1())
+            << QString("000.000.000.000;0")
+            << QString("127.0.0.1")
+            << QString("127...1")
+            << QString("127.000.000.100")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "parts of ip_localhost").toLatin1())
+            << QString("000.000.000.000")
+            << QString(".0.0.1")
+            << QString(".0.0.1")
+            << QString("   .0  .0  .1  ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "ip_null").toLatin1())
+            << QString("000.000.000.000")
+            << QString()
+            << QString("...")
+            << QString("   .   .   .   ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "ip_null_hash").toLatin1())
+            << QString("000.000.000.000;#")
+            << QString()
+            << QString("...")
+            << QString("###.###.###.###")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "ip_overflow").toLatin1())
+            << QString("000.000.000.000")
+            << QString("1234123412341234")
+            << QString("123.412.341.234")
+            << QString("123.412.341.234")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "uppercase").toLatin1())
+            << QString(">AAAA")
+            << QString("AbCd")
+            << QString("ABCD")
+            << QString("ABCD")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "lowercase").toLatin1())
+            << QString("<AAAA")
+            << QString("AbCd")
+            << QString("abcd")
+            << QString("abcd")
+            << bool(insert_text);
+
+        QTest::newRow(QString(insert_mode + "nocase").toLatin1())
+            << QString("!AAAA")
+            << QString("AbCd")
+            << QString("AbCd")
+            << QString("AbCd")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "nocase1").toLatin1())
+            << QString("!A!A!A!A")
+            << QString("AbCd")
+            << QString("AbCd")
+            << QString("AbCd")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "nocase2").toLatin1())
+            << QString("AAAA")
+            << QString("AbCd")
+            << QString("AbCd")
+            << QString("AbCd")
+            << bool(insert_text);
+
+        QTest::newRow(QString(insert_mode + "reserved").toLatin1())
+            << QString("{n}[0]")
+            << QString("A9")
+            << QString("A9")
+            << QString("A9")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "escape01").toLatin1())
+            << QString("\\\\N\\\\n00")
+            << QString("9")
+            << QString("Nn9")
+            << QString("Nn9 ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "escape02").toLatin1())
+            << QString("\\\\\\\\00")
+            << QString("0")
+            << QString("\\0")
+            << QString("\\0 ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "escape03").toLatin1())
+            << QString("\\\\(00\\\\)")
+            << QString("0")
+            << QString("(0)")
+            << QString("(0 )")
+            << bool(insert_text);
+
+        QTest::newRow(QString(insert_mode + "upper_lower_nocase1").toLatin1())
+            << QString(">AAAA<AAAA!AAAA")
+            << QString("AbCdEfGhIjKl")
+            << QString("ABCDefghIjKl")
+            << QString("ABCDefghIjKl")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "upper_lower_nocase2").toLatin1())
+            << QString(">aaaa<aaaa!aaaa")
+            << QString("AbCdEfGhIjKl")
+            << QString("ABCDefghIjKl")
+            << QString("ABCDefghIjKl")
+            << bool(insert_text);
+
+        QTest::newRow(QString(insert_mode + "exact_case1").toLatin1())
+            << QString(">A<A<A>A>A<A!A!A")
+            << QString("AbCdEFGH")
+            << QString("AbcDEfGH")
+            << QString("AbcDEfGH")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "exact_case2").toLatin1())
+            << QString(">A<A<A>A>A<A!A!A")
+            << QString("aBcDefgh")
+            << QString("AbcDEfgh")
+            << QString("AbcDEfgh")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "exact_case3").toLatin1())
+            << QString(">a<a<a>a>a<a!a!a")
+            << QString("AbCdEFGH")
+            << QString("AbcDEfGH")
+            << QString("AbcDEfGH")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "exact_case4").toLatin1())
+            << QString(">a<a<a>a>a<a!a!a")
+            << QString("aBcDefgh")
+            << QString("AbcDEfgh")
+            << QString("AbcDEfgh")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "exact_case5").toLatin1())
+            << QString(">H<H<H>H>H<H!H!H")
+            << QString("aBcDef01")
+            << QString("AbcDEf01")
+            << QString("AbcDEf01")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "exact_case6").toLatin1())
+            << QString(">h<h<h>h>h<h!h!h")
+            << QString("aBcDef92")
+            << QString("AbcDEf92")
+            << QString("AbcDEf92")
+            << bool(insert_text);
+
+        QTest::newRow(QString(insert_mode + "illegal_keys1").toLatin1())
+            << QString("AAAAAAAA")
+            << QString("A2#a;.0!")
+            << QString("Aa")
+            << QString("Aa      ")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "illegal_keys2").toLatin1())
+            << QString("AAAA")
+            << QString("f4f4f4f4")
+            << QString("ffff")
+            << QString("ffff")
+            << bool(insert_text);
+        QTest::newRow(QString(insert_mode + "blank=input").toLatin1())
+            << QString("9999;0")
+            << QString("2004")
+            << QString("2004")
+            << QString("2004")
+            << bool(insert_text);
+    }
+}
+
+void tst_qquicktextinput::setInputMask()
+{
+    QFETCH(QString, mask);
+    QFETCH(QString, input);
+    QFETCH(QString, expectedText);
+    QFETCH(QString, expectedDisplay);
+    QFETCH(bool, insert_text);
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + mask + "\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    // then either insert using insert() or keyboard
+    if (insert_text) {
+        textInput->insert(0, input);
+    } else {
+        QQuickWindow window;
+        textInput->setParentItem(window.contentItem());
+        window.show();
+        window.requestActivateWindow();
+        QTest::qWaitForWindowActive(&window);
+        QVERIFY(textInput->hasActiveFocus());
+
+        simulateKey(&window, Qt::Key_Home);
+        for (int i = 0; i < input.length(); i++)
+            QTest::keyClick(&window, input.at(i).toLatin1());
+    }
+
+    QEXPECT_FAIL( "keys blank=input", "To eat blanks or not? Known issue. Task 43172", Abort);
+    QEXPECT_FAIL( "insert blank=input", "To eat blanks or not? Known issue. Task 43172", Abort);
+
+    QCOMPARE(textInput->text(), expectedText);
+    QCOMPARE(textInput->displayText(), expectedDisplay);
+}
+
+void tst_qquicktextinput::inputMask_data()
+{
+    QTest::addColumn<QString>("mask");
+    QTest::addColumn<QString>("expectedMask");
+
+    // if no mask is set a nul string should be returned
+    QTest::newRow("nul 1") << QString("") << QString();
+    QTest::newRow("nul 2") << QString() << QString();
+
+    // try different masks
+    QTest::newRow("mask 1") << QString("000.000.000.000") << QString("000.000.000.000; ");
+    QTest::newRow("mask 2") << QString("000.000.000.000;#") << QString("000.000.000.000;#");
+    QTest::newRow("mask 3") << QString("AAA.aa.999.###;") << QString("AAA.aa.999.###; ");
+    QTest::newRow("mask 4") << QString(">abcdef<GHIJK") << QString(">abcdef<GHIJK; ");
+
+    // set an invalid input mask...
+    // the current behaviour is that this exact (faulty) string is returned.
+    QTest::newRow("invalid") << QString("ABCDEFGHIKLMNOP;") << QString("ABCDEFGHIKLMNOP; ");
+
+    // verify that we can unset the mask again
+    QTest::newRow("unset") << QString("") << QString();
+}
+
+void tst_qquicktextinput::inputMask()
+{
+    QFETCH(QString, mask);
+    QFETCH(QString, expectedMask);
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + mask + "\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    QCOMPARE(textInput->inputMask(), expectedMask);
+}
+
+void tst_qquicktextinput::clearInputMask()
+{
+    QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"000.000.000.000\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    QVERIFY(!textInput->inputMask().isEmpty());
+    textInput->setInputMask(QString());
+    QCOMPARE(textInput->inputMask(), QString());
+}
+
+void tst_qquicktextinput::keypress_inputMask_data()
+{
+    QTest::addColumn<QString>("mask");
+    QTest::addColumn<KeyList>("keys");
+    QTest::addColumn<QString>("expectedText");
+    QTest::addColumn<QString>("expectedDisplayText");
+
+    {
+        KeyList keys;
+        // inserting 'A1.2B'
+        keys << Qt::Key_Home << "A1.2B";
+        QTest::newRow("jumping on period(separator)") << QString("000.000;_") << keys << QString("1.2") << QString("1__.2__");
+    }
+    {
+        KeyList keys;
+        // inserting '0!P3'
+        keys << Qt::Key_Home << "0!P3";
+        QTest::newRow("jumping on input") << QString("D0.AA.XX.AA.00;_") << keys << QString("0..!P..3") << QString("_0.__.!P.__.3_");
+    }
+    {
+        KeyList keys;
+        // pressing delete
+        keys << Qt::Key_Home
+             << Qt::Key_Delete;
+        QTest::newRow("delete") << QString("000.000;_") << keys << QString(".") << QString("___.___");
+    }
+    {
+        KeyList keys;
+        // selecting all and delete
+        keys << Qt::Key_Home
+             << Key(Qt::ShiftModifier, Qt::Key_End)
+             << Qt::Key_Delete;
+        QTest::newRow("deleting all") << QString("000.000;_") << keys << QString(".") << QString("___.___");
+    }
+    {
+        KeyList keys;
+        // inserting '12.12' then two backspaces
+        keys << Qt::Key_Home << "12.12" << Qt::Key_Backspace << Qt::Key_Backspace;
+        QTest::newRow("backspace") << QString("000.000;_") << keys << QString("12.") << QString("12_.___");
+    }
+    {
+        KeyList keys;
+        // inserting '12ab'
+        keys << Qt::Key_Home << "12ab";
+        QTest::newRow("uppercase") << QString("9999 >AA;_") << keys << QString("12 AB") << QString("12__ AB");
+    }
+}
+
+void tst_qquicktextinput::keypress_inputMask()
+{
+    QFETCH(QString, mask);
+    QFETCH(KeyList, keys);
+    QFETCH(QString, expectedText);
+    QFETCH(QString, expectedDisplayText);
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { focus: true; inputMask: \"" + mask + "\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    QQuickWindow window;
+    textInput->setParentItem(window.contentItem());
+    window.show();
+    window.requestActivateWindow();
+    QTest::qWaitForWindowActive(&window);
+    QVERIFY(textInput->hasActiveFocus());
+
+    simulateKeys(&window, keys);
+
+    QCOMPARE(textInput->text(), expectedText);
+    QCOMPARE(textInput->displayText(), expectedDisplayText);
+}
+
+
+void tst_qquicktextinput::hasAcceptableInputMask_data()
+{
+    QTest::addColumn<QString>("optionalMask");
+    QTest::addColumn<QString>("requiredMask");
+    QTest::addColumn<QString>("invalid");
+    QTest::addColumn<QString>("valid");
+
+    QTest::newRow("Alphabetic optional and required")
+        << QString("aaaa") << QString("AAAA") << QString("ab") << QString("abcd");
+    QTest::newRow("Alphanumeric optional and require")
+        << QString("nnnn") << QString("NNNN") << QString("R2") << QString("R2D2");
+    QTest::newRow("Any optional and required")
+        << QString("xxxx") << QString("XXXX") << QString("+-") << QString("+-*/");
+    QTest::newRow("Numeric (0-9) required")
+        << QString("0000") << QString("9999") << QString("11") << QString("1138");
+    QTest::newRow("Numeric (1-9) optional and required")
+        << QString("dddd") << QString("DDDD") << QString("12") << QString("1234");
+}
+
+void tst_qquicktextinput::hasAcceptableInputMask()
+{
+    QFETCH(QString, optionalMask);
+    QFETCH(QString, requiredMask);
+    QFETCH(QString, invalid);
+    QFETCH(QString, valid);
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { inputMask: \"" + optionalMask + "\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    // test that invalid input (for required) work for optionalMask
+    textInput->setText(invalid);
+    QVERIFY(textInput->hasAcceptableInput());
+
+    // at the moment we don't strip the blank character if it is valid input, this makes the test between x vs X useless
+    QEXPECT_FAIL( "Any optional and required", "To eat blanks or not? Known issue. Task 43172", Abort);
+
+    // test requiredMask
+    textInput->setInputMask(requiredMask);
+    textInput->setText(invalid);
+    QVERIFY(!textInput->hasAcceptableInput());
+
+    textInput->setText(valid);
+    QVERIFY(textInput->hasAcceptableInput());
+}
+
+void tst_qquicktextinput::maskCharacter_data()
+{
+    QTest::addColumn<QString>("mask");
+    QTest::addColumn<QString>("input");
+    QTest::addColumn<bool>("expectedValid");
+
+    QTest::newRow("Hex") << QString("H")
+                         << QString("0123456789abcdefABCDEF") << true;
+    QTest::newRow("hex") << QString("h")
+                         << QString("0123456789abcdefABCDEF") << true;
+    QTest::newRow("HexInvalid") << QString("H")
+                                << QString("ghijklmnopqrstuvwxyzGHIJKLMNOPQRSTUVWXYZ")
+                                << false;
+    QTest::newRow("hexInvalid") << QString("h")
+                                << QString("ghijklmnopqrstuvwxyzGHIJKLMNOPQRSTUVWXYZ")
+                                << false;
+    QTest::newRow("Bin") << QString("B")
+                         << QString("01") << true;
+    QTest::newRow("bin") << QString("b")
+                         << QString("01") << true;
+    QTest::newRow("BinInvalid") << QString("B")
+                                << QString("23456789qwertyuiopasdfghjklzxcvbnm")
+                                << false;
+    QTest::newRow("binInvalid") << QString("b")
+                                << QString("23456789qwertyuiopasdfghjklzxcvbnm")
+                                << false;
+}
+
+void tst_qquicktextinput::maskCharacter()
+{
+    QFETCH(QString, mask);
+    QFETCH(QString, input);
+    QFETCH(bool, expectedValid);
+
+    QString componentStr = "import QtQuick 2.0\nTextInput { inputMask: \"" + mask + "\" }";
+    QQmlComponent textInputComponent(&engine);
+    textInputComponent.setData(componentStr.toLatin1(), QUrl());
+    QQuickTextInput *textInput = qobject_cast<QQuickTextInput*>(textInputComponent.create());
+    QVERIFY(textInput != 0);
+
+    for (int i = 0; i < input.size(); ++i) {
+        QString in = QString(input.at(i));
+        QString expected = expectedValid ? in : QString();
+        textInput->setText(QString(input.at(i)));
+        QCOMPARE(textInput->text(), expected);
+    }
+}
+
 QTEST_MAIN(tst_qquicktextinput)
 
 #include "tst_qquicktextinput.moc"