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 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 java.awt.Color;
  34 import java.awt.Graphics2D;
  35 import java.awt.image.BufferedImage;
  36 import java.awt.image.DataBufferInt;
  37 import java.io.IOException;
  38 import javax.imageio.ImageIO;
  39 
  40 import static org.testng.AssertJUnit.assertNotNull;
  41 import static org.testng.Assert.assertNull;
  42 import static org.testng.Assert.fail;
  43 import static org.testng.AssertJUnit.assertEquals;
  44 
  45 /**
  46  *
  47  * @author KAM
  48  */
  49 public class NaturalImageComparatorTest {
  50 
  51     public NaturalImageComparatorTest() {
  52     }
  53     static final int NUMBER_OF_IMAGES = 3;
  54     static BufferedImage[] images;
  55 
  56     @BeforeClass
  57     public static void setUpClass() throws Exception {
  58         images = new BufferedImage[NUMBER_OF_IMAGES];
  59         for (int i = 0; i < NUMBER_OF_IMAGES; i++) {
  60             images[i] = ImageIO.read(NaturalImageComparatorTest.class.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 NaturalImageComparator.
  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         NaturalImageComparator instance = new NaturalImageComparator();
  97         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image2)));
  98     }
  99 
 100     /**
 101      * Test of compare method, of class NaturalImageComparator.
 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         NaturalImageComparator instance = new NaturalImageComparator();
 121         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 122     }
 123 
 124     /**
 125      * Test of compare method, of class NaturalImageComparator.
 126      */
 127     @Test
 128     public void testCompare3() {
 129         System.out.println("compare3");
 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.53f, 0.5f, 0.5f));
 143         g.fillRect(3, 3, 1, 1);
 144         g.dispose();
 145 
 146         NaturalImageComparator instance = new NaturalImageComparator();
 147         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 148     }
 149 
 150     /**
 151      * Test of compare method, of class NaturalImageComparator.
 152      */
 153     @Test
 154     public void testCompare4() {
 155         System.out.println("compare4");
 156         Graphics2D g;
 157 
 158         BufferedImage image1 = new BufferedImage(10, 11, BufferedImage.TYPE_INT_RGB);
 159         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 160 
 161         NaturalImageComparator instance = new NaturalImageComparator();
 162         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 163     }
 164 
 165     /**
 166      * Test of compare method, of class NaturalImageComparator.
 167      */
 168     @Test
 169     public void testCompare5() {
 170         System.out.println("compare5");
 171         Graphics2D g;
 172 
 173         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 174         BufferedImage image3 = new BufferedImage(11, 10, BufferedImage.TYPE_INT_RGB);
 175 
 176         NaturalImageComparator instance = new NaturalImageComparator();
 177         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 178     }
 179 
 180     /**
 181      * Test of compare method, of class NaturalImageComparator.
 182      *
 183      * @throws IOException
 184      */
 185     @Test
 186     public void testCompare() throws IOException {
 187         System.out.println("compare");
 188         boolean[][][] expected = {
 189             // NO_RESIZE
 190             {
 191                 {true, false, false},
 192                 {false, true, false},
 193                 {false, false, true}
 194             },
 195             // PROPORTIONAL_RESIZE
 196             {
 197                 {true, true, false},
 198                 {true, true, false},
 199                 {false, false, true}
 200             },
 201             // ARBITRARY_RESIZE
 202             {
 203                 {true, true, true},
 204                 {true, true, true},
 205                 {true, true, true}
 206             }
 207         };
 208         for (int i = 0; i < NUMBER_OF_IMAGES; i++) {
 209             BufferedImage image1 = images[i];
 210             for (int j = i; j < NUMBER_OF_IMAGES; j++) {
 211                 BufferedImage image2 = images[j];
 212                 System.out.println("\nimage " + i + " " + image1.getWidth()
 213                         + "x" + image1.getHeight());
 214                 System.out.println("image " + j + " " + image2.getWidth() + "x"
 215                         + image2.getHeight());
 216                 for (ResizeImageComparator.ResizeMode resizeMode :
 217                         ResizeImageComparator.ResizeMode.values()) {
 218 
 219                     System.out.println("\n " + resizeMode);
 220                     AWTImage.setComparator(new ResizeImageComparator(resizeMode,
 221                             new NaturalImageComparator(1.1)));
 222                     Image awtImage1 = new AWTImage(image1);
 223                     Image awtImage2 = new AWTImage(image2);
 224                     boolean expResult = expected[resizeMode.ordinal()][i][j];
 225                     Image diff = awtImage1.compareTo(awtImage2);
 226                     boolean result = diff == null;
 227                     if (diff != null) {
 228                         diff.save("diff" + i + j + resizeMode + ".png");
 229                     }
 230                     assertEquals("Failed comparison for image " + i + " with "
 231                             + "image " + j + ", resizeMode = " + resizeMode,
 232                             expResult, result);
 233                 }
 234             }
 235         }
 236     }
 237 
 238     @Test
 239     public void testFXDiff() {
 240         System.out.println("fxDiff");
 241         AWTImage.setComparator(new NaturalImageComparator());
 242         ClasspathImageLoader cil = new ClasspathImageLoader();
 243         cil.setClassLoader(this.getClass().getClassLoader());
 244         cil.setRootPackage(this.getClass().getPackage());
 245         Image im1 = cil.load("AreaChart_a.png");
 246         Image im2 = cil.load("AreaChart_a_res.png");
 247         Image diff = im1.compareTo(im2);
 248         if (diff != null) {
 249             diff.save("testFXDiff_1to2.png");
 250             checkDiff(diff);
 251         }
 252         assertNotNull("Images has to be different", diff);
 253 
 254         diff = im2.compareTo(im1);
 255         if (diff != null) {
 256             diff.save("testFXDiff_2to1.png");
 257             checkDiff(diff);
 258         }
 259         assertNotNull("Images has to be different", diff);
 260     }
 261 
 262     /**
 263      * http://javafx-jira.kenai.com/browse/JMY-202
 264      * Jemmy produces completely black diffs in some cases
 265      */
 266     @Test
 267     public void testFXDiff2() {
 268         System.out.println("fxDiff2");
 269         AWTImage.setComparator(new NaturalImageComparator());
 270         ClasspathImageLoader cil = new ClasspathImageLoader();
 271         cil.setClassLoader(this.getClass().getClassLoader());
 272         cil.setRootPackage(this.getClass().getPackage());
 273 
 274         BufferedImage img1 = ((AWTImage) cil.load("AreaChart_a.png")).getTheImage();
 275         BufferedImage img3 = new BufferedImage(img1.getWidth(), img1.getHeight(), 3);
 276         for (int x = 0; x < img1.getWidth(); x++) {
 277             for (int y = 0; y < img1.getHeight(); y++) {
 278                 img3.setRGB(x, y, img1.getRGB(x, y));
 279             }
 280         }
 281         Image im1 = new AWTImage(img3);
 282         Image im2 = cil.load("AreaChart_a_res.png");
 283         Image diff = im1.compareTo(im2);
 284         if (diff != null) {
 285             diff.save("testFXDiff2_1to2.png");
 286             checkDiff(diff);
 287         }
 288         assertNotNull("Images has to be different", diff);
 289 
 290         diff = im2.compareTo(im1);
 291         if (diff != null) {
 292             diff.save("testFXDiff2_2to1.png");
 293             checkDiff(diff);
 294         }
 295         assertNotNull("Images has to be different", diff);
 296     }
 297 
 298     private void checkDiff(Image diff) {
 299         BufferedImage im = ((AWTImage) diff).getTheImage();
 300         int[] data = ((DataBufferInt) im.getRaster().getDataBuffer()).getData();
 301         for (int d : data) {
 302             if ((d & 0xffffff) != 0) {
 303                 System.out.println("d = " + Integer.toBinaryString(d));
 304                 return;
 305             }
 306             if (d != 0) {
 307                 System.out.println("d = " + Integer.toBinaryString(d));
 308             }
 309         }
 310         fail("Diff is completely black");
 311     }
 312 }