diff options
author | Jonah Graham | 2020-05-13 21:04:38 +0000 |
---|---|---|
committer | Alexander Kurtakov | 2020-06-11 07:50:06 +0000 |
commit | 6f25fa51112fbf1670baee56c085ab60273daf47 (patch) | |
tree | 03e9b1771b22e8f7c87608f5e916624a1e07a26a | |
parent | 0d2bf81f8e81626dd7863db2168f2a7e1e62ae80 (diff) | |
download | eclipse.platform.swt-6f25fa51112fbf1670baee56c085ab60273daf47.tar.gz eclipse.platform.swt-6f25fa51112fbf1670baee56c085ab60273daf47.tar.xz eclipse.platform.swt-6f25fa51112fbf1670baee56c085ab60273daf47.zip |
Bug 563560: Add new constructors to Color that don't require a Device
Change-Id: Icfca812fa3f3ccbf99cd5395e7d3c255fb3e674b
Signed-off-by: Jonah Graham <jonah@kichwacoders.com>
4 files changed, 819 insertions, 5 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Color.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Color.java index 3f6ad73581..880f8c2092 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Color.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/Color.java @@ -47,6 +47,10 @@ public final class Color extends Resource { */ public double [] handle; +Color() { + super(); +} + Color(Device device) { super(device); } @@ -77,6 +81,29 @@ public Color(Device device, int red, int green, int blue) { } /** + * Constructs a new instance of this class given the + * desired red, green and blue values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(int red, int green, int blue) { + super(); + init(red, green, blue, 255); +} + +/** * Constructs a new instance of this class given a device and the * desired red, green, blue & alpha values expressed as ints in the range * 0 to 255 (where 0 is black and 255 is full brightness). On limited @@ -105,6 +132,31 @@ public Color(Device device, int red, int green, int blue, int alpha) { } /** + * Constructs a new instance of this class given the + * desired red, green, blue & alpha values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * @param alpha the amount of alpha in the color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(int red, int green, int blue, int alpha) { + super(); + init(red, green, blue, alpha); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGB</code> describing the desired red, green and blue values. * On limited color devices, the color instance created by this call @@ -129,6 +181,28 @@ public Color(Device device, RGB rgb) { } /** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(RGB rgb) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, 255); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGBA</code> describing the desired red, green, blue & alpha values. * On limited color devices, the color instance created by this call @@ -155,6 +229,29 @@ public Color(Device device, RGBA rgba) { } /** + * Constructs a new instance of this class given an + * <code>RGBA</code> describing the desired red, green, blue & alpha values. + * On limited color devices, the color instance created by this call + * may not have the same RGBA values as the ones specified by the + * argument. The RGBA values on the returned instance will be the color + * values of the operating system color + alpha. + * + * @param rgba the RGBA values of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgba argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGBA rgba) { + super(); + if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); +} + +/** * Constructs a new instance of this class given a device, an * <code>RGB</code> describing the desired red, green and blue values, * alpha specifying the level of transparency. @@ -182,6 +279,31 @@ public Color(Device device, RGB rgb, int alpha) { init(); } +/** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values, + * alpha specifying the level of transparency. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * @param alpha the alpha value of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGB rgb, int alpha) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, alpha); +} + @Override void destroy() { handle = null; @@ -194,6 +316,23 @@ void destroy() { @Override public void dispose() { super.dispose(); + destroy(); +} + +/** + * Returns the <code>Device</code> where this resource was + * created. In cases where no <code>Device</code> was used + * at creation, returns the current or default Device. + * + * @return <code>Device</code> the device of the receiver + * @since 3.2 + */ +@Override +public Device getDevice() { + // Fall back on Device.getDevice only if we haven't been disposed + // already. + if (this.device == null && this.handle != null) return Device.getDevice(); + return super.getDevice(); } /** @@ -213,7 +352,7 @@ public boolean equals(Object object) { Color color = (Color)object; double [] rgbColor = color.handle; if (handle == rgbColor) return true; - return device == color.device && + return (int)(handle[0] * 255) == (int)(rgbColor[0] * 255) && (int)(handle[1] * 255) == (int)(rgbColor[1] * 255) && (int)(handle[2] * 255) == (int)(rgbColor[2] * 255) && diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Color.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Color.java index d7e182cb98..183fef6f5f 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Color.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Color.java @@ -49,6 +49,10 @@ public final class Color extends Resource { public GdkRGBA handle; int alpha = 0; +Color() { + super(); +} + Color(Device device) { super(device); } @@ -79,6 +83,29 @@ public Color(Device device, int red, int green, int blue) { } /** + * Constructs a new instance of this class given the + * desired red, green and blue values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(int red, int green, int blue) { + super(); + init(red, green, blue, 255); +} + +/** * Constructs a new instance of this class given a device and the * desired red, green, blue & alpha values expressed as ints in the range * 0 to 255 (where 0 is black and 255 is full brightness). On limited @@ -107,6 +134,31 @@ public Color(Device device, int red, int green, int blue, int alpha) { } /** + * Constructs a new instance of this class given the + * desired red, green, blue & alpha values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * @param alpha the amount of alpha in the color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(int red, int green, int blue, int alpha) { + super(); + init(red, green, blue, alpha); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGB</code> describing the desired red, green and blue values. * On limited color devices, the color instance created by this call @@ -131,6 +183,28 @@ public Color(Device device, RGB rgb) { } /** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(RGB rgb) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, 255); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGBA</code> describing the desired red, green, blue & alpha values. * On limited color devices, the color instance created by this call @@ -157,6 +231,29 @@ public Color(Device device, RGBA rgba) { } /** + * Constructs a new instance of this class given an + * <code>RGBA</code> describing the desired red, green, blue & alpha values. + * On limited color devices, the color instance created by this call + * may not have the same RGBA values as the ones specified by the + * argument. The RGBA values on the returned instance will be the color + * values of the operating system color + alpha. + * + * @param rgba the RGBA values of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgba argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGBA rgba) { + super(); + if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); +} + +/** * Constructs a new instance of this class given a device, an * <code>RGB</code> describing the desired red, green and blue values, * alpha specifying the level of transparency. @@ -184,6 +281,31 @@ public Color(Device device, RGB rgb, int alpha) { init(); } +/** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values, + * alpha specifying the level of transparency. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * @param alpha the alpha value of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGB rgb, int alpha) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, alpha); +} + @Override void destroy() { handle = null; @@ -196,6 +318,23 @@ void destroy() { @Override public void dispose() { super.dispose(); + destroy(); +} + +/** + * Returns the <code>Device</code> where this resource was + * created. In cases where no <code>Device</code> was used + * at creation, returns the current or default Device. + * + * @return <code>Device</code> the device of the receiver + * @since 3.2 + */ +@Override +public Device getDevice() { + // Fall back on Device.getDevice only if we haven't been disposed + // already. + if (this.device == null && this.handle != null) return Device.getDevice(); + return super.getDevice(); } /** @@ -219,7 +358,7 @@ public boolean equals(Object object) { if (this.getGreen() != color.getGreen()) return false; if (this.getBlue() != color.getBlue()) return false; if (this.getAlpha() != color.getAlpha()) return false; - return device == color.device; + return true; } /** diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java index d9c6a19bbc..46349141ed 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Color.java @@ -52,6 +52,13 @@ public final class Color extends Resource { /** * Prevents uninitialized instances from being created outside the package. */ +Color() { + super(); +} + +/** + * Prevents uninitialized instances from being created outside the package. + */ Color(Device device) { super(device); } @@ -82,6 +89,29 @@ public Color (Device device, int red, int green, int blue) { } /** + * Constructs a new instance of this class given the + * desired red, green and blue values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(int red, int green, int blue) { + super(); + init(red, green, blue, 255); +} + +/** * Constructs a new instance of this class given a device and the * desired red, green, blue & alpha values expressed as ints in the range * 0 to 255 (where 0 is black and 255 is full brightness). On limited @@ -110,6 +140,31 @@ public Color (Device device, int red, int green, int blue, int alpha) { } /** + * Constructs a new instance of this class given the + * desired red, green, blue & alpha values expressed as ints in the range + * 0 to 255 (where 0 is black and 255 is full brightness). On limited + * color devices, the color instance created by this call may not have + * the same RGB values as the ones specified by the arguments. The + * RGB values on the returned instance will be the color values of + * the operating system color. + * + * @param red the amount of red in the color + * @param green the amount of green in the color + * @param blue the amount of blue in the color + * @param alpha the amount of alpha in the color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha argument is not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(int red, int green, int blue, int alpha) { + super(); + init(red, green, blue, alpha); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGB</code> describing the desired red, green and blue values. * On limited color devices, the color instance created by this call @@ -134,6 +189,28 @@ public Color (Device device, RGB rgb) { } /** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue components of the argument are not between 0 and 255</li> + * </ul> + * @since 3.115 + */ +public Color(RGB rgb) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, 255); +} + +/** * Constructs a new instance of this class given a device and an * <code>RGBA</code> describing the desired red, green, blue & alpha values. * On limited color devices, the color instance created by this call @@ -160,6 +237,29 @@ public Color(Device device, RGBA rgba) { } /** + * Constructs a new instance of this class given an + * <code>RGBA</code> describing the desired red, green, blue & alpha values. + * On limited color devices, the color instance created by this call + * may not have the same RGBA values as the ones specified by the + * argument. The RGBA values on the returned instance will be the color + * values of the operating system color + alpha. + * + * @param rgba the RGBA values of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgba argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGBA rgba) { + super(); + if (rgba == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); +} + +/** * Constructs a new instance of this class given a device, an * <code>RGB</code> describing the desired red, green and blue values, * alpha specifying the level of transparency. @@ -187,6 +287,31 @@ public Color(Device device, RGB rgb, int alpha) { init(); } +/** + * Constructs a new instance of this class given an + * <code>RGB</code> describing the desired red, green and blue values, + * alpha specifying the level of transparency. + * On limited color devices, the color instance created by this call + * may not have the same RGB values as the ones specified by the + * argument. The RGB values on the returned instance will be the color + * values of the operating system color. + * + * @param rgb the RGB values of the desired color + * @param alpha the alpha value of the desired color. Currently, SWT only honors extreme values for alpha i.e. 0 (transparent) or 255 (opaque). + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the rgb argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the red, green, blue or alpha components of the argument are not between 0 and 255</li> + * </ul> + * + * @since 3.115 + */ +public Color(RGB rgb, int alpha) { + super(); + if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); + init(rgb.red, rgb.green, rgb.blue, alpha); +} + @Override void destroy() { handle = -1; @@ -199,6 +324,23 @@ void destroy() { @Override public void dispose() { super.dispose(); + destroy(); +} + +/** + * Returns the <code>Device</code> where this resource was + * created. In cases where no <code>Device</code> was used + * at creation, returns the current or default Device. + * + * @return <code>Device</code> the device of the receiver + * @since 3.2 + */ +@Override +public Device getDevice() { + // Fall back on Device.getDevice only if we haven't been disposed + // already. + if (this.device == null && this.handle != -1) return Device.getDevice(); + return super.getDevice(); } /** @@ -216,7 +358,7 @@ public boolean equals (Object object) { if (object == this) return true; if (!(object instanceof Color)) return false; Color color = (Color) object; - return device == color.device && (handle & 0xFFFFFF) == (color.handle & 0xFFFFFF) && (alpha == color.alpha); + return (handle & 0xFFFFFF) == (color.handle & 0xFFFFFF) && (alpha == color.alpha); } /** diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java index 4e7d9dfe9d..cd87c37415 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2015 IBM Corporation and others. + * Copyright (c) 2000, 2020 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -19,7 +19,10 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.eclipse.swt.SWT; +import org.eclipse.swt.SWTException; import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.graphics.RGBA; import org.eclipse.swt.widgets.Display; @@ -29,6 +32,9 @@ import org.junit.Test; /** * Automated Test Suite for class org.eclipse.swt.graphics.Color * + * Within this test are tests that cover use of constructors with device + * and those without. + * * @see org.eclipse.swt.graphics.Color */ public class Test_org_eclipse_swt_graphics_Color { @@ -40,6 +46,79 @@ public void setUp() { @Test public void test_ConstructorLorg_eclipse_swt_graphics_DeviceIII() { + // Test new Color(int red, int green, int blue) + // IllegalArgumentException if the red, green or blue argument is not between 0 and 255 + + // valid color (black) + @SuppressWarnings("unused") + Color color = new Color(0, 0, 0); + + // valid color (black with alpha) + color = new Color(0, 0, 0, 0); + + // valid color (white) + color = new Color(255, 255, 255); + + // valid color (white with alpha) + color = new Color(255, 255, 255, 0); + + // valid color (random grey) + color = new Color(20, 20, 20); + + // valid color (random grey with alpha) + color = new Color(20, 20, 20, 0); + + // valid color (random) + color = new Color(102, 255, 0); + + // valid color (random with alpha) + color = new Color(102, 255, 0, 0); + + // illegal argument, rgb < 0 + try { + color = new Color(-10, -10, -10); + fail("No exception thrown for rgb < 0"); + } catch (IllegalArgumentException e) { + } + + // illegal argument, alpha < 0 + try { + color = new Color(0, 0, 0, -10); + fail("No exception thrown for rgba < 0"); + } catch (IllegalArgumentException e) { + } + + // illegal argument, rgb > 255 + try { + color = new Color(1000, 2000, 3000); + fail("No exception thrown for rgb > 255"); + } catch (IllegalArgumentException e) { + } + + // illegal argument, rgba > 255 + try { + color = new Color(1000, 2000, 3000, 4000); + fail("No exception thrown for rgba > 255"); + } catch (IllegalArgumentException e) { + } + + // illegal argument, blue > 255 + try { + color = new Color(10, 10, 256); + fail("No exception thrown for blue > 255"); + } catch (IllegalArgumentException e) { + } + + // illegal argument, alpha > 255 + try { + color = new Color(10, 10, 10, 256); + fail("No exception thrown for alpha > 255"); + } catch (IllegalArgumentException e) { + } +} + +@Test +public void test_ConstructorLorg_eclipse_swt_graphics_DeviceIII_with_device() { // Test new Color(Device device, int red, int green, int blue) // IllegalArgumentException if the red, green or blue argument is not between 0 and 255 @@ -134,6 +213,87 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceIII() { @Test public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGB() { + // Test new Color(RGB rgb) + // IllegalArgumentException if the red, green or blue argument is not between 0 and 255; or rgb is null + + // valid color (black) + @SuppressWarnings("unused") + Color color = new Color(new RGB(0, 0, 0)); + + // valid color (black with alpha) + color = new Color(new RGB(0, 0, 0), 0); + + // valid color (white) + color = new Color(new RGB(255, 255, 255)); + + // valid color (white with alpha) + color = new Color(new RGB(255, 255, 255), 0); + + // valid color (random grey) + color = new Color(new RGB(10, 10, 10)); + + // valid color (random grey with alpha) + color = new Color(new RGB(10, 10, 10), 0); + + // valid color (random) + color = new Color(new RGB(102, 255, 0)); + + // valid color (random with alpha) + color = new Color(new RGB(102, 255, 0), 0); + + // illegal argument, rgb < 0 + try { + color = new Color(new RGB(-10, -10, -10)); + fail("No exception thrown for rgb < 0"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, alpha < 0 + try { + color = new Color(new RGB(0, 0, 0), -10); + fail("No exception thrown for rgba < 0"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, rgb > 255 + try { + color = new Color(new RGB(1000, 2000, 3000)); + fail("No exception thrown for rgb > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, rgba > 255 + try { + color = new Color(new RGB(1000, 2000, 3000), 4000); + fail("No exception thrown for rgba > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, blue > 255 + try { + color = new Color(new RGB(10, 10, 256)); + fail("No exception thrown for blue > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, alpha > 255 + try { + color = new Color(new RGB(10, 10, 10), 256); + fail("No exception thrown for alpha > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, rgb == null with alpha + try { + color = new Color(null, 0); + fail("No exception thrown for rgb == null with alpha"); + } + catch (IllegalArgumentException e) { + } +} + +@Test +public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGB_with_device() { // Test new Color(Device device, RGB rgb) // IllegalArgumentException if the red, green or blue argument is not between 0 and 255; or rgb is null @@ -237,6 +397,73 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra @Test public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGBA() { + // Test new Color(RGBA rgba) + // IllegalArgumentException if the red, green, blue or alpha argument is not between 0 and 255; or rgba is null + + // valid color (black) + @SuppressWarnings("unused") + Color color = new Color(new RGBA(0, 0, 0, 255)); + + // valid color (black with alpha) + color = new Color(new RGBA(0, 0, 0, 0)); + + // valid color (white) + color = new Color(new RGBA(255, 255, 255, 255)); + + // valid color (white with alpha) + color = new Color(new RGBA(255, 255, 255, 0)); + + // valid color (random grey) + color = new Color(new RGBA(10, 10, 10, 10)); + + // valid color (random grey with alpha) + color = new Color(new RGBA(10, 10, 10, 0)); + + // valid color (random) + color = new Color(new RGBA(102, 255, 0, 255)); + + // valid color (random with alpha) + color = new Color(new RGBA(102, 255, 0, 0)); + + // illegal argument, rgba < 0 + try { + color = new Color(new RGBA(-10, -10, -10, -10)); + fail("No exception thrown for rgba < 0"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, alpha < 0 + try { + color = new Color(new RGBA(0, 0, 0, -10)); + fail("No exception thrown for alpha < 0"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, rgba > 255 + try { + color = new Color(new RGBA(1000, 2000, 3000, 4000)); + fail("No exception thrown for rgba > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, blue > 255 + try { + color = new Color(new RGBA(10, 10, 256, 10)); + fail("No exception thrown for blue > 255"); + } + catch (IllegalArgumentException e) { + } + // illegal argument, alpha > 255 + try { + color = new Color(new RGBA(10, 10, 10, 256)); + fail("No exception thrown for alpha > 255"); + } + catch (IllegalArgumentException e) { + } +} + +@Test +public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_graphics_RGBA_with_device() { // Test new Color(Device device, RGBA rgba) // IllegalArgumentException if the red, green, blue or alpha argument is not between 0 and 255; or rgba is null @@ -321,9 +548,41 @@ public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLorg_eclipse_swt_gra catch (IllegalArgumentException e) { } } - @Test public void test_equalsLjava_lang_Object() { + Color color = new Color(1, 2, 3); + Color sameColor = new Color(1, 2, 3); + Color sameColor2 = new Color(new RGB(1, 2, 3)); + Color otherColor = new Color(5, 6, 7); + + // Test Color.equals(Object) + assertTrue("!color.equals((Object)null)", !color.equals((Object)null)); + + // Test Color.equals(Color) + assertTrue("!color.equals((Color)null)", !color.equals((Color)null)); + assertTrue("color.equals(color)", color.equals(color)); + assertTrue("color.equals(sameColor)", color.equals(sameColor)); + assertTrue("color.equals(sameColor2)", color.equals(sameColor2)); + assertTrue("!color.equals(otherColor)", !color.equals(otherColor)); + + // With alpha + color = new Color(1, 2, 3, 0); + sameColor = new Color(1, 2, 3, 0); + sameColor2 = new Color(new RGB(1, 2, 3), 0); + otherColor = new Color(5, 6, 7, 0); + // Test Color.equals(Object) + assertTrue("!color.equals((Object)null)", !color.equals((Object)null)); + + // Test Color.equals(Color) + assertTrue("!color.equals((Color)null)", !color.equals((Color)null)); + assertTrue("color.equals(color)", color.equals(color)); + assertTrue("color.equals(sameColor)", color.equals(sameColor)); + assertTrue("color.equals(sameColor2)", color.equals(sameColor2)); + assertTrue("!color.equals(otherColor)", !color.equals(otherColor)); +} + +@Test +public void test_equalsLjava_lang_Object_with_device() { Color color = new Color(display, 1, 2, 3); Color sameColor = new Color(display, 1, 2, 3); Color sameColor2 = new Color(display, new RGB(1, 2, 3)); @@ -369,8 +628,45 @@ public void test_equalsLjava_lang_Object() { } @Test +public void test_equalsLjava_lang_Object_mix() { + Color color = new Color(display, 1, 2, 3); + Color sameColorNoDevice = new Color(1, 2, 3); + Color otherColorNoDevice = new Color(5, 6, 7); + try { + // Test Color.equals(Color) + assertTrue("color.equals(sameColor)", color.equals(sameColorNoDevice)); + assertTrue("!color.equals(otherColor)", !color.equals(otherColorNoDevice)); + assertTrue("color.equals(sameColor)", sameColorNoDevice.equals(color)); + assertTrue("!color.equals(otherColor)", !otherColorNoDevice.equals(color)); + } finally { + color.dispose(); + } + + // With alpha + color = new Color(display, 1, 2, 3, 0); + sameColorNoDevice = new Color(1, 2, 3, 0); + otherColorNoDevice = new Color(5, 6, 7, 0); + try { + // Test Color.equals(Color) + assertTrue("color.equals(sameColor)", color.equals(sameColorNoDevice)); + assertTrue("!color.equals(otherColor)", !color.equals(otherColorNoDevice)); + assertTrue("color.equals(sameColor)", sameColorNoDevice.equals(color)); + assertTrue("!color.equals(otherColor)", !otherColorNoDevice.equals(color)); + } finally { + color.dispose(); + } +} + +@Test public void test_getBlue() { // Test Color.getBlue() + Color color = new Color(0, 0, 255); + assertEquals("color.getBlue()", color.getBlue(), 255); +} + +@Test +public void test_getBlue_with_device() { + // Test Color.getBlue() Color color = new Color(display, 0, 0, 255); try { assertEquals("color.getBlue()", color.getBlue(), 255); @@ -383,6 +679,13 @@ public void test_getBlue() { @Test public void test_getGreen() { // Test Color.getGreen() + Color color = new Color(0, 255, 0); + assertEquals("color.getGreen()", color.getGreen(), 255); +} + +@Test +public void test_getGreen_with_device() { + // Test Color.getGreen() Color color = new Color(display, 0, 255, 0); try { assertEquals("color.getGreen()", color.getGreen(), 255); @@ -393,6 +696,13 @@ public void test_getGreen() { @Test public void test_getRGB() { + Color color = new Color(255, 255, 255); + assertNotNull(color.getRGB()); + assertEquals(new RGB(255, 255, 255), color.getRGB()); +} + +@Test +public void test_getRGB_with_device() { Color color = new Color(display, 255, 255, 255); assertNotNull(color.getRGB()); assertEquals(new RGB(255, 255, 255), color.getRGB()); @@ -402,6 +712,13 @@ public void test_getRGB() { @Test public void test_getRed() { // Test Color.getRed() + Color color = new Color(255, 0, 0); + assertEquals("color.getRed()", color.getRed(), 255); +} + +@Test +public void test_getRed_with_device() { + // Test Color.getRed() Color color = new Color(display, 255, 0, 0); try { assertEquals("color.getRed()", color.getRed(), 255); @@ -413,6 +730,13 @@ public void test_getRed() { @Test public void test_getAlpha() { // Test Color.getRed() + Color color = new Color(255, 0, 0, 0); + assertEquals("color.getAlpha()", color.getAlpha(), 0); +} + +@Test +public void test_getAlpha_with_device() { + // Test Color.getRed() Color color = new Color(display, 255, 0, 0, 0); try { assertEquals("color.getAlpha()", color.getAlpha(), 0); @@ -423,6 +747,15 @@ public void test_getAlpha() { @Test public void test_hashCode() { + Color color = new Color(12, 34, 56, 0); + Color otherColor = new Color(12, 34, 56, 0); + if (color.equals(otherColor)) { + assertEquals("Hash codes of equal objects should be equal", color.hashCode(), otherColor.hashCode()); + } +} + +@Test +public void test_hashCode_with_device() { Color color = new Color(display, 12, 34, 56, 0); Color otherColor = new Color(display, 12, 34, 56, 0); if (color.equals(otherColor)) { @@ -432,9 +765,26 @@ public void test_hashCode() { otherColor.dispose(); } +/** + * While Colors do not require disposal, if they are disposed we need to + * follow existing disposal semantics. + */ @Test public void test_isDisposed() { // Test Color.isDisposed() false + Color color = new Color(34, 67, 98, 0); + try { + assertTrue("Color should not be disposed", !color.isDisposed()); + } finally { + // Test Color.isDisposed() true + color.dispose(); + assertTrue("Color should be disposed", color.isDisposed()); + } +} + +@Test +public void test_isDisposed_with_device() { + // Test Color.isDisposed() false Color color = new Color(display, 34, 67, 98, 0); try { assertTrue("Color should not be disposed", !color.isDisposed()); @@ -447,6 +797,14 @@ public void test_isDisposed() { @Test public void test_toString() { + Color color = new Color(0, 0, 255, 255); + assertNotNull(color.toString()); + assertTrue(color.toString().length() > 0); + assertEquals("Color {0, 0, 255, 255}", color.toString()); +} + +@Test +public void test_toString_with_device() { Color color = new Color(display, 0, 0, 255, 255); try { assertNotNull(color.toString()); @@ -457,6 +815,42 @@ public void test_toString() { } } +@Test +public void test_getDevice() { + Color color = new Color(0, 0, 255, 255); + Device device = color.getDevice(); + assertEquals("The existing display should have been returned", display, device); + + color = new Color(0, 0, 255, 255); + // see test_isDisposed - we need to keep dispose semantics + color.dispose(); + try { + color.getDevice(); + fail("No exception thrown for getDevice on disposed Color"); + } catch (SWTException e) { + assertEquals("Color should have thrown device disposed on getDevice", SWT.ERROR_GRAPHIC_DISPOSED, e.code); + } +} + +@Test +public void test_getDevice_with_device() { + Color color = new Color(display, 0, 0, 255, 255); + try { + assertEquals("Color should return device as constructed", display, color.getDevice()); + } finally { + color.dispose(); + } + + color = new Color(display, 0, 0, 255, 255); + color.dispose(); + try { + color.getDevice(); + fail("No exception thrown for getDevice on disposed Color"); + } catch (SWTException e) { + assertEquals("Color should have thrown device disposed on getDevice", SWT.ERROR_GRAPHIC_DISPOSED, e.code); + } +} + /* custom */ Display display; } |