|
39 | 39 | import org.eclipse.swt.graphics.GC; |
40 | 40 | import org.eclipse.swt.graphics.Image; |
41 | 41 | import org.eclipse.swt.graphics.ImageData; |
| 42 | +import org.eclipse.swt.graphics.PaletteData; |
42 | 43 | import org.eclipse.swt.graphics.Point; |
43 | 44 | import org.eclipse.swt.layout.RowLayout; |
44 | 45 | import org.eclipse.swt.widgets.Display; |
@@ -135,40 +136,64 @@ public void testHtmlTransfer() throws InterruptedException { |
135 | 136 | } |
136 | 137 |
|
137 | 138 | /** |
138 | | - * DnD an image using the {@link ImageTransfer}. |
| 139 | + * DnD ImageData retrieved from an image (created as DDB) using the {@link ImageTransfer}. |
139 | 140 | */ |
140 | 141 | @Test |
141 | | -public void testImageTransfer() throws InterruptedException { |
142 | | - final Image image = new Image(shell.getDisplay(), 16, 16); |
| 142 | +public void testImageTransfer_fromImage() throws InterruptedException { |
| 143 | + final Image image = createTestImage(); |
143 | 144 | try { |
144 | | - Color color = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE); |
145 | | - GC gc = new GC(image); |
146 | | - gc.setBackground(color); |
147 | | - gc.fillRectangle(image.getBounds()); |
148 | | - gc.dispose(); |
149 | | - |
150 | 145 | final ImageData drag = image.getImageData(); |
151 | | - final ImageData drop; |
152 | | - |
153 | | - drop = testTransferRoundtrip(ImageTransfer.getInstance(), drag); |
154 | | - // ImageData has no custom equals method and the default one isn't sufficient |
155 | | - boolean equals = (drag == drop); |
156 | | - if (!equals && drag != null && drop != null) { |
157 | | - equals = (drag.width == drop.width && drag.height == drop.height); |
158 | | - assertEquals("TransparencyType", drag.getTransparencyType(),drop.getTransparencyType()); |
159 | | - if (equals) { |
160 | | - for (int y = 0; y < drag.height; y++) { |
161 | | - for (int x = 0; x < drag.width; x++) { |
162 | | - String dragPixel = String.format("0x%08X", drag.getPixel(x, y)); |
163 | | - String dropPixel = String.format("0x%08X", drop.getPixel(x, y)); |
164 | | - //FIXME win32: dragged ALPHA=FF, dropped ALPHA=00, but other transparencyType => alpha stored in ImageData.alphaData |
165 | | - assertEquals("Drop received other pixel as we dragged. x=" + x + " y=" + y, dragPixel, |
166 | | - dropPixel); |
167 | | - } |
168 | | - } |
169 | | - } |
| 146 | + final ImageData drop = testTransferRoundtrip(ImageTransfer.getInstance(), drag); |
| 147 | + assertImageDataEqualsIgoringAlphaInData(drag, drop); |
| 148 | + } finally { |
| 149 | + image.dispose(); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * DnD ImageData created from image copy using the {@link ImageTransfer}. |
| 155 | + */ |
| 156 | +@Test |
| 157 | +public void testImageTransfer_fromCopiedImage() throws InterruptedException { |
| 158 | + final Image image = createTestImage(); |
| 159 | + try { |
| 160 | + final ImageData drag = new Image(shell.getDisplay(), image, SWT.IMAGE_COPY).getImageData(); |
| 161 | + final ImageData drop = testTransferRoundtrip(ImageTransfer.getInstance(), drag); |
| 162 | + assertImageDataEqualsIgoringAlphaInData(drag, drop); |
| 163 | + } finally { |
| 164 | + image.dispose(); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * DnD manually created ImageData using the {@link ImageTransfer}. |
| 170 | + */ |
| 171 | +@Test |
| 172 | +public void testImageTransfer_fromImageData() throws InterruptedException { |
| 173 | + final ImageData imageData = new ImageData(16, 16, 32, new PaletteData(0xFF00, 0xFF0000, 0xFF000000)); |
| 174 | + for (int i = 0; i < imageData.data.length; i++) { |
| 175 | + imageData.data[i] = (byte) (i % 3 == 0 ? 128 : 0); |
| 176 | + } |
| 177 | + final ImageData drag = imageData; |
| 178 | + final ImageData drop = testTransferRoundtrip(ImageTransfer.getInstance(), drag); |
| 179 | + assertImageDataEqualsIgoringAlphaInData(drag, drop); |
| 180 | +} |
| 181 | + |
| 182 | +/** |
| 183 | + * DnD ImageData created from image data using the {@link ImageTransfer}. |
| 184 | + */ |
| 185 | +@Test |
| 186 | +public void testImageTransfer_fromImageDataFromImage() throws InterruptedException { |
| 187 | + final Image image = createTestImage(); |
| 188 | + try { |
| 189 | + Image imageFromImageData = new Image(shell.getDisplay(), image.getImageData()); |
| 190 | + try { |
| 191 | + final ImageData drag = imageFromImageData.getImageData(); |
| 192 | + final ImageData drop = testTransferRoundtrip(ImageTransfer.getInstance(), drag); |
| 193 | + assertImageDataEqualsIgoringAlphaInData(drag, drop); |
| 194 | + } finally { |
| 195 | + imageFromImageData.dispose(); |
170 | 196 | } |
171 | | - assertTrue("Drop received other data as we dragged.", equals); |
172 | 197 | } finally { |
173 | 198 | image.dispose(); |
174 | 199 | } |
@@ -210,6 +235,65 @@ public void testUrlTransfer() throws InterruptedException { |
210 | 235 | assertEquals("Drop received other data as we dragged.", drag, drop); |
211 | 236 | } |
212 | 237 |
|
| 238 | +/** |
| 239 | + * Creates a DDB test image with a uniform color applied to all pixels. |
| 240 | + */ |
| 241 | +private Image createTestImage() { |
| 242 | + final Image image = new Image(shell.getDisplay(), 16, 16); |
| 243 | + try { |
| 244 | + Color color = shell.getDisplay().getSystemColor(SWT.COLOR_DARK_BLUE); |
| 245 | + GC gc = new GC(image); |
| 246 | + gc.setBackground(color); |
| 247 | + gc.fillRectangle(image.getBounds()); |
| 248 | + gc.dispose(); |
| 249 | + } catch (Exception e) { |
| 250 | + image.dispose(); |
| 251 | + fail("test image could not be initialized: " + e); |
| 252 | + } |
| 253 | + return image; |
| 254 | +} |
| 255 | + |
| 256 | +/** |
| 257 | + * Asserts that both given ImageData are equal, i.e. that: |
| 258 | + * <ul> |
| 259 | + * <li>depths are equal (considering 24/32 bit as equals since alpha data is stored separately)</li> |
| 260 | + * <li>width and height are equal</li> |
| 261 | + * <li>all pixel RGB values are equal</li> |
| 262 | + * <li>all pixel alpha values in the alphaData are equal</li> |
| 263 | + * </ul> |
| 264 | + * In case any of these properties differ, the test will fail. |
| 265 | + * |
| 266 | + * @param expected the expected ImageData |
| 267 | + * @param actual the actual ImageData |
| 268 | + */ |
| 269 | +// This method is necessary because ImageData has no custom equals method and the default one isn't sufficient. |
| 270 | +private void assertImageDataEqualsIgoringAlphaInData(final ImageData expected, final ImageData actual) { |
| 271 | + assertNotNull("expected data must not be null", expected); |
| 272 | + assertNotNull("actual data must not be null", actual); |
| 273 | + if (expected == actual) { |
| 274 | + return; |
| 275 | + } |
| 276 | + assertEquals("height of expected image is different from actual image", expected.height, actual.height); |
| 277 | + // Alpha values are taken from alpha data, so ignore whether data depth is 24 or 32 bits |
| 278 | + int expectedNormalizedDepth = expected.depth == 32 ? 24 : expected.depth; |
| 279 | + int actualNormalizedDepth = expected.depth == 32 ? 24 : expected.depth; |
| 280 | + assertEquals("depth of image data to compare must be equal", expectedNormalizedDepth, actualNormalizedDepth); |
| 281 | + assertEquals("width of expected image is different from actual image", expected.width, actual.width); |
| 282 | + |
| 283 | + for (int y = 0; y < expected.height; y++) { |
| 284 | + for (int x = 0; x < expected.width; x++) { |
| 285 | + // FIXME win32: dragged ALPHA=FF, dropped ALPHA=00, but other transparencyType |
| 286 | + // => alpha stored in ImageData.alphaData |
| 287 | + String expectedPixel = String.format("0x%08X", expected.getPixel(x, y) >> (expected.depth == 32 ? 8 : 0)); |
| 288 | + String actualPixel = String.format("0x%08X", actual.getPixel(x, y) >> (actual.depth == 32 ? 8 : 0)); |
| 289 | + assertEquals("actual pixel at x=" + x + " y=" + y + " is different from expected pixel", expectedPixel, actualPixel); |
| 290 | + int expectedAlpha = expected.getAlpha(x, y); |
| 291 | + int actualAlpha = actual.getAlpha(x, y); |
| 292 | + assertEquals("actual pixel alpha at x=" + x + " y=" + y + " is different from expected pixel", expectedAlpha, actualAlpha); |
| 293 | + } |
| 294 | + } |
| 295 | +} |
| 296 | + |
213 | 297 | /** |
214 | 298 | * Test transfer implementation by dragging and dropping some data onto ourself. |
215 | 299 | * |
|
0 commit comments