tests/benchmarks/qtext/main.cpp
branchRCL_3
changeset 4 3b1da2848fc7
child 5 d3bac044e0f0
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 #include <QDebug>
       
    43 #include <QTextDocument>
       
    44 #include <QTextDocumentWriter>
       
    45 #include <QTextLayout>
       
    46 #include <QTextCursor>
       
    47 #include <QFile>
       
    48 #include <QBuffer>
       
    49 #include <qtest.h>
       
    50 
       
    51 #ifdef Q_OS_SYMBIAN
       
    52 #define SRCDIR ""
       
    53 #endif
       
    54 
       
    55 Q_DECLARE_METATYPE(QTextDocument*)
       
    56 
       
    57 class tst_QText: public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 public:
       
    61     tst_QText() {
       
    62         m_lorem = QString::fromLatin1("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.");
       
    63     }
       
    64 
       
    65 private slots:
       
    66     void loadHtml_data();
       
    67     void loadHtml();
       
    68 
       
    69     void shaping_data();
       
    70     void shaping();
       
    71 
       
    72     void odfWriting_empty();
       
    73     void odfWriting_text();
       
    74     void odfWriting_images();
       
    75 
       
    76 private:
       
    77     QString m_lorem;
       
    78 };
       
    79 
       
    80 void tst_QText::loadHtml_data()
       
    81 {
       
    82     QTest::addColumn<QString>("source");
       
    83     QTest::newRow("empty") << QString();
       
    84     QTest::newRow("simple") << QString::fromLatin1("<html><b>Foo</b></html>");
       
    85     QTest::newRow("simple2") << QString::fromLatin1("<b>Foo</b>");
       
    86 
       
    87     QString parag = QString::fromLatin1("<p>%1</p>").arg(m_lorem);
       
    88     QString header = QString::fromLatin1("<html><head><title>test</title></head><body>");
       
    89     QTest::newRow("long") << QString::fromLatin1("<html><head><title>test</title></head><body>") + parag + parag + parag
       
    90         + parag + parag + parag + parag + parag + parag + parag + parag + parag + parag + parag + parag + parag + parag
       
    91         + QString::fromLatin1("</html>");
       
    92     QTest::newRow("table") <<  header + QLatin1String("<table border=\"1\"1><tr><td>xx</td></tr><tr><td colspan=\"2\">")
       
    93         + parag + QLatin1String("</td></tr></table></html");
       
    94     QTest::newRow("crappy") <<  header + QLatin1String("<table border=\"1\"1><tr><td>xx</td></tr><tr><td colspan=\"2\">")
       
    95         + parag;
       
    96 }
       
    97 
       
    98 void tst_QText::loadHtml()
       
    99 {
       
   100     QFETCH(QString, source);
       
   101     QTextDocument doc;
       
   102     QBENCHMARK {
       
   103         doc.setHtml(source);
       
   104     }
       
   105 }
       
   106 
       
   107 void tst_QText::shaping_data()
       
   108 {
       
   109     QTest::addColumn<QString>("parag");
       
   110     QTest::newRow("empty") << QString();
       
   111     QTest::newRow("lorem") << m_lorem;
       
   112     QTest::newRow("short") << QString::fromLatin1("Lorem ipsum dolor sit amet");
       
   113 
       
   114     QFile file(QString::fromLatin1(SRCDIR) + QLatin1String("/bidi.txt"));
       
   115     QVERIFY(file.open(QFile::ReadOnly));
       
   116     QByteArray data = file.readAll();
       
   117     QVERIFY(data.count() > 1000);
       
   118     QStringList list = QString::fromUtf8(data.data()).split(QLatin1Char('\n'), QString::SkipEmptyParts);
       
   119     QVERIFY(list.count() %2 == 0); // even amount as we have title and then content.
       
   120     for (int i=0; i < list.count(); i+=2) {
       
   121         QTest::newRow(list.at(i).toLatin1()) << list.at(i+1);
       
   122     }
       
   123 }
       
   124 
       
   125 void tst_QText::shaping()
       
   126 {
       
   127     QFETCH(QString, parag);
       
   128 
       
   129     QTextLayout lay(parag);
       
   130     lay.setCacheEnabled(false);
       
   131 
       
   132     // do one run to make sure any fonts are loaded.
       
   133     lay.beginLayout();
       
   134     lay.createLine();
       
   135     lay.endLayout();
       
   136 
       
   137     QBENCHMARK {
       
   138         lay.beginLayout();
       
   139         lay.createLine();
       
   140         lay.endLayout();
       
   141     }
       
   142 }
       
   143 
       
   144 void tst_QText::odfWriting_empty()
       
   145 {
       
   146     QVERIFY(QTextDocumentWriter::supportedDocumentFormats().contains("ODF")); // odf compiled in
       
   147     QTextDocument *doc = new QTextDocument();
       
   148     // write it
       
   149     QBENCHMARK {
       
   150         QBuffer buffer;
       
   151         buffer.open(QIODevice::WriteOnly);
       
   152         QTextDocumentWriter writer(&buffer, "ODF");
       
   153         writer.write(doc);
       
   154     }
       
   155     delete doc;
       
   156 }
       
   157 
       
   158 void tst_QText::odfWriting_text()
       
   159 {
       
   160     QTextDocument *doc = new QTextDocument();
       
   161     QTextCursor cursor(doc);
       
   162     QTextBlockFormat bf;
       
   163     bf.setIndent(2);
       
   164     cursor.insertBlock(bf);
       
   165     cursor.insertText(m_lorem);
       
   166     bf.setTopMargin(10);
       
   167     cursor.insertBlock(bf);
       
   168     cursor.insertText(m_lorem);
       
   169     bf.setRightMargin(30);
       
   170     cursor.insertBlock(bf);
       
   171     cursor.insertText(m_lorem);
       
   172 
       
   173     // write it
       
   174     QBENCHMARK {
       
   175         QBuffer buffer;
       
   176         buffer.open(QIODevice::WriteOnly);
       
   177         QTextDocumentWriter writer(&buffer, "ODF");
       
   178         writer.write(doc);
       
   179     }
       
   180     delete doc;
       
   181 }
       
   182 
       
   183 void tst_QText::odfWriting_images()
       
   184 {
       
   185     QTextDocument *doc = new QTextDocument();
       
   186     QTextCursor cursor(doc);
       
   187     cursor.insertText(m_lorem);
       
   188     QImage image(400, 200, QImage::Format_ARGB32_Premultiplied);
       
   189     cursor.insertImage(image);
       
   190     cursor.insertText(m_lorem);
       
   191 
       
   192     // write it
       
   193     QBENCHMARK {
       
   194         QBuffer buffer;
       
   195         buffer.open(QIODevice::WriteOnly);
       
   196         QTextDocumentWriter writer(&buffer, "ODF");
       
   197         writer.write(doc);
       
   198     }
       
   199     delete doc;
       
   200 }
       
   201 
       
   202 QTEST_MAIN(tst_QText)
       
   203 
       
   204 #include "main.moc"