View Javadoc
1   /*
2    * SmartSprites Project
3    *
4    * Copyright (C) 2007-2009, Stanisław Osiński.
5    * All rights reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without modification,
8    * are permitted provided that the following conditions are met:
9    *
10   * - Redistributions of  source code must  retain the above  copyright notice, this
11   *   list of conditions and the following disclaimer.
12   *
13   * - Redistributions in binary form must reproduce the above copyright notice, this
14   *   list of conditions and the following  disclaimer in  the documentation  and/or
15   *   other materials provided with the distribution.
16   *
17   * - Neither the name of the SmartSprites Project nor the names of its contributors
18   *   may  be used  to endorse  or  promote  products derived   from  this  software
19   *   without specific prior written permission.
20   *
21   * - We kindly request that you include in the end-user documentation provided with
22   *   the redistribution and/or in the software itself an acknowledgement equivalent
23   *   to  the  following: "This product includes software developed by the SmartSprites
24   *   Project."
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  AND
27   * ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED  TO, THE IMPLIED
28   * WARRANTIES  OF  MERCHANTABILITY  AND  FITNESS  FOR  A  PARTICULAR  PURPOSE   ARE
29   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE  FOR
30   * ANY DIRECT, INDIRECT, INCIDENTAL,  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  DAMAGES
31   * (INCLUDING, BUT  NOT LIMITED  TO, PROCUREMENT  OF SUBSTITUTE  GOODS OR SERVICES;
32   * LOSS OF USE, DATA, OR PROFITS;  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND  ON
33   * ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT  LIABILITY,  OR TORT
34   * (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY  OUT OF THE USE  OF THIS
35   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   */
37  package org.carrot2.labs.test;
38  
39  import static org.assertj.core.api.Assertions.assertThat;
40  
41  import java.awt.Dimension;
42  import java.awt.image.BufferedImage;
43  
44  import org.carrot2.labs.smartsprites.css.CssProperty;
45  import org.carrot2.util.BufferedImageUtils;
46  
47  /**
48   * Assertions on instances of {@link CssProperty}.
49   */
50  public class BufferedImageAssertion {
51  
52      /** The actual property. */
53      private final BufferedImage actual;
54  
55      /** Assertion description. */
56      private String description = "image";
57  
58      /**
59       * Creates a {@link BufferedImage} assertion.
60       *
61       * @param actual
62       *            the actual
63       */
64      public BufferedImageAssertion(BufferedImage actual) {
65          this.actual = actual;
66      }
67  
68      /**
69       * Asserts that the image is an indexed color image.
70       *
71       * @return the buffered image assertion
72       */
73      public BufferedImageAssertion isIndexedColor() {
74          assertThat(isIndexed()).as(description + ".indexed").isTrue();
75          return this;
76      }
77  
78      /**
79       * Asserts that the image is a direct color image.
80       *
81       * @return the buffered image assertion
82       */
83      public BufferedImageAssertion isDirectColor() {
84          assertThat(!isIndexed()).as(description + ".direct").isTrue();
85          return this;
86      }
87  
88      /**
89       * Checks if is indexed.
90       *
91       * @return true, if is indexed
92       */
93      private boolean isIndexed() {
94          return actual.getType() == BufferedImage.TYPE_BYTE_INDEXED
95                  || actual.getType() == BufferedImage.TYPE_BYTE_BINARY;
96      }
97  
98      /**
99       * Asserts that the image has bit (0/1) alpha areas.
100      *
101      * @return the buffered image assertion
102      */
103     public BufferedImageAssertion hasBitAlpha() {
104         final int[][] rgb = BufferedImageUtils.getRgb(actual);
105         int width = actual.getWidth();
106         int height = actual.getHeight();
107         boolean hasBitAlpha = false;
108 
109         exit: for (int x = 0; x < width; x++) {
110             for (int y = 0; y < height; y++) {
111                 final int alpha = (rgb[x][y] & 0xff000000) >> 24;
112                 if (alpha == 0) {
113                     hasBitAlpha = true;
114                 }
115 
116                 if (alpha > 0 && alpha != 255) {
117                     hasBitAlpha = false;
118                     break exit;
119                 }
120             }
121         }
122 
123         assertThat(hasBitAlpha).as(description + ".hasBitAlpha").isTrue();
124         return this;
125     }
126 
127     /**
128      * Asserts that the image has true (0..1) alpha areas.
129      *
130      * @return the buffered image assertion
131      */
132     public BufferedImageAssertion hasTrueAlpha() {
133         final int[][] rgb = BufferedImageUtils.getRgb(actual);
134         int width = actual.getWidth();
135         int height = actual.getHeight();
136         boolean hasTrueAlpha = false;
137 
138         exit: for (int x = 0; x < width; x++) {
139             for (int y = 0; y < height; y++) {
140                 final int alpha = (rgb[x][y] & 0xff000000) >> 24;
141                 if (alpha > 0 && alpha < 255) {
142                     hasTrueAlpha = true;
143                     break exit;
144                 }
145             }
146         }
147 
148         assertThat(hasTrueAlpha).as(description + ".hasTrueAlpha").isTrue();
149         return this;
150     }
151 
152     /**
153      * Asserts that the image has or doesn't have any transparent areas.
154      *
155      * @return the buffered image assertion
156      */
157     public BufferedImageAssertion doesNotHaveAlpha() {
158         final int[][] rgb = BufferedImageUtils.getRgb(actual);
159         int width = actual.getWidth();
160         int height = actual.getHeight();
161         boolean hasAlpha = false;
162 
163         exit: for (int x = 0; x < width; x++) {
164             for (int y = 0; y < height; y++) {
165                 if ((rgb[x][y] & 0xff000000) != 0xff000000) {
166                     hasAlpha = true;
167                     break exit;
168                 }
169             }
170         }
171 
172         assertThat(hasAlpha).as(description + ".hasAlpha").isFalse();
173         return this;
174     }
175 
176     /**
177      * Asserts that the image has the specified number of colors, fully transparent pixels are not counted.
178      *
179      * @param colors
180      *            the colors
181      *
182      * @return the buffered image assertion
183      */
184     public BufferedImageAssertion hasNumberOfColorsEqualTo(int colors) {
185         assertThat(BufferedImageUtils.countDistinctColors(actual)).as(description + ".colors").isEqualTo(colors);
186         return this;
187     }
188 
189     /**
190      * Checks if is equal to.
191      *
192      * @param expected
193      *            the expected
194      *
195      * @return the buffered image assertion
196      */
197     public BufferedImageAssertion isEqualTo(BufferedImage expected) {
198         assertThat(compareImage(expected)).isTrue();
199         return this;
200     }
201 
202     /**
203      * Checks if is not equal to.
204      *
205      * @param expected
206      *            the expected
207      *
208      * @return the buffered image assertion
209      */
210     public BufferedImageAssertion isNotEqualTo(BufferedImage expected) {
211         assertThat(actual).isNotEqualTo(expected);
212         return this;
213     }
214 
215     /**
216      * As.
217      *
218      * @param description
219      *            the description
220      *
221      * @return the buffered image assertion
222      */
223     public BufferedImageAssertion as(String description) {
224         this.description = description;
225         return this;
226     }
227 
228     /**
229      * Checks for size.
230      *
231      * @param dimension
232      *            the dimension
233      *
234      * @return the buffered image assertion
235      */
236     public BufferedImageAssertion hasSize(Dimension dimension) {
237         assertThat(new Dimension(actual.getWidth(), actual.getHeight())).isEqualTo(dimension);
238         return this;
239     }
240 
241     /**
242      * Compare image.
243      *
244      * @param expected
245      *            the expected
246      *
247      * @return true, if successful
248      */
249     private boolean compareImage(BufferedImage expected) {
250         if (actual.getWidth() != expected.getWidth() || actual.getHeight() != expected.getHeight()) {
251             return false;
252         }
253         for (int x = 0; x < actual.getWidth(); x++) {
254             for (int y = 0; y < actual.getHeight(); y++) {
255                 if (actual.getRGB(x, y) != expected.getRGB(x, y)) {
256                     return false;
257                 }
258             }
259         }
260         return true;
261     }
262 }