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_DeviceLjava_io_InputStream.
@Test
public void test_ConstructorLorg_eclipse_swt_graphics_DeviceLjava_io_InputStream() {
InputStream stream = null;
Image image = null;
try {
try {
image = new Image(display, stream);
image.dispose();
fail("No exception thrown for InputStream == null");
} catch (IllegalArgumentException e) {
assertSWTProblem("Incorrect exception thrown for InputStream == null", SWT.ERROR_NULL_ARGUMENT, e);
}
stream = SwtTestUtil.class.getResourceAsStream("empty.txt");
try {
image = new Image(display, stream);
image.dispose();
try {
stream.close();
} catch (IOException e) {
}
fail("No exception thrown for invalid InputStream");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for invalid InputStream", SWT.ERROR_UNSUPPORTED_FORMAT, e);
}
int numFormats = SwtTestUtil.imageFormats.length;
String fileName = SwtTestUtil.invalidImageFilenames[0];
Display[] displays = { display, null };
for (int j = 0; j < displays.length; j++) {
// Display tempDisplay = displays[j];
for (int i = 0; i < numFormats; i++) {
String format = SwtTestUtil.imageFormats[i];
stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format);
try {
image = new Image(display, stream);
image.dispose();
try {
stream.close();
} catch (IOException e) {
}
fail("No exception thrown for invalid InputStream");
} catch (SWTException e) {
// Bug 70167 - Image(Device, InputStream) throws incorrect exception for bad PNG
// remove comment when bug is fixed.
// assertEquals("Incorrect exception thrown for invalid image InputStream", SWT.ERROR_INVALID_IMAGE, e);
}
}
}
stream = SwtTestUtil.class.getResourceAsStream(SwtTestUtil.invalidImageFilenames[1]);
try {
image = new Image(display, stream);
image.dispose();
try {
stream.close();
} catch (IOException e) {
}
fail("No exception thrown for invalid InputStream");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for invalid image InputStream", SWT.ERROR_INVALID_IMAGE, e);
}
// create valid images
for (int j = 0; j < displays.length; j++) {
Display tempDisplay = displays[j];
int numFileNames = SwtTestUtil.imageFilenames.length;
for (int k = 0; k < numFileNames; k++) {
fileName = SwtTestUtil.imageFilenames[k];
for (int i = 0; i < numFormats; i++) {
String format = SwtTestUtil.imageFormats[i];
stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format);
image = new Image(tempDisplay, stream);
image.dispose();
try {
stream.close();
} catch (IOException e) {
}
}
}
}
} finally {
try {
stream.close();
} catch (Exception e) {
}
}
}
use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_graphics_Image method test_getBackground.
@Test
public void test_getBackground() {
Image image = new Image(display, 10, 10);
image.dispose();
try {
image.getBackground();
fail("No exception thrown for disposed image");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for disposed image", SWT.ERROR_GRAPHIC_DISPOSED, e);
}
// remainder tested in setBackground method
}
use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_graphics_ImageData method test_setPixelsIII$BI.
@Test
public void test_setPixelsIII$BI() {
final int SIZE = 20;
final int OFFSET = 1;
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)));
byte[] values = new byte[] { 0x1, 0x1, 0x1, 0x1, 0x1 };
imageData.setPixels(0, 1, values.length - OFFSET, values, OFFSET);
imageData.getPixels(0, 1, IMAGE_DIMENSION, pixelData, 0);
for (int i = 0; i < pixelData.length; i++) {
if (i + OFFSET < values.length) {
assertEquals(":a:", values[i + OFFSET], pixelData[i]);
} else {
assertEquals(":b:", 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)));
values = new byte[] { 0x1, 0x2, 0x3, 0x2, 0x1 };
imageData.setPixels(0, 1, values.length - OFFSET, values, OFFSET);
imageData.getPixels(0, 1, IMAGE_DIMENSION, pixelData, 0);
for (int i = 0; i < pixelData.length; i++) {
if (i + OFFSET < values.length) {
assertEquals(":c:", values[i + OFFSET], pixelData[i]);
} else {
assertEquals(":d:", 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)));
values = new byte[] { 0x1, 0x2, 0x3, 0x4, 0xF };
imageData.setPixels(0, 1, values.length - OFFSET, values, OFFSET);
imageData.getPixels(0, 1, IMAGE_DIMENSION, pixelData, 0);
for (int i = 0; i < pixelData.length; i++) {
if (i + OFFSET < values.length) {
assertEquals(":e:", values[i + OFFSET], pixelData[i]);
} else {
assertEquals(":f:", 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)));
values = new byte[] { 0x1, 0x2, 0x3, 0xF, (byte) 0xFF };
imageData.setPixels(0, 1, values.length - OFFSET, values, OFFSET);
imageData.getPixels(0, 1, IMAGE_DIMENSION, pixelData, 0);
for (int i = 0; i < pixelData.length; i++) {
if (i + OFFSET < values.length) {
assertEquals(":g:", values[i + OFFSET], pixelData[i]);
} else {
assertEquals(":h:", 0, pixelData[i]);
}
}
// exception cases
try {
imageData.setPixels(0, 1, IMAGE_DIMENSION * IMAGE_DIMENSION, pixelData, OFFSET);
fail("No exception thrown for putWidth out of bounds");
} catch (IndexOutOfBoundsException e) {
}
try {
imageData.setPixels(0, 1, IMAGE_DIMENSION, (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.setPixels(-1, 1, IMAGE_DIMENSION, 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.setPixels(IMAGE_DIMENSION, 1, IMAGE_DIMENSION, 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.setPixels(0, -1, IMAGE_DIMENSION, 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.setPixels(0, IMAGE_DIMENSION, IMAGE_DIMENSION, 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.setPixels(0, 1, -1, pixelData, OFFSET);
fail("No exception thrown for putWidth < 0");
} catch (IllegalArgumentException e) {
assertSWTProblem("Incorrect exception thrown for putWidth < 0", SWT.ERROR_INVALID_ARGUMENT, e);
}
imageData = new ImageData(IMAGE_DIMENSION, IMAGE_DIMENSION, 32, new PaletteData(0xFF0000, 0xFF00, 0xFF));
try {
imageData.setPixels(0, 1, IMAGE_DIMENSION, pixelData, OFFSET);
fail("No exception thrown for invalid depth");
} catch (SWTException e) {
assertSWTProblem("Incorrect exception thrown for invalid depth", SWT.ERROR_UNSUPPORTED_DEPTH, e);
}
}
use of org.eclipse.swt.SWTException in project eclipse.platform.swt by eclipse.
the class Test_org_eclipse_swt_graphics_ImageData method test_ConstructorLjava_io_InputStream.
@Test
public void test_ConstructorLjava_io_InputStream() {
try (InputStream stream = null) {
new ImageData(stream);
fail("No exception thrown for InputStream == null");
} catch (IllegalArgumentException | IOException e) {
}
try (InputStream stream = SwtTestUtil.class.getResourceAsStream("empty.txt")) {
new ImageData(stream);
fail("No exception thrown for invalid InputStream");
} catch (SWTException | IOException e) {
}
int numFormats = SwtTestUtil.imageFormats.length;
String fileName = SwtTestUtil.imageFilenames[0];
for (int i = 0; i < numFormats; i++) {
String format = SwtTestUtil.imageFormats[i];
try (InputStream stream = SwtTestUtil.class.getResourceAsStream(fileName + "." + format)) {
new ImageData(stream);
} catch (IOException e) {
}
}
}
use of org.eclipse.swt.SWTException in project uiautomatorviewer by yangzaiCN.
the class UiAutomatorModel method loadScreenshotAndXmlDump.
public boolean loadScreenshotAndXmlDump(File screenshotFile, File xmlDumpFile) {
if (screenshotFile != null && xmlDumpFile != null && screenshotFile.isFile() && xmlDumpFile.isFile()) {
ImageData[] data = null;
Image img = null;
try {
// use SWT's ImageLoader to read png from path
data = new ImageLoader().load(screenshotFile.getAbsolutePath());
} catch (SWTException e) {
e.printStackTrace();
return false;
}
// i.e. gifs or icons, we just care if it has at least one here
if (data.length < 1)
return false;
BasicTreeNode rootNode = new UiHierarchyXmlLoader().parseXml(xmlDumpFile.getAbsolutePath());
if (rootNode == null) {
System.err.println("null rootnode after parsing.");
return false;
}
try {
// Image is tied to ImageData and a Display, so we only need to create once
// per new image
img = new Image(mView.getShell().getDisplay(), data[0]);
} catch (SWTException e) {
e.printStackTrace();
return false;
}
// only update screenhot and xml if both are loaded successfully
if (mScreenshot != null) {
mScreenshot.dispose();
}
mScreenshot = img;
if (mRootNode != null) {
mRootNode.clearAllChildren();
}
// TODO: we should verify here if the coordinates in the XML matches the png
// or not: think loading a phone screenshot with a tablet XML dump
mRootNode = rootNode;
mScreenshotFile = screenshotFile;
mXmlDumpFile = xmlDumpFile;
mExploreMode = true;
mView.loadScreenshotAndXml();
return true;
}
return false;
}
Aggregations