File indexing completed on 2024-12-22 04:10:14

0001 /*
0002  *  SPDX-FileCopyrightText: 2011 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_crop_processing_visitor_test.h"
0008 
0009 #include <simpletest.h>
0010 #include <KoColorSpaceRegistry.h>
0011 #include "processing/kis_crop_processing_visitor.h"
0012 #include "commands_new/kis_processing_command.h"
0013 #include "kis_paint_layer.h"
0014 #include "kis_paint_device.h"
0015 #include "kis_fill_painter.h"
0016 #include "kis_transparency_mask.h"
0017 #include <testutil.h>
0018 #include "kis_surrogate_undo_adapter.h"
0019 #include "kis_image.h"
0020 #include "kis_pixel_selection.h"
0021 
0022 void KisCropProcessingVisitorTest::testUndo()
0023 {
0024     KisSurrogateUndoAdapter undoAdapter;
0025 
0026     const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();
0027     KisImageSP image = new KisImage(0, 300, 300, cs, "test");
0028     KisPaintLayerSP layer = new KisPaintLayer(image, "testlayer", OPACITY_OPAQUE_U8);
0029     KisPaintDeviceSP device = layer->paintDevice();
0030 
0031     QRect fillRect(50,50,100,100);
0032     QRect cropRect(25,25,100,100);
0033 
0034     KisFillPainter painter(device);
0035     painter.fillRect(fillRect, KoColor(Qt::white, cs));
0036     QImage image1 = device->convertToQImage(0, 0, 0, 300, 300);
0037 
0038     undoAdapter.beginMacro(kundo2_noi18n("test"));
0039 
0040     KisCropProcessingVisitor visitor(cropRect, true, true);
0041     layer->accept(visitor, &undoAdapter);
0042 
0043     undoAdapter.endMacro();
0044     undoAdapter.undo();
0045 
0046     QImage image2 = device->convertToQImage(0, 0, 0, 300, 300);
0047 
0048     QCOMPARE(image1, image2);
0049 }
0050 
0051 void KisCropProcessingVisitorTest::testCropTransparencyMask()
0052 {
0053     KisSurrogateUndoAdapter undoAdapter;
0054 
0055     const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();
0056     KisImageSP image = new KisImage(0, 300, 300, cs, "test");
0057 
0058     QRect cropRect(25,25,100,100);
0059     QRect selectionRect(40,40,100,100);
0060     KisTransparencyMaskSP mask = new KisTransparencyMask(image, "tmask");
0061     mask->testingInitSelection(selectionRect, 0);
0062     KisPixelSelectionSP pixelSelection = mask->selection()->pixelSelection();
0063 
0064     QCOMPARE(pixelSelection->selectedExactRect(), selectionRect);
0065 
0066     undoAdapter.beginMacro(kundo2_noi18n("test"));
0067     KisCropProcessingVisitor visitor(cropRect, true, true);
0068     mask->accept(visitor, &undoAdapter);
0069     undoAdapter.endMacro();
0070 
0071     QCOMPARE(pixelSelection->selectedExactRect(), QRect(15,15,85,85));
0072 
0073     undoAdapter.undo();
0074 
0075     QCOMPARE(pixelSelection->selectedExactRect(), selectionRect);
0076 
0077 }
0078 
0079 void KisCropProcessingVisitorTest::testWrappedInCommand()
0080 {
0081     const KoColorSpace *cs = KoColorSpaceRegistry::instance()->rgb8();
0082     KisImageSP image = new KisImage(0, 300, 300, cs, "test");
0083     KisPaintLayerSP layer = new KisPaintLayer(image, "testlayer", OPACITY_OPAQUE_U8);
0084     KisPaintDeviceSP device = layer->paintDevice();
0085 
0086     QRect fillRect(50,50,100,100);
0087     QRect cropRect(25,25,100,100);
0088 
0089     KisFillPainter painter(device);
0090     painter.fillRect(fillRect, KoColor(Qt::white, cs));
0091     QImage image1 = device->convertToQImage(0, 0, 0, 300, 300);
0092 
0093 
0094     KisProcessingVisitorSP visitor = new KisCropProcessingVisitor(cropRect, true, true);
0095 
0096     KisProcessingCommand command(visitor, layer);
0097 
0098     command.redo();
0099 
0100     QImage image2 = device->convertToQImage(0, 0, 0, 300, 300);
0101 
0102     command.undo();
0103 
0104     QImage image3 = device->convertToQImage(0, 0, 0, 300, 300);
0105 
0106     command.redo();
0107 
0108     QImage image4 = device->convertToQImage(0, 0, 0, 300, 300);
0109 
0110     command.undo();
0111 
0112     QImage image5 = device->convertToQImage(0, 0, 0, 300, 300);
0113 
0114     QCOMPARE(image2, image4);
0115     QCOMPARE(image1, image3);
0116     QCOMPARE(image1, image5);
0117 }
0118 
0119 SIMPLE_TEST_MAIN(KisCropProcessingVisitorTest)