Test for quadratic behaviour when rendering long line in QTextEdit
authorOlivier Goffart <ogoffart@woboq.com>
Thu, 16 Aug 2012 07:55:50 +0000 (09:55 +0200)
committerQt by Nokia <qt-info@nokia.com>
Thu, 23 Aug 2012 21:31:10 +0000 (23:31 +0200)
QTextEdit showing long lines using lots of text format (for example
with a syntax highlighter) used to expose a quadratic behaviour which
make it impossible to edit files with long lines.

It was fiexed in the few previous commit

Task-number: QTBUG-8389
Change-Id: Ib7203497a7699a85ae1dfb70fe65d5fb36884b58
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp

index 846bb41..7d64282 100644 (file)
@@ -62,6 +62,7 @@
 
 #include <qabstracttextdocumentlayout.h>
 #include <qtextdocumentfragment.h>
+#include <qsyntaxhighlighter.h>
 
 #include "../../../shared/platforminputcontext.h"
 #include <private/qinputmethod_p.h>
@@ -212,6 +213,8 @@ private slots:
     void inputMethodQueryImHints_data();
     void inputMethodQueryImHints();
 
+    void highlightLongLine();
+
 private:
     void createSelection();
     int blockCount() const;
@@ -2483,5 +2486,38 @@ void tst_QTextEdit::inputMethodQueryImHints()
     QCOMPARE(static_cast<Qt::InputMethodHints>(value.toInt()), hints);
 }
 
+void tst_QTextEdit::highlightLongLine()
+{
+    QTextEdit edit;
+    edit.setAcceptRichText(false);
+    edit.setWordWrapMode(QTextOption::NoWrap);
+
+    QString singeLongLine;
+    for (int i = 0; i < 10000; ++i)
+        singeLongLine += "0123456789";
+    edit.setPlainText(singeLongLine);
+
+    class NumHighlighter : public QSyntaxHighlighter {
+    public:
+        explicit NumHighlighter(QTextDocument*doc) : QSyntaxHighlighter(doc) {};
+        virtual void highlightBlock(const QString& text) {
+            // odd number in bold
+            QTextCharFormat format;
+            format.setFontWeight(QFont::Bold);
+            for (int i = 0; i < text.size(); ++i) {
+                if (text.at(i).unicode() % 2)
+                    setFormat(i, 1, format);
+            }
+        }
+    };
+    NumHighlighter nh(edit.document());
+    edit.show();
+    QTest::qWaitForWindowActive(edit.windowHandle());
+    QCoreApplication::processEvents();
+    //If there is a quadratic behaviour, this would take forever.
+    QVERIFY(true);
+}
+
+
 QTEST_MAIN(tst_QTextEdit)
 #include "tst_qtextedit.moc"