From 520cca521ed320ab6751041d9a7bf9c18ee98fa1 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 24 Feb 2010 08:57:09 +1000 Subject: Fix illegal access in QmlStyledText and add auto tests --- src/declarative/util/qmlstyledtext.cpp | 24 +++--- tests/auto/declarative/declarative.pro | 1 + .../declarative/qmlstyledtext/qmlstyledtext.pro | 9 ++ .../qmlstyledtext/tst_qmlstyledtext.cpp | 96 ++++++++++++++++++++++ 4 files changed, 118 insertions(+), 12 deletions(-) create mode 100644 tests/auto/declarative/qmlstyledtext/qmlstyledtext.pro create mode 100644 tests/auto/declarative/qmlstyledtext/tst_qmlstyledtext.cpp diff --git a/src/declarative/util/qmlstyledtext.cpp b/src/declarative/util/qmlstyledtext.cpp index 1f31214..6d01e3c 100644 --- a/src/declarative/util/qmlstyledtext.cpp +++ b/src/declarative/util/qmlstyledtext.cpp @@ -111,6 +111,8 @@ QmlStyledText::~QmlStyledText() void QmlStyledText::parse(const QString &string, QTextLayout &layout) { + if (string.isEmpty()) + return; QmlStyledText styledText(string, layout); styledText.d->parse(); } @@ -142,8 +144,10 @@ void QmlStyledTextPrivate::parse() ++ch; if (*ch == slash) { ++ch; - if (parseCloseTag(ch, text)) - formatStack.pop(); + if (parseCloseTag(ch, text)) { + if (formatStack.count()) + formatStack.pop(); + } } else { QTextCharFormat format; if (formatStack.count()) @@ -164,7 +168,8 @@ void QmlStyledTextPrivate::parse() } else { ++textLength; } - ++ch; + if (!ch->isNull()) + ++ch; } if (textLength) drawText.append(QStringRef(&text, textStart, textLength)); @@ -191,20 +196,15 @@ bool QmlStyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QSt QStringRef tag(&textIn, tagStart, tagLength); const QChar char0 = tag.at(0); if (char0 == QLatin1Char('b')) { - if (tagLength == 1) { + if (tagLength == 1) format.setFontWeight(QFont::Bold); - return true; - } else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) { + else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) textOut.append(QChar(QChar::LineSeparator)); - return true; - } } else if (char0 == QLatin1Char('i')) { - if (tagLength == 1) { + if (tagLength == 1) format.setFontItalic(true); - return true; - } } - return false; + return true; } else if (ch->isSpace()) { // may have params. QStringRef tag(&textIn, tagStart, tagLength); diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index 870c92b..c9b1052 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -58,6 +58,7 @@ SUBDIRS += \ qmlvaluetypes \ # Cover qmlxmlhttprequest \ # Cover qmlimageprovider \ # Cover + qmlstyledtext \ # Cover sql # Cover contains(QT_CONFIG, webkit) { diff --git a/tests/auto/declarative/qmlstyledtext/qmlstyledtext.pro b/tests/auto/declarative/qmlstyledtext/qmlstyledtext.pro new file mode 100644 index 0000000..d535835 --- /dev/null +++ b/tests/auto/declarative/qmlstyledtext/qmlstyledtext.pro @@ -0,0 +1,9 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative +QT += network +macx:CONFIG -= app_bundle + +SOURCES += tst_qmlstyledtext.cpp + +# QMAKE_CXXFLAGS = -fprofile-arcs -ftest-coverage +# LIBS += -lgcov diff --git a/tests/auto/declarative/qmlstyledtext/tst_qmlstyledtext.cpp b/tests/auto/declarative/qmlstyledtext/tst_qmlstyledtext.cpp new file mode 100644 index 0000000..5e675b1 --- /dev/null +++ b/tests/auto/declarative/qmlstyledtext/tst_qmlstyledtext.cpp @@ -0,0 +1,96 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include +#include +#include +#include + +class tst_qmlstyledtext : public QObject +{ + Q_OBJECT +public: + tst_qmlstyledtext() + { + } + +private slots: + void textOutput(); + void textOutput_data(); +}; + +// For malformed input all we test is that we get the expected text out. +// +void tst_qmlstyledtext::textOutput_data() +{ + QTest::addColumn("input"); + QTest::addColumn("output"); + + QTest::newRow("bold") << "bold" << "bold"; + QTest::newRow("italic") << "italic" << "italic"; + QTest::newRow("missing >") << "text") << "text") << "text<" << "text"; + QTest::newRow("missing ") << "text" << "text"; + QTest::newRow("bad nest") << "text italic" << "text italic"; + QTest::newRow("font color") << "red text" << "red text"; + QTest::newRow("font size") << "text" << "text"; + QTest::newRow("font empty") << "text" << "text"; + QTest::newRow("font bad 1") << "text" << "text"; + QTest::newRow("font bad 2") << "text" << ""; + QTest::newRow("extra close") << "text" << "text"; + QTest::newRow("empty") << "" << ""; +} + +void tst_qmlstyledtext::textOutput() +{ + QFETCH(QString, input); + QFETCH(QString, output); + + QTextLayout layout; + QmlStyledText::parse(input, layout); + + QCOMPARE(layout.text(), output); +} + + +QTEST_MAIN(tst_qmlstyledtext) + +#include "tst_qmlstyledtext.moc" -- cgit v0.12