Little trick with SWTBotTree manipulation if it returns an IllegalArgumentException

aurelien.pupier's picture
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:

Caused by: java.lang.IllegalArgumentException: Argument not valid
    at org.eclipse.swt.SWT.error(SWT.java:4422)
    at org.eclipse.swt.SWT.error(SWT.java:4356)
    at org.eclipse.swt.SWT.error(SWT.java:4327)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:476)
    at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4986)
    at org.eclipse.swt.widgets.Tree.setSelection(Tree.java:4948)
    at org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem$10.run(SWTBotTreeItem.java:362)
    at org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable$4.doRun(UIThreadRunnable.java:196)
    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:

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

Technical details

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

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

The thing which is strange is that the SWTBotTreeItem is calling setFocus before calling the setSelection, as you can see here in SWTBotTreeItem.select():

public SWTBotTreeItem select() {
                assertEnabled();
                syncExec(new VoidResult() {
                        public void run() {
                                tree.setFocus();
                                tree.setSelection(widget);
                        }
                });
                notifySelect();
                return this;
        }

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