1/* This file is part of the KDE libraries
2 * Copyright (C) 2015 Zoe Clifford <zoeacacia@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "bug205447.h"
21
22#include <katedocument.h>
23#include <kateglobal.h>
24#include <kateview.h>
25#include <kmainwindow.h>
26#include <ktexteditor/documentcursor.h>
27
28#include <QtTestWidgets>
29
30#include "testutils.h"
31
32QTEST_MAIN(BugTest)
33
34using namespace KTextEditor;
35
36BugTest::BugTest()
37 : QObject()
38{
39}
40
41BugTest::~BugTest()
42{
43}
44
45void BugTest::initTestCase()
46{
47 KTextEditor::EditorPrivate::enableUnitTestMode();
48}
49
50void BugTest::cleanupTestCase()
51{
52}
53
54void BugTest::deleteSurrogates()
55{
56 // set up document and view and open test file
57 KTextEditor::DocumentPrivate doc;
58 KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
59 const QUrl url = QUrl::fromLocalFile(QLatin1String(TEST_DATA_DIR"bug205447.txt"));
60 doc.setEncoding(QStringLiteral("UTF-8"));
61 QVERIFY(doc.openUrl(url));
62
63 // test delete
64 // get UTF-32 representation of original line (before any deletes)
65 QVector<uint> line = doc.line(0).toUcs4();
66 QVERIFY(line.size() == 23);
67 // delete from start of line
68 view->setCursorPosition(Cursor(0, 0));
69 QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
70 for (int i = 0; i < line.size(); i++) {
71 // get the current line, after `i` delete presses, and convert it to utf32
72 // then ensure it's the expected substring of the original line
73 QVector<uint> current = doc.line(0).toUcs4();
74 QCOMPARE(current, line.mid(i));
75
76 // press the delete key and verify that the new text position isn't invalid
77 view->keyDelete();
78 QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
79 }
80 QCOMPARE(doc.lineLength(0), 0);
81}
82
83void BugTest::backspaceSurrogates()
84{
85 // set up document and view and open test file
86 KTextEditor::DocumentPrivate doc;
87 KTextEditor::ViewPrivate *view = static_cast<KTextEditor::ViewPrivate *>(doc.createView(nullptr));
88 const QUrl url = QUrl::fromLocalFile(QLatin1String(TEST_DATA_DIR"bug205447.txt"));
89 doc.setEncoding(QStringLiteral("UTF-8"));
90 QVERIFY(doc.openUrl(url));
91
92 // test backspace
93 // get UTF-32 representation of original line (before any backspaces)
94 QVector<uint> line = doc.line(0).toUcs4();
95 QVERIFY(line.size() == 23);
96 // backspace from end of line
97 view->setCursorPosition(Cursor(0, doc.line(0).size()));
98 QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
99 for (int i = 0; i < line.size(); i++) {
100 // get the current line, after `i` delete presses, and convert it to utf32
101 // then ensure it's the expected substring of the original line
102 QVector<uint> current = doc.line(0).toUcs4();
103 QCOMPARE(current, line.mid(0, line.size()-i));
104
105 // press the backspace key and verify that the new text position isn't invalid
106 view->backspace();
107 QVERIFY(DocumentCursor(&doc, view->cursorPosition()).isValidTextPosition());
108 }
109 QCOMPARE(doc.lineLength(0), 0);
110}
111
112#include "moc_bug205447.cpp"
113