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     */
036    public class ImageAssert extends AbstractAssert<ImageAssert, BufferedImage> {
037    
038      @VisibleForTesting
039      Images images = Images.instance();
040    
041      protected ImageAssert(BufferedImage actual) {
042        super(actual, ImageAssert.class);
043      }
044    
045      /**
046       * Verifies that the actual image is equal to the given one. Two images are equal if:
047       * <ol>
048       * <li>they have equal size</li>
049       * <li>the the RGB values of the color at each pixel are equal</li>
050       * </ol>
051       * @param expected the given image to compare the actual image to.
052       * @return {@code this} assertion object.
053       * @throws AssertionError if the actual image is not equal to the given one.
054       */
055      @Override
056      public ImageAssert isEqualTo(BufferedImage expected) {
057        images.assertEqual(info, actual, expected);
058        return this;
059      }
060    
061      /**
062       * Verifies that the actual image is equal to the given one. Two images are equal if:
063       * <ol>
064       * <li>they have the same size</li>
065       * <li>the difference between the RGB values of the color at each pixel is less than or equal to the given offset</li>
066       * </ol>
067       * @param expected the given image to compare the actual image to.
068       * @param offset helps decide if the color of two pixels are similar: two pixels that are identical to the human eye
069       *          may still have slightly different color values. For example, by using an offset of 1 we can indicate that
070       *          a blue value of 60 is similar to a blue value of 61.
071       * @return {@code this} assertion object.
072       * @throws NullPointerException if the given offset is {@code null}.
073       * @throws AssertionError if the actual image is not equal to the given one.
074       */
075      public ImageAssert isEqualTo(BufferedImage expected, Offset<Integer> offset) {
076        images.assertEqual(info, actual, expected, offset);
077        return this;
078      }
079    
080      /** {@inheritDoc} */
081      @Override
082      public ImageAssert isNotEqualTo(BufferedImage other) {
083        images.assertNotEqual(info, actual, other);
084        return this;
085      }
086    
087      /**
088       * Verifies that the actual image has the given size.
089       * @param expected the expected size of the actual image.
090       * @return {@code this} assertion object.
091       * @throws NullPointerException if the given size is {@code null}.
092       * @throws AssertionError if the size of the actual image is not equal to the given size.
093       */
094      public ImageAssert hasSize(Dimension expected) {
095        images.assertHasSize(info, actual, expected);
096        return this;
097      }
098    
099      @Override
100      public ImageAssert usingComparator(Comparator<?> customComparator) {
101        throw new UnsupportedOperationException("custom Comparator is not supported for image comparison");
102      }
103    
104      @Override
105      public ImageAssert usingDefaultComparator() {
106        super.usingDefaultComparator();
107        this.images = Images.instance();
108        return myself;
109      }
110    }