1 /*
2  *  This file is part of Calligra tests
3  *
4  *  Copyright (C) 2018 Dag Andersen <danders@get2net.dk>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 #include "TestAllowedInteraction.h"
21 #include <MockShapes.h>
22 
23 #include <KoSelection.h>
24 #include <FlakeDebug.h>
25 
26 #include <QTest>
27 
testVisible()28 void TestAllowedInteraction::testVisible()
29 {
30     MockShape *shape = new MockShape();
31 
32     QVERIFY(shape->isVisible());
33     shape->setVisible(false);
34     QVERIFY(!shape->isVisible());
35     shape->setVisible(true);
36     QVERIFY(shape->isVisible());
37 
38     KoShapeContainer *parent = new MockContainer();
39     parent->addShape(shape);
40 
41     parent->setVisible(false);
42     QVERIFY(!shape->isVisible(true));
43     parent->setVisible(true);
44     QVERIFY(shape->isVisible(true));
45 }
46 
testSelectable()47 void TestAllowedInteraction::testSelectable()
48 {
49     MockShape *shape = new MockShape();
50 
51     QVERIFY(shape->isSelectable());
52     shape->setSelectable(false);
53     QVERIFY(!shape->isSelectable());
54     shape->setSelectable(true);
55     QVERIFY(shape->isSelectable());
56 
57     // visible does not affect selectable
58     shape->setVisible(false);
59     QVERIFY(shape->isSelectable());
60     shape->setVisible(true);
61     QVERIFY(shape->isSelectable());
62 
63     KoShapeContainer *parent = new MockContainer();
64     parent->addShape(shape);
65 
66     // parent does not affect selectable
67     parent->setSelectable(false);
68     QVERIFY(shape->isSelectable());
69     parent->setVisible(true);
70     QVERIFY(shape->isSelectable());
71 
72     parent->setVisible(false);
73     QVERIFY(shape->isSelectable());
74     parent->setVisible(true);
75     QVERIFY(shape->isSelectable());
76 }
77 
testDeletable()78 void TestAllowedInteraction::testDeletable()
79 {
80     MockShape *shape = new MockShape();
81 
82     QVERIFY(shape->isDeletable());
83     shape->setDeletable(false);
84     QVERIFY(!shape->isDeletable());
85     shape->setDeletable(true);
86     QVERIFY(shape->isDeletable());
87 
88     // visible does not affect isDeletable
89     shape->setVisible(false);
90     QVERIFY(shape->isDeletable());
91     shape->setVisible(true);
92     QVERIFY(shape->isDeletable());
93 
94     KoShapeContainer *parent = new MockContainer();
95     parent->addShape(shape);
96 
97     // parent does not affect isDeletable
98     parent->setDeletable(false);
99     QVERIFY(shape->isDeletable());
100     parent->setDeletable(true);
101     QVERIFY(shape->isDeletable());
102 
103     parent->setVisible(false);
104     QVERIFY(shape->isDeletable());
105     parent->setDeletable(true);
106     QVERIFY(shape->isSelectable());
107 }
108 
testGeometryProtected()109 void TestAllowedInteraction::testGeometryProtected()
110 {
111     MockShape *shape = new MockShape();
112 
113     QVERIFY(!shape->isGeometryProtected());
114     shape->setGeometryProtected(true);
115     QVERIFY(shape->isGeometryProtected());
116     shape->setGeometryProtected(false);
117     QVERIFY(!shape->isGeometryProtected());
118 
119     // visible does not affect isGeometryProtected
120     shape->setVisible(false);
121     QVERIFY(!shape->isGeometryProtected());
122     shape->setVisible(true);
123     QVERIFY(!shape->isGeometryProtected());
124 
125     KoShapeContainer *parent = new MockContainer();
126     parent->addShape(shape);
127 
128     // parent does not affect isGeometryProtected
129     parent->setGeometryProtected(false);
130     QVERIFY(!shape->isGeometryProtected());
131     parent->setGeometryProtected(true);
132     QVERIFY(!shape->isGeometryProtected());
133 
134     parent->setVisible(false);
135     QVERIFY(!shape->isGeometryProtected());
136     parent->setVisible(true);
137     QVERIFY(!shape->isGeometryProtected());
138 }
139 
testContentProtected()140 void TestAllowedInteraction::testContentProtected()
141 {
142     MockShape *shape = new MockShape();
143 
144     QVERIFY(!shape->isContentProtected());
145     shape->setContentProtected(true);
146     QVERIFY(shape->isContentProtected());
147     shape->setContentProtected(false);
148     QVERIFY(!shape->isContentProtected());
149 
150     // visible does not affect isContentProtected
151     shape->setVisible(false);
152     QVERIFY(!shape->isContentProtected());
153     shape->setVisible(true);
154     QVERIFY(!shape->isContentProtected());
155 
156     KoShapeContainer *parent = new MockContainer();
157     parent->addShape(shape);
158 
159     // parent does not affect isContentProtected
160     parent->setContentProtected(true);
161     QVERIFY(!shape->isContentProtected());
162     parent->setContentProtected(false);
163     QVERIFY(!shape->isContentProtected());
164 
165     parent->setVisible(false);
166     QVERIFY(!shape->isContentProtected());
167     parent->setVisible(true);
168     QVERIFY(!shape->isContentProtected());
169 }
170 
testEditable()171 void TestAllowedInteraction::testEditable()
172 {
173     MockShape *shape = new MockShape();
174 
175     QVERIFY(shape->isSelectable());
176     shape->setGeometryProtected(true);
177     QVERIFY(!shape->isEditable());
178     shape->setGeometryProtected(false);
179     QVERIFY(shape->isEditable());
180 
181     shape->setVisible(false);
182     QVERIFY(!shape->isEditable());
183     shape->setVisible(true);
184     QVERIFY(shape->isEditable());
185 
186     KoShapeContainer *parent = new MockContainer();
187     parent->addShape(shape);
188 
189     parent->setGeometryProtected(true);
190     QEXPECT_FAIL("", "Should check parent according to api doc", Continue);
191     QVERIFY(!shape->isEditable());
192     parent->setGeometryProtected(false);
193     QVERIFY(shape->isEditable());
194 
195     parent->setVisible(false);
196     QEXPECT_FAIL("", "Should check parent according to api doc", Continue);
197     QVERIFY(!shape->isEditable());
198     parent->setVisible(true);
199     QVERIFY(shape->isEditable());
200 }
201 
testShape()202 void TestAllowedInteraction::testShape()
203 {
204     MockShape *shape = new MockShape();
205 
206     QVERIFY(shape->allowedInteractions().testFlag(KoShape::MoveAllowed));
207     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, false));
208     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::MoveAllowed));
209     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, true));
210     QVERIFY(shape->allowedInteractions().testFlag(KoShape::MoveAllowed));
211 
212     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ResizeAllowed));
213     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, false));
214     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::ResizeAllowed));
215     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, true));
216     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ResizeAllowed));
217 
218     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ShearingAllowed));
219     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ShearingAllowed, false));
220     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::ShearingAllowed));
221     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ShearingAllowed, true));
222     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ShearingAllowed));
223 
224     QVERIFY(shape->allowedInteractions().testFlag(KoShape::RotationAllowed));
225     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::RotationAllowed, false));
226     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::RotationAllowed));
227     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::RotationAllowed, true));
228     QVERIFY(shape->allowedInteractions().testFlag(KoShape::RotationAllowed));
229 
230     QVERIFY(shape->allowedInteractions().testFlag(KoShape::SelectionAllowed));
231     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::SelectionAllowed, false));
232     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::SelectionAllowed));
233     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::SelectionAllowed, true));
234     QVERIFY(shape->allowedInteractions().testFlag(KoShape::SelectionAllowed));
235 
236     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ContentChangeAllowed));
237     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ContentChangeAllowed, false));
238     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::ContentChangeAllowed));
239     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ContentChangeAllowed, true));
240     QVERIFY(shape->allowedInteractions().testFlag(KoShape::ContentChangeAllowed));
241 
242     QVERIFY(shape->allowedInteractions().testFlag(KoShape::DeletionAllowed));
243     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::DeletionAllowed, false));
244     QVERIFY(!shape->allowedInteractions().testFlag(KoShape::DeletionAllowed));
245     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::DeletionAllowed, true));
246     QVERIFY(shape->allowedInteractions().testFlag(KoShape::DeletionAllowed));
247 
248     QVERIFY(shape->isSelectable());
249     shape->setSelectable(false);
250     QVERIFY(!shape->isSelectable());
251     shape->setSelectable(true);
252     QVERIFY(shape->isSelectable());
253 
254     QVERIFY(shape->isDeletable());
255     shape->setDeletable(false);
256     QVERIFY(!shape->isDeletable());
257     shape->setDeletable(true);
258     QVERIFY(shape->isDeletable());
259 
260     QVERIFY(!shape->isGeometryProtected());
261     shape->setGeometryProtected(true);
262     QVERIFY(shape->isGeometryProtected());
263     shape->setGeometryProtected(false);
264     QVERIFY(!shape->isGeometryProtected());
265     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, false));
266     QVERIFY(shape->isGeometryProtected());
267     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, true));
268     QVERIFY(!shape->isGeometryProtected());
269     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, false));
270     QVERIFY(shape->isGeometryProtected());
271     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, true));
272     QVERIFY(!shape->isGeometryProtected());
273     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, false));
274     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, false));
275     QVERIFY(shape->isGeometryProtected());
276     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::MoveAllowed, true));
277     shape->setAllowedInteractions(shape->allowedInteractions().setFlag(KoShape::ResizeAllowed, true));
278     QVERIFY(!shape->isGeometryProtected());
279 
280     QVERIFY(!shape->isContentProtected());
281     shape->setContentProtected(true);
282     QVERIFY(shape->isContentProtected());
283     shape->setContentProtected(false);
284     QVERIFY(!shape->isContentProtected());
285 
286 }
287 
testParentAllowedInteractions()288 void TestAllowedInteraction::testParentAllowedInteractions()
289 {
290     MockShape *shape = new MockShape();
291     KoShapeContainer *parent = new MockContainer();
292     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
293     parent->addShape(shape);
294     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
295     parent->setAllowedInteractions(0);
296     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
297     QVERIFY(!(shape->allowedInteractions() & KoShape::MoveAllowed));
298     QVERIFY(!(shape->allowedInteractions() & KoShape::ResizeAllowed));
299     QVERIFY(!(shape->allowedInteractions() & KoShape::ShearingAllowed));
300     QVERIFY(!(shape->allowedInteractions() & KoShape::RotationAllowed));
301     QVERIFY(!(shape->allowedInteractions() & KoShape::SelectionAllowed));
302     QVERIFY(!(shape->allowedInteractions() & KoShape::ContentChangeAllowed));
303     QVERIFY(!(shape->allowedInteractions() & KoShape::DeletionAllowed));
304 
305     parent->setAllowedInteractions(KoShape::MoveAllowed);
306     QVERIFY(shape->allowedInteractions() & KoShape::MoveAllowed);
307 
308     parent->setAllowedInteractions(KoShape::ResizeAllowed);
309     QVERIFY(shape->allowedInteractions() & KoShape::ResizeAllowed);
310 
311     parent->setAllowedInteractions(KoShape::ShearingAllowed);
312     QVERIFY(shape->allowedInteractions() & KoShape::ShearingAllowed);
313 
314     parent->setAllowedInteractions(KoShape::RotationAllowed);
315     QVERIFY(shape->allowedInteractions() & KoShape::RotationAllowed);
316 
317     parent->setAllowedInteractions(KoShape::SelectionAllowed);
318     QVERIFY(shape->allowedInteractions() & KoShape::SelectionAllowed);
319 
320     parent->setAllowedInteractions(KoShape::ContentChangeAllowed);
321     QVERIFY(shape->allowedInteractions() & KoShape::ContentChangeAllowed);
322 
323     parent->setAllowedInteractions(KoShape::DeletionAllowed);
324     QVERIFY(shape->allowedInteractions() & KoShape::DeletionAllowed);
325 }
326 
testParentAllowedInteraction()327 void TestAllowedInteraction::testParentAllowedInteraction()
328 {
329     MockShape *shape = new MockShape();
330     KoShapeContainer *parent = new MockContainer();
331     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
332     parent->addShape(shape);
333     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
334 
335     parent->setAllowedInteraction(KoShape::MoveAllowed, true);
336     QVERIFY(shape->allowedInteraction(KoShape::MoveAllowed));
337 
338     parent->setAllowedInteraction(KoShape::ResizeAllowed, true);
339     QVERIFY(shape->allowedInteraction(KoShape::ResizeAllowed));
340 
341     parent->setAllowedInteraction(KoShape::ShearingAllowed, true);
342     QVERIFY(shape->allowedInteraction(KoShape::ShearingAllowed));
343 
344     parent->setAllowedInteraction(KoShape::RotationAllowed, true);
345     QVERIFY(shape->allowedInteraction(KoShape::RotationAllowed));
346 
347     parent->setAllowedInteraction(KoShape::SelectionAllowed, true);
348     QVERIFY(shape->allowedInteraction(KoShape::SelectionAllowed));
349 
350     parent->setAllowedInteraction(KoShape::ContentChangeAllowed, true);
351     QVERIFY(shape->allowedInteraction(KoShape::ContentChangeAllowed));
352 
353     parent->setAllowedInteraction(KoShape::DeletionAllowed, true);
354     QVERIFY(shape->allowedInteraction(KoShape::DeletionAllowed));
355 
356     parent->setAllowedInteraction(KoShape::MoveAllowed, false);
357     QVERIFY(!shape->allowedInteraction(KoShape::MoveAllowed));
358 
359     parent->setAllowedInteraction(KoShape::ResizeAllowed, false);
360     QVERIFY(!shape->allowedInteraction(KoShape::ResizeAllowed));
361 
362     parent->setAllowedInteraction(KoShape::ShearingAllowed, false);
363     QVERIFY(!shape->allowedInteraction(KoShape::ShearingAllowed));
364 
365     parent->setAllowedInteraction(KoShape::RotationAllowed, false);
366     QVERIFY(!shape->allowedInteraction(KoShape::RotationAllowed));
367 
368     parent->setAllowedInteraction(KoShape::SelectionAllowed, false);
369     QVERIFY(!shape->allowedInteraction(KoShape::SelectionAllowed));
370 
371     parent->setAllowedInteraction(KoShape::ContentChangeAllowed, false);
372     QVERIFY(!shape->allowedInteraction(KoShape::ContentChangeAllowed));
373 
374     parent->setAllowedInteraction(KoShape::DeletionAllowed, false);
375     QVERIFY(!shape->allowedInteraction(KoShape::DeletionAllowed));
376 }
377 
testGrandParentAllowedInteractions()378 void TestAllowedInteraction::testGrandParentAllowedInteractions()
379 {
380     MockShape *shape = new MockShape();
381     KoShapeContainer *parent = new MockContainer();
382     KoShapeContainer *grandParent = new MockContainer();
383 
384     QVERIFY(shape->allowedInteractions() == grandParent->allowedInteractions());
385     parent->addShape(shape);
386     grandParent->addShape(parent);
387     QVERIFY(shape->allowedInteractions() == parent->allowedInteractions(shape));
388 
389     grandParent->setAllowedInteractions(0);
390     QVERIFY(shape->allowedInteractions() == grandParent->allowedInteractions());
391     QVERIFY(!(shape->allowedInteractions() & KoShape::MoveAllowed));
392     QVERIFY(!(shape->allowedInteractions() & KoShape::ResizeAllowed));
393     QVERIFY(!(shape->allowedInteractions() & KoShape::ShearingAllowed));
394     QVERIFY(!(shape->allowedInteractions() & KoShape::RotationAllowed));
395     QVERIFY(!(shape->allowedInteractions() & KoShape::SelectionAllowed));
396     QVERIFY(!(shape->allowedInteractions() & KoShape::ContentChangeAllowed));
397     QVERIFY(!(shape->allowedInteractions() & KoShape::DeletionAllowed));
398 
399     grandParent->setAllowedInteractions(KoShape::MoveAllowed);
400     QVERIFY(shape->allowedInteractions() & KoShape::MoveAllowed);
401 
402     grandParent->setAllowedInteractions(KoShape::ResizeAllowed);
403     QVERIFY(shape->allowedInteractions() & KoShape::ResizeAllowed);
404 
405     grandParent->setAllowedInteractions(KoShape::ShearingAllowed);
406     QVERIFY(shape->allowedInteractions() & KoShape::ShearingAllowed);
407 
408     grandParent->setAllowedInteractions(KoShape::RotationAllowed);
409     QVERIFY(shape->allowedInteractions() & KoShape::RotationAllowed);
410 
411     grandParent->setAllowedInteractions(KoShape::SelectionAllowed);
412     QVERIFY(shape->allowedInteractions() & KoShape::SelectionAllowed);
413 
414     grandParent->setAllowedInteractions(KoShape::ContentChangeAllowed);
415     QVERIFY(shape->allowedInteractions() & KoShape::ContentChangeAllowed);
416 
417     grandParent->setAllowedInteractions(KoShape::DeletionAllowed);
418     QVERIFY(shape->allowedInteractions() & KoShape::DeletionAllowed);
419 }
420 
testGrandParentAllowedInteraction()421 void TestAllowedInteraction::testGrandParentAllowedInteraction()
422 {
423     MockShape *shape = new MockShape();
424     KoShapeContainer *parent = new MockContainer();
425     KoShapeContainer *grandParent = new MockContainer();
426 
427     parent->addShape(shape);
428     grandParent->addShape(parent);
429 
430     QVERIFY(shape->allowedInteractions() == grandParent->allowedInteractions());
431 
432     grandParent->setAllowedInteraction(KoShape::MoveAllowed, true);
433     QVERIFY(shape->allowedInteraction(KoShape::MoveAllowed));
434 
435     grandParent->setAllowedInteraction(KoShape::ResizeAllowed, true);
436     QVERIFY(shape->allowedInteraction(KoShape::ResizeAllowed));
437 
438     grandParent->setAllowedInteraction(KoShape::ShearingAllowed, true);
439     QVERIFY(shape->allowedInteraction(KoShape::ShearingAllowed));
440 
441     grandParent->setAllowedInteraction(KoShape::RotationAllowed, true);
442     QVERIFY(shape->allowedInteraction(KoShape::RotationAllowed));
443 
444     grandParent->setAllowedInteraction(KoShape::SelectionAllowed, true);
445     QVERIFY(shape->allowedInteraction(KoShape::SelectionAllowed));
446 
447     grandParent->setAllowedInteraction(KoShape::ContentChangeAllowed, true);
448     QVERIFY(shape->allowedInteraction(KoShape::ContentChangeAllowed));
449 
450     grandParent->setAllowedInteraction(KoShape::DeletionAllowed, true);
451     QVERIFY(shape->allowedInteraction(KoShape::DeletionAllowed));
452 
453     grandParent->setAllowedInteraction(KoShape::MoveAllowed, false);
454     QVERIFY(!shape->allowedInteraction(KoShape::MoveAllowed));
455 
456     grandParent->setAllowedInteraction(KoShape::ResizeAllowed, false);
457     QVERIFY(!shape->allowedInteraction(KoShape::ResizeAllowed));
458 
459     grandParent->setAllowedInteraction(KoShape::ShearingAllowed, false);
460     QVERIFY(!shape->allowedInteraction(KoShape::ShearingAllowed));
461 
462     grandParent->setAllowedInteraction(KoShape::RotationAllowed, false);
463     QVERIFY(!shape->allowedInteraction(KoShape::RotationAllowed));
464 
465     grandParent->setAllowedInteraction(KoShape::SelectionAllowed, false);
466     QVERIFY(!shape->allowedInteraction(KoShape::SelectionAllowed));
467 
468     grandParent->setAllowedInteraction(KoShape::ContentChangeAllowed, false);
469     QVERIFY(!shape->allowedInteraction(KoShape::ContentChangeAllowed));
470 
471     grandParent->setAllowedInteraction(KoShape::DeletionAllowed, false);
472     QVERIFY(!shape->allowedInteraction(KoShape::DeletionAllowed));
473 }
474 
475 QTEST_GUILESS_MAIN(TestAllowedInteraction)
476