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