1
/****************************************************************************
3
** Copyright (C) 2016 The Qt Company Ltd.
4
** Contact: https://www.qt.io/licensing/
6
** This file is part of the test suite of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and The Qt Company. For licensing terms
14
** and conditions see https://www.qt.io/terms-conditions. For further
15
** information use the contact form at https://www.qt.io/contact-us.
17
** GNU General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU
19
** General Public License version 3 as published by the Free Software
20
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21
** included in the packaging of this file. Please review the following
22
** information to ensure the GNU General Public License requirements will
23
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
27
****************************************************************************/
29
#include <QtTest/QTest>
31
#include <qtextdocument.h>
34
# include <private/qtextdocument_p.h>
37
#include <qtextobject.h>
38
#include <qtextcursor.h>
40
QT_FORWARD_DECLARE_CLASS(QTextDocument)
42
class tst_QTextBlock : public QObject
49
void fragmentOverBlockBoundaries();
50
void excludeParagraphSeparatorFragment();
51
void backwardsBlockIterator();
52
void previousBlock_qtbug18026();
53
void removedBlock_qtbug18500();
60
void tst_QTextBlock::init()
62
doc = new QTextDocument;
63
cursor = QTextCursor(doc);
66
void tst_QTextBlock::cleanup()
68
cursor = QTextCursor();
73
void tst_QTextBlock::fragmentOverBlockBoundaries()
75
/* this creates two fragments in the piecetable:
76
* 1) 'hello<parag separator here>world'
77
* 2) '<parag separator>'
78
* (they are not united because the former was interested after the latter,
79
* hence their position in the pt buffer is the other way around)
81
cursor.insertText("Hello");
83
cursor.insertText("World");
85
cursor.movePosition(QTextCursor::Start);
87
const QTextDocument *doc = cursor.block().document();
89
// Block separators are always a fragment of their self. Thus:
90
// |Hello|\b|World|\b|
91
#if !defined(Q_OS_WIN)
92
QCOMPARE(doc->docHandle()->fragmentMap().numNodes(), 4);
94
QCOMPARE(cursor.block().text(), QString("Hello"));
95
cursor.movePosition(QTextCursor::NextBlock);
96
QCOMPARE(cursor.block().text(), QString("World"));
99
void tst_QTextBlock::excludeParagraphSeparatorFragment()
102
fmt.setForeground(Qt::blue);
103
cursor.insertText("Hello", fmt);
105
QTextBlock block = doc->begin();
106
QVERIFY(block.isValid());
108
QTextBlock::Iterator it = block.begin();
110
QTextFragment fragment = it.fragment();
111
QVERIFY(fragment.isValid());
112
QCOMPARE(fragment.text(), QString("Hello"));
116
QCOMPARE(it, block.end());
119
void tst_QTextBlock::backwardsBlockIterator()
123
fmt.setForeground(Qt::magenta);
124
cursor.insertText("A", fmt);
126
fmt.setForeground(Qt::red);
127
cursor.insertText("A", fmt);
129
fmt.setForeground(Qt::magenta);
130
cursor.insertText("A", fmt);
132
QTextBlock block = doc->begin();
133
QVERIFY(block.isValid());
135
QTextBlock::Iterator it = block.begin();
136
QCOMPARE(it.fragment().position(), 0);
138
QCOMPARE(it.fragment().position(), 1);
141
QCOMPARE(it.fragment().position(), 2);
144
QCOMPARE(it.fragment().position(), 1);
146
QCOMPARE(it.fragment().position(), 0);
149
void tst_QTextBlock::previousBlock_qtbug18026()
151
QTextBlock last = doc->end().previous();
152
QVERIFY(last.isValid());
155
void tst_QTextBlock::removedBlock_qtbug18500()
157
cursor.insertText("line 1\nline 2\nline 3 \nline 4\n");
158
cursor.setPosition(7);
159
QTextBlock block = cursor.block();
160
cursor.setPosition(21, QTextCursor::KeepAnchor);
162
cursor.removeSelectedText();
163
QVERIFY(!block.isValid());
166
QTEST_MAIN(tst_QTextBlock)
167
#include "tst_qtextblock.moc"