Search in sources :

Example 11 with SWTException

use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Image method test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider.

@Test
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageFileNameProvider() {
    // Null provider
    ImageFileNameProvider provider = null;
    try {
        Image image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for file name == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_NULL_ARGUMENT, e);
    }
    // Invalid provider
    provider = zoom -> null;
    try {
        Image image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for non-existent file name");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    // Valid provider
    Image image = new Image(display, imageFileNameProvider);
    image.dispose();
    // Corrupt Image provider
    provider = zoom -> {
        String fileName;
        switch(zoom) {
            case 100:
                fileName = "corrupt.png";
                break;
            case 150:
                fileName = "corrupt.png";
                break;
            case 200:
                fileName = "corrupt.png";
                break;
            default:
                return null;
        }
        return getPath(fileName);
    };
    try {
        image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for corrupt image file.");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
    }
    // Valid provider only 100% zoom
    provider = zoom -> {
        String fileName;
        switch(zoom) {
            case 100:
                fileName = "collapseall.png";
                break;
            case 150:
            case 200:
            default:
                return null;
        }
        return getPath(fileName);
    };
    image = new Image(display, provider);
    image.dispose();
}
Also used : SWTException(org.eclipse.swt.SWTException) ImageFileNameProvider(org.eclipse.swt.graphics.ImageFileNameProvider) Image(org.eclipse.swt.graphics.Image) Test(org.junit.Test)

Example 12 with SWTException

use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Image method test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider.

@Test
public void test_ConstructorLorg_eclipse_swt_graphics_Device_ImageDataProvider() {
    // Null provider
    ImageDataProvider provider = null;
    try {
        Image image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for file name == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_NULL_ARGUMENT, e);
    }
    // Invalid provider
    provider = zoom -> null;
    try {
        Image image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for non-existent file name");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for provider == null", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    // Valid provider
    Image image = new Image(display, imageDataProvider);
    image.dispose();
    // Corrupt Image provider
    provider = zoom -> {
        String fileName;
        switch(zoom) {
            case 100:
                fileName = "corrupt.png";
                break;
            case 150:
                fileName = "corrupt.png";
                break;
            case 200:
                fileName = "corrupt.png";
                break;
            default:
                return null;
        }
        return new ImageData(getPath(fileName));
    };
    try {
        image = new Image(display, provider);
        image.dispose();
        fail("No exception thrown for corrupt image file.");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for provider with corrupt images", SWT.ERROR_INVALID_IMAGE, e);
    }
    // Valid provider only 100% zoom
    provider = zoom -> {
        String fileName;
        switch(zoom) {
            case 100:
                fileName = "collapseall.png";
                break;
            case 150:
            case 200:
            default:
                return null;
        }
        return new ImageData(getPath(fileName));
    };
    image = new Image(display, provider);
    image.dispose();
}
Also used : SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Image(org.eclipse.swt.graphics.Image) ImageDataProvider(org.eclipse.swt.graphics.ImageDataProvider) Test(org.junit.Test)

Example 13 with SWTException

use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Image method test_getBounds.

@Test
public void test_getBounds() {
    Rectangle bounds = new Rectangle(0, 0, 10, 20);
    Image image = new Image(display, bounds.width, bounds.height);
    image.dispose();
    try {
        image.getBounds();
        fail("No exception thrown for disposed image");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
    }
    // creates bitmap image
    image = new Image(display, bounds.width, bounds.height);
    Rectangle bounds1 = image.getBounds();
    image.dispose();
    assertEquals(":a:", bounds, bounds1);
    image = new Image(display, bounds);
    bounds1 = image.getBounds();
    image.dispose();
    assertEquals(":b:", bounds, bounds1);
    // create icon image
    ImageData imageData = new ImageData(bounds.width, bounds.height, 1, new PaletteData(new RGB(0, 0, 0)));
    image = new Image(display, imageData);
    bounds1 = image.getBounds();
    image.dispose();
    assertEquals(":c:", bounds, bounds1);
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 14 with SWTException

use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_Image method test_setBackgroundLorg_eclipse_swt_graphics_Color.

@Test
public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
    if (SwtTestUtil.isGTK) {
        // TODO Fix GTK failure.
        if (SwtTestUtil.verbose) {
            System.out.println("Excluded test_setBackgroundLorg_eclipse_swt_graphics_Color(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_Image)");
        }
        return;
    }
    Image image = new Image(display, 10, 10);
    try {
        image.setBackground(null);
        fail("No exception thrown for color == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for color == null", SWT.ERROR_NULL_ARGUMENT, e);
    } finally {
        image.dispose();
    }
    image = new Image(display, 10, 10);
    Color color = new Color(display, 255, 255, 255);
    color.dispose();
    try {
        image.setBackground(color);
        fail("No exception thrown for disposed color");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for disposed color", SWT.ERROR_INVALID_ARGUMENT, e);
    } finally {
        image.dispose();
    }
    image = new Image(display, 10, 10);
    image.dispose();
    color = new Color(display, 255, 255, 255);
    try {
        image.setBackground(color);
        fail("No exception thrown for disposed image");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
    } finally {
        color.dispose();
    }
    // this image does not have a transparent pixel by default so setBackground has no effect
    image = new Image(display, 10, 10);
    image.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    color = image.getBackground();
    assertNull("background color should be null for non-transparent image", color);
    image.dispose();
    // create an image with transparency and then set the background color
    ImageData imageData = new ImageData(10, 10, 2, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255), new RGB(50, 100, 150)));
    // transparent pixel is currently black
    imageData.transparentPixel = 0;
    image = new Image(display, imageData);
    image.setBackground(display.getSystemColor(SWT.COLOR_GREEN));
    color = image.getBackground();
    assertEquals("background color should have been set to green", display.getSystemColor(SWT.COLOR_GREEN), color);
    image.dispose();
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) Color(org.eclipse.swt.graphics.Color) Image(org.eclipse.swt.graphics.Image) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Example 15 with SWTException

use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.

the class Test_org_eclipse_swt_graphics_ImageData method test_getPixelsIII$BI.

@Test
public void test_getPixelsIII$BI() {
    final int SIZE = 20;
    final int GET_WIDTH = 10;
    final int OFFSET = 10;
    byte[] pixelData = new byte[SIZE];
    // test 1 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 1, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":a:", 0, pixelData[i]);
    }
    byte[] values = new byte[] { 0x1, 0x1, 0x1, 0x1, 0x1 };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":b:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":c:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":d:", 0, pixelData[i]);
        }
    }
    // test 2 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 2, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":e:", 0, pixelData[i]);
    }
    values = new byte[] { 0x1, 0x2, 0x3, 0x2, 0x1 };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":f:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":g:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":h:", 0, pixelData[i]);
        }
    }
    // test 4 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 4, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":i:", 0, pixelData[i]);
    }
    values = new byte[] { 0x1, 0x2, 0x3, 0x4, 0xF };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":j:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":k:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":l:", 0, pixelData[i]);
        }
    }
    // test 8 bit
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 8, new PaletteData(new RGB(0, 0, 0), new RGB(255, 255, 255)));
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        assertEquals(":m:", 0, pixelData[i]);
    }
    values = new byte[] { 0x1, 0x2, 0x3, 0xF, (byte) 0xFF };
    imageData.setPixels(0, 1, values.length, values, 0);
    imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
    for (int i = 0; i < pixelData.length; i++) {
        if (i < OFFSET) {
            assertEquals(":n:", 0, pixelData[i]);
        } else if (i < OFFSET + values.length) {
            assertEquals(":o:", values[i - OFFSET], pixelData[i]);
        } else if (i < OFFSET + GET_WIDTH) {
            assertEquals(":p:", 0, pixelData[i]);
        }
    }
    // exception cases
    try {
        imageData.getPixels(0, 1, GET_WIDTH * GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for getWidth out of bounds");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        imageData.getPixels(0, 1, GET_WIDTH, (byte[]) null, OFFSET);
        fail("No exception thrown for pixels == null");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for pixels == null", SWT.ERROR_NULL_ARGUMENT, e);
    }
    try {
        imageData.getPixels(-1, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(IMAGE_DIMENSION, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for x out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for x out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, -1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, IMAGE_DIMENSION, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for y out of bounds");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for y out of bounds", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    try {
        imageData.getPixels(0, 1, -1, pixelData, OFFSET);
        fail("No exception thrown for getWidth < 0");
    } catch (IllegalArgumentException e) {
        assertSWTProblem("Incorrect exception thrown for getWidth < 0", SWT.ERROR_INVALID_ARGUMENT, e);
    }
    imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 32, new PaletteData(0xFF0000, 0xFF00, 0xFF));
    try {
        imageData.getPixels(0, 1, GET_WIDTH, pixelData, OFFSET);
        fail("No exception thrown for invalid depth");
    } catch (SWTException e) {
        assertSWTProblem("Incorrect exception thrown for invalid depth", SWT.ERROR_UNSUPPORTED_DEPTH, e);
    }
}
Also used : PaletteData(org.eclipse.swt.graphics.PaletteData) SWTException(org.eclipse.swt.SWTException) ImageData(org.eclipse.swt.graphics.ImageData) RGB(org.eclipse.swt.graphics.RGB) Test(org.junit.Test)

Aggregations

SWTException (org.eclipse.swt.SWTException)57 Test (org.junit.Test)29 Image (org.eclipse.swt.graphics.Image)21 Rectangle (org.eclipse.swt.graphics.Rectangle)14 ImageData (org.eclipse.swt.graphics.ImageData)13 IOException (java.io.IOException)8 Region (org.eclipse.swt.graphics.Region)8 Display (org.eclipse.swt.widgets.Display)8 SWTError (org.eclipse.swt.SWTError)7 PaletteData (org.eclipse.swt.graphics.PaletteData)7 RGB (org.eclipse.swt.graphics.RGB)7 InputStream (java.io.InputStream)6 Point (org.eclipse.swt.graphics.Point)6 MessageBox (org.eclipse.swt.widgets.MessageBox)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Cursor (org.eclipse.swt.graphics.Cursor)4 ImageLoader (org.eclipse.swt.graphics.ImageLoader)4 FileDialog (org.eclipse.swt.widgets.FileDialog)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Shell (org.eclipse.swt.widgets.Shell)3