/**************************************************************************** ** ** Copyright (C) 2016 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the test suite of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3 as published by the Free Software ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT ** included in the packaging of this file. Please review the following ** information to ensure the GNU General Public License requirements will ** be met: https://www.gnu.org/licenses/gpl-3.0.html. ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include #include #include #include class tst_QTextOdfWriter : public QObject { Q_OBJECT public slots: void init(); void cleanup(); private slots: void testWriteParagraph_data(); void testWriteParagraph(); void testWriteStyle1_data(); void testWriteStyle1(); void testWriteStyle2(); void testWriteList(); void testWriteList2(); void createArchive(); void testWriteAll(); void testWriteSection(); void testWriteTable(); void testWriteFrameFormat(); private: /// closes the document and returns the part of the XML stream that the test wrote QString getContentFromXml(); private: QTextDocument *document; QXmlStreamWriter *xmlWriter; QTextOdfWriter *odfWriter; QBuffer *buffer; }; void tst_QTextOdfWriter::init() { document = new QTextDocument(); odfWriter = new QTextOdfWriter(*document, 0); buffer = new QBuffer(); buffer->open(QIODevice::WriteOnly); xmlWriter = new QXmlStreamWriter(buffer); xmlWriter->writeNamespace(odfWriter->officeNS, "office"); xmlWriter->writeNamespace(odfWriter->textNS, "text"); xmlWriter->writeNamespace(odfWriter->styleNS, "style"); xmlWriter->writeNamespace(odfWriter->foNS, "fo"); xmlWriter->writeNamespace(odfWriter->tableNS, "table"); xmlWriter->writeStartDocument(); xmlWriter->writeStartElement("dummy"); } void tst_QTextOdfWriter::cleanup() { delete document; delete odfWriter; delete xmlWriter; delete buffer; } QString tst_QTextOdfWriter::getContentFromXml() { xmlWriter->writeEndDocument(); buffer->close(); QString stringContent = QString::fromUtf8(buffer->data()); QString ret; int index = stringContent.indexOf(" 0) { index = stringContent.indexOf('>', index); if (index > 0) ret = stringContent.mid(index+1, stringContent.length() - index - 10); } return ret; } void tst_QTextOdfWriter::testWriteParagraph_data() { QTest::addColumn("input"); QTest::addColumn("xml"); QTest::newRow("empty") << "" << ""; QTest::newRow("spaces") << "foobar word" << "foobar word"; QTest::newRow("starting spaces") << " starting spaces" << "starting spaces"; QTest::newRow("trailing spaces") << "trailing spaces " << "trailing spaces "; QTest::newRow("tab") << "word\ttab x" << "wordtab x"; QTest::newRow("tab2") << "word\t\ttab\tx" << "wordtabx"; QTest::newRow("misc") << "foobar word\ttab x" << "foobar wordtab x"; QTest::newRow("misc2") << "\t \tFoo" << " Foo"; QTest::newRow("linefeed") << (QStringLiteral("line1") + QChar(0x2028) + QStringLiteral("line2")) << "line1line2"; QTest::newRow("spaces") << "The quick brown fox jumped over the lazy dog" << "The quick brown fox jumped over the lazy dog"; } void tst_QTextOdfWriter::testWriteParagraph() { QFETCH(QString, input); QFETCH(QString, xml); QTextCursor cursor(document); cursor.insertText(input); odfWriter->writeBlock(*xmlWriter, document->begin()); QCOMPARE( getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteStyle1_data() { QTest::addColumn("htmlInput"); QTest::addColumn("cursorPosition"); QTest::addColumn("xml"); QString text1 = "NormalbolditalicBold/Italic"; QTest::newRow("normal") << text1 << 2 << ""; QTest::newRow("bold") << text1 << 10 << ""; QTest::newRow("italic") << text1 << 14 << ""; QTest::newRow("bold+italic") << text1 << 25 << ""; QString colorText = " Color Text "; QTest::newRow("green/red") << colorText << 3 << ""; } void tst_QTextOdfWriter::testWriteStyle1() { QFETCH(QString, htmlInput); QFETCH(int, cursorPosition); QFETCH(QString, xml); document->setHtml(htmlInput); QTextCursor cursor(document); cursor.setPosition(cursorPosition); odfWriter->writeCharacterFormat(*xmlWriter, cursor.charFormat(), 4); QCOMPARE( getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteStyle2() { QTextBlockFormat bf; // = cursor.blockFormat(); QList tabs; QTextOption::Tab tab1(40, QTextOption::RightTab); tabs << tab1; QTextOption::Tab tab2(80, QTextOption::DelimiterTab, 'o'); tabs << tab2; bf.setTabPositions(tabs); odfWriter->writeBlockFormat(*xmlWriter, bf, 1); QString xml = QString::fromLatin1( "" "" "" "" "" "" "" ""); QCOMPARE(getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteList() { QTextCursor cursor(document); QTextList *list = cursor.createList(QTextListFormat::ListDisc); cursor.insertText("ListItem 1"); list->add(cursor.block()); cursor.insertBlock(); cursor.insertText("ListItem 2"); list->add(cursor.block()); odfWriter->writeBlock(*xmlWriter, cursor.block()); QString xml = QString::fromLatin1( "" "" //"" //"")+ QChar(0x25cf) + QString::fromLatin1("" // 0x25cf is a bullet "ListItem 2" "" ""); QCOMPARE(getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteList2() { QTextCursor cursor(document); QTextList *list = cursor.createList(QTextListFormat::ListDisc); cursor.insertText("Cars"); list->add(cursor.block()); cursor.insertBlock(); QTextListFormat level2; level2.setStyle(QTextListFormat::ListSquare); level2.setIndent(2); QTextList *list2 = cursor.createList(level2); cursor.insertText("Model T"); list2->add(cursor.block()); cursor.insertBlock(); cursor.insertText("Kitt"); list2->add(cursor.block()); cursor.insertBlock(); cursor.insertText("Animals"); list->add(cursor.block()); cursor.insertBlock(QTextBlockFormat(), QTextCharFormat()); // start a new completely unrelated list. QTextList *list3 = cursor.createList(QTextListFormat::ListDecimal); cursor.insertText("Foo"); list3->add(cursor.block()); // and another block thats NOT in a list. cursor.insertBlock(QTextBlockFormat(), QTextCharFormat()); cursor.insertText("Bar"); odfWriter->writeFrame(*xmlWriter, document->rootFrame()); QString xml = QString::fromLatin1( "" "" //"" //"")+ QChar(0x25cf) + QString::fromLatin1("" // 0x25cf is a bullet "Cars" "" "" "" "" "Model T" "" "" "Kitt" "" "" "" "" "Animals" "" "" "" "" "Foo" "" "" "Bar"); // QString x = getContentFromXml(); // for (int i=0; i < x.length(); i+=150) qDebug() << x.mid(i, 150); QCOMPARE(getContentFromXml(), xml); } void tst_QTextOdfWriter::createArchive() { document->setPlainText("a"); // simple doc is enough ;) QTextOdfWriter writer(*document, buffer); QCOMPARE(writer.createArchive(), true); // default writer.writeAll(); /* QFile file("createArchive-odt"); file.open(QIODevice::WriteOnly); file.write(buffer->data()); file.close(); */ QVERIFY(buffer->data().length() > 80); QCOMPARE(buffer->data()[0], 'P'); // its a zip :) QCOMPARE(buffer->data()[1], 'K'); QString mimetype(buffer->data().mid(38, 39)); QCOMPARE(mimetype, QString::fromLatin1("application/vnd.oasis.opendocument.text")); } void tst_QTextOdfWriter::testWriteAll() { document->setPlainText("a"); // simple doc is enough ;) QTextOdfWriter writer(*document, buffer); QCOMPARE(writer.createArchive(), true); writer.setCreateArchive(false); writer.writeAll(); QString result = QString(buffer->data()); // details we check elsewhere, all we have to do is check availability. QVERIFY(result.indexOf("office:automatic-styles") >= 0); QVERIFY(result.indexOf("= 0); QVERIFY(result.indexOf("= 0); QVERIFY(result.indexOf("office:body") >= 0); QVERIFY(result.indexOf("office:text") >= 0); QVERIFY(result.indexOf("style:style") >= 0); } void tst_QTextOdfWriter::testWriteSection() { QTextCursor cursor(document); cursor.insertText("foo\nBar"); QTextFrameFormat ff; cursor.insertFrame(ff); cursor.insertText("baz"); odfWriter->writeFrame(*xmlWriter, document->rootFrame()); QString xml = QString::fromLatin1( "foo" "Bar" "" "baz" "" ""); QCOMPARE(getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteTable() { // create table with merged cells QTextCursor cursor(document); QTextTable * table = cursor.insertTable(3, 3); table->mergeCells(1, 0, 2, 2); table->mergeCells(0, 1, 1, 2); cursor = table->cellAt(0, 0).firstCursorPosition(); cursor.insertText("a"); cursor.movePosition(QTextCursor::NextCell); cursor.insertText("b"); cursor.movePosition(QTextCursor::NextCell); cursor.insertText("c"); cursor.movePosition(QTextCursor::NextCell); cursor.insertText("d"); cursor.movePosition(QTextCursor::NextCell); cursor.insertText("e"); /* +-+---+ |a|b | +-+-+-+ |c |d| + +-+ | |e| +-+-+-+ */ odfWriter->writeFrame(*xmlWriter, document->rootFrame()); QString xml = QString::fromLatin1( "" "" "" "" "" "a" "" "" "b" "" "" "" "" "c" "" "" "d" "" "" "" "" "e" "" "" "" ""); QCOMPARE(getContentFromXml(), xml); } void tst_QTextOdfWriter::testWriteFrameFormat() { QTextFrameFormat tff; tff.setTopMargin(20); tff.setBottomMargin(20); tff.setLeftMargin(20); tff.setRightMargin(20); QTextCursor tc(document); odfWriter->writeFrameFormat(*xmlWriter, tff, 0); // Value of 15pt is based on the pixelToPoint() calculation done in qtextodfwriter.cpp QString xml = QString::fromLatin1( "" "" ""); QCOMPARE(getContentFromXml(), xml); } QTEST_MAIN(tst_QTextOdfWriter) #include "tst_qtextodfwriter.moc"