diff options
Diffstat (limited to 'tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java')
-rw-r--r-- | tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Color.java | 398 |
1 files changed, 396 insertions, 2 deletions
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; } |