tests/auto/qtexttable/tst_qtexttable.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 #include <qtextdocument.h>
       
    47 #include <qtextdocumentfragment.h>
       
    48 #include <qtexttable.h>
       
    49 #include <qdebug.h>
       
    50 #include <qtextcursor.h>
       
    51 
       
    52 //TESTED_FILES=
       
    53 
       
    54 QT_FORWARD_DECLARE_CLASS(QTextDocument)
       
    55 
       
    56 class tst_QTextTable : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 
       
    60 public:
       
    61     tst_QTextTable();
       
    62 
       
    63 
       
    64 public slots:
       
    65     void init();
       
    66     void cleanup();
       
    67 private slots:
       
    68     void cursorPositioning();
       
    69     void variousTableModifications();
       
    70     void tableShrinking();
       
    71     void spans();
       
    72     void variousModifications2();
       
    73     void tableManager_undo();
       
    74     void tableManager_removeCell();
       
    75     void rowAt();
       
    76     void rowAtWithSpans();
       
    77     void multiBlockCells();
       
    78     void insertRows();
       
    79     void deleteInTable();
       
    80     void mergeCells();
       
    81     void splitCells();
       
    82     void blocksForTableShouldHaveEmptyFormat();
       
    83     void removeTableByRemoveRows();
       
    84     void removeTableByRemoveColumns();
       
    85     void setCellFormat();
       
    86     void removeRows1();
       
    87     void removeRows2();
       
    88     void removeRows3();
       
    89     void removeRows4();
       
    90     void removeRows5();
       
    91     void removeColumns1();
       
    92     void removeColumns2();
       
    93     void removeColumns3();
       
    94     void removeColumns4();
       
    95     void removeColumns5();
       
    96 
       
    97 private:
       
    98     QTextTable *create2x2Table();
       
    99     QTextTable *create4x4Table();
       
   100 
       
   101     QTextTable *createTable(int rows, int cols);
       
   102 
       
   103     QTextDocument *doc;
       
   104     QTextCursor cursor;
       
   105 };
       
   106 
       
   107 tst_QTextTable::tst_QTextTable()
       
   108 {}
       
   109 
       
   110 void tst_QTextTable::init()
       
   111 {
       
   112     doc = new QTextDocument;
       
   113     cursor = QTextCursor(doc);
       
   114 }
       
   115 
       
   116 void tst_QTextTable::cleanup()
       
   117 {
       
   118     cursor = QTextCursor();
       
   119     delete doc;
       
   120     doc = 0;
       
   121 }
       
   122 
       
   123 void tst_QTextTable::cursorPositioning()
       
   124 {
       
   125     // ensure the cursor is placed at the beginning of the first cell upon
       
   126     // table creation
       
   127     QTextTable *table = cursor.insertTable(2, 2);
       
   128 
       
   129     QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
       
   130     QVERIFY(table->cellAt(0, 0).firstPosition() == table->firstPosition());
       
   131 }
       
   132 
       
   133 void tst_QTextTable::variousTableModifications()
       
   134 {
       
   135     QTextTableFormat tableFmt;
       
   136 
       
   137     QTextTable *tab = cursor.insertTable(2, 2, tableFmt);
       
   138     QVERIFY(doc->toPlainText().length() == 5);
       
   139     QVERIFY(tab == cursor.currentTable());
       
   140     QVERIFY(tab->columns() == 2);
       
   141     QVERIFY(tab->rows() == 2);
       
   142 
       
   143     QVERIFY(cursor.position() == 1);
       
   144     QTextCharFormat fmt = cursor.charFormat();
       
   145     QVERIFY(fmt.objectIndex() == -1);
       
   146     QTextTableCell cell = tab->cellAt(cursor);
       
   147     QVERIFY(cell.isValid());
       
   148     QVERIFY(cell.row() == 0);
       
   149     QVERIFY(cell.column() == 0);
       
   150 
       
   151     cursor.movePosition(QTextCursor::NextBlock);
       
   152     QVERIFY(cursor.position() == 2);
       
   153     fmt = cursor.charFormat();
       
   154     QVERIFY(fmt.objectIndex() == -1);
       
   155     cell = tab->cellAt(cursor);
       
   156     QVERIFY(cell.isValid());
       
   157     QVERIFY(cell.row() == 0);
       
   158     QVERIFY(cell.column() == 1);
       
   159 
       
   160     cursor.movePosition(QTextCursor::NextBlock);
       
   161     QVERIFY(cursor.position() == 3);
       
   162     fmt = cursor.charFormat();
       
   163     QVERIFY(fmt.objectIndex() == -1);
       
   164     cell = tab->cellAt(cursor);
       
   165     QVERIFY(cell.isValid());
       
   166     QVERIFY(cell.row() == 1);
       
   167     QVERIFY(cell.column() == 0);
       
   168 
       
   169     cursor.movePosition(QTextCursor::NextBlock);
       
   170     QVERIFY(cursor.position() == 4);
       
   171     fmt = cursor.charFormat();
       
   172     QVERIFY(fmt.objectIndex() == -1);
       
   173     cell = tab->cellAt(cursor);
       
   174     QVERIFY(cell.isValid());
       
   175     QVERIFY(cell.row() == 1);
       
   176     QVERIFY(cell.column() == 1);
       
   177 
       
   178     cursor.movePosition(QTextCursor::NextBlock);
       
   179     QVERIFY(cursor.position() == 5);
       
   180     fmt = cursor.charFormat();
       
   181     QVERIFY(fmt.objectIndex() == -1);
       
   182     cell = tab->cellAt(cursor);
       
   183     QVERIFY(!cell.isValid());
       
   184 
       
   185     cursor.movePosition(QTextCursor::NextBlock);
       
   186     QVERIFY(cursor.position() == 5);
       
   187 
       
   188     // check we can't delete the cells with the cursor
       
   189     cursor.movePosition(QTextCursor::Start);
       
   190     cursor.movePosition(QTextCursor::NextBlock);
       
   191     QVERIFY(cursor.position() == 1);
       
   192     cursor.deleteChar();
       
   193     QVERIFY(doc->toPlainText().length() == 5);
       
   194     cursor.movePosition(QTextCursor::NextBlock);
       
   195     QVERIFY(cursor.position() == 2);
       
   196     cursor.deleteChar();
       
   197     QVERIFY(doc->toPlainText().length() == 5);
       
   198     cursor.deletePreviousChar();
       
   199     QVERIFY(cursor.position() == 2);
       
   200     QVERIFY(doc->toPlainText().length() == 5);
       
   201 
       
   202     QTextTable *table = cursor.currentTable();
       
   203     QVERIFY(table->rows() == 2);
       
   204     QVERIFY(table->columns() == 2);
       
   205 
       
   206     table->insertRows(2, 1);
       
   207     QVERIFY(table->rows() == 3);
       
   208     QVERIFY(table->columns() == 2);
       
   209     QVERIFY(doc->toPlainText().length() == 7);
       
   210     table->insertColumns(2, 2);
       
   211     QVERIFY(table->rows() == 3);
       
   212     QVERIFY(table->columns() == 4);
       
   213     QVERIFY(doc->toPlainText().length() == 13);
       
   214 
       
   215     table->resize(4, 5);
       
   216     QVERIFY(table->rows() == 4);
       
   217     QVERIFY(table->columns() == 5);
       
   218     QVERIFY(doc->toPlainText().length() == 21);
       
   219 }
       
   220 
       
   221 void tst_QTextTable::tableShrinking()
       
   222 {
       
   223     QTextTableFormat tableFmt;
       
   224 
       
   225     cursor.insertTable(3, 4, tableFmt);
       
   226     QVERIFY(doc->toPlainText().length() == 13);
       
   227 
       
   228     QTextTable *table = cursor.currentTable();
       
   229     QVERIFY(table->rows() == 3);
       
   230     QVERIFY(table->columns() == 4);
       
   231 
       
   232     table->removeRows(1, 1);
       
   233     QVERIFY(table->rows() == 2);
       
   234     QVERIFY(table->columns() == 4);
       
   235     QVERIFY(doc->toPlainText().length() == 9);
       
   236     table->removeColumns(1, 2);
       
   237     QVERIFY(table->rows() == 2);
       
   238     QVERIFY(table->columns() == 2);
       
   239     QVERIFY(doc->toPlainText().length() == 5);
       
   240 
       
   241     table->resize(1, 1);
       
   242     QVERIFY(table->rows() == 1);
       
   243     QVERIFY(table->columns() == 1);
       
   244     QVERIFY(doc->toPlainText().length() == 2);
       
   245 }
       
   246 
       
   247 void tst_QTextTable::spans()
       
   248 {
       
   249     QTextTableFormat tableFmt;
       
   250 
       
   251     cursor.insertTable(2, 2, tableFmt);
       
   252 
       
   253     QTextTable *table = cursor.currentTable();
       
   254     QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
       
   255     table->mergeCells(0, 0, 1, 2);
       
   256     QVERIFY(table->rows() == 2);
       
   257     QVERIFY(table->columns() == 2);
       
   258     QVERIFY(table->cellAt(0, 0) == table->cellAt(0, 1));
       
   259     table->mergeCells(0, 0, 2, 2);
       
   260     QVERIFY(table->rows() == 2);
       
   261     QVERIFY(table->columns() == 2);
       
   262 }
       
   263 
       
   264 void tst_QTextTable::variousModifications2()
       
   265 {
       
   266     QTextTableFormat tableFmt;
       
   267 
       
   268     cursor.insertTable(2, 5, tableFmt);
       
   269     QVERIFY(doc->toPlainText().length() == 11);
       
   270     QTextTable *table = cursor.currentTable();
       
   271     QVERIFY(cursor.position() == 1);
       
   272     QVERIFY(table->rows() == 2);
       
   273     QVERIFY(table->columns() == 5);
       
   274 
       
   275     table->insertColumns(0, 1);
       
   276     QVERIFY(table->rows() == 2);
       
   277     QVERIFY(table->columns() == 6);
       
   278     table->insertColumns(6, 1);
       
   279     QVERIFY(table->rows() == 2);
       
   280     QVERIFY(table->columns() == 7);
       
   281 
       
   282     table->insertRows(0, 1);
       
   283     QVERIFY(table->rows() == 3);
       
   284     QVERIFY(table->columns() == 7);
       
   285     table->insertRows(3, 1);
       
   286     QVERIFY(table->rows() == 4);
       
   287     QVERIFY(table->columns() == 7);
       
   288 
       
   289     table->removeRows(0, 1);
       
   290     QVERIFY(table->rows() == 3);
       
   291     QVERIFY(table->columns() == 7);
       
   292     table->removeRows(2, 1);
       
   293     QVERIFY(table->rows() == 2);
       
   294     QVERIFY(table->columns() == 7);
       
   295 
       
   296     table->removeColumns(0, 1);
       
   297     QVERIFY(table->rows() == 2);
       
   298     QVERIFY(table->columns() == 6);
       
   299     table->removeColumns(5, 1);
       
   300     QVERIFY(table->rows() == 2);
       
   301     QVERIFY(table->columns() == 5);
       
   302 
       
   303     tableFmt = table->format();
       
   304     table->insertColumns(2, 1);
       
   305     table->setFormat(tableFmt);
       
   306     table->insertColumns(2, 1);
       
   307     QVERIFY(table->columns() == 7);
       
   308 }
       
   309 
       
   310 void tst_QTextTable::tableManager_undo()
       
   311 {
       
   312     QTextTableFormat fmt;
       
   313     fmt.setBorder(10);
       
   314     QTextTable *table = cursor.insertTable(2, 2, fmt);
       
   315     QVERIFY(table);
       
   316 
       
   317     QVERIFY(table->format().border() == 10);
       
   318 
       
   319     fmt.setBorder(20);
       
   320     table->setFormat(fmt);
       
   321 
       
   322     QVERIFY(table->format().border() == 20);
       
   323 
       
   324     doc->undo();
       
   325 
       
   326     QVERIFY(table->format().border() == 10);
       
   327 }
       
   328 
       
   329 void tst_QTextTable::tableManager_removeCell()
       
   330 {
       
   331     // essentially a test for TableManager::removeCell, in particular to remove empty items from the rowlist.
       
   332     // If it fails it'll triger assertions inside TableManager. Yeah, not pretty, should VERIFY here ;(
       
   333     cursor.insertTable(2, 2, QTextTableFormat());
       
   334     doc->undo();
       
   335     // ###
       
   336     QVERIFY(true);
       
   337 }
       
   338 
       
   339 void tst_QTextTable::rowAt()
       
   340 {
       
   341     // test TablePrivate::rowAt
       
   342     QTextTable *table = cursor.insertTable(4, 2);
       
   343 
       
   344     QCOMPARE(table->rows(), 4);
       
   345     QCOMPARE(table->columns(), 2);
       
   346 
       
   347     QTextCursor cell00Cursor = table->cellAt(0, 0).firstCursorPosition();
       
   348     QTextCursor cell10Cursor = table->cellAt(1, 0).firstCursorPosition();
       
   349     QTextCursor cell20Cursor = table->cellAt(2, 0).firstCursorPosition();
       
   350     QTextCursor cell21Cursor = table->cellAt(2, 1).firstCursorPosition();
       
   351     QTextCursor cell30Cursor = table->cellAt(3, 0).firstCursorPosition();
       
   352     QVERIFY(table->cellAt(cell00Cursor).firstCursorPosition() == cell00Cursor);
       
   353     QVERIFY(table->cellAt(cell10Cursor).firstCursorPosition() == cell10Cursor);
       
   354     QVERIFY(table->cellAt(cell20Cursor).firstCursorPosition() == cell20Cursor);
       
   355     QVERIFY(table->cellAt(cell30Cursor).firstCursorPosition() == cell30Cursor);
       
   356 
       
   357     table->mergeCells(1, 0, 2, 1);
       
   358 
       
   359     QCOMPARE(table->rows(), 4);
       
   360     QCOMPARE(table->columns(), 2);
       
   361 
       
   362     QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
       
   363     QVERIFY(cell10Cursor == table->cellAt(1, 0).firstCursorPosition());
       
   364     QVERIFY(cell10Cursor == table->cellAt(2, 0).firstCursorPosition());
       
   365     QVERIFY(cell21Cursor == table->cellAt(2, 1).firstCursorPosition());
       
   366     QVERIFY(cell30Cursor == table->cellAt(3, 0).firstCursorPosition());
       
   367 
       
   368     table->mergeCells(1, 0, 2, 2);
       
   369 
       
   370     QCOMPARE(table->rows(), 4);
       
   371     QCOMPARE(table->columns(), 2);
       
   372 
       
   373     QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
       
   374     QVERIFY(cell00Cursor == table->cellAt(0, 0).firstCursorPosition());
       
   375     QVERIFY(cell10Cursor == table->cellAt(1, 0).firstCursorPosition());
       
   376     QVERIFY(cell10Cursor == table->cellAt(1, 1).firstCursorPosition());
       
   377     QVERIFY(cell10Cursor == table->cellAt(2, 0).firstCursorPosition());
       
   378     QVERIFY(cell10Cursor == table->cellAt(2, 1).firstCursorPosition());
       
   379     QVERIFY(cell30Cursor == table->cellAt(3, 0).firstCursorPosition());
       
   380 }
       
   381 
       
   382 void tst_QTextTable::rowAtWithSpans()
       
   383 {
       
   384     QTextTable *table = cursor.insertTable(2, 2);
       
   385 
       
   386     QCOMPARE(table->rows(), 2);
       
   387     QCOMPARE(table->columns(), 2);
       
   388 
       
   389     table->mergeCells(0, 0, 2, 1);
       
   390     QVERIFY(table->cellAt(0, 0).rowSpan() == 2);
       
   391 
       
   392     QCOMPARE(table->rows(), 2);
       
   393     QCOMPARE(table->columns(), 2);
       
   394 
       
   395     table->mergeCells(0, 0, 2, 2);
       
   396     QVERIFY(table->cellAt(0, 0).columnSpan() == 2);
       
   397 
       
   398     QCOMPARE(table->rows(), 2);
       
   399     QCOMPARE(table->columns(), 2);
       
   400 }
       
   401 
       
   402 void tst_QTextTable::multiBlockCells()
       
   403 {
       
   404     // little testcase for multi-block cells
       
   405     QTextTable *table = cursor.insertTable(2, 2);
       
   406 
       
   407     QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
       
   408 
       
   409     cursor.insertText("Hello");
       
   410     cursor.insertBlock(QTextBlockFormat());
       
   411     cursor.insertText("World");
       
   412 
       
   413     cursor.movePosition(QTextCursor::Left);
       
   414     QVERIFY(table->cellAt(0, 0) == table->cellAt(cursor));
       
   415 }
       
   416 
       
   417 void tst_QTextTable::insertRows()
       
   418 {
       
   419     // little testcase for multi-block cells
       
   420     QTextTable *table = cursor.insertTable(2, 2);
       
   421 
       
   422     QVERIFY(cursor == table->cellAt(0, 0).firstCursorPosition());
       
   423 
       
   424     table->insertRows(0, 1);
       
   425     QVERIFY(table->rows() == 3);
       
   426 
       
   427     table->insertRows(1, 1);
       
   428     QVERIFY(table->rows() == 4);
       
   429 
       
   430     table->insertRows(-1, 1);
       
   431     QVERIFY(table->rows() == 5);
       
   432 
       
   433     table->insertRows(5, 2);
       
   434     QVERIFY(table->rows() == 7);
       
   435 
       
   436 }
       
   437 
       
   438 void tst_QTextTable::deleteInTable()
       
   439 {
       
   440     QTextTable *table = cursor.insertTable(2, 2);
       
   441     table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
       
   442     table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
       
   443     table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
       
   444     table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
       
   445 
       
   446     cursor = table->cellAt(1, 1).firstCursorPosition();
       
   447     cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::KeepAnchor);
       
   448 
       
   449     QCOMPARE(table->cellAt(cursor.position()).row(), 1);
       
   450     QCOMPARE(table->cellAt(cursor.position()).column(), 0);
       
   451 
       
   452     cursor.removeSelectedText();
       
   453 
       
   454     QCOMPARE(table->columns(), 2);
       
   455     QCOMPARE(table->rows(), 2);
       
   456 
       
   457     // verify table is still all in shape. Only the text inside should get deleted
       
   458     for (int row = 0; row < table->rows(); ++row)
       
   459         for (int col = 0; col < table->columns(); ++col) {
       
   460             const QTextTableCell cell = table->cellAt(row, col);
       
   461             QVERIFY(cell.isValid());
       
   462             QCOMPARE(cell.rowSpan(), 1);
       
   463             QCOMPARE(cell.columnSpan(), 1);
       
   464         }
       
   465 }
       
   466 
       
   467 QTextTable *tst_QTextTable::create2x2Table()
       
   468 {
       
   469     cleanup();
       
   470     init();
       
   471     QTextTable *table = cursor.insertTable(2, 2);
       
   472     table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
       
   473     table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
       
   474     table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
       
   475     table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
       
   476     return table;
       
   477 }
       
   478 
       
   479 QTextTable *tst_QTextTable::create4x4Table()
       
   480 {
       
   481     cleanup();
       
   482     init();
       
   483     QTextTable *table = cursor.insertTable(4, 4);
       
   484     table->cellAt(0, 0).firstCursorPosition().insertText("Blah");
       
   485     table->cellAt(0, 1).firstCursorPosition().insertText("Foo");
       
   486     table->cellAt(1, 0).firstCursorPosition().insertText("Bar");
       
   487     table->cellAt(1, 1).firstCursorPosition().insertText("Hah");
       
   488     return table;
       
   489 }
       
   490 
       
   491 QTextTable *tst_QTextTable::createTable(int rows, int cols)
       
   492 {
       
   493     cleanup();
       
   494     init();
       
   495     QTextTable *table = cursor.insertTable(rows, cols);
       
   496     return table;
       
   497 }
       
   498 
       
   499 void tst_QTextTable::mergeCells()
       
   500 {
       
   501     QTextTable *table = create4x4Table();
       
   502 
       
   503     table->mergeCells(1, 1, 1, 2);
       
   504     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   505 
       
   506     table->mergeCells(1, 1, 2, 2);
       
   507     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   508     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   509     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   510 
       
   511     table = create4x4Table();
       
   512 
       
   513     table->mergeCells(1, 1, 2, 1);
       
   514     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   515 
       
   516     table->mergeCells(1, 1, 2, 2);
       
   517     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   518     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   519     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   520 
       
   521     table = create4x4Table();
       
   522 
       
   523     table->mergeCells(1, 1, 2, 2);
       
   524     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   525     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   526     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   527 
       
   528     // should do nothing
       
   529     table->mergeCells(1, 1, 1, 1);
       
   530     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   531     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   532     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   533 
       
   534     table = create2x2Table();
       
   535 
       
   536     table->mergeCells(0, 1, 2, 1);
       
   537     table->mergeCells(0, 0, 2, 2);
       
   538     QVERIFY(table->cellAt(0, 0) == table->cellAt(0, 1));
       
   539     QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 0));
       
   540     QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 1));
       
   541 
       
   542     QTextBlock block = table->cellAt(0, 0).firstCursorPosition().block();
       
   543 
       
   544     QVERIFY(block.text() == "Blah Foo");
       
   545     QVERIFY(block.next().text() == "Hah");
       
   546     QVERIFY(block.next().next().text() == "Bar");
       
   547 
       
   548     table = create4x4Table();
       
   549 
       
   550     QTextCursor cursor = table->cellAt(3, 3).firstCursorPosition();
       
   551     QTextTable *t2 = cursor.insertTable(2, 2);
       
   552     t2->cellAt(0, 0).firstCursorPosition().insertText("Test");
       
   553 
       
   554     table->mergeCells(2, 2, 2, 2);
       
   555     cursor = table->cellAt(2, 2).firstCursorPosition();
       
   556 
       
   557     QTextFrame *frame = cursor.currentFrame();
       
   558 
       
   559     QTextFrame::iterator it = frame->begin();
       
   560 
       
   561     // find the embedded table
       
   562     while (it != frame->end() && !it.currentFrame())
       
   563         ++it;
       
   564 
       
   565     table = qobject_cast<QTextTable *>(it.currentFrame());
       
   566 
       
   567     QVERIFY(table);
       
   568 
       
   569     if (table) {
       
   570         cursor = table->cellAt(0, 0).firstCursorPosition();
       
   571 
       
   572         QVERIFY(cursor.block().text() == "Test");
       
   573     }
       
   574 
       
   575     table = create2x2Table();
       
   576 
       
   577     table->mergeCells(0, 1, 2, 1);
       
   578 
       
   579     QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
       
   580     QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
       
   581 
       
   582     // should do nothing
       
   583     table->mergeCells(0, 0, 1, 2);
       
   584 
       
   585     QVERIFY(table->cellAt(0, 0) != table->cellAt(0, 1));
       
   586     QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
       
   587 }
       
   588 
       
   589 void tst_QTextTable::splitCells()
       
   590 {
       
   591     QTextTable *table = create4x4Table();
       
   592     table->mergeCells(1, 1, 2, 2);
       
   593     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   594     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   595     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   596 
       
   597     table->splitCell(1, 1, 1, 2);
       
   598     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   599     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
       
   600     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
       
   601 
       
   602     table->splitCell(1, 1, 1, 1);
       
   603     QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
       
   604     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
       
   605     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
       
   606 
       
   607 
       
   608     table = create4x4Table();
       
   609     table->mergeCells(1, 1, 2, 2);
       
   610     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   611     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   612     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   613 
       
   614     table->splitCell(1, 1, 2, 1);
       
   615     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   616     QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
       
   617     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
       
   618 
       
   619     table->splitCell(1, 1, 1, 1);
       
   620     QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
       
   621     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
       
   622     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
       
   623 
       
   624 
       
   625     table = create4x4Table();
       
   626     table->mergeCells(1, 1, 2, 2);
       
   627     QVERIFY(table->cellAt(1, 1) == table->cellAt(1, 2));
       
   628     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 1));
       
   629     QVERIFY(table->cellAt(1, 1) == table->cellAt(2, 2));
       
   630 
       
   631     table->splitCell(1, 1, 1, 1);
       
   632     QVERIFY(table->cellAt(1, 1) != table->cellAt(1, 2));
       
   633     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 1));
       
   634     QVERIFY(table->cellAt(1, 1) != table->cellAt(2, 2));
       
   635 
       
   636     table = createTable(2, 5);
       
   637     table->mergeCells(0, 0, 2, 1);
       
   638     table->mergeCells(0, 1, 2, 1);
       
   639     QVERIFY(table->cellAt(0, 0) == table->cellAt(1, 0));
       
   640     QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
       
   641     table->splitCell(0, 0, 1, 1);
       
   642     QVERIFY(table->cellAt(0, 0) != table->cellAt(1, 0));
       
   643     QVERIFY(table->cellAt(0, 1) == table->cellAt(1, 1));
       
   644 
       
   645     table = createTable(2, 5);
       
   646     table->mergeCells(0, 4, 2, 1);
       
   647     QVERIFY(table->cellAt(0, 4) == table->cellAt(1, 4));
       
   648 
       
   649     table->splitCell(0, 4, 1, 1);
       
   650     QVERIFY(table->cellAt(0, 4) != table->cellAt(1, 4));
       
   651 }
       
   652 
       
   653 void tst_QTextTable::blocksForTableShouldHaveEmptyFormat()
       
   654 {
       
   655     QTextBlockFormat fmt;
       
   656     fmt.setProperty(QTextFormat::UserProperty, true);
       
   657     cursor.insertBlock(fmt);
       
   658     QVERIFY(cursor.blockFormat().hasProperty(QTextFormat::UserProperty));
       
   659 
       
   660     QTextTable *table = cursor.insertTable(1, 1);
       
   661     QVERIFY(!table->cellAt(0, 0).firstCursorPosition().blockFormat().hasProperty(QTextFormat::UserProperty));
       
   662 
       
   663     int userPropCount = 0;
       
   664     for (QTextBlock block = doc->begin();
       
   665          block.isValid(); block = block.next()) {
       
   666         if (block.blockFormat().hasProperty(QTextFormat::UserProperty))
       
   667             userPropCount++;
       
   668     }
       
   669     QCOMPARE(userPropCount, 1);
       
   670 }
       
   671 
       
   672 void tst_QTextTable::removeTableByRemoveRows()
       
   673 {
       
   674     QPointer<QTextTable> table1 = QTextCursor(cursor).insertTable(4, 4);
       
   675     QPointer<QTextTable> table2 = QTextCursor(cursor).insertTable(4, 4);
       
   676     QPointer<QTextTable> table3 = QTextCursor(cursor).insertTable(4, 4);
       
   677 
       
   678     QVERIFY(table1);
       
   679     QVERIFY(table2);
       
   680     QVERIFY(table3);
       
   681 
       
   682     table2->removeRows(1, 1);
       
   683 
       
   684     QVERIFY(table1);
       
   685     QVERIFY(table2);
       
   686     QVERIFY(table3);
       
   687 
       
   688     table2->removeRows(0, table2->rows());
       
   689 
       
   690     QVERIFY(table1);
       
   691     QVERIFY(!table2);
       
   692     QVERIFY(table3);
       
   693 }
       
   694 
       
   695 void tst_QTextTable::removeTableByRemoveColumns()
       
   696 {
       
   697     QPointer<QTextTable> table1 = QTextCursor(cursor).insertTable(4, 4);
       
   698     QPointer<QTextTable> table2 = QTextCursor(cursor).insertTable(4, 4);
       
   699     QPointer<QTextTable> table3 = QTextCursor(cursor).insertTable(4, 4);
       
   700 
       
   701     QVERIFY(table1);
       
   702     QVERIFY(table2);
       
   703     QVERIFY(table3);
       
   704 
       
   705     table2->removeColumns(1, 1);
       
   706 
       
   707     QVERIFY(table1);
       
   708     QVERIFY(table2);
       
   709     QVERIFY(table3);
       
   710 
       
   711     table2->removeColumns(0, table2->columns());
       
   712 
       
   713     QVERIFY(table1);
       
   714     QVERIFY(!table2);
       
   715     QVERIFY(table3);
       
   716 }
       
   717 
       
   718 void tst_QTextTable::setCellFormat()
       
   719 {
       
   720     QTextTable *table = cursor.insertTable(2, 2);
       
   721     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   722     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   723     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   724     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   725     QTextTableCell cell = table->cellAt(0, 0);
       
   726     QTextCharFormat fmt;
       
   727     fmt.setObjectIndex(23);
       
   728     fmt.setBackground(Qt::blue);
       
   729     fmt.setTableCellColumnSpan(25);
       
   730     fmt.setTableCellRowSpan(42);
       
   731     cell.setFormat(fmt);
       
   732     QVERIFY(cell.format().background().color() == QColor(Qt::blue));
       
   733     QCOMPARE(cell.format().tableCellColumnSpan(), 1);
       
   734     QCOMPARE(cell.format().tableCellRowSpan(), 1);
       
   735 }
       
   736 
       
   737 void tst_QTextTable::removeRows1()
       
   738 {
       
   739     QTextTable *table = cursor.insertTable(2, 2);
       
   740     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   741     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   742     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   743     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   744     table->removeRows(0, 1);
       
   745     QCOMPARE(table->rows(), 1);
       
   746     QCOMPARE(table->columns(), 2);
       
   747     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Third"));
       
   748     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Fourth"));
       
   749 }
       
   750 
       
   751 void tst_QTextTable::removeRows2()
       
   752 {
       
   753     QTextTable *table = cursor.insertTable(2, 2);
       
   754     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   755     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   756     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   757     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   758     table->removeRows(1, 1);
       
   759     QCOMPARE(table->rows(), 1);
       
   760     QCOMPARE(table->columns(), 2);
       
   761     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   762     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
       
   763 }
       
   764 
       
   765 void tst_QTextTable::removeRows3()
       
   766 {
       
   767     QTextTable *table = cursor.insertTable(3, 2);
       
   768     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   769     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   770     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   771     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   772     table->cellAt(2, 0).firstCursorPosition().insertText("Fifth");
       
   773     table->cellAt(2, 1).firstCursorPosition().insertText("Sixth");
       
   774     table->removeRows(1, 1);
       
   775     QCOMPARE(table->rows(), 2);
       
   776     QCOMPARE(table->columns(), 2);
       
   777     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   778     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
       
   779     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fifth"));
       
   780     QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Sixth"));
       
   781 }
       
   782 
       
   783 void tst_QTextTable::removeRows4()
       
   784 {
       
   785     QTextTable *table = cursor.insertTable(4, 2);
       
   786     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   787     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   788     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   789     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   790     table->cellAt(2, 0).firstCursorPosition().insertText("Fifth");
       
   791     table->cellAt(2, 1).firstCursorPosition().insertText("Sixth");
       
   792     table->cellAt(3, 0).firstCursorPosition().insertText("Seventh");
       
   793     table->cellAt(3, 1).firstCursorPosition().insertText("Eighth");
       
   794     table->removeRows(1, 2);
       
   795     QCOMPARE(table->rows(), 2);
       
   796     QCOMPARE(table->columns(), 2);
       
   797     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   798     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
       
   799     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Seventh"));
       
   800     QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Eighth"));
       
   801 }
       
   802 
       
   803 void tst_QTextTable::removeRows5()
       
   804 {
       
   805     QTextTable *table = cursor.insertTable(2,2);
       
   806     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   807     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   808     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   809     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   810     table->insertRows(1,1);
       
   811     table->mergeCells(1,0,1,2);
       
   812     table->removeRows(1,1);
       
   813     QCOMPARE(table->rows(), 2);
       
   814     QCOMPARE(table->columns(), 2);
       
   815     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   816     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Second"));
       
   817     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Third"));
       
   818     QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Fourth"));
       
   819 }
       
   820 
       
   821 void tst_QTextTable::removeColumns1()
       
   822 {
       
   823     QTextTable *table = cursor.insertTable(2, 2);
       
   824     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   825     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   826     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   827     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   828     table->removeColumns(0, 1);
       
   829     QCOMPARE(table->rows(), 2);
       
   830     QCOMPARE(table->columns(), 1);
       
   831     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("Second"));
       
   832     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fourth"));
       
   833 }
       
   834 
       
   835 void tst_QTextTable::removeColumns2()
       
   836 {
       
   837     QTextTable *table = cursor.insertTable(2, 2);
       
   838     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   839     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   840     table->cellAt(1, 0).firstCursorPosition().insertText("Third");
       
   841     table->cellAt(1, 1).firstCursorPosition().insertText("Fourth");
       
   842     table->removeColumns(1, 1);
       
   843     QCOMPARE(table->rows(), 2);
       
   844     QCOMPARE(table->columns(), 1);
       
   845     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   846     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Third"));
       
   847 }
       
   848 
       
   849 void tst_QTextTable::removeColumns3()
       
   850 {
       
   851     QTextTable *table = cursor.insertTable(2, 3);
       
   852     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   853     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   854     table->cellAt(0, 2).firstCursorPosition().insertText("Third");
       
   855     table->cellAt(1, 0).firstCursorPosition().insertText("Fourth");
       
   856     table->cellAt(1, 1).firstCursorPosition().insertText("Fifth");
       
   857     table->cellAt(1, 2).firstCursorPosition().insertText("Sixth");
       
   858     table->removeColumns(1, 1);
       
   859     QCOMPARE(table->rows(), 2);
       
   860     QCOMPARE(table->columns(), 2);
       
   861     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   862     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Third"));
       
   863     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fourth"));
       
   864     QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Sixth"));
       
   865 }
       
   866 
       
   867 void tst_QTextTable::removeColumns4()
       
   868 {
       
   869     QTextTable *table = cursor.insertTable(2, 4);
       
   870     table->cellAt(0, 0).firstCursorPosition().insertText("First");
       
   871     table->cellAt(0, 1).firstCursorPosition().insertText("Second");
       
   872     table->cellAt(0, 2).firstCursorPosition().insertText("Third");
       
   873     table->cellAt(0, 3).firstCursorPosition().insertText("Fourth");
       
   874     table->cellAt(1, 0).firstCursorPosition().insertText("Fifth");
       
   875     table->cellAt(1, 1).firstCursorPosition().insertText("Sixth");
       
   876     table->cellAt(1, 2).firstCursorPosition().insertText("Seventh");
       
   877     table->cellAt(1, 3).firstCursorPosition().insertText("Eighth");
       
   878     table->removeColumns(1, 2);
       
   879     QCOMPARE(table->rows(), 2);
       
   880     QCOMPARE(table->columns(), 2);
       
   881     QCOMPARE(table->cellAt(0, 0).firstCursorPosition().block().text(), QString("First"));
       
   882     QCOMPARE(table->cellAt(0, 1).firstCursorPosition().block().text(), QString("Fourth"));
       
   883     QCOMPARE(table->cellAt(1, 0).firstCursorPosition().block().text(), QString("Fifth"));
       
   884     QCOMPARE(table->cellAt(1, 1).firstCursorPosition().block().text(), QString("Eighth"));
       
   885 }
       
   886 
       
   887 void tst_QTextTable::removeColumns5()
       
   888 {
       
   889     QTextTable *table = cursor.insertTable(4, 4);
       
   890     QTextCursor tc (doc);
       
   891     tc.setPosition(table->cellAt(2,0).firstPosition());
       
   892     tc.setPosition(table->cellAt(3,1).firstPosition(), QTextCursor::KeepAnchor);
       
   893     table->mergeCells(tc);
       
   894     QCOMPARE(table->rows(), 4);
       
   895     QCOMPARE(table->columns(), 4);
       
   896     QCOMPARE(table->cellAt(0, 0).firstPosition(), 1);
       
   897     QCOMPARE(table->cellAt(0, 1).firstPosition(), 2);
       
   898     QCOMPARE(table->cellAt(0, 2).firstPosition(), 3);
       
   899     QCOMPARE(table->cellAt(0, 3).firstPosition(), 4);
       
   900     QCOMPARE(table->cellAt(1, 0).firstPosition(), 5);
       
   901     QCOMPARE(table->cellAt(1, 1).firstPosition(), 6);
       
   902     QCOMPARE(table->cellAt(1, 2).firstPosition(), 7);
       
   903     QCOMPARE(table->cellAt(1, 3).firstPosition(), 8);
       
   904     QCOMPARE(table->cellAt(2, 0).firstPosition(), 9);
       
   905     QCOMPARE(table->cellAt(2, 0).rowSpan(), 2);
       
   906     QCOMPARE(table->cellAt(2, 0).columnSpan(), 2);
       
   907     QCOMPARE(table->cellAt(2, 1).firstPosition(), 9);
       
   908     QCOMPARE(table->cellAt(2, 2).firstPosition(), 10);
       
   909     QCOMPARE(table->cellAt(2, 3).firstPosition(), 11);
       
   910     QCOMPARE(table->cellAt(3, 0).firstPosition(), 9);
       
   911     QCOMPARE(table->cellAt(3, 1).firstPosition(), 9);
       
   912     QCOMPARE(table->cellAt(3, 2).firstPosition(), 12);
       
   913     QCOMPARE(table->cellAt(3, 3).firstPosition(), 13);
       
   914 
       
   915     table->removeColumns(1, 1);
       
   916     QCOMPARE(table->rows(), 4);
       
   917     QCOMPARE(table->columns(), 3);
       
   918     QCOMPARE(table->cellAt(0, 0).firstPosition(), 1);
       
   919     QCOMPARE(table->cellAt(0, 1).firstPosition(), 2);
       
   920     QCOMPARE(table->cellAt(0, 2).firstPosition(), 3);
       
   921     QCOMPARE(table->cellAt(1, 0).firstPosition(), 4);
       
   922     QCOMPARE(table->cellAt(1, 1).firstPosition(), 5);
       
   923     QCOMPARE(table->cellAt(1, 2).firstPosition(), 6);
       
   924     QCOMPARE(table->cellAt(2, 0).firstPosition(), 7);
       
   925     QCOMPARE(table->cellAt(2, 0).rowSpan(), 2);
       
   926     QCOMPARE(table->cellAt(2, 0).columnSpan(), 1);
       
   927     QCOMPARE(table->cellAt(2, 1).firstPosition(), 8);
       
   928     QCOMPARE(table->cellAt(2, 2).firstPosition(), 9);
       
   929     QCOMPARE(table->cellAt(3, 0).firstPosition(), 7);
       
   930     QCOMPARE(table->cellAt(3, 1).firstPosition(), 10);
       
   931     QCOMPARE(table->cellAt(3, 2).firstPosition(), 11);
       
   932 }
       
   933 
       
   934 QTEST_MAIN(tst_QTextTable)
       
   935 #include "tst_qtexttable.moc"