1 /* This file is part of the KDE project
2    Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
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 #include "TestPointRemoveCommand.h"
20 
21 #include <QPainterPath>
22 #include "KoPathShape.h"
23 #include "KoPathPointRemoveCommand.h"
24 #include "KoShapeController.h"
25 #include <MockShapes.h>
26 #include <sdk/tests/kistest.h>
27 #include <QTest>
28 
redoUndoPointRemove()29 void TestPointRemoveCommand::redoUndoPointRemove()
30 {
31     KoPathShape path1;
32     path1.moveTo(QPointF(0, 0));
33     path1.lineTo(QPointF(0, 100));
34     KoPathPoint *point1 = path1.curveTo(QPointF(0, 50), QPointF(100, 50), QPointF(100, 100));
35     KoPathPoint *point2 = path1.lineTo(QPointF(200, 100));
36     path1.curveTo(QPointF(200, 50), QPointF(300, 50), QPointF(300, 100));
37 
38     QPainterPath orig1(QPointF(0, 0));
39     orig1.lineTo(0, 100);
40     orig1.cubicTo(0, 50, 100, 50, 100, 100);
41     orig1.lineTo(200, 100);
42     orig1.cubicTo(200, 50, 300, 50, 300, 100);
43 
44     QVERIFY(orig1 == path1.outline());
45 
46     KoPathShape path2;
47     path2.moveTo(QPointF(0, 0));
48     KoPathPoint *point3 = path2.curveTo(QPointF(50, 0), QPointF(100, 50), QPointF(100, 100));
49     path2.curveTo(QPointF(50, 100), QPointF(0, 50), QPointF(0, 0));
50     path2.closeMerge();
51 
52     QList<KoPathPointData> pd;
53     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point1)));
54     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point2)));
55     pd.append(KoPathPointData(&path2, path2.pathPointIndex(point3)));
56 
57     QPainterPath ppath1Org = path1.outline();
58     QPainterPath ppath2Org = path2.outline();
59 
60     MockShapeController mockController;
61     KoShapeController shapeController(0, &mockController);
62 
63     KUndo2Command *cmd = KoPathPointRemoveCommand::createCommand(pd, &shapeController);
64     cmd->redo();
65 
66     QPainterPath ppath1(QPointF(0, 0));
67     ppath1.lineTo(0, 100);
68     ppath1.cubicTo(0, 50, 300, 50, 300, 100);
69 
70     QPainterPath ppath2(QPointF(0, 0));
71     ppath2.cubicTo(50, 0, 0, 50, 0, 0);
72     ppath2.closeSubpath();
73 
74     QVERIFY(ppath1 == path1.outline());
75     QVERIFY(ppath2 == path2.outline());
76 
77     cmd->undo();
78 
79     QVERIFY(ppath1Org == path1.outline());
80     QVERIFY(ppath2Org == path2.outline());
81 
82     delete cmd;
83 }
84 
redoUndoSubpathRemove()85 void TestPointRemoveCommand::redoUndoSubpathRemove()
86 {
87     KoPathShape path1;
88     KoPathPoint *point11 = path1.moveTo(QPointF(0, 0));
89     KoPathPoint *point12 = path1.lineTo(QPointF(0, 100));
90     KoPathPoint *point13 = path1.curveTo(QPointF(0, 50), QPointF(100, 50), QPointF(100, 100));
91     KoPathPoint *point14 = path1.lineTo(QPointF(200, 100));
92     KoPathPoint *point15 = path1.curveTo(QPointF(200, 50), QPointF(300, 50), QPointF(300, 100));
93     KoPathPoint *point21 = path1.moveTo(QPointF(0, 0));
94     KoPathPoint *point22 = path1.curveTo(QPointF(50, 0), QPointF(100, 50), QPointF(100, 100));
95     path1.curveTo(QPointF(50, 100), QPointF(0, 50), QPointF(0, 0));
96     path1.closeMerge();
97     path1.moveTo(QPointF(100, 0));
98     path1.lineTo(QPointF(100, 100));
99 
100     QList<KoPathPointData> pd;
101     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point11)));
102     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point12)));
103     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point13)));
104     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point14)));
105     pd.append(KoPathPointData(&path1, path1.pathPointIndex(point15)));
106 
107     QPainterPath ppath1Org = path1.outline();
108 
109     MockShapeController mockController;
110     KoShapeController shapeController(0, &mockController);
111 
112     KUndo2Command *cmd1 = KoPathPointRemoveCommand::createCommand(pd, &shapeController);
113     cmd1->redo();
114 
115     QPainterPath ppath1(QPointF(0, 0));
116     ppath1.cubicTo(50, 0, 100, 50, 100, 100);
117     ppath1.cubicTo(50, 100, 0, 50, 0, 0);
118     ppath1.closeSubpath();
119     ppath1.moveTo(100, 0);
120     ppath1.lineTo(100, 100);
121 
122     QPainterPath ppath1mod = path1.outline();
123     QVERIFY(ppath1 == ppath1mod);
124 
125     QList<KoPathPointData> pd2;
126     pd2.append(KoPathPointData(&path1, path1.pathPointIndex(point21)));
127     pd2.append(KoPathPointData(&path1, path1.pathPointIndex(point22)));
128 
129     KUndo2Command *cmd2 = KoPathPointRemoveCommand::createCommand(pd2, &shapeController);
130     cmd2->redo();
131 
132     QPainterPath ppath2(QPointF(0, 0));
133     ppath2.lineTo(0, 100);
134 
135     QVERIFY(ppath2 == path1.outline());
136 
137     cmd2->undo();
138 
139     QVERIFY(ppath1mod == path1.outline());
140 
141     cmd1->undo();
142 
143     QVERIFY(ppath1Org == path1.outline());
144 
145     delete cmd2;
146     delete cmd1;
147 }
148 
redoUndoShapeRemove()149 void TestPointRemoveCommand::redoUndoShapeRemove()
150 {
151     KoPathShape *path1 = new KoPathShape();
152     KoPathPoint *point11 = path1->moveTo(QPointF(0, 0));
153     KoPathPoint *point12 = path1->lineTo(QPointF(0, 100));
154     KoPathPoint *point13 = path1->curveTo(QPointF(0, 50), QPointF(100, 50), QPointF(100, 100));
155     KoPathPoint *point14 = path1->lineTo(QPointF(200, 100));
156     KoPathPoint *point15 = path1->curveTo(QPointF(200, 50), QPointF(300, 50), QPointF(300, 100));
157     KoPathShape *path2 = new KoPathShape();
158     KoPathPoint *point21 = path2->moveTo(QPointF(0, 0));
159     KoPathPoint *point22 = path2->curveTo(QPointF(50, 0), QPointF(100, 50), QPointF(100, 100));
160     path2->curveTo(QPointF(50, 100), QPointF(0, 50), QPointF(0, 0));
161     path2->closeMerge();
162 
163     QList<KoPathPointData> pd;
164     pd.append(KoPathPointData(path1, path1->pathPointIndex(point12)));
165     pd.append(KoPathPointData(path1, path1->pathPointIndex(point11)));
166     pd.append(KoPathPointData(path1, path1->pathPointIndex(point13)));
167     pd.append(KoPathPointData(path1, path1->pathPointIndex(point15)));
168     pd.append(KoPathPointData(path1, path1->pathPointIndex(point14)));
169     pd.append(KoPathPointData(path2, path2->pathPointIndex(point22)));
170     pd.append(KoPathPointData(path2, path2->pathPointIndex(point21)));
171 
172     QPainterPath ppath1Org = path1->outline();
173     QPainterPath ppath2Org = path2->outline();
174 
175     MockShapeController mockController;
176     KoShapeController shapeController(0, &mockController);
177     QScopedPointer<MockContainer> rootContainer(new MockContainer());
178     rootContainer->addShape(path1);
179     rootContainer->addShape(path2);
180 
181     KUndo2Command *cmd = KoPathPointRemoveCommand::createCommand(pd, &shapeController);
182     cmd->redo();
183     QVERIFY(!rootContainer->contains(path1));
184     QVERIFY(!rootContainer->contains(path2));
185     cmd->undo();
186 
187     QVERIFY(rootContainer->contains(path1));
188     QVERIFY(rootContainer->contains(path2));
189 
190     QVERIFY(ppath1Org == path1->outline());
191     QVERIFY(ppath2Org == path2->outline());
192 
193     delete cmd;
194     rootContainer.reset();
195 }
196 
redoUndo()197 void TestPointRemoveCommand::redoUndo()
198 {
199     KoPathShape *path1 = new KoPathShape();
200     KoPathPoint *point11 = path1->moveTo(QPointF(0, 0));
201     KoPathPoint *point12 = path1->lineTo(QPointF(0, 100));
202     KoPathPoint *point13 = path1->curveTo(QPointF(0, 50), QPointF(100, 50), QPointF(100, 100));
203     KoPathPoint *point14 = path1->lineTo(QPointF(200, 100));
204     KoPathPoint *point15 = path1->curveTo(QPointF(200, 50), QPointF(300, 50), QPointF(300, 100));
205     KoPathPoint *point16 = path1->moveTo(QPointF(100, 0));
206     KoPathPoint *point17 = path1->curveTo(QPointF(150, 0), QPointF(200, 50), QPointF(200, 100));
207     path1->curveTo(QPointF(150, 100), QPointF(100, 50), QPointF(100, 0));
208     path1->closeMerge();
209     KoPathPoint *point18 = path1->moveTo(QPointF(200, 0));
210     KoPathPoint *point19 = path1->lineTo(QPointF(200, 100));
211 
212     KoPathShape *path2 = new KoPathShape();
213     KoPathPoint *point21 = path2->moveTo(QPointF(0, 0));
214     KoPathPoint *point22 = path2->curveTo(QPointF(50, 0), QPointF(100, 50), QPointF(100, 100));
215     path2->curveTo(QPointF(50, 100), QPointF(0, 50), QPointF(0, 0));
216     path2->closeMerge();
217 
218     KoPathShape *path3 = new KoPathShape();
219     KoPathPoint *point31 = path3->moveTo(QPointF(0, 0));
220     KoPathPoint *point32 = path3->lineTo(QPointF(100, 100));
221     KoPathPoint *point33 = path3->lineTo(QPointF(200, 150));
222 
223     MockShapeController mockController;
224     QScopedPointer<MockContainer> rootContainer(new MockContainer());
225     rootContainer->addShape(path1);
226     rootContainer->addShape(path2);
227     rootContainer->addShape(path3);
228 
229     QList<KoPathPointData> pd;
230     pd.append(KoPathPointData(path2, path2->pathPointIndex(point21)));
231     pd.append(KoPathPointData(path1, path1->pathPointIndex(point13)));
232     pd.append(KoPathPointData(path1, path1->pathPointIndex(point11)));
233     pd.append(KoPathPointData(path3, path3->pathPointIndex(point31)));
234     pd.append(KoPathPointData(path1, path1->pathPointIndex(point12)));
235     pd.append(KoPathPointData(path1, path1->pathPointIndex(point15)));
236     pd.append(KoPathPointData(path2, path2->pathPointIndex(point22)));
237     pd.append(KoPathPointData(path1, path1->pathPointIndex(point14)));
238 
239     KoShapeController shapeController(0, &mockController);
240 
241     QPainterPath ppath1Org = path1->outline();
242     QPainterPath ppath2Org = path2->outline();
243     QPainterPath ppath3Org = path3->outline();
244 
245     KUndo2Command *cmd1 = KoPathPointRemoveCommand::createCommand(pd, &shapeController);
246     cmd1->redo();
247 
248     QVERIFY(rootContainer->contains(path1));
249     QVERIFY(!rootContainer->contains(path2));
250     QVERIFY(rootContainer->contains(path3));
251 
252     QPainterPath ppath1(QPointF(0, 0));
253     ppath1.cubicTo(50, 0, 100, 50, 100, 100);
254     ppath1.cubicTo(50, 100, 0, 50, 0, 0);
255     ppath1.closeSubpath();
256     ppath1.moveTo(100, 0);
257     ppath1.lineTo(100, 100);
258 
259     QPainterPath ppath1mod = path1->outline();
260     QVERIFY(ppath1 == ppath1mod);
261 
262     QPainterPath ppath3(QPointF(0, 0));
263     ppath3.lineTo(100, 50);
264 
265     QPainterPath ppath3mod = path3->outline();
266     QVERIFY(ppath3 == ppath3mod);
267 
268     QList<KoPathPointData> pd2;
269     pd2.append(KoPathPointData(path1, path1->pathPointIndex(point16)));
270     pd2.append(KoPathPointData(path1, path1->pathPointIndex(point17)));
271     pd2.append(KoPathPointData(path1, path1->pathPointIndex(point18)));
272     pd2.append(KoPathPointData(path1, path1->pathPointIndex(point19)));
273     pd2.append(KoPathPointData(path3, path3->pathPointIndex(point32)));
274     pd2.append(KoPathPointData(path3, path3->pathPointIndex(point33)));
275 
276     KUndo2Command *cmd2 = KoPathPointRemoveCommand::createCommand(pd2, &shapeController);
277     cmd2->redo();
278 
279     QVERIFY(!rootContainer->contains(path1));
280     QVERIFY(!rootContainer->contains(path2));
281     QVERIFY(!rootContainer->contains(path3));
282 
283     cmd2->undo();
284 
285     QVERIFY(rootContainer->contains(path1));
286     QVERIFY(!rootContainer->contains(path2));
287     QVERIFY(rootContainer->contains(path3));
288     QVERIFY(ppath1 == ppath1mod);
289     QVERIFY(ppath3 == ppath3mod);
290 
291     cmd1->undo();
292 
293     QVERIFY(ppath1Org == path1->outline());
294     QVERIFY(ppath2Org == path2->outline());
295     QVERIFY(ppath3Org == path3->outline());
296 
297     cmd1->redo();
298     cmd2->redo();
299 
300     delete cmd2;
301     delete cmd1;
302 }
303 
304 KISTEST_MAIN(TestPointRemoveCommand)
305