1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.image;
  26 
  27 import java.awt.image.BufferedImage;
  28 import org.jemmy.image.ResizeImageComparator.ResizeMode;
  29 import org.testng.annotations.AfterClass;
  30 import org.testng.annotations.AfterMethod;
  31 import org.testng.annotations.BeforeClass;
  32 import org.testng.annotations.BeforeMethod;
  33 import org.testng.annotations.Test;
  34 
  35 import static org.testng.Assert.assertNull;
  36 import static org.testng.AssertJUnit.assertEquals;
  37 
  38 /**
  39  *
  40  * @author mrkam
  41  */
  42 public class ImageResizerTest {
  43 
  44     public ImageResizerTest() {
  45     }
  46 
  47     @BeforeClass
  48     public static void setUpClass() throws Exception {
  49     }
  50 
  51     @AfterClass
  52     public static void tearDownClass() throws Exception {
  53     }
  54 
  55     @BeforeMethod
  56     public void setUp() {
  57     }
  58 
  59     @AfterMethod
  60     public void tearDown() {
  61     }
  62 
  63     /**
  64      * Test of default value of ResizeMode, of class ImageResizer.
  65      */
  66     @Test
  67     public void testDefaultResizeMode() {
  68         System.out.println("testDefaultResizeMode");
  69         ResizeMode expResult = ResizeMode.PROPORTIONAL_RESIZE;
  70         ResizeMode result = new ResizeImageComparator(null).resizeMode;
  71         assertEquals(expResult, result);
  72     }
  73 
  74     /**
  75      * Test of setDefaultResizeMode method, of class ImageResizer.
  76      */
  77     @Test
  78     public void testProportionalResizeMode() {
  79         System.out.println("testProportionalResizeMode");
  80 
  81         new ResizeImageComparator(new ImageComparator() {
  82 
  83             public Image compare(Image image1,
  84                     Image image2) {
  85                 assertEquals(50, ((AWTImage) image2).getSize().width);
  86                 assertEquals(50, ((AWTImage) image2).getSize().height);
  87                 return null;
  88             }
  89 
  90             public String getID() {
  91                 return "test";
  92             }
  93         }).compare(new AWTImage(new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB)),
  94                 new AWTImage(new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB)));
  95     }
  96 
  97     /**
  98      * Test of getResized method, of class ImageResizer.
  99      */
 100     @Test
 101     public void testGetResizedNoResize() {
 102         System.out.println("getResizedNoResize");
 103         final AWTImage image1 =
 104                 new AWTImage(new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB));
 105         final AWTImage image2 =
 106                 new AWTImage(new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB));
 107 
 108         new ResizeImageComparator(ResizeMode.NO_RESIZE, new ImageComparator() {
 109 
 110             public Image compare(Image im1,
 111                     Image im2) {
 112                 assertEquals(image1, im1);
 113                 assertEquals(image2, im2);
 114                 return null;
 115             }
 116 
 117             public String getID() {
 118                 return "test";
 119             }
 120         }).compare(image1, image2);
 121     }
 122 
 123     /**
 124      * Test of getResized method, of class ImageResizer.
 125      */
 126     @Test
 127     public void testGetResizedProportional1() {
 128         System.out.println("getResizedProportional1");
 129         BufferedImage image1 =
 130                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 131         BufferedImage image2 =
 132                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 133         assertNull(new ResizeImageComparator(ResizeMode.PROPORTIONAL_RESIZE,
 134                 new StrictImageComparator()).compare(new AWTImage(image1), new AWTImage(image2)));
 135     }
 136 
 137     /**
 138      * Test of getResized method, of class ImageResizer.
 139      */
 140     @Test
 141     public void testGetResizedProportional2() {
 142         System.out.println("getResizedProportional2");
 143         BufferedImage image1 =
 144                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 145         BufferedImage image2 =
 146                 new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB);
 147         assertNull(new ResizeImageComparator(ResizeMode.PROPORTIONAL_RESIZE,
 148                 new StrictImageComparator()).compare(new AWTImage(image1), new AWTImage(image2)));
 149     }
 150 
 151     /**
 152      * Test of getResized method, of class ImageResizer.
 153      */
 154     @Test
 155     public void testGetResizedProportional3() {
 156         System.out.println("getResizedProportional3");
 157         final AWTImage image1 =
 158                 new AWTImage(new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB));
 159         final AWTImage image2 =
 160                 new AWTImage(new BufferedImage(10, 15, BufferedImage.TYPE_INT_RGB));
 161         new ResizeImageComparator(ResizeMode.PROPORTIONAL_RESIZE,
 162                 new ImageComparator() {
 163 
 164                     public Image compare(Image im1,
 165                             Image im2) {
 166                         assertEquals(image1, im1);
 167                         assertEquals(image2, im2);
 168                         return null;
 169                     }
 170 
 171                     public String getID() {
 172                         return "test";
 173                     }
 174                 }).compare(image1, image2);
 175     }
 176 
 177     /**
 178      * Test of getResized method, of class ImageResizer.
 179      */
 180     @Test
 181     public void testGetResizedArbitrary1() {
 182         System.out.println("getResizedArbitrary1");
 183         BufferedImage image1 =
 184                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 185         BufferedImage image2 =
 186                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 187         assertNull(new ResizeImageComparator(ResizeMode.ARBITRARY_RESIZE,
 188                 new StrictImageComparator()).compare(new AWTImage(image1), new AWTImage(image2)));
 189     }
 190 
 191     /**
 192      * Test of getResized method, of class ImageResizer.
 193      */
 194     @Test
 195     public void testGetResizedArbitrary2() {
 196         System.out.println("getResizedArbitrary2");
 197         BufferedImage image1 =
 198                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 199         BufferedImage image2 =
 200                 new BufferedImage(15, 15, BufferedImage.TYPE_INT_RGB);
 201         assertNull(new ResizeImageComparator(ResizeMode.ARBITRARY_RESIZE,
 202                 new StrictImageComparator()).compare(new AWTImage(image1), new AWTImage(image2)));
 203     }
 204 
 205     /**
 206      * Test of getResized method, of class ImageResizer.
 207      */
 208     @Test
 209     public void testGetResizedArbitrary3() {
 210         System.out.println("getResizedArbitrary3");
 211         BufferedImage image1 =
 212                 new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 213         BufferedImage image2 =
 214                 new BufferedImage(10, 15, BufferedImage.TYPE_INT_RGB);
 215         assertNull(new ResizeImageComparator(ResizeMode.ARBITRARY_RESIZE,
 216                 new StrictImageComparator()).compare(new AWTImage(image1), new AWTImage(image2)));
 217     }
 218 }