1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28#include <qtest.h>
29#include <QtTest/QtTest>
30#include <QtGui/QTextLayout>
31#include <QtCore/QList>
32#include <QtQuick/private/qquickstyledtext_p.h>
33
34class tst_qquickstyledtext : public QObject
35{
36 Q_OBJECT
37public:
38 tst_qquickstyledtext()
39 {
40 }
41
42 struct Format {
43 enum Type {
44 Bold = 0x01,
45 Underline = 0x02,
46 Italic = 0x04,
47 Anchor = 0x08,
48 StrikeOut = 0x10
49 };
50 Format(int t, int s, int l)
51 : type(t), start(s), length(l) {}
52 int type;
53 int start;
54 int length;
55 };
56 typedef QList<Format> FormatList;
57
58 static const QChar bullet;
59 static const QChar disc;
60 static const QChar square;
61
62private slots:
63 void textOutput();
64 void textOutput_data();
65 void anchors();
66 void anchors_data();
67 void longString();
68};
69
70Q_DECLARE_METATYPE(tst_qquickstyledtext::FormatList);
71
72const QChar tst_qquickstyledtext::bullet(0x2022);
73const QChar tst_qquickstyledtext::disc(0x25e6);
74const QChar tst_qquickstyledtext::square(0x25a1);
75
76// For malformed input all we test is that we get the expected text and format out.
77//
78void tst_qquickstyledtext::textOutput_data()
79{
80 QTest::addColumn<QString>(name: "input");
81 QTest::addColumn<QString>(name: "output");
82 QTest::addColumn<FormatList>(name: "formats");
83 QTest::addColumn<bool>(name: "modifiesFontSize");
84
85 QTest::newRow(dataTag: "empty") << "" << "" << FormatList() << false;
86 QTest::newRow(dataTag: "empty tag") << "<>test</>" << "test" << FormatList() << false;
87 QTest::newRow(dataTag: "nest opening") << "<b<b>>test</b>" << ">test" << FormatList() << false;
88 QTest::newRow(dataTag: "nest closing") << "<b>test<</b>/b>" << "test/b>" << (FormatList() << Format(Format::Bold, 0, 7)) << false;
89 QTest::newRow(dataTag: "bold") << "<b>bold</b>" << "bold" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
90 QTest::newRow(dataTag: "bold 2") << "<b>>>>>bold</b>" << ">>>>bold" << (FormatList() << Format(Format::Bold, 0, 8)) << false;
91 QTest::newRow(dataTag: "bold 3") << "<b>bold<>/b>" << "bold/b>" << (FormatList() << Format(Format::Bold, 0, 7)) << false;
92 QTest::newRow(dataTag: "italic") << "<i>italic</i>" << "italic" << (FormatList() << Format(Format::Italic, 0, 6)) << false;
93 QTest::newRow(dataTag: "underline") << "<u>underline</u>" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)) << false;
94 QTest::newRow(dataTag: "strong") << "<strong>strong</strong>" << "strong" << (FormatList() << Format(Format::Bold, 0, 6)) << false;
95 QTest::newRow(dataTag: "underline") << "<u>underline</u>" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)) << false;
96 QTest::newRow(dataTag: "strike out s") << "<s>strike out</s>" << "strike out" << (FormatList() << Format(Format::StrikeOut, 0, 10)) << false;
97 QTest::newRow(dataTag: "strike out del") << "<del>strike out</del>" << "strike out" << (FormatList() << Format(Format::StrikeOut, 0, 10)) << false;
98 QTest::newRow(dataTag: "strike out not s") << "this is <s>not</s> a test" << "this is not a test" << (FormatList() << Format(Format::StrikeOut, 8, 3)) << false;
99 QTest::newRow(dataTag: "strike out not del") << "this is <del>not</del> a test" << "this is not a test" << (FormatList() << Format(Format::StrikeOut, 8, 3)) << false;
100 QTest::newRow(dataTag: "missing >") << "<b>text</b" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
101 QTest::newRow(dataTag: "missing b>") << "<b>text</" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
102 QTest::newRow(dataTag: "missing /b>") << "<b>text<" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
103 QTest::newRow(dataTag: "missing </b>") << "<b>text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
104 QTest::newRow(dataTag: "nested") << "<b>text <i>italic</i> bold</b>" << "text italic bold" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6) << Format(Format::Bold, 11, 5)) << false;
105 QTest::newRow(dataTag: "bad nest") << "<b>text <i>italic</b></i>" << "text italic" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6)) << false;
106 QTest::newRow(dataTag: "font color") << "<font color=\"red\">red text</font>" << "red text" << (FormatList() << Format(0, 0, 8)) << false;
107 QTest::newRow(dataTag: "font color: single quote") << "<font color='red'>red text</font>" << "red text" << (FormatList() << Format(0, 0, 8)) << false;
108 QTest::newRow(dataTag: "font size") << "<font size=\"1\">text</font>" << "text" << (FormatList() << Format(0, 0, 4)) << true;
109 QTest::newRow(dataTag: "font empty") << "<font>text</font>" << "text" << FormatList() << false;
110 QTest::newRow(dataTag: "font bad 1") << "<font ezis=\"blah\">text</font>" << "text" << FormatList() << false;
111 QTest::newRow(dataTag: "font bad 2") << "<font size=\"1>text</font>" << "" << FormatList() << false;
112 QTest::newRow(dataTag: "extra close") << "<b>text</b></b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
113 QTest::newRow(dataTag: "extra space") << "<b >text</b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false;
114 QTest::newRow(dataTag: "entities") << "&lt;b&gt;&quot;this&quot; &amp; that&lt;/b&gt;" << "<b>\"this\" & that</b>" << FormatList() << false;
115 QTest::newRow(dataTag: "newline") << "text<br>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
116 QTest::newRow(dataTag: "self-closing newline") << "text<br/>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
117 QTest::newRow(dataTag: "paragraph") << "text<p>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
118 QTest::newRow(dataTag: "paragraph closed") << "text<p>more text</p>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
119 QTest::newRow(dataTag: "paragraph closed bold") << "<b>text<p>more text</p>more text</b>" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << (FormatList() << Format(Format::Bold, 0, 24)) << false;
120 QTest::newRow(dataTag: "unknown tag") << "<a href='#'><foo>underline</foo></a> not" << "underline not" << (FormatList() << Format(Format::Underline, 0, 9)) << false;
121 QTest::newRow(dataTag: "ordered list") << "<ol><li>one<li>two" << QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList() << false;
122 QTest::newRow(dataTag: "ordered list closed") << "<ol><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
123 QTest::newRow(dataTag: "ordered list alpha") << "<ol type=\"a\"><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("a.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("b.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
124 QTest::newRow(dataTag: "ordered list upper alpha") << "<ol type=\"A\"><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("A.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("B.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
125 QTest::newRow(dataTag: "ordered list roman") << "<ol type=\"i\"><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("i.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("ii.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
126 QTest::newRow(dataTag: "ordered list upper roman") << "<ol type=\"I\"><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("I.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("II.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
127 QTest::newRow(dataTag: "ordered list bad") << "<ol type=\"z\"><li>one</li><li>two</li></ol>" << QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
128 QTest::newRow(dataTag: "unordered list") << "<ul><li>one<li>two" << QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList() << false;
129 QTest::newRow(dataTag: "unordered list closed") << "<ul><li>one</li><li>two</li></ul>" << QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
130 QTest::newRow(dataTag: "unordered list disc") << "<ul type=\"disc\"><li>one</li><li>two</li></ul>" << QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
131 QTest::newRow(dataTag: "unordered list square") << "<ul type=\"square\"><li>one</li><li>two</li></ul>" << QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
132 QTest::newRow(dataTag: "unordered list bad") << "<ul type=\"bad\"><li>one</li><li>two</li></ul>" << QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false;
133 QTest::newRow(dataTag: "header close") << "<h1>head</h1>more" << QLatin1String("head") + QChar(QChar::LineSeparator) + QLatin1String("more") << (FormatList() << Format(Format::Bold, 0, 4)) << true;
134 QTest::newRow(dataTag: "h0") << "<h0>head" << "head" << FormatList() << false;
135 QTest::newRow(dataTag: "h1") << "<h1>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
136 QTest::newRow(dataTag: "h2") << "<h2>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
137 QTest::newRow(dataTag: "h3") << "<h3>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
138 QTest::newRow(dataTag: "h4") << "<h4>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
139 QTest::newRow(dataTag: "h5") << "<h5>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
140 QTest::newRow(dataTag: "h6") << "<h6>head" << "head" << (FormatList() << Format(Format::Bold, 0, 4)) << true;
141 QTest::newRow(dataTag: "h7") << "<h7>head" << "head" << FormatList() << false;
142 QTest::newRow(dataTag: "pre") << "normal<pre>pre text</pre>normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 9)) << false;
143 QTest::newRow(dataTag: "pre lb") << "normal<pre>pre\n text</pre>normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::LineSeparator) + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 10)) << false;
144 QTest::newRow(dataTag: "line feed") << "line\nfeed" << "line feed" << FormatList() << false;
145 QTest::newRow(dataTag: "leading whitespace") << " leading whitespace" << "leading whitespace" << FormatList() << false;
146 QTest::newRow(dataTag: "trailing whitespace") << "trailing whitespace " << "trailing whitespace" << FormatList() << false;
147 QTest::newRow(dataTag: "consecutive whitespace") << " consecutive \t \n whitespace" << "consecutive whitespace" << FormatList() << false;
148 QTest::newRow(dataTag: "space after newline") << "text<br/> more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
149 QTest::newRow(dataTag: "space after paragraph") << "text<p> more text</p> more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false;
150 QTest::newRow(dataTag: "space in header") << "<h1> head</h1> " << QLatin1String("head") + QChar(QChar::LineSeparator) << (FormatList() << Format(Format::Bold, 0, 4)) << true;
151 QTest::newRow(dataTag: "space before bold") << "this is <b>bold</b>" << "this is bold" << (FormatList() << Format(Format::Bold, 8, 4)) << false;
152 QTest::newRow(dataTag: "space leading bold") << "this is<b> bold</b>" << "this is bold" << (FormatList() << Format(Format::Bold, 7, 5)) << false;
153 QTest::newRow(dataTag: "space trailing bold") << "this is <b>bold </b>" << "this is bold " << (FormatList() << Format(Format::Bold, 8, 5)) << false;
154 QTest::newRow(dataTag: "img") << "a<img src=\"blah.png\"/>b" << "a b" << FormatList() << false;
155 QTest::newRow(dataTag: "tag mix") << "<f6>ds<b></img><pro>gfh</b><w><w>ghj</stron><ql><sl><pl>dfg</j6><img><bol><r><prp>dfg<bkj></b><up><string>ewrq</al><bl>jklhj<zl>" << "dsgfhghjdfgdfgewrqjklhj" << (FormatList() << Format(Format::Bold, 2, 3)) << false;
156 QTest::newRow(dataTag: "named html entities") << "&gt; &lt; &amp; &quot; &nbsp;" << QLatin1String("> < & \" ") + QChar(QChar::Nbsp) << FormatList() << false;
157 QTest::newRow(dataTag: "invalid html entities") << "a &hello & a &goodbye;" << "a &hello & a " << FormatList() << false;
158}
159
160void tst_qquickstyledtext::textOutput()
161{
162 QFETCH(QString, input);
163 QFETCH(QString, output);
164 QFETCH(FormatList, formats);
165 QFETCH(bool, modifiesFontSize);
166
167 QTextLayout layout;
168 QList<QQuickStyledTextImgTag*> imgTags;
169 bool fontSizeModified = false;
170 QQuickStyledText::parse(string: input, layout, imgTags, baseUrl: QUrl(), context: nullptr, preloadImages: false, fontSizeModified: &fontSizeModified);
171
172 QCOMPARE(layout.text(), output);
173
174 const QVector<QTextLayout::FormatRange> layoutFormats = layout.formats();
175
176 QCOMPARE(layoutFormats.count(), formats.count());
177 for (int i = 0; i < formats.count(); ++i) {
178 QCOMPARE(layoutFormats.at(i).start, formats.at(i).start);
179 QCOMPARE(layoutFormats.at(i).length, formats.at(i).length);
180 if (formats.at(i).type & Format::Bold)
181 QCOMPARE(layoutFormats.at(i).format.fontWeight(), int(QFont::Bold));
182 else
183 QCOMPARE(layoutFormats.at(i).format.fontWeight(), int(QFont::Normal));
184 QVERIFY(layoutFormats.at(i).format.fontItalic() == bool(formats.at(i).type & Format::Italic));
185 QVERIFY(layoutFormats.at(i).format.fontUnderline() == bool(formats.at(i).type & Format::Underline));
186 QVERIFY(layoutFormats.at(i).format.fontStrikeOut() == bool(formats.at(i).type & Format::StrikeOut));
187 }
188 QCOMPARE(fontSizeModified, modifiesFontSize);
189}
190
191void tst_qquickstyledtext::anchors()
192{
193 QFETCH(QString, input);
194 QFETCH(QString, output);
195 QFETCH(FormatList, formats);
196
197 QTextLayout layout;
198 QList<QQuickStyledTextImgTag*> imgTags;
199 bool fontSizeModified = false;
200 QQuickStyledText::parse(string: input, layout, imgTags, baseUrl: QUrl(), context: nullptr, preloadImages: false, fontSizeModified: &fontSizeModified);
201
202 QCOMPARE(layout.text(), output);
203
204 const QVector<QTextLayout::FormatRange> layoutFormats = layout.formats();
205
206 QCOMPARE(layoutFormats.count(), formats.count());
207 for (int i = 0; i < formats.count(); ++i) {
208 QCOMPARE(layoutFormats.at(i).start, formats.at(i).start);
209 QCOMPARE(layoutFormats.at(i).length, formats.at(i).length);
210 QVERIFY(layoutFormats.at(i).format.isAnchor() == bool(formats.at(i).type & Format::Anchor));
211 }
212}
213
214void tst_qquickstyledtext::anchors_data()
215{
216 QTest::addColumn<QString>(name: "input");
217 QTest::addColumn<QString>(name: "output");
218 QTest::addColumn<FormatList>(name: "formats");
219
220 QTest::newRow(dataTag: "empty 1") << "Test string with <a href=>url</a>." << "Test string with url." << FormatList();
221 QTest::newRow(dataTag: "empty 2") << "Test string with <a href="">url</a>." << "Test string with url." << FormatList();
222 QTest::newRow(dataTag: "unknown attr") << "Test string with <a hfre=\"http://strange<username>@ok-hostname\">url</a>." << "Test string with url." << FormatList();
223 QTest::newRow(dataTag: "close") << "Test string with <a href=\"http://strange<username>@ok-hostname\"/>url." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 4));
224 QTest::newRow(dataTag: "username") << "Test string with <a href=\"http://strange<username>@ok-hostname\">url</a>." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 3));
225 QTest::newRow(dataTag: "query") << "Test string with <a href=\"http://www.foo.bar?hello=world\">url</a>." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 3));
226 QTest::newRow(dataTag: "ipv6") << "Test string with <a href=\"//user:pass@[56::56:56:56:127.0.0.1]:99\">url</a>." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 3));
227 QTest::newRow(dataTag: "uni") << "Test string with <a href=\"data:text/javascript,d5%20%3D%20'five\\u0027s'%3B\">url</a>." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 3));
228 QTest::newRow(dataTag: "utf8") << "Test string with <a href=\"http://www.räksmörgås.se/pub?a=b&a=dø&a=f#vræl\">url</a>." << "Test string with url." << (FormatList() << Format(Format::Anchor, 17, 3));
229}
230
231void tst_qquickstyledtext::longString()
232{
233 QTextLayout layout;
234 QList<QQuickStyledTextImgTag*> imgTags;
235 bool fontSizeModified = false;
236
237 QString input(9999999, QChar('.'));
238 QQuickStyledText::parse(string: input, layout, imgTags, baseUrl: QUrl(), context: nullptr, preloadImages: false, fontSizeModified: &fontSizeModified);
239 QCOMPARE(layout.text(), input);
240
241 input = QString(9999999, QChar('\t')); // whitespace
242 QQuickStyledText::parse(string: input, layout, imgTags, baseUrl: QUrl(), context: nullptr, preloadImages: false, fontSizeModified: &fontSizeModified);
243 QCOMPARE(layout.text(), QString(""));
244}
245
246QTEST_MAIN(tst_qquickstyledtext)
247
248#include "tst_qquickstyledtext.moc"
249

source code of qtdeclarative/tests/auto/quick/qquickstyledtext/tst_qquickstyledtext.cpp