1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <QTextDocument>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <private/qsgtext_p.h>
46 #include <private/qsgtext_p_p.h>
47 #include <private/qdeclarativevaluetype_p.h>
48 #include <private/qsgdistancefieldglyphcache_p.h>
49 #include <QFontMetrics>
50 #include <QGraphicsSceneMouseEvent>
53 #include <private/qapplication_p.h>
56 #include "../../../shared/util.h"
57 #include "testhttpserver.h"
58 #include <QtOpenGL/QGLShaderProgram>
61 // In Symbian OS test data is located in applications private dir
65 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
67 class tst_qsgtext : public QObject
76 void cleanupTestCase();
83 void alignments_data();
86 void embeddedImages_data();
87 void embeddedImages();
92 // ### these tests may be trivial
93 void horizontalAlignment();
94 void horizontalAlignment_RightToLeft();
95 void verticalAlignment();
101 // QDeclarativeFontValueType
106 void capitalization();
107 void letterSpacing();
113 void implicitSize_data();
118 QStringList standard;
119 QStringList richText;
121 QStringList horizontalAlignmentmentStrings;
122 QStringList verticalAlignmentmentStrings;
124 QList<Qt::Alignment> verticalAlignmentments;
125 QList<Qt::Alignment> horizontalAlignmentments;
127 QStringList styleStrings;
128 QList<QSGText::TextStyle> styles;
130 QStringList colorStrings;
132 QDeclarativeEngine engine;
134 QSGView *createView(const QString &filename);
136 void tst_qsgtext::initTestCase()
139 if (!QGLShaderProgram::hasOpenGLShaderPrograms(canvas.context()))
140 QSKIP("Text item needs OpenGL 2.0", SkipAll);
143 void tst_qsgtext::cleanupTestCase()
147 tst_qsgtext::tst_qsgtext()
149 standard << "the quick brown fox jumped over the lazy dog"
150 << "the quick brown fox\n jumped over the lazy dog";
152 richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
153 << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
155 horizontalAlignmentmentStrings << "AlignLeft"
159 verticalAlignmentmentStrings << "AlignTop"
163 horizontalAlignmentments << Qt::AlignLeft
167 verticalAlignmentments << Qt::AlignTop
171 styleStrings << "Normal"
176 styles << QSGText::Normal
181 colorStrings << "aliceblue"
194 // need a different test to do alpha channel test
200 QSGView *tst_qsgtext::createView(const QString &filename)
202 QSGView *canvas = new QSGView(0);
204 canvas->setSource(QUrl::fromLocalFile(filename));
208 void tst_qsgtext::text()
211 QDeclarativeComponent textComponent(&engine);
212 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
213 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
215 QVERIFY(textObject != 0);
216 QCOMPARE(textObject->text(), QString(""));
217 QVERIFY(textObject->width() == 0);
222 for (int i = 0; i < standard.size(); i++)
224 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
225 QDeclarativeComponent textComponent(&engine);
226 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
228 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
230 QVERIFY(textObject != 0);
231 QCOMPARE(textObject->text(), standard.at(i));
232 QVERIFY(textObject->width() > 0);
237 for (int i = 0; i < richText.size(); i++)
239 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
240 QDeclarativeComponent textComponent(&engine);
241 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
242 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
244 QVERIFY(textObject != 0);
245 QString expected = richText.at(i);
246 QCOMPARE(textObject->text(), expected.replace("\\\"", "\""));
247 QVERIFY(textObject->width() > 0);
253 void tst_qsgtext::width()
255 // uses Font metrics to find the width for standard and document to find the width for rich
257 QDeclarativeComponent textComponent(&engine);
258 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
259 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
261 QVERIFY(textObject != 0);
262 QCOMPARE(textObject->width(), 0.);
267 bool requiresUnhintedMetrics = !qmlDisableDistanceField();
269 for (int i = 0; i < standard.size(); i++)
271 QVERIFY(!Qt::mightBeRichText(standard.at(i))); // self-test
274 qreal metricWidth = 0.0;
276 if (requiresUnhintedMetrics) {
277 QString s = standard.at(i);
278 s.replace(QLatin1Char('\n'), QChar::LineSeparator);
280 QTextLayout layout(s);
281 layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic);
284 option.setUseDesignMetrics(true);
285 layout.setTextOption(option);
288 layout.beginLayout();
290 QTextLine line = layout.createLine();
297 metricWidth = qCeil(layout.boundingRect().width());
300 qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
301 metricWidth = qCeil(metricWidth);
304 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
305 QDeclarativeComponent textComponent(&engine);
306 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
307 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
309 QVERIFY(textObject != 0);
310 QVERIFY(textObject->boundingRect().width() > 0);
311 QCOMPARE(textObject->width(), qreal(metricWidth));
312 QVERIFY(textObject->textFormat() == QSGText::AutoText); // setting text doesn't change format
317 for (int i = 0; i < richText.size(); i++)
319 QVERIFY(Qt::mightBeRichText(richText.at(i))); // self-test
321 QTextDocument document;
322 document.setHtml(richText.at(i));
323 document.setDocumentMargin(0);
325 int documentWidth = document.idealWidth();
327 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
328 QDeclarativeComponent textComponent(&engine);
329 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
330 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
332 QVERIFY(textObject != 0);
333 QCOMPARE(textObject->width(), qreal(documentWidth));
334 QVERIFY(textObject->textFormat() == QSGText::AutoText); // setting text doesn't change format
340 void tst_qsgtext::wrap()
343 // for specified width and wrap set true
345 QDeclarativeComponent textComponent(&engine);
346 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; wrapMode: Text.WordWrap; width: 300 }", QUrl::fromLocalFile(""));
347 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
348 textHeight = textObject->height();
350 QVERIFY(textObject != 0);
351 QVERIFY(textObject->wrapMode() == QSGText::WordWrap);
352 QCOMPARE(textObject->width(), 300.);
357 for (int i = 0; i < standard.size(); i++)
359 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + standard.at(i) + "\" }";
360 QDeclarativeComponent textComponent(&engine);
361 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
362 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
364 QVERIFY(textObject != 0);
365 QCOMPARE(textObject->width(), 30.);
366 QVERIFY(textObject->height() > textHeight);
368 int oldHeight = textObject->height();
369 textObject->setWidth(100);
370 QVERIFY(textObject->height() < oldHeight);
375 for (int i = 0; i < richText.size(); i++)
377 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + richText.at(i) + "\" }";
378 QDeclarativeComponent textComponent(&engine);
379 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
380 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
382 QVERIFY(textObject != 0);
383 QCOMPARE(textObject->width(), 30.);
384 QVERIFY(textObject->height() > textHeight);
386 qreal oldHeight = textObject->height();
387 textObject->setWidth(100);
388 QVERIFY(textObject->height() < oldHeight);
393 // richtext again with a fixed height
394 for (int i = 0; i < richText.size(); i++)
396 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; height: 50; text: \"" + richText.at(i) + "\" }";
397 QDeclarativeComponent textComponent(&engine);
398 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
399 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
401 QVERIFY(textObject != 0);
402 QCOMPARE(textObject->width(), 30.);
403 QVERIFY(textObject->implicitHeight() > textHeight);
405 qreal oldHeight = textObject->implicitHeight();
406 textObject->setWidth(100);
407 QVERIFY(textObject->implicitHeight() < oldHeight);
413 void tst_qsgtext::elide()
415 for (QSGText::TextElideMode m = QSGText::ElideLeft; m<=QSGText::ElideNone; m=QSGText::TextElideMode(int(m)+1)) {
416 const char* elidename[]={"ElideLeft", "ElideRight", "ElideMiddle", "ElideNone"};
417 QString elide = "elide: Text." + QString(elidename[int(m)]) + ";";
419 // XXX Poor coverage.
422 QDeclarativeComponent textComponent(&engine);
423 textComponent.setData(("import QtQuick 2.0\nText { text: \"\"; "+elide+" width: 100 }").toLatin1(), QUrl::fromLocalFile(""));
424 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
426 QCOMPARE(textObject->elideMode(), m);
427 QCOMPARE(textObject->width(), 100.);
432 for (int i = 0; i < standard.size(); i++)
434 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + standard.at(i) + "\" }";
435 QDeclarativeComponent textComponent(&engine);
436 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
437 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
439 QCOMPARE(textObject->elideMode(), m);
440 QCOMPARE(textObject->width(), 100.);
445 // richtext - does nothing
446 for (int i = 0; i < richText.size(); i++)
448 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + richText.at(i) + "\" }";
449 QDeclarativeComponent textComponent(&engine);
450 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
451 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
453 QCOMPARE(textObject->elideMode(), m);
454 QCOMPARE(textObject->width(), 100.);
461 void tst_qsgtext::textFormat()
464 QDeclarativeComponent textComponent(&engine);
465 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile(""));
466 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
468 QVERIFY(textObject != 0);
469 QVERIFY(textObject->textFormat() == QSGText::RichText);
474 QDeclarativeComponent textComponent(&engine);
475 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile(""));
476 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
478 QVERIFY(textObject != 0);
479 QVERIFY(textObject->textFormat() == QSGText::PlainText);
486 void tst_qsgtext::alignments_data()
488 QTest::addColumn<int>("hAlign");
489 QTest::addColumn<int>("vAlign");
490 QTest::addColumn<QString>("expectfile");
492 QTest::newRow("LT") << int(Qt::AlignLeft) << int(Qt::AlignTop) << SRCDIR "/data/alignments_lt.png";
493 QTest::newRow("RT") << int(Qt::AlignRight) << int(Qt::AlignTop) << SRCDIR "/data/alignments_rt.png";
494 QTest::newRow("CT") << int(Qt::AlignHCenter) << int(Qt::AlignTop) << SRCDIR "/data/alignments_ct.png";
496 QTest::newRow("LB") << int(Qt::AlignLeft) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_lb.png";
497 QTest::newRow("RB") << int(Qt::AlignRight) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_rb.png";
498 QTest::newRow("CB") << int(Qt::AlignHCenter) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_cb.png";
500 QTest::newRow("LC") << int(Qt::AlignLeft) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_lc.png";
501 QTest::newRow("RC") << int(Qt::AlignRight) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_rc.png";
502 QTest::newRow("CC") << int(Qt::AlignHCenter) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_cc.png";
506 void tst_qsgtext::alignments()
508 QSKIP("Text alignment pixmap comparison tests will not work with scenegraph", SkipAll);
512 QFETCH(QString, expectfile);
515 // Font-specific, but not likely platform-specific, so only test on one platform
517 fn.setRawName("-misc-fixed-medium-r-*-*-8-*-*-*-*-*-*-*");
518 QApplication::setFont(fn);
521 QSGView *canvas = createView(SRCDIR "/data/alignments.qml");
524 QApplication::setActiveWindow(canvas);
525 QTest::qWaitForWindowShown(canvas);
526 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
528 QObject *ob = canvas->rootObject();
530 ob->setProperty("horizontalAlignment",hAlign);
531 ob->setProperty("verticalAlignment",vAlign);
532 QTRY_COMPARE(ob->property("running").toBool(),false);
533 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32);
534 actual.fill(qRgb(255,255,255));
538 QImage expect(expectfile);
541 // Font-specific, but not likely platform-specific, so only test on one platform
542 if (QApplicationPrivate::graphics_system_name == "raster" || QApplicationPrivate::graphics_system_name == "") {
543 QCOMPARE(actual,expect);
550 //the alignment tests may be trivial o.oa
551 void tst_qsgtext::horizontalAlignment()
553 //test one align each, and then test if two align fails.
555 for (int i = 0; i < standard.size(); i++)
557 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
559 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
560 QDeclarativeComponent textComponent(&engine);
561 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
562 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
564 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
570 for (int i = 0; i < richText.size(); i++)
572 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
574 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
575 QDeclarativeComponent textComponent(&engine);
576 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
577 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
579 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
587 void tst_qsgtext::horizontalAlignment_RightToLeft()
589 QSGView *canvas = createView(SRCDIR "/data/horizontalAlignment_RightToLeft.qml");
590 QSGText *text = canvas->rootObject()->findChild<QSGText*>("text");
594 QSGTextPrivate *textPrivate = QSGTextPrivate::get(text);
595 QVERIFY(textPrivate != 0);
597 // implicit alignment should follow the reading direction of RTL text
598 QCOMPARE(text->hAlign(), QSGText::AlignRight);
599 QCOMPARE(text->effectiveHAlign(), text->hAlign());
600 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
602 // explicitly left aligned text
603 text->setHAlign(QSGText::AlignLeft);
604 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
605 QCOMPARE(text->effectiveHAlign(), text->hAlign());
606 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
608 // explicitly right aligned text
609 text->setHAlign(QSGText::AlignRight);
610 QCOMPARE(text->hAlign(), QSGText::AlignRight);
611 QCOMPARE(text->effectiveHAlign(), text->hAlign());
612 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
614 // change to rich text
615 QString textString = text->text();
616 text->setText(QString("<i>") + textString + QString("</i>"));
617 text->setTextFormat(QSGText::RichText);
620 // implicitly aligned rich text should follow the reading direction of text
621 QCOMPARE(text->hAlign(), QSGText::AlignRight);
622 QCOMPARE(text->effectiveHAlign(), text->hAlign());
623 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
625 // explicitly left aligned rich text
626 text->setHAlign(QSGText::AlignLeft);
627 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
628 QCOMPARE(text->effectiveHAlign(), text->hAlign());
629 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight);
631 // explicitly right aligned rich text
632 text->setHAlign(QSGText::AlignRight);
633 QCOMPARE(text->hAlign(), QSGText::AlignRight);
634 QCOMPARE(text->effectiveHAlign(), text->hAlign());
635 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
637 text->setText(textString);
638 text->setTextFormat(QSGText::PlainText);
640 // explicitly center aligned
641 text->setHAlign(QSGText::AlignHCenter);
642 QCOMPARE(text->hAlign(), QSGText::AlignHCenter);
643 QCOMPARE(text->effectiveHAlign(), text->hAlign());
644 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
645 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2);
647 // reseted alignment should go back to following the text reading direction
649 QCOMPARE(text->hAlign(), QSGText::AlignRight);
650 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
652 // mirror the text item
653 QSGItemPrivate::get(text)->setLayoutMirror(true);
655 // mirrored implicit alignment should continue to follow the reading direction of the text
656 QCOMPARE(text->hAlign(), QSGText::AlignRight);
657 QCOMPARE(text->effectiveHAlign(), QSGText::AlignRight);
658 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
660 // mirrored explicitly right aligned behaves as left aligned
661 text->setHAlign(QSGText::AlignRight);
662 QCOMPARE(text->hAlign(), QSGText::AlignRight);
663 QCOMPARE(text->effectiveHAlign(), QSGText::AlignLeft);
664 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
666 // mirrored explicitly left aligned behaves as right aligned
667 text->setHAlign(QSGText::AlignLeft);
668 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
669 QCOMPARE(text->effectiveHAlign(), QSGText::AlignRight);
670 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
673 QSGItemPrivate::get(text)->setLayoutMirror(false);
676 // English text should be implicitly left aligned
677 text->setText("Hello world!");
678 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
679 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
681 #ifndef Q_OS_MAC // QTBUG-18040
682 // empty text with implicit alignment follows the system locale-based
683 // keyboard input direction from QApplication::keyboardInputDirection
685 QCOMPARE(text->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
686 QSGText::AlignLeft : QSGText::AlignRight);
687 text->setHAlign(QSGText::AlignRight);
688 QCOMPARE(text->hAlign(), QSGText::AlignRight);
693 #ifndef Q_OS_MAC // QTBUG-18040
694 // alignment of Text with no text set to it
695 QString componentStr = "import QtQuick 2.0\nText {}";
696 QDeclarativeComponent textComponent(&engine);
697 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
698 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
699 QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
700 QSGText::AlignLeft : QSGText::AlignRight);
705 void tst_qsgtext::verticalAlignment()
707 //test one align each, and then test if two align fails.
709 for (int i = 0; i < standard.size(); i++)
711 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
713 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
714 QDeclarativeComponent textComponent(&engine);
715 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
716 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
718 QVERIFY(textObject != 0);
719 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
725 for (int i = 0; i < richText.size(); i++)
727 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
729 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
730 QDeclarativeComponent textComponent(&engine);
731 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
732 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
734 QVERIFY(textObject != 0);
735 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
743 void tst_qsgtext::font()
745 //test size, then bold, then italic, then family
747 QString componentStr = "import QtQuick 2.0\nText { font.pointSize: 40; text: \"Hello World\" }";
748 QDeclarativeComponent textComponent(&engine);
749 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
750 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
752 QCOMPARE(textObject->font().pointSize(), 40);
753 QCOMPARE(textObject->font().bold(), false);
754 QCOMPARE(textObject->font().italic(), false);
760 QString componentStr = "import QtQuick 2.0\nText { font.pixelSize: 40; text: \"Hello World\" }";
761 QDeclarativeComponent textComponent(&engine);
762 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
763 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
765 QCOMPARE(textObject->font().pixelSize(), 40);
766 QCOMPARE(textObject->font().bold(), false);
767 QCOMPARE(textObject->font().italic(), false);
773 QString componentStr = "import QtQuick 2.0\nText { font.bold: true; text: \"Hello World\" }";
774 QDeclarativeComponent textComponent(&engine);
775 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
776 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
778 QCOMPARE(textObject->font().bold(), true);
779 QCOMPARE(textObject->font().italic(), false);
785 QString componentStr = "import QtQuick 2.0\nText { font.italic: true; text: \"Hello World\" }";
786 QDeclarativeComponent textComponent(&engine);
787 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
788 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
790 QCOMPARE(textObject->font().italic(), true);
791 QCOMPARE(textObject->font().bold(), false);
797 QString componentStr = "import QtQuick 2.0\nText { font.family: \"Helvetica\"; text: \"Hello World\" }";
798 QDeclarativeComponent textComponent(&engine);
799 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
800 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
802 QCOMPARE(textObject->font().family(), QString("Helvetica"));
803 QCOMPARE(textObject->font().bold(), false);
804 QCOMPARE(textObject->font().italic(), false);
810 QString componentStr = "import QtQuick 2.0\nText { font.family: \"\"; text: \"Hello World\" }";
811 QDeclarativeComponent textComponent(&engine);
812 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
813 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
815 QCOMPARE(textObject->font().family(), QString(""));
821 void tst_qsgtext::style()
824 for (int i = 0; i < styles.size(); i++)
826 QString componentStr = "import QtQuick 2.0\nText { style: \"" + styleStrings.at(i) + "\"; styleColor: \"white\"; text: \"Hello World\" }";
827 QDeclarativeComponent textComponent(&engine);
828 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
829 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
831 QCOMPARE((int)textObject->style(), (int)styles.at(i));
832 QCOMPARE(textObject->styleColor(), QColor("white"));
836 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello World\" }";
837 QDeclarativeComponent textComponent(&engine);
838 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
839 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
841 QRectF brPre = textObject->boundingRect();
842 textObject->setStyle(QSGText::Outline);
843 QRectF brPost = textObject->boundingRect();
845 QVERIFY(brPre.width() < brPost.width());
846 QVERIFY(brPre.height() < brPost.height());
851 void tst_qsgtext::color()
854 for (int i = 0; i < colorStrings.size(); i++)
856 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
857 QDeclarativeComponent textComponent(&engine);
858 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
859 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
861 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
862 QCOMPARE(textObject->styleColor(), QColor());
867 for (int i = 0; i < colorStrings.size(); i++)
869 QString componentStr = "import QtQuick 2.0\nText { styleColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
870 QDeclarativeComponent textComponent(&engine);
871 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
872 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
874 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(i)));
875 // default color to black?
876 QCOMPARE(textObject->color(), QColor("black"));
881 for (int i = 0; i < colorStrings.size(); i++)
883 for (int j = 0; j < colorStrings.size(); j++)
885 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; styleColor: \"" + colorStrings.at(j) + "\"; text: \"Hello World\" }";
886 QDeclarativeComponent textComponent(&engine);
887 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
888 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
890 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
891 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(j)));
897 QString colorStr = "#AA001234";
898 QColor testColor("#001234");
899 testColor.setAlpha(170);
901 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStr + "\"; text: \"Hello World\" }";
902 QDeclarativeComponent textComponent(&engine);
903 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
904 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
906 QCOMPARE(textObject->color(), testColor);
912 void tst_qsgtext::smooth()
914 for (int i = 0; i < standard.size(); i++)
917 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + standard.at(i) + "\" }";
918 QDeclarativeComponent textComponent(&engine);
919 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
920 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
921 QCOMPARE(textObject->smooth(), true);
926 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
927 QDeclarativeComponent textComponent(&engine);
928 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
929 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
930 QCOMPARE(textObject->smooth(), false);
935 for (int i = 0; i < richText.size(); i++)
938 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + richText.at(i) + "\" }";
939 QDeclarativeComponent textComponent(&engine);
940 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
941 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
942 QCOMPARE(textObject->smooth(), true);
947 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
948 QDeclarativeComponent textComponent(&engine);
949 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
950 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
951 QCOMPARE(textObject->smooth(), false);
958 void tst_qsgtext::weight()
961 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
962 QDeclarativeComponent textComponent(&engine);
963 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
964 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
966 QVERIFY(textObject != 0);
967 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Normal);
972 QString componentStr = "import QtQuick 2.0\nText { font.weight: \"Bold\"; text: \"Hello world!\" }";
973 QDeclarativeComponent textComponent(&engine);
974 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
975 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
977 QVERIFY(textObject != 0);
978 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Bold);
984 void tst_qsgtext::underline()
987 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
988 QDeclarativeComponent textComponent(&engine);
989 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
990 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
992 QVERIFY(textObject != 0);
993 QCOMPARE(textObject->font().underline(), false);
998 QString componentStr = "import QtQuick 2.0\nText { font.underline: true; text: \"Hello world!\" }";
999 QDeclarativeComponent textComponent(&engine);
1000 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1001 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1003 QVERIFY(textObject != 0);
1004 QCOMPARE(textObject->font().underline(), true);
1010 void tst_qsgtext::overline()
1013 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1014 QDeclarativeComponent textComponent(&engine);
1015 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1016 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1018 QVERIFY(textObject != 0);
1019 QCOMPARE(textObject->font().overline(), false);
1024 QString componentStr = "import QtQuick 2.0\nText { font.overline: true; text: \"Hello world!\" }";
1025 QDeclarativeComponent textComponent(&engine);
1026 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1027 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1029 QVERIFY(textObject != 0);
1030 QCOMPARE(textObject->font().overline(), true);
1036 void tst_qsgtext::strikeout()
1039 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1040 QDeclarativeComponent textComponent(&engine);
1041 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1042 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1044 QVERIFY(textObject != 0);
1045 QCOMPARE(textObject->font().strikeOut(), false);
1050 QString componentStr = "import QtQuick 2.0\nText { font.strikeout: true; text: \"Hello world!\" }";
1051 QDeclarativeComponent textComponent(&engine);
1052 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1053 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1055 QVERIFY(textObject != 0);
1056 QCOMPARE(textObject->font().strikeOut(), true);
1062 void tst_qsgtext::capitalization()
1065 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1066 QDeclarativeComponent textComponent(&engine);
1067 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1068 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1070 QVERIFY(textObject != 0);
1071 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::MixedCase);
1076 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllUppercase\" }";
1077 QDeclarativeComponent textComponent(&engine);
1078 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1079 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1081 QVERIFY(textObject != 0);
1082 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllUppercase);
1087 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllLowercase\" }";
1088 QDeclarativeComponent textComponent(&engine);
1089 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1090 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1092 QVERIFY(textObject != 0);
1093 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllLowercase);
1098 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"SmallCaps\" }";
1099 QDeclarativeComponent textComponent(&engine);
1100 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1101 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1103 QVERIFY(textObject != 0);
1104 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::SmallCaps);
1109 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"Capitalize\" }";
1110 QDeclarativeComponent textComponent(&engine);
1111 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1112 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1114 QVERIFY(textObject != 0);
1115 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::Capitalize);
1121 void tst_qsgtext::letterSpacing()
1124 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1125 QDeclarativeComponent textComponent(&engine);
1126 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1127 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1129 QVERIFY(textObject != 0);
1130 QCOMPARE(textObject->font().letterSpacing(), 0.0);
1135 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: -2 }";
1136 QDeclarativeComponent textComponent(&engine);
1137 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1138 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1140 QVERIFY(textObject != 0);
1141 QCOMPARE(textObject->font().letterSpacing(), -2.);
1146 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: 3 }";
1147 QDeclarativeComponent textComponent(&engine);
1148 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1149 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1151 QVERIFY(textObject != 0);
1152 QCOMPARE(textObject->font().letterSpacing(), 3.);
1158 void tst_qsgtext::wordSpacing()
1161 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1162 QDeclarativeComponent textComponent(&engine);
1163 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1164 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1166 QVERIFY(textObject != 0);
1167 QCOMPARE(textObject->font().wordSpacing(), 0.0);
1172 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: -50 }";
1173 QDeclarativeComponent textComponent(&engine);
1174 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1175 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1177 QVERIFY(textObject != 0);
1178 QCOMPARE(textObject->font().wordSpacing(), -50.);
1183 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: 200 }";
1184 QDeclarativeComponent textComponent(&engine);
1185 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1186 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1188 QVERIFY(textObject != 0);
1189 QCOMPARE(textObject->font().wordSpacing(), 200.);
1195 void tst_qsgtext::QTBUG_12291()
1197 QSGView *canvas = createView(SRCDIR "/data/rotated.qml");
1200 QApplication::setActiveWindow(canvas);
1201 QTest::qWaitForWindowShown(canvas);
1202 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
1204 QObject *ob = canvas->rootObject();
1207 QSGText *text = ob->findChild<QSGText*>("text");
1209 QVERIFY(text->boundingRect().isValid());
1214 class EventSender : public QSGItem
1217 void sendEvent(QEvent *event) {
1218 if (event->type() == QEvent::GraphicsSceneMousePress)
1219 mousePressEvent(static_cast<QGraphicsSceneMouseEvent*>(event));
1220 else if (event->type() == QEvent::GraphicsSceneMouseRelease)
1221 mouseReleaseEvent(static_cast<QGraphicsSceneMouseEvent*>(event));
1223 qWarning() << "Trying to send unsupported event type";
1227 class LinkTest : public QObject
1236 void linkClicked(QString l) { link = l; }
1239 void tst_qsgtext::clickLink()
1242 QString componentStr = "import QtQuick 2.0\nText { text: \"<a href=\\\"http://qt.nokia.com\\\">Hello world!</a>\" }";
1243 QDeclarativeComponent textComponent(&engine);
1244 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1245 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1247 QVERIFY(textObject != 0);
1250 QObject::connect(textObject, SIGNAL(linkActivated(QString)), &test, SLOT(linkClicked(QString)));
1253 QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMousePress);
1254 me.setPos(QPointF(textObject->x()/2, textObject->y()/2));
1255 me.setButton(Qt::LeftButton);
1256 static_cast<EventSender*>(static_cast<QSGItem*>(textObject))->sendEvent(&me);
1260 QGraphicsSceneMouseEvent me(QEvent::GraphicsSceneMouseRelease);
1261 me.setPos(QPointF(textObject->x()/2, textObject->y()/2));
1262 me.setButton(Qt::LeftButton);
1263 static_cast<EventSender*>(static_cast<QSGItem*>(textObject))->sendEvent(&me);
1266 QCOMPARE(test.link, QLatin1String("http://qt.nokia.com"));
1272 void tst_qsgtext::embeddedImages_data()
1274 QTest::addColumn<QUrl>("qmlfile");
1275 QTest::addColumn<QString>("error");
1276 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocal.qml") << "";
1277 QTest::newRow("local-error") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocalError.qml")
1278 << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocalError.qml").toString()+":3:1: QML Text: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/http/notexists.png").toString();
1279 QTest::newRow("remote") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesRemote.qml") << "";
1280 QTest::newRow("remote-error") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesRemoteError.qml")
1281 << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesRemoteError.qml").toString()+":3:1: QML Text: Error downloading http://127.0.0.1:14453/notexists.png - server replied: Not found";
1284 void tst_qsgtext::embeddedImages()
1288 QFETCH(QUrl, qmlfile);
1289 QFETCH(QString, error);
1291 TestHTTPServer server(14453);
1292 server.serveDirectory(SRCDIR "/data/http");
1294 if (!error.isEmpty())
1295 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
1297 QDeclarativeComponent textComponent(&engine, qmlfile);
1298 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1300 QVERIFY(textObject != 0);
1302 QTRY_COMPARE(textObject->resourcesLoading(), 0);
1304 QPixmap pm(SRCDIR "/data/http/exists.png");
1305 if (error.isEmpty()) {
1306 QCOMPARE(textObject->width(), double(pm.width()));
1307 QCOMPARE(textObject->height(), double(pm.height()));
1309 QVERIFY(16 != pm.width()); // check test is effective
1310 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
1311 QCOMPARE(textObject->height(), 16.0);
1317 void tst_qsgtext::lineCount()
1319 QSGView *canvas = createView(SRCDIR "/data/lineCount.qml");
1321 QSGText *myText = canvas->rootObject()->findChild<QSGText*>("myText");
1322 QVERIFY(myText != 0);
1324 QVERIFY(myText->lineCount() > 1);
1325 QVERIFY(!myText->truncated());
1326 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1328 myText->setMaximumLineCount(2);
1329 QCOMPARE(myText->lineCount(), 2);
1330 QCOMPARE(myText->truncated(), true);
1331 QCOMPARE(myText->maximumLineCount(), 2);
1333 myText->resetMaximumLineCount();
1334 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1335 QCOMPARE(myText->truncated(), false);
1337 myText->setElideMode(QSGText::ElideRight);
1338 myText->setMaximumLineCount(2);
1339 QCOMPARE(myText->lineCount(), 2);
1340 QCOMPARE(myText->truncated(), true);
1341 QCOMPARE(myText->maximumLineCount(), 2);
1346 void tst_qsgtext::lineHeight()
1348 QSGView *canvas = createView(SRCDIR "/data/lineHeight.qml");
1350 QSGText *myText = canvas->rootObject()->findChild<QSGText*>("myText");
1351 QVERIFY(myText != 0);
1353 QVERIFY(myText->lineHeight() == 1);
1354 QVERIFY(myText->lineHeightMode() == QSGText::ProportionalHeight);
1356 qreal h = myText->height();
1357 myText->setLineHeight(1.5);
1358 QVERIFY(myText->height() == h * 1.5);
1360 myText->setLineHeightMode(QSGText::FixedHeight);
1361 myText->setLineHeight(20);
1362 QCOMPARE(myText->height(), myText->lineCount() * 20.0);
1364 myText->setText("Lorem ipsum sit <b>amet</b>, consectetur adipiscing elit. Integer felis nisl, varius in pretium nec, venenatis non erat. Proin lobortis interdum dictum.");
1365 myText->setLineHeightMode(QSGText::ProportionalHeight);
1366 myText->setLineHeight(1.0);
1368 qreal h2 = myText->height();
1369 myText->setLineHeight(2.0);
1370 QVERIFY(myText->height() == h2 * 2.0);
1372 myText->setLineHeightMode(QSGText::FixedHeight);
1373 myText->setLineHeight(10);
1374 QCOMPARE(myText->height(), myText->lineCount() * 10.0);
1379 void tst_qsgtext::implicitSize_data()
1381 QTest::addColumn<QString>("text");
1382 QTest::addColumn<QString>("wrap");
1383 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "Text.NoWrap";
1384 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.NoWrap";
1385 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "Text.Wrap";
1386 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.Wrap";
1389 void tst_qsgtext::implicitSize()
1391 QFETCH(QString, text);
1392 QFETCH(QString, wrap);
1393 QString componentStr = "import QtQuick 2.0\nText { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
1394 QDeclarativeComponent textComponent(&engine);
1395 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1396 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1398 QVERIFY(textObject->width() < textObject->implicitWidth());
1399 QVERIFY(textObject->height() == textObject->implicitHeight());
1401 textObject->resetWidth();
1402 QVERIFY(textObject->width() == textObject->implicitWidth());
1403 QVERIFY(textObject->height() == textObject->implicitHeight());
1408 void tst_qsgtext::qtbug_14734()
1410 QSGView *canvas = createView(SRCDIR "/data/qtbug_14734.qml");
1414 QApplication::setActiveWindow(canvas);
1415 QTest::qWaitForWindowShown(canvas);
1416 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
1421 QTEST_MAIN(tst_qsgtext)
1423 #include "tst_qsgtext.moc"