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);
523 QQuickView *canvas = createView(TESTDATA("alignments.qml"));
525 canvas->requestActivateWindow();
527 QTRY_COMPARE(QApplication::activeWindow(), static_cast<QWidget *>(canvas));
529 QObject *ob = canvas->rootObject();
531 ob->setProperty("horizontalAlignment",hAlign);
532 ob->setProperty("verticalAlignment",vAlign);
533 QTRY_COMPARE(ob->property("running").toBool(),false);
534 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32);
535 actual.fill(qRgb(255,255,255));
539 QImage expect(expectfile);
540 if (QApplicationPrivate::graphics_system_name == "raster" || QApplicationPrivate::graphics_system_name == "") {
541 QCOMPARE(actual,expect);
547 //the alignment tests may be trivial o.oa
548 void tst_qquicktext::horizontalAlignment()
550 //test one align each, and then test if two align fails.
552 for (int i = 0; i < standard.size(); i++)
554 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
556 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
557 QDeclarativeComponent textComponent(&engine);
558 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
559 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
561 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
567 for (int i = 0; i < richText.size(); i++)
569 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
571 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
572 QDeclarativeComponent textComponent(&engine);
573 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
574 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
576 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
584 void tst_qquicktext::horizontalAlignment_RightToLeft()
586 QQuickView *canvas = createView(TESTDATA("horizontalAlignment_RightToLeft.qml"));
587 QQuickText *text = canvas->rootObject()->findChild<QQuickText*>("text");
591 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(text);
592 QVERIFY(textPrivate != 0);
594 // implicit alignment should follow the reading direction of RTL text
595 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
596 QCOMPARE(text->effectiveHAlign(), text->hAlign());
597 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
599 // explicitly left aligned text
600 text->setHAlign(QQuickText::AlignLeft);
601 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
602 QCOMPARE(text->effectiveHAlign(), text->hAlign());
603 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
605 // explicitly right aligned text
606 text->setHAlign(QQuickText::AlignRight);
607 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
608 QCOMPARE(text->effectiveHAlign(), text->hAlign());
609 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
611 // change to rich text
612 QString textString = text->text();
613 text->setText(QString("<i>") + textString + QString("</i>"));
614 text->setTextFormat(QQuickText::RichText);
617 // implicitly aligned rich text should follow the reading direction of text
618 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
619 QCOMPARE(text->effectiveHAlign(), text->hAlign());
620 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
622 // explicitly left aligned rich text
623 text->setHAlign(QQuickText::AlignLeft);
624 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
625 QCOMPARE(text->effectiveHAlign(), text->hAlign());
626 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight);
628 // explicitly right aligned rich text
629 text->setHAlign(QQuickText::AlignRight);
630 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
631 QCOMPARE(text->effectiveHAlign(), text->hAlign());
632 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
634 text->setText(textString);
635 text->setTextFormat(QQuickText::PlainText);
637 // explicitly center aligned
638 text->setHAlign(QQuickText::AlignHCenter);
639 QCOMPARE(text->hAlign(), QQuickText::AlignHCenter);
640 QCOMPARE(text->effectiveHAlign(), text->hAlign());
641 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
642 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2);
644 // reseted alignment should go back to following the text reading direction
646 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
647 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
649 // mirror the text item
650 QQuickItemPrivate::get(text)->setLayoutMirror(true);
652 // mirrored implicit alignment should continue to follow the reading direction of the text
653 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
654 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
655 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
657 // mirrored explicitly right aligned behaves as left aligned
658 text->setHAlign(QQuickText::AlignRight);
659 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
660 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignLeft);
661 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
663 // mirrored explicitly left aligned behaves as right aligned
664 text->setHAlign(QQuickText::AlignLeft);
665 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
666 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
667 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
670 QQuickItemPrivate::get(text)->setLayoutMirror(false);
673 // English text should be implicitly left aligned
674 text->setText("Hello world!");
675 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
676 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
678 #ifndef Q_OS_MAC // QTBUG-18040
679 // empty text with implicit alignment follows the system locale-based
680 // keyboard input direction from QApplication::keyboardInputDirection
682 QCOMPARE(text->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
683 QQuickText::AlignLeft : QQuickText::AlignRight);
684 text->setHAlign(QQuickText::AlignRight);
685 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
690 #ifndef Q_OS_MAC // QTBUG-18040
691 // alignment of Text with no text set to it
692 QString componentStr = "import QtQuick 2.0\nText {}";
693 QDeclarativeComponent textComponent(&engine);
694 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
695 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
696 QCOMPARE(textObject->hAlign(), QApplication::keyboardInputDirection() == Qt::LeftToRight ?
697 QQuickText::AlignLeft : QQuickText::AlignRight);
702 void tst_qquicktext::verticalAlignment()
704 //test one align each, and then test if two align fails.
706 for (int i = 0; i < standard.size(); i++)
708 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
710 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
711 QDeclarativeComponent textComponent(&engine);
712 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
713 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
715 QVERIFY(textObject != 0);
716 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
722 for (int i = 0; i < richText.size(); i++)
724 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
726 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
727 QDeclarativeComponent textComponent(&engine);
728 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
729 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
731 QVERIFY(textObject != 0);
732 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
740 void tst_qquicktext::font()
742 //test size, then bold, then italic, then family
744 QString componentStr = "import QtQuick 2.0\nText { font.pointSize: 40; text: \"Hello World\" }";
745 QDeclarativeComponent textComponent(&engine);
746 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
747 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
749 QCOMPARE(textObject->font().pointSize(), 40);
750 QCOMPARE(textObject->font().bold(), false);
751 QCOMPARE(textObject->font().italic(), false);
757 QString componentStr = "import QtQuick 2.0\nText { font.pixelSize: 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().pixelSize(), 40);
763 QCOMPARE(textObject->font().bold(), false);
764 QCOMPARE(textObject->font().italic(), false);
770 QString componentStr = "import QtQuick 2.0\nText { font.bold: true; 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().bold(), true);
776 QCOMPARE(textObject->font().italic(), false);
782 QString componentStr = "import QtQuick 2.0\nText { font.italic: true; text: \"Hello World\" }";
783 QDeclarativeComponent textComponent(&engine);
784 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
785 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
787 QCOMPARE(textObject->font().italic(), true);
788 QCOMPARE(textObject->font().bold(), false);
794 QString componentStr = "import QtQuick 2.0\nText { font.family: \"Helvetica\"; text: \"Hello World\" }";
795 QDeclarativeComponent textComponent(&engine);
796 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
797 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
799 QCOMPARE(textObject->font().family(), QString("Helvetica"));
800 QCOMPARE(textObject->font().bold(), false);
801 QCOMPARE(textObject->font().italic(), false);
807 QString componentStr = "import QtQuick 2.0\nText { font.family: \"\"; 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(""));
818 void tst_qquicktext::style()
821 for (int i = 0; i < styles.size(); i++)
823 QString componentStr = "import QtQuick 2.0\nText { style: \"" + styleStrings.at(i) + "\"; styleColor: \"white\"; text: \"Hello World\" }";
824 QDeclarativeComponent textComponent(&engine);
825 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
826 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
828 QCOMPARE((int)textObject->style(), (int)styles.at(i));
829 QCOMPARE(textObject->styleColor(), QColor("white"));
833 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello World\" }";
834 QDeclarativeComponent textComponent(&engine);
835 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
836 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
838 QRectF brPre = textObject->boundingRect();
839 textObject->setStyle(QQuickText::Outline);
840 QRectF brPost = textObject->boundingRect();
842 QVERIFY(brPre.width() < brPost.width());
843 QVERIFY(brPre.height() < brPost.height());
848 void tst_qquicktext::color()
851 for (int i = 0; i < colorStrings.size(); i++)
853 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
854 QDeclarativeComponent textComponent(&engine);
855 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
856 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
858 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
859 QCOMPARE(textObject->styleColor(), QColor());
864 for (int i = 0; i < colorStrings.size(); i++)
866 QString componentStr = "import QtQuick 2.0\nText { styleColor: \"" + 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->styleColor(), QColor(colorStrings.at(i)));
872 // default color to black?
873 QCOMPARE(textObject->color(), QColor("black"));
878 for (int i = 0; i < colorStrings.size(); i++)
880 for (int j = 0; j < colorStrings.size(); j++)
882 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; styleColor: \"" + colorStrings.at(j) + "\"; text: \"Hello World\" }";
883 QDeclarativeComponent textComponent(&engine);
884 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
885 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
887 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
888 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(j)));
894 QString colorStr = "#AA001234";
895 QColor testColor("#001234");
896 testColor.setAlpha(170);
898 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStr + "\"; text: \"Hello World\" }";
899 QDeclarativeComponent textComponent(&engine);
900 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
901 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
903 QCOMPARE(textObject->color(), testColor);
909 void tst_qquicktext::smooth()
911 for (int i = 0; i < standard.size(); i++)
914 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + standard.at(i) + "\" }";
915 QDeclarativeComponent textComponent(&engine);
916 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
917 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
918 QCOMPARE(textObject->smooth(), true);
923 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
924 QDeclarativeComponent textComponent(&engine);
925 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
926 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
927 QCOMPARE(textObject->smooth(), false);
932 for (int i = 0; i < richText.size(); i++)
935 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + richText.at(i) + "\" }";
936 QDeclarativeComponent textComponent(&engine);
937 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
938 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
939 QCOMPARE(textObject->smooth(), true);
944 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
945 QDeclarativeComponent textComponent(&engine);
946 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
947 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
948 QCOMPARE(textObject->smooth(), false);
955 void tst_qquicktext::weight()
958 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
959 QDeclarativeComponent textComponent(&engine);
960 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
961 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
963 QVERIFY(textObject != 0);
964 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Normal);
969 QString componentStr = "import QtQuick 2.0\nText { font.weight: \"Bold\"; text: \"Hello world!\" }";
970 QDeclarativeComponent textComponent(&engine);
971 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
972 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
974 QVERIFY(textObject != 0);
975 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Bold);
981 void tst_qquicktext::underline()
984 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
985 QDeclarativeComponent textComponent(&engine);
986 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
987 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
989 QVERIFY(textObject != 0);
990 QCOMPARE(textObject->font().underline(), false);
995 QString componentStr = "import QtQuick 2.0\nText { font.underline: true; text: \"Hello world!\" }";
996 QDeclarativeComponent textComponent(&engine);
997 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
998 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1000 QVERIFY(textObject != 0);
1001 QCOMPARE(textObject->font().underline(), true);
1007 void tst_qquicktext::overline()
1010 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1011 QDeclarativeComponent textComponent(&engine);
1012 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1013 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1015 QVERIFY(textObject != 0);
1016 QCOMPARE(textObject->font().overline(), false);
1021 QString componentStr = "import QtQuick 2.0\nText { font.overline: true; text: \"Hello world!\" }";
1022 QDeclarativeComponent textComponent(&engine);
1023 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1024 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1026 QVERIFY(textObject != 0);
1027 QCOMPARE(textObject->font().overline(), true);
1033 void tst_qquicktext::strikeout()
1036 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1037 QDeclarativeComponent textComponent(&engine);
1038 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1039 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1041 QVERIFY(textObject != 0);
1042 QCOMPARE(textObject->font().strikeOut(), false);
1047 QString componentStr = "import QtQuick 2.0\nText { font.strikeout: true; text: \"Hello world!\" }";
1048 QDeclarativeComponent textComponent(&engine);
1049 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1050 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1052 QVERIFY(textObject != 0);
1053 QCOMPARE(textObject->font().strikeOut(), true);
1059 void tst_qquicktext::capitalization()
1062 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1063 QDeclarativeComponent textComponent(&engine);
1064 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1065 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1067 QVERIFY(textObject != 0);
1068 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::MixedCase);
1073 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllUppercase\" }";
1074 QDeclarativeComponent textComponent(&engine);
1075 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1076 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1078 QVERIFY(textObject != 0);
1079 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllUppercase);
1084 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllLowercase\" }";
1085 QDeclarativeComponent textComponent(&engine);
1086 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1087 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1089 QVERIFY(textObject != 0);
1090 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllLowercase);
1095 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"SmallCaps\" }";
1096 QDeclarativeComponent textComponent(&engine);
1097 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1098 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1100 QVERIFY(textObject != 0);
1101 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::SmallCaps);
1106 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"Capitalize\" }";
1107 QDeclarativeComponent textComponent(&engine);
1108 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1109 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1111 QVERIFY(textObject != 0);
1112 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::Capitalize);
1118 void tst_qquicktext::letterSpacing()
1121 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1122 QDeclarativeComponent textComponent(&engine);
1123 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1124 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1126 QVERIFY(textObject != 0);
1127 QCOMPARE(textObject->font().letterSpacing(), 0.0);
1132 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: -2 }";
1133 QDeclarativeComponent textComponent(&engine);
1134 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1135 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1137 QVERIFY(textObject != 0);
1138 QCOMPARE(textObject->font().letterSpacing(), -2.);
1143 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: 3 }";
1144 QDeclarativeComponent textComponent(&engine);
1145 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1146 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1148 QVERIFY(textObject != 0);
1149 QCOMPARE(textObject->font().letterSpacing(), 3.);
1155 void tst_qquicktext::wordSpacing()
1158 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1159 QDeclarativeComponent textComponent(&engine);
1160 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1161 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1163 QVERIFY(textObject != 0);
1164 QCOMPARE(textObject->font().wordSpacing(), 0.0);
1169 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: -50 }";
1170 QDeclarativeComponent textComponent(&engine);
1171 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1172 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1174 QVERIFY(textObject != 0);
1175 QCOMPARE(textObject->font().wordSpacing(), -50.);
1180 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: 200 }";
1181 QDeclarativeComponent textComponent(&engine);
1182 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1183 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1185 QVERIFY(textObject != 0);
1186 QCOMPARE(textObject->font().wordSpacing(), 200.);
1195 class EventSender : public QQuickItem
1198 void sendEvent(QMouseEvent *event) {
1199 if (event->type() == QEvent::MouseButtonPress)
1200 mousePressEvent(event);
1201 else if (event->type() == QEvent::MouseButtonRelease)
1202 mouseReleaseEvent(event);
1204 qWarning() << "Trying to send unsupported event type";
1208 class LinkTest : public QObject
1217 void linkClicked(QString l) { link = l; }
1220 void tst_qquicktext::clickLink()
1223 QString componentStr = "import QtQuick 2.0\nText { text: \"<a href=\\\"http://qt.nokia.com\\\">Hello world!</a>\" }";
1224 QDeclarativeComponent textComponent(&engine);
1225 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1226 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1228 QVERIFY(textObject != 0);
1231 QObject::connect(textObject, SIGNAL(linkActivated(QString)), &test, SLOT(linkClicked(QString)));
1234 QMouseEvent me(QEvent::MouseButtonPress,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1235 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1240 QMouseEvent me(QEvent::MouseButtonRelease,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1241 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1246 QCOMPARE(test.link, QLatin1String("http://qt.nokia.com"));
1252 void tst_qquicktext::embeddedImages_data()
1254 QTest::addColumn<QUrl>("qmlfile");
1255 QTest::addColumn<QString>("error");
1256 QTest::newRow("local") << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocal.qml")) << "";
1257 QTest::newRow("local-error") << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocalError.qml"))
1258 << QUrl::fromLocalFile(TESTDATA("embeddedImagesLocalError.qml")).toString()+":3:1: QML Text: Cannot open: " + QUrl::fromLocalFile(TESTDATA("http/notexists.png")).toString();
1259 QTest::newRow("remote") << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemote.qml")) << "";
1260 QTest::newRow("remote-error") << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemoteError.qml"))
1261 << QUrl::fromLocalFile(TESTDATA("embeddedImagesRemoteError.qml")).toString()+":3:1: QML Text: Error downloading http://127.0.0.1:14453/notexists.png - server replied: Not found";
1264 void tst_qquicktext::embeddedImages()
1268 QFETCH(QUrl, qmlfile);
1269 QFETCH(QString, error);
1271 TestHTTPServer server(14453);
1272 server.serveDirectory(TESTDATA("http"));
1274 if (!error.isEmpty())
1275 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
1277 QDeclarativeComponent textComponent(&engine, qmlfile);
1278 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1280 QVERIFY(textObject != 0);
1282 QTRY_COMPARE(textObject->resourcesLoading(), 0);
1284 QPixmap pm(TESTDATA("http/exists.png"));
1285 if (error.isEmpty()) {
1286 QCOMPARE(textObject->width(), double(pm.width()));
1287 QCOMPARE(textObject->height(), double(pm.height()));
1289 QVERIFY(16 != pm.width()); // check test is effective
1290 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
1291 QCOMPARE(textObject->height(), 16.0);
1297 void tst_qquicktext::lineCount()
1299 QQuickView *canvas = createView(TESTDATA("lineCount.qml"));
1301 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1302 QVERIFY(myText != 0);
1304 QVERIFY(myText->lineCount() > 1);
1305 QVERIFY(!myText->truncated());
1306 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1308 myText->setMaximumLineCount(2);
1309 QCOMPARE(myText->lineCount(), 2);
1310 QCOMPARE(myText->truncated(), true);
1311 QCOMPARE(myText->maximumLineCount(), 2);
1313 myText->resetMaximumLineCount();
1314 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1315 QCOMPARE(myText->truncated(), false);
1317 myText->setElideMode(QQuickText::ElideRight);
1318 myText->setMaximumLineCount(2);
1319 QCOMPARE(myText->lineCount(), 2);
1320 QCOMPARE(myText->truncated(), true);
1321 QCOMPARE(myText->maximumLineCount(), 2);
1326 void tst_qquicktext::lineHeight()
1328 QQuickView *canvas = createView(TESTDATA("lineHeight.qml"));
1330 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1331 QVERIFY(myText != 0);
1333 QVERIFY(myText->lineHeight() == 1);
1334 QVERIFY(myText->lineHeightMode() == QQuickText::ProportionalHeight);
1336 qreal h = myText->height();
1337 myText->setLineHeight(1.5);
1338 QEXPECT_FAIL("", "QTBUG-21009 fails", Continue);
1339 QVERIFY(myText->height() == h * 1.5);
1341 myText->setLineHeightMode(QQuickText::FixedHeight);
1342 myText->setLineHeight(20);
1343 QCOMPARE(myText->height(), myText->lineCount() * 20.0);
1345 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.");
1346 myText->setLineHeightMode(QQuickText::ProportionalHeight);
1347 myText->setLineHeight(1.0);
1349 qreal h2 = myText->height();
1350 myText->setLineHeight(2.0);
1351 QVERIFY(myText->height() == h2 * 2.0);
1353 myText->setLineHeightMode(QQuickText::FixedHeight);
1354 myText->setLineHeight(10);
1355 QCOMPARE(myText->height(), myText->lineCount() * 10.0);
1360 void tst_qquicktext::implicitSize_data()
1362 QTest::addColumn<QString>("text");
1363 QTest::addColumn<QString>("wrap");
1364 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "Text.NoWrap";
1365 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.NoWrap";
1366 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "Text.Wrap";
1367 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.Wrap";
1370 void tst_qquicktext::implicitSize()
1372 QFETCH(QString, text);
1373 QFETCH(QString, wrap);
1374 QString componentStr = "import QtQuick 2.0\nText { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
1375 QDeclarativeComponent textComponent(&engine);
1376 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1377 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1379 QVERIFY(textObject->width() < textObject->implicitWidth());
1380 QVERIFY(textObject->height() == textObject->implicitHeight());
1382 textObject->resetWidth();
1383 QVERIFY(textObject->width() == textObject->implicitWidth());
1384 QVERIFY(textObject->height() == textObject->implicitHeight());
1389 void tst_qquicktext::lineLaidOut()
1391 QQuickView *canvas = createView(TESTDATA("lineLayout.qml"));
1393 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1394 QVERIFY(myText != 0);
1396 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(myText);
1397 QVERIFY(textPrivate != 0);
1399 QTextDocument *doc = textPrivate->textDocument();
1402 QVERIFY(myText->lineCount() == textPrivate->linesRects.count());
1404 for (int i = 0; i < textPrivate->linesRects.count(); ++i) {
1405 QRectF r = textPrivate->linesRects.at(i);
1406 QVERIFY(r.width() == i * 15);
1408 QVERIFY(r.x() == r.width() + 30);
1410 QVERIFY(r.x() == r.width() * 2 + 60);
1411 QVERIFY(r.height() == 20);
1416 QTEST_MAIN(tst_qquicktext)
1418 #include "tst_qquicktext.moc"