tests/auto/qsyntaxhighlighter/tst_qsyntaxhighlighter.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <QTextDocument>
       
    45 #include <QTextLayout>
       
    46 #include <QDebug>
       
    47 #include <QAbstractTextDocumentLayout>
       
    48 #include <QSyntaxHighlighter>
       
    49 
       
    50 //TESTED_CLASS=
       
    51 //TESTED_FILES=
       
    52 //
       
    53 class QTestDocumentLayout : public QAbstractTextDocumentLayout
       
    54 {
       
    55     Q_OBJECT
       
    56 public:
       
    57     inline QTestDocumentLayout(QTextDocument *doc)
       
    58         : QAbstractTextDocumentLayout(doc), documentChangedCalled(false) {}
       
    59 
       
    60         virtual void draw(QPainter *, const QAbstractTextDocumentLayout::PaintContext &)  {}
       
    61 
       
    62         virtual int hitTest(const QPointF &, Qt::HitTestAccuracy ) const { return 0; }
       
    63 
       
    64         virtual void documentChanged(int, int, int) { documentChangedCalled = true; }
       
    65 
       
    66         virtual int pageCount() const { return 1; }
       
    67 
       
    68         virtual QSizeF documentSize() const { return QSize(); }
       
    69 
       
    70         virtual QRectF frameBoundingRect(QTextFrame *) const { return QRectF(); }
       
    71         virtual QRectF blockBoundingRect(const QTextBlock &) const { return QRectF(); }
       
    72 
       
    73         bool documentChangedCalled;
       
    74 };
       
    75 
       
    76 class tst_QSyntaxHighlighter : public QObject
       
    77 {
       
    78     Q_OBJECT
       
    79 public:
       
    80     inline tst_QSyntaxHighlighter() {}
       
    81 
       
    82 public slots:
       
    83     void init();
       
    84     void cleanup();
       
    85 
       
    86 private slots:
       
    87     void basic();
       
    88     void basicTwo();
       
    89     void removeFormatsOnDelete();
       
    90     void emptyBlocks();
       
    91     void setCharFormat();
       
    92     void highlightOnInit();
       
    93     void stopHighlightingWhenStateDoesNotChange();
       
    94     void unindent();
       
    95     void highlightToEndOfDocument();
       
    96     void highlightToEndOfDocument2();
       
    97     void preservePreeditArea();
       
    98     void task108530();
       
    99     void avoidUnnecessaryRehighlight();
       
   100     void noContentsChangedDuringHighlight();
       
   101     void rehighlight();
       
   102     void rehighlightBlock();
       
   103     
       
   104 private:
       
   105     QTextDocument *doc;
       
   106     QTestDocumentLayout *lout;
       
   107     QTextCursor cursor;
       
   108 };
       
   109 
       
   110 void tst_QSyntaxHighlighter::init()
       
   111 {
       
   112     doc = new QTextDocument;
       
   113     lout = new QTestDocumentLayout(doc);
       
   114     doc->setDocumentLayout(lout);
       
   115     cursor = QTextCursor(doc);
       
   116 }
       
   117 
       
   118 void tst_QSyntaxHighlighter::cleanup()
       
   119 {
       
   120     delete doc;
       
   121     doc = 0;
       
   122 }
       
   123 
       
   124 class TestHighlighter : public QSyntaxHighlighter
       
   125 {
       
   126 public:
       
   127     inline TestHighlighter(const QList<QTextLayout::FormatRange> &fmts, QTextDocument *parent)
       
   128         : QSyntaxHighlighter(parent), formats(fmts), highlighted(false), callCount(0) {}
       
   129         inline TestHighlighter(QTextDocument *parent)
       
   130             : QSyntaxHighlighter(parent), highlighted(false), callCount(0) {}
       
   131 
       
   132             virtual void highlightBlock(const QString &text)
       
   133             {
       
   134                 for (int i = 0; i < formats.count(); ++i) {
       
   135                     const QTextLayout::FormatRange &range = formats.at(i);
       
   136                     setFormat(range.start, range.length, range.format);
       
   137                 }
       
   138                 highlighted = true;
       
   139                 highlightedText += text;
       
   140                 ++callCount;
       
   141             }
       
   142 
       
   143             QList<QTextLayout::FormatRange> formats;
       
   144             bool highlighted;
       
   145             int callCount;
       
   146             QString highlightedText;
       
   147 };
       
   148 
       
   149 QT_BEGIN_NAMESPACE
       
   150 bool operator==(const QTextLayout::FormatRange &lhs, const QTextLayout::FormatRange &rhs)
       
   151 {
       
   152     return lhs.start == rhs.start
       
   153         && lhs.length == rhs.length
       
   154         && lhs.format == rhs.format;
       
   155 }
       
   156 QT_END_NAMESPACE
       
   157 
       
   158 void tst_QSyntaxHighlighter::basic()
       
   159 {
       
   160     QList<QTextLayout::FormatRange> formats;
       
   161     QTextLayout::FormatRange range;
       
   162     range.start = 0;
       
   163     range.length = 2;
       
   164     range.format.setForeground(Qt::blue);
       
   165     formats.append(range);
       
   166 
       
   167     range.start = 4;
       
   168     range.length = 2;
       
   169     range.format.setFontItalic(true);
       
   170     formats.append(range);
       
   171 
       
   172     range.start = 9;
       
   173     range.length = 2;
       
   174     range.format.setFontUnderline(true);
       
   175     formats.append(range);
       
   176 
       
   177     TestHighlighter *hl = new TestHighlighter(formats, doc);
       
   178 
       
   179     lout->documentChangedCalled = false;
       
   180     doc->setPlainText("Hello World");
       
   181     QVERIFY(hl->highlighted);
       
   182     QVERIFY(lout->documentChangedCalled);
       
   183 
       
   184     QVERIFY(doc->begin().layout()->additionalFormats() == formats);
       
   185 }
       
   186 
       
   187 class CommentTestHighlighter : public QSyntaxHighlighter
       
   188 {
       
   189 public:
       
   190     inline CommentTestHighlighter(QTextDocument *parent)
       
   191         : QSyntaxHighlighter(parent), highlighted(false) {}
       
   192 
       
   193         inline void reset()
       
   194         {
       
   195             highlighted = false;
       
   196         }
       
   197 
       
   198         virtual void highlightBlock(const QString &text)
       
   199         {
       
   200             QTextCharFormat commentFormat;
       
   201             commentFormat.setForeground(Qt::darkGreen);
       
   202             commentFormat.setFontWeight(QFont::StyleItalic);
       
   203             commentFormat.setFontFixedPitch(true);
       
   204             int textLength = text.length();
       
   205 
       
   206             if (text.startsWith(QLatin1Char(';'))){
       
   207                 // The entire line is a comment
       
   208                 setFormat(0, textLength, commentFormat);
       
   209                 highlighted = true;
       
   210             }
       
   211         }
       
   212         bool highlighted;
       
   213 };
       
   214 
       
   215 
       
   216 void tst_QSyntaxHighlighter::basicTwo()
       
   217 {
       
   218     // Done for task 104409
       
   219     CommentTestHighlighter *hl = new CommentTestHighlighter(doc);
       
   220     doc->setPlainText("; a test");
       
   221     QVERIFY(hl->highlighted);
       
   222     QVERIFY(lout->documentChangedCalled);
       
   223 }
       
   224 
       
   225 void tst_QSyntaxHighlighter::removeFormatsOnDelete()
       
   226 {
       
   227     QList<QTextLayout::FormatRange> formats;
       
   228     QTextLayout::FormatRange range;
       
   229     range.start = 0;
       
   230     range.length = 9;
       
   231     range.format.setForeground(Qt::blue);
       
   232     formats.append(range);
       
   233 
       
   234     TestHighlighter *hl = new TestHighlighter(formats, doc);
       
   235 
       
   236     lout->documentChangedCalled = false;
       
   237     doc->setPlainText("Hello World");
       
   238     QVERIFY(hl->highlighted);
       
   239     QVERIFY(lout->documentChangedCalled);
       
   240 
       
   241     lout->documentChangedCalled = false;
       
   242     QVERIFY(!doc->begin().layout()->additionalFormats().isEmpty());
       
   243     delete hl;
       
   244     QVERIFY(doc->begin().layout()->additionalFormats().isEmpty());
       
   245     QVERIFY(lout->documentChangedCalled);
       
   246 }
       
   247 
       
   248 void tst_QSyntaxHighlighter::emptyBlocks()
       
   249 {
       
   250     TestHighlighter *hl = new TestHighlighter(doc);
       
   251 
       
   252     cursor.insertText("Foo");
       
   253     cursor.insertBlock();
       
   254     cursor.insertBlock();
       
   255     hl->highlighted = false;
       
   256     cursor.insertBlock();
       
   257     QVERIFY(hl->highlighted);
       
   258 }
       
   259 
       
   260 void tst_QSyntaxHighlighter::setCharFormat()
       
   261 {
       
   262     TestHighlighter *hl = new TestHighlighter(doc);
       
   263 
       
   264     cursor.insertText("FooBar");
       
   265     cursor.insertBlock();
       
   266     cursor.insertText("Blah");
       
   267     cursor.movePosition(QTextCursor::Start);
       
   268     cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
       
   269     QTextCharFormat fmt;
       
   270     fmt.setFontItalic(true);
       
   271     hl->highlighted = false;
       
   272     hl->callCount = 0;
       
   273     cursor.mergeCharFormat(fmt);
       
   274     QVERIFY(hl->highlighted);
       
   275     QCOMPARE(hl->callCount, 2);
       
   276 }
       
   277 
       
   278 void tst_QSyntaxHighlighter::highlightOnInit()
       
   279 {
       
   280     cursor.insertText("Hello");
       
   281     cursor.insertBlock();
       
   282     cursor.insertText("World");
       
   283 
       
   284     TestHighlighter *hl = new TestHighlighter(doc);
       
   285     QTest::qWait(100);
       
   286     QVERIFY(hl->highlighted);
       
   287 }
       
   288 
       
   289 class StateTestHighlighter : public QSyntaxHighlighter
       
   290 {
       
   291 public:
       
   292     inline StateTestHighlighter(QTextDocument *parent)
       
   293         : QSyntaxHighlighter(parent), state(0), highlighted(false) {}
       
   294 
       
   295         inline void reset()
       
   296         {
       
   297             highlighted = false;
       
   298             state = 0;
       
   299         }
       
   300 
       
   301         virtual void highlightBlock(const QString &text)
       
   302         {
       
   303             highlighted = true;
       
   304             if (text == QLatin1String("changestate"))
       
   305                 setCurrentBlockState(state++);
       
   306         }
       
   307 
       
   308         int state;
       
   309         bool highlighted;
       
   310 };
       
   311 
       
   312 void tst_QSyntaxHighlighter::stopHighlightingWhenStateDoesNotChange()
       
   313 {
       
   314     cursor.insertText("state");
       
   315     cursor.insertBlock();
       
   316     cursor.insertText("changestate");
       
   317     cursor.insertBlock();
       
   318     cursor.insertText("keepstate");
       
   319     cursor.insertBlock();
       
   320     cursor.insertText("changestate");
       
   321     cursor.insertBlock();
       
   322     cursor.insertText("changestate");
       
   323 
       
   324     StateTestHighlighter *hl = new StateTestHighlighter(doc);
       
   325     QTest::qWait(100);
       
   326     QVERIFY(hl->highlighted);
       
   327 
       
   328     hl->reset();
       
   329 
       
   330     // turn the text of the first block into 'changestate'
       
   331     cursor.movePosition(QTextCursor::Start);
       
   332     cursor.insertText("change");
       
   333 
       
   334     // verify that we highlighted only to the 'keepstate' block,
       
   335     // not beyond
       
   336     QCOMPARE(hl->state, 2);
       
   337 }
       
   338 
       
   339 void tst_QSyntaxHighlighter::unindent()
       
   340 {
       
   341     const QString spaces("    ");
       
   342     const QString text("Foobar");
       
   343     QString plainText;
       
   344     for (int i = 0; i < 5; ++i) {
       
   345         cursor.insertText(spaces + text);
       
   346         cursor.insertBlock();
       
   347 
       
   348         plainText += spaces;
       
   349         plainText += text;
       
   350         plainText += QLatin1Char('\n');
       
   351     }
       
   352     QCOMPARE(doc->toPlainText(), plainText);
       
   353 
       
   354     TestHighlighter *hl = new TestHighlighter(doc);
       
   355     hl->callCount = 0;
       
   356 
       
   357     cursor.movePosition(QTextCursor::Start);
       
   358     cursor.beginEditBlock();
       
   359 
       
   360     plainText.clear();
       
   361     for (int i = 0; i < 5; ++i) {
       
   362         cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, 4);
       
   363         cursor.removeSelectedText();
       
   364         cursor.movePosition(QTextCursor::NextBlock);
       
   365 
       
   366         plainText += text;
       
   367         plainText += QLatin1Char('\n');
       
   368     }
       
   369 
       
   370     cursor.endEditBlock();
       
   371     QCOMPARE(doc->toPlainText(), plainText);
       
   372     QCOMPARE(hl->callCount, 5);
       
   373 }
       
   374 
       
   375 void tst_QSyntaxHighlighter::highlightToEndOfDocument()
       
   376 {
       
   377     TestHighlighter *hl = new TestHighlighter(doc);
       
   378     hl->callCount = 0;
       
   379 
       
   380     cursor.movePosition(QTextCursor::Start);
       
   381     cursor.beginEditBlock();
       
   382 
       
   383     cursor.insertText("Hello");
       
   384     cursor.insertBlock();
       
   385     cursor.insertBlock();
       
   386     cursor.insertText("World");
       
   387     cursor.insertBlock();
       
   388 
       
   389     cursor.endEditBlock();
       
   390 
       
   391     QCOMPARE(hl->callCount, 4);
       
   392 }
       
   393 
       
   394 void tst_QSyntaxHighlighter::highlightToEndOfDocument2()
       
   395 {
       
   396     TestHighlighter *hl = new TestHighlighter(doc);
       
   397     hl->callCount = 0;
       
   398 
       
   399     cursor.movePosition(QTextCursor::End);
       
   400     cursor.beginEditBlock();
       
   401     QTextBlockFormat fmt;
       
   402     fmt.setAlignment(Qt::AlignLeft);
       
   403     cursor.setBlockFormat(fmt);
       
   404     cursor.insertText("Three\nLines\nHere");
       
   405     cursor.endEditBlock();
       
   406 
       
   407     QCOMPARE(hl->callCount, 3);
       
   408 }
       
   409 
       
   410 void tst_QSyntaxHighlighter::preservePreeditArea()
       
   411 {
       
   412     QList<QTextLayout::FormatRange> formats;
       
   413     QTextLayout::FormatRange range;
       
   414     range.start = 0;
       
   415     range.length = 8;
       
   416     range.format.setForeground(Qt::blue);
       
   417     formats << range;
       
   418     range.start = 9;
       
   419     range.length = 1;
       
   420     range.format.setForeground(Qt::red);
       
   421     formats << range;
       
   422     TestHighlighter *hl = new TestHighlighter(formats, doc);
       
   423 
       
   424     doc->setPlainText("Hello World");
       
   425     cursor.movePosition(QTextCursor::Start);
       
   426 
       
   427     QTextLayout *layout = cursor.block().layout();
       
   428 
       
   429     layout->setPreeditArea(5, QString("foo"));
       
   430     range.start = 5;
       
   431     range.length = 3;
       
   432     range.format.setFontUnderline(true);
       
   433     formats.clear();
       
   434     formats << range;
       
   435 
       
   436     hl->callCount = 0;
       
   437 
       
   438     cursor.beginEditBlock();
       
   439     layout->setAdditionalFormats(formats);
       
   440     cursor.endEditBlock();
       
   441 
       
   442     QCOMPARE(hl->callCount, 1);
       
   443 
       
   444     formats = layout->additionalFormats();
       
   445     QCOMPARE(formats.count(), 3);
       
   446 
       
   447     range = formats.at(0);
       
   448 
       
   449     QCOMPARE(range.start, 5);
       
   450     QCOMPARE(range.length, 3);
       
   451     QVERIFY(range.format.fontUnderline());
       
   452 
       
   453     range = formats.at(1);
       
   454     QCOMPARE(range.start, 0);
       
   455     QCOMPARE(range.length, 8 + 3);
       
   456 
       
   457     range = formats.at(2);
       
   458     QCOMPARE(range.start, 9 + 3);
       
   459     QCOMPARE(range.length, 1);
       
   460 }
       
   461 
       
   462 void tst_QSyntaxHighlighter::task108530()
       
   463 {
       
   464     TestHighlighter *hl = new TestHighlighter(doc);
       
   465 
       
   466     cursor.insertText("test");
       
   467     hl->callCount = 0;
       
   468     hl->highlightedText.clear();
       
   469     cursor.movePosition(QTextCursor::Start);
       
   470     cursor.insertBlock();
       
   471 
       
   472     QCOMPARE(hl->highlightedText, QString("test"));
       
   473     QCOMPARE(hl->callCount, 2);
       
   474 }
       
   475 
       
   476 void tst_QSyntaxHighlighter::avoidUnnecessaryRehighlight()
       
   477 {
       
   478     TestHighlighter *hl = new TestHighlighter(doc);
       
   479     QVERIFY(!hl->highlighted);
       
   480 
       
   481     doc->setPlainText("Hello World");
       
   482     QVERIFY(hl->highlighted);
       
   483 
       
   484     hl->highlighted = false;
       
   485     QTest::qWait(100);
       
   486     QVERIFY(!hl->highlighted);
       
   487 }
       
   488 
       
   489 void tst_QSyntaxHighlighter::noContentsChangedDuringHighlight()
       
   490 {
       
   491     QList<QTextLayout::FormatRange> formats;
       
   492     QTextLayout::FormatRange range;
       
   493     range.start = 0;
       
   494     range.length = 10;
       
   495     range.format.setForeground(Qt::blue);
       
   496     formats.append(range);
       
   497 
       
   498     TestHighlighter *hl = new TestHighlighter(formats, doc);
       
   499 
       
   500     lout->documentChangedCalled = false;
       
   501     QTextCursor cursor(doc);
       
   502 
       
   503     QSignalSpy contentsChangedSpy(doc, SIGNAL(contentsChanged()));
       
   504     cursor.insertText("Hello World");
       
   505 
       
   506     QCOMPARE(contentsChangedSpy.count(), 1);
       
   507     QVERIFY(hl->highlighted);
       
   508     QVERIFY(lout->documentChangedCalled);
       
   509 }
       
   510 
       
   511 void tst_QSyntaxHighlighter::rehighlight()
       
   512 {
       
   513     TestHighlighter *hl = new TestHighlighter(doc);
       
   514     hl->callCount = 0;
       
   515     doc->setPlainText("Hello");
       
   516     hl->callCount = 0;
       
   517     hl->rehighlight();
       
   518     QCOMPARE(hl->callCount, 1);
       
   519 }
       
   520 
       
   521 void tst_QSyntaxHighlighter::rehighlightBlock()
       
   522 {
       
   523     TestHighlighter *hl = new TestHighlighter(doc);
       
   524     
       
   525     cursor.movePosition(QTextCursor::Start);
       
   526     cursor.beginEditBlock();
       
   527     cursor.insertText("Hello");
       
   528     cursor.insertBlock();
       
   529     cursor.insertText("World");
       
   530     cursor.endEditBlock();
       
   531     
       
   532     hl->callCount = 0;
       
   533     hl->highlightedText.clear();
       
   534     QTextBlock block = doc->begin();
       
   535     hl->rehighlightBlock(block);
       
   536     
       
   537     QCOMPARE(hl->highlightedText, QString("Hello"));
       
   538     QCOMPARE(hl->callCount, 1);
       
   539     
       
   540     hl->callCount = 0;
       
   541     hl->highlightedText.clear();
       
   542     hl->rehighlightBlock(block.next());
       
   543     
       
   544     QCOMPARE(hl->highlightedText, QString("World"));
       
   545     QCOMPARE(hl->callCount, 1);
       
   546 }
       
   547 
       
   548 QTEST_MAIN(tst_QSyntaxHighlighter)
       
   549 #include "tst_qsyntaxhighlighter.moc"