1 #include "TestChangeListCommand.h"
2 #include "tests/MockShapes.h"
3 #include "../commands/ChangeListCommand.h"
4
5 #include <KoListStyle.h>
6 #include <KoListLevelProperties.h>
7 #include <KoStyleManager.h>
8 #include <KoTextDocument.h>
9
10 #include <TextTool.h>
11 #include <KoCanvasBase.h>
12
13 #include <QTextDocument>
14 #include <QTextCursor>
15 #include <QTextList>
16
17
addList()18 void TestChangeListCommand::addList()
19 {
20 QTextDocument doc;
21 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
22 TextTool *tool = new TextTool(new MockCanvas);
23 QTextCursor cursor(&doc);
24 cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
25
26 QTextBlock block = doc.begin().next();
27 cursor.setPosition(block.position());
28 ChangeListCommand clc(cursor, KoListStyle::DecimalItem);
29 clc.setTool(tool);
30 clc.redo();
31
32 block = doc.begin();
33 QVERIFY(block.textList() == 0);
34 block = block.next();
35 QVERIFY(block.textList()); // this one we just changed
36 QTextList *tl = block.textList();
37 block = block.next();
38 QVERIFY(block.textList() == 0);
39
40 QTextListFormat format = tl->format();
41 QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DecimalItem);
42
43 cursor.setPosition(block.position());
44 ChangeListCommand clc2(cursor, KoListStyle::Bullet);
45 clc2.setTool(tool);
46 clc2.redo();
47
48 block = doc.begin();
49 QVERIFY(block.textList() == 0);
50 block = block.next();
51 QCOMPARE(block.textList(), tl);
52 block = block.next();
53 QVERIFY(block.textList());
54 QVERIFY(block.textList() != tl);
55 }
56
addListWithLevel2()57 void TestChangeListCommand::addListWithLevel2()
58 {
59 QTextDocument doc;
60 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
61 TextTool *tool = new TextTool(new MockCanvas);
62 QTextCursor cursor(&doc);
63 cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
64
65 QTextBlock block = doc.begin().next();
66 cursor.setPosition(block.position());
67
68 KoListStyle style;
69 KoListLevelProperties llp;
70 llp.setLevel(2);
71 llp.setDisplayLevel(2);
72 llp.setStyle(KoListStyle::Bullet);
73 style.setLevelProperties(llp);
74
75 ChangeListCommand clc(cursor, &style, 2);
76 clc.setTool(tool);
77 clc.redo();
78
79 block = doc.begin();
80 QVERIFY(block.textList() == 0);
81 block = block.next();
82 QVERIFY(block.textList()); // this one we just changed
83 QTextList *tl = block.textList();
84 block = block.next();
85 QVERIFY(block.textList() == 0);
86
87 QTextListFormat format = tl->format();
88 QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int) KoListStyle::Bullet);
89 QCOMPARE(format.intProperty(KoListStyle::DisplayLevel), (int) 2);
90 QCOMPARE(format.intProperty(KoListStyle::Level), (int) 2);
91 }
92
removeList()93 void TestChangeListCommand::removeList()
94 {
95 QTextDocument doc;
96 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
97 TextTool *tool = new TextTool(new MockCanvas);
98 QTextCursor cursor(&doc);
99 cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
100 KoListStyle style;
101 QTextBlock block = doc.begin().next();
102 while (block.isValid()) {
103 style.applyStyle(block);
104 block = block.next();
105 }
106
107 block = doc.begin().next();
108 QVERIFY(block.textList()); // init, we should not have to test KoListStyle here ;)
109
110 cursor.setPosition(block.position());
111 ChangeListCommand clc(cursor, KoListStyle::None);
112 clc.setTool(tool);
113 clc.redo();
114
115 block = doc.begin();
116 QVERIFY(block.textList() == 0);
117 block = block.next();
118 QVERIFY(block.textList() == 0);
119 block = block.next();
120 QVERIFY(block.textList());
121 block = block.next();
122 QVERIFY(block.textList());
123 block = block.next();
124 QVERIFY(block.textList());
125
126 cursor.setPosition(block.position());
127 ChangeListCommand clc2(cursor, KoListStyle::None);
128 clc2.setTool(tool);
129 clc2.redo();
130 block = doc.begin();
131 QVERIFY(block.textList() == 0);
132 block = block.next();
133 QVERIFY(block.textList() == 0);
134 block = block.next();
135 QVERIFY(block.textList());
136 block = block.next();
137 QVERIFY(block.textList());
138 block = block.next();
139 QVERIFY(block.textList() == 0);
140 }
141
joinList()142 void TestChangeListCommand::joinList()
143 {
144 QTextDocument doc;
145 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
146 TextTool *tool = new TextTool(new MockCanvas);
147 QTextCursor cursor(&doc);
148 cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4\n");
149 KoListStyle style;
150 KoListLevelProperties llp;
151 llp.setLevel(1);
152 llp.setStyle(KoListStyle::Bullet);
153 style.setLevelProperties(llp);
154 QTextBlock block = doc.begin().next();
155 style.applyStyle(block);
156 block = block.next();
157 block = block.next(); // skip parag2
158 style.applyStyle(block);
159 block = block.next();
160 style.applyStyle(block);
161
162 block = doc.begin().next();
163 QTextList *tl = block.textList();
164 QVERIFY(tl); // init, we should not have to test KoListStyle here ;)
165 block = block.next(); // parag2
166 QVERIFY(block.textList() == 0);
167
168 cursor.setPosition(block.position());
169 ChangeListCommand clc(cursor, KoListStyle::Bullet);
170 clc.setTool(tool);
171 clc.redo();
172 QCOMPARE(block.textList(), tl);
173 }
174
joinList2()175 void TestChangeListCommand::joinList2()
176 {
177 // test usecase of joining with the one before and the one after based on similar styles.
178 QTextDocument doc;
179 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
180 TextTool *tool = new TextTool(new MockCanvas);
181 QTextCursor cursor(&doc);
182 cursor.insertText("Root\nparag1\nparag2\nparag3\nparag4");
183 KoListStyle style;
184 KoListLevelProperties llp1;
185 llp1.setLevel(1);
186 llp1.setStyle(KoListStyle::Bullet);
187 style.setLevelProperties(llp1);
188 QTextBlock block = doc.begin().next().next();
189 style.applyStyle(block); // apply on parag2
190
191 KoListStyle style2;
192 KoListLevelProperties llp;
193 llp.setLevel(1);
194 llp.setStyle(KoListStyle::DecimalItem);
195 llp.setListItemSuffix(".");
196 style2.setLevelProperties(llp);
197 block = block.next().next(); // parag4
198 style2.applyStyle(block);
199
200 // now apply the default 'Bullet' on 'parag1' expecting it to join with the list already set on 'parag2'
201 block = doc.begin().next();
202 cursor.setPosition(block.position());
203 ChangeListCommand clc(cursor, KoListStyle::Bullet);
204 clc.setTool(tool);
205 clc.redo();
206 QTextList *tl = block.textList();
207 QVERIFY(tl);
208 block = block.next();
209 QCOMPARE(tl, block.textList());
210 QCOMPARE(tl->format().intProperty(QTextListFormat::ListStyle), (int) KoListStyle::Bullet);
211
212 // now apply the 'DecimalItem' on 'parag3' and expect it to join with the list already set on 'parag4'
213 block = doc.lastBlock(); // parag4
214 QCOMPARE(block.text(), QString("parag4"));
215 QTextList *numberedList = block.textList();
216 QVERIFY(numberedList);
217 block = block.previous(); // parag3
218 QVERIFY(block.textList() == 0);
219 cursor.setPosition(block.position());
220 ChangeListCommand clc2(cursor, KoListStyle::DecimalItem);
221 clc2.setTool(tool);
222 clc2.redo();
223 QVERIFY(block.textList());
224 QVERIFY(block.textList() != tl);
225 QVERIFY(block.textList() == numberedList);
226 QCOMPARE(numberedList->format().intProperty(QTextListFormat::ListStyle), (int) KoListStyle::DecimalItem);
227 }
228
splitList()229 void TestChangeListCommand::splitList()
230 {
231 // assume I start with;
232 // 1 paragA
233 // 1.1 paragB
234 // 1.2 paragC
235 // 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.
236
237 QTextDocument doc;
238 KoTextDocument(&doc).setStyleManager(new KoStyleManager);
239 TextTool *tool = new TextTool(new MockCanvas);
240 QTextCursor cursor(&doc);
241 cursor.insertText("Root\nparagA\nparagB\nparagC");
242 QTextBlock block = doc.begin().next();
243 KoListStyle style;
244 style.applyStyle(block); // apply on parag2
245
246 KoListStyle style2;
247 KoListLevelProperties llp = style2.levelProperties(2);
248 style2.setLevelProperties(llp);
249 block = block.next();
250 style2.applyStyle(block);
251 block = block.next();
252 style2.applyStyle(block);
253
254 QTextBlock paragA = doc.begin().next();
255 QVERIFY(paragA.textList());
256 QTextBlock paragB = paragA.next();
257 QVERIFY(paragB.textList());
258 QVERIFY(paragB.textList() != paragA.textList());
259 QTextBlock paragC = paragB.next();
260 QVERIFY(paragC.textList());
261 QCOMPARE(paragC.textList(), paragB.textList());
262
263 QTextList *tl = paragB.textList();
264 cursor.setPosition(paragB.position());
265 ChangeListCommand clc(cursor, KoListStyle::AlphaLowerItem);
266 clc.setTool(tool);
267 clc.redo();
268
269 QVERIFY(doc.begin().textList() == 0);
270 QVERIFY(paragA.textList());
271 QTextList *newTextList = paragB.textList();
272 QVERIFY(newTextList == tl);
273 QCOMPARE(paragC.textList(), tl);
274
275 QCOMPARE(tl->format().intProperty(KoListStyle::Level), 2);
276 QCOMPARE(newTextList->format().intProperty(KoListStyle::Level), 2);
277 }
278
279 QTEST_MAIN(TestChangeListCommand)
280