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.awt;
  26 
  27 import org.jemmy.image.Image;
  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.awt.image.DataBufferInt;
  38 import java.io.IOException;
  39 import javax.imageio.ImageIO;
  40 
  41 import static org.testng.AssertJUnit.assertNotNull;
  42 import static org.testng.Assert.assertNull;
  43 import static org.testng.Assert.fail;
  44 import static org.testng.AssertJUnit.assertEquals;
  45 
  46 /**
  47  *
  48  * @author KAM
  49  */
  50 public class NaturalImageComparatorTest {
  51 
  52     public NaturalImageComparatorTest() {
  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(NaturalImageComparatorTest.class.getResource("image" + (i + 1) + ".jpg"));
  62         }
  63     }
  64 
  65     @AfterClass
  66     public static void tearDownClass() throws Exception {
  67     }
  68 
  69     @BeforeMethod
  70     public void setUp() {
  71     }
  72 
  73     @AfterMethod
  74     public void tearDown() {
  75     }
  76 
  77     /**
  78      * Test of compare method, of class NaturalImageComparator.
  79      */
  80     @Test
  81     public void testCompare1() {
  82         System.out.println("compare1");
  83         Graphics2D g;
  84 
  85         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  86         g = image1.createGraphics();
  87         g.setColor(new Color(0.5f, 0.5f, 0.5f));
  88         g.fillRect(0, 0, 10, 10);
  89         g.dispose();
  90 
  91         BufferedImage image2 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
  92         g = image2.createGraphics();
  93         g.setColor(new Color(0.52f, 0.5f, 0.5f));
  94         g.fillRect(0, 0, 10, 10);
  95         g.dispose();
  96 
  97         NaturalImageComparator instance = new NaturalImageComparator();
  98         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image2)));
  99     }
 100 
 101     /**
 102      * Test of compare method, of class NaturalImageComparator.
 103      */
 104     @Test
 105     public void testCompare2() {
 106         System.out.println("compare2");
 107         Graphics2D g;
 108 
 109         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 110         g = image1.createGraphics();
 111         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 112         g.fillRect(0, 0, 10, 10);
 113         g.dispose();
 114 
 115         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 116         g = image3.createGraphics();
 117         g.setColor(new Color(0.51f, 0.51f, 0.51f));
 118         g.fillRect(0, 0, 10, 10);
 119         g.dispose();
 120 
 121         NaturalImageComparator instance = new NaturalImageComparator();
 122         assertNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 123     }
 124 
 125     /**
 126      * Test of compare method, of class NaturalImageComparator.
 127      */
 128     @Test
 129     public void testCompare3() {
 130         System.out.println("compare3");
 131         Graphics2D g;
 132 
 133         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 134         g = image1.createGraphics();
 135         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 136         g.fillRect(0, 0, 10, 10);
 137         g.dispose();
 138 
 139         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 140         g = image3.createGraphics();
 141         g.setColor(new Color(0.5f, 0.5f, 0.5f));
 142         g.fillRect(0, 0, 10, 10);
 143         g.setColor(new Color(0.53f, 0.5f, 0.5f));
 144         g.fillRect(3, 3, 1, 1);
 145         g.dispose();
 146 
 147         NaturalImageComparator instance = new NaturalImageComparator();
 148         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 149     }
 150 
 151     /**
 152      * Test of compare method, of class NaturalImageComparator.
 153      */
 154     @Test
 155     public void testCompare4() {
 156         System.out.println("compare4");
 157         Graphics2D g;
 158 
 159         BufferedImage image1 = new BufferedImage(10, 11, BufferedImage.TYPE_INT_RGB);
 160         BufferedImage image3 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 161 
 162         NaturalImageComparator instance = new NaturalImageComparator();
 163         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 164     }
 165 
 166     /**
 167      * Test of compare method, of class NaturalImageComparator.
 168      */
 169     @Test
 170     public void testCompare5() {
 171         System.out.println("compare5");
 172         Graphics2D g;
 173 
 174         BufferedImage image1 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
 175         BufferedImage image3 = new BufferedImage(11, 10, BufferedImage.TYPE_INT_RGB);
 176 
 177         NaturalImageComparator instance = new NaturalImageComparator();
 178         assertNotNull(instance.compare(new AWTImage(image1), new AWTImage(image3)));
 179     }
 180 
 181     /**
 182      * Test of compare method, of class NaturalImageComparator.
 183      *
 184      * @throws IOException
 185      */
 186     @Test
 187     public void testCompare() throws IOException {
 188         System.out.println("compare");
 189         boolean[][][] expected = {
 190             // NO_RESIZE
 191             {
 192                 {true, false, false},
 193                 {false, true, false},
 194                 {false, false, true}
 195             },
 196             // PROPORTIONAL_RESIZE
 197             {
 198                 {true, true, false},
 199                 {true, true, false},
 200                 {false, false, true}
 201             },
 202             // ARBITRARY_RESIZE
 203             {
 204                 {true, true, true},
 205                 {true, true, true},
 206                 {true, true, true}
 207             }
 208         };
 209         for (int i = 0; i < NUMBER_OF_IMAGES; i++) {
 210             BufferedImage image1 = images[i];
 211             for (int j = i; j < NUMBER_OF_IMAGES; j++) {
 212                 BufferedImage image2 = images[j];
 213                 System.out.println("\nimage " + i + " " + image1.getWidth()
 214                         + "x" + image1.getHeight());
 215                 System.out.println("image " + j + " " + image2.getWidth() + "x"
 216                         + image2.getHeight());
 217                 for (ResizeImageComparator.ResizeMode resizeMode :
 218                         ResizeImageComparator.ResizeMode.values()) {
 219 
 220                     System.out.println("\n " + resizeMode);
 221                     AWTImage.setComparator(new ResizeImageComparator(resizeMode,
 222                             new NaturalImageComparator(1.1)));
 223                     Image awtImage1 = new AWTImage(image1);
 224                     Image awtImage2 = new AWTImage(image2);
 225                     boolean expResult = expected[resizeMode.ordinal()][i][j];
 226                     Image diff = awtImage1.compareTo(awtImage2);
 227                     boolean result = diff == null;
 228                     if (diff != null) {
 229                         diff.save("diff" + i + j + resizeMode + ".png");
 230                     }
 231                     assertEquals("Failed comparison for image " + i + " with "
 232                             + "image " + j + ", resizeMode = " + resizeMode,
 233                             expResult, result);
 234                 }
 235             }
 236         }
 237     }
 238 
 239     @Test
 240     public void testFXDiff() {
 241         System.out.println("fxDiff");
 242         AWTImage.setComparator(new NaturalImageComparator());
 243         ClasspathImageLoader cil = new ClasspathImageLoader();
 244         cil.setClassLoader(this.getClass().getClassLoader());
 245         cil.setRootPackage(this.getClass().getPackage());
 246         Image im1 = cil.load("AreaChart_a.png");
 247         Image im2 = cil.load("AreaChart_a_res.png");
 248         Image diff = im1.compareTo(im2);
 249         if (diff != null) {
 250             diff.save("testFXDiff_1to2.png");
 251             checkDiff(diff);
 252         }
 253         assertNotNull("Images has to be different", diff);
 254 
 255         diff = im2.compareTo(im1);
 256         if (diff != null) {
 257             diff.save("testFXDiff_2to1.png");
 258             checkDiff(diff);
 259         }
 260         assertNotNull("Images has to be different", diff);
 261     }
 262 
 263     /**
 264      * http://javafx-jira.kenai.com/browse/JMY-202
 265      * Jemmy produces completely black diffs in some cases
 266      */
 267     @Test
 268     public void testFXDiff2() {
 269         System.out.println("fxDiff2");
 270         AWTImage.setComparator(new NaturalImageComparator());
 271         ClasspathImageLoader cil = new ClasspathImageLoader();
 272         cil.setClassLoader(this.getClass().getClassLoader());
 273         cil.setRootPackage(this.getClass().getPackage());
 274 
 275         BufferedImage img1 = ((AWTImage) cil.load("AreaChart_a.png")).getTheImage();
 276         BufferedImage img3 = new BufferedImage(img1.getWidth(), img1.getHeight(), 3);
 277         for (int x = 0; x < img1.getWidth(); x++) {
 278             for (int y = 0; y < img1.getHeight(); y++) {
 279                 img3.setRGB(x, y, img1.getRGB(x, y));
 280             }
 281         }
 282         Image im1 = new AWTImage(img3);
 283         Image im2 = cil.load("AreaChart_a_res.png");
 284         Image diff = im1.compareTo(im2);
 285         if (diff != null) {
 286             diff.save("testFXDiff2_1to2.png");
 287             checkDiff(diff);
 288         }
 289         assertNotNull("Images has to be different", diff);
 290 
 291         diff = im2.compareTo(im1);
 292         if (diff != null) {
 293             diff.save("testFXDiff2_2to1.png");
 294             checkDiff(diff);
 295         }
 296         assertNotNull("Images has to be different", diff);
 297     }
 298 
 299     private void checkDiff(Image diff) {
 300         BufferedImage im = ((AWTImage) diff).getTheImage();
 301         int[] data = ((DataBufferInt) im.getRaster().getDataBuffer()).getData();
 302         for (int d : data) {
 303             if ((d & 0xffffff) != 0) {
 304                 System.out.println("d = " + Integer.toBinaryString(d));
 305                 return;
 306             }
 307             if (d != 0) {
 308                 System.out.println("d = " + Integer.toBinaryString(d));
 309             }
 310         }
 311         fail("Diff is completely black");
 312     }
 313 }