tests/auto/qtextblock/tst_qtextblock.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 
       
    45 
       
    46 #define private public
       
    47 #include <qtextdocument.h>
       
    48 #include <qdebug.h>
       
    49 #ifndef Q_WS_WIN
       
    50 #include <private/qtextdocument_p.h>
       
    51 #endif
       
    52 
       
    53 
       
    54 
       
    55 #include <qtextobject.h>
       
    56 #include <qtextcursor.h>
       
    57 
       
    58 
       
    59 //TESTED_FILES=
       
    60 
       
    61 QT_FORWARD_DECLARE_CLASS(QTextDocument)
       
    62 
       
    63 class tst_QTextBlock : public QObject
       
    64 {
       
    65     Q_OBJECT
       
    66 
       
    67 public:
       
    68     tst_QTextBlock();
       
    69 
       
    70 
       
    71 public slots:
       
    72     void init();
       
    73     void cleanup();
       
    74 private slots:
       
    75     void fragmentOverBlockBoundaries();
       
    76     void excludeParagraphSeparatorFragment();
       
    77     void backwardsBlockIterator();
       
    78 
       
    79 private:
       
    80     QTextDocument *doc;
       
    81     QTextCursor cursor;
       
    82 };
       
    83 
       
    84 tst_QTextBlock::tst_QTextBlock()
       
    85 {}
       
    86 
       
    87 void tst_QTextBlock::init()
       
    88 {
       
    89     doc = new QTextDocument;
       
    90     cursor = QTextCursor(doc);
       
    91 }
       
    92 
       
    93 void tst_QTextBlock::cleanup()
       
    94 {
       
    95     cursor = QTextCursor();
       
    96     delete doc;
       
    97     doc = 0;
       
    98 }
       
    99 
       
   100 void tst_QTextBlock::fragmentOverBlockBoundaries()
       
   101 {
       
   102     /* this creates two fragments in the piecetable:
       
   103      * 1) 'hello<parag separator here>world'
       
   104      * 2) '<parag separator>'
       
   105      * (they are not united because the former was interested after the latter,
       
   106      * hence their position in the pt buffer is the other way around)
       
   107      */
       
   108     cursor.insertText("Hello");
       
   109     cursor.insertBlock();
       
   110     cursor.insertText("World");
       
   111 
       
   112     cursor.movePosition(QTextCursor::Start);
       
   113 
       
   114     const QTextDocument *doc = cursor.block().document();
       
   115     QVERIFY(doc);
       
   116     // Block separators are always a fragment of their self. Thus:
       
   117     // |Hello|\b|World|\b|
       
   118 #if !defined(Q_WS_WIN) && !defined(Q_WS_S60)
       
   119     QVERIFY(doc->d_func()->fragmentMap().numNodes() == 4);
       
   120 #endif
       
   121 
       
   122     QCOMPARE(cursor.block().text(), QString("Hello"));
       
   123     cursor.movePosition(QTextCursor::NextBlock);
       
   124     QCOMPARE(cursor.block().text(), QString("World"));
       
   125 }
       
   126 
       
   127 void tst_QTextBlock::excludeParagraphSeparatorFragment()
       
   128 {
       
   129     QTextCharFormat fmt;
       
   130     fmt.setForeground(Qt::blue);
       
   131     cursor.insertText("Hello", fmt);
       
   132 
       
   133     QTextBlock block = doc->begin();
       
   134     QVERIFY(block.isValid());
       
   135 
       
   136     QTextBlock::Iterator it = block.begin();
       
   137 
       
   138     QTextFragment fragment = it.fragment();
       
   139     QVERIFY(fragment.isValid());
       
   140     QCOMPARE(fragment.text(), QString("Hello"));
       
   141 
       
   142     ++it;
       
   143     QVERIFY(it.atEnd());
       
   144     QVERIFY(it == block.end());
       
   145 }
       
   146 
       
   147 void tst_QTextBlock::backwardsBlockIterator()
       
   148 {
       
   149     QTextCharFormat fmt;
       
   150 
       
   151     fmt.setForeground(Qt::magenta);
       
   152     cursor.insertText("A", fmt);
       
   153 
       
   154     fmt.setForeground(Qt::red);
       
   155     cursor.insertText("A", fmt);
       
   156 
       
   157     fmt.setForeground(Qt::magenta);
       
   158     cursor.insertText("A", fmt);
       
   159 
       
   160     QTextBlock block = doc->begin();
       
   161     QVERIFY(block.isValid());
       
   162 
       
   163     QTextBlock::Iterator it = block.begin();
       
   164     QCOMPARE(it.fragment().position(), 0);
       
   165     ++it;
       
   166     QCOMPARE(it.fragment().position(), 1);
       
   167     ++it;
       
   168 
       
   169     QCOMPARE(it.fragment().position(), 2);
       
   170 
       
   171     --it;
       
   172     QCOMPARE(it.fragment().position(), 1);
       
   173     --it;
       
   174     QCOMPARE(it.fragment().position(), 0);
       
   175 }
       
   176 
       
   177 QTEST_MAIN(tst_QTextBlock)
       
   178 #include "tst_qtextblock.moc"