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