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/qquicktext_p.h>
46 #include <private/qquicktext_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"
59 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
61 class tst_qquicktext : public QObject
69 void cleanupTestCase();
76 void alignments_data();
79 void embeddedImages_data();
80 void embeddedImages();
85 // ### these tests may be trivial
86 void horizontalAlignment();
87 void horizontalAlignment_RightToLeft();
88 void verticalAlignment();
94 // QDeclarativeFontValueType
99 void capitalization();
100 void letterSpacing();
105 void implicitSize_data();
112 QStringList standard;
113 QStringList richText;
115 QStringList horizontalAlignmentmentStrings;
116 QStringList verticalAlignmentmentStrings;
118 QList<Qt::Alignment> verticalAlignmentments;
119 QList<Qt::Alignment> horizontalAlignmentments;
121 QStringList styleStrings;
122 QList<QQuickText::TextStyle> styles;
124 QStringList colorStrings;
126 QDeclarativeEngine engine;
128 QQuickView *createView(const QString &filename);
130 void tst_qquicktext::initTestCase()
134 void tst_qquicktext::cleanupTestCase()
138 tst_qquicktext::tst_qquicktext()
140 standard << "the quick brown fox jumped over the lazy dog"
141 << "the quick brown fox\n jumped over the lazy dog";
143 richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
144 << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
146 horizontalAlignmentmentStrings << "AlignLeft"
150 verticalAlignmentmentStrings << "AlignTop"
154 horizontalAlignmentments << Qt::AlignLeft
158 verticalAlignmentments << Qt::AlignTop
162 styleStrings << "Normal"
167 styles << QQuickText::Normal
168 << QQuickText::Outline
169 << QQuickText::Raised
170 << QQuickText::Sunken;
172 colorStrings << "aliceblue"
185 // need a different test to do alpha channel test
191 QQuickView *tst_qquicktext::createView(const QString &filename)
193 QQuickView *canvas = new QQuickView(0);
195 canvas->setSource(QUrl::fromLocalFile(filename));
199 void tst_qquicktext::text()
202 QDeclarativeComponent textComponent(&engine);
203 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
204 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
206 QVERIFY(textObject != 0);
207 QCOMPARE(textObject->text(), QString(""));
208 QVERIFY(textObject->width() == 0);
213 for (int i = 0; i < standard.size(); i++)
215 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
216 QDeclarativeComponent textComponent(&engine);
217 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
219 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
221 QVERIFY(textObject != 0);
222 QCOMPARE(textObject->text(), standard.at(i));
223 QVERIFY(textObject->width() > 0);
228 for (int i = 0; i < richText.size(); i++)
230 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
231 QDeclarativeComponent textComponent(&engine);
232 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
233 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
235 QVERIFY(textObject != 0);
236 QString expected = richText.at(i);
237 QCOMPARE(textObject->text(), expected.replace("\\\"", "\""));
238 QVERIFY(textObject->width() > 0);
244 void tst_qquicktext::width()
246 // uses Font metrics to find the width for standard and document to find the width for rich
248 QDeclarativeComponent textComponent(&engine);
249 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
250 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
252 QVERIFY(textObject != 0);
253 QCOMPARE(textObject->width(), 0.);
258 bool requiresUnhintedMetrics = !qmlDisableDistanceField();
260 for (int i = 0; i < standard.size(); i++)
262 QVERIFY(!Qt::mightBeRichText(standard.at(i))); // self-test
265 qreal metricWidth = 0.0;
267 if (requiresUnhintedMetrics) {
268 QString s = standard.at(i);
269 s.replace(QLatin1Char('\n'), QChar::LineSeparator);
271 QTextLayout layout(s);
272 layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic);
275 option.setUseDesignMetrics(true);
276 layout.setTextOption(option);
279 layout.beginLayout();
281 QTextLine line = layout.createLine();
288 metricWidth = qCeil(layout.boundingRect().width());
291 qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
292 metricWidth = qCeil(metricWidth);
295 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
296 QDeclarativeComponent textComponent(&engine);
297 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
298 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
300 QVERIFY(textObject != 0);
301 QVERIFY(textObject->boundingRect().width() > 0);
302 QCOMPARE(textObject->width(), qreal(metricWidth));
303 QVERIFY(textObject->textFormat() == QQuickText::AutoText); // setting text doesn't change format
308 for (int i = 0; i < richText.size(); i++)
310 QVERIFY(Qt::mightBeRichText(richText.at(i))); // self-test
312 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\"; textFormat: Text.RichText }";
313 QDeclarativeComponent textComponent(&engine);
314 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
315 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
316 QVERIFY(textObject != 0);
318 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
319 QVERIFY(textPrivate != 0);
321 QTextDocument *doc = textPrivate->textDocument();
324 QCOMPARE(int(textObject->width()), int(doc->idealWidth()));
325 QVERIFY(textObject->textFormat() == QQuickText::RichText);
331 void tst_qquicktext::wrap()
334 // for specified width and wrap set true
336 QDeclarativeComponent textComponent(&engine);
337 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; wrapMode: Text.WordWrap; width: 300 }", QUrl::fromLocalFile(""));
338 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
339 textHeight = textObject->height();
341 QVERIFY(textObject != 0);
342 QVERIFY(textObject->wrapMode() == QQuickText::WordWrap);
343 QCOMPARE(textObject->width(), 300.);
348 for (int i = 0; i < standard.size(); i++)
350 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + standard.at(i) + "\" }";
351 QDeclarativeComponent textComponent(&engine);
352 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
353 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
355 QVERIFY(textObject != 0);
356 QCOMPARE(textObject->width(), 30.);
357 QVERIFY(textObject->height() > textHeight);
359 int oldHeight = textObject->height();
360 textObject->setWidth(100);
361 QVERIFY(textObject->height() < oldHeight);
366 for (int i = 0; i < richText.size(); i++)
368 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + richText.at(i) + "\" }";
369 QDeclarativeComponent textComponent(&engine);
370 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
371 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
373 QVERIFY(textObject != 0);
374 QCOMPARE(textObject->width(), 30.);
375 QVERIFY(textObject->height() > textHeight);
377 qreal oldHeight = textObject->height();
378 textObject->setWidth(100);
379 QVERIFY(textObject->height() < oldHeight);
384 // richtext again with a fixed height
385 for (int i = 0; i < richText.size(); i++)
387 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; height: 50; text: \"" + richText.at(i) + "\" }";
388 QDeclarativeComponent textComponent(&engine);
389 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
390 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
392 QVERIFY(textObject != 0);
393 QCOMPARE(textObject->width(), 30.);
394 QVERIFY(textObject->implicitHeight() > textHeight);
396 qreal oldHeight = textObject->implicitHeight();
397 textObject->setWidth(100);
398 QVERIFY(textObject->implicitHeight() < oldHeight);
404 void tst_qquicktext::elide()
406 for (QQuickText::TextElideMode m = QQuickText::ElideLeft; m<=QQuickText::ElideNone; m=QQuickText::TextElideMode(int(m)+1)) {
407 const char* elidename[]={"ElideLeft", "ElideRight", "ElideMiddle", "ElideNone"};
408 QString elide = "elide: Text." + QString(elidename[int(m)]) + ";";
410 // XXX Poor coverage.
413 QDeclarativeComponent textComponent(&engine);
414 textComponent.setData(("import QtQuick 2.0\nText { text: \"\"; "+elide+" width: 100 }").toLatin1(), QUrl::fromLocalFile(""));
415 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
417 QCOMPARE(textObject->elideMode(), m);
418 QCOMPARE(textObject->width(), 100.);
423 for (int i = 0; i < standard.size(); i++)
425 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + standard.at(i) + "\" }";
426 QDeclarativeComponent textComponent(&engine);
427 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
428 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
430 QCOMPARE(textObject->elideMode(), m);
431 QCOMPARE(textObject->width(), 100.);
436 // richtext - does nothing
437 for (int i = 0; i < richText.size(); i++)
439 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + richText.at(i) + "\" }";
440 QDeclarativeComponent textComponent(&engine);
441 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
442 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
444 QCOMPARE(textObject->elideMode(), m);
445 QCOMPARE(textObject->width(), 100.);
452 void tst_qquicktext::textFormat()
455 QDeclarativeComponent textComponent(&engine);
456 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile(""));
457 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
459 QVERIFY(textObject != 0);
460 QVERIFY(textObject->textFormat() == QQuickText::RichText);
462 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
463 QVERIFY(textPrivate != 0);
464 QVERIFY(textPrivate->richText == true);
469 QDeclarativeComponent textComponent(&engine);
470 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\" }", QUrl::fromLocalFile(""));
471 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
473 QVERIFY(textObject != 0);
474 QVERIFY(textObject->textFormat() == QQuickText::AutoText);
476 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
477 QVERIFY(textPrivate != 0);
478 QVERIFY(textPrivate->styledText == true);
483 QDeclarativeComponent textComponent(&engine);
484 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile(""));
485 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
487 QVERIFY(textObject != 0);
488 QVERIFY(textObject->textFormat() == QQuickText::PlainText);
495 void tst_qquicktext::alignments_data()
497 QTest::addColumn<int>("hAlign");
498 QTest::addColumn<int>("vAlign");
499 QTest::addColumn<QString>("expectfile");
501 QTest::newRow("LT") << int(Qt::AlignLeft) << int(Qt::AlignTop) << TESTDATA("alignments_lt.png");
502 QTest::newRow("RT") << int(Qt::AlignRight) << int(Qt::AlignTop) << TESTDATA("alignments_rt.png");
503 QTest::newRow("CT") << int(Qt::AlignHCenter) << int(Qt::AlignTop) << TESTDATA("alignments_ct.png");
505 QTest::newRow("LB") << int(Qt::AlignLeft) << int(Qt::AlignBottom) << TESTDATA("alignments_lb.png");
506 QTest::newRow("RB") << int(Qt::AlignRight) << int(Qt::AlignBottom) << TESTDATA("alignments_rb.png");
507 QTest::newRow("CB") << int(Qt::AlignHCenter) << int(Qt::AlignBottom) << TESTDATA("alignments_cb.png");
509 QTest::newRow("LC") << int(Qt::AlignLeft) << int(Qt::AlignVCenter) << TESTDATA("alignments_lc.png");
510 QTest::newRow("RC") << int(Qt::AlignRight) << int(Qt::AlignVCenter) << TESTDATA("alignments_rc.png");
511 QTest::newRow("CC") << int(Qt::AlignHCenter) << int(Qt::AlignVCenter) << TESTDATA("alignments_cc.png");
515 void tst_qquicktext::alignments()
517 QSKIP("Text alignment pixmap comparison tests will not work with scenegraph");
518 #if (0)// No widgets in scenegraph
521 QFETCH(QString, expectfile);
524 // Font-specific, but not likely platform-specific, so only test on one platform
526 fn.setRawName("-misc-fixed-medium-r-*-*-8-*-*-*-*-*-*-*");
527 QApplication::setFont(fn);
530 QQuickView *canvas = createView(TESTDATA("alignments.qml"));
533 canvas->requestActivateWindow();
535 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
537 QObject *ob = canvas->rootObject();
539 ob->setProperty("horizontalAlignment",hAlign);
540 ob->setProperty("verticalAlignment",vAlign);
541 QTRY_COMPARE(ob->property("running").toBool(),false);
542 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32);
543 actual.fill(qRgb(255,255,255));
547 QImage expect(expectfile);
550 // Font-specific, but not likely platform-specific, so only test on one platform
551 if (QApplicationPrivate::graphics_system_name == "raster" || QApplicationPrivate::graphics_system_name == "") {
552 QCOMPARE(actual,expect);
560 //the alignment tests may be trivial o.oa
561 void tst_qquicktext::horizontalAlignment()
563 //test one align each, and then test if two align fails.
565 for (int i = 0; i < standard.size(); i++)
567 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
569 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
570 QDeclarativeComponent textComponent(&engine);
571 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
572 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
574 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
580 for (int i = 0; i < richText.size(); i++)
582 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
584 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
585 QDeclarativeComponent textComponent(&engine);
586 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
587 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
589 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
597 void tst_qquicktext::horizontalAlignment_RightToLeft()
599 QQuickView *canvas = createView(TESTDATA("horizontalAlignment_RightToLeft.qml"));
600 QQuickText *text = canvas->rootObject()->findChild<QQuickText*>("text");
604 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(text);
605 QVERIFY(textPrivate != 0);
607 // implicit alignment should follow the reading direction of RTL text
608 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
609 QCOMPARE(text->effectiveHAlign(), text->hAlign());
610 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
612 // explicitly left aligned text
613 text->setHAlign(QQuickText::AlignLeft);
614 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
615 QCOMPARE(text->effectiveHAlign(), text->hAlign());
616 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
618 // explicitly right aligned text
619 text->setHAlign(QQuickText::AlignRight);
620 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
621 QCOMPARE(text->effectiveHAlign(), text->hAlign());
622 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
624 // change to rich text
625 QString textString = text->text();
626 text->setText(QString("<i>") + textString + QString("</i>"));
627 text->setTextFormat(QQuickText::RichText);
630 // implicitly aligned rich text should follow the reading direction of text
631 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
632 QCOMPARE(text->effectiveHAlign(), text->hAlign());
633 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
635 // explicitly left aligned rich text
636 text->setHAlign(QQuickText::AlignLeft);
637 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
638 QCOMPARE(text->effectiveHAlign(), text->hAlign());
639 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight);
641 // explicitly right aligned rich text
642 text->setHAlign(QQuickText::AlignRight);
643 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
644 QCOMPARE(text->effectiveHAlign(), text->hAlign());
645 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
647 text->setText(textString);
648 text->setTextFormat(QQuickText::PlainText);
650 // explicitly center aligned
651 text->setHAlign(QQuickText::AlignHCenter);
652 QCOMPARE(text->hAlign(), QQuickText::AlignHCenter);
653 QCOMPARE(text->effectiveHAlign(), text->hAlign());
654 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
655 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2);
657 // reseted alignment should go back to following the text reading direction
659 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
660 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
662 // mirror the text item
663 QQuickItemPrivate::get(text)->setLayoutMirror(true);
665 // mirrored implicit alignment should continue to follow the reading direction of the text
666 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
667 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
668 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
670 // mirrored explicitly right aligned behaves as left aligned
671 text->setHAlign(QQuickText::AlignRight);
672 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
673 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignLeft);
674 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
676 // mirrored explicitly left aligned behaves as right aligned
677 text->setHAlign(QQuickText::AlignLeft);
678 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
679 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
680 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
683 QQuickItemPrivate::get(text)->setLayoutMirror(false);
686 // English text should be implicitly left aligned
687 text->setText("Hello world!");
688 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
689 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
691 #ifndef Q_OS_MAC // QTBUG-18040
692 // empty text with implicit alignment follows the system locale-based
693 // keyboard input direction from QApplication::keyboardInputDirection
695 QCOMPARE(text->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
696 QQuickText::AlignLeft : QQuickText::AlignRight);
697 text->setHAlign(QQuickText::AlignRight);
698 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
703 #ifndef Q_OS_MAC // QTBUG-18040
704 // alignment of Text with no text set to it
705 QString componentStr = "import QtQuick 2.0\nText {}";
706 QDeclarativeComponent textComponent(&engine);
707 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
708 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
709 QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
710 QQuickText::AlignLeft : QQuickText::AlignRight);
715 void tst_qquicktext::verticalAlignment()
717 //test one align each, and then test if two align fails.
719 for (int i = 0; i < standard.size(); i++)
721 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
723 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
724 QDeclarativeComponent textComponent(&engine);
725 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
726 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
728 QVERIFY(textObject != 0);
729 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
735 for (int i = 0; i < richText.size(); i++)
737 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
739 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
740 QDeclarativeComponent textComponent(&engine);
741 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
742 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
744 QVERIFY(textObject != 0);
745 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
753 void tst_qquicktext::font()
755 //test size, then bold, then italic, then family
757 QString componentStr = "import QtQuick 2.0\nText { font.pointSize: 40; text: \"Hello World\" }";
758 QDeclarativeComponent textComponent(&engine);
759 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
760 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
762 QCOMPARE(textObject->font().pointSize(), 40);
763 QCOMPARE(textObject->font().bold(), false);
764 QCOMPARE(textObject->font().italic(), false);
770 QString componentStr = "import QtQuick 2.0\nText { font.pixelSize: 40; text: \"Hello World\" }";
771 QDeclarativeComponent textComponent(&engine);
772 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
773 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
775 QCOMPARE(textObject->font().pixelSize(), 40);
776 QCOMPARE(textObject->font().bold(), false);
777 QCOMPARE(textObject->font().italic(), false);
783 QString componentStr = "import QtQuick 2.0\nText { font.bold: true; text: \"Hello World\" }";
784 QDeclarativeComponent textComponent(&engine);
785 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
786 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
788 QCOMPARE(textObject->font().bold(), true);
789 QCOMPARE(textObject->font().italic(), false);
795 QString componentStr = "import QtQuick 2.0\nText { font.italic: true; text: \"Hello World\" }";
796 QDeclarativeComponent textComponent(&engine);
797 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
798 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
800 QCOMPARE(textObject->font().italic(), true);
801 QCOMPARE(textObject->font().bold(), false);
807 QString componentStr = "import QtQuick 2.0\nText { font.family: \"Helvetica\"; text: \"Hello World\" }";
808 QDeclarativeComponent textComponent(&engine);
809 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
810 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
812 QCOMPARE(textObject->font().family(), QString("Helvetica"));
813 QCOMPARE(textObject->font().bold(), false);
814 QCOMPARE(textObject->font().italic(), false);
820 QString componentStr = "import QtQuick 2.0\nText { font.family: \"\"; text: \"Hello World\" }";
821 QDeclarativeComponent textComponent(&engine);
822 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
823 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
825 QCOMPARE(textObject->font().family(), QString(""));
831 void tst_qquicktext::style()
834 for (int i = 0; i < styles.size(); i++)
836 QString componentStr = "import QtQuick 2.0\nText { style: \"" + styleStrings.at(i) + "\"; styleColor: \"white\"; text: \"Hello World\" }";
837 QDeclarativeComponent textComponent(&engine);
838 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
839 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
841 QCOMPARE((int)textObject->style(), (int)styles.at(i));
842 QCOMPARE(textObject->styleColor(), QColor("white"));
846 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello World\" }";
847 QDeclarativeComponent textComponent(&engine);
848 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
849 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
851 QRectF brPre = textObject->boundingRect();
852 textObject->setStyle(QQuickText::Outline);
853 QRectF brPost = textObject->boundingRect();
855 QVERIFY(brPre.width() < brPost.width());
856 QVERIFY(brPre.height() < brPost.height());
861 void tst_qquicktext::color()
864 for (int i = 0; i < colorStrings.size(); i++)
866 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
867 QDeclarativeComponent textComponent(&engine);
868 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
869 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
871 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
872 QCOMPARE(textObject->styleColor(), QColor());
877 for (int i = 0; i < colorStrings.size(); i++)
879 QString componentStr = "import QtQuick 2.0\nText { styleColor: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
880 QDeclarativeComponent textComponent(&engine);
881 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
882 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
884 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(i)));
885 // default color to black?
886 QCOMPARE(textObject->color(), QColor("black"));
891 for (int i = 0; i < colorStrings.size(); i++)
893 for (int j = 0; j < colorStrings.size(); j++)
895 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; styleColor: \"" + colorStrings.at(j) + "\"; text: \"Hello World\" }";
896 QDeclarativeComponent textComponent(&engine);
897 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
898 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
900 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
901 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(j)));
907 QString colorStr = "#AA001234";
908 QColor testColor("#001234");
909 testColor.setAlpha(170);
911 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStr + "\"; text: \"Hello World\" }";
912 QDeclarativeComponent textComponent(&engine);
913 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
914 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
916 QCOMPARE(textObject->color(), testColor);
922 void tst_qquicktext::smooth()
924 for (int i = 0; i < standard.size(); i++)
927 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + standard.at(i) + "\" }";
928 QDeclarativeComponent textComponent(&engine);
929 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
930 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
931 QCOMPARE(textObject->smooth(), true);
936 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
937 QDeclarativeComponent textComponent(&engine);
938 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
939 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
940 QCOMPARE(textObject->smooth(), false);
945 for (int i = 0; i < richText.size(); i++)
948 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + richText.at(i) + "\" }";
949 QDeclarativeComponent textComponent(&engine);
950 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
951 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
952 QCOMPARE(textObject->smooth(), true);
957 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
958 QDeclarativeComponent textComponent(&engine);
959 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
960 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
961 QCOMPARE(textObject->smooth(), false);
968 void tst_qquicktext::weight()
971 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
972 QDeclarativeComponent textComponent(&engine);
973 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
974 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
976 QVERIFY(textObject != 0);
977 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Normal);
982 QString componentStr = "import QtQuick 2.0\nText { font.weight: \"Bold\"; text: \"Hello world!\" }";
983 QDeclarativeComponent textComponent(&engine);
984 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
985 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
987 QVERIFY(textObject != 0);
988 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Bold);
994 void tst_qquicktext::underline()
997 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
998 QDeclarativeComponent textComponent(&engine);
999 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1000 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1002 QVERIFY(textObject != 0);
1003 QCOMPARE(textObject->font().underline(), false);
1008 QString componentStr = "import QtQuick 2.0\nText { font.underline: true; text: \"Hello world!\" }";
1009 QDeclarativeComponent textComponent(&engine);
1010 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1011 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1013 QVERIFY(textObject != 0);
1014 QCOMPARE(textObject->font().underline(), true);
1020 void tst_qquicktext::overline()
1023 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1024 QDeclarativeComponent textComponent(&engine);
1025 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1026 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1028 QVERIFY(textObject != 0);
1029 QCOMPARE(textObject->font().overline(), false);
1034 QString componentStr = "import QtQuick 2.0\nText { font.overline: true; text: \"Hello world!\" }";
1035 QDeclarativeComponent textComponent(&engine);
1036 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1037 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1039 QVERIFY(textObject != 0);
1040 QCOMPARE(textObject->font().overline(), true);
1046 void tst_qquicktext::strikeout()
1049 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1050 QDeclarativeComponent textComponent(&engine);
1051 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1052 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1054 QVERIFY(textObject != 0);
1055 QCOMPARE(textObject->font().strikeOut(), false);
1060 QString componentStr = "import QtQuick 2.0\nText { font.strikeout: true; text: \"Hello world!\" }";
1061 QDeclarativeComponent textComponent(&engine);
1062 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1063 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1065 QVERIFY(textObject != 0);
1066 QCOMPARE(textObject->font().strikeOut(), true);
1072 void tst_qquicktext::capitalization()
1075 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1076 QDeclarativeComponent textComponent(&engine);
1077 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1078 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1080 QVERIFY(textObject != 0);
1081 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::MixedCase);
1086 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllUppercase\" }";
1087 QDeclarativeComponent textComponent(&engine);
1088 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1089 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1091 QVERIFY(textObject != 0);
1092 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllUppercase);
1097 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllLowercase\" }";
1098 QDeclarativeComponent textComponent(&engine);
1099 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1100 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1102 QVERIFY(textObject != 0);
1103 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllLowercase);
1108 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"SmallCaps\" }";
1109 QDeclarativeComponent textComponent(&engine);
1110 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1111 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1113 QVERIFY(textObject != 0);
1114 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::SmallCaps);
1119 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"Capitalize\" }";
1120 QDeclarativeComponent textComponent(&engine);
1121 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1122 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1124 QVERIFY(textObject != 0);
1125 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::Capitalize);
1131 void tst_qquicktext::letterSpacing()
1134 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1135 QDeclarativeComponent textComponent(&engine);
1136 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1137 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1139 QVERIFY(textObject != 0);
1140 QCOMPARE(textObject->font().letterSpacing(), 0.0);
1145 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: -2 }";
1146 QDeclarativeComponent textComponent(&engine);
1147 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1148 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1150 QVERIFY(textObject != 0);
1151 QCOMPARE(textObject->font().letterSpacing(), -2.);
1156 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: 3 }";
1157 QDeclarativeComponent textComponent(&engine);
1158 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1159 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1161 QVERIFY(textObject != 0);
1162 QCOMPARE(textObject->font().letterSpacing(), 3.);
1168 void tst_qquicktext::wordSpacing()
1171 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1172 QDeclarativeComponent textComponent(&engine);
1173 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1174 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1176 QVERIFY(textObject != 0);
1177 QCOMPARE(textObject->font().wordSpacing(), 0.0);
1182 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: -50 }";
1183 QDeclarativeComponent textComponent(&engine);
1184 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1185 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1187 QVERIFY(textObject != 0);
1188 QCOMPARE(textObject->font().wordSpacing(), -50.);
1193 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: 200 }";
1194 QDeclarativeComponent textComponent(&engine);
1195 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1196 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1198 QVERIFY(textObject != 0);
1199 QCOMPARE(textObject->font().wordSpacing(), 200.);
1208 class EventSender : public QQuickItem
1211 void sendEvent(QMouseEvent *event) {
1212 if (event->type() == QEvent::MouseButtonPress)
1213 mousePressEvent(event);
1214 else if (event->type() == QEvent::MouseButtonRelease)
1215 mouseReleaseEvent(event);
1217 qWarning() << "Trying to send unsupported event type";
1221 class LinkTest : public QObject
1230 void linkClicked(QString l) { link = l; }
1233 void tst_qquicktext::clickLink()
1236 QString componentStr = "import QtQuick 2.0\nText { text: \"<a href=\\\"http://qt.nokia.com\\\">Hello world!</a>\" }";
1237 QDeclarativeComponent textComponent(&engine);
1238 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1239 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1241 QVERIFY(textObject != 0);
1244 QObject::connect(textObject, SIGNAL(linkActivated(QString)), &test, SLOT(linkClicked(QString)));
1247 QMouseEvent me(QEvent::MouseButtonPress,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1248 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1253 QMouseEvent me(QEvent::MouseButtonRelease,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1254 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1259 QCOMPARE(test.link, QLatin1String("http://qt.nokia.com"));
1265 void tst_qquicktext::embeddedImages_data()
1267 QTest::addColumn<QUrl>("qmlfile");
1268 QTest::addColumn<QString>("error");
1269 QTest::newRow("local") << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocal.qml")) << "";
1270 QTest::newRow("local-error") << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocalError.qml"))
1271 << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocalError.qml")).toString()+":3:1: QML Text: Cannot open: " + QUrl::fromLocalFile(TESTDATA("http/notexists.png")).toString();
1272 QTest::newRow("remote") << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemote.qml")) << "";
1273 QTest::newRow("remote-error") << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemoteError.qml"))
1274 << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemoteError.qml")).toString()+":3:1: QML Text: Error downloading http://127.0.0.1:14453/notexists.png - server replied: Not found";
1277 void tst_qquicktext::embeddedImages()
1281 QFETCH(QUrl, qmlfile);
1282 QFETCH(QString, error);
1284 TestHTTPServer server(14453);
1285 server.serveDirectory(TESTDATA("http"));
1287 if (!error.isEmpty())
1288 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
1290 QDeclarativeComponent textComponent(&engine, qmlfile);
1291 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1293 QVERIFY(textObject != 0);
1295 QTRY_COMPARE(textObject->resourcesLoading(), 0);
1297 QPixmap pm(TESTDATA("http/exists.png"));
1298 if (error.isEmpty()) {
1299 QCOMPARE(textObject->width(), double(pm.width()));
1300 QCOMPARE(textObject->height(), double(pm.height()));
1302 QVERIFY(16 != pm.width()); // check test is effective
1303 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
1304 QCOMPARE(textObject->height(), 16.0);
1310 void tst_qquicktext::lineCount()
1312 QQuickView *canvas = createView(TESTDATA("lineCount.qml"));
1314 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1315 QVERIFY(myText != 0);
1317 QVERIFY(myText->lineCount() > 1);
1318 QVERIFY(!myText->truncated());
1319 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1321 myText->setMaximumLineCount(2);
1322 QCOMPARE(myText->lineCount(), 2);
1323 QCOMPARE(myText->truncated(), true);
1324 QCOMPARE(myText->maximumLineCount(), 2);
1326 myText->resetMaximumLineCount();
1327 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1328 QCOMPARE(myText->truncated(), false);
1330 myText->setElideMode(QQuickText::ElideRight);
1331 myText->setMaximumLineCount(2);
1332 QCOMPARE(myText->lineCount(), 2);
1333 QCOMPARE(myText->truncated(), true);
1334 QCOMPARE(myText->maximumLineCount(), 2);
1339 void tst_qquicktext::lineHeight()
1341 QQuickView *canvas = createView(TESTDATA("lineHeight.qml"));
1343 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1344 QVERIFY(myText != 0);
1346 QVERIFY(myText->lineHeight() == 1);
1347 QVERIFY(myText->lineHeightMode() == QQuickText::ProportionalHeight);
1349 qreal h = myText->height();
1350 myText->setLineHeight(1.5);
1351 QEXPECT_FAIL("", "QTBUG-21009 fails", Continue);
1352 QVERIFY(myText->height() == h * 1.5);
1354 myText->setLineHeightMode(QQuickText::FixedHeight);
1355 myText->setLineHeight(20);
1356 QCOMPARE(myText->height(), myText->lineCount() * 20.0);
1358 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.");
1359 myText->setLineHeightMode(QQuickText::ProportionalHeight);
1360 myText->setLineHeight(1.0);
1362 qreal h2 = myText->height();
1363 myText->setLineHeight(2.0);
1364 QVERIFY(myText->height() == h2 * 2.0);
1366 myText->setLineHeightMode(QQuickText::FixedHeight);
1367 myText->setLineHeight(10);
1368 QCOMPARE(myText->height(), myText->lineCount() * 10.0);
1373 void tst_qquicktext::implicitSize_data()
1375 QTest::addColumn<QString>("text");
1376 QTest::addColumn<QString>("wrap");
1377 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "Text.NoWrap";
1378 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.NoWrap";
1379 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "Text.Wrap";
1380 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.Wrap";
1383 void tst_qquicktext::implicitSize()
1385 QFETCH(QString, text);
1386 QFETCH(QString, wrap);
1387 QString componentStr = "import QtQuick 2.0\nText { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
1388 QDeclarativeComponent textComponent(&engine);
1389 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1390 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1392 QVERIFY(textObject->width() < textObject->implicitWidth());
1393 QVERIFY(textObject->height() == textObject->implicitHeight());
1395 textObject->resetWidth();
1396 QVERIFY(textObject->width() == textObject->implicitWidth());
1397 QVERIFY(textObject->height() == textObject->implicitHeight());
1402 void tst_qquicktext::lineLaidOut()
1404 QQuickView *canvas = createView(TESTDATA("lineLayout.qml"));
1406 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1407 QVERIFY(myText != 0);
1409 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(myText);
1410 QVERIFY(textPrivate != 0);
1412 QTextDocument *doc = textPrivate->textDocument();
1415 QVERIFY(myText->lineCount() == textPrivate->linesRects.count());
1417 for (int i = 0; i < textPrivate->linesRects.count(); ++i) {
1418 QRectF r = textPrivate->linesRects.at(i);
1419 QVERIFY(r.width() == i * 15);
1421 QVERIFY(r.x() == r.width() + 30);
1423 QVERIFY(r.x() == r.width() * 2 + 60);
1424 QVERIFY(r.height() == 20);
1429 QTEST_MAIN(tst_qquicktext)
1431 #include "tst_qquicktext.moc"