Stabilize text tests that grab windows.
authorFriedemann Kleint <Friedemann.Kleint@digia.com>
Mon, 22 Jul 2013 13:52:54 +0000 (15:52 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Thu, 25 Jul 2013 18:45:19 +0000 (20:45 +0200)
Add window flags to make sure window stays on top to
tst_qquicktext::hAlignImplicitWidth() and
tst_qquicktextedit::hAlignVisual(). Add diagnostic output.
Determine the section size by checking the font size.
Add more characters to line 2 to make it more discriminative.

Task-number: QTBUG-32540
Change-Id: I19a74a775bf2663027ec211784870fd9f6547b48
Reviewed-by: Simo Fält <simo.falt@digia.com>
Reviewed-by: Alan Alpert <aalpert@blackberry.com>

tests/auto/quick/qquicktext/data/hAlignImplicitWidth.qml
tests/auto/quick/qquicktext/tst_qquicktext.cpp
tests/auto/quick/qquicktextedit/data/hAlignVisual.qml
tests/auto/quick/qquicktextedit/tst_qquicktextedit.cpp

index 136e5d2..9c9318d 100644 (file)
@@ -6,7 +6,7 @@ Rectangle {
 
     Text {
         objectName: "textItem"
-        text: "AA\nBBBBB\nCCCCCCCCCCCCCCCC"
+        text: "AA\nBBBBBBB\nCCCCCCCCCCCCCCCC"
         anchors.centerIn: parent
         horizontalAlignment: Text.AlignLeft
     }
index 6637d36..51a5b25 100644 (file)
@@ -854,9 +854,20 @@ int tst_qquicktext::numberOfNonWhitePixels(int fromX, int toX, const QImage &ima
     return pixels;
 }
 
+static inline QByteArray msgNotGreaterThan(int n1, int n2)
+{
+    return QByteArray::number(n1) + QByteArrayLiteral(" is not greater than ") + QByteArray::number(n2);
+}
+
+static inline QByteArray msgNotLessThan(int n1, int n2)
+{
+    return QByteArray::number(n1) + QByteArrayLiteral(" is not less than ") + QByteArray::number(n2);
+}
+
 void tst_qquicktext::hAlignImplicitWidth()
 {
     QQuickView view(testFileUrl("hAlignImplicitWidth.qml"));
+    view.setFlags(view.flags() | Qt::WindowStaysOnTopHint); // Prevent being obscured by other windows.
     view.show();
     view.requestActivate();
     QVERIFY(QTest::qWaitForWindowActive(&view));
@@ -864,34 +875,45 @@ void tst_qquicktext::hAlignImplicitWidth()
     QQuickText *text = view.rootObject()->findChild<QQuickText*>("textItem");
     QVERIFY(text != 0);
 
+    // Try to check whether alignment works by checking the number of black
+    // pixels in the thirds of the grabbed image.
+    const int windowWidth = 200;
+    const int textWidth = qCeil(text->implicitWidth());
+    QVERIFY2(textWidth < windowWidth, "System font too large.");
+    const int sectionWidth = textWidth / 3;
+    const int centeredSection1 = (windowWidth - textWidth) / 2;
+    const int centeredSection2 = centeredSection1 + sectionWidth;
+    const int centeredSection3 = centeredSection2 + sectionWidth;
+    const int centeredSection3End = centeredSection3 + sectionWidth;
+
     {
         // Left Align
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left > mid);
-        QVERIFY(mid > right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        QVERIFY2(left > mid, msgNotGreaterThan(left, mid).constData());
+        QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
     }
     {
         // HCenter Align
         text->setHAlign(QQuickText::AlignHCenter);
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left < mid);
-        QVERIFY(mid > right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
+        QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
     }
     {
         // Right Align
         text->setHAlign(QQuickText::AlignRight);
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left < mid);
-        QVERIFY(mid < right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
+        QVERIFY2(mid < right, msgNotLessThan(mid, right).constData());
     }
 }
 
index 136e5d2..9c9318d 100644 (file)
@@ -6,7 +6,7 @@ Rectangle {
 
     Text {
         objectName: "textItem"
-        text: "AA\nBBBBB\nCCCCCCCCCCCCCCCC"
+        text: "AA\nBBBBBBB\nCCCCCCCCCCCCCCCC"
         anchors.centerIn: parent
         horizontalAlignment: Text.AlignLeft
     }
index 667d52e..6e74d84 100644 (file)
@@ -818,42 +818,66 @@ static int numberOfNonWhitePixels(int fromX, int toX, const QImage &image)
     return pixels;
 }
 
+static inline QByteArray msgNotGreaterThan(int n1, int n2)
+{
+    return QByteArray::number(n1) + QByteArrayLiteral(" is not greater than ") + QByteArray::number(n2);
+}
+
+static inline QByteArray msgNotLessThan(int n1, int n2)
+{
+    return QByteArray::number(n1) + QByteArrayLiteral(" is not less than ") + QByteArray::number(n2);
+}
+
 void tst_qquicktextedit::hAlignVisual()
 {
     QQuickView view(testFileUrl("hAlignVisual.qml"));
+    view.setFlags(view.flags() | Qt::WindowStaysOnTopHint); // Prevent being obscured by other windows.
     view.show();
     QVERIFY(QTest::qWaitForWindowExposed(&view));
 
     QQuickText *text = view.rootObject()->findChild<QQuickText*>("textItem");
     QVERIFY(text != 0);
+
+    // Try to check whether alignment works by checking the number of black
+    // pixels in the thirds of the grabbed image.
+    const int windowWidth = 200;
+    const int textWidth = qCeil(text->implicitWidth());
+    QVERIFY2(textWidth < windowWidth, "System font too large.");
+    const int sectionWidth = textWidth / 3;
+    const int centeredSection1 = (windowWidth - textWidth) / 2;
+    const int centeredSection2 = centeredSection1 + sectionWidth;
+    const int centeredSection3 = centeredSection2 + sectionWidth;
+    const int centeredSection3End = centeredSection3 + sectionWidth;
+
     {
         // Left Align
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left > mid);
-        QVERIFY(mid > right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        QVERIFY2(left > mid, msgNotGreaterThan(left, mid).constData());
+        QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
     }
     {
         // HCenter Align
         text->setHAlign(QQuickText::AlignHCenter);
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left < mid);
-        QVERIFY(mid > right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
+        QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
     }
     {
         // Right Align
         text->setHAlign(QQuickText::AlignRight);
         QImage image = view.grabWindow();
-        int left = numberOfNonWhitePixels(0, image.width() / 3, image);
-        int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
-        int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
-        QVERIFY(left < mid);
-        QVERIFY(mid < right);
+        const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
+        const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
+        const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
+        image.save("test3.png");
+        QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
+        QVERIFY2(mid < right, msgNotLessThan(mid, right).constData());
     }
 
     text->setWidth(200);
@@ -864,8 +888,8 @@ void tst_qquicktextedit::hAlignVisual()
         int x = qCeil(text->implicitWidth());
         int left = numberOfNonWhitePixels(0, x, image);
         int right = numberOfNonWhitePixels(x, image.width() - x, image);
-        QVERIFY(left > 0);
-        QVERIFY(right == 0);
+        QVERIFY2(left > 0, msgNotGreaterThan(left, 0).constData());
+        QCOMPARE(right, 0);
     }
     {
         // HCenter Align
@@ -876,9 +900,9 @@ void tst_qquicktextedit::hAlignVisual()
         int left = numberOfNonWhitePixels(0, x1, image);
         int mid = numberOfNonWhitePixels(x1, x2 - x1, image);
         int right = numberOfNonWhitePixels(x2, image.width() - x2, image);
-        QVERIFY(left == 0);
-        QVERIFY(mid > 0);
-        QVERIFY(right == 0);
+        QCOMPARE(left, 0);
+        QVERIFY2(mid > 0, msgNotGreaterThan(left, 0).constData());
+        QCOMPARE(right, 0);
     }
     {
         // Right Align
@@ -887,8 +911,8 @@ void tst_qquicktextedit::hAlignVisual()
         int x = image.width() - qCeil(text->implicitWidth());
         int left = numberOfNonWhitePixels(0, x, image);
         int right = numberOfNonWhitePixels(x, image.width() - x, image);
-        QVERIFY(left == 0);
-        QVERIFY(right > 0);
+        QCOMPARE(left, 0);
+        QVERIFY2(right > 0, msgNotGreaterThan(left, 0).constData());
     }
 }