tests/auto/qtextdocument/tst_qtextdocument.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    57 #include <qurl.h>
    57 #include <qurl.h>
    58 #include <qpainter.h>
    58 #include <qpainter.h>
    59 #include <qfontmetrics.h>
    59 #include <qfontmetrics.h>
    60 #include <qimage.h>
    60 #include <qimage.h>
    61 #include <qtextlayout.h>
    61 #include <qtextlayout.h>
       
    62 #include <QDomDocument>
    62 #include "common.h"
    63 #include "common.h"
    63 
    64 
    64 
    65 
    65 QT_FORWARD_DECLARE_CLASS(QTextDocument)
    66 QT_FORWARD_DECLARE_CLASS(QTextDocument)
    66 
    67 
   173     void testUndoCommandAdded();
   174     void testUndoCommandAdded();
   174 
   175 
   175     void testUndoBlocks();
   176     void testUndoBlocks();
   176 
   177 
   177     void receiveCursorPositionChangedAfterContentsChange();
   178     void receiveCursorPositionChangedAfterContentsChange();
       
   179     void escape_data();
       
   180     void escape();
   178 
   181 
   179 private:
   182 private:
   180     void backgroundImage_checkExpectedHtml(const QTextDocument &doc);
   183     void backgroundImage_checkExpectedHtml(const QTextDocument &doc);
   181 
   184 
   182     QTextDocument *doc;
   185     QTextDocument *doc;
   574         }
   577         }
   575     }
   578     }
   576 }
   579 }
   577 
   580 
   578 void tst_QTextDocument::stylesheetFont_data()
   581 void tst_QTextDocument::stylesheetFont_data()
   579 {    
   582 {
   580     QTest::addColumn<QString>("stylesheet");
   583     QTest::addColumn<QString>("stylesheet");
   581     QTest::addColumn<QFont>("font");
   584     QTest::addColumn<QFont>("font");
   582 
   585 
   583     {
   586     {
   584         QFont font;
   587         QFont font;
   730         QTextCharFormat fmt;
   733         QTextCharFormat fmt;
   731         fmt.setFontFamily("Foo's Family");
   734         fmt.setFontFamily("Foo's Family");
   732         cursor.insertText("Blah", fmt);
   735         cursor.insertText("Blah", fmt);
   733 
   736 
   734         QTest::newRow("font-family-with-quotes1") << QTextDocumentFragment(&doc)
   737         QTest::newRow("font-family-with-quotes1") << QTextDocumentFragment(&doc)
   735                                   << QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:\"Foo's Family\";\">Blah</span></p>");
   738                                   << QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:&quot;Foo's Family&quot;;\">Blah</span></p>");
   736     }
   739     }
   737 
   740 
   738     {
   741     {
   739         CREATE_DOC_AND_CURSOR();
   742         CREATE_DOC_AND_CURSOR();
   740 
   743 
   741         QTextCharFormat fmt;
   744         QTextCharFormat fmt;
   742         fmt.setFontFamily("Foo\"s Family");
   745         fmt.setFontFamily("Foo\"s Family");
   743         cursor.insertText("Blah", fmt);
   746         cursor.insertText("Blah", fmt);
   744 
   747 
   745         QTest::newRow("font-family-with-quotes2") << QTextDocumentFragment(&doc)
   748         QTest::newRow("font-family-with-quotes2") << QTextDocumentFragment(&doc)
   746                                   << QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:'Foo\"s Family';\">Blah</span></p>");
   749                                   << QString("<p DEFAULTBLOCKSTYLE><span style=\" font-family:'Foo&quot;s Family';\">Blah</span></p>");
   747     }
   750     }
   748 
   751 
   749     {
   752     {
   750         CREATE_DOC_AND_CURSOR();
   753         CREATE_DOC_AND_CURSOR();
   751 
   754 
   967         fmt.setAnchorHref("http://www.kde.org/");
   970         fmt.setAnchorHref("http://www.kde.org/");
   968         cursor.insertText("Blah", fmt);
   971         cursor.insertText("Blah", fmt);
   969 
   972 
   970         QTest::newRow("href anchor") << QTextDocumentFragment(&doc)
   973         QTest::newRow("href anchor") << QTextDocumentFragment(&doc)
   971                                   << QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/\">Blah</a></p>");
   974                                   << QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/\">Blah</a></p>");
       
   975     }
       
   976 
       
   977     {
       
   978         CREATE_DOC_AND_CURSOR();
       
   979 
       
   980         QTextCharFormat fmt;
       
   981         fmt.setAnchor(true);
       
   982         fmt.setAnchorHref("http://www.kde.org/?a=1&b=2");
       
   983         cursor.insertText("Blah", fmt);
       
   984 
       
   985         QTest::newRow("href anchor with &") << QTextDocumentFragment(&doc)
       
   986                                   << QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/?a=1&amp;b=2\">Blah</a></p>");
       
   987     }
       
   988 
       
   989     {
       
   990         CREATE_DOC_AND_CURSOR();
       
   991 
       
   992         QTextCharFormat fmt;
       
   993         fmt.setAnchor(true);
       
   994         fmt.setAnchorHref("http://www.kde.org/?a='&b=\"");
       
   995         cursor.insertText("Blah", fmt);
       
   996 
       
   997         QTest::newRow("href anchor with ' and \"") << QTextDocumentFragment(&doc)
       
   998                                   << QString("<p DEFAULTBLOCKSTYLE><a href=\"http://www.kde.org/?a='&amp;b=&quot;\">Blah</a></p>");
   972     }
   999     }
   973 
  1000 
   974     {
  1001     {
   975         CREATE_DOC_AND_CURSOR();
  1002         CREATE_DOC_AND_CURSOR();
   976 
  1003 
  1539     expectedOutput.append(htmlTail);
  1566     expectedOutput.append(htmlTail);
  1540 
  1567 
  1541     QString output = doc->toHtml();
  1568     QString output = doc->toHtml();
  1542 
  1569 
  1543     QCOMPARE(output, expectedOutput);
  1570     QCOMPARE(output, expectedOutput);
       
  1571 
       
  1572     QDomDocument document;
       
  1573     QVERIFY2(document.setContent(output), "Output was not valid XML");
  1544 }
  1574 }
  1545 
  1575 
  1546 void tst_QTextDocument::toHtml2()
  1576 void tst_QTextDocument::toHtml2()
  1547 {
  1577 {
  1548     QTextDocument doc;
  1578     QTextDocument doc;
  1689     cf.setFontCapitalization(QFont::SmallCaps);
  1719     cf.setFontCapitalization(QFont::SmallCaps);
  1690     cursor.mergeCharFormat(cf);
  1720     cursor.mergeCharFormat(cf);
  1691 
  1721 
  1692     const QString smallcaps = doc->toHtml();
  1722     const QString smallcaps = doc->toHtml();
  1693     QVERIFY(re.exactMatch(doc->toHtml()));
  1723     QVERIFY(re.exactMatch(doc->toHtml()));
  1694     QCOMPARE(re.numCaptures(), 1);
  1724     QCOMPARE(re.captureCount(), 1);
  1695     QCOMPARE(re.cap(1).trimmed(), QString("font-variant:small-caps;"));
  1725     QCOMPARE(re.cap(1).trimmed(), QString("font-variant:small-caps;"));
  1696 
  1726 
  1697     cf.setFontCapitalization(QFont::AllUppercase);
  1727     cf.setFontCapitalization(QFont::AllUppercase);
  1698     cursor.mergeCharFormat(cf);
  1728     cursor.mergeCharFormat(cf);
  1699     const QString uppercase = doc->toHtml();
  1729     const QString uppercase = doc->toHtml();
  1700     QVERIFY(re.exactMatch(doc->toHtml()));
  1730     QVERIFY(re.exactMatch(doc->toHtml()));
  1701     QCOMPARE(re.numCaptures(), 1);
  1731     QCOMPARE(re.captureCount(), 1);
  1702     QCOMPARE(re.cap(1).trimmed(), QString("text-transform:uppercase;"));
  1732     QCOMPARE(re.cap(1).trimmed(), QString("text-transform:uppercase;"));
  1703 
  1733 
  1704     cf.setFontCapitalization(QFont::AllLowercase);
  1734     cf.setFontCapitalization(QFont::AllLowercase);
  1705     cursor.mergeCharFormat(cf);
  1735     cursor.mergeCharFormat(cf);
  1706     const QString lowercase = doc->toHtml();
  1736     const QString lowercase = doc->toHtml();
  1707     QVERIFY(re.exactMatch(doc->toHtml()));
  1737     QVERIFY(re.exactMatch(doc->toHtml()));
  1708     QCOMPARE(re.numCaptures(), 1);
  1738     QCOMPARE(re.captureCount(), 1);
  1709     QCOMPARE(re.cap(1).trimmed(), QString("text-transform:lowercase;"));
  1739     QCOMPARE(re.cap(1).trimmed(), QString("text-transform:lowercase;"));
  1710 
  1740 
  1711     doc->setHtml(smallcaps);
  1741     doc->setHtml(smallcaps);
  1712     cursor.setPosition(1);
  1742     cursor.setPosition(1);
  1713     QCOMPARE(cursor.charFormat().fontCapitalization(), QFont::SmallCaps);
  1743     QCOMPARE(cursor.charFormat().fontCapitalization(), QFont::SmallCaps);
  1729     QTextCharFormat cf;
  1759     QTextCharFormat cf;
  1730     cf.setFontWordSpacing(4);
  1760     cf.setFontWordSpacing(4);
  1731     cursor.mergeCharFormat(cf);
  1761     cursor.mergeCharFormat(cf);
  1732 
  1762 
  1733     QVERIFY(re.exactMatch(doc->toHtml()));
  1763     QVERIFY(re.exactMatch(doc->toHtml()));
  1734     QCOMPARE(re.numCaptures(), 1);
  1764     QCOMPARE(re.captureCount(), 1);
  1735     QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:4px;"));
  1765     QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:4px;"));
  1736 
  1766 
  1737     cf.setFontWordSpacing(-8.5);
  1767     cf.setFontWordSpacing(-8.5);
  1738     cursor.mergeCharFormat(cf);
  1768     cursor.mergeCharFormat(cf);
  1739 
  1769 
  1740     QVERIFY(re.exactMatch(doc->toHtml()));
  1770     QVERIFY(re.exactMatch(doc->toHtml()));
  1741     QCOMPARE(re.numCaptures(), 1);
  1771     QCOMPARE(re.captureCount(), 1);
  1742     QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:-8.5px;"));
  1772     QCOMPARE(re.cap(1).trimmed(), QString("word-spacing:-8.5px;"));
  1743 }
  1773 }
  1744 
  1774 
  1745 class CursorPosSignalSpy : public QObject
  1775 class CursorPosSignalSpy : public QObject
  1746 {
  1776 {
  2583     QVERIFY(spy.isEmpty());
  2613     QVERIFY(spy.isEmpty());
  2584     QTextCharFormat cf;
  2614     QTextCharFormat cf;
  2585     cf.setFontItalic(true);
  2615     cf.setFontItalic(true);
  2586     cursor.mergeCharFormat(cf);
  2616     cursor.mergeCharFormat(cf);
  2587     QCOMPARE(spy.count(), 1);
  2617     QCOMPARE(spy.count(), 1);
       
  2618 
       
  2619     spy.clear();
       
  2620     doc->undo();
       
  2621     QCOMPARE(spy.count(), 0);
       
  2622     doc->undo();
       
  2623     QCOMPARE(spy.count(), 0);
       
  2624     spy.clear();
       
  2625     doc->redo();
       
  2626     QCOMPARE(spy.count(), 0);
       
  2627     doc->redo();
       
  2628     QCOMPARE(spy.count(), 0);
  2588 }
  2629 }
  2589 
  2630 
  2590 void tst_QTextDocument::testUndoBlocks()
  2631 void tst_QTextDocument::testUndoBlocks()
  2591 {
  2632 {
  2592     QVERIFY(doc);
  2633     QVERIFY(doc);
  2650             &rec, SLOT(contentsChange()));
  2691             &rec, SLOT(contentsChange()));
  2651     cursor.insertText("Hello World");
  2692     cursor.insertText("Hello World");
  2652     QCOMPARE(rec.first, QString("contentsChanged"));
  2693     QCOMPARE(rec.first, QString("contentsChanged"));
  2653 }
  2694 }
  2654 
  2695 
       
  2696 void tst_QTextDocument::escape_data()
       
  2697 {
       
  2698     QTest::addColumn<QString>("original");
       
  2699     QTest::addColumn<QString>("expected");
       
  2700 
       
  2701     QTest::newRow("1") << "Hello World\n" << "Hello World\n";
       
  2702     QTest::newRow("2") << "#include <QtCore>" << "#include &lt;QtCore&gt;";
       
  2703     QTest::newRow("3") << "<p class=\"cool\"><a href=\"http://example.com/?foo=bar&amp;bar=foo\">plop --&gt; </a></p>"
       
  2704                        << "&lt;p class=&quot;cool&quot;&gt;&lt;a href=&quot;http://example.com/?foo=bar&amp;amp;bar=foo&quot;&gt;plop --&amp;gt; &lt;/a&gt;&lt;/p&gt;";
       
  2705     QTest::newRow("4") << QString::fromUtf8("<\320\222\321\201>") << QString::fromUtf8("&lt;\320\222\321\201&gt;");
       
  2706 }
       
  2707 
       
  2708 void tst_QTextDocument::escape()
       
  2709 {
       
  2710     QFETCH(QString, original);
       
  2711     QFETCH(QString, expected);
       
  2712 
       
  2713     QCOMPARE(Qt::escape(original), expected);
       
  2714 }
       
  2715 
  2655 QTEST_MAIN(tst_QTextDocument)
  2716 QTEST_MAIN(tst_QTextDocument)
  2656 #include "tst_qtextdocument.moc"
  2717 #include "tst_qtextdocument.moc"