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>
55 #include <QtGui/QMouseEvent>
56 #include "../../../shared/util.h"
57 #include "testhttpserver.h"
58 #include <QtOpenGL/QGLShaderProgram>
60 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
62 class tst_qsgtext : public QObject
70 void cleanupTestCase();
77 void alignments_data();
80 void embeddedImages_data();
81 void embeddedImages();
86 // ### these tests may be trivial
87 void horizontalAlignment();
88 void horizontalAlignment_RightToLeft();
89 void verticalAlignment();
95 // QDeclarativeFontValueType
100 void capitalization();
101 void letterSpacing();
106 void implicitSize_data();
113 QStringList standard;
114 QStringList richText;
116 QStringList horizontalAlignmentmentStrings;
117 QStringList verticalAlignmentmentStrings;
119 QList<Qt::Alignment> verticalAlignmentments;
120 QList<Qt::Alignment> horizontalAlignmentments;
122 QStringList styleStrings;
123 QList<QSGText::TextStyle> styles;
125 QStringList colorStrings;
127 QDeclarativeEngine engine;
129 QSGView *createView(const QString &filename);
131 void tst_qsgtext::initTestCase()
135 void tst_qsgtext::cleanupTestCase()
139 tst_qsgtext::tst_qsgtext()
141 standard << "the quick brown fox jumped over the lazy dog"
142 << "the quick brown fox\n jumped over the lazy dog";
144 richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
145 << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
147 horizontalAlignmentmentStrings << "AlignLeft"
151 verticalAlignmentmentStrings << "AlignTop"
155 horizontalAlignmentments << Qt::AlignLeft
159 verticalAlignmentments << Qt::AlignTop
163 styleStrings << "Normal"
168 styles << QSGText::Normal
173 colorStrings << "aliceblue"
186 // need a different test to do alpha channel test
192 QSGView *tst_qsgtext::createView(const QString &filename)
194 QSGView *canvas = new QSGView(0);
196 canvas->setSource(QUrl::fromLocalFile(filename));
200 void tst_qsgtext::text()
203 QDeclarativeComponent textComponent(&engine);
204 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
205 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
207 QVERIFY(textObject != 0);
208 QCOMPARE(textObject->text(), QString(""));
209 QVERIFY(textObject->width() == 0);
214 for (int i = 0; i < standard.size(); i++)
216 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
217 QDeclarativeComponent textComponent(&engine);
218 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
220 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
222 QVERIFY(textObject != 0);
223 QCOMPARE(textObject->text(), standard.at(i));
224 QVERIFY(textObject->width() > 0);
229 for (int i = 0; i < richText.size(); i++)
231 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
232 QDeclarativeComponent textComponent(&engine);
233 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
234 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
236 QVERIFY(textObject != 0);
237 QString expected = richText.at(i);
238 QCOMPARE(textObject->text(), expected.replace("\\\"", "\""));
239 QVERIFY(textObject->width() > 0);
245 void tst_qsgtext::width()
247 // uses Font metrics to find the width for standard and document to find the width for rich
249 QDeclarativeComponent textComponent(&engine);
250 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
251 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
253 QVERIFY(textObject != 0);
254 QCOMPARE(textObject->width(), 0.);
259 bool requiresUnhintedMetrics = !qmlDisableDistanceField();
261 for (int i = 0; i < standard.size(); i++)
263 QVERIFY(!Qt::mightBeRichText(standard.at(i))); // self-test
266 qreal metricWidth = 0.0;
268 if (requiresUnhintedMetrics) {
269 QString s = standard.at(i);
270 s.replace(QLatin1Char('\n'), QChar::LineSeparator);
272 QTextLayout layout(s);
273 layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic);
276 option.setUseDesignMetrics(true);
277 layout.setTextOption(option);
280 layout.beginLayout();
282 QTextLine line = layout.createLine();
289 metricWidth = qCeil(layout.boundingRect().width());
292 qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
293 metricWidth = qCeil(metricWidth);
296 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
297 QDeclarativeComponent textComponent(&engine);
298 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
299 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
301 QVERIFY(textObject != 0);
302 QVERIFY(textObject->boundingRect().width() > 0);
303 QCOMPARE(textObject->width(), qreal(metricWidth));
304 QVERIFY(textObject->textFormat() == QSGText::AutoText); // setting text doesn't change format
309 for (int i = 0; i < richText.size(); i++)
311 QVERIFY(Qt::mightBeRichText(richText.at(i))); // self-test
313 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\"; textFormat: Text.RichText }";
314 QDeclarativeComponent textComponent(&engine);
315 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
316 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
317 QVERIFY(textObject != 0);
319 QSGTextPrivate *textPrivate = QSGTextPrivate::get(textObject);
320 QVERIFY(textPrivate != 0);
322 QTextDocument *doc = textPrivate->textDocument();
325 QCOMPARE(int(textObject->width()), int(doc->idealWidth()));
326 QVERIFY(textObject->textFormat() == QSGText::RichText);
332 void tst_qsgtext::wrap()
335 // for specified width and wrap set true
337 QDeclarativeComponent textComponent(&engine);
338 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; wrapMode: Text.WordWrap; width: 300 }", QUrl::fromLocalFile(""));
339 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
340 textHeight = textObject->height();
342 QVERIFY(textObject != 0);
343 QVERIFY(textObject->wrapMode() == QSGText::WordWrap);
344 QCOMPARE(textObject->width(), 300.);
349 for (int i = 0; i < standard.size(); i++)
351 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + standard.at(i) + "\" }";
352 QDeclarativeComponent textComponent(&engine);
353 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
354 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
356 QVERIFY(textObject != 0);
357 QCOMPARE(textObject->width(), 30.);
358 QVERIFY(textObject->height() > textHeight);
360 int oldHeight = textObject->height();
361 textObject->setWidth(100);
362 QVERIFY(textObject->height() < oldHeight);
367 for (int i = 0; i < richText.size(); i++)
369 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + richText.at(i) + "\" }";
370 QDeclarativeComponent textComponent(&engine);
371 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
372 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
374 QVERIFY(textObject != 0);
375 QCOMPARE(textObject->width(), 30.);
376 QVERIFY(textObject->height() > textHeight);
378 qreal oldHeight = textObject->height();
379 textObject->setWidth(100);
380 QVERIFY(textObject->height() < oldHeight);
385 // richtext again with a fixed height
386 for (int i = 0; i < richText.size(); i++)
388 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; height: 50; text: \"" + richText.at(i) + "\" }";
389 QDeclarativeComponent textComponent(&engine);
390 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
391 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
393 QVERIFY(textObject != 0);
394 QCOMPARE(textObject->width(), 30.);
395 QVERIFY(textObject->implicitHeight() > textHeight);
397 qreal oldHeight = textObject->implicitHeight();
398 textObject->setWidth(100);
399 QVERIFY(textObject->implicitHeight() < oldHeight);
405 void tst_qsgtext::elide()
407 for (QSGText::TextElideMode m = QSGText::ElideLeft; m<=QSGText::ElideNone; m=QSGText::TextElideMode(int(m)+1)) {
408 const char* elidename[]={"ElideLeft", "ElideRight", "ElideMiddle", "ElideNone"};
409 QString elide = "elide: Text." + QString(elidename[int(m)]) + ";";
411 // XXX Poor coverage.
414 QDeclarativeComponent textComponent(&engine);
415 textComponent.setData(("import QtQuick 2.0\nText { text: \"\"; "+elide+" width: 100 }").toLatin1(), QUrl::fromLocalFile(""));
416 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
418 QCOMPARE(textObject->elideMode(), m);
419 QCOMPARE(textObject->width(), 100.);
424 for (int i = 0; i < standard.size(); i++)
426 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + standard.at(i) + "\" }";
427 QDeclarativeComponent textComponent(&engine);
428 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
429 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
431 QCOMPARE(textObject->elideMode(), m);
432 QCOMPARE(textObject->width(), 100.);
437 // richtext - does nothing
438 for (int i = 0; i < richText.size(); i++)
440 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + richText.at(i) + "\" }";
441 QDeclarativeComponent textComponent(&engine);
442 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
443 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
445 QCOMPARE(textObject->elideMode(), m);
446 QCOMPARE(textObject->width(), 100.);
453 void tst_qsgtext::textFormat()
456 QDeclarativeComponent textComponent(&engine);
457 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile(""));
458 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
460 QVERIFY(textObject != 0);
461 QVERIFY(textObject->textFormat() == QSGText::RichText);
463 QSGTextPrivate *textPrivate = QSGTextPrivate::get(textObject);
464 QVERIFY(textPrivate != 0);
465 QVERIFY(textPrivate->richText == true);
470 QDeclarativeComponent textComponent(&engine);
471 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\" }", QUrl::fromLocalFile(""));
472 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
474 QVERIFY(textObject != 0);
475 QVERIFY(textObject->textFormat() == QSGText::AutoText);
477 QSGTextPrivate *textPrivate = QSGTextPrivate::get(textObject);
478 QVERIFY(textPrivate != 0);
479 QVERIFY(textPrivate->styledText == true);
484 QDeclarativeComponent textComponent(&engine);
485 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile(""));
486 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
488 QVERIFY(textObject != 0);
489 QVERIFY(textObject->textFormat() == QSGText::PlainText);
496 void tst_qsgtext::alignments_data()
498 QTest::addColumn<int>("hAlign");
499 QTest::addColumn<int>("vAlign");
500 QTest::addColumn<QString>("expectfile");
502 QTest::newRow("LT") << int(Qt::AlignLeft) << int(Qt::AlignTop) << SRCDIR "/data/alignments_lt.png";
503 QTest::newRow("RT") << int(Qt::AlignRight) << int(Qt::AlignTop) << SRCDIR "/data/alignments_rt.png";
504 QTest::newRow("CT") << int(Qt::AlignHCenter) << int(Qt::AlignTop) << SRCDIR "/data/alignments_ct.png";
506 QTest::newRow("LB") << int(Qt::AlignLeft) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_lb.png";
507 QTest::newRow("RB") << int(Qt::AlignRight) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_rb.png";
508 QTest::newRow("CB") << int(Qt::AlignHCenter) << int(Qt::AlignBottom) << SRCDIR "/data/alignments_cb.png";
510 QTest::newRow("LC") << int(Qt::AlignLeft) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_lc.png";
511 QTest::newRow("RC") << int(Qt::AlignRight) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_rc.png";
512 QTest::newRow("CC") << int(Qt::AlignHCenter) << int(Qt::AlignVCenter) << SRCDIR "/data/alignments_cc.png";
516 void tst_qsgtext::alignments()
519 QSKIP("Text alignment pixmap comparison tests will not work with scenegraph", SkipAll);
520 #if (0)// No widgets in scenegraph
523 QFETCH(QString, expectfile);
526 // Font-specific, but not likely platform-specific, so only test on one platform
528 fn.setRawName("-misc-fixed-medium-r-*-*-8-*-*-*-*-*-*-*");
529 QApplication::setFont(fn);
532 QSGView *canvas = createView(SRCDIR "/data/alignments.qml");
535 canvas->requestActivateWindow();
537 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
539 QObject *ob = canvas->rootObject();
541 ob->setProperty("horizontalAlignment",hAlign);
542 ob->setProperty("verticalAlignment",vAlign);
543 QTRY_COMPARE(ob->property("running").toBool(),false);
544 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32);
545 actual.fill(qRgb(255,255,255));
549 QImage expect(expectfile);
552 // Font-specific, but not likely platform-specific, so only test on one platform
553 if (QApplicationPrivate::graphics_system_name == "raster" || QApplicationPrivate::graphics_system_name == "") {
554 QCOMPARE(actual,expect);
562 //the alignment tests may be trivial o.oa
563 void tst_qsgtext::horizontalAlignment()
565 //test one align each, and then test if two align fails.
567 for (int i = 0; i < standard.size(); i++)
569 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
571 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
572 QDeclarativeComponent textComponent(&engine);
573 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
574 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
576 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
582 for (int i = 0; i < richText.size(); i++)
584 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
586 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
587 QDeclarativeComponent textComponent(&engine);
588 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
589 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
591 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
599 void tst_qsgtext::horizontalAlignment_RightToLeft()
601 QSGView *canvas = createView(SRCDIR "/data/horizontalAlignment_RightToLeft.qml");
602 QSGText *text = canvas->rootObject()->findChild<QSGText*>("text");
606 QSGTextPrivate *textPrivate = QSGTextPrivate::get(text);
607 QVERIFY(textPrivate != 0);
609 // implicit alignment should follow the reading direction of RTL text
610 QCOMPARE(text->hAlign(), QSGText::AlignRight);
611 QCOMPARE(text->effectiveHAlign(), text->hAlign());
612 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
614 // explicitly left aligned text
615 text->setHAlign(QSGText::AlignLeft);
616 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
617 QCOMPARE(text->effectiveHAlign(), text->hAlign());
618 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
620 // explicitly right aligned text
621 text->setHAlign(QSGText::AlignRight);
622 QCOMPARE(text->hAlign(), QSGText::AlignRight);
623 QCOMPARE(text->effectiveHAlign(), text->hAlign());
624 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
626 // change to rich text
627 QString textString = text->text();
628 text->setText(QString("<i>") + textString + QString("</i>"));
629 text->setTextFormat(QSGText::RichText);
632 // implicitly aligned rich text should follow the reading direction of text
633 QCOMPARE(text->hAlign(), QSGText::AlignRight);
634 QCOMPARE(text->effectiveHAlign(), text->hAlign());
635 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
637 // explicitly left aligned rich text
638 text->setHAlign(QSGText::AlignLeft);
639 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
640 QCOMPARE(text->effectiveHAlign(), text->hAlign());
641 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight);
643 // explicitly right aligned rich text
644 text->setHAlign(QSGText::AlignRight);
645 QCOMPARE(text->hAlign(), QSGText::AlignRight);
646 QCOMPARE(text->effectiveHAlign(), text->hAlign());
647 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
649 text->setText(textString);
650 text->setTextFormat(QSGText::PlainText);
652 // explicitly center aligned
653 text->setHAlign(QSGText::AlignHCenter);
654 QCOMPARE(text->hAlign(), QSGText::AlignHCenter);
655 QCOMPARE(text->effectiveHAlign(), text->hAlign());
656 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
657 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2);
659 // reseted alignment should go back to following the text reading direction
661 QCOMPARE(text->hAlign(), QSGText::AlignRight);
662 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
664 // mirror the text item
665 QSGItemPrivate::get(text)->setLayoutMirror(true);
667 // mirrored implicit alignment should continue to follow the reading direction of the text
668 QCOMPARE(text->hAlign(), QSGText::AlignRight);
669 QCOMPARE(text->effectiveHAlign(), QSGText::AlignRight);
670 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
672 // mirrored explicitly right aligned behaves as left aligned
673 text->setHAlign(QSGText::AlignRight);
674 QCOMPARE(text->hAlign(), QSGText::AlignRight);
675 QCOMPARE(text->effectiveHAlign(), QSGText::AlignLeft);
676 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
678 // mirrored explicitly left aligned behaves as right aligned
679 text->setHAlign(QSGText::AlignLeft);
680 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
681 QCOMPARE(text->effectiveHAlign(), QSGText::AlignRight);
682 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
685 QSGItemPrivate::get(text)->setLayoutMirror(false);
688 // English text should be implicitly left aligned
689 text->setText("Hello world!");
690 QCOMPARE(text->hAlign(), QSGText::AlignLeft);
691 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
693 #ifndef Q_OS_MAC // QTBUG-18040
694 // empty text with implicit alignment follows the system locale-based
695 // keyboard input direction from QApplication::keyboardInputDirection
697 QCOMPARE(text->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
698 QSGText::AlignLeft : QSGText::AlignRight);
699 text->setHAlign(QSGText::AlignRight);
700 QCOMPARE(text->hAlign(), QSGText::AlignRight);
705 #ifndef Q_OS_MAC // QTBUG-18040
706 // alignment of Text with no text set to it
707 QString componentStr = "import QtQuick 2.0\nText {}";
708 QDeclarativeComponent textComponent(&engine);
709 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
710 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
711 QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
712 QSGText::AlignLeft : QSGText::AlignRight);
717 void tst_qsgtext::verticalAlignment()
719 //test one align each, and then test if two align fails.
721 for (int i = 0; i < standard.size(); i++)
723 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
725 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
726 QDeclarativeComponent textComponent(&engine);
727 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
728 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
730 QVERIFY(textObject != 0);
731 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
737 for (int i = 0; i < richText.size(); i++)
739 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
741 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
742 QDeclarativeComponent textComponent(&engine);
743 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
744 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
746 QVERIFY(textObject != 0);
747 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
755 void tst_qsgtext::font()
757 //test size, then bold, then italic, then family
759 QString componentStr = "import QtQuick 2.0\nText { font.pointSize: 40; text: \"Hello World\" }";
760 QDeclarativeComponent textComponent(&engine);
761 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
762 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
764 QCOMPARE(textObject->font().pointSize(), 40);
765 QCOMPARE(textObject->font().bold(), false);
766 QCOMPARE(textObject->font().italic(), false);
772 QString componentStr = "import QtQuick 2.0\nText { font.pixelSize: 40; text: \"Hello World\" }";
773 QDeclarativeComponent textComponent(&engine);
774 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
775 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
777 QCOMPARE(textObject->font().pixelSize(), 40);
778 QCOMPARE(textObject->font().bold(), false);
779 QCOMPARE(textObject->font().italic(), false);
785 QString componentStr = "import QtQuick 2.0\nText { font.bold: 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().bold(), true);
791 QCOMPARE(textObject->font().italic(), false);
797 QString componentStr = "import QtQuick 2.0\nText { font.italic: true; 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().italic(), true);
803 QCOMPARE(textObject->font().bold(), false);
809 QString componentStr = "import QtQuick 2.0\nText { font.family: \"Helvetica\"; text: \"Hello World\" }";
810 QDeclarativeComponent textComponent(&engine);
811 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
812 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
814 QCOMPARE(textObject->font().family(), QString("Helvetica"));
815 QCOMPARE(textObject->font().bold(), false);
816 QCOMPARE(textObject->font().italic(), false);
822 QString componentStr = "import QtQuick 2.0\nText { font.family: \"\"; text: \"Hello World\" }";
823 QDeclarativeComponent textComponent(&engine);
824 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
825 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
827 QCOMPARE(textObject->font().family(), QString(""));
833 void tst_qsgtext::style()
836 for (int i = 0; i < styles.size(); i++)
838 QString componentStr = "import QtQuick 2.0\nText { style: \"" + styleStrings.at(i) + "\"; styleColor: \"white\"; text: \"Hello World\" }";
839 QDeclarativeComponent textComponent(&engine);
840 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
841 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
843 QCOMPARE((int)textObject->style(), (int)styles.at(i));
844 QCOMPARE(textObject->styleColor(), QColor("white"));
848 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello World\" }";
849 QDeclarativeComponent textComponent(&engine);
850 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
851 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
853 QRectF brPre = textObject->boundingRect();
854 textObject->setStyle(QSGText::Outline);
855 QRectF brPost = textObject->boundingRect();
857 QVERIFY(brPre.width() < brPost.width());
858 QVERIFY(brPre.height() < brPost.height());
863 void tst_qsgtext::color()
866 for (int i = 0; i < colorStrings.size(); i++)
868 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
869 QDeclarativeComponent textComponent(&engine);
870 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
871 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
873 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
874 QCOMPARE(textObject->styleColor(), QColor());
879 for (int i = 0; i < colorStrings.size(); i++)
881 QString componentStr = "import QtQuick 2.0\nText { styleColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
882 QDeclarativeComponent textComponent(&engine);
883 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
884 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
886 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(i)));
887 // default color to black?
888 QCOMPARE(textObject->color(), QColor("black"));
893 for (int i = 0; i < colorStrings.size(); i++)
895 for (int j = 0; j < colorStrings.size(); j++)
897 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; styleColor: \"" + colorStrings.at(j) + "\"; text: \"Hello World\" }";
898 QDeclarativeComponent textComponent(&engine);
899 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
900 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
902 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
903 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(j)));
909 QString colorStr = "#AA001234";
910 QColor testColor("#001234");
911 testColor.setAlpha(170);
913 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStr + "\"; text: \"Hello World\" }";
914 QDeclarativeComponent textComponent(&engine);
915 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
916 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
918 QCOMPARE(textObject->color(), testColor);
924 void tst_qsgtext::smooth()
926 for (int i = 0; i < standard.size(); i++)
929 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + standard.at(i) + "\" }";
930 QDeclarativeComponent textComponent(&engine);
931 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
932 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
933 QCOMPARE(textObject->smooth(), true);
938 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.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(), false);
947 for (int i = 0; i < richText.size(); i++)
950 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + richText.at(i) + "\" }";
951 QDeclarativeComponent textComponent(&engine);
952 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
953 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
954 QCOMPARE(textObject->smooth(), true);
959 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
960 QDeclarativeComponent textComponent(&engine);
961 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
962 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
963 QCOMPARE(textObject->smooth(), false);
970 void tst_qsgtext::weight()
973 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
974 QDeclarativeComponent textComponent(&engine);
975 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
976 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
978 QVERIFY(textObject != 0);
979 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Normal);
984 QString componentStr = "import QtQuick 2.0\nText { font.weight: \"Bold\"; text: \"Hello world!\" }";
985 QDeclarativeComponent textComponent(&engine);
986 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
987 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
989 QVERIFY(textObject != 0);
990 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Bold);
996 void tst_qsgtext::underline()
999 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1000 QDeclarativeComponent textComponent(&engine);
1001 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1002 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1004 QVERIFY(textObject != 0);
1005 QCOMPARE(textObject->font().underline(), false);
1010 QString componentStr = "import QtQuick 2.0\nText { font.underline: true; text: \"Hello world!\" }";
1011 QDeclarativeComponent textComponent(&engine);
1012 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1013 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1015 QVERIFY(textObject != 0);
1016 QCOMPARE(textObject->font().underline(), true);
1022 void tst_qsgtext::overline()
1025 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1026 QDeclarativeComponent textComponent(&engine);
1027 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1028 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1030 QVERIFY(textObject != 0);
1031 QCOMPARE(textObject->font().overline(), false);
1036 QString componentStr = "import QtQuick 2.0\nText { font.overline: true; text: \"Hello world!\" }";
1037 QDeclarativeComponent textComponent(&engine);
1038 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1039 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1041 QVERIFY(textObject != 0);
1042 QCOMPARE(textObject->font().overline(), true);
1048 void tst_qsgtext::strikeout()
1051 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1052 QDeclarativeComponent textComponent(&engine);
1053 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1054 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1056 QVERIFY(textObject != 0);
1057 QCOMPARE(textObject->font().strikeOut(), false);
1062 QString componentStr = "import QtQuick 2.0\nText { font.strikeout: true; text: \"Hello world!\" }";
1063 QDeclarativeComponent textComponent(&engine);
1064 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1065 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1067 QVERIFY(textObject != 0);
1068 QCOMPARE(textObject->font().strikeOut(), true);
1074 void tst_qsgtext::capitalization()
1077 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1078 QDeclarativeComponent textComponent(&engine);
1079 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1080 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1082 QVERIFY(textObject != 0);
1083 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::MixedCase);
1088 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllUppercase\" }";
1089 QDeclarativeComponent textComponent(&engine);
1090 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1091 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1093 QVERIFY(textObject != 0);
1094 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllUppercase);
1099 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllLowercase\" }";
1100 QDeclarativeComponent textComponent(&engine);
1101 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1102 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1104 QVERIFY(textObject != 0);
1105 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllLowercase);
1110 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"SmallCaps\" }";
1111 QDeclarativeComponent textComponent(&engine);
1112 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1113 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1115 QVERIFY(textObject != 0);
1116 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::SmallCaps);
1121 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"Capitalize\" }";
1122 QDeclarativeComponent textComponent(&engine);
1123 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1124 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1126 QVERIFY(textObject != 0);
1127 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::Capitalize);
1133 void tst_qsgtext::letterSpacing()
1136 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1137 QDeclarativeComponent textComponent(&engine);
1138 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1139 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1141 QVERIFY(textObject != 0);
1142 QCOMPARE(textObject->font().letterSpacing(), 0.0);
1147 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: -2 }";
1148 QDeclarativeComponent textComponent(&engine);
1149 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1150 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1152 QVERIFY(textObject != 0);
1153 QCOMPARE(textObject->font().letterSpacing(), -2.);
1158 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: 3 }";
1159 QDeclarativeComponent textComponent(&engine);
1160 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1161 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1163 QVERIFY(textObject != 0);
1164 QCOMPARE(textObject->font().letterSpacing(), 3.);
1170 void tst_qsgtext::wordSpacing()
1173 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1174 QDeclarativeComponent textComponent(&engine);
1175 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1176 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1178 QVERIFY(textObject != 0);
1179 QCOMPARE(textObject->font().wordSpacing(), 0.0);
1184 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: -50 }";
1185 QDeclarativeComponent textComponent(&engine);
1186 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1187 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1189 QVERIFY(textObject != 0);
1190 QCOMPARE(textObject->font().wordSpacing(), -50.);
1195 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: 200 }";
1196 QDeclarativeComponent textComponent(&engine);
1197 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1198 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1200 QVERIFY(textObject != 0);
1201 QCOMPARE(textObject->font().wordSpacing(), 200.);
1210 class EventSender : public QSGItem
1213 void sendEvent(QMouseEvent *event) {
1214 if (event->type() == QEvent::MouseButtonPress)
1215 mousePressEvent(event);
1216 else if (event->type() == QEvent::MouseButtonRelease)
1217 mouseReleaseEvent(event);
1219 qWarning() << "Trying to send unsupported event type";
1223 class LinkTest : public QObject
1232 void linkClicked(QString l) { link = l; }
1235 void tst_qsgtext::clickLink()
1238 QString componentStr = "import QtQuick 2.0\nText { text: \"<a href=\\\"http://qt.nokia.com\\\">Hello world!</a>\" }";
1239 QDeclarativeComponent textComponent(&engine);
1240 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1241 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1243 QVERIFY(textObject != 0);
1246 QObject::connect(textObject, SIGNAL(linkActivated(QString)), &test, SLOT(linkClicked(QString)));
1249 QMouseEvent me(QEvent::MouseButtonPress,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1250 static_cast<EventSender*>(static_cast<QSGItem*>(textObject))->sendEvent(&me);
1255 QMouseEvent me(QEvent::MouseButtonRelease,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1256 static_cast<EventSender*>(static_cast<QSGItem*>(textObject))->sendEvent(&me);
1261 QCOMPARE(test.link, QLatin1String("http://qt.nokia.com"));
1267 void tst_qsgtext::embeddedImages_data()
1269 QTest::addColumn<QUrl>("qmlfile");
1270 QTest::addColumn<QString>("error");
1271 QTest::newRow("local") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocal.qml") << "";
1272 QTest::newRow("local-error") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocalError.qml")
1273 << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesLocalError.qml").toString()+":3:1: QML Text: Cannot open: " + QUrl::fromLocalFile(SRCDIR "/data/http/notexists.png").toString();
1274 QTest::newRow("remote") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesRemote.qml") << "";
1275 QTest::newRow("remote-error") << QUrl::fromLocalFile(SRCDIR "/data/embeddedImagesRemoteError.qml")
1276 << 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";
1279 void tst_qsgtext::embeddedImages()
1283 QFETCH(QUrl, qmlfile);
1284 QFETCH(QString, error);
1286 TestHTTPServer server(14453);
1287 server.serveDirectory(SRCDIR "/data/http");
1289 if (!error.isEmpty())
1290 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
1292 QDeclarativeComponent textComponent(&engine, qmlfile);
1293 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1295 QVERIFY(textObject != 0);
1297 QTRY_COMPARE(textObject->resourcesLoading(), 0);
1299 QPixmap pm(SRCDIR "/data/http/exists.png");
1300 if (error.isEmpty()) {
1301 QCOMPARE(textObject->width(), double(pm.width()));
1302 QCOMPARE(textObject->height(), double(pm.height()));
1304 QVERIFY(16 != pm.width()); // check test is effective
1305 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
1306 QCOMPARE(textObject->height(), 16.0);
1312 void tst_qsgtext::lineCount()
1314 QSGView *canvas = createView(SRCDIR "/data/lineCount.qml");
1316 QSGText *myText = canvas->rootObject()->findChild<QSGText*>("myText");
1317 QVERIFY(myText != 0);
1319 QVERIFY(myText->lineCount() > 1);
1320 QVERIFY(!myText->truncated());
1321 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1323 myText->setMaximumLineCount(2);
1324 QCOMPARE(myText->lineCount(), 2);
1325 QCOMPARE(myText->truncated(), true);
1326 QCOMPARE(myText->maximumLineCount(), 2);
1328 myText->resetMaximumLineCount();
1329 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1330 QCOMPARE(myText->truncated(), false);
1332 myText->setElideMode(QSGText::ElideRight);
1333 myText->setMaximumLineCount(2);
1334 QCOMPARE(myText->lineCount(), 2);
1335 QCOMPARE(myText->truncated(), true);
1336 QCOMPARE(myText->maximumLineCount(), 2);
1341 void tst_qsgtext::lineHeight()
1343 QSGView *canvas = createView(SRCDIR "/data/lineHeight.qml");
1345 QSGText *myText = canvas->rootObject()->findChild<QSGText*>("myText");
1346 QVERIFY(myText != 0);
1348 QVERIFY(myText->lineHeight() == 1);
1349 QVERIFY(myText->lineHeightMode() == QSGText::ProportionalHeight);
1351 qreal h = myText->height();
1352 myText->setLineHeight(1.5);
1354 QEXPECT_FAIL("", "QTBUG-21009 fails", Continue);
1356 QVERIFY(myText->height() == h * 1.5);
1358 myText->setLineHeightMode(QSGText::FixedHeight);
1359 myText->setLineHeight(20);
1360 QCOMPARE(myText->height(), myText->lineCount() * 20.0);
1362 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.");
1363 myText->setLineHeightMode(QSGText::ProportionalHeight);
1364 myText->setLineHeight(1.0);
1366 qreal h2 = myText->height();
1367 myText->setLineHeight(2.0);
1368 QVERIFY(myText->height() == h2 * 2.0);
1370 myText->setLineHeightMode(QSGText::FixedHeight);
1371 myText->setLineHeight(10);
1372 QCOMPARE(myText->height(), myText->lineCount() * 10.0);
1377 void tst_qsgtext::implicitSize_data()
1379 QTest::addColumn<QString>("text");
1380 QTest::addColumn<QString>("wrap");
1381 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "Text.NoWrap";
1382 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.NoWrap";
1383 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "Text.Wrap";
1384 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.Wrap";
1387 void tst_qsgtext::implicitSize()
1389 QFETCH(QString, text);
1390 QFETCH(QString, wrap);
1391 QString componentStr = "import QtQuick 2.0\nText { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
1392 QDeclarativeComponent textComponent(&engine);
1393 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1394 QSGText *textObject = qobject_cast<QSGText*>(textComponent.create());
1396 QVERIFY(textObject->width() < textObject->implicitWidth());
1397 QVERIFY(textObject->height() == textObject->implicitHeight());
1399 textObject->resetWidth();
1400 QVERIFY(textObject->width() == textObject->implicitWidth());
1401 QVERIFY(textObject->height() == textObject->implicitHeight());
1406 void tst_qsgtext::lineLaidOut()
1408 QSGView *canvas = createView(SRCDIR "/data/lineLayout.qml");
1410 QSGText *myText = canvas->rootObject()->findChild<QSGText*>("myText");
1411 QVERIFY(myText != 0);
1413 QSGTextPrivate *textPrivate = QSGTextPrivate::get(myText);
1414 QVERIFY(textPrivate != 0);
1416 QTextDocument *doc = textPrivate->textDocument();
1419 QVERIFY(myText->lineCount() == textPrivate->linesRects.count());
1421 for (int i = 0; i < textPrivate->linesRects.count(); ++i) {
1422 QRectF r = textPrivate->linesRects.at(i);
1423 QVERIFY(r.width() == i * 15);
1425 QVERIFY(r.x() == r.width() + 30);
1427 QVERIFY(r.x() == r.width() * 2 + 60);
1428 QVERIFY(r.height() == 20);
1433 QTEST_MAIN(tst_qsgtext)
1435 #include "tst_qsgtext.moc"