001    /*
002     * Created on Oct 20, 2010
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     * 
007     * http://www.apache.org/licenses/LICENSE-2.0
008     * 
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     * 
013     * Copyright @2010-2011 the original author or authors.
014     */
015    package org.fest.assertions.api;
016    
017    import java.awt.Dimension;
018    import java.awt.image.BufferedImage;
019    import java.util.Comparator;
020    
021    import org.fest.assertions.data.Offset;
022    import org.fest.assertions.internal.Images;
023    import org.fest.util.VisibleForTesting;
024    
025    /**
026     * Assertion methods for images.
027     * <p>
028     * To create an instance of this class, invoke <code>{@link Assertions#assertThat(BufferedImage)}</code>.
029     * </p>
030     * 
031     * @author Yvonne Wang
032     * @author Alex Ruiz
033     * @author Ansgar Konermann
034     * @author Joel Costigliola
035     * @author Mikhail Mazursky
036     */
037    public class ImageAssert extends AbstractAssert<ImageAssert, BufferedImage> {
038    
039      @VisibleForTesting
040      Images images = Images.instance();
041    
042      protected ImageAssert(BufferedImage actual) {
043        super(actual, ImageAssert.class);
044      }
045    
046      /**
047       * Verifies that the actual image is equal to the given one. Two images are equal if:
048       * <ol>
049       * <li>they have equal size</li>
050       * <li>the the RGB values of the color at each pixel are equal</li>
051       * </ol>
052       * @param expected the given image to compare the actual image to.
053       * @return {@code this} assertion object.
054       * @throws AssertionError if the actual image is not equal to the given one.
055       */
056      @Override
057      public ImageAssert isEqualTo(BufferedImage expected) {
058        images.assertEqual(info, actual, expected);
059        return this;
060      }
061    
062      /**
063       * Verifies that the actual image is equal to the given one. Two images are equal if:
064       * <ol>
065       * <li>they have the same size</li>
066       * <li>the difference between the RGB values of the color at each pixel is less than or equal to the given offset</li>
067       * </ol>
068       * @param expected the given image to compare the actual image to.
069       * @param offset helps decide if the color of two pixels are similar: two pixels that are identical to the human eye
070       *          may still have slightly different color values. For example, by using an offset of 1 we can indicate that
071       *          a blue value of 60 is similar to a blue value of 61.
072       * @return {@code this} assertion object.
073       * @throws NullPointerException if the given offset is {@code null}.
074       * @throws AssertionError if the actual image is not equal to the given one.
075       */
076      public ImageAssert isEqualTo(BufferedImage expected, Offset<Integer> offset) {
077        images.assertEqual(info, actual, expected, offset);
078        return this;
079      }
080    
081      /** {@inheritDoc} */
082      @Override
083      public ImageAssert isNotEqualTo(BufferedImage other) {
084        images.assertNotEqual(info, actual, other);
085        return this;
086      }
087    
088      /**
089       * Verifies that the actual image has the given size.
090       * @param expected the expected size of the actual image.
091       * @return {@code this} assertion object.
092       * @throws NullPointerException if the given size is {@code null}.
093       * @throws AssertionError if the size of the actual image is not equal to the given size.
094       */
095      public ImageAssert hasSize(Dimension expected) {
096        images.assertHasSize(info, actual, expected);
097        return this;
098      }
099    
100      @Override
101      public ImageAssert usingComparator(Comparator<? super BufferedImage> customComparator) {
102        throw new UnsupportedOperationException("custom Comparator is not supported for image comparison");
103      }
104    
105      @Override
106      public ImageAssert usingDefaultComparator() {
107        super.usingDefaultComparator();
108        this.images = Images.instance();
109        return myself;
110      }
111    }