利用SWTBot中自定义Widget和Control的实现

        http://wiki.eclipse.org/SWTBot/Testing_Custom_Controls

SWTBot/Testing Custom Controls

 

Thomas Py wrote: > hI! > i´ve got a rcp-application with own swt-items.. for example there is a "QCombo extends Composite" how can i access this combobox with the swtbot? it´s not a ccombobox and not a combobox so i dont know how can i change the selected item with the swtbot. can swtbot list all widgets? > thnx for any help

Finding the widget is quite easy. Using it is something else...

To find your QCombo widget, do this:

QCombo widget = bot.widget(widgetOfType(QCombo.class));

Now to use it, you can't just do QCombo.setSelectedItem() (or whatever API it might have), you must ensure that the code that needs running in the UI thread be run in the UI thread, using sync or async execution. For some quick testing, you could do this:

asyncExec(new VoidResult(){
   void run(){
      widget.setSelectedItem();
   }
}

but it quickly burdens your test code. What you should do instead is use the page object pattern: make a wrapper for your widget. Ex:

public class SWTBotQCombo extends AbstractSWTBot<QCombo>{
 
   public SWTBotQCombo(QCombo w) throws WidgetNotFoundException {
      this(w, null);
   }
 
   public SWTBotQCombo(QCombo w, SelfDescribing description) throws WidgetNotFoundException {
      super(w, description);
   }
 
   public void setSelectedItem() {
      asyncExec(new VoidResult(){
         void run() {
            widget.setSelectedItem();
         }
      }
   }
   ...
}

在swtbot中所有swt的widget控件必须实现AbstractSWTBot类实现.

 

利用SWTBot中自定义Widget和Control的实现

AbstractSWTBot泛型实现类为:

public abstract class AbstractSWTBot<T extends Widget> {

   在AbstractSWTBot中触发非阻塞的事件的方法如下:

protected void notify(final int eventType, final Event createEvent) 

   为实现该类的子类使用提供了条件.

同步执行的方法为syncExec的集中重载如下:

/**
	 * Invokes {@link ArrayResult#run()} on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the array returned by toExecute.
	 */
	protected <T> T[] syncExec(ArrayResult<T> toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link VoidResult#run()} on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 */
	protected void syncExec(VoidResult toExecute) {
		UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link ListResult#run()} on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the list returned by toExecute
	 */
	protected <E> List<E> syncExec(ListResult<E> toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link BoolResult#run()} synchronously on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the boolean returned by toExecute
	 */
	protected boolean syncExec(BoolResult toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link BoolResult#run()} synchronously on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the boolean returned by toExecute
	 */

	protected String syncExec(StringResult toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link Result#run()} synchronously on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the boolean returned by toExecute
	 */
	protected <T> T syncExec(Result<T> toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link WidgetResult#run()} synchronously on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the Widget returned by toExecute
	 */
	protected T syncExec(WidgetResult<T> toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

	/**
	 * Invokes {@link IntResult#run()} synchronously on the UI thread.
	 *
	 * @param toExecute the object to be invoked in the UI thread.
	 * @return the integer returned by toExecute
	 */

	protected int syncExec(IntResult toExecute) {
		return UIThreadRunnable.syncExec(display, toExecute);
	}

 

异步执行的方法asyncExec如下:

 /**
  * Invokes {@link BoolResult#run()} asynchronously on the UI thread.
  *
  * @param toExecute the object to be invoked in the UI thread.
  */
 protected void asyncExec(VoidResult toExecute) {
  UIThreadRunnable.asyncExec(display, toExecute);
 }

 

快捷键实现如下:

/**
	 * Presses the shortcut specified by the given keys.
	 *
	 * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} |
	 *            {@link SWT#COMMAND}.
	 * @param c the character
	 * @return the same instance
	 * @see Keystrokes#toKeys(int, char)
	 */
	public AbstractSWTBot<T> pressShortcut(int modificationKeys, char c) {
		waitForEnabled();
		setFocus();
		keyboard().pressShortcut(modificationKeys, c);
		return this;
	}

	/**
	 * Presses the shortcut specified by the given keys.
	 *
	 * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} | {@link SWT#COMMAND}.
	 * @param keyCode the keyCode, these may be special keys like F1-F12, or navigation keys like HOME, PAGE_UP
	 * @param c the character
	 * @return the same instance
	 * @see Keystrokes#toKeys(int, char)
	 */
	public AbstractSWTBot<T> pressShortcut(int modificationKeys, int keyCode, char c) {
		waitForEnabled();
		setFocus();
		keyboard().pressShortcut(modificationKeys, keyCode, c);
		return this;
	}

	/**
	 * Presses the shortcut specified by the given keys.
	 *
	 * @param keys the keys to press
	 * @return the same instance
	 * @see Keyboard#pressShortcut(KeyStroke...)
	 * @see Keystrokes
	 */
	public AbstractSWTBot<T> pressShortcut(KeyStroke... keys) {
		waitForEnabled();
		setFocus();
		keyboard().pressShortcut(keys);
		return this;
	}

 

你可能感兴趣的:(SWT,swtbot)