Search in sources :

Example 41 with SWTException

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

the class ImageAnalyzer method showErrorDialog.

/*
	 * Open an error dialog displaying the specified information.
	 */
void showErrorDialog(String operation, String filename, Throwable e) {
    MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
    String message = createMsg(bundle.getString("Error"), new String[] { operation, filename });
    String errorMessage = "";
    if (e != null) {
        if (e instanceof SWTException) {
            SWTException swte = (SWTException) e;
            errorMessage = swte.getMessage();
            if (swte.throwable != null) {
                errorMessage += ":\n" + swte.throwable.toString();
            }
        } else if (e instanceof SWTError) {
            SWTError swte = (SWTError) e;
            errorMessage = swte.getMessage();
            if (swte.throwable != null) {
                errorMessage += ":\n" + swte.throwable.toString();
            }
        } else {
            errorMessage = e.toString();
        }
    }
    box.setMessage(message + errorMessage);
    box.open();
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 42 with SWTException

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

the class SwtTestUtil method assertSWTProblem.

public static void assertSWTProblem(String message, int expectedCode, Throwable actualThrowable) {
    if (actualThrowable instanceof SWTError) {
        SWTError error = (SWTError) actualThrowable;
        assertEquals(message, expectedCode, error.code);
    } else if (actualThrowable instanceof SWTException) {
        SWTException exception = (SWTException) actualThrowable;
        assertEquals(message, expectedCode, exception.code);
    } else {
        try {
            SWT.error(expectedCode);
        } catch (Throwable expectedThrowable) {
            if (actualThrowable.getMessage().length() > expectedThrowable.getMessage().length()) {
                assertTrue(message, actualThrowable.getMessage().startsWith(expectedThrowable.getMessage()));
            } else {
                assertEquals(message, expectedThrowable.getMessage(), actualThrowable.getMessage());
            }
        }
    }
}
Also used : SWTError(org.eclipse.swt.SWTError) SWTException(org.eclipse.swt.SWTException)

Example 43 with SWTException

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

the class Test_org_eclipse_swt_SWTException method test_printStackTrace.

@Test
public void test_printStackTrace() {
    try {
        Class.forName("java.io.PrintStream");
    } catch (ClassNotFoundException e) {
        // ignore test if running on CLDC
        return;
    }
    // test default SWTException
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    System.setErr(new PrintStream(out));
    SWTException error = new SWTException();
    error.printStackTrace();
    assertTrue(out.size() > 0);
    assertTrue(new String(out.toByteArray()).indexOf("test_printStackTrace") != -1);
    // test SWTException with code
    out = new ByteArrayOutputStream();
    System.setErr(new PrintStream(out));
    error = new SWTException(SWT.ERROR_INVALID_ARGUMENT);
    error.printStackTrace();
    assertTrue(out.size() > 0);
    assertTrue(new String(out.toByteArray()).indexOf("test_printStackTrace") != -1);
}
Also used : PrintStream(java.io.PrintStream) SWTException(org.eclipse.swt.SWTException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 44 with SWTException

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

the class Test_org_eclipse_swt_browser_Browser method test_OpenWindowListener_evaluateInCallback.

/**
 * Verify that evaluation works inside an OpenWindowListener
 */
@Test
public void test_OpenWindowListener_evaluateInCallback() {
    // This works on Webkit1, but can sporadically fail, see Bug 509411
    assumeTrue(!isWebkit1);
    AtomicBoolean eventFired = new AtomicBoolean(false);
    browser.addOpenWindowListener(event -> {
        browser.evaluate("SWTopenListener = true");
        eventFired.set(true);
        event.required = true;
    });
    shell.open();
    browser.evaluate("window.open()");
    boolean fired = waitForPassCondition(() -> eventFired.get());
    boolean evaluated = false;
    try {
        evaluated = (Boolean) browser.evaluate("return SWTopenListener");
    } catch (SWTException e) {
    }
    boolean passed = fired && evaluated;
    String errMsg = "Event fired:" + fired + "   evaluated:" + evaluated;
    assertTrue(errMsg, passed);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SWTException(org.eclipse.swt.SWTException) Test(org.junit.Test)

Example 45 with SWTException

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

the class Test_org_eclipse_swt_browser_Browser method test_evaluate_invalid_return_value.

/**
 * Test the evaluate() api that throws the invalid return value exception. Functionality based on Snippet308.
 * Only wait till success. Otherwise timeout after 3 seconds.
 */
@Test
public void test_evaluate_invalid_return_value() {
    // Bug 509411
    assumeFalse(webkit1SkipMsg(), isWebkit1);
    if (SwtTestUtil.isWindows) {
        /* Bug 508210 . Inconsistent beahiour on windows at the moment.
		 * Fixing requires deeper investigation. Disabling newly added test for now.
		 */
        return;
    }
    final AtomicInteger exception = new AtomicInteger(-1);
    browser.addProgressListener(completedAdapter(event -> {
        try {
            // Date is not supoprted as return value.
            browser.evaluate("return new Date()");
        } catch (SWTException e) {
            exception.set(e.code);
        }
    }));
    browser.setText("<html><body>HelloWorld</body></html>");
    shell.open();
    AtomicBoolean wrongExceptionCode = new AtomicBoolean(false);
    boolean passed = waitForPassCondition(() -> {
        if (exception.get() != -1) {
            if (exception.get() == SWT.ERROR_INVALID_RETURN_VALUE) {
                return true;
            } else if (exception.get() == SWT.ERROR_FAILED_EVALUATE) {
                wrongExceptionCode.set(true);
                return true;
            }
        }
        return false;
    });
    if (wrongExceptionCode.get()) {
        System.err.println("SWT Warning: test_evaluate_invalid_return_value threw wrong exception code." + " Expected ERROR_INVALID_RETURN_VALUE but got ERROR_FAILED_EVALUATE");
    }
    String message = exception.get() == -1 ? "Exception was not thrown. Test timed out" : "Exception thrown, but wrong code: " + exception.get();
    assertTrue(message, passed);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) StatusTextListener(org.eclipse.swt.browser.StatusTextListener) URL(java.net.URL) Assume.assumeFalse(org.junit.Assume.assumeFalse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocationAdapter(org.eclipse.swt.browser.LocationAdapter) CloseWindowListener(org.eclipse.swt.browser.CloseWindowListener) ProgressListener(org.eclipse.swt.browser.ProgressListener) TitleListener(org.eclipse.swt.browser.TitleListener) VisibilityWindowAdapter(org.eclipse.swt.browser.VisibilityWindowAdapter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Supplier(java.util.function.Supplier) Point(org.eclipse.swt.graphics.Point) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) BrowserFunction(org.eclipse.swt.browser.BrowserFunction) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) TestName(org.junit.rules.TestName) LocationListener.changedAdapter(org.eclipse.swt.browser.LocationListener.changedAdapter) VisibilityWindowListener.showAdapter(org.eclipse.swt.browser.VisibilityWindowListener.showAdapter) WindowEvent(org.eclipse.swt.browser.WindowEvent) LocationListener(org.eclipse.swt.browser.LocationListener) ProgressListener.completedAdapter(org.eclipse.swt.browser.ProgressListener.completedAdapter) Assert.fail(org.junit.Assert.fail) SWTException(org.eclipse.swt.SWTException) VisibilityWindowListener(org.eclipse.swt.browser.VisibilityWindowListener) FillLayout(org.eclipse.swt.layout.FillLayout) AtomicIntegerArray(java.util.concurrent.atomic.AtomicIntegerArray) Before(org.junit.Before) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Browser(org.eclipse.swt.browser.Browser) Shell(org.eclipse.swt.widgets.Shell) MalformedURLException(java.net.MalformedURLException) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Display(org.eclipse.swt.widgets.Display) Instant(java.time.Instant) LocationListener.changingAdapter(org.eclipse.swt.browser.LocationListener.changingAdapter) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) Assert.assertFalse(org.junit.Assert.assertFalse) SWT(org.eclipse.swt.SWT) LocationEvent(org.eclipse.swt.browser.LocationEvent) Assume.assumeTrue(org.junit.Assume.assumeTrue) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SWTException(org.eclipse.swt.SWTException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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