org.eclipse.swtbot.swt.finder.utils
Class SWTUtils

java.lang.Object
  extended by org.eclipse.swtbot.swt.finder.utils.SWTUtils

public abstract class SWTUtils
extends Object

Version:
$Id$
Author:
Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com>

Constructor Summary
SWTUtils()
           
 
Method Summary
static Thread[] allThreads()
          Gets all the thread in the VM.
static boolean captureScreenshot(String fileName)
          This captures a screen shot and saves it to the given file.
static boolean captureScreenshot(String fileName, Control control)
          This captures a screen shot of a widget and saves it to the given file.
static boolean captureScreenshot(String fileName, Rectangle bounds)
          This captures a screen shot of an area and saves it to the given file.
static Display display()
          Caches the display for later use.
static Object getAttribute(Object object, String attributeName)
          Get the value of an attribute on the object even if the attribute is not accessible.
static String getText(Object obj)
          Gets the text of the given object.
static String getToolTipText(Object obj)
          Gets the tooltip text for the given object.
static boolean hasStyle(Widget w, int style)
          Checks if the widget has the given style.
static Object invokeMethod(Object object, String methodName)
          Invokes the specified methodName on the object, and returns the result, or null if the method returns void.
static boolean isCarbon()
           
static boolean isCocoa()
           
static boolean isEmptyOrNullText(Widget w)
          Checks if the widget text is an empty string.
static boolean isMac()
           
static boolean isUIThread()
          Return true if the current thread is the UI thread.
static boolean isUIThread(Display display)
          Return true if the current thread is the UI thread.
static Widget nextWidget(Widget w)
          Gets the next sibling of this passed in widget.
static Widget previousWidget(Widget w)
          Gets the previous sibling of the passed in widget.
static ThreadGroup primaryThreadGroup()
          Gets the primary thread group.
static Widget[] siblings(Widget w)
           
static void sleep(long millis)
          Sleeps for the given number of milliseconds.
static String toString(Display display, Object o)
          Convers the display and object to a string.
static String toString(Widget w)
          Converts the given widget to a string.
static void waitForDisplayToAppear()
          Waits until a display appears.
static void waitForDisplayToAppear(long timeout)
          Waits until a display appears.
static int widgetIndex(Widget w)
          Gets the index of the given widget in its current container.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SWTUtils

public SWTUtils()
Method Detail

siblings

public static Widget[] siblings(Widget w)
Parameters:
w - the widget
Returns:
the siblings of the widget, or an empty array, if there are none.

widgetIndex

public static int widgetIndex(Widget w)
Gets the index of the given widget in its current container.

Parameters:
w - the widget
Returns:
-1 if the the widget is null or if the widget does not have a parent; a number greater than or equal to zero indicating the index of the widget among its siblings

previousWidget

public static Widget previousWidget(Widget w)
Gets the previous sibling of the passed in widget.

Parameters:
w - the widget
Returns:
the previous sibling of w

nextWidget

public static Widget nextWidget(Widget w)
Gets the next sibling of this passed in widget.

Parameters:
w - the widget.
Returns:
the sibling of the specified widget, or null if it has none.

getText

public static String getText(Object obj)
Gets the text of the given object.

Parameters:
obj - the object which should be a widget.
Returns:
the result of invocation of Widget#getText()

getToolTipText

public static String getToolTipText(Object obj)
Gets the tooltip text for the given object.

Parameters:
obj - the object which should be a widget.
Returns:
the result of invocation of Widget#getToolTipText()
Since:
1.0

toString

public static String toString(Widget w)
Converts the given widget to a string.

Parameters:
w - the widget.
Returns:
the string representation of the widget.

toString

public static String toString(Display display,
                              Object o)
Convers the display and object to a string.

Parameters:
display - the display on which the object should be evaluated.
o - the object to evaluate.
Returns:
the string representation of the object when evaluated in the display thread.

hasStyle

public static boolean hasStyle(Widget w,
                               int style)
Checks if the widget has the given style.

Parameters:
w - the widget.
style - the style.
Returns:
true if the widget has the specified style bit set. Otherwise false.

sleep

public static void sleep(long millis)
Sleeps for the given number of milliseconds.

Parameters:
millis - the time in milliseconds to sleep.

allThreads

public static Thread[] allThreads()
Gets all the thread in the VM.

Returns:
all the threads in the VM.

primaryThreadGroup

public static ThreadGroup primaryThreadGroup()
Gets the primary thread group.

Returns:
the top level thread group.

display

public static Display display()
Caches the display for later use.

Returns:
the display.

isEmptyOrNullText

public static boolean isEmptyOrNullText(Widget w)
Checks if the widget text is an empty string.

Parameters:
w - the widget
Returns:
true if the widget does not have any text set on it. Othrewise false.

invokeMethod

public static Object invokeMethod(Object object,
                                  String methodName)
                           throws NoSuchMethodException,
                                  IllegalAccessException,
                                  InvocationTargetException
Invokes the specified methodName on the object, and returns the result, or null if the method returns void.

Parameters:
object - the object
methodName - the method name
Returns:
the result of invoking the method on the object
Throws:
NoSuchMethodException - if the method methodName does not exist.
IllegalAccessException - if the java access control does not allow invocation.
InvocationTargetException - if the method methodName throws an exception.
Since:
1.0
See Also:
Method.invoke(Object, Object[])

getAttribute

public static Object getAttribute(Object object,
                                  String attributeName)
                           throws SecurityException,
                                  NoSuchFieldException,
                                  IllegalArgumentException,
                                  IllegalAccessException
Get the value of an attribute on the object even if the attribute is not accessible.

Parameters:
object - the object
attributeName - the attribute name
Returns:
the value the attribute value
Throws:
SecurityException - if the attribute accessibility may not be changed.
NoSuchFieldException - if the attribute attributeName does not exist.
IllegalAccessException - if the java access control does not allow access.
IllegalArgumentException

captureScreenshot

public static boolean captureScreenshot(String fileName)
This captures a screen shot and saves it to the given file.

Parameters:
fileName - the filename to save screenshot to.
Returns:
true if the screenshot was created and saved, false otherwise.
Since:
1.0

captureScreenshot

public static boolean captureScreenshot(String fileName,
                                        Control control)
This captures a screen shot of a widget and saves it to the given file.

Parameters:
fileName - the filename to save screenshot to.
control - the control
Returns:
true if the screenshot was created and saved, false otherwise.
Since:
2.0

captureScreenshot

public static boolean captureScreenshot(String fileName,
                                        Rectangle bounds)
This captures a screen shot of an area and saves it to the given file.

Parameters:
fileName - the filename to save screenshot to.
bounds - the area to capture.
Returns:
true if the screenshot was created and saved, false otherwise.
Since:
2.0

waitForDisplayToAppear

public static void waitForDisplayToAppear()
Waits until a display appears.

Throws:
TimeoutException - if the condition does not evaluate to true after SWTBotPreferences.TIMEOUT milliseconds.

waitForDisplayToAppear

public static void waitForDisplayToAppear(long timeout)
Waits until a display appears.

Parameters:
timeout - the timeout in ms.
Throws:
TimeoutException - if the condition does not evaluate to true after timeoutms milliseconds.

isUIThread

public static boolean isUIThread(Display display)
Return true if the current thread is the UI thread.

Parameters:
display - the display
Returns:
true if the current thread is the UI thread, false otherwise.

isUIThread

public static boolean isUIThread()
Return true if the current thread is the UI thread.

Returns:
true if this instance is running in the UI thread, false otherwise.

isMac

public static boolean isMac()
Returns:
true if the current OS is macosx.

isCocoa

public static boolean isCocoa()
Returns:
true if the current platform is cocoa

isCarbon

public static boolean isCarbon()
Returns:
true if the current platform is carbon