45 #include <private/qdeclarativetext_p.h> |
45 #include <private/qdeclarativetext_p.h> |
46 #include <private/qdeclarativevaluetype_p.h> |
46 #include <private/qdeclarativevaluetype_p.h> |
47 #include <QFontMetrics> |
47 #include <QFontMetrics> |
48 #include <QGraphicsSceneMouseEvent> |
48 #include <QGraphicsSceneMouseEvent> |
49 #include <qmath.h> |
49 #include <qmath.h> |
|
50 #include <QDeclarativeView> |
|
51 #include <private/qapplication_p.h> |
50 |
52 |
51 #include "../../../shared/util.h" |
53 #include "../../../shared/util.h" |
52 #include "testhttpserver.h" |
54 #include "testhttpserver.h" |
|
55 |
|
56 #ifdef Q_OS_SYMBIAN |
|
57 // In Symbian OS test data is located in applications private dir |
|
58 #define SRCDIR "." |
|
59 #endif |
53 |
60 |
54 class tst_qdeclarativetext : public QObject |
61 class tst_qdeclarativetext : public QObject |
55 |
62 |
56 { |
63 { |
57 Q_OBJECT |
64 Q_OBJECT |
101 QList<QDeclarativeText::TextStyle> styles; |
113 QList<QDeclarativeText::TextStyle> styles; |
102 |
114 |
103 QStringList colorStrings; |
115 QStringList colorStrings; |
104 |
116 |
105 QDeclarativeEngine engine; |
117 QDeclarativeEngine engine; |
|
118 |
|
119 QDeclarativeView *createView(const QString &filename); |
106 }; |
120 }; |
107 |
121 |
108 tst_qdeclarativetext::tst_qdeclarativetext() |
122 tst_qdeclarativetext::tst_qdeclarativetext() |
109 { |
123 { |
110 standard << "the quick brown fox jumped over the lazy dog" |
124 standard << "the quick brown fox jumped over the lazy dog" |
224 QDeclarativeComponent textComponent(&engine); |
246 QDeclarativeComponent textComponent(&engine); |
225 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
247 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
226 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
248 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
227 |
249 |
228 QVERIFY(textObject != 0); |
250 QVERIFY(textObject != 0); |
|
251 QVERIFY(textObject->boundingRect().width() > 0); |
229 QCOMPARE(textObject->width(), qreal(metricWidth)); |
252 QCOMPARE(textObject->width(), qreal(metricWidth)); |
230 QVERIFY(textObject->textFormat() == QDeclarativeText::AutoText); // setting text doesn't change format |
253 QVERIFY(textObject->textFormat() == QDeclarativeText::AutoText); // setting text doesn't change format |
231 } |
254 } |
232 |
255 |
233 for (int i = 0; i < richText.size(); i++) |
256 for (int i = 0; i < richText.size(); i++) |
374 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
397 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
375 |
398 |
376 QVERIFY(textObject != 0); |
399 QVERIFY(textObject != 0); |
377 QVERIFY(textObject->textFormat() == QDeclarativeText::PlainText); |
400 QVERIFY(textObject->textFormat() == QDeclarativeText::PlainText); |
378 } |
401 } |
|
402 } |
|
403 |
|
404 |
|
405 void tst_qdeclarativetext::alignments_data() |
|
406 { |
|
407 QTest::addColumn<int>("hAlign"); |
|
408 QTest::addColumn<int>("vAlign"); |
|
409 QTest::addColumn<QString>("expectfile"); |
|
410 |
|
411 QTest::newRow("LT") << int(Qt::AlignLeft) << int(Qt::AlignTop) << SRCDIR "/data/alignments_lt.png"; |
|
412 QTest::newRow("RT") << int(Qt::AlignRight) << int(Qt::AlignTop) << SRCDIR "/data/alignments_rt.png"; |
|
413 QTest::newRow("CT") << int(Qt::AlignHCenter) << int(Qt::AlignTop) << SRCDIR "/data/alignments_ct.png"; |
|
414 |
|
415 QTest::newRow("LB") << int(Qt::AlignLeft) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_lb.png"; |
|
416 QTest::newRow("RB") << int(Qt::AlignRight) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_rb.png"; |
|
417 QTest::newRow("CB") << int(Qt::AlignHCenter) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_cb.png"; |
|
418 |
|
419 QTest::newRow("LC") << int(Qt::AlignLeft) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_lc.png"; |
|
420 QTest::newRow("RC") << int(Qt::AlignRight) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_rc.png"; |
|
421 QTest::newRow("CC") << int(Qt::AlignHCenter) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_cc.png"; |
|
422 } |
|
423 |
|
424 |
|
425 void tst_qdeclarativetext::alignments() |
|
426 { |
|
427 QFETCH(int, hAlign); |
|
428 QFETCH(int, vAlign); |
|
429 QFETCH(QString, expectfile); |
|
430 |
|
431 #ifdef Q_WS_X11 |
|
432 // Font-specific, but not likely platform-specific, so only test on one platform |
|
433 QFont fn; |
|
434 fn.setRawName("-misc-fixed-medium-r-*-*-8-*-*-*-*-*-*-*"); |
|
435 QApplication::setFont(fn); |
|
436 #endif |
|
437 |
|
438 QDeclarativeView *canvas = createView(SRCDIR "/data/alignments.qml"); |
|
439 |
|
440 canvas->show(); |
|
441 QApplication::setActiveWindow(canvas); |
|
442 QTest::qWaitForWindowShown(canvas); |
|
443 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); |
|
444 |
|
445 QObject *ob = canvas->rootObject(); |
|
446 QVERIFY(ob != 0); |
|
447 ob->setProperty("horizontalAlignment",hAlign); |
|
448 ob->setProperty("verticalAlignment",vAlign); |
|
449 QTRY_COMPARE(ob->property("running").toBool(),false); |
|
450 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32); |
|
451 actual.fill(qRgb(255,255,255)); |
|
452 QPainter p(&actual); |
|
453 canvas->render(&p); |
|
454 |
|
455 QImage expect(expectfile); |
|
456 |
|
457 #ifdef Q_WS_X11 |
|
458 // Font-specific, but not likely platform-specific, so only test on one platform |
|
459 if (QApplicationPrivate::graphics_system_name == "raster" || QApplicationPrivate::graphics_system_name == "") { |
|
460 QCOMPARE(actual,expect); |
|
461 } |
|
462 #endif |
379 } |
463 } |
380 |
464 |
381 //the alignment tests may be trivial o.oa |
465 //the alignment tests may be trivial o.oa |
382 void tst_qdeclarativetext::horizontalAlignment() |
466 void tst_qdeclarativetext::horizontalAlignment() |
383 { |
467 { |
764 |
848 |
765 QVERIFY(textObject != 0); |
849 QVERIFY(textObject != 0); |
766 QCOMPARE(textObject->font().letterSpacing(), 0.0); |
850 QCOMPARE(textObject->font().letterSpacing(), 0.0); |
767 } |
851 } |
768 { |
852 { |
769 QString componentStr = "import Qt 4.7\nText { text: \"Hello world!\"; font.letterSpacing: -50 }"; |
853 QString componentStr = "import Qt 4.7\nText { text: \"Hello world!\"; font.letterSpacing: -2 }"; |
770 QDeclarativeComponent textComponent(&engine); |
854 QDeclarativeComponent textComponent(&engine); |
771 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
855 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
772 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
856 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
773 |
857 |
774 QVERIFY(textObject != 0); |
858 QVERIFY(textObject != 0); |
775 QCOMPARE(textObject->font().letterSpacing(), -50.); |
859 QCOMPARE(textObject->font().letterSpacing(), -2.); |
776 } |
860 } |
777 { |
861 { |
778 QString componentStr = "import Qt 4.7\nText { text: \"Hello world!\"; font.letterSpacing: 200 }"; |
862 QString componentStr = "import Qt 4.7\nText { text: \"Hello world!\"; font.letterSpacing: 3 }"; |
779 QDeclarativeComponent textComponent(&engine); |
863 QDeclarativeComponent textComponent(&engine); |
780 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
864 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile("")); |
781 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
865 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
782 |
866 |
783 QVERIFY(textObject != 0); |
867 QVERIFY(textObject != 0); |
784 QCOMPARE(textObject->font().letterSpacing(), 200.); |
868 QCOMPARE(textObject->font().letterSpacing(), 3.); |
785 } |
869 } |
786 } |
870 } |
787 |
871 |
788 void tst_qdeclarativetext::wordSpacing() |
872 void tst_qdeclarativetext::wordSpacing() |
789 { |
873 { |
812 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
896 QDeclarativeText *textObject = qobject_cast<QDeclarativeText*>(textComponent.create()); |
813 |
897 |
814 QVERIFY(textObject != 0); |
898 QVERIFY(textObject != 0); |
815 QCOMPARE(textObject->font().wordSpacing(), 200.); |
899 QCOMPARE(textObject->font().wordSpacing(), 200.); |
816 } |
900 } |
|
901 } |
|
902 |
|
903 void tst_qdeclarativetext::QTBUG_12291() |
|
904 { |
|
905 QDeclarativeView *canvas = createView(SRCDIR "/data/rotated.qml"); |
|
906 |
|
907 canvas->show(); |
|
908 QApplication::setActiveWindow(canvas); |
|
909 QTest::qWaitForWindowShown(canvas); |
|
910 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas)); |
|
911 |
|
912 QObject *ob = canvas->rootObject(); |
|
913 QVERIFY(ob != 0); |
|
914 |
|
915 QDeclarativeText *text = ob->findChild<QDeclarativeText*>("text"); |
|
916 QVERIFY(text); |
|
917 QVERIFY(text->boundingRect().isValid()); |
817 } |
918 } |
818 |
919 |
819 class EventSender : public QGraphicsItem |
920 class EventSender : public QGraphicsItem |
820 { |
921 { |
821 public: |
922 public: |