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