Little trick with SWTBotTree manipulation if it returns an IllegalArgumentException

Blog Categories: 

I came across a strange issue with manipulating SWTBotTree, and it took me few hours to find a workaround. I found no exact match with my issue during my research, so let's keep a trace here while waiting for a better understanding of the issue and a fix in SWTBot.

The issue

In a SWTBot Test, calls to SWTBotTreeItem.contextMenu() or might lead to the following exception:

  1. Caused by: java.lang.IllegalArgumentException: Argument not valid
  2. at org.eclipse.swt.SWT.error(
  3. at org.eclipse.swt.SWT.error(
  4. at org.eclipse.swt.SWT.error(
  5. at org.eclipse.swt.widgets.Widget.error(
  6. at org.eclipse.swt.widgets.Tree.setSelection(
  7. at org.eclipse.swt.widgets.Tree.setSelection(
  8. at org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem$
  9. at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$4.doRun(
  10. at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$

Quite an obscure message... what is happening? How can I solve my issue?

The workaround

Easily applicable - but hard to guess - the workaround consists of simply calling SWTBotTree.setFocus() before manipulating the SWTBotTree. For instance:

  1. final SWTBotTree tree = bot.tree();
  2. tree.setFocus();
  3. final SWTBotTreeItem cssNodeFile = tree.expandNode("application", "css", "bonita_form_confirm.css");
  4. cssNodeFile.contextMenu("Open").click();

Technical details

If we look closer in Tree.setSelection method , the exception is due to a disposed TreeItem:

  1. TreeItem item = items [0];
  2. if (item != null) {
  3. if (item.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
  4. ...
  5. }

The thing which is strange is that the SWTBotTreeItem is calling setFocus before calling the setSelection, as you can see here in

  1. public SWTBotTreeItem select() {
  2. assertEnabled();
  3. syncExec(new VoidResult() {
  4. public void run() {
  5. tree.setFocus();
  6. tree.setSelection(widget);
  7. }
  8. });
  9. notifySelect();
  10. return this;
  11. }

If someone understands the issue better than me, please share your ideas in the bug report: