1 /*
2  *  Copyright (c) 2011 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program 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
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "kis_selection_manager_test.h"
20 #include <operations/kis_operation_configuration.h>
21 
22 #include <QTest>
23 
24 #include <sdk/tests/testutil.h>
25 
26 #include "ui_manager_test.h"
27 
28 class SelectionManagerTester : public TestUtil::UiManagerTest
29 {
30 public:
SelectionManagerTester(bool useSelection)31     SelectionManagerTester(bool useSelection)
32         : UiManagerTest(useSelection, false,  "selection_manager_test")
33     {
34         Q_ASSERT(selectionManager);
35     }
36 
37 };
38 
39 
testFillForegroundWithoutSelection()40 void KisSelectionManagerTest::testFillForegroundWithoutSelection()
41 {
42     SelectionManagerTester t(false);
43 
44     t.selectionManager->fillForegroundColor();
45     t.image->waitForDone();
46     QVERIFY(t.checkLayers("fill_foreground_without_selection"));
47 
48     t.checkUndo();
49     t.startConcurrentTask();
50 
51     t.selectionManager->fillForegroundColor();
52     t.image->waitForDone();
53     QVERIFY(t.checkLayers("fill_foreground_without_selection"));
54 }
55 
testFillForegroundWithSelection()56 void KisSelectionManagerTest::testFillForegroundWithSelection()
57 {
58     SelectionManagerTester t(true);
59 
60     t.selectionManager->fillForegroundColor();
61     t.image->waitForDone();
62     QVERIFY(t.checkLayers("fill_foreground_with_selection"));
63 
64     t.checkUndo();
65     t.startConcurrentTask();
66 
67     t.selectionManager->fillForegroundColor();
68     t.image->waitForDone();
69 
70     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
71     QVERIFY(t.checkLayers("fill_foreground_with_selection"));
72 }
73 
testFillBackgroundWithSelection()74 void KisSelectionManagerTest::testFillBackgroundWithSelection()
75 {
76     SelectionManagerTester t(true);
77 
78     t.selectionManager->fillBackgroundColor();
79     t.image->waitForDone();
80     QVERIFY(t.checkLayers("fill_background_with_selection"));
81 
82     t.checkUndo();
83     t.startConcurrentTask();
84 
85     t.selectionManager->fillBackgroundColor();
86     t.image->waitForDone();
87     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
88     QVERIFY(t.checkLayers("fill_background_with_selection"));
89 }
90 
testFillPatternWithSelection()91 void KisSelectionManagerTest::testFillPatternWithSelection()
92 {
93     SelectionManagerTester t(true);
94 
95     t.selectionManager->fillPattern();
96     t.image->waitForDone();
97     QVERIFY(t.checkLayers("fill_pattern_with_selection"));
98 
99     t.checkUndo();
100     t.startConcurrentTask();
101 
102     t.selectionManager->fillPattern();
103     t.image->waitForDone();
104     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
105     QVERIFY(t.checkLayers("fill_pattern_with_selection"));
106 }
107 
testResizeToSelection()108 void KisSelectionManagerTest::testResizeToSelection()
109 {
110     SelectionManagerTester t(true);
111 
112     t.selectionManager->imageResizeToSelection();
113     t.image->waitForDone();
114     QVERIFY(t.checkLayers("resize_to_selection"));
115 
116     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
117     t.checkUndo();
118     t.startConcurrentTask();
119 
120     t.selectionManager->imageResizeToSelection();
121     t.image->waitForDone();
122 
123     QEXPECT_FAIL("", "The user may run Resize to Selection concurrently. It will cause wrong image/selection size fetched for the crop. There is some barrier needed. At least it doesn't crash.", Continue);
124     QVERIFY(t.checkLayers("resize_to_selection"));
125 }
126 
testSelectAll()127 void KisSelectionManagerTest::testSelectAll()
128 {
129     SelectionManagerTester t(true);
130 
131     t.selectionManager->selectAll();
132     t.image->waitForDone();
133     QVERIFY(t.checkSelectionOnly("select_all"));
134 
135     t.checkUndo();
136     t.startConcurrentTask();
137 
138     t.selectionManager->selectAll();
139     t.image->waitForDone();
140     QVERIFY(t.checkSelectionOnly("select_all"));
141 }
142 
testDeselectReselect()143 void KisSelectionManagerTest::testDeselectReselect()
144 {
145     SelectionManagerTester t(true);
146 
147     t.selectionManager->deselect();
148     t.image->waitForDone();
149     QVERIFY(t.checkNoSelection());
150 
151     t.checkUndo();
152     t.startConcurrentTask();
153 
154     t.selectionManager->deselect();
155     t.image->waitForDone();
156     QVERIFY(t.checkNoSelection());
157 
158     t.selectionManager->reselect();
159     t.image->waitForDone();
160     QVERIFY(t.checkSelectionOnly("initial"));
161 
162     t.undoStore->undo();
163     t.image->waitForDone();
164     QVERIFY(t.checkNoSelection());
165 
166     t.startConcurrentTask();
167 
168     t.selectionManager->reselect();
169     t.image->waitForDone();
170     QVERIFY(t.checkSelectionOnly("initial"));
171 }
172 
testCopyPaste()173 void KisSelectionManagerTest::testCopyPaste()
174 {
175     SelectionManagerTester t(true);
176 
177     t.selectionManager->copy();
178     t.selectionManager->paste();
179     t.image->waitForDone();
180     QVERIFY(t.checkLayers("copy_paste"));
181 
182     t.checkUndo();
183     t.startConcurrentTask();
184 
185     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
186     t.selectionManager->copy();
187     t.selectionManager->paste();
188     t.image->waitForDone();
189     QVERIFY(t.checkLayers("copy_paste"));
190 
191     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
192     t.checkUndo();
193     t.startConcurrentTask();
194 
195 
196     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
197     t.selectionManager->paste();
198     t.image->waitForDone();
199     QVERIFY(t.checkLayers("copy_paste"));
200 }
201 
testCopyPasteMerged()202 void KisSelectionManagerTest::testCopyPasteMerged()
203 {
204     SelectionManagerTester t(true);
205 
206     t.selectionManager->copyMerged();
207     t.selectionManager->paste();
208     t.image->waitForDone();
209     QVERIFY(t.checkLayersFuzzy("copy_paste_merged"));
210 
211     t.checkUndo();
212     t.startConcurrentTask();
213 
214     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
215     t.selectionManager->copyMerged();
216     t.selectionManager->paste();
217     t.image->waitForDone();
218     QVERIFY(t.checkLayersFuzzy("copy_paste_merged"));
219 }
220 
testCutPaste()221 void KisSelectionManagerTest::testCutPaste()
222 {
223     SelectionManagerTester t(true);
224 
225     t.selectionManager->cut();
226     t.selectionManager->paste();
227     t.image->waitForDone();
228     QVERIFY(t.checkLayers("cut_paste"));
229 
230     t.checkDoubleUndo();
231     t.startConcurrentTask();
232 
233     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
234     t.selectionManager->cut();
235     t.selectionManager->paste();
236     t.image->waitForDone();
237     QVERIFY(t.checkLayers("cut_paste"));
238 }
239 
testInvertSelection()240 void KisSelectionManagerTest::testInvertSelection()
241 {
242     SelectionManagerTester t(true);
243 
244     KisOperationConfigurationSP config = new KisOperationConfiguration("invertselection");
245     t.actionManager->runOperationFromConfiguration(config);
246     t.image->waitForDone();
247     QVERIFY(t.checkLayers("invert_selection"));
248 
249     t.checkUndo();
250     t.startConcurrentTask();
251 
252     QEXPECT_FAIL("", "Fix some race condition on clone layers!", Continue);
253     config = new KisOperationConfiguration("invertselection");
254     t.actionManager->runOperationFromConfiguration(config);
255     t.image->waitForDone();
256     QVERIFY(t.checkLayers("invert_selection"));
257 }
258 
testFeatherSelection()259 void KisSelectionManagerTest::testFeatherSelection()
260 {
261     SelectionManagerTester t(true);
262 
263     KisOperationConfigurationSP config = new KisOperationConfiguration("featherselection");
264     config->setProperty("radius", 10);
265     t.actionManager->runOperationFromConfiguration(config);
266     t.image->waitForDone();
267     QVERIFY(t.checkSelectionOnly("feather_selection"));
268 
269     t.checkUndo();
270     t.startConcurrentTask();
271 
272     config = new KisOperationConfiguration("featherselection");
273     config->setProperty("radius", 10);
274     t.actionManager->runOperationFromConfiguration(config);
275     t.image->waitForDone();
276     QVERIFY(t.checkSelectionOnly("feather_selection"));
277 }
278 
testGrowSelectionSimplified()279 void KisSelectionManagerTest::testGrowSelectionSimplified()
280 {
281     SelectionManagerTester t(true);
282 
283     KisOperationConfigurationSP config = new KisOperationConfiguration("growselection");
284     config->setProperty("x-radius", 10);
285     config->setProperty("y-radius", 5);
286     t.actionManager->runOperationFromConfiguration(config);
287     t.image->waitForDone();
288     QVERIFY(t.checkSelectionOnly("grow_selection"));
289 }
290 
testShrinkSelectionUnlockedSimplified()291 void KisSelectionManagerTest::testShrinkSelectionUnlockedSimplified()
292 {
293     SelectionManagerTester t(true);
294 
295     KisOperationConfigurationSP config = new KisOperationConfiguration("shrinkselection");
296     config->setProperty("x-radius", 10);
297     config->setProperty("y-radius", 5);
298     config->setProperty("edgeLock", false);
299     t.actionManager->runOperationFromConfiguration(config);
300     t.image->waitForDone();
301     QVERIFY(t.checkSelectionOnly("shrink_selection_unlocked"));
302 }
303 
testShrinkSelectionLockedSimplified()304 void KisSelectionManagerTest::testShrinkSelectionLockedSimplified()
305 {
306     SelectionManagerTester t(true);
307 
308     KisOperationConfigurationSP config = new KisOperationConfiguration("shrinkselection");
309     config->setProperty("x-radius", 10);
310     config->setProperty("y-radius", 5);
311     config->setProperty("edgeLock", true);
312     t.actionManager->runOperationFromConfiguration(config);
313     t.image->waitForDone();
314     QVERIFY(t.checkSelectionOnly("shrink_selection_locked"));
315 }
316 
testSmoothSelectionSimplified()317 void KisSelectionManagerTest::testSmoothSelectionSimplified()
318 {
319     SelectionManagerTester t(true);
320 
321     KisOperationConfigurationSP config = new KisOperationConfiguration("smoothselection");
322     t.actionManager->runOperationFromConfiguration(config);
323     t.image->waitForDone();
324     QVERIFY(t.checkSelectionOnly("smooth_selection"));
325 }
326 
testErodeSelectionSimplified()327 void KisSelectionManagerTest::testErodeSelectionSimplified()
328 {
329 //     SelectionManagerTester t(true);
330 //
331 //     t.selectionManager->erode();
332 //     t.image->waitForDone();
333 //     QVERIFY(t.checkSelectionOnly("erode_selection"));
334 }
335 
testDilateSelectionSimplified()336 void KisSelectionManagerTest::testDilateSelectionSimplified()
337 {
338 //     SelectionManagerTester t(true);
339 //
340 //     t.selectionManager->dilate();
341 //     t.image->waitForDone();
342 //     QVERIFY(t.checkSelectionOnly("dilate_selection"));
343 }
344 
testBorderSelectionSimplified()345 void KisSelectionManagerTest::testBorderSelectionSimplified()
346 {
347     SelectionManagerTester t(true);
348 
349     KisOperationConfigurationSP config = new KisOperationConfiguration("borderselection");
350     config->setProperty("x-radius", 10);
351     config->setProperty("y-radius", 5);
352     t.actionManager->runOperationFromConfiguration(config);
353     t.image->waitForDone();
354     QVERIFY(t.checkSelectionOnly("border_selection"));
355 }
356 
357 #include <floodfill/kis_scanline_fill.h>
358 
testScanline16bit()359 void KisSelectionManagerTest::testScanline16bit()
360 {
361     const int THRESHOLD = 20;
362 
363     QString fileName = TestUtil::fetchDataFileLazy("flood_fill_16bit.kra");
364     QVERIFY(QFile::exists(fileName));
365 
366     KisDocument *doc = KisPart::instance()->createDocument();
367     doc->loadNativeFormat(fileName);
368 
369     KisPaintDeviceSP dev = doc->image()->root()->firstChild()->paintDevice();
370     QVERIFY(dev);
371 
372     dbgKrita << ppVar(dev->colorSpace());
373 
374     QRect imageRect = doc->image()->bounds();
375 
376     dbgKrita << ppVar(imageRect);
377 
378     QPoint startPoint = imageRect.center();
379 
380     dbgKrita << ppVar(startPoint);
381 
382     KisPixelSelectionSP pixelSelection = new KisPixelSelection();
383 
384     {
385         KisScanlineFill gc(dev, startPoint, imageRect);
386         gc.setThreshold(THRESHOLD);
387         gc.fillSelection(pixelSelection);
388 
389         QImage resultImage =
390             pixelSelection->convertToQImage(0,
391                                             imageRect);
392 
393         QVERIFY(TestUtil::checkQImage(resultImage,
394                                       "selection_manager_test",
395                                       "scanline",
396                                       "16bit_thres_20"));
397     }
398 
399     const KoColorSpace *rgb8CS = KoColorSpaceRegistry::instance()->rgb8();
400     pixelSelection->clear();
401     dev->convertTo(rgb8CS);
402 
403     {
404         KisScanlineFill gc(dev, startPoint, imageRect);
405         gc.setThreshold(THRESHOLD);
406         gc.fillSelection(pixelSelection);
407 
408         QImage resultImage =
409             pixelSelection->convertToQImage(0,
410                                             imageRect);
411 
412         QVERIFY(TestUtil::checkQImage(resultImage,
413                                       "selection_manager_test",
414                                       "scanline",
415                                       "8bit_thres_20"));
416     }
417 
418 }
419 
420 KISTEST_MAIN(KisSelectionManagerTest)
421