diff options
author | Martin Jones <martin.jones@nokia.com> | 2011-12-14 15:20:25 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-12-14 09:02:24 +0100 |
commit | 22a8387aeab93fd086b297443945487677aef280 (patch) | |
tree | 518a2d8ca1bdeb0a3e827ded1688b72e16b763f2 /tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp | |
parent | 5ee8eaf094b1648009b9dc410120df1b5f2fd664 (diff) |
Correctly ignore unknown tags in StyledText
Also improve tests to compare expected formatting and improve coverage.
Change-Id: I021dbdcd147dd7340a0dc1c30f4b104f22efece0
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
Diffstat (limited to 'tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp')
-rw-r--r-- | tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp | 88 |
1 files changed, 67 insertions, 21 deletions
diff --git a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp index 2c5c66f280..a17c4294b3 100644 --- a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp +++ b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp @@ -51,49 +51,95 @@ public: { } + struct Format { + enum Type { + Bold = 0x01, + Underline = 0x02, + Italic = 0x04 + }; + Format(int t, int s, int l) + : type(t), start(s), length(l) {} + int type; + int start; + int length; + }; + typedef QList<Format> FormatList; + private slots: void textOutput(); void textOutput_data(); }; -// For malformed input all we test is that we get the expected text out. +Q_DECLARE_METATYPE(tst_qdeclarativestyledtext::FormatList); + + +// For malformed input all we test is that we get the expected text and format out. // void tst_qdeclarativestyledtext::textOutput_data() { QTest::addColumn<QString>("input"); QTest::addColumn<QString>("output"); + QTest::addColumn<FormatList>("formats"); - QTest::newRow("bold") << "<b>bold</b>" << "bold"; - QTest::newRow("italic") << "<b>italic</b>" << "italic"; - QTest::newRow("strong") << "<strong>strong</strong>" << "strong"; - QTest::newRow("missing >") << "<b>text</b" << "text"; - QTest::newRow("missing b>") << "<b>text</" << "text"; - QTest::newRow("missing /b>") << "<b>text<" << "text"; - QTest::newRow("missing </b>") << "<b>text" << "text"; - QTest::newRow("bad nest") << "<b>text <i>italic</b></i>" << "text italic"; - QTest::newRow("font color") << "<font color=\"red\">red text</font>" << "red text"; - QTest::newRow("font color: single quote") << "<font color='red'>red text</font>" << "red text"; - QTest::newRow("font size") << "<font size=\"1\">text</font>" << "text"; - QTest::newRow("font empty") << "<font>text</font>" << "text"; - QTest::newRow("font bad 1") << "<font ezis=\"blah\">text</font>" << "text"; - QTest::newRow("font bad 2") << "<font size=\"1>text</font>" << ""; - QTest::newRow("extra close") << "<b>text</b></b>" << "text"; - QTest::newRow("extra space") << "<b >text</b>" << "text"; - QTest::newRow("entities") << "<b>this & that</b>" << "<b>this & that</b>"; - QTest::newRow("newline") << "text<br>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") ; - QTest::newRow("self-closing newline") << "text<br/>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") ; - QTest::newRow("empty") << "" << ""; + QTest::newRow("bold") << "<b>bold</b>" << "bold" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("italic") << "<i>italic</i>" << "italic" << (FormatList() << Format(Format::Italic, 0, 6)); + QTest::newRow("strong") << "<strong>strong</strong>" << "strong" << (FormatList() << Format(Format::Bold, 0, 6)); + QTest::newRow("missing >") << "<b>text</b" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("missing b>") << "<b>text</" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("missing /b>") << "<b>text<" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("missing </b>") << "<b>text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + 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)); + 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)); + QTest::newRow("font color") << "<font color=\"red\">red text</font>" << "red text" << (FormatList() << Format(0, 0, 8)); + QTest::newRow("font color: single quote") << "<font color='red'>red text</font>" << "red text" << (FormatList() << Format(0, 0, 8)); + QTest::newRow("font size") << "<font size=\"1\">text</font>" << "text" << (FormatList() << Format(0, 0, 4)); + QTest::newRow("font empty") << "<font>text</font>" << "text" << FormatList(); + QTest::newRow("font bad 1") << "<font ezis=\"blah\">text</font>" << "text" << FormatList(); + QTest::newRow("font bad 2") << "<font size=\"1>text</font>" << "" << FormatList(); + QTest::newRow("extra close") << "<b>text</b></b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("extra space") << "<b >text</b>" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); + QTest::newRow("entities") << "<b>this & that</b>" << "<b>this & that</b>" << FormatList(); + QTest::newRow("newline") << "text<br>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); + QTest::newRow("paragraph") << "text<p>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); + 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(); + 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)); + QTest::newRow("self-closing newline") << "text<br/>more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); + QTest::newRow("empty") << "" << "" << FormatList(); + QTest::newRow("unknown tag") << "<a href='#'><foo>underline</foo></a> not" << "underline not" << (FormatList() << Format(Format::Underline, 0, 9)); + QTest::newRow("h0") << "<h0>head" << "head" << FormatList(); + QTest::newRow("h1") << "<h1>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h2") << "<h2>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h3") << "<h3>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h4") << "<h4>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h5") << "<h5>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h6") << "<h6>head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); + QTest::newRow("h7") << "<h7>head" << "head" << FormatList(); } void tst_qdeclarativestyledtext::textOutput() { QFETCH(QString, input); QFETCH(QString, output); + QFETCH(FormatList, formats); QTextLayout layout; QDeclarativeStyledText::parse(input, layout); QCOMPARE(layout.text(), output); + + QList<QTextLayout::FormatRange> layoutFormats = layout.additionalFormats(); + + QCOMPARE(layoutFormats.count(), formats.count()); + for (int i = 0; i < formats.count(); ++i) { + QCOMPARE(layoutFormats.at(i).start, formats.at(i).start); + QCOMPARE(layoutFormats.at(i).length, formats.at(i).length); + if (formats.at(i).type & Format::Bold) + QVERIFY(layoutFormats.at(i).format.fontWeight() == QFont::Bold); + else + QVERIFY(layoutFormats.at(i).format.fontWeight() == QFont::Normal); + QVERIFY(layoutFormats.at(i).format.fontItalic() == bool(formats.at(i).type & Format::Italic)); + QVERIFY(layoutFormats.at(i).format.fontUnderline() == bool(formats.at(i).type & Format::Underline)); + } } |