|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Vector Informatik GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + *******************************************************************************/ |
| 11 | +package org.eclipse.swt.tests.junit; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | + |
| 18 | +import org.eclipse.swt.SWT; |
| 19 | +import org.eclipse.swt.graphics.Color; |
| 20 | +import org.eclipse.swt.graphics.Image; |
| 21 | +import org.eclipse.swt.graphics.Pattern; |
| 22 | +import org.eclipse.swt.widgets.Display; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +/** |
| 28 | + * Automated Test Suite for class org.eclipse.swt.graphics.Pattern |
| 29 | + * |
| 30 | + * @see org.eclipse.swt.graphics.Pattern |
| 31 | + */ |
| 32 | +public class Test_org_eclipse_swt_graphics_Pattern { |
| 33 | + |
| 34 | + private Display display; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp() { |
| 38 | + display = Display.getDefault(); |
| 39 | + } |
| 40 | + |
| 41 | + @AfterEach |
| 42 | + public void tearDown() { |
| 43 | + // display is shared; do not dispose |
| 44 | + } |
| 45 | + |
| 46 | + // --- Image-based constructor --- |
| 47 | + |
| 48 | + @Test |
| 49 | + public void test_Constructor_image_valid() { |
| 50 | + Image image = new Image(display, 10, 10); |
| 51 | + try { |
| 52 | + Pattern pattern = new Pattern(display, image); |
| 53 | + assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed"); |
| 54 | + pattern.dispose(); |
| 55 | + } finally { |
| 56 | + image.dispose(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void test_Constructor_image_nullImage() { |
| 62 | + assertThrows(IllegalArgumentException.class, () -> new Pattern(display, (Image) null)); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void test_Constructor_image_disposedImage() { |
| 67 | + Image image = new Image(display, 10, 10); |
| 68 | + image.dispose(); |
| 69 | + assertThrows(IllegalArgumentException.class, () -> new Pattern(display, image)); |
| 70 | + } |
| 71 | + |
| 72 | + // --- Gradient constructors --- |
| 73 | + |
| 74 | + @Test |
| 75 | + public void test_Constructor_gradient_valid() { |
| 76 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 77 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 78 | + Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue); |
| 79 | + assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed"); |
| 80 | + pattern.dispose(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void test_Constructor_gradientAlpha_valid() { |
| 85 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 86 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 87 | + Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, 128, blue, 255); |
| 88 | + assertFalse(pattern.isDisposed(), "Newly constructed Pattern must not be disposed"); |
| 89 | + pattern.dispose(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void test_Constructor_gradient_nullColor1() { |
| 94 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 95 | + assertThrows(IllegalArgumentException.class, |
| 96 | + () -> new Pattern(display, 0, 0, 100, 100, null, blue)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void test_Constructor_gradient_nullColor2() { |
| 101 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 102 | + assertThrows(IllegalArgumentException.class, |
| 103 | + () -> new Pattern(display, 0, 0, 100, 100, red, null)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void test_Constructor_gradientAlpha_nullColor1() { |
| 108 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 109 | + assertThrows(IllegalArgumentException.class, |
| 110 | + () -> new Pattern(display, 0, 0, 100, 100, null, 255, blue, 255)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void test_Constructor_gradientAlpha_nullColor2() { |
| 115 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 116 | + assertThrows(IllegalArgumentException.class, |
| 117 | + () -> new Pattern(display, 0, 0, 100, 100, red, 255, null, 255)); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void test_Constructor_gradient_disposedColor1() { |
| 122 | + Color red = new Color(display, 255, 0, 0); |
| 123 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 124 | + red.dispose(); |
| 125 | + assertThrows(IllegalArgumentException.class, |
| 126 | + () -> new Pattern(display, 0, 0, 100, 100, red, blue)); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void test_Constructor_gradient_disposedColor2() { |
| 131 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 132 | + Color blue = new Color(display, 0, 0, 255); |
| 133 | + blue.dispose(); |
| 134 | + assertThrows(IllegalArgumentException.class, |
| 135 | + () -> new Pattern(display, 0, 0, 100, 100, red, blue)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void test_Constructor_gradientAlpha_disposedColor1() { |
| 140 | + Color red = new Color(display, 255, 0, 0); |
| 141 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 142 | + red.dispose(); |
| 143 | + assertThrows(IllegalArgumentException.class, |
| 144 | + () -> new Pattern(display, 0, 0, 100, 100, red, 255, blue, 255)); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void test_Constructor_gradientAlpha_disposedColor2() { |
| 149 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 150 | + Color blue = new Color(display, 0, 0, 255); |
| 151 | + blue.dispose(); |
| 152 | + assertThrows(IllegalArgumentException.class, |
| 153 | + () -> new Pattern(display, 0, 0, 100, 100, red, 255, blue, 255)); |
| 154 | + } |
| 155 | + |
| 156 | + // --- isDisposed --- |
| 157 | + |
| 158 | + @Test |
| 159 | + public void test_isDisposed() { |
| 160 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 161 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 162 | + Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue); |
| 163 | + |
| 164 | + assertFalse(pattern.isDisposed(), "Pattern must not be disposed before dispose() is called"); |
| 165 | + pattern.dispose(); |
| 166 | + assertTrue(pattern.isDisposed(), "Pattern must be disposed after dispose() is called"); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void test_dispose_twice() { |
| 171 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 172 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 173 | + Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue); |
| 174 | + |
| 175 | + // Disposing twice must not throw |
| 176 | + pattern.dispose(); |
| 177 | + pattern.dispose(); |
| 178 | + assertTrue(pattern.isDisposed()); |
| 179 | + } |
| 180 | + |
| 181 | + // --- toString --- |
| 182 | + |
| 183 | + @Test |
| 184 | + public void test_toString() { |
| 185 | + Color red = display.getSystemColor(SWT.COLOR_RED); |
| 186 | + Color blue = display.getSystemColor(SWT.COLOR_BLUE); |
| 187 | + Pattern pattern = new Pattern(display, 0, 0, 100, 100, red, blue); |
| 188 | + |
| 189 | + String s = pattern.toString(); |
| 190 | + assertNotNull(s, "toString() must not return null"); |
| 191 | + assertFalse(s.isEmpty(), "toString() must not return an empty string"); |
| 192 | + |
| 193 | + pattern.dispose(); |
| 194 | + s = pattern.toString(); |
| 195 | + assertNotNull(s, "toString() on disposed Pattern must not return null"); |
| 196 | + assertFalse(s.isEmpty(), "toString() on disposed Pattern must not return an empty string"); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments