Fix expected failure in TextInput and TextEdit positionAt test.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 9 Nov 2011 03:26:36 +0000 (13:26 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 10 Nov 2011 09:42:06 +0000 (10:42 +0100)
Test the mid point of the text input falls between the positions of the
cursors before and after that point rather trying to test a minimum
distance that can vary with font size.

Change-Id: Ie3270b4bfb0c579b8b4d5bb47b784d6926521f49
Reviewed-by: Martin Jones <martin.jones@nokia.com>

tests/auto/declarative/qquicktextedit/tst_qquicktextedit.cpp
tests/auto/declarative/qquicktextinput/tst_qquicktextinput.cpp

index 0556bfb..693619f 100644 (file)
@@ -1525,36 +1525,28 @@ void tst_qquicktextedit::positionAt()
     const int y1 = fm.height() * 3 / 2;
 
     int pos = texteditObject->positionAt(texteditObject->width()/2, y0);
-    int width = 0;
+    int widthBegin = 0;
+    int widthEnd = 0;
     if (!qmlDisableDistanceField()) {
-        QTextLayout layout(texteditObject->text().left(pos));
+        QTextLayout layout(texteditObject->text());
 
-        {
-            QTextOption option;
-            option.setUseDesignMetrics(true);
-            layout.setTextOption(option);
-        }
+        QTextOption option;
+        option.setUseDesignMetrics(true);
+        layout.setTextOption(option);
 
         layout.beginLayout();
         QTextLine line = layout.createLine();
         layout.endLayout();
 
-        width = ceil(line.horizontalAdvance());
-
+        widthBegin = floor(line.cursorToX(pos - 1));
+        widthEnd = ceil(line.cursorToX(pos + 1));
     } else {
-        width = fm.width(texteditObject->text().left(pos));
+        widthBegin = fm.width(texteditObject->text().left(pos - 1));
+        widthEnd = fm.width(texteditObject->text().left(pos + 1));
     }
 
-
-    int diff = abs(int(width-texteditObject->width()/2));
-
-    QEXPECT_FAIL("", "QTBUG-21689", Abort);
-    // some tollerance for different fonts.
-#ifdef Q_OS_LINUX
-    QVERIFY(diff < 2);
-#else
-    QVERIFY(diff < 5);
-#endif
+    QVERIFY(widthBegin <= texteditObject->width() / 2);
+    QVERIFY(widthEnd >= texteditObject->width() / 2);
 
     const qreal x0 = texteditObject->positionToRectangle(pos).x();
     const qreal x1 = texteditObject->positionToRectangle(pos + 1).x();
@@ -1564,7 +1556,7 @@ void tst_qquicktextedit::positionAt()
     texteditObject->setCursorPosition(0);
 
     QInputMethodEvent inputEvent(preeditText, QList<QInputMethodEvent::Attribute>());
-    QGuiApplication::sendEvent(&canvas, &inputEvent);
+    QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &inputEvent);
 
     // Check all points within the preedit text return the same position.
     QCOMPARE(texteditObject->positionAt(0, y0), 0);
index ee2afa9..bf29f88 100644 (file)
@@ -1215,52 +1215,30 @@ void tst_qquicktextinput::positionAt()
 
     int pos = textinputObject->positionAt(textinputObject->width()/2);
     int textWidth = 0;
-    int textLeftWidth = 0;
+    int textLeftWidthBegin = 0;
+    int textLeftWidthEnd = 0;
     if (!qmlDisableDistanceField()) {
-        {
-            QTextLayout layout(textinputObject->text().left(pos));
+        QTextLayout layout(textinputObject->text());
 
-            {
-                QTextOption option;
-                option.setUseDesignMetrics(true);
-                layout.setTextOption(option);
-            }
-
-            layout.beginLayout();
-            QTextLine line = layout.createLine();
-            layout.endLayout();
-
-            textLeftWidth = ceil(line.horizontalAdvance());
-        }
-        {
-            QTextLayout layout(textinputObject->text());
-
-            {
-                QTextOption option;
-                option.setUseDesignMetrics(true);
-                layout.setTextOption(option);
-            }
+        QTextOption option;
+        option.setUseDesignMetrics(true);
+        layout.setTextOption(option);
 
-            layout.beginLayout();
-            QTextLine line = layout.createLine();
-            layout.endLayout();
+        layout.beginLayout();
+        QTextLine line = layout.createLine();
+        layout.endLayout();
 
-            textWidth = ceil(line.horizontalAdvance());
-        }
+        textLeftWidthBegin = floor(line.cursorToX(pos - 1));
+        textLeftWidthEnd = ceil(line.cursorToX(pos + 1));
+        textWidth = floor(line.horizontalAdvance());
     } else {
         textWidth = fm.width(textinputObject->text());
-        textLeftWidth = fm.width(textinputObject->text().left(pos));
+        textLeftWidthBegin = fm.width(textinputObject->text().left(pos - 1));
+        textLeftWidthEnd = fm.width(textinputObject->text().left(pos + 1));
     }
 
-    int diff = abs(textWidth - (textLeftWidth+textinputObject->width()/2));
-
-    // some tollerance for different fonts.
-    QEXPECT_FAIL("", "QTBUG-21689", Abort);
-#ifdef Q_OS_LINUX
-    QVERIFY(diff < 2);
-#else
-    QVERIFY(diff < 5);
-#endif
+    QVERIFY(textLeftWidthBegin <= textWidth - textinputObject->width() / 2);
+    QVERIFY(textLeftWidthEnd >= textWidth - textinputObject->width() / 2);
 
     int x = textinputObject->positionToRectangle(pos + 1).x() - 1;
     QCOMPARE(textinputObject->positionAt(x, QQuickTextInput::CursorBetweenCharacters), pos + 1);
@@ -1271,33 +1249,25 @@ void tst_qquicktextinput::positionAt()
     pos = textinputObject->positionAt(textinputObject->width()/2);
 
     if (!qmlDisableDistanceField()) {
-        {
-            QTextLayout layout(textinputObject->text().left(pos));
+        QTextLayout layout(textinputObject->text());
 
-            {
-                QTextOption option;
-                option.setUseDesignMetrics(true);
-                layout.setTextOption(option);
-            }
+        QTextOption option;
+        option.setUseDesignMetrics(true);
+        layout.setTextOption(option);
 
-            layout.beginLayout();
-            QTextLine line = layout.createLine();
-            layout.endLayout();
+        layout.beginLayout();
+        QTextLine line = layout.createLine();
+        layout.endLayout();
 
-            textLeftWidth = ceil(line.horizontalAdvance());
-        }
+        textLeftWidthBegin = floor(line.cursorToX(pos - 1));
+        textLeftWidthEnd = ceil(line.cursorToX(pos + 1));
     } else {
-        textLeftWidth = fm.width(textinputObject->text().left(pos));
+        textLeftWidthBegin = fm.width(textinputObject->text().left(pos - 1));
+        textLeftWidthEnd = fm.width(textinputObject->text().left(pos + 1));
     }
 
-    diff = abs(int(textLeftWidth-textinputObject->width()/2));
-
-    // some tollerance for different fonts.
-#ifdef Q_OS_LINUX
-    QVERIFY(diff < 2);
-#else
-    QVERIFY(diff < 5);
-#endif
+    QVERIFY(textLeftWidthBegin <= textinputObject->width() / 2);
+    QVERIFY(textLeftWidthEnd >= textinputObject->width() / 2);
 
     x = textinputObject->positionToRectangle(pos + 1).x() - 1;
     QCOMPARE(textinputObject->positionAt(x, QQuickTextInput::CursorBetweenCharacters), pos + 1);
@@ -1311,7 +1281,7 @@ void tst_qquicktextinput::positionAt()
     textinputObject->setCursorPosition(0);
 
     QInputMethodEvent inputEvent(preeditText, QList<QInputMethodEvent::Attribute>());
-    QGuiApplication::sendEvent(&canvas, &inputEvent);
+    QGuiApplication::sendEvent(qGuiApp->inputPanel()->inputItem(), &inputEvent);
 
     // Check all points within the preedit text return the same position.
     QCOMPARE(textinputObject->positionAt(0), 0);