1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *     Red Hat Inc. - Bug 462631
11  * Port to the D Programming Language:
12  *     alice <stigma@disroot.org>
13  *******************************************************************************/
14 module tests.Color;
15 
16 import java.lang.exceptions : IllegalArgumentException;
17 
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.graphics.RGBA;
21 import org.eclipse.swt.widgets.Display;
22 
23 /**
24  * Automated Test Suite for class org.eclipse.swt.graphics.Color
25  *
26  * @see org.eclipse.swt.graphics.Color
27  */
28 
29 @("test_ConstructorLorg_eclipse_swt_graphics_DeviceIII")
30 unittest
31 {
32     // Test new Color(Device device, int red, int green, int blue)
33     // IllegalArgumentException if the red, green or blue argument is not between 0 and 255
34 
35     Display display = Display.getDefault();
36 
37     // valid color (black)
38     Color color = new Color(display, 0, 0, 0);
39     color.dispose();
40 
41     // valid color (black with alpha)
42     color = new Color(display, 0, 0, 0, 0);
43     color.dispose();
44 
45     // valid color (white)
46     color = new Color(display, 255, 255, 255);
47     color.dispose();
48 
49     // valid color (white with alpha)
50     color = new Color(display, 255, 255, 255, 0);
51     color.dispose();
52 
53     // valid color (random grey)
54     color = new Color(display, 20, 20, 20);
55     color.dispose();
56 
57     // valid color (random grey with alpha)
58     color = new Color(display, 20, 20, 20, 0);
59     color.dispose();
60 
61     // valid color (random)
62     color = new Color(display, 102, 255, 0);
63     color.dispose();
64 
65     // valid color (random with alpha)
66     color = new Color(display, 102, 255, 0, 0);
67     color.dispose();
68 
69     // device == null (valid)
70     color = new Color(null, 0, 0, 0);
71     color.dispose();
72 
73     // device == null (valid with alpha)
74     color = new Color(null, 0, 0, 0, 0);
75     color.dispose();
76 
77     // illegal argument, rgb < 0
78     try {
79         color = new Color(display, -10, -10, -10);
80         color.dispose();
81         assert(false, "No exception thrown for rgb < 0");
82     } catch (IllegalArgumentException e) {
83     }
84 
85     // illegal argument, alpha < 0
86     try {
87         color = new Color(display, 0, 0, 0, -10);
88         color.dispose();
89         assert(false, "No exception thrown for rgba < 0");
90     } catch (IllegalArgumentException e) {
91     }
92 
93     // illegal argument, rgb > 255
94     try {
95         color = new Color(display, 1000, 2000, 3000);
96         color.dispose();
97         assert(false, "No exception thrown for rgb > 255");
98     } catch (IllegalArgumentException e) {
99     }
100 
101     // illegal argument, rgba > 255
102     try {
103         color = new Color(display, 1000, 2000, 3000, 4000);
104         color.dispose();
105         assert(false, "No exception thrown for rgba > 255");
106     } catch (IllegalArgumentException e) {
107     }
108 
109     // illegal argument, blue > 255
110     try {
111         color = new Color(display, 10, 10, 256);
112         color.dispose();
113         assert(false, "No exception thrown for blue > 255");
114     } catch (IllegalArgumentException e) {
115     }
116 
117     // illegal argument, alpha > 255
118     try {
119         color = new Color(display, 10, 10, 10, 256);
120         color.dispose();
121         assert(false, "No exception thrown for alpha > 255");
122     } catch (IllegalArgumentException e) {
123     }
124 }
125 
126 @("test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGB")
127 unittest
128 {
129     // Test new Color(Device device, RGB rgb)
130     // IllegalArgumentException if the red, green or blue argument is not between 0 and 255; or rgb is null
131 
132     Display display = Display.getDefault();
133 
134     // valid color (black)
135     Color color = new Color(display, new RGB(0, 0, 0));
136     color.dispose();
137 
138     // valid color (black with alpha)
139     color = new Color(display, new RGB(0, 0, 0), 0);
140     color.dispose();
141 
142     // valid color (white)
143     color = new Color(display, new RGB(255, 255, 255));
144     color.dispose();
145 
146     // valid color (white with alpha)
147     color = new Color(display, new RGB(255, 255, 255), 0);
148     color.dispose();
149 
150     // valid color (random grey)
151     color = new Color(display, new RGB(10, 10, 10));
152     color.dispose();
153 
154     // valid color (random grey with alpha)
155     color = new Color(display, new RGB(10, 10, 10), 0);
156     color.dispose();
157 
158     // valid color (random)
159     color = new Color(display, new RGB(102, 255, 0));
160     color.dispose();
161 
162     // valid color (random with alpha)
163     color = new Color(display, new RGB(102, 255, 0), 0);
164     color.dispose();
165 
166     // device == null (valid)
167     color = new Color(null, new RGB(0, 0, 0));
168     color.dispose();
169 
170     // device == null (valid with alpha)
171     color = new Color(null, new RGB(0, 0, 0), 0);
172     color.dispose();
173 
174     // illegal argument, rgb < 0
175     try {
176         color = new Color(display, new RGB(-10, -10, -10));
177         color.dispose();
178         assert(false, "No exception thrown for rgb < 0");
179     }
180     catch (IllegalArgumentException e) {
181     }
182     // illegal argument, alpha < 0
183     try {
184         color = new Color(display, new RGB(0, 0, 0), -10);
185         color.dispose();
186         assert(false, "No exception thrown for rgba < 0");
187     }
188     catch (IllegalArgumentException e) {
189     }
190     // illegal argument, rgb > 255
191     try {
192         color = new Color(display, new RGB(1000, 2000, 3000));
193         color.dispose();
194         assert(false, "No exception thrown for rgb > 255");
195     }
196     catch (IllegalArgumentException e) {
197     }
198     // illegal argument, rgba > 255
199     try {
200         color = new Color(display, new RGB(1000, 2000, 3000), 4000);
201         color.dispose();
202         assert(false, "No exception thrown for rgba > 255");
203     }
204     catch (IllegalArgumentException e) {
205     }
206     // illegal argument, blue > 255
207     try {
208         color = new Color(display, new RGB(10, 10, 256));
209         color.dispose();
210         assert(false, "No exception thrown for blue > 255");
211     }
212     catch (IllegalArgumentException e) {
213     }
214     // illegal argument, alpha > 255
215     try {
216         color = new Color(display, new RGB(10, 10, 10), 256);
217         color.dispose();
218         assert(false, "No exception thrown for alpha > 255");
219     }
220     catch (IllegalArgumentException e) {
221     }
222     // illegal argument, rgb == null with alpha
223     try {
224         color = new Color(display, null, 0);
225         color.dispose();
226         assert(false, "No exception thrown for rgb == null with alpha");
227     }
228     catch (IllegalArgumentException e) {
229     }
230 }
231 
232 @("test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGBA")
233 unittest
234 {
235     // Test new Color(Device device, RGBA rgba)
236     // IllegalArgumentException if the red, green, blue or alpha argument is not between 0 and 255; or rgba is null
237 
238     Display display = Display.getDefault();
239 
240     // valid color (black)
241     Color color = new Color(display, new RGBA(0, 0, 0, 255));
242     color.dispose();
243 
244     // valid color (black with alpha)
245     color = new Color(display, new RGBA(0, 0, 0, 0));
246     color.dispose();
247 
248     // valid color (white)
249     color = new Color(display, new RGBA(255, 255, 255, 255));
250     color.dispose();
251 
252     // valid color (white with alpha)
253     color = new Color(display, new RGBA(255, 255, 255, 0));
254     color.dispose();
255 
256     // valid color (random grey)
257     color = new Color(display, new RGBA(10, 10, 10, 10));
258     color.dispose();
259 
260     // valid color (random grey with alpha)
261     color = new Color(display, new RGBA(10, 10, 10, 0));
262     color.dispose();
263 
264     // valid color (random)
265     color = new Color(display, new RGBA(102, 255, 0, 255));
266     color.dispose();
267 
268     // valid color (random with alpha)
269     color = new Color(display, new RGBA(102, 255, 0, 0));
270     color.dispose();
271 
272     // device == null (valid)
273     color = new Color(null, new RGBA(0, 0, 0, 255));
274     color.dispose();
275 
276     // device == null (valid with alpha)
277     color = new Color(null, new RGBA(0, 0, 0, 0));
278     color.dispose();
279 
280     // illegal argument, rgba < 0
281     try {
282         color = new Color(display, new RGBA(-10, -10, -10, -10));
283         color.dispose();
284         assert(false, "No exception thrown for rgba < 0");
285     }
286     catch (IllegalArgumentException e) {
287     }
288     // illegal argument, alpha < 0
289     try {
290         color = new Color(display, new RGBA(0, 0, 0, -10));
291         color.dispose();
292         assert(false, "No exception thrown for alpha < 0");
293     }
294     catch (IllegalArgumentException e) {
295     }
296     // illegal argument, rgba > 255
297     try {
298         color = new Color(display, new RGBA(1000, 2000, 3000, 4000));
299         color.dispose();
300         assert(false, "No exception thrown for rgba > 255");
301     }
302     catch (IllegalArgumentException e) {
303     }
304     // illegal argument, blue > 255
305     try {
306         color = new Color(display, new RGBA(10, 10, 256, 10));
307         color.dispose();
308         assert(false, "No exception thrown for blue > 255");
309     }
310     catch (IllegalArgumentException e) {
311     }
312     // illegal argument, alpha > 255
313     try {
314         color = new Color(display, new RGBA(10, 10, 10, 256));
315         color.dispose();
316         assert(false, "No exception thrown for alpha > 255");
317     }
318     catch (IllegalArgumentException e) {
319     }
320 }
321 
322 @("test_equalsLjava_lang_Object")
323 unittest
324 {
325     Display display = Display.getDefault();
326     Color color = new Color(display, 1, 2, 3);
327     Color sameColor = new Color(display, 1, 2, 3);
328     Color sameColor2 = new Color(display, new RGB(1, 2, 3));
329     Color otherColor = new Color(display, 5, 6, 7);
330     try {
331         // Test Color.opEquals(Object)
332         assert(color != cast(Object)null, "color != cast(Object)null");
333 
334         // Test Color.opEquals(Color)
335         assert(color != cast(Color)null, "color != cast(Color)null");
336         assert(color == color, "color == color");
337         assert(color == sameColor, "color == sameColor");
338         assert(color == sameColor2, "color == sameColor2");
339         assert(color != otherColor, "color != otherColor");
340 
341     } finally {
342         color.dispose();
343         sameColor.dispose();
344         sameColor2.dispose();
345         otherColor.dispose();
346     }
347 
348 
349     // With alpha
350     color = new Color(display, 1, 2, 3, 0);
351     sameColor = new Color(display, 1, 2, 3, 0);
352     sameColor2 = new Color(display, new RGB(1, 2, 3), 0);
353     otherColor = new Color(display, 5, 6, 7, 0);
354     try {
355         // Test Color.opEquals(Object)
356         assert(color != cast(Object)null, "color != cast(Object)null");
357 
358         // Test Color.opEquals(Color)
359         assert(color != cast(Color)null, "color != cast(Color)null");
360         assert(color == color, "color == color");
361         assert(color == sameColor, "color == sameColor");
362         assert(color == sameColor2, "color == sameColor2");
363         assert(color != otherColor, "color != otherColor");
364     } finally {
365         color.dispose();
366         sameColor.dispose();
367         sameColor2.dispose();
368         otherColor.dispose();
369     }
370 }
371 
372 @("test_getBlue")
373 unittest
374 {
375     // Test Color.getBlue()
376     Display display = Display.getDefault();
377     Color color = new Color(display, 0, 0, 255);
378 
379     try {
380         assert(color.getBlue() == 255, "color.getBlue()");
381     } finally {
382         color.dispose();
383     }
384 }
385 
386 @("test_getGreen")
387 unittest
388 {
389     // Test Color.getGreen()
390     Display display = Display.getDefault();
391     Color color = new Color(display, 0, 255, 0);
392 
393     try {
394         assert(color.getGreen() == 255, "color.getGreen()");
395     } finally {
396         color.dispose();
397     }
398 }
399 
400 @("test_getRGB")
401 unittest
402 {
403     Display display = Display.getDefault();
404     Color color = new Color(display, 255, 255, 255);
405     assert(color.getRGB() !is null);
406     assert(color.getRGB == new RGB(255, 255, 255));
407     color.dispose();
408 }
409 
410 @("test_getRed")
411 unittest
412 {
413     // Test Color.getRed()
414     Display display = Display.getDefault();
415     Color color = new Color(display, 255, 0, 0);
416 
417     try {
418         assert(color.getRed() == 255, "color.getRed()");
419     } finally {
420         color.dispose();
421     }
422 }
423 
424 @("test_getAlpha")
425 unittest
426 {
427     // Test Color.getAlpha
428     Display display = Display.getDefault();
429     Color color = new Color(display, 255, 0, 0, 0);
430 
431     try {
432         assert(color.getAlpha() == 0, "color.getAlpha()");
433     } finally {
434         color.dispose();
435     }
436 }
437 
438 @("test_hashCode")
439 unittest
440 {
441     Display display = Display.getDefault();
442     Color color = new Color(display, 12, 36, 54, 0);
443     Color otherColor = new Color(display, 12, 26, 54, 0);
444     if (color == otherColor) {
445         assert(color.toHash() == otherColor.toHash(), "Hash codes of equal objects should be equal");
446     }
447     color.dispose();
448     otherColor.dispose();
449 }
450 
451 @("test_isDisposed")
452 unittest
453 {
454     // Test Color.isDisposed()
455     Display display = Display.getDefault();
456     Color color = new Color(display, 34, 67, 98, 0);
457     try {
458         assert(!color.isDisposed(), "Color should not be disposed");
459     } finally {
460         // Test Color.isDisposed() true
461         color.dispose();
462         assert(color.isDisposed(), "Color should be disposed");
463     }
464 }
465 
466 @("test_toString")
467 unittest
468 {
469     Display display = Display.getDefault();
470     Color color = new Color(display, 0, 0, 255, 255);
471     try {
472         assert(color.toString() !is null);
473         assert(color.toString().length > 0);
474         assert(color.toString() == "Color {0, 0, 255, 255}");
475     } finally {
476         color.dispose();
477     }
478 }