1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
49
50 public class BufferedImageAssertion {
51
52
53 private final BufferedImage actual;
54
55
56 private String description = "image";
57
58
59
60
61
62
63
64 public BufferedImageAssertion(BufferedImage actual) {
65 this.actual = actual;
66 }
67
68
69
70
71
72
73 public BufferedImageAssertion isIndexedColor() {
74 assertThat(isIndexed()).as(description + ".indexed").isTrue();
75 return this;
76 }
77
78
79
80
81
82
83 public BufferedImageAssertion isDirectColor() {
84 assertThat(!isIndexed()).as(description + ".direct").isTrue();
85 return this;
86 }
87
88
89
90
91
92
93 private boolean isIndexed() {
94 return actual.getType() == BufferedImage.TYPE_BYTE_INDEXED
95 || actual.getType() == BufferedImage.TYPE_BYTE_BINARY;
96 }
97
98
99
100
101
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
129
130
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
154
155
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
178
179
180
181
182
183
184 public BufferedImageAssertion hasNumberOfColorsEqualTo(int colors) {
185 assertThat(BufferedImageUtils.countDistinctColors(actual)).as(description + ".colors").isEqualTo(colors);
186 return this;
187 }
188
189
190
191
192
193
194
195
196
197 public BufferedImageAssertion isEqualTo(BufferedImage expected) {
198 assertThat(compareImage(expected)).isTrue();
199 return this;
200 }
201
202
203
204
205
206
207
208
209
210 public BufferedImageAssertion isNotEqualTo(BufferedImage expected) {
211 assertThat(actual).isNotEqualTo(expected);
212 return this;
213 }
214
215
216
217
218
219
220
221
222
223 public BufferedImageAssertion as(String description) {
224 this.description = description;
225 return this;
226 }
227
228
229
230
231
232
233
234
235
236 public BufferedImageAssertion hasSize(Dimension dimension) {
237 assertThat(new Dimension(actual.getWidth(), actual.getHeight())).isEqualTo(dimension);
238 return this;
239 }
240
241
242
243
244
245
246
247
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 }