857
static inline QByteArray msgNotGreaterThan(int n1, int n2)
859
return QByteArray::number(n1) + QByteArrayLiteral(" is not greater than ") + QByteArray::number(n2);
862
static inline QByteArray msgNotLessThan(int n1, int n2)
864
return QByteArray::number(n1) + QByteArrayLiteral(" is not less than ") + QByteArray::number(n2);
855
867
void tst_qquicktext::hAlignImplicitWidth()
857
869
QQuickView view(testFileUrl("hAlignImplicitWidth.qml"));
870
view.setFlags(view.flags() | Qt::WindowStaysOnTopHint); // Prevent being obscured by other windows.
859
872
view.requestActivate();
860
873
QVERIFY(QTest::qWaitForWindowActive(&view));
862
875
QQuickText *text = view.rootObject()->findChild<QQuickText*>("textItem");
863
876
QVERIFY(text != 0);
878
// Try to check whether alignment works by checking the number of black
879
// pixels in the thirds of the grabbed image.
880
const int windowWidth = 200;
881
const int textWidth = qCeil(text->implicitWidth());
882
QVERIFY2(textWidth < windowWidth, "System font too large.");
883
const int sectionWidth = textWidth / 3;
884
const int centeredSection1 = (windowWidth - textWidth) / 2;
885
const int centeredSection2 = centeredSection1 + sectionWidth;
886
const int centeredSection3 = centeredSection2 + sectionWidth;
887
const int centeredSection3End = centeredSection3 + sectionWidth;
867
891
QImage image = view.grabWindow();
868
int left = numberOfNonWhitePixels(0, image.width() / 3, image);
869
int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
870
int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
872
QVERIFY(mid > right);
892
const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
893
const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
894
const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
895
QVERIFY2(left > mid, msgNotGreaterThan(left, mid).constData());
896
QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
876
900
text->setHAlign(QQuickText::AlignHCenter);
877
901
QImage image = view.grabWindow();
878
int left = numberOfNonWhitePixels(0, image.width() / 3, image);
879
int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
880
int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
882
QVERIFY(mid > right);
902
const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
903
const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
904
const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
905
QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
906
QVERIFY2(mid > right, msgNotGreaterThan(mid, right).constData());
886
910
text->setHAlign(QQuickText::AlignRight);
887
911
QImage image = view.grabWindow();
888
int left = numberOfNonWhitePixels(0, image.width() / 3, image);
889
int mid = numberOfNonWhitePixels(image.width() / 3, 2 * image.width() / 3, image);
890
int right = numberOfNonWhitePixels( 2 * image.width() / 3, image.width(), image);
892
QVERIFY(mid < right);
912
const int left = numberOfNonWhitePixels(centeredSection1, centeredSection2, image);
913
const int mid = numberOfNonWhitePixels(centeredSection2, centeredSection3, image);
914
const int right = numberOfNonWhitePixels(centeredSection3, centeredSection3End, image);
915
QVERIFY2(left < mid, msgNotLessThan(left, mid).constData());
916
QVERIFY2(mid < right, msgNotLessThan(mid, right).constData());