tests/auto/qtextdocumentlayout/tst_qtextdocumentlayout.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 #include <qtextdocument.h>
       
    46 #include <qabstracttextdocumentlayout.h>
       
    47 #include <qdebug.h>
       
    48 #include <qpainter.h>
       
    49 #include <qtexttable.h>
       
    50 #include <qtextedit.h>
       
    51 #include <qscrollbar.h>
       
    52 
       
    53 //TESTED_CLASS=
       
    54 //TESTED_FILES=gui/text/qtextdocumentlayout_p.h gui/text/qtextdocumentlayout.cpp
       
    55 
       
    56 class tst_QTextDocumentLayout : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 public:
       
    60     inline tst_QTextDocumentLayout() {}
       
    61     virtual ~tst_QTextDocumentLayout() {}
       
    62 
       
    63 public slots:
       
    64     void init();
       
    65     void cleanup();
       
    66 
       
    67 private slots:
       
    68     void defaultPageSizeHandling();
       
    69     void idealWidth();
       
    70     void lineSeparatorFollowingTable();
       
    71     void wrapAtWordBoundaryOrAnywhere();
       
    72     void inlineImage();
       
    73     void clippedTableCell();
       
    74     void floatingTablePageBreak();
       
    75 
       
    76 private:
       
    77     QTextDocument *doc;
       
    78 };
       
    79 
       
    80 void tst_QTextDocumentLayout::init()
       
    81 {
       
    82     doc = new QTextDocument;
       
    83 }
       
    84 
       
    85 void tst_QTextDocumentLayout::cleanup()
       
    86 {
       
    87     delete doc;
       
    88     doc = 0;
       
    89 }
       
    90 
       
    91 void tst_QTextDocumentLayout::defaultPageSizeHandling()
       
    92 {
       
    93     QAbstractTextDocumentLayout *layout = doc->documentLayout();
       
    94     QVERIFY(layout);
       
    95 
       
    96     QVERIFY(!doc->pageSize().isValid());
       
    97     QSizeF docSize = layout->documentSize();
       
    98     QVERIFY(docSize.width() > 0 && docSize.width() < 1000);
       
    99     QVERIFY(docSize.height() > 0 && docSize.height() < 1000);
       
   100 
       
   101     doc->setPlainText("Some text\nwith a few lines\nand not real information\nor anything otherwise useful");
       
   102 
       
   103     docSize = layout->documentSize();
       
   104     QVERIFY(docSize.isValid());
       
   105     QVERIFY(docSize.width() != INT_MAX);
       
   106     QVERIFY(docSize.height() != INT_MAX);
       
   107 }
       
   108 
       
   109 void tst_QTextDocumentLayout::idealWidth()
       
   110 {
       
   111     doc->setPlainText("Some text\nwith a few lines\nand not real information\nor anything otherwise useful");
       
   112     doc->setTextWidth(1000);
       
   113     QCOMPARE(doc->textWidth(), qreal(1000));
       
   114     QCOMPARE(doc->size().width(), doc->textWidth());
       
   115     QVERIFY(doc->idealWidth() < doc->textWidth());
       
   116     QVERIFY(doc->idealWidth() > 0);
       
   117 
       
   118     QTextBlockFormat fmt;
       
   119     fmt.setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
       
   120     QTextCursor cursor(doc);
       
   121     cursor.select(QTextCursor::Document);
       
   122     cursor.mergeBlockFormat(fmt);
       
   123 
       
   124     QCOMPARE(doc->textWidth(), qreal(1000));
       
   125     QCOMPARE(doc->size().width(), doc->textWidth());
       
   126     QVERIFY(doc->idealWidth() < doc->textWidth());
       
   127     QVERIFY(doc->idealWidth() > 0);
       
   128 }
       
   129 
       
   130 // none of the QTextLine items in the document should intersect with the margin rect
       
   131 void tst_QTextDocumentLayout::lineSeparatorFollowingTable()
       
   132 {
       
   133     QString html_begin("<html><table border=1><tr><th>Column 1</th></tr><tr><td>Data</td></tr></table><br>");
       
   134     QString html_text("bla bla bla bla bla bla bla bla<br>");
       
   135     QString html_end("<table border=1><tr><th>Column 1</th></tr><tr><td>Data</td></tr></table></html>");
       
   136 
       
   137     QString html = html_begin;
       
   138 
       
   139     for (int i = 0; i < 80; ++i)
       
   140         html += html_text;
       
   141 
       
   142     html += html_end;
       
   143 
       
   144     doc->setHtml(html);
       
   145 
       
   146     QTextCursor cursor(doc);
       
   147     cursor.movePosition(QTextCursor::Start);
       
   148 
       
   149     const int margin = 87;
       
   150     const int pageWidth = 873;
       
   151     const int pageHeight = 1358;
       
   152 
       
   153     QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
       
   154     fmt.setMargin(margin);
       
   155     doc->rootFrame()->setFrameFormat(fmt);
       
   156 
       
   157     QFont font(doc->defaultFont());
       
   158     font.setPointSize(10);
       
   159     doc->setDefaultFont(font);
       
   160     doc->setPageSize(QSizeF(pageWidth, pageHeight));
       
   161 
       
   162     QRectF marginRect(QPointF(0, pageHeight - margin), QSizeF(pageWidth, 2 * margin));
       
   163 
       
   164     // force layouting
       
   165     doc->pageCount();
       
   166 
       
   167     for (QTextBlock block = doc->begin(); block != doc->end(); block = block.next()) {
       
   168         QTextLayout *layout = block.layout();
       
   169         for (int i = 0; i < layout->lineCount(); ++i) {
       
   170             QTextLine line = layout->lineAt(i);
       
   171             QRectF rect = line.rect().translated(layout->position());
       
   172             QVERIFY(!rect.intersects(marginRect));
       
   173         }
       
   174     }
       
   175 }
       
   176 
       
   177 void tst_QTextDocumentLayout::wrapAtWordBoundaryOrAnywhere()
       
   178 {
       
   179     //task 150562
       
   180     QTextEdit edit;
       
   181     edit.setText("<table><tr><td>hello hello hello"
       
   182             "thisisabigwordthisisabigwordthisisabigwordthisisabigwordthisisabigword"
       
   183             "hello hello hello</td></tr></table>");
       
   184     edit.setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
       
   185     edit.resize(100, 100);
       
   186     edit.show();
       
   187     QVERIFY(!edit.horizontalScrollBar()->isVisible());
       
   188 }
       
   189 
       
   190 void tst_QTextDocumentLayout::inlineImage()
       
   191 {
       
   192     doc->setPageSize(QSizeF(800, 500));
       
   193 
       
   194     QImage img(400, 400, QImage::Format_RGB32);
       
   195     QLatin1String name("bigImage");
       
   196 
       
   197     doc->addResource(QTextDocument::ImageResource, QUrl(name), img);
       
   198 
       
   199     QTextImageFormat imgFormat;
       
   200     imgFormat.setName(name);
       
   201     imgFormat.setWidth(img.width());
       
   202 
       
   203     QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
       
   204     qreal height = doc->pageSize().height() - fmt.topMargin() - fmt.bottomMargin();
       
   205     imgFormat.setHeight(height);
       
   206 
       
   207     QTextCursor cursor(doc);
       
   208     cursor.insertImage(imgFormat);
       
   209 
       
   210     QCOMPARE(doc->pageCount(), 1);
       
   211 }
       
   212 
       
   213 void tst_QTextDocumentLayout::clippedTableCell()
       
   214 {
       
   215     const char *html =
       
   216         "<table style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\""
       
   217         "border=\"0\" margin=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td></td></tr></table>";
       
   218 
       
   219     doc->setHtml(html);
       
   220     doc->pageSize();
       
   221 
       
   222     QTextCursor cursor(doc);
       
   223     cursor.movePosition(QTextCursor::Right);
       
   224 
       
   225     QTextTable *table = cursor.currentTable();
       
   226     QVERIFY(table);
       
   227 
       
   228     QTextCursor cellCursor = table->cellAt(0, 0).firstCursorPosition();
       
   229     QImage src(16, 16, QImage::Format_ARGB32_Premultiplied);
       
   230     src.fill(0xffff0000);
       
   231     cellCursor.insertImage(src);
       
   232 
       
   233     QTextBlock block = cellCursor.block();
       
   234     QRectF r = doc->documentLayout()->blockBoundingRect(block);
       
   235 
       
   236     QRectF rect(0, 0, r.left() + 1, 64);
       
   237 
       
   238     QImage img(64, 64, QImage::Format_ARGB32_Premultiplied);
       
   239     img.fill(0x0);
       
   240     QImage expected = img;
       
   241     QPainter p(&img);
       
   242     doc->drawContents(&p, rect);
       
   243     p.end();
       
   244     p.begin(&expected);
       
   245     r.setWidth(1);
       
   246     p.fillRect(r, Qt::red);
       
   247     p.end();
       
   248 
       
   249     img.save("img.png");
       
   250     expected.save("expected.png");
       
   251     QCOMPARE(img, expected);
       
   252 }
       
   253 
       
   254 void tst_QTextDocumentLayout::floatingTablePageBreak()
       
   255 {
       
   256     doc->clear();
       
   257 
       
   258     QTextCursor cursor(doc);
       
   259 
       
   260     QTextTableFormat tableFormat;
       
   261     tableFormat.setPosition(QTextFrameFormat::FloatLeft);
       
   262     QTextTable *table = cursor.insertTable(50, 1, tableFormat);
       
   263 
       
   264     // Make height of document 2/3 of the table, fitting the table into two pages
       
   265     QSizeF documentSize = doc->size();
       
   266     documentSize.rheight() *= 2.0 / 3.0;
       
   267 
       
   268     doc->setPageSize(documentSize);
       
   269 
       
   270     QCOMPARE(doc->pageCount(), 2);
       
   271 }
       
   272 
       
   273 
       
   274 QTEST_MAIN(tst_QTextDocumentLayout)
       
   275 #include "tst_qtextdocumentlayout.moc"