在SWTBot中触发右键菜单(ContextMenu)

来源:互联网 发布:分水岭算法图像分割 编辑:程序博客网 时间:2024/05/07 08:32

转自:http://hintcnuie.iteye.com/blog/961117

在SWTBot中,触发右键菜单是简单的task,但是当要触发contextMenu的二级菜单时,就会出现问题,原因可能是事件的丢失,诸如下面的菜单:

   Tree

     TreeItem1

       TreeItem2

  想要在TreeItem2上触发SOMA->SOMA sub menu是困难的,下面是我的解决方案(从SWTBot 论坛上抄出来的呵呵http://www.eclipse.org/forums/index.php?t=msg&goto=629948&S=96549f36b839765eb99c047d8b51151c)

 

经验是,不能直接传入TreeItem, 要把整个Tree node传入才行 

 

Java代码  收藏代码
  1. String[] paths={"SOMA-ME","Extract SOMA Service Model Elements"};             
Java代码  收藏代码
  1. ContextMenuHelper.clickContextMenu(projectTree,paths);  

 

ContextMenuHelper 源代码:

Java代码  收藏代码
  1. package com.ibm.asset.adit.mdhi.test.util;  
  2.   
  3. import static  
  4. org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic;  
  5. import static org.hamcrest.Matchers.allOf;  
  6. import static org.hamcrest.Matchers.instanceOf;  
  7.   
  8. import java.util.Arrays;  
  9.   
  10. import org.eclipse.swt.SWT;  
  11. import org.eclipse.swt.widgets.Control;  
  12. import org.eclipse.swt.widgets.Event;  
  13. import org.eclipse.swt.widgets.Menu;  
  14. import org.eclipse.swt.widgets.MenuItem;  
  15. import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException;  
  16. import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;  
  17. import org.eclipse.swtbot.swt.finder.results.VoidResult;  
  18. import org.eclipse.swtbot.swt.finder.results.WidgetResult;  
  19. import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot;  
  20. import org.hamcrest.Matcher;  
  21.   
  22. public class ContextMenuHelper {  
  23.     /** 
  24.      * Clicks the context menu matching the text. 
  25.      *  
  26.      * @param text 
  27.      *            the text on the context menu. 
  28.      * @throws WidgetNotFoundException 
  29.      *             if the widget is not found. 
  30.      */  
  31.     public static void clickContextMenu(final AbstractSWTBot<?> bot,  
  32.             final String... texts) {  
  33.   
  34.         // show  
  35.         final MenuItem menuItem = UIThreadRunnable  
  36.                 .syncExec(new WidgetResult<MenuItem>() {  
  37.                     public MenuItem run() {  
  38.                         MenuItem menuItem = null;  
  39.                         Control control = (Control) bot.widget;  
  40.                         Menu menu = control.getMenu();  
  41.                         for (String text : texts) {  
  42.                             Matcher<?> matcher = allOf(  
  43.                                     instanceOf(MenuItem.class),  
  44.                                     withMnemonic(text));  
  45.                             menuItem = show(menu, matcher);  
  46.                             if (menuItem != null) {  
  47.                                 menu = menuItem.getMenu();  
  48.                             } else {  
  49.                                 hide(menu);  
  50.                                 break;  
  51.                             }  
  52.                         }  
  53.   
  54.                         return menuItem;  
  55.                     }  
  56.                 });  
  57.         if (menuItem == null) {  
  58.             throw new WidgetNotFoundException("Could not find menu: "  
  59.                     + Arrays.asList(texts));  
  60.         }  
  61.   
  62.         // click  
  63.         click(menuItem);  
  64.   
  65.         // hide  
  66.         UIThreadRunnable.syncExec(new VoidResult() {  
  67.             public void run() {  
  68.                 hide(menuItem.getParent());  
  69.             }  
  70.         });  
  71.     }  
  72.   
  73.     private static MenuItem show(final Menu menu, final Matcher<?> matcher) {  
  74.         if (menu != null) {  
  75.             menu.notifyListeners(SWT.Show, new Event());  
  76.             MenuItem[] items = menu.getItems();  
  77.             for (final MenuItem menuItem : items) {  
  78.                 if (matcher.matches(menuItem)) {  
  79.                     return menuItem;  
  80.                 }  
  81.             }  
  82.             menu.notifyListeners(SWT.Hide, new Event());  
  83.         }  
  84.         return null;  
  85.     }  
  86.   
  87.     private static void click(final MenuItem menuItem) {  
  88.         final Event event = new Event();  
  89.         event.time = (int) System.currentTimeMillis();  
  90.         event.widget = menuItem;  
  91.         event.display = menuItem.getDisplay();  
  92.         event.type = SWT.Selection;  
  93.   
  94.         UIThreadRunnable.asyncExec(menuItem.getDisplay(), new VoidResult() {  
  95.             public void run() {  
  96.                 menuItem.notifyListeners(SWT.Selection, event);  
  97.             }  
  98.         });  
  99.     }  
  100.   
  101.     private static void hide(final Menu menu) {  
  102.         menu.notifyListeners(SWT.Hide, new Event());  
  103.         if (menu.getParentMenu() != null) {  
  104.             hide(menu.getParentMenu());  
  105.         }  
  106.     }  
  107. }  
原创粉丝点击