Little trick with SWTBotTree manipulation if it returns an IllegalArgumentException

aurelien.pupier
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 SWTBotTreeItem.select() might lead to the following exception:

  1. Caused by: java.lang.IllegalArgumentException: Argument not valid
  2. at org.eclipse.swt.SWT.error(SWT.java:4422)
  3. at org.eclipse.swt.SWT.error(SWT.java:4356)
  4. at org.eclipse.swt.SWT.error(SWT.java:4327)
  5. at org.eclipse.swt.widgets.Widget.error(Widget.java:476)
  6. at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4986)
  7. at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4948)
  8. at org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem$10.run(SWTBotTreeItem.java:362)
  9. at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$4.doRun(UIThreadRunnable.java:196)
  10. at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$1.run(UIThreadRunnable.java:89)

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 SWTBotTreeItem.select():

  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: https://bugs.eclipse.org/bugs/show_bug.cgi?id=458975

Notifications