2

I've written up a GUI test using SWTBot to test the Extract Method refactoring. I use editor.selectRange() to select a statement to extract into a method. But, when I run the unit test, the Extract Method refactoring menu item is disabled. Thus, SWTBot fails to invoke the refactoring.

When we change org.eclipse.jdt.ui.actions.ExtractMethodAction so that the "Extract Method..." menu item is always enabled, our SWTBot passes. But, SWTBot should let us select the menu item without hacking the org.eclipse.jdt.ui plugin.

The whole project containing the above unit test is available at github. I've also reported the problem on the Eclipse forum for SWTBot and SWTBot bug tracking system. But, we haven't received a solution from the forum.

reprogrammer
  • 14,298
  • 16
  • 57
  • 93

1 Answers1

1

Maybe you must flush any pending events to enable the menu. Add this loop before you try to click on the menu:

while(Display.getDefault().readAndDispatch());

[EDIT] I think the problem is that the menu is enabled through an extension point or a condition. Check all the plugin.xml files for the menu or the underlying action and on which condition it becomes enabled.

To trigger the condition, you may have to notify the workbench about the new selection. Check http://www.eclipse.org/articles/Article-WorkbenchSelections/article.html and http://www.eclipse.org/articles/article.php?file=Article-action-contribution/index.html

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • @Aaron Digulla Thanks for the suggestion. We tried it. But, it didn't work for us because the extract method refactoring menu doesn't become enabled. – reprogrammer Jan 04 '11 at 19:55
  • Ah ... I think I know where this is coming from. See my edits. – Aaron Digulla Jan 04 '11 at 20:52
  • The following is how the `Extract Method Refactoring` is set up in the `org.eclipse.ui.actionSets` extension point of `org.eclipse.jdt.ui/plugin.xml`. ` ` – reprogrammer Jun 21 '11 at 20:21
  • The extension does not seem to specify the condition under which the menu becomes enabled. Is that the `plugin.xml` you were referring to? – reprogrammer Jun 21 '11 at 20:35
  • Yes. I think your problem is that you need Eclipse to run the condition. Not sure how that works. Did you have a look into the test cases for other plugins, especially those in the JDT that test refactoring? – Aaron Digulla Jun 24 '11 at 08:37
  • Do you know if the `conditions` are supported in the version of `plugin.xml` that JDT uses? I believe JDT is using an old format of `plugin.xml` that doesn't support `conditions`. – reprogrammer Jun 26 '11 at 16:42
  • Sorry, no idea. Consider asking in the JDT forum: http://www.eclipse.org/forums/index.php?t=thread&frm_id=13 – Aaron Digulla Jun 26 '11 at 18:00