Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Adding convenient method to test for exceptions thrown on the UI thread.
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan <jordan.deyton@gmail.com>
  • Loading branch information
jdeyton committed Jun 20, 2015
1 parent f68c37d commit 4bf9f46
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*******************************************************************************/
package org.eclipse.ice.client.widgets.test.utils;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.swt.SWTException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swtbot.swt.finder.SWTBot;
Expand Down Expand Up @@ -155,4 +158,32 @@ public void run() {
// Proceed with the default post-tests cleanup.
super.afterAllTests();
}

/**
* This can be used to verify an exception that is thrown on the UI thread.
*
* @param runnable
* The runnable that would normally be sent to
* {@link Display#syncExec(Runnable)}.
* @return The first {@link SWTException} that was thrown, or {@code null}
* if one was not thrown.
*/
protected SWTException catchSWTException(final Runnable runnable) {
// Use an AtomicReference to catch the exception.
final AtomicReference<SWTException> exceptionRef;
exceptionRef = new AtomicReference<SWTException>();
// Run the runnable synchronously, but try to catch the SWTException.
getDisplay().syncExec(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (SWTException e) {
exceptionRef.set(e);
}
}
});
// Return any SWTException that was thrown from the specified runnable.
return exceptionRef.get();
}
}

0 comments on commit 4bf9f46

Please sign in to comment.