File indexing completed on 2024-04-21 03:57:09

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2015 Zoe Clifford <zoeacacia@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "bug205447.h"
0009 
0010 #include <katedocument.h>
0011 #include <kateglobal.h>
0012 #include <kateview.h>
0013 #include <kmainwindow.h>
0014 #include <ktexteditor/documentcursor.h>
0015 
0016 #include <QTest>
0017 
0018 #include "testutils.h"
0019 
0020 QTEST_MAIN(BugTest)
0021 
0022 using namespace KTextEditor;
0023 
0024 BugTest::BugTest()
0025     : QObject()
0026 {
0027 }
0028 
0029 BugTest::~BugTest()
0030 {
0031 }
0032 
0033 void BugTest::initTestCase()
0034 {
0035     KTextEditor::EditorPrivate::enableUnitTestMode();
0036 }
0037 
0038 void BugTest::cleanupTestCase()
0039 {
0040 }
0041 
0042 void BugTest::deleteSurrogates()
0043 {
0044     // set up document and view and open test file
0045     KTextEditor::DocumentPrivate doc;
0046     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0047     const QUrl url = QUrl::fromLocalFile(QLatin1String(TEST_DATA_DIR "bug205447.txt"));
0048     doc.setEncoding(QStringLiteral("UTF-8"));
0049     QVERIFY(doc.openUrl(url));
0050 
0051     // test delete
0052     // get UTF-32 representation of original line (before any deletes)
0053     QList<uint> line = doc.line(0).toUcs4();
0054     QVERIFY(line.size() == 23);
0055     // delete from start of line
0056     view->setCursorPosition(Cursor(0, 0));
0057     QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
0058     for (int i = 0; i < line.size(); i++) {
0059         // get the current line, after `i` delete presses, and convert it to utf32
0060         // then ensure it's the expected substring of the original line
0061         QList<uint> current = doc.line(0).toUcs4();
0062         QCOMPARE(current, line.mid(i));
0063 
0064         // press the delete key and verify that the new text position isn't invalid
0065         view->keyDelete();
0066         QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
0067     }
0068     QCOMPARE(doc.lineLength(0), 0);
0069 }
0070 
0071 void BugTest::backspaceSurrogates()
0072 {
0073     // set up document and view and open test file
0074     KTextEditor::DocumentPrivate doc;
0075     KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
0076     const QUrl url = QUrl::fromLocalFile(QLatin1String(TEST_DATA_DIR "bug205447.txt"));
0077     doc.setEncoding(QStringLiteral("UTF-8"));
0078     QVERIFY(doc.openUrl(url));
0079 
0080     // test backspace
0081     // get UTF-32 representation of original line (before any backspaces)
0082     QList<uint> line = doc.line(0).toUcs4();
0083     QVERIFY(line.size() == 23);
0084     // backspace from end of line
0085     view->setCursorPosition(Cursor(0, doc.line(0).size()));
0086     QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
0087     for (int i = 0; i < line.size(); i++) {
0088         // get the current line, after `i` delete presses, and convert it to utf32
0089         // then ensure it's the expected substring of the original line
0090         QList<uint> current = doc.line(0).toUcs4();
0091         QCOMPARE(current, line.mid(0, line.size() - i));
0092 
0093         // press the backspace key and verify that the new text position isn't invalid
0094         view->backspace();
0095         QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
0096     }
0097     QCOMPARE(doc.lineLength(0), 0);
0098 }
0099 
0100 #include "moc_bug205447.cpp"