a64caafcc88c73003a6440483b2da8fc17dc496c
[profile/ivi/qtdeclarative.git] / tests / auto / qtquick2 / qdeclarativestyledtext / tst_qdeclarativestyledtext.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <QtTest/QtTest>
43 #include <QtGui/QTextLayout>
44 #include <private/qdeclarativestyledtext_p.h>
45
46 class tst_qdeclarativestyledtext : public QObject
47 {
48     Q_OBJECT
49 public:
50     tst_qdeclarativestyledtext()
51     {
52     }
53
54     struct Format {
55         enum Type {
56             Bold = 0x01,
57             Underline = 0x02,
58             Italic = 0x04
59         };
60         Format(int t, int s, int l)
61             : type(t), start(s), length(l) {}
62         int type;
63         int start;
64         int length;
65     };
66     typedef QList<Format> FormatList;
67
68 private slots:
69     void textOutput();
70     void textOutput_data();
71 };
72
73 Q_DECLARE_METATYPE(tst_qdeclarativestyledtext::FormatList);
74
75
76 // For malformed input all we test is that we get the expected text and format out.
77 // 
78 void tst_qdeclarativestyledtext::textOutput_data()
79 {
80     QTest::addColumn<QString>("input");
81     QTest::addColumn<QString>("output");
82     QTest::addColumn<FormatList>("formats");
83
84     QTest::newRow("bold") << "<b>bold</b>" << "bold" << (FormatList() << Format(Format::Bold, 0, 4));
85     QTest::newRow("italic") << "<i>italic</i>" << "italic" << (FormatList() << Format(Format::Italic, 0, 6));
86     QTest::newRow("strong") << "<strong>strong</strong>" << "strong" << (FormatList() << Format(Format::Bold, 0, 6));
87     QTest::newRow("missing >") << "<b>text</b" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
88     QTest::newRow("missing b>") << "<b>text</" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
89     QTest::newRow("missing /b>") << "<b>text<" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
90     QTest::newRow("missing </b>") << "<b>text" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
91     QTest::newRow("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));
92     QTest::newRow("bad nest") << "<b>text <i>italic</b></i>" << "text italic" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6));
93     QTest::newRow("font color") << "<font color=\"red\">red text</font>" << "red text" << (FormatList() << Format(0, 0, 8));
94     QTest::newRow("font color: single quote") << "<font color='red'>red text</font>" << "red text" << (FormatList() << Format(0, 0, 8));
95     QTest::newRow("font size") << "<font size=\"1\">text</font>" << "text" << (FormatList() << Format(0, 0, 4));
96     QTest::newRow("font empty") << "<font>text</font>" << "text" << FormatList();
97     QTest::newRow("font bad 1") << "<font ezis=\"blah\">text</font>" << "text" << FormatList();
98     QTest::newRow("font bad 2") << "<font size=\"1>text</font>" << "" << FormatList();
99     QTest::newRow("extra close") << "<b>text</b></b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
100     QTest::newRow("extra space") << "<b >text</b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4));
101     QTest::newRow("entities") << "&lt;b&gt;this &amp; that&lt;/b&gt;" << "<b>this & that</b>" << FormatList();
102     QTest::newRow("newline") << "text<br>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList();
103     QTest::newRow("paragraph") << "text<p>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList();
104     QTest::newRow("paragraph closed") << "text<p>more text</p>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text")  + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList();
105     QTest::newRow("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));
106     QTest::newRow("self-closing newline") << "text<br/>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList();
107     QTest::newRow("empty") << "" << "" << FormatList();
108     QTest::newRow("unknown tag") << "<a href='#'><foo>underline</foo></a> not" << "underline not" << (FormatList() << Format(Format::Underline, 0, 9));
109     QTest::newRow("h0") << "<h0>head" << "head" << FormatList();
110     QTest::newRow("h1") << "<h1>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
111     QTest::newRow("h2") << "<h2>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
112     QTest::newRow("h3") << "<h3>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
113     QTest::newRow("h4") << "<h4>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
114     QTest::newRow("h5") << "<h5>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
115     QTest::newRow("h6") << "<h6>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5));
116     QTest::newRow("h7") << "<h7>head" << "head" << FormatList();
117     QTest::newRow("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));
118 }
119
120 void tst_qdeclarativestyledtext::textOutput()
121 {
122     QFETCH(QString, input);
123     QFETCH(QString, output);
124     QFETCH(FormatList, formats);
125
126     QTextLayout layout;
127     QDeclarativeStyledText::parse(input, layout);
128
129     QCOMPARE(layout.text(), output);
130
131     QList<QTextLayout::FormatRange> layoutFormats = layout.additionalFormats();
132
133     QCOMPARE(layoutFormats.count(), formats.count());
134     for (int i = 0; i < formats.count(); ++i) {
135         QCOMPARE(layoutFormats.at(i).start, formats.at(i).start);
136         QCOMPARE(layoutFormats.at(i).length, formats.at(i).length);
137         if (formats.at(i).type & Format::Bold)
138             QVERIFY(layoutFormats.at(i).format.fontWeight() == QFont::Bold);
139         else
140             QVERIFY(layoutFormats.at(i).format.fontWeight() == QFont::Normal);
141         QVERIFY(layoutFormats.at(i).format.fontItalic() == bool(formats.at(i).type & Format::Italic));
142         QVERIFY(layoutFormats.at(i).format.fontUnderline() == bool(formats.at(i).type & Format::Underline));
143     }
144 }
145
146
147 QTEST_MAIN(tst_qdeclarativestyledtext)
148
149 #include "tst_qdeclarativestyledtext.moc"