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 
  26 import org.testng.annotations.AfterClass;
  27 import org.testng.annotations.AfterMethod;
  28 import org.testng.annotations.BeforeClass;
  29 import org.testng.annotations.BeforeMethod;
  30 import org.testng.annotations.Test;
  31 
  32 import java.awt.Color;
  33 import java.awt.Graphics2D;
  34 import java.awt.image.BufferedImage;
  35 import java.io.IOException;
  36 import javax.imageio.ImageIO;
  37 
  38 import static org.testng.Assert.assertNotNull;
  39 import static org.testng.Assert.assertNull;
  40 import static org.testng.AssertJUnit.assertEquals;
  41 
  42 
  43 /**
  44  *
  45  * @author KAM
  46  */
  47 public class AverageDistanceImageComparatorTest {
  48 
  49     public AverageDistanceImageComparatorTest() {
  50     }
  51 
  52     static final int NUMBER_OF_IMAGES = 3;
  53     static BufferedImage[] images;
  54 
  55     @BeforeClass
  56     public static void setUpClass() throws Exception {
  57         images = new BufferedImage[NUMBER_OF_IMAGES];
  58         for(int i = 0; i < NUMBER_OF_IMAGES; i++) {
  59             images[i] = ImageIO.read(AverageDistanceImageComparatorTest.class
  60                     .getResource("image" + (i + 1) + ".jpg"));
  61         }
  62     }
  63 
  64     @AfterClass
  65     public static void tearDownClass() throws Exception {
  66     }
  67 
  68     @BeforeMethod
  69     public void setUp() {
  70     }
  71 
  72     @AfterMethod
  73     public void tearDown() {
  74     }
  75 
  76     /**
  77      * Test of compare method, of class AverageDistanceImageComparator.
  78      */
  79     @Test
  80     public void testCompare1() {
  81         System.out.println("compare1");
  82         Graphics2D g;
  83 
  84         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  85         g = image1.createGraphics();
  86         g.setColor(new Color(0.5f, 0.5f, 0.5f));
  87         g.fillRect(0, 0, 10, 10);
  88         g.dispose();
  89 
  90         BufferedImage image2 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  91         g = image2.createGraphics();
  92         g.setColor(new Color(0.52f, 0.5f, 0.5f));
  93         g.fillRect(0, 0, 10, 10);
  94         g.dispose();
  95 
  96         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
  97         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image2)));
  98     }
  99 
 100     /**
 101      * Test of compare method, of class AverageDistanceImageComparator.
 102      */
 103     @Test
 104     public void testCompare2() {
 105         System.out.println("compare2");
 106         Graphics2D g;
 107 
 108         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 109         g = image1.createGraphics();
 110         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 111         g.fillRect(0, 0, 10, 10);
 112         g.dispose();
 113 
 114         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 115         g = image3.createGraphics();
 116         g.setColor(new Color(0.51f, 0.51f, 0.51f));
 117         g.fillRect(0, 0, 10, 10);
 118         g.dispose();
 119 
 120         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 121         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 122     }
 123 
 124     /**
 125      * Test of compare method, of class AverageDistanceImageComparator.
 126      */
 127     @Test
 128     public void testCompare3Pos() {
 129         System.out.println("compare3Pos");
 130         Graphics2D g;
 131 
 132         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 133         g = image1.createGraphics();
 134         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 135         g.fillRect(0, 0, 10, 10);
 136         g.dispose();
 137 
 138         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 139         g = image3.createGraphics();
 140         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 141         g.fillRect(0, 0, 10, 10);
 142         g.setColor(new Color(0.6f, 0.6f, 0.6f));
 143         g.fillRect(3, 3, 3, 3);
 144         g.dispose();
 145 
 146         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 147         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 148     }
 149 
 150     /**
 151      * Test of compare method, of class AverageDistanceImageComparator.
 152      */
 153     @Test
 154     public void testCompare3Neg() {
 155         System.out.println("compare3Neg");
 156         Graphics2D g;
 157 
 158         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 159         g = image1.createGraphics();
 160         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 161         g.fillRect(0, 0, 10, 10);
 162         g.dispose();
 163 
 164         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 165         g = image3.createGraphics();
 166         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 167         g.fillRect(0, 0, 10, 10);
 168         g.setColor(new Color(0.6f, 0.6f, 0.6f));
 169         g.fillRect(3, 3, 3, 4);
 170         g.dispose();
 171 
 172         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 173         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 174     }
 175 
 176     /**
 177      * Test of compare method, of class AverageDistanceImageComparator.
 178      */
 179     @Test
 180     public void testCompare4() {
 181         System.out.println("compare4");
 182         Graphics2D g;
 183 
 184         BufferedImage image1 = new BufferedImage(10, 11, BufferedImage.TYPE_INT_RGB);
 185         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 186 
 187         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 188         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 189     }
 190 
 191     /**
 192      * Test of compare method, of class AverageDistanceImageComparator.
 193      */
 194     @Test
 195     public void testCompare5() {
 196         System.out.println("compare5");
 197         Graphics2D g;
 198 
 199         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 200         BufferedImage image3 = new BufferedImage(11, 10, BufferedImage.TYPE_INT_RGB);
 201 
 202         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 203         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 204     }
 205 
 206     /**
 207      * Test of compare method, of class AverageDistanceImageComparator.
 208      */
 209     @Test
 210     public void testCompare6() {
 211         System.out.println("compare6");
 212         Graphics2D g;
 213 
 214         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 215         g = image1.createGraphics();
 216         g.setColor(new Color(0, 0, 0));
 217         g.fillRect(0, 0, 10, 10);
 218         g.dispose();
 219 
 220         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 221         g = image3.createGraphics();
 222         g.setColor(new Color(255, 255, 255));
 223         g.fillRect(0, 0, 10, 10);
 224         g.dispose();
 225 
 226         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 227         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 228     }
 229 
 230     /**
 231      * Test of compare method, of class AverageDistanceImageComparator.
 232      */
 233     @Test
 234     public void testCompare7() {
 235         System.out.println("compare7");
 236         Graphics2D g;
 237 
 238         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 239         g = image1.createGraphics();
 240         g.setColor(new Color(0.51f, 0.51f, 0.51f));
 241         g.fillRect(0, 0, 10, 5);
 242         g.setColor(new Color(0.49f, 0.49f, 0.49f));
 243         g.fillRect(0, 5, 10, 5);
 244         g.dispose();
 245 
 246         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 247         g = image3.createGraphics();
 248         g.setColor(new Color(0.49f, 0.49f, 0.49f));
 249         g.fillRect(0, 5, 10, 5);
 250         g.setColor(new Color(0.51f, 0.51f, 0.51f));
 251         g.fillRect(0, 0, 10, 5);
 252         g.dispose();
 253 
 254         AverageDistanceImageComparator instance = new AverageDistanceImageComparator();
 255         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 256     }
 257 
 258     /**
 259      * Test of compare method, of class AverageDistanceImageComparator.
 260      * @throws IOException
 261      */
 262     @Test
 263     public void testCompare() throws IOException {
 264         System.out.println("compare");
 265         boolean[][][] expected = {
 266             // NO_RESIZE
 267             {
 268                 { true, false, false },
 269                 { false, true, false },
 270                 { false, false, true }
 271             },
 272             // PROPORTIONAL_RESIZE
 273             {
 274                 { true, true, false },
 275                 { true, true, false },
 276                 { false, false, true }
 277             },
 278             // ARBITRARY_RESIZE
 279             {
 280                 { true, true, true },
 281                 { true, true, true },
 282                 { true, true, true }
 283             }
 284         };
 285         for(int i = 0; i < NUMBER_OF_IMAGES; i++) {
 286             BufferedImage image1 = images[i];
 287             for(int j = i; j < NUMBER_OF_IMAGES; j++) {
 288                 System.out.println("Comparing " + i + " to " + j);
 289                 BufferedImage image2 = images[j];
 290                 System.out.println("\nimage " + i + " " + image1.getWidth() +
 291                         "x" + image1.getHeight());
 292                 System.out.println("image " + j + " " + image2.getWidth() + "x"
 293                         + image2.getHeight());
 294                 for(ResizeImageComparator.ResizeMode resizeMode :
 295                         ResizeImageComparator.ResizeMode.values()) {
 296 
 297                     System.out.println("\n " + resizeMode);
 298                     AWTImage.setComparator(new ResizeImageComparator(resizeMode,
 299                             new AverageDistanceImageComparator(0.024)));
 300                     Image awtImage1 = new AWTImage(image1);
 301                     Image awtImage2 = new AWTImage(image2);
 302                     boolean expResult = expected[resizeMode.ordinal()][i][j];
 303                     Image diff = awtImage1.compareTo(awtImage2);
 304                     boolean result = diff == null;
 305                     if (diff != null) {
 306                         diff.save("diff" + i + j + resizeMode + ".png");
 307                     }
 308                     assertEquals("Failed comparison for image " + i + " with " +
 309                             "image " + j + ", resizeMode = " + resizeMode,
 310                             expResult, result);
 311                 }
 312             }
 313         }
 314     }
 315 
 316 }