File indexing completed on 2024-07-21 03:43:55

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2008 David Faure <faure@kde.org>
0004     SPDX-FileCopyrightText: 2008 Stephen Kelly <steveire@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include <QClipboard>
0010 #include <QTest>
0011 
0012 #include <ktextedit.h>
0013 
0014 class KTextEdit_UnitTest : public QObject
0015 {
0016     Q_OBJECT
0017 
0018 private Q_SLOTS:
0019     void testPaste();
0020     // These tests are probably invalid due to using invalid html.
0021     //     void testImportWithHorizontalTraversal();
0022     //     void testImportWithVerticalTraversal();
0023     //     void testBrInsideParagraphThroughTextEdit();
0024 };
0025 
0026 void KTextEdit_UnitTest::testPaste()
0027 {
0028     const QString origText = QApplication::clipboard()->text();
0029     const QString pastedText = QStringLiteral("Test paste from ktextedit_unittest");
0030     QApplication::clipboard()->setText(pastedText);
0031     KTextEdit w;
0032     w.setPlainText(QStringLiteral("Hello world"));
0033     w.selectAll();
0034     QTest::keyClick(&w, Qt::Key_V, Qt::ControlModifier);
0035     QCOMPARE(w.toPlainText(), pastedText);
0036     QApplication::clipboard()->setText(origText);
0037 }
0038 
0039 // void KTextEdit_UnitTest::testImportWithVerticalTraversal()
0040 // {
0041 //     QTextEdit *te = new QTextEdit();
0042 //
0043 //     te->setHtml("<p>Foo</p><br /><br /><br /><p>Bar</p>");
0044 //
0045 //     QTextCursor cursor = te->textCursor();
0046 //     cursor.movePosition(QTextCursor::Start);
0047 //     QVERIFY(cursor.block().text() == QString( "Foo" ));
0048 //     cursor.movePosition(QTextCursor::Down, QTextCursor::MoveAnchor, 4);
0049 //
0050 //     // Cursor is at the beginning of the block.
0051 //     QVERIFY(cursor.block().position() == cursor.position());
0052 //     QVERIFY(cursor.block().text() == QString( "Bar" ));
0053 // }
0054 //
0055 // void KTextEdit_UnitTest::testImportWithHorizontalTraversal()
0056 // {
0057 //     QTextEdit *te = new QTextEdit();
0058 //
0059 //     te->setHtml("<p>Foo</p><br /><p>Bar</p>");
0060 //
0061 //     // br elements should be represented just like empty paragraphs.
0062 //
0063 //     QTextCursor cursor = te->textCursor();
0064 //     cursor.movePosition(QTextCursor::Start);
0065 //     QVERIFY(cursor.block().text() == QString( "Foo" ));
0066 //     cursor.movePosition(QTextCursor::EndOfBlock);
0067 //     cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 2);
0068 //
0069 //     // Cursor is at the beginning of the block.
0070 //     QVERIFY(cursor.block().position() == cursor.position());
0071 //     QVERIFY(cursor.block().text() == QString( "Bar" ));
0072 // }
0073 //
0074 // void KTextEdit_UnitTest::testBrInsideParagraphThroughTextEdit()
0075 // {
0076 //     QSKIP("This is worked around during export");
0077 //     QTextEdit *te = new QTextEdit();
0078 //
0079 //     te->setHtml("<p>Foo<br />Bar</p>");
0080 //
0081 //     // br elements inside paragraphs should be a single linebreak.
0082 //
0083 //     QTextCursor cursor = te->textCursor();
0084 //     cursor.movePosition(QTextCursor::Start);
0085 //
0086 //     // This doesn't work, because Qt puts Foo and Bar in the same block, separated by a QChar::LineSeparator
0087 //
0088 //     QVERIFY(cursor.block().text() == QString( "Foo" ));
0089 //     cursor.movePosition(QTextCursor::EndOfBlock);
0090 //     cursor.movePosition(QTextCursor::Right);
0091 //
0092 //     // Cursor is at the beginning of the block.
0093 //     QVERIFY(cursor.block().position() == cursor.position());
0094 //     QVERIFY(cursor.block().text() == QString( "Bar" ));
0095 //
0096 // }
0097 
0098 QTEST_MAIN(KTextEdit_UnitTest)
0099 
0100 #include "ktextedit_unittest.moc"