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.comparator;
  24 
  25 import java.awt.Color;
  26 import java.io.File;
  27 import java.awt.image.BufferedImage;
  28 import java.io.FileNotFoundException;
  29 import java.io.IOException;
  30 import org.jemmy.image.*;
  31 import org.testng.annotations.AfterClass;
  32 import org.testng.annotations.AfterMethod;
  33 import org.testng.annotations.BeforeClass;
  34 import org.testng.annotations.BeforeMethod;
  35 import org.testng.annotations.Test;
  36 
  37 import static org.testng.Assert.assertNotNull;
  38 import static org.testng.Assert.fail;
  39 
  40 /**
  41  *
  42  * @author shura
  43  */
  44 public class ComparatorTest {
  45 
  46     public ComparatorTest() {
  47     }
  48 
  49     @BeforeClass
  50     public static void setUpClass() throws Exception {
  51     }
  52 
  53     @AfterClass
  54     public static void tearDownClass() throws Exception {
  55     }
  56 
  57     @BeforeMethod
  58     public void setUp() {
  59     }
  60 
  61     @AfterMethod
  62     public void tearDown() {
  63     }
  64 
  65     // TODO add test methods here.
  66     // The methods must be annotated with annotation @Test. For example:
  67     //
  68     @Test
  69     public void average() {
  70         BufferedImage golden =
  71                 PNGDecoder.decode(getClass().getClassLoader().
  72                 getResourceAsStream("org/jemmy/image/comparator/golden-averagedistance.png"), true);
  73         BufferedImage actual =
  74                 PNGDecoder.decode(getClass().getClassLoader().
  75                 getResourceAsStream("org/jemmy/image/comparator/actual-averagedistance.png"), true);
  76         assertNotNull(new AverageDistanceImageComparator(.001).compare(new AWTImage(golden), new AWTImage(actual)));
  77     }
  78 
  79     @Test
  80     public void strict() {
  81         BufferedImage golden =
  82                 PNGDecoder.decode(getClass().getClassLoader().
  83                 getResourceAsStream("org/jemmy/image/comparator/golden-strict.png"), true);
  84         BufferedImage actual =
  85                 PNGDecoder.decode(getClass().getClassLoader().
  86                 getResourceAsStream("org/jemmy/image/comparator/actual-strict.png"), true);
  87         assertNotNull(new StrictImageComparator().compare(new AWTImage(golden), new AWTImage(actual)));
  88     }
  89 
  90     @Test
  91     public void strict_hyperlink() throws FileNotFoundException, IOException {
  92         BufferedImage golden =
  93                 PNGDecoder.decode(getClass().getClassLoader().
  94                 getResourceAsStream("org/jemmy/image/comparator/golden-hyperlink.png"), true);
  95         BufferedImage actual =
  96                 PNGDecoder.decode(getClass().getClassLoader().
  97                 getResourceAsStream("org/jemmy/image/comparator/actual-hyperlink.png"), true);
  98         BufferedImage diff = ((AWTImage)new StrictImageComparator().compare(new AWTImage(golden), new AWTImage(actual))).getTheImage();
  99         //assertNull(diff);
 100         //barbashov sucks!
 101         //he submits similar images
 102         new PNGEncoder(new File("/tmp/aaa.png")).encode(diff);
 103         assertNotNull(diff);
 104         for (int x = 0; x < diff.getWidth(); x++) {
 105             for (int y = 0; y < diff.getHeight(); y++) {
 106                 /*
 107                 if(new Color(golden.getRGB(x, y)).getAlpha() != 255) {
 108                     System.out.println("Haha!");
 109                 }
 110                 if(new Color(actual.getRGB(x, y)).getAlpha() != 255) {
 111                     System.out.println("Haha!");
 112                 }
 113                  *
 114                  */
 115                 Color color = new Color(diff.getRGB(x, y));
 116                 if (color.getRed() != 0 || color.getGreen() != 0 || color.getBlue() != 0) {
 117                     return;
 118                 }
 119             }
 120         }
 121         fail("There got to be non black pixels.");
 122     }
 123 }