Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.operations.cropresize

File AutoCropAndResizeTest.java

 

Code metrics

0
174
17
1
324
232
17
0.1
10.24
17
1

Classes

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

Contributing tests

This file is covered by 15 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 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    * Tests for {@link info.magnolia.imaging.operations.cropresize.AutoCropAndResize}.
49    *
50    * TODO tests where source is smaller than target
51    */
 
52    public class AutoCropAndResizeTest extends AbstractImagingTest {
53   
 
54  1 toggle @Test
55    public void testJustResizeIfTargetRatioIsEquivalentToSourceRatio() throws Exception {
56  1 final AutoCropAndResize op = new AutoCropAndResize();
57  1 op.setTargetWidth(400);
58  1 op.setTargetHeight(300);
59   
60  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
61  1 assertEquals(1600, coords.getWidth());
62  1 assertEquals(1200, coords.getHeight());
63  1 assertEquals(0, coords.getX1());
64  1 assertEquals(1600, coords.getX2());
65  1 assertEquals(0, coords.getY1());
66  1 assertEquals(1200, coords.getY2());
67   
68  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
69  1 write(res);
70  1 assertEquals(400, res.getWidth());
71  1 assertEquals(300, res.getHeight());
72    }
73   
 
74  1 toggle @Test
75    public void testRespectsRatioOfTargetSizeIfCroppingIsNeededAndCropFromTheCenter() throws Exception {
76  1 final AutoCropAndResize op = new AutoCropAndResize();
77  1 op.setTargetWidth(400);
78  1 op.setTargetHeight(225);
79   
80  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);// 16:9 ratio
81  1 assertEquals(1600, coords.getWidth());
82  1 assertEquals(900, coords.getHeight());
83  1 assertEquals(0, coords.getX1());
84  1 assertEquals(1600, coords.getX2());
85  1 assertEquals(150, coords.getY1()); // 1200 cropped to 900
86  1 assertEquals(1050, coords.getY2());
87   
88  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
89  1 write(res);
90  1 assertEquals(400, res.getWidth());
91  1 assertEquals(225, res.getHeight());
92    }
93   
 
94  1 toggle @Test
95    public void testVerticalRatiosAlsoWork() throws Exception {
96  1 final AutoCropAndResize op = new AutoCropAndResize();
97  1 op.setTargetWidth(200);
98  1 op.setTargetHeight(600);
99   
100  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
101  1 assertEquals(400, coords.getWidth());
102  1 assertEquals(1200, coords.getHeight());
103  1 assertEquals(600, coords.getX1());
104  1 assertEquals(1000, coords.getX2());
105  1 assertEquals(0, coords.getY1());
106  1 assertEquals(1200, coords.getY2());
107   
108  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
109  1 write(res);
110  1 assertEquals(200, res.getWidth());
111  1 assertEquals(600, res.getHeight());
112    }
113   
 
114  1 toggle @Test
115    public void testDoesNotCropIfOnlyTargetHeightIsSpecifiedAndRespectsOriginalRatio() throws Exception {
116  1 final AutoCropAndResize op = new AutoCropAndResize();
117  1 op.setTargetWidth(0);
118  1 op.setTargetHeight(300);
119   
120  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
121  1 assertEquals(1600, coords.getWidth());
122  1 assertEquals(1200, coords.getHeight());
123  1 assertEquals(0, coords.getX1());
124  1 assertEquals(1600, coords.getX2());
125  1 assertEquals(0, coords.getY1());
126  1 assertEquals(1200, coords.getY2());
127   
128  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
129  1 write(res);
130  1 assertEquals(400, res.getWidth());
131  1 assertEquals(300, res.getHeight());
132    }
133   
 
134  1 toggle @Test
135    public void testDoesNotCropIfOnlyTargetWidthIsSpecifiedAndRespectsOriginalRatio() throws Exception {
136  1 final AutoCropAndResize op = new AutoCropAndResize();
137  1 op.setTargetWidth(400);
138  1 op.setTargetHeight(0);
139   
140  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
141  1 assertEquals(1600, coords.getWidth());
142  1 assertEquals(1200, coords.getHeight());
143  1 assertEquals(0, coords.getX1());
144  1 assertEquals(1600, coords.getX2());
145  1 assertEquals(0, coords.getY1());
146  1 assertEquals(1200, coords.getY2());
147   
148  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
149  1 write(res);
150  1 assertEquals(400, res.getWidth());
151  1 assertEquals(300, res.getHeight());
152    }
153   
154    /**
155    * Tests with 1:1 ratio.
156    */
 
157  1 toggle @Test
158    public void testSquareTargetUsesLargestPossibleZoneForHorizontalSource() throws Exception {
159  1 final AutoCropAndResize op = new AutoCropAndResize();
160  1 op.setTargetWidth(400);
161  1 op.setTargetHeight(400);
162   
163  1 final Coords coords = op.getCroopCoords(getHorizontalTestImage(), null);
164  1 assertEquals(1200, coords.getWidth());
165  1 assertEquals(1200, coords.getHeight());
166  1 assertEquals(200, coords.getX1());
167  1 assertEquals(1400, coords.getX2());
168  1 assertEquals(0, coords.getY1());
169  1 assertEquals(1200, coords.getY2());
170   
171  1 final BufferedImage res = op.apply(getHorizontalTestImage(), null);
172  1 write(res);
173  1 assertEquals(400, res.getWidth());
174  1 assertEquals(400, res.getHeight());
175    }
176   
 
177  1 toggle @Test
178    public void testSquareTargetUsesLargestPossibleZoneForVerticalSourceToo() throws Exception {
179  1 final AutoCropAndResize op = new AutoCropAndResize();
180  1 op.setTargetWidth(400);
181  1 op.setTargetHeight(400);
182   
183  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
184  1 assertEquals(1200, coords.getWidth());
185  1 assertEquals(1200, coords.getHeight());
186  1 assertEquals(0, coords.getX1());
187  1 assertEquals(1200, coords.getX2());
188  1 assertEquals(200, coords.getY1());
189  1 assertEquals(1400, coords.getY2());
190   
191  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
192  1 write(res);
193  1 assertEquals(400, res.getWidth());
194  1 assertEquals(400, res.getHeight());
195    }
196   
197    /**
198    * Tests with vertical source.
199    */
 
200  1 toggle @Test
201    public void testVerticalSourceIsHandledJustAsWell() throws Exception {
202  1 final AutoCropAndResize op = new AutoCropAndResize();
203  1 op.setTargetWidth(400);
204  1 op.setTargetHeight(300);
205   
206  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
207  1 assertEquals(1200, coords.getWidth());
208  1 assertEquals(900, coords.getHeight());
209  1 assertEquals(0, coords.getX1());
210  1 assertEquals(1200, coords.getX2());
211  1 assertEquals(350, coords.getY1());
212  1 assertEquals(1250, coords.getY2());
213   
214  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
215  1 write(res);
216  1 assertEquals(400, res.getWidth());
217  1 assertEquals(300, res.getHeight());
218    }
219   
 
220  1 toggle @Test
221    public void testVerticalSourceWithVerticalRatioIsAlsoSwell() throws Exception {
222  1 final AutoCropAndResize op = new AutoCropAndResize();
223  1 op.setTargetWidth(150);
224  1 op.setTargetHeight(600);
225   
226  1 final Coords coords = op.getCroopCoords(getVerticalTestImage(), null);
227  1 assertEquals(400, coords.getWidth());
228  1 assertEquals(1600, coords.getHeight());
229  1 assertEquals(400, coords.getX1());
230  1 assertEquals(800, coords.getX2());
231  1 assertEquals(0, coords.getY1());
232  1 assertEquals(1600, coords.getY2());
233   
234  1 final BufferedImage res = op.apply(getVerticalTestImage(), null);
235  1 write(res);
236  1 assertEquals(150, res.getWidth());
237  1 assertEquals(600, res.getHeight());
238    }
239   
 
240  1 toggle @Test
241    public void testPhilippSawBlackBorders() throws Exception {
242  1 final AutoCropAndResize op = new AutoCropAndResize();
243  1 op.setTargetWidth(150);
244  1 op.setTargetHeight(100);
245   
246  1 final BufferedImage src = generateArbitraryTestImage(400, 200);
247   
248  1 final Coords coords = op.getCroopCoords(src, null);
249  1 assertEquals(300, coords.getWidth());
250  1 assertEquals(200, coords.getHeight());
251  1 assertEquals(50, coords.getX1());
252  1 assertEquals(350, coords.getX2());
253  1 assertEquals(0, coords.getY1());
254  1 assertEquals(200, coords.getY2());
255   
256  1 final BufferedImage res = op.apply(src, null);
257  1 write(res);
258  1 assertEquals(150, res.getWidth());
259  1 assertEquals(100, res.getHeight());
260    }
261   
 
262  1 toggle @Test
263    public void testThereWereVerticalBlackBordersToo() throws Exception {
264  1 final AutoCropAndResize op = new AutoCropAndResize();
265  1 op.setTargetWidth(100);
266  1 op.setTargetHeight(150);
267   
268  1 final BufferedImage src = generateArbitraryTestImage(200, 400);
269   
270  1 final Coords coords = op.getCroopCoords(src, null);
271  1 assertEquals(200, coords.getWidth());
272  1 assertEquals(300, coords.getHeight());
273  1 assertEquals(0, coords.getX1());
274  1 assertEquals(200, coords.getX2());
275  1 assertEquals(50, coords.getY1());
276  1 assertEquals(350, coords.getY2());
277   
278  1 final BufferedImage res = op.apply(src, null);
279  1 write(res);
280  1 assertEquals(100, res.getWidth());
281  1 assertEquals(150, res.getHeight());
282    }
283   
 
284  1 toggle @Test
285    public void testNoResizeIfWidthAndHeightAreNotSpecified() throws IOException {
286  1 doResizeTest(-1, -1, 16, 8);
287  1 doResizeTest(0, 0, 16, 8);
288  1 doResizeTest(0, 0, new Coords(5, 6, 10, 18), 5, 12);
289    }
290   
 
291  1 toggle @Test
292    public void testResizesProportionallyIfOnlyWidthIsSpecified() throws IOException {
293  1 doResizeTest(50, -1, 50, 25);
294  1 doResizeTest(8, -1, 8, 4);
295    }
296   
 
297  1 toggle @Test
298    public void testResizesProportionallyIfOnlyHeightIsSpecified() throws IOException {
299  1 doResizeTest(-1, 50, 100, 50);
300  1 doResizeTest(-1, 6, 12, 6);
301    }
302   
 
303  1 toggle @Test
304    public void testResizesUsingBothWidthAndHeightIfSpecified() throws IOException {
305  1 doResizeTest(30, 30, 30, 30);
306  1 doResizeTest(20, 50, 20, 50);
307    }
308   
 
309  8 toggle private void doResizeTest(int targetWidth, int targetHeight, int expectedWidth, int expectedHeight) throws IOException {
310  8 doResizeTest(targetWidth, targetHeight, new Coords(0, 0, 16, 8), expectedWidth, expectedHeight);
311    }
312   
 
313  9 toggle private void doResizeTest(int targetWidth, int targetHeight, Coords cropCoords, int expectedWidth, int expectedHeight) throws IOException {
314  9 final BufferedImage dummyImg = ImageIO.read(getClass().getResourceAsStream("/funnel.gif"));
315  9 final AutoCropAndResize op = new AutoCropAndResize();
316  9 op.setTargetWidth(targetWidth);
317  9 op.setTargetHeight(targetHeight);
318  9 final Size effectiveTargetSize = op.getEffectiveTargetSize(dummyImg, cropCoords, null);
319  9 final BufferedImage result = op.resize(dummyImg, cropCoords, effectiveTargetSize);
320  9 assertEquals(expectedWidth, result.getWidth());
321  9 assertEquals(expectedHeight, result.getHeight());
322    }
323   
324    }