Clover icon

Magnolia Imaging Module 3.4.2-SUPPORT-10161

  1. Project Clover database Tue Jul 16 2019 23:33:19 EEST
  2. Package info.magnolia.imaging.operations.cropresize

File AutoCropAndResizeTest.java

 

Code metrics

0
174
17
1
322
232
17
0.1
10.24
17
1

Classes

Class Line # Actions
AutoCropAndResizeTest 50 174 0% 17 0
1.0100%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.imaging.operations.cropresize;
35   
36    import static org.junit.Assert.assertEquals;
37   
38    import info.magnolia.imaging.AbstractImagingTest;
39   
40    import java.awt.image.BufferedImage;
41    import java.io.IOException;
42   
43    import javax.imageio.ImageIO;
44   
45    import org.junit.Test;
46   
47    /**
48    * TODO tests where source is smaller than target
49    */
 
50    public class AutoCropAndResizeTest extends AbstractImagingTest {
51   
 
52  1 toggle @Test
53    public void justResizeIfTargetRatioIsEquivalentToSourceRatio() throws Exception {
54  1 final AutoCropAndResize op = new AutoCropAndResize();
55  1 op.setTargetWidth(400);
56  1 op.setTargetHeight(300);
57   
58  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
59  1 assertEquals(1600, coords.getWidth());
60  1 assertEquals(1200, coords.getHeight());
61  1 assertEquals(0, coords.getX1());
62  1 assertEquals(1600, coords.getX2());
63  1 assertEquals(0, coords.getY1());
64  1 assertEquals(1200, coords.getY2());
65   
66  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
67  1 write(res);
68  1 assertEquals(400, res.getWidth());
69  1 assertEquals(300, res.getHeight());
70    }
71   
 
72  1 toggle @Test
73    public void respectsRatioOfTargetSizeIfCroppingIsNeededAndCropFromTheCenter() throws Exception {
74  1 final AutoCropAndResize op = new AutoCropAndResize();
75  1 op.setTargetWidth(400);
76  1 op.setTargetHeight(225);
77   
78  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);// 16:9 ratio
79  1 assertEquals(1600, coords.getWidth());
80  1 assertEquals(900, coords.getHeight());
81  1 assertEquals(0, coords.getX1());
82  1 assertEquals(1600, coords.getX2());
83  1 assertEquals(150, coords.getY1()); // 1200 cropped to 900
84  1 assertEquals(1050, coords.getY2());
85   
86  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
87  1 write(res);
88  1 assertEquals(400, res.getWidth());
89  1 assertEquals(225, res.getHeight());
90    }
91   
 
92  1 toggle @Test
93    public void verticalRatiosAlsoWork() throws Exception {
94  1 final AutoCropAndResize op = new AutoCropAndResize();
95  1 op.setTargetWidth(200);
96  1 op.setTargetHeight(600);
97   
98  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
99  1 assertEquals(400, coords.getWidth());
100  1 assertEquals(1200, coords.getHeight());
101  1 assertEquals(600, coords.getX1());
102  1 assertEquals(1000, coords.getX2());
103  1 assertEquals(0, coords.getY1());
104  1 assertEquals(1200, coords.getY2());
105   
106  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
107  1 write(res);
108  1 assertEquals(200, res.getWidth());
109  1 assertEquals(600, res.getHeight());
110    }
111   
 
112  1 toggle @Test
113    public void doesNotCropIfOnlyTargetHeightIsSpecifiedAndRespectsOriginalRatio() throws Exception {
114  1 final AutoCropAndResize op = new AutoCropAndResize();
115  1 op.setTargetWidth(0);
116  1 op.setTargetHeight(300);
117   
118  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
119  1 assertEquals(1600, coords.getWidth());
120  1 assertEquals(1200, coords.getHeight());
121  1 assertEquals(0, coords.getX1());
122  1 assertEquals(1600, coords.getX2());
123  1 assertEquals(0, coords.getY1());
124  1 assertEquals(1200, coords.getY2());
125   
126  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
127  1 write(res);
128  1 assertEquals(400, res.getWidth());
129  1 assertEquals(300, res.getHeight());
130    }
131   
 
132  1 toggle @Test
133    public void doesNotCropIfOnlyTargetWidthIsSpecifiedAndRespectsOriginalRatio() throws Exception {
134  1 final AutoCropAndResize op = new AutoCropAndResize();
135  1 op.setTargetWidth(400);
136  1 op.setTargetHeight(0);
137   
138  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
139  1 assertEquals(1600, coords.getWidth());
140  1 assertEquals(1200, coords.getHeight());
141  1 assertEquals(0, coords.getX1());
142  1 assertEquals(1600, coords.getX2());
143  1 assertEquals(0, coords.getY1());
144  1 assertEquals(1200, coords.getY2());
145   
146  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
147  1 write(res);
148  1 assertEquals(400, res.getWidth());
149  1 assertEquals(300, res.getHeight());
150    }
151   
152    /**
153    * Tests with 1:1 ratio.
154    */
 
155  1 toggle @Test
156    public void squareTargetUsesLargestPossibleZoneForHorizontalSource() throws Exception {
157  1 final AutoCropAndResize op = new AutoCropAndResize();
158  1 op.setTargetWidth(400);
159  1 op.setTargetHeight(400);
160   
161  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
162  1 assertEquals(1200, coords.getWidth());
163  1 assertEquals(1200, coords.getHeight());
164  1 assertEquals(200, coords.getX1());
165  1 assertEquals(1400, coords.getX2());
166  1 assertEquals(0, coords.getY1());
167  1 assertEquals(1200, coords.getY2());
168   
169  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
170  1 write(res);
171  1 assertEquals(400, res.getWidth());
172  1 assertEquals(400, res.getHeight());
173    }
174   
 
175  1 toggle @Test
176    public void squareTargetUsesLargestPossibleZoneForVerticalSourceToo() throws Exception {
177  1 final AutoCropAndResize op = new AutoCropAndResize();
178  1 op.setTargetWidth(400);
179  1 op.setTargetHeight(400);
180   
181  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
182  1 assertEquals(1200, coords.getWidth());
183  1 assertEquals(1200, coords.getHeight());
184  1 assertEquals(0, coords.getX1());
185  1 assertEquals(1200, coords.getX2());
186  1 assertEquals(200, coords.getY1());
187  1 assertEquals(1400, coords.getY2());
188   
189  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
190  1 write(res);
191  1 assertEquals(400, res.getWidth());
192  1 assertEquals(400, res.getHeight());
193    }
194   
195    /**
196    * Tests with vertical source.
197    */
 
198  1 toggle @Test
199    public void verticalSourceIsHandledJustAsWell() throws Exception {
200  1 final AutoCropAndResize op = new AutoCropAndResize();
201  1 op.setTargetWidth(400);
202  1 op.setTargetHeight(300);
203   
204  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
205  1 assertEquals(1200, coords.getWidth());
206  1 assertEquals(900, coords.getHeight());
207  1 assertEquals(0, coords.getX1());
208  1 assertEquals(1200, coords.getX2());
209  1 assertEquals(350, coords.getY1());
210  1 assertEquals(1250, coords.getY2());
211   
212  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
213  1 write(res);
214  1 assertEquals(400, res.getWidth());
215  1 assertEquals(300, res.getHeight());
216    }
217   
 
218  1 toggle @Test
219    public void verticalSourceWithVerticalRatioIsAlsoSwell() throws Exception {
220  1 final AutoCropAndResize op = new AutoCropAndResize();
221  1 op.setTargetWidth(150);
222  1 op.setTargetHeight(600);
223   
224  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
225  1 assertEquals(400, coords.getWidth());
226  1 assertEquals(1600, coords.getHeight());
227  1 assertEquals(400, coords.getX1());
228  1 assertEquals(800, coords.getX2());
229  1 assertEquals(0, coords.getY1());
230  1 assertEquals(1600, coords.getY2());
231   
232  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
233  1 write(res);
234  1 assertEquals(150, res.getWidth());
235  1 assertEquals(600, res.getHeight());
236    }
237   
 
238  1 toggle @Test
239    public void philippSawBlackBorders() throws Exception {
240  1 final AutoCropAndResize op = new AutoCropAndResize();
241  1 op.setTargetWidth(150);
242  1 op.setTargetHeight(100);
243   
244  1 final BufferedImage src = generateArbitraryTestImage(400, 200);
245   
246  1 final Coords coords = op.getCroopCoords(src, null);
247  1 assertEquals(300, coords.getWidth());
248  1 assertEquals(200, coords.getHeight());
249  1 assertEquals(50, coords.getX1());
250  1 assertEquals(350, coords.getX2());
251  1 assertEquals(0, coords.getY1());
252  1 assertEquals(200, coords.getY2());
253   
254  1 final BufferedImage res = op.apply(src, null);
255  1 write(res);
256  1 assertEquals(150, res.getWidth());
257  1 assertEquals(100, res.getHeight());
258    }
259   
 
260  1 toggle @Test
261    public void thereWereVerticalBlackBordersToo() throws Exception {
262  1 final AutoCropAndResize op = new AutoCropAndResize();
263  1 op.setTargetWidth(100);
264  1 op.setTargetHeight(150);
265   
266  1 final BufferedImage src = generateArbitraryTestImage(200, 400);
267   
268  1 final Coords coords = op.getCroopCoords(src, null);
269  1 assertEquals(200, coords.getWidth());
270  1 assertEquals(300, coords.getHeight());
271  1 assertEquals(0, coords.getX1());
272  1 assertEquals(200, coords.getX2());
273  1 assertEquals(50, coords.getY1());
274  1 assertEquals(350, coords.getY2());
275   
276  1 final BufferedImage res = op.apply(src, null);
277  1 write(res);
278  1 assertEquals(100, res.getWidth());
279  1 assertEquals(150, res.getHeight());
280    }
281   
 
282  1 toggle @Test
283    public void noResizeIfWidthAndHeightAreNotSpecified() throws IOException {
284  1 doResizeTest(-1, -1, 16, 8);
285  1 doResizeTest(0, 0, 16, 8);
286  1 doResizeTest(0, 0, new Coords(5, 6, 10, 18), 5, 12);
287    }
288   
 
289  1 toggle @Test
290    public void resizesProportionallyIfOnlyWidthIsSpecified() throws IOException {
291  1 doResizeTest(50, -1, 50, 25);
292  1 doResizeTest(8, -1, 8, 4);
293    }
294   
 
295  1 toggle @Test
296    public void resizesProportionallyIfOnlyHeightIsSpecified() throws IOException {
297  1 doResizeTest(-1, 50, 100, 50);
298  1 doResizeTest(-1, 6, 12, 6);
299    }
300   
 
301  1 toggle @Test
302    public void resizesUsingBothWidthAndHeightIfSpecified() throws IOException {
303  1 doResizeTest(30, 30, 30, 30);
304  1 doResizeTest(20, 50, 20, 50);
305    }
306   
 
307  8 toggle private void doResizeTest(int targetWidth, int targetHeight, int expectedWidth, int expectedHeight) throws IOException {
308  8 doResizeTest(targetWidth, targetHeight, new Coords(0, 0, 16, 8), expectedWidth, expectedHeight);
309    }
310   
 
311  9 toggle private void doResizeTest(int targetWidth, int targetHeight, Coords cropCoords, int expectedWidth, int expectedHeight) throws IOException {
312  9 final BufferedImage dummyImg = ImageIO.read(getClass().getResourceAsStream("/funnel.gif"));
313  9 final AutoCropAndResize op = new AutoCropAndResize();
314  9 op.setTargetWidth(targetWidth);
315  9 op.setTargetHeight(targetHeight);
316  9 final Size effectiveTargetSize = op.getEffectiveTargetSize(dummyImg, cropCoords, null);
317  9 final BufferedImage result = op.resize(dummyImg, cropCoords, effectiveTargetSize);
318  9 assertEquals(expectedWidth, result.getWidth());
319  9 assertEquals(expectedHeight, result.getHeight());
320    }
321   
322    }