1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
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.
16**
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.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtTest/QTest>
30
31#include <qtextdocument.h>
32#include <qdebug.h>
33#ifndef Q_OS_WIN
34# include <private/qtextdocument_p.h>
35#endif
36
37#include <qtextobject.h>
38#include <qtextcursor.h>
39
40QT_FORWARD_DECLARE_CLASS(QTextDocument)
41
42class tst_QTextBlock : public QObject
43{
44 Q_OBJECT
45
46private slots:
47 void init();
48 void cleanup();
49 void fragmentOverBlockBoundaries();
50 void excludeParagraphSeparatorFragment();
51 void backwardsBlockIterator();
52 void previousBlock_qtbug18026();
53 void removedBlock_qtbug18500();
54
55private:
56 QTextDocument *doc;
57 QTextCursor cursor;
58};
59
60void tst_QTextBlock::init()
61{
62 doc = new QTextDocument;
63 cursor = QTextCursor(doc);
64}
65
66void tst_QTextBlock::cleanup()
67{
68 cursor = QTextCursor();
69 delete doc;
70 doc = 0;
71}
72
73void tst_QTextBlock::fragmentOverBlockBoundaries()
74{
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)
80 */
81 cursor.insertText(text: "Hello");
82 cursor.insertBlock();
83 cursor.insertText(text: "World");
84
85 cursor.movePosition(op: QTextCursor::Start);
86
87 const QTextDocument *doc = cursor.block().document();
88 QVERIFY(doc);
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);
93#endif
94 QCOMPARE(cursor.block().text(), QString("Hello"));
95 cursor.movePosition(op: QTextCursor::NextBlock);
96 QCOMPARE(cursor.block().text(), QString("World"));
97}
98
99void tst_QTextBlock::excludeParagraphSeparatorFragment()
100{
101 QTextCharFormat fmt;
102 fmt.setForeground(Qt::blue);
103 cursor.insertText(text: "Hello", format: fmt);
104
105 QTextBlock block = doc->begin();
106 QVERIFY(block.isValid());
107
108 QTextBlock::Iterator it = block.begin();
109
110 QTextFragment fragment = it.fragment();
111 QVERIFY(fragment.isValid());
112 QCOMPARE(fragment.text(), QString("Hello"));
113
114 ++it;
115 QVERIFY(it.atEnd());
116 QCOMPARE(it, block.end());
117}
118
119void tst_QTextBlock::backwardsBlockIterator()
120{
121 QTextCharFormat fmt;
122
123 fmt.setForeground(Qt::magenta);
124 cursor.insertText(text: "A", format: fmt);
125
126 fmt.setForeground(Qt::red);
127 cursor.insertText(text: "A", format: fmt);
128
129 fmt.setForeground(Qt::magenta);
130 cursor.insertText(text: "A", format: fmt);
131
132 QTextBlock block = doc->begin();
133 QVERIFY(block.isValid());
134
135 QTextBlock::Iterator it = block.begin();
136 QCOMPARE(it.fragment().position(), 0);
137 ++it;
138 QCOMPARE(it.fragment().position(), 1);
139 ++it;
140
141 QCOMPARE(it.fragment().position(), 2);
142
143 --it;
144 QCOMPARE(it.fragment().position(), 1);
145 --it;
146 QCOMPARE(it.fragment().position(), 0);
147}
148
149void tst_QTextBlock::previousBlock_qtbug18026()
150{
151 QTextBlock last = doc->end().previous();
152 QVERIFY(last.isValid());
153}
154
155void tst_QTextBlock::removedBlock_qtbug18500()
156{
157 cursor.insertText(text: "line 1\nline 2\nline 3 \nline 4\n");
158 cursor.setPosition(pos: 7);
159 QTextBlock block = cursor.block();
160 cursor.setPosition(pos: 21, mode: QTextCursor::KeepAnchor);
161
162 cursor.removeSelectedText();
163 QVERIFY(!block.isValid());
164}
165
166QTEST_MAIN(tst_QTextBlock)
167#include "tst_qtextblock.moc"
168

source code of qtbase/tests/auto/gui/text/qtextblock/tst_qtextblock.cpp