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