tests/auto/qtextodfwriter/tst_qtextodfwriter.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 #include <QtTest/QtTest>
       
    42 #include <QTextDocument>
       
    43 #include <QTextCursor>
       
    44 #include <QTextBlock>
       
    45 #include <QTextList>
       
    46 #include <QTextTable>
       
    47 #include <QBuffer>
       
    48 #include <QDebug>
       
    49 
       
    50 #ifdef Q_OS_SYMBIAN
       
    51 #define SRCDIR "."
       
    52 #endif
       
    53 
       
    54 #include <private/qtextodfwriter_p.h>
       
    55 
       
    56 class tst_QTextOdfWriter : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 public slots:
       
    60     void init();
       
    61     void cleanup();
       
    62 
       
    63 private slots:
       
    64     void testWriteParagraph_data();
       
    65     void testWriteParagraph();
       
    66     void testWriteStyle1_data();
       
    67     void testWriteStyle1();
       
    68     void testWriteStyle2();
       
    69     void testWriteList();
       
    70     void testWriteList2();
       
    71     void createArchive();
       
    72     void testWriteAll();
       
    73     void testWriteSection();
       
    74     void testWriteTable();
       
    75 
       
    76 private:
       
    77     /// closes the document and returns the part of the XML stream that the test wrote
       
    78     QString getContentFromXml();
       
    79 
       
    80 private:
       
    81     QTextDocument *document;
       
    82     QXmlStreamWriter *xmlWriter;
       
    83     QTextOdfWriter *odfWriter;
       
    84     QBuffer *buffer;
       
    85 };
       
    86 
       
    87 void tst_QTextOdfWriter::init()
       
    88 {
       
    89     document = new QTextDocument();
       
    90     odfWriter = new QTextOdfWriter(*document, 0);
       
    91 
       
    92     buffer = new QBuffer();
       
    93     buffer->open(QIODevice::WriteOnly);
       
    94     xmlWriter = new QXmlStreamWriter(buffer);
       
    95     xmlWriter->writeNamespace(odfWriter->officeNS, "office");
       
    96     xmlWriter->writeNamespace(odfWriter->textNS, "text");
       
    97     xmlWriter->writeNamespace(odfWriter->styleNS, "style");
       
    98     xmlWriter->writeNamespace(odfWriter->foNS, "fo");
       
    99     xmlWriter->writeNamespace(odfWriter->tableNS, "table");
       
   100     xmlWriter->writeStartDocument();
       
   101     xmlWriter->writeStartElement("dummy");
       
   102 }
       
   103 
       
   104 void tst_QTextOdfWriter::cleanup()
       
   105 {
       
   106     delete document;
       
   107     delete odfWriter;
       
   108     delete xmlWriter;
       
   109     delete buffer;
       
   110 }
       
   111 
       
   112 QString tst_QTextOdfWriter::getContentFromXml()
       
   113 {
       
   114     xmlWriter->writeEndDocument();
       
   115     buffer->close();
       
   116     QString stringContent = QString::fromUtf8(buffer->data());
       
   117     int index = stringContent.indexOf("<dummy");
       
   118     Q_ASSERT(index);
       
   119     index = stringContent.indexOf('>', index);
       
   120     stringContent = stringContent.mid(index+1, stringContent.length() - index - 10);
       
   121     return stringContent;
       
   122 }
       
   123 
       
   124 void tst_QTextOdfWriter::testWriteParagraph_data()
       
   125 {
       
   126     QTest::addColumn<QString>("input");
       
   127     QTest::addColumn<QString>("xml");
       
   128 
       
   129     QTest::newRow("empty") << "" <<
       
   130         "<text:p text:style-name=\"p1\"/>";
       
   131     QTest::newRow("spaces") << "foobar   word" <<
       
   132         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">foobar <text:s text:c=\"2\"/>word</text:span></text:p>";
       
   133     QTest::newRow("starting spaces") << "  starting spaces" <<
       
   134         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\"><text:s text:c=\"2\"/>starting spaces</text:span></text:p>";
       
   135     QTest::newRow("trailing spaces") << "trailing spaces  " <<
       
   136         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">trailing spaces <text:s/></text:span></text:p>";
       
   137     QTest::newRow("tab") << "word\ttab x" <<
       
   138         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">word<text:tab/>tab x</text:span></text:p>";
       
   139     QTest::newRow("tab2") << "word\t\ttab\tx" <<
       
   140         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">word<text:tab/><text:tab/>tab<text:tab/>x</text:span></text:p>";
       
   141     QTest::newRow("misc") << "foobar   word\ttab x" <<
       
   142         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">foobar <text:s text:c=\"2\"/>word<text:tab/>tab x</text:span></text:p>";
       
   143     QTest::newRow("misc2") << "\t     \tFoo" <<
       
   144         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\"><text:tab/> <text:s text:c=\"4\"/><text:tab/>Foo</text:span></text:p>";
       
   145     QTest::newRow("linefeed") << QString("line1%1line2").arg(QChar(0x2028)) <<
       
   146         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">line1<text:line-break/>line2</text:span></text:p>";
       
   147     QTest::newRow("spaces") << "The quick brown fox jumped over the lazy dog" <<
       
   148         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">The quick brown fox jumped over the lazy dog</text:span></text:p>";
       
   149 }
       
   150 
       
   151 void tst_QTextOdfWriter::testWriteParagraph()
       
   152 {
       
   153     QFETCH(QString, input);
       
   154     QFETCH(QString, xml);
       
   155 
       
   156     QTextCursor cursor(document);
       
   157     cursor.insertText(input);
       
   158 
       
   159     odfWriter->writeBlock(*xmlWriter, document->begin());
       
   160     QCOMPARE( getContentFromXml(), xml);
       
   161 }
       
   162 
       
   163 void tst_QTextOdfWriter::testWriteStyle1_data()
       
   164 {
       
   165     QTest::addColumn<QString>("htmlInput");
       
   166     QTest::addColumn<int>("cursorPosition");
       
   167     QTest::addColumn<QString>("xml");
       
   168 
       
   169     QString text1 = "Normal<b>bold</b><i>italic</i><b><i>Bold/Italic</i></b>";
       
   170     QTest::newRow("normal") << text1 << 2 <<
       
   171         "<style:style style:name=\"c4\" style:family=\"text\"><style:text-properties fo:font-family=\"Sans\"/></style:style>";
       
   172     QTest::newRow("bold") << text1 << 10 <<
       
   173         "<style:style style:name=\"c4\" style:family=\"text\"><style:text-properties fo:font-weight=\"bold\" fo:font-family=\"Sans\"/></style:style>";
       
   174     QTest::newRow("italic") << text1 << 14 <<
       
   175         "<style:style style:name=\"c4\" style:family=\"text\"><style:text-properties fo:font-style=\"italic\" fo:font-family=\"Sans\"/></style:style>";
       
   176     QTest::newRow("bold+italic") << text1 << 25 <<
       
   177         "<style:style style:name=\"c4\" style:family=\"text\"><style:text-properties fo:font-style=\"italic\" fo:font-weight=\"bold\" fo:font-family=\"Sans\"/></style:style>";
       
   178 }
       
   179 
       
   180 void tst_QTextOdfWriter::testWriteStyle1()
       
   181 {
       
   182     QFETCH(QString, htmlInput);
       
   183     QFETCH(int, cursorPosition);
       
   184     QFETCH(QString, xml);
       
   185     document->setHtml(htmlInput);
       
   186 
       
   187     QTextCursor cursor(document);
       
   188     cursor.setPosition(cursorPosition);
       
   189     odfWriter->writeCharacterFormat(*xmlWriter, cursor.charFormat(), 4);
       
   190     QCOMPARE( getContentFromXml(), xml);
       
   191 }
       
   192 
       
   193 void tst_QTextOdfWriter::testWriteStyle2()
       
   194 {
       
   195     QTextBlockFormat bf; // = cursor.blockFormat();
       
   196     QList<QTextOption::Tab> tabs;
       
   197     QTextOption::Tab tab1;
       
   198     tab1.position = 40;
       
   199     tab1.type = QTextOption::RightTab;
       
   200     tabs << tab1;
       
   201     QTextOption::Tab tab2;
       
   202     tab2.position = 80;
       
   203     tab2.type = QTextOption::DelimiterTab;
       
   204     tab2.delimiter = 'o';
       
   205     tabs << tab2;
       
   206     bf.setTabPositions(tabs);
       
   207 
       
   208     odfWriter->writeBlockFormat(*xmlWriter, bf, 1);
       
   209     QString xml = QString::fromLatin1(
       
   210         "<style:style style:name=\"p1\" style:family=\"paragraph\">"
       
   211             "<style:paragraph-properties>"
       
   212                 "<style:style-tab-stops>"
       
   213                     "<style:style-tab-stop style:position=\"30pt\" style:type=\"right\"/>"
       
   214                     "<style:style-tab-stop style:position=\"60pt\" style:type=\"char\" style:char=\"o\"/>"
       
   215                 "</style:style-tab-stops>"
       
   216             "</style:paragraph-properties>"
       
   217         "</style:style>");
       
   218     QCOMPARE(getContentFromXml(), xml);
       
   219 }
       
   220 
       
   221 void tst_QTextOdfWriter::testWriteList()
       
   222 {
       
   223     QTextCursor cursor(document);
       
   224     QTextList *list = cursor.createList(QTextListFormat::ListDisc);
       
   225     cursor.insertText("ListItem 1");
       
   226     list->add(cursor.block());
       
   227     cursor.insertBlock();
       
   228     cursor.insertText("ListItem 2");
       
   229     list->add(cursor.block());
       
   230 
       
   231     odfWriter->writeBlock(*xmlWriter, cursor.block());
       
   232     QString xml = QString::fromLatin1(
       
   233         "<text:list text:style-name=\"L2\">"
       
   234           "<text:list-item>"
       
   235         //"<text:numbered-paragraph text:style-name=\"L2\" text:level=\"1\">"
       
   236             //"<text:number>")+ QChar(0x25cf) + QString::fromLatin1("</text:number>" // 0x25cf is a bullet
       
   237             "<text:p text:style-name=\"p3\"><text:span text:style-name=\"c0\">ListItem 2</text:span></text:p>"
       
   238           "</text:list-item>"
       
   239         "</text:list>");
       
   240 
       
   241     QCOMPARE(getContentFromXml(), xml);
       
   242 }
       
   243 
       
   244 void tst_QTextOdfWriter::testWriteList2()
       
   245 {
       
   246     QTextCursor cursor(document);
       
   247     QTextList *list = cursor.createList(QTextListFormat::ListDisc);
       
   248     cursor.insertText("Cars");
       
   249     list->add(cursor.block());
       
   250     cursor.insertBlock();
       
   251     QTextListFormat level2;
       
   252     level2.setStyle(QTextListFormat::ListSquare);
       
   253     level2.setIndent(2);
       
   254     QTextList *list2 = cursor.createList(level2);
       
   255     cursor.insertText("Model T");
       
   256     list2->add(cursor.block());
       
   257     cursor.insertBlock();
       
   258     cursor.insertText("Kitt");
       
   259     list2->add(cursor.block());
       
   260     cursor.insertBlock();
       
   261     cursor.insertText("Animals");
       
   262     list->add(cursor.block());
       
   263 
       
   264     cursor.insertBlock(QTextBlockFormat(), QTextCharFormat()); // start a new completely unrelated list.
       
   265     QTextList *list3 = cursor.createList(QTextListFormat::ListDecimal);
       
   266     cursor.insertText("Foo");
       
   267     list3->add(cursor.block());
       
   268 
       
   269     // and another block thats NOT in a list.
       
   270     cursor.insertBlock(QTextBlockFormat(), QTextCharFormat());
       
   271     cursor.insertText("Bar");
       
   272 
       
   273     odfWriter->writeFrame(*xmlWriter, document->rootFrame());
       
   274     QString xml = QString::fromLatin1(
       
   275         "<text:list text:style-name=\"L2\">"
       
   276           "<text:list-item>"
       
   277         //"<text:numbered-paragraph text:style-name=\"L2\" text:level=\"1\">"
       
   278             //"<text:number>")+ QChar(0x25cf) + QString::fromLatin1("</text:number>" // 0x25cf is a bullet
       
   279             "<text:p text:style-name=\"p3\"><text:span text:style-name=\"c0\">Cars</text:span></text:p>"
       
   280           "</text:list-item>"
       
   281           "<text:list-item>"
       
   282             "<text:list text:style-name=\"L4\">"
       
   283               "<text:list-item>"
       
   284                 "<text:p text:style-name=\"p5\"><text:span text:style-name=\"c0\">Model T</text:span></text:p>"
       
   285               "</text:list-item>"
       
   286               "<text:list-item>"
       
   287                 "<text:p text:style-name=\"p5\"><text:span text:style-name=\"c0\">Kitt</text:span></text:p>"
       
   288               "</text:list-item>"
       
   289             "</text:list>"
       
   290           "</text:list-item>"
       
   291           "<text:list-item>"
       
   292             "<text:p text:style-name=\"p3\"><text:span text:style-name=\"c0\">Animals</text:span></text:p>"
       
   293           "</text:list-item>"
       
   294         "</text:list>"
       
   295         "<text:list text:style-name=\"L6\">"
       
   296           "<text:list-item>"
       
   297             "<text:p text:style-name=\"p7\"><text:span text:style-name=\"c0\">Foo</text:span></text:p>"
       
   298           "</text:list-item>"
       
   299         "</text:list>"
       
   300         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">Bar</text:span></text:p>");
       
   301 
       
   302     // QString x = getContentFromXml();
       
   303     // for (int i=0; i < x.length(); i+=150) qDebug() << x.mid(i, 150);
       
   304     QCOMPARE(getContentFromXml(), xml);
       
   305 }
       
   306 
       
   307 
       
   308 void tst_QTextOdfWriter::createArchive()
       
   309 {
       
   310     document->setPlainText("a"); // simple doc is enough ;)
       
   311     QTextOdfWriter writer(*document, buffer);
       
   312     QCOMPARE(writer.createArchive(), true); // default
       
   313     writer.writeAll();
       
   314 /*
       
   315 QFile file("createArchive-odt");
       
   316 file.open(QIODevice::WriteOnly);
       
   317 file.write(buffer->data());
       
   318 file.close();
       
   319 */
       
   320     QVERIFY(buffer->data().length() > 80);
       
   321     QCOMPARE(buffer->data()[0], 'P'); // its a zip :)
       
   322     QCOMPARE(buffer->data()[1], 'K');
       
   323     QString mimetype(buffer->data().mid(38, 39));
       
   324     QCOMPARE(mimetype, QString::fromLatin1("application/vnd.oasis.opendocument.text"));
       
   325 }
       
   326 
       
   327 void tst_QTextOdfWriter::testWriteAll()
       
   328 {
       
   329     document->setPlainText("a"); // simple doc is enough ;)
       
   330     QTextOdfWriter writer(*document, buffer);
       
   331     QCOMPARE(writer.createArchive(), true);
       
   332     writer.setCreateArchive(false);
       
   333     writer.writeAll();
       
   334     QString result = QString(buffer->data());
       
   335     // details we check elsewhere, all we have to do is check availability.
       
   336     QVERIFY(result.indexOf("office:automatic-styles") >= 0);
       
   337     QVERIFY(result.indexOf("<style:style style:name=\"p1\"") >= 0);
       
   338     QVERIFY(result.indexOf("<style:style style:name=\"c0\"") >= 0);
       
   339     QVERIFY(result.indexOf("office:body") >= 0);
       
   340     QVERIFY(result.indexOf("office:text") >= 0);
       
   341     QVERIFY(result.indexOf("style:style") >= 0);
       
   342 }
       
   343 
       
   344 void tst_QTextOdfWriter::testWriteSection()
       
   345 {
       
   346     QTextCursor cursor(document);
       
   347     cursor.insertText("foo\nBar");
       
   348     QTextFrameFormat ff;
       
   349     cursor.insertFrame(ff);
       
   350     cursor.insertText("baz");
       
   351 
       
   352     odfWriter->writeFrame(*xmlWriter, document->rootFrame());
       
   353     QString xml = QString::fromLatin1(
       
   354         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">foo</text:span></text:p>"
       
   355         "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">Bar</text:span></text:p>"
       
   356         "<text:section>"
       
   357             "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">baz</text:span></text:p>"
       
   358         "</text:section>"
       
   359         "<text:p text:style-name=\"p1\"/>");
       
   360 
       
   361     QCOMPARE(getContentFromXml(), xml);
       
   362 }
       
   363 
       
   364 void tst_QTextOdfWriter::testWriteTable()
       
   365 {
       
   366     // create table with merged cells
       
   367     QTextCursor cursor(document);
       
   368     QTextTable * table = cursor.insertTable(3, 3);
       
   369     table->mergeCells(1, 0, 2, 2);
       
   370     table->mergeCells(0, 1, 1, 2);
       
   371     cursor = table->cellAt(0, 0).firstCursorPosition();
       
   372     cursor.insertText("a");
       
   373     cursor.movePosition(QTextCursor::NextCell);
       
   374     cursor.insertText("b");
       
   375     cursor.movePosition(QTextCursor::NextCell);
       
   376     cursor.insertText("c");
       
   377     cursor.movePosition(QTextCursor::NextCell);
       
   378     cursor.insertText("d");
       
   379     cursor.movePosition(QTextCursor::NextCell);
       
   380     cursor.insertText("e");
       
   381     /*
       
   382       +-+---+
       
   383       |a|b  |
       
   384       +-+-+-+
       
   385       |c  |d|
       
   386       +   +-+
       
   387       |   |e|
       
   388       +-+-+-+
       
   389     */
       
   390 
       
   391     odfWriter->writeFrame(*xmlWriter, document->rootFrame());
       
   392     QString xml = QString::fromLatin1(
       
   393         "<text:p text:style-name=\"p1\"/>"
       
   394         "<table:table>"
       
   395             "<table:table-column table:number-columns-repeated=\"3\"/>"
       
   396             "<table:table-row>"
       
   397                 "<table:table-cell table:style-name=\"T3\">"
       
   398                     "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">a</text:span></text:p>"
       
   399                 "</table:table-cell>"
       
   400                 "<table:table-cell table:number-columns-spanned=\"2\" table:style-name=\"T6\">"
       
   401                     "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c7\">b</text:span></text:p>"
       
   402                 "</table:table-cell>"
       
   403             "</table:table-row>"
       
   404             "<table:table-row>"
       
   405                 "<table:table-cell table:number-columns-spanned=\"2\" table:number-rows-spanned=\"2\" table:style-name=\"T5\">"
       
   406                     "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c8\">c</text:span></text:p>"
       
   407                 "</table:table-cell>"
       
   408                 "<table:table-cell table:style-name=\"T3\">"
       
   409                     "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">d</text:span></text:p>"
       
   410                 "</table:table-cell>"
       
   411             "</table:table-row>"
       
   412             "<table:table-row>"
       
   413                 "<table:table-cell table:style-name=\"T3\">"
       
   414                     "<text:p text:style-name=\"p1\"><text:span text:style-name=\"c0\">e</text:span></text:p>"
       
   415                 "</table:table-cell>"
       
   416             "</table:table-row>"
       
   417         "</table:table>"
       
   418         "<text:p text:style-name=\"p1\"/>");
       
   419 
       
   420     QCOMPARE(getContentFromXml(), xml);
       
   421 }
       
   422 
       
   423 QTEST_MAIN(tst_QTextOdfWriter)
       
   424 #include "tst_qtextodfwriter.moc"