File: TestChangeListCommand.cpp

package info (click to toggle)
koffice 1%3A2.2.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 157,656 kB
  • ctags: 110,762
  • sloc: cpp: 889,358; xml: 22,758; ansic: 6,533; python: 5,224; perl: 2,771; sh: 1,805; yacc: 1,304; ruby: 1,219; sql: 720; lex: 455; makefile: 76
file content (290 lines) | stat: -rw-r--r-- 9,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "TestChangeListCommand.h"
#include "tests/MockShapes.h"
#include "../commands/ChangeListCommand.h"

#include <KoListStyle.h>
#include <KoListLevelProperties.h>
#include <KoStyleManager.h>
#include <KoTextDocument.h>

#include <TextTool.h>
#include <KoCanvasBase.h>

#include <QTextDocument>
#include <QTextCursor>
#include <QTextList>


class MockTextTool : public TextTool
{
public:
    MockTextTool(MockCanvas *canvas)
        : TextTool(canvas)
    {
    }
};

void TestChangeListCommand::addList()
{
    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");

    QTextBlock block = doc.begin().next();
    cursor.setPosition(block.position());
    ChangeListCommand clc(cursor, KoListStyle::DecimalItem);
    clc.setTool(tool);
    clc.redo();

    block = doc.begin();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList()); // this one we just changed
    QTextList *tl = block.textList();
    block = block.next();
    QVERIFY(block.textList() == 0);

    QTextListFormat format = tl->format();
    QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DecimalItem);

    cursor.setPosition(block.position());
    ChangeListCommand clc2(cursor, KoListStyle::DiscItem);
    clc2.setTool(tool);
    clc2.redo();

    block = doc.begin();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QCOMPARE(block.textList(), tl);
    block = block.next();
    QVERIFY(block.textList());
    QVERIFY(block.textList() != tl);
}

void TestChangeListCommand::addListWithLevel2()
{
    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");

    QTextBlock block = doc.begin().next();
    cursor.setPosition(block.position());

    KoListStyle style;
    KoListLevelProperties llp;
    llp.setLevel(2);
    llp.setDisplayLevel(2);
    llp.setStyle(KoListStyle::DiscItem);
    style.setLevelProperties(llp);

    ChangeListCommand clc(cursor, &style, 2);
    clc.setTool(tool);
    clc.redo();

    block = doc.begin();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList()); // this one we just changed
    QTextList *tl = block.textList();
    block = block.next();
    QVERIFY(block.textList() == 0);

    QTextListFormat format = tl->format();
    QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DiscItem);
    QCOMPARE(format.intProperty(KoListStyle::DisplayLevel), (int) 2);
    QCOMPARE(format.intProperty(KoListStyle::Level), (int) 2);
}

void TestChangeListCommand::removeList()
{
    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
    KoListStyle style;
    QTextBlock block = doc.begin().next();
    while (block.isValid()) {
        style.applyStyle(block);
        block = block.next();
    }

    block = doc.begin().next();
    QVERIFY(block.textList()); // init, we should not have to test KoListStyle here ;)

    cursor.setPosition(block.position());
    ChangeListCommand clc(cursor, KoListStyle::None);
    clc.setTool(tool);
    clc.redo();

    block = doc.begin();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList());
    block = block.next();
    QVERIFY(block.textList());
    block = block.next();
    QVERIFY(block.textList());

    cursor.setPosition(block.position());
    ChangeListCommand clc2(cursor, KoListStyle::None);
    clc2.setTool(tool);
    clc2.redo();
    block = doc.begin();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList() == 0);
    block = block.next();
    QVERIFY(block.textList());
    block = block.next();
    QVERIFY(block.textList());
    block = block.next();
    QVERIFY(block.textList() == 0);
}

void TestChangeListCommand::joinList()
{
    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
    KoListStyle style;
    KoListLevelProperties llp;
    llp.setLevel(1);
    llp.setStyle(KoListStyle::DiscItem);
    style.setLevelProperties(llp);
    QTextBlock block = doc.begin().next();
    style.applyStyle(block);
    block = block.next();
    block = block.next(); // skip parag2
    style.applyStyle(block);
    block = block.next();
    style.applyStyle(block);

    block = doc.begin().next();
    QTextList *tl = block.textList();
    QVERIFY(tl); // init, we should not have to test KoListStyle here ;)
    block = block.next(); // parag2
    QVERIFY(block.textList() == 0);

    cursor.setPosition(block.position());
    ChangeListCommand clc(cursor, KoListStyle::DiscItem);
    clc.setTool(tool);
    clc.redo();
    QCOMPARE(block.textList(), tl);
}

void TestChangeListCommand::joinList2()
{
    // test usecase of joining with the one before and the one after based on similar styles.
    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4");
    KoListStyle style;
    KoListLevelProperties llp1;
    llp1.setLevel(1);
    llp1.setStyle(KoListStyle::DiscItem);
    style.setLevelProperties(llp1);
    QTextBlock block = doc.begin().next().next();
    style.applyStyle(block); // apply on parag2

    KoListStyle style2;
    KoListLevelProperties llp;
    llp.setLevel(1);
    llp.setStyle(KoListStyle::DecimalItem);
    llp.setListItemSuffix(".");
    style2.setLevelProperties(llp);
    block = block.next().next(); // parag4
    style2.applyStyle(block);

    // now apply the default 'DiscItem' on 'parag1' expecting it to join with the list already set on 'parag2'
    block = doc.begin().next();
    cursor.setPosition(block.position());
    ChangeListCommand clc(cursor, KoListStyle::DiscItem);
    clc.setTool(tool);
    clc.redo();
    QTextList *tl = block.textList();
    QVERIFY(tl);
    block = block.next();
    QCOMPARE(tl, block.textList());
    QCOMPARE(tl->format().intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DiscItem);

    // now apply the 'DecimalItem' on 'parag3' and expect it to join with the list already set on 'parag4'
    block = doc.end().previous(); // parag4
    QCOMPARE(block.text(), QString("parag4"));
    QTextList *numberedList = block.textList();
    QVERIFY(numberedList);
    block = block.previous(); // parag3
    QVERIFY(block.textList() == 0);
    cursor.setPosition(block.position());
    ChangeListCommand clc2(cursor, KoListStyle::DecimalItem);
    clc2.setTool(tool);
    clc2.redo();
    QVERIFY(block.textList());
    QVERIFY(block.textList() != tl);
    QVERIFY(block.textList() == numberedList);
    QCOMPARE(numberedList->format().intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DecimalItem);
}

void TestChangeListCommand::splitList()
{
    // assume I start with;
    // 1 paragA
    // 1.1 paragB
    // 1.2 paragC
    // now I change parag 'B' to '1.a'  then C should have 1.1 as a numbering. I.e. we should split an existing list.

    QTextDocument doc;
    KoTextDocument(&doc).setStyleManager(new KoStyleManager);
    MockTextTool *tool = new MockTextTool(new MockCanvas);
    QTextCursor cursor(&doc);
    cursor.insertText("Root\nparagA\nparagB\nparagC");
    QTextBlock block = doc.begin().next();
    KoListStyle style;
    style.applyStyle(block); // apply on parag2

    KoListStyle style2;
    KoListLevelProperties llp = style2.levelProperties(2);
    style2.setLevelProperties(llp);
    block = block.next();
    style2.applyStyle(block);
    block = block.next();
    style2.applyStyle(block);

    QTextBlock paragA = doc.begin().next();
    QVERIFY(paragA.textList());
    QTextBlock paragB = paragA.next();
    QVERIFY(paragB.textList());
    QVERIFY(paragB.textList() != paragA.textList());
    QTextBlock paragC = paragB.next();
    QVERIFY(paragC.textList());
    QCOMPARE(paragC.textList(), paragB.textList());

    QTextList *tl = paragB.textList();
    cursor.setPosition(paragB.position());
    ChangeListCommand clc(cursor, KoListStyle::AlphaLowerItem);
    clc.setTool(tool);
    clc.redo();

    QVERIFY(doc.begin().textList() == 0);
    QVERIFY(paragA.textList());
    QTextList *newTextList = paragB.textList();
    QVERIFY(newTextList == tl);
    QCOMPARE(paragC.textList(), tl);

    QCOMPARE(tl->format().intProperty(KoListStyle::Level), 2);
    QCOMPARE(newTextList->format().intProperty(KoListStyle::Level), 2);
}

QTEST_MAIN(TestChangeListCommand)

#include <TestChangeListCommand.moc>