1 /****************************************************************************
3 ** Copyright (C) 2012 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 <QtQuick/private/qquicktext_p.h>
46 #include <private/qquicktext_p_p.h>
47 #include <private/qdeclarativevaluetype_p.h>
48 #include <QFontMetrics>
49 #include <QGraphicsSceneMouseEvent>
51 #include <QtQuick/QQuickView>
52 #include <private/qapplication_p.h>
54 #include <QtGui/QMouseEvent>
55 #include "../../shared/util.h"
56 #include "testhttpserver.h"
58 DEFINE_BOOL_CONFIG_OPTION(qmlDisableDistanceField, QML_DISABLE_DISTANCEFIELD)
60 class tst_qquicktext : public QDeclarativeDataTest
71 void multilineElide();
74 void alignments_data();
77 void embeddedImages_data();
78 void embeddedImages();
83 // ### these tests may be trivial
84 void horizontalAlignment();
85 void horizontalAlignment_RightToLeft();
86 void verticalAlignment();
92 // QDeclarativeFontValueType
97 void capitalization();
103 void implicitSize_data();
110 QStringList standard;
111 QStringList richText;
113 QStringList horizontalAlignmentmentStrings;
114 QStringList verticalAlignmentmentStrings;
116 QList<Qt::Alignment> verticalAlignmentments;
117 QList<Qt::Alignment> horizontalAlignmentments;
119 QStringList styleStrings;
120 QList<QQuickText::TextStyle> styles;
122 QStringList colorStrings;
124 QDeclarativeEngine engine;
126 QQuickView *createView(const QString &filename);
129 tst_qquicktext::tst_qquicktext()
131 standard << "the quick brown fox jumped over the lazy dog"
132 << "the quick brown fox\n jumped over the lazy dog";
134 richText << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a> jumped over the <b>lazy</b> dog</i>"
135 << "<i>the <b>quick</b> brown <a href=\\\"http://www.google.com\\\">fox</a><br>jumped over the <b>lazy</b> dog</i>";
137 horizontalAlignmentmentStrings << "AlignLeft"
141 verticalAlignmentmentStrings << "AlignTop"
145 horizontalAlignmentments << Qt::AlignLeft
149 verticalAlignmentments << Qt::AlignTop
153 styleStrings << "Normal"
158 styles << QQuickText::Normal
159 << QQuickText::Outline
160 << QQuickText::Raised
161 << QQuickText::Sunken;
163 colorStrings << "aliceblue"
176 // need a different test to do alpha channel test
182 QQuickView *tst_qquicktext::createView(const QString &filename)
184 QQuickView *canvas = new QQuickView(0);
186 canvas->setSource(QUrl::fromLocalFile(filename));
190 void tst_qquicktext::text()
193 QDeclarativeComponent textComponent(&engine);
194 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
195 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
197 QVERIFY(textObject != 0);
198 QCOMPARE(textObject->text(), QString(""));
199 QVERIFY(textObject->width() == 0);
204 for (int i = 0; i < standard.size(); i++)
206 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
207 QDeclarativeComponent textComponent(&engine);
208 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
210 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
212 QVERIFY(textObject != 0);
213 QCOMPARE(textObject->text(), standard.at(i));
214 QVERIFY(textObject->width() > 0);
219 for (int i = 0; i < richText.size(); i++)
221 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
222 QDeclarativeComponent textComponent(&engine);
223 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
224 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
226 QVERIFY(textObject != 0);
227 QString expected = richText.at(i);
228 QCOMPARE(textObject->text(), expected.replace("\\\"", "\""));
229 QVERIFY(textObject->width() > 0);
235 void tst_qquicktext::width()
237 // uses Font metrics to find the width for standard and document to find the width for rich
239 QDeclarativeComponent textComponent(&engine);
240 textComponent.setData("import QtQuick 2.0\nText { text: \"\" }", QUrl::fromLocalFile(""));
241 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
243 QVERIFY(textObject != 0);
244 QCOMPARE(textObject->width(), 0.);
249 bool requiresUnhintedMetrics = !qmlDisableDistanceField();
251 for (int i = 0; i < standard.size(); i++)
253 QVERIFY(!Qt::mightBeRichText(standard.at(i))); // self-test
256 qreal metricWidth = 0.0;
258 if (requiresUnhintedMetrics) {
259 QString s = standard.at(i);
260 s.replace(QLatin1Char('\n'), QChar::LineSeparator);
262 QTextLayout layout(s);
263 layout.setFlags(Qt::TextExpandTabs | Qt::TextShowMnemonic);
266 option.setUseDesignMetrics(true);
267 layout.setTextOption(option);
270 layout.beginLayout();
272 QTextLine line = layout.createLine();
279 metricWidth = qCeil(layout.boundingRect().width());
282 qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
283 metricWidth = qCeil(metricWidth);
286 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
287 QDeclarativeComponent textComponent(&engine);
288 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
289 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
291 QVERIFY(textObject != 0);
292 QVERIFY(textObject->boundingRect().width() > 0);
293 QCOMPARE(textObject->width(), qreal(metricWidth));
294 QVERIFY(textObject->textFormat() == QQuickText::AutoText); // setting text doesn't change format
299 for (int i = 0; i < richText.size(); i++)
301 QVERIFY(Qt::mightBeRichText(richText.at(i))); // self-test
303 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\"; textFormat: Text.RichText }";
304 QDeclarativeComponent textComponent(&engine);
305 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
306 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
307 QVERIFY(textObject != 0);
309 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
310 QVERIFY(textPrivate != 0);
312 QTextDocument *doc = textPrivate->textDocument();
315 QCOMPARE(int(textObject->width()), int(doc->idealWidth()));
316 QVERIFY(textObject->textFormat() == QQuickText::RichText);
322 void tst_qquicktext::wrap()
325 // for specified width and wrap set true
327 QDeclarativeComponent textComponent(&engine);
328 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; wrapMode: Text.WordWrap; width: 300 }", QUrl::fromLocalFile(""));
329 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
330 textHeight = textObject->height();
332 QVERIFY(textObject != 0);
333 QVERIFY(textObject->wrapMode() == QQuickText::WordWrap);
334 QCOMPARE(textObject->width(), 300.);
339 for (int i = 0; i < standard.size(); i++)
341 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + standard.at(i) + "\" }";
342 QDeclarativeComponent textComponent(&engine);
343 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
344 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
346 QVERIFY(textObject != 0);
347 QCOMPARE(textObject->width(), 30.);
348 QVERIFY(textObject->height() > textHeight);
350 int oldHeight = textObject->height();
351 textObject->setWidth(100);
352 QVERIFY(textObject->height() < oldHeight);
357 for (int i = 0; i < richText.size(); i++)
359 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; text: \"" + richText.at(i) + "\" }";
360 QDeclarativeComponent textComponent(&engine);
361 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
362 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
364 QVERIFY(textObject != 0);
365 QCOMPARE(textObject->width(), 30.);
366 QVERIFY(textObject->height() > textHeight);
368 qreal oldHeight = textObject->height();
369 textObject->setWidth(100);
370 QVERIFY(textObject->height() < oldHeight);
375 // richtext again with a fixed height
376 for (int i = 0; i < richText.size(); i++)
378 QString componentStr = "import QtQuick 2.0\nText { wrapMode: Text.WordWrap; width: 30; height: 50; text: \"" + richText.at(i) + "\" }";
379 QDeclarativeComponent textComponent(&engine);
380 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
381 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
383 QVERIFY(textObject != 0);
384 QCOMPARE(textObject->width(), 30.);
385 QVERIFY(textObject->implicitHeight() > textHeight);
387 qreal oldHeight = textObject->implicitHeight();
388 textObject->setWidth(100);
389 QVERIFY(textObject->implicitHeight() < oldHeight);
395 void tst_qquicktext::elide()
397 for (QQuickText::TextElideMode m = QQuickText::ElideLeft; m<=QQuickText::ElideNone; m=QQuickText::TextElideMode(int(m)+1)) {
398 const char* elidename[]={"ElideLeft", "ElideRight", "ElideMiddle", "ElideNone"};
399 QString elide = "elide: Text." + QString(elidename[int(m)]) + ";";
401 // XXX Poor coverage.
404 QDeclarativeComponent textComponent(&engine);
405 textComponent.setData(("import QtQuick 2.0\nText { text: \"\"; "+elide+" width: 100 }").toLatin1(), QUrl::fromLocalFile(""));
406 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
408 QCOMPARE(textObject->elideMode(), m);
409 QCOMPARE(textObject->width(), 100.);
414 for (int i = 0; i < standard.size(); i++)
416 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + standard.at(i) + "\" }";
417 QDeclarativeComponent textComponent(&engine);
418 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
419 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
421 QCOMPARE(textObject->elideMode(), m);
422 QCOMPARE(textObject->width(), 100.);
427 // richtext - does nothing
428 for (int i = 0; i < richText.size(); i++)
430 QString componentStr = "import QtQuick 2.0\nText { "+elide+" width: 100; text: \"" + richText.at(i) + "\" }";
431 QDeclarativeComponent textComponent(&engine);
432 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
433 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
435 QCOMPARE(textObject->elideMode(), m);
436 QCOMPARE(textObject->width(), 100.);
443 void tst_qquicktext::multilineElide()
445 QQuickView *canvas = createView(testFile("multilineelide.qml"));
447 QQuickText *myText = qobject_cast<QQuickText*>(canvas->rootObject());
448 QVERIFY(myText != 0);
450 QCOMPARE(myText->lineCount(), 3);
451 QCOMPARE(myText->truncated(), true);
453 qreal lineHeight = myText->paintedHeight() / 3.;
455 // reduce size and ensure fewer lines are drawn
456 myText->setHeight(lineHeight * 2);
457 QCOMPARE(myText->lineCount(), 2);
459 myText->setHeight(lineHeight);
460 QCOMPARE(myText->lineCount(), 1);
462 myText->setHeight(5);
463 QCOMPARE(myText->lineCount(), 1);
465 myText->setHeight(lineHeight * 3);
466 QCOMPARE(myText->lineCount(), 3);
468 // remove max count and show all lines.
469 myText->setHeight(1000);
470 myText->resetMaximumLineCount();
472 QCOMPARE(myText->truncated(), false);
475 myText->setHeight(lineHeight * 2);
476 QCOMPARE(myText->lineCount(), 2);
477 QCOMPARE(myText->truncated(), true);
479 // change line height
480 myText->setLineHeight(1.1);
481 QCOMPARE(myText->lineCount(), 1);
486 void tst_qquicktext::textFormat()
489 QDeclarativeComponent textComponent(&engine);
490 textComponent.setData("import QtQuick 2.0\nText { text: \"Hello\"; textFormat: Text.RichText }", QUrl::fromLocalFile(""));
491 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
493 QVERIFY(textObject != 0);
494 QVERIFY(textObject->textFormat() == QQuickText::RichText);
496 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
497 QVERIFY(textPrivate != 0);
498 QVERIFY(textPrivate->richText == true);
503 QDeclarativeComponent textComponent(&engine);
504 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\" }", QUrl::fromLocalFile(""));
505 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
507 QVERIFY(textObject != 0);
508 QVERIFY(textObject->textFormat() == QQuickText::AutoText);
510 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(textObject);
511 QVERIFY(textPrivate != 0);
512 QVERIFY(textPrivate->styledText == true);
517 QDeclarativeComponent textComponent(&engine);
518 textComponent.setData("import QtQuick 2.0\nText { text: \"<b>Hello</b>\"; textFormat: Text.PlainText }", QUrl::fromLocalFile(""));
519 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
521 QVERIFY(textObject != 0);
522 QVERIFY(textObject->textFormat() == QQuickText::PlainText);
529 void tst_qquicktext::alignments_data()
531 QTest::addColumn<int>("hAlign");
532 QTest::addColumn<int>("vAlign");
533 QTest::addColumn<QString>("expectfile");
535 QTest::newRow("LT") << int(Qt::AlignLeft) << int(Qt::AlignTop) << testFile("alignments_lt.png");
536 QTest::newRow("RT") << int(Qt::AlignRight) << int(Qt::AlignTop) << testFile("alignments_rt.png");
537 QTest::newRow("CT") << int(Qt::AlignHCenter) << int(Qt::AlignTop) << testFile("alignments_ct.png");
539 QTest::newRow("LB") << int(Qt::AlignLeft) << int(Qt::AlignBottom) << testFile("alignments_lb.png");
540 QTest::newRow("RB") << int(Qt::AlignRight) << int(Qt::AlignBottom) << testFile("alignments_rb.png");
541 QTest::newRow("CB") << int(Qt::AlignHCenter) << int(Qt::AlignBottom) << testFile("alignments_cb.png");
543 QTest::newRow("LC") << int(Qt::AlignLeft) << int(Qt::AlignVCenter) << testFile("alignments_lc.png");
544 QTest::newRow("RC") << int(Qt::AlignRight) << int(Qt::AlignVCenter) << testFile("alignments_rc.png");
545 QTest::newRow("CC") << int(Qt::AlignHCenter) << int(Qt::AlignVCenter) << testFile("alignments_cc.png");
549 void tst_qquicktext::alignments()
551 QSKIP("Text alignment pixmap comparison tests will not work with scenegraph");
552 #if (0)// No widgets in scenegraph
555 QFETCH(QString, expectfile);
557 QQuickView *canvas = createView(testFile("alignments.qml"));
559 canvas->requestActivateWindow();
561 QTRY_COMPARE(QGuiApplication::activeWindow(), static_cast<QWidget *>(canvas));
563 QObject *ob = canvas->rootObject();
565 ob->setProperty("horizontalAlignment",hAlign);
566 ob->setProperty("verticalAlignment",vAlign);
567 QTRY_COMPARE(ob->property("running").toBool(),false);
568 QImage actual(canvas->width(), canvas->height(), QImage::Format_RGB32);
569 actual.fill(qRgb(255,255,255));
573 QImage expect(expectfile);
574 if (QGuiApplicationPrivate::graphics_system_name == "raster" || QGuiApplicationPrivate::graphics_system_name == "") {
575 QCOMPARE(actual,expect);
581 //the alignment tests may be trivial o.oa
582 void tst_qquicktext::horizontalAlignment()
584 //test one align each, and then test if two align fails.
586 for (int i = 0; i < standard.size(); i++)
588 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
590 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
591 QDeclarativeComponent textComponent(&engine);
592 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
593 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
595 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
601 for (int i = 0; i < richText.size(); i++)
603 for (int j=0; j < horizontalAlignmentmentStrings.size(); j++)
605 QString componentStr = "import QtQuick 2.0\nText { horizontalAlignment: \"" + horizontalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
606 QDeclarativeComponent textComponent(&engine);
607 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
608 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
610 QCOMPARE((int)textObject->hAlign(), (int)horizontalAlignmentments.at(j));
618 void tst_qquicktext::horizontalAlignment_RightToLeft()
620 QQuickView *canvas = createView(testFile("horizontalAlignment_RightToLeft.qml"));
621 QQuickText *text = canvas->rootObject()->findChild<QQuickText*>("text");
625 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(text);
626 QVERIFY(textPrivate != 0);
628 QTRY_VERIFY(textPrivate->layout.lineCount());
630 // implicit alignment should follow the reading direction of RTL text
631 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
632 QCOMPARE(text->effectiveHAlign(), text->hAlign());
633 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
635 // explicitly left aligned text
636 text->setHAlign(QQuickText::AlignLeft);
637 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
638 QCOMPARE(text->effectiveHAlign(), text->hAlign());
639 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
641 // explicitly right aligned text
642 text->setHAlign(QQuickText::AlignRight);
643 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
644 QCOMPARE(text->effectiveHAlign(), text->hAlign());
645 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
647 // change to rich text
648 QString textString = text->text();
649 text->setText(QString("<i>") + textString + QString("</i>"));
650 text->setTextFormat(QQuickText::RichText);
653 // implicitly aligned rich text should follow the reading direction of text
654 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
655 QCOMPARE(text->effectiveHAlign(), text->hAlign());
656 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
658 // explicitly left aligned rich text
659 text->setHAlign(QQuickText::AlignLeft);
660 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
661 QCOMPARE(text->effectiveHAlign(), text->hAlign());
662 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignRight);
664 // explicitly right aligned rich text
665 text->setHAlign(QQuickText::AlignRight);
666 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
667 QCOMPARE(text->effectiveHAlign(), text->hAlign());
668 QVERIFY(textPrivate->textDocument()->defaultTextOption().alignment() & Qt::AlignLeft);
670 text->setText(textString);
671 text->setTextFormat(QQuickText::PlainText);
673 // explicitly center aligned
674 text->setHAlign(QQuickText::AlignHCenter);
675 QCOMPARE(text->hAlign(), QQuickText::AlignHCenter);
676 QCOMPARE(text->effectiveHAlign(), text->hAlign());
677 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
678 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().right() > canvas->width()/2);
680 // reseted alignment should go back to following the text reading direction
682 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
683 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
685 // mirror the text item
686 QQuickItemPrivate::get(text)->setLayoutMirror(true);
688 // mirrored implicit alignment should continue to follow the reading direction of the text
689 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
690 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
691 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
693 // mirrored explicitly right aligned behaves as left aligned
694 text->setHAlign(QQuickText::AlignRight);
695 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
696 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignLeft);
697 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
699 // mirrored explicitly left aligned behaves as right aligned
700 text->setHAlign(QQuickText::AlignLeft);
701 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
702 QCOMPARE(text->effectiveHAlign(), QQuickText::AlignRight);
703 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() > canvas->width()/2);
706 QQuickItemPrivate::get(text)->setLayoutMirror(false);
709 // English text should be implicitly left aligned
710 text->setText("Hello world!");
711 QCOMPARE(text->hAlign(), QQuickText::AlignLeft);
712 QVERIFY(textPrivate->layout.lineAt(0).naturalTextRect().left() < canvas->width()/2);
714 // empty text with implicit alignment follows the system locale-based
715 // keyboard input direction from QInputPanel::inputDirection()
717 QCOMPARE(text->hAlign(), qApp->inputPanel()->inputDirection() == Qt::LeftToRight ?
718 QQuickText::AlignLeft : QQuickText::AlignRight);
719 text->setHAlign(QQuickText::AlignRight);
720 QCOMPARE(text->hAlign(), QQuickText::AlignRight);
724 // alignment of Text with no text set to it
725 QString componentStr = "import QtQuick 2.0\nText {}";
726 QDeclarativeComponent textComponent(&engine);
727 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
728 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
729 QCOMPARE(textObject->hAlign(), qApp->inputPanel()->inputDirection() == Qt::LeftToRight ?
730 QQuickText::AlignLeft : QQuickText::AlignRight);
734 void tst_qquicktext::verticalAlignment()
736 //test one align each, and then test if two align fails.
738 for (int i = 0; i < standard.size(); i++)
740 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
742 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + standard.at(i) + "\" }";
743 QDeclarativeComponent textComponent(&engine);
744 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
745 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
747 QVERIFY(textObject != 0);
748 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
754 for (int i = 0; i < richText.size(); i++)
756 for (int j=0; j < verticalAlignmentmentStrings.size(); j++)
758 QString componentStr = "import QtQuick 2.0\nText { verticalAlignment: \"" + verticalAlignmentmentStrings.at(j) + "\"; text: \"" + richText.at(i) + "\" }";
759 QDeclarativeComponent textComponent(&engine);
760 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
761 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
763 QVERIFY(textObject != 0);
764 QCOMPARE((int)textObject->vAlign(), (int)verticalAlignmentments.at(j));
772 void tst_qquicktext::font()
774 //test size, then bold, then italic, then family
776 QString componentStr = "import QtQuick 2.0\nText { font.pointSize: 40; text: \"Hello World\" }";
777 QDeclarativeComponent textComponent(&engine);
778 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
779 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
781 QCOMPARE(textObject->font().pointSize(), 40);
782 QCOMPARE(textObject->font().bold(), false);
783 QCOMPARE(textObject->font().italic(), false);
789 QString componentStr = "import QtQuick 2.0\nText { font.pixelSize: 40; text: \"Hello World\" }";
790 QDeclarativeComponent textComponent(&engine);
791 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
792 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
794 QCOMPARE(textObject->font().pixelSize(), 40);
795 QCOMPARE(textObject->font().bold(), false);
796 QCOMPARE(textObject->font().italic(), false);
802 QString componentStr = "import QtQuick 2.0\nText { font.bold: true; text: \"Hello World\" }";
803 QDeclarativeComponent textComponent(&engine);
804 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
805 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
807 QCOMPARE(textObject->font().bold(), true);
808 QCOMPARE(textObject->font().italic(), false);
814 QString componentStr = "import QtQuick 2.0\nText { font.italic: true; text: \"Hello World\" }";
815 QDeclarativeComponent textComponent(&engine);
816 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
817 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
819 QCOMPARE(textObject->font().italic(), true);
820 QCOMPARE(textObject->font().bold(), false);
826 QString componentStr = "import QtQuick 2.0\nText { font.family: \"Helvetica\"; text: \"Hello World\" }";
827 QDeclarativeComponent textComponent(&engine);
828 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
829 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
831 QCOMPARE(textObject->font().family(), QString("Helvetica"));
832 QCOMPARE(textObject->font().bold(), false);
833 QCOMPARE(textObject->font().italic(), false);
839 QString componentStr = "import QtQuick 2.0\nText { font.family: \"\"; text: \"Hello World\" }";
840 QDeclarativeComponent textComponent(&engine);
841 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
842 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
844 QCOMPARE(textObject->font().family(), QString(""));
850 void tst_qquicktext::style()
853 for (int i = 0; i < styles.size(); i++)
855 QString componentStr = "import QtQuick 2.0\nText { style: \"" + styleStrings.at(i) + "\"; styleColor: \"white\"; text: \"Hello World\" }";
856 QDeclarativeComponent textComponent(&engine);
857 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
858 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
860 QCOMPARE((int)textObject->style(), (int)styles.at(i));
861 QCOMPARE(textObject->styleColor(), QColor("white"));
865 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello World\" }";
866 QDeclarativeComponent textComponent(&engine);
867 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
868 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
870 QRectF brPre = textObject->boundingRect();
871 textObject->setStyle(QQuickText::Outline);
872 QRectF brPost = textObject->boundingRect();
874 QVERIFY(brPre.width() < brPost.width());
875 QVERIFY(brPre.height() < brPost.height());
880 void tst_qquicktext::color()
883 for (int i = 0; i < colorStrings.size(); i++)
885 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; text: \"Hello World\" }";
886 QDeclarativeComponent textComponent(&engine);
887 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
888 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
890 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
891 QCOMPARE(textObject->styleColor(), QColor());
896 for (int i = 0; i < colorStrings.size(); i++)
898 QString componentStr = "import QtQuick 2.0\nText { styleColor: \"" + colorStrings.at(i) + "\"; 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->styleColor(), QColor(colorStrings.at(i)));
904 // default color to black?
905 QCOMPARE(textObject->color(), QColor("black"));
910 for (int i = 0; i < colorStrings.size(); i++)
912 for (int j = 0; j < colorStrings.size(); j++)
914 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStrings.at(i) + "\"; styleColor: \"" + colorStrings.at(j) + "\"; text: \"Hello World\" }";
915 QDeclarativeComponent textComponent(&engine);
916 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
917 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
919 QCOMPARE(textObject->color(), QColor(colorStrings.at(i)));
920 QCOMPARE(textObject->styleColor(), QColor(colorStrings.at(j)));
926 QString colorStr = "#AA001234";
927 QColor testColor("#001234");
928 testColor.setAlpha(170);
930 QString componentStr = "import QtQuick 2.0\nText { color: \"" + colorStr + "\"; text: \"Hello World\" }";
931 QDeclarativeComponent textComponent(&engine);
932 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
933 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
935 QCOMPARE(textObject->color(), testColor);
941 void tst_qquicktext::smooth()
943 for (int i = 0; i < standard.size(); i++)
946 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + standard.at(i) + "\" }";
947 QDeclarativeComponent textComponent(&engine);
948 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
949 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
950 QCOMPARE(textObject->smooth(), true);
955 QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
956 QDeclarativeComponent textComponent(&engine);
957 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
958 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
959 QCOMPARE(textObject->smooth(), false);
964 for (int i = 0; i < richText.size(); i++)
967 QString componentStr = "import QtQuick 2.0\nText { smooth: true; text: \"" + richText.at(i) + "\" }";
968 QDeclarativeComponent textComponent(&engine);
969 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
970 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
971 QCOMPARE(textObject->smooth(), true);
976 QString componentStr = "import QtQuick 2.0\nText { text: \"" + richText.at(i) + "\" }";
977 QDeclarativeComponent textComponent(&engine);
978 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
979 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
980 QCOMPARE(textObject->smooth(), false);
987 void tst_qquicktext::weight()
990 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
991 QDeclarativeComponent textComponent(&engine);
992 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
993 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
995 QVERIFY(textObject != 0);
996 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Normal);
1001 QString componentStr = "import QtQuick 2.0\nText { font.weight: \"Bold\"; text: \"Hello world!\" }";
1002 QDeclarativeComponent textComponent(&engine);
1003 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1004 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1006 QVERIFY(textObject != 0);
1007 QCOMPARE((int)textObject->font().weight(), (int)QDeclarativeFontValueType::Bold);
1013 void tst_qquicktext::underline()
1016 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1017 QDeclarativeComponent textComponent(&engine);
1018 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1019 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1021 QVERIFY(textObject != 0);
1022 QCOMPARE(textObject->font().underline(), false);
1027 QString componentStr = "import QtQuick 2.0\nText { font.underline: true; text: \"Hello world!\" }";
1028 QDeclarativeComponent textComponent(&engine);
1029 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1030 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1032 QVERIFY(textObject != 0);
1033 QCOMPARE(textObject->font().underline(), true);
1039 void tst_qquicktext::overline()
1042 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1043 QDeclarativeComponent textComponent(&engine);
1044 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1045 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1047 QVERIFY(textObject != 0);
1048 QCOMPARE(textObject->font().overline(), false);
1053 QString componentStr = "import QtQuick 2.0\nText { font.overline: true; text: \"Hello world!\" }";
1054 QDeclarativeComponent textComponent(&engine);
1055 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1056 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1058 QVERIFY(textObject != 0);
1059 QCOMPARE(textObject->font().overline(), true);
1065 void tst_qquicktext::strikeout()
1068 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1069 QDeclarativeComponent textComponent(&engine);
1070 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1071 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1073 QVERIFY(textObject != 0);
1074 QCOMPARE(textObject->font().strikeOut(), false);
1079 QString componentStr = "import QtQuick 2.0\nText { font.strikeout: true; text: \"Hello world!\" }";
1080 QDeclarativeComponent textComponent(&engine);
1081 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1082 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1084 QVERIFY(textObject != 0);
1085 QCOMPARE(textObject->font().strikeOut(), true);
1091 void tst_qquicktext::capitalization()
1094 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1095 QDeclarativeComponent textComponent(&engine);
1096 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1097 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1099 QVERIFY(textObject != 0);
1100 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::MixedCase);
1105 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllUppercase\" }";
1106 QDeclarativeComponent textComponent(&engine);
1107 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1108 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1110 QVERIFY(textObject != 0);
1111 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllUppercase);
1116 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"AllLowercase\" }";
1117 QDeclarativeComponent textComponent(&engine);
1118 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1119 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1121 QVERIFY(textObject != 0);
1122 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::AllLowercase);
1127 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"SmallCaps\" }";
1128 QDeclarativeComponent textComponent(&engine);
1129 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1130 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1132 QVERIFY(textObject != 0);
1133 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::SmallCaps);
1138 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.capitalization: \"Capitalize\" }";
1139 QDeclarativeComponent textComponent(&engine);
1140 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1141 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1143 QVERIFY(textObject != 0);
1144 QCOMPARE((int)textObject->font().capitalization(), (int)QDeclarativeFontValueType::Capitalize);
1150 void tst_qquicktext::letterSpacing()
1153 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1154 QDeclarativeComponent textComponent(&engine);
1155 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1156 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1158 QVERIFY(textObject != 0);
1159 QCOMPARE(textObject->font().letterSpacing(), 0.0);
1164 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: -2 }";
1165 QDeclarativeComponent textComponent(&engine);
1166 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1167 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1169 QVERIFY(textObject != 0);
1170 QCOMPARE(textObject->font().letterSpacing(), -2.);
1175 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.letterSpacing: 3 }";
1176 QDeclarativeComponent textComponent(&engine);
1177 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1178 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1180 QVERIFY(textObject != 0);
1181 QCOMPARE(textObject->font().letterSpacing(), 3.);
1187 void tst_qquicktext::wordSpacing()
1190 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\" }";
1191 QDeclarativeComponent textComponent(&engine);
1192 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1193 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1195 QVERIFY(textObject != 0);
1196 QCOMPARE(textObject->font().wordSpacing(), 0.0);
1201 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: -50 }";
1202 QDeclarativeComponent textComponent(&engine);
1203 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1204 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1206 QVERIFY(textObject != 0);
1207 QCOMPARE(textObject->font().wordSpacing(), -50.);
1212 QString componentStr = "import QtQuick 2.0\nText { text: \"Hello world!\"; font.wordSpacing: 200 }";
1213 QDeclarativeComponent textComponent(&engine);
1214 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1215 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1217 QVERIFY(textObject != 0);
1218 QCOMPARE(textObject->font().wordSpacing(), 200.);
1227 class EventSender : public QQuickItem
1230 void sendEvent(QMouseEvent *event) {
1231 if (event->type() == QEvent::MouseButtonPress)
1232 mousePressEvent(event);
1233 else if (event->type() == QEvent::MouseButtonRelease)
1234 mouseReleaseEvent(event);
1236 qWarning() << "Trying to send unsupported event type";
1240 class LinkTest : public QObject
1249 void linkClicked(QString l) { link = l; }
1252 void tst_qquicktext::clickLink()
1255 QString componentStr = "import QtQuick 2.0\nText { text: \"<a href=\\\"http://qt.nokia.com\\\">Hello world!</a>\" }";
1256 QDeclarativeComponent textComponent(&engine);
1257 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1258 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1260 QVERIFY(textObject != 0);
1263 QObject::connect(textObject, SIGNAL(linkActivated(QString)), &test, SLOT(linkClicked(QString)));
1266 QMouseEvent me(QEvent::MouseButtonPress,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1267 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1272 QMouseEvent me(QEvent::MouseButtonRelease,QPointF(textObject->x()/2, textObject->y()/2), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
1273 static_cast<EventSender*>(static_cast<QQuickItem*>(textObject))->sendEvent(&me);
1278 QCOMPARE(test.link, QLatin1String("http://qt.nokia.com"));
1284 void tst_qquicktext::embeddedImages_data()
1286 QTest::addColumn<QUrl>("qmlfile");
1287 QTest::addColumn<QString>("error");
1288 QTest::newRow("local") << testFileUrl("embeddedImagesLocal.qml") << "";
1289 QTest::newRow("local-error") << testFileUrl("embeddedImagesLocalError.qml")
1290 << testFileUrl("embeddedImagesLocalError.qml").toString()+":3:1: QML Text: Cannot open: " + testFileUrl("http/notexists.png").toString();
1291 QTest::newRow("remote") << testFileUrl("embeddedImagesRemote.qml") << "";
1292 QTest::newRow("remote-error") << testFileUrl("embeddedImagesRemoteError.qml")
1293 << testFileUrl("embeddedImagesRemoteError.qml").toString()+":3:1: QML Text: Error downloading http://127.0.0.1:14453/notexists.png - server replied: Not found";
1296 void tst_qquicktext::embeddedImages()
1300 QFETCH(QUrl, qmlfile);
1301 QFETCH(QString, error);
1303 TestHTTPServer server(14453);
1304 server.serveDirectory(testFile("http"));
1306 if (!error.isEmpty())
1307 QTest::ignoreMessage(QtWarningMsg, error.toLatin1());
1309 QDeclarativeComponent textComponent(&engine, qmlfile);
1310 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1312 QVERIFY(textObject != 0);
1314 QTRY_COMPARE(textObject->resourcesLoading(), 0);
1316 QPixmap pm(testFile("http/exists.png"));
1317 if (error.isEmpty()) {
1318 QCOMPARE(textObject->width(), double(pm.width()));
1319 QCOMPARE(textObject->height(), double(pm.height()));
1321 QVERIFY(16 != pm.width()); // check test is effective
1322 QCOMPARE(textObject->width(), 16.0); // default size of QTextDocument broken image icon
1323 QCOMPARE(textObject->height(), 16.0);
1329 void tst_qquicktext::lineCount()
1331 QQuickView *canvas = createView(testFile("lineCount.qml"));
1333 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1334 QVERIFY(myText != 0);
1336 QVERIFY(myText->lineCount() > 1);
1337 QVERIFY(!myText->truncated());
1338 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1340 myText->setMaximumLineCount(2);
1341 QCOMPARE(myText->lineCount(), 2);
1342 QCOMPARE(myText->truncated(), true);
1343 QCOMPARE(myText->maximumLineCount(), 2);
1345 myText->resetMaximumLineCount();
1346 QCOMPARE(myText->maximumLineCount(), INT_MAX);
1347 QCOMPARE(myText->truncated(), false);
1349 myText->setElideMode(QQuickText::ElideRight);
1350 myText->setMaximumLineCount(2);
1351 QCOMPARE(myText->lineCount(), 2);
1352 QCOMPARE(myText->truncated(), true);
1353 QCOMPARE(myText->maximumLineCount(), 2);
1358 void tst_qquicktext::lineHeight()
1360 QQuickView *canvas = createView(testFile("lineHeight.qml"));
1362 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1363 QVERIFY(myText != 0);
1365 QVERIFY(myText->lineHeight() == 1);
1366 QVERIFY(myText->lineHeightMode() == QQuickText::ProportionalHeight);
1368 qreal h = myText->height();
1369 myText->setLineHeight(1.5);
1370 QVERIFY(myText->height() == qCeil(h * 1.5));
1372 myText->setLineHeightMode(QQuickText::FixedHeight);
1373 myText->setLineHeight(20);
1374 QCOMPARE(myText->height(), myText->lineCount() * 20.0);
1376 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.");
1377 myText->setLineHeightMode(QQuickText::ProportionalHeight);
1378 myText->setLineHeight(1.0);
1380 qreal h2 = myText->height();
1381 myText->setLineHeight(2.0);
1382 QVERIFY(myText->height() == h2 * 2.0);
1384 myText->setLineHeightMode(QQuickText::FixedHeight);
1385 myText->setLineHeight(10);
1386 QCOMPARE(myText->height(), myText->lineCount() * 10.0);
1391 void tst_qquicktext::implicitSize_data()
1393 QTest::addColumn<QString>("text");
1394 QTest::addColumn<QString>("wrap");
1395 QTest::newRow("plain") << "The quick red fox jumped over the lazy brown dog" << "Text.NoWrap";
1396 QTest::newRow("richtext") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.NoWrap";
1397 QTest::newRow("plain_wrap") << "The quick red fox jumped over the lazy brown dog" << "Text.Wrap";
1398 QTest::newRow("richtext_wrap") << "<b>The quick red fox jumped over the lazy brown dog</b>" << "Text.Wrap";
1401 void tst_qquicktext::implicitSize()
1403 QFETCH(QString, text);
1404 QFETCH(QString, wrap);
1405 QString componentStr = "import QtQuick 2.0\nText { text: \"" + text + "\"; width: 50; wrapMode: " + wrap + " }";
1406 QDeclarativeComponent textComponent(&engine);
1407 textComponent.setData(componentStr.toLatin1(), QUrl::fromLocalFile(""));
1408 QQuickText *textObject = qobject_cast<QQuickText*>(textComponent.create());
1410 QVERIFY(textObject->width() < textObject->implicitWidth());
1411 QVERIFY(textObject->height() == textObject->implicitHeight());
1413 textObject->resetWidth();
1414 QVERIFY(textObject->width() == textObject->implicitWidth());
1415 QVERIFY(textObject->height() == textObject->implicitHeight());
1420 void tst_qquicktext::lineLaidOut()
1422 QQuickView *canvas = createView(testFile("lineLayout.qml"));
1424 QQuickText *myText = canvas->rootObject()->findChild<QQuickText*>("myText");
1425 QVERIFY(myText != 0);
1427 QQuickTextPrivate *textPrivate = QQuickTextPrivate::get(myText);
1428 QVERIFY(textPrivate != 0);
1430 QTextDocument *doc = textPrivate->textDocument();
1433 #if defined(Q_OS_MAC)
1434 QVERIFY(myText->lineCount() == textPrivate->linesRects.count());
1437 for (int i = 0; i < textPrivate->layout.lineCount(); ++i) {
1438 QRectF r = textPrivate->layout.lineAt(i).rect();
1439 QVERIFY(r.width() == i * 15);
1441 QVERIFY(r.x() == r.width() + 30);
1443 QVERIFY(r.x() == r.width() * 2 + 60);
1444 QVERIFY(r.height() == 20);
1451 QTEST_MAIN(tst_qquicktext)
1453 #include "tst_qquicktext.moc"