Eclipse Action 4

来源:互联网 发布:什么是php程序员 编辑:程序博客网 时间:2024/05/01 15:22
 
6.2.7. 手工测试action
测试刚刚所作的修改内容需要启动运行时工作台,运行方式在第二章一个简单的插件样例 中讨论过。如果Favorites菜单没有在运行时工作台菜单栏出现或者在工具栏没有找到Favorites图标,则使用如下方法:
  • 选择Window > Customize Perspective...,打开透视图定制对话框,启用action集。在对话框中,选择 Commands tab, 选中Favorites ActionSet (见 图 6-7)。
6-7. 透视图定制对话框
  • 使用Window > Reset Perspective 重新初始化透视图。
  • 关闭并重新打开透视图。
  • 如果无效,尝试在启动运行时工作台前清除工作空间数据。在启动菜单中选择Run...,选择Favorites启动配置,选中Clear workspace data before launching复选框。点击Run按钮启动运行时工作台。
6.2.8. 为action添加测试代码
在工作完成之前,你需要为Open Favorites View action作出测试代码。你已经有了FavoritesViewTest (见 2.8.3节, 创建插件测试代码) ,从那里可以提取出通用的测试功能。
为所有的测试代码创建一个新的超类AbstractFavoritesTest,从已经存在的FavoritesViewTest中提升delay(), assertEquals()和waitForJobs()方法。VIEW_ID 常量和FavoritesView.ID 常量是相同的,所以可以用FavoritesView.ID取代它。下一步,创建AbstractFavoritesTest的子类来测试OpenFavoritesViewActionDelegate 类。
package com.qualityeclipse.favorites.test;
 
import ...
 
public class OpenFavoritesViewTest extends AbstractFavoritesTest {
   public OpenFavoritesViewTest(String name) {
      super(name);
   }
 
重写 setUp() 方法,保证系统在测试执行前处于适当的状态。
protected void setUp() throws Exception {
   super.setUp();
 
   // Ensure that the view is not open.
   waitForJobs();
   IWorkbenchPage page = PlatformUI.getWorkbench()
         .getActiveWorkbenchWindow().getActivePage();
   IViewPart view = page.findView(FavoritesView.ID);
   if (view != null)
      page.hideView(view);
 
   // Delay for 3 seconds so that
   // the Favorites view can be seen.
   waitForJobs();
   delay(3000);
}
 
最后,创建测试方法测试OpenFavoritesViewActionDelegate 类.
public void testOpenFavoritesView() {
 
 // Execute the operation.
 (new Action("OpenFavoritesViewTest") {
    public void run() {
       IWorkbenchWindowActionDelegate delegate =
          new OpenFavoritesViewActionDelegate();
       delegate.init(PlatformUI.getWorkbench()
          .getActiveWorkbenchWindow());
       delegate.selectionChanged(this, StructuredSelection.EMPTY);
       delegate.run(this);
    }
 }).run();
 
 // Test that the operation completed successfully.
 waitForJobs();
 IWorkbenchPage page = PlatformUI.getWorkbench()
      .getActiveWorkbenchWindow().getActivePage();
 assertTrue(page.findView(FavoritesView.ID) != null);
}
 
在输入前面的测试代码后,Problems 视图中出现如下错误:
Access restriction: The type OpenFavoritesViewActionDelegate
is not accessible due to restriction on required project
com.qualityeclipse.favorites.
 
这表明com.qualityeclipse.favorites插件没有为其他类提供OpenFavoritesViewActionDelegate类的访问性。要修正这种情况,打开插件装载文件编辑器到Exported Packages节 (见 2.8.1节, 测试准备),点击Add..., 选择com.qualityeclipse.favorites.actions 包并且保存。
现在可以执行测试代码了,
FavoritesViewTest和 OpenFavoritesViewTest 可以被组合到一个单独的测试套FavoritesTestSuite中,这样可以一次执行两个测试:
package com.qualityeclipse.favorites.test;
 
import ...
 
public class FavoritesTestSuite
{
 
   public static Test suite() {
 
      TestSuite suite =
         new TestSuite("Favorites test suite");
 
      suite.addTest(
         new TestSuite(FavoritesViewTest.class));
 
      suite.addTest(
         new TestSuite(OpenFavoritesViewTest.class));
 
      return suite;
 
   }
}
 
当只有两个测试用例时,单独执行测试并不是什么问题;但是如果将来插件中加入了更多的测试用例,使用一个单独的测试套就能节省很多时间。要运行测试套,在启动菜单中选择Run...,选择FavoritesViewTest启动配置(见2.8.4节, 运行插件测试),更改目标为新的FavoritesTestSuite测试套。
6.2.9. 讨论
定义顶级菜单,还是不定义顶层菜单,这是一个问题。一方面,当程序刚安装时,顶级菜单能很好的促进新产品,可以使潜在的用户更习惯新的功能。另一方面,如果每个插件都定义顶级菜单,那么菜单栏将混乱不堪,Eclipse也很快就会变得不可用了。另外,如果用户对于不想看到菜单要经过多步的操作才能把它们去掉(见1.2.2.4节,定制可用的action),用户就会很烦。那么应该怎么做呢?
Action集是一种解决方案。可以在plugin.xml文件中定义action集在哪些透视图中可见。使用IActionSetDescriptor.setInitiallyVisible() 方法,可以编程重写它在plugin.xml中指定的可见性,所以顶级菜单就不会再在任何新开的透视图中出现了。你可以通过使用关联IWorkbenchPage.hideActionSet()的setInitiallyVisible()方法,创建新的action从所有当前或未来的透视图中去掉你的顶级菜单。你的产品应该在属性设置页有一个复选框 (见12.2节, 属性设置页面 API),使用这个action来显示或隐藏你的顶级菜单。
注意   
我们为新的IActionSetDescriptor API提交了一个特性请求和Eclipse补丁 (见 Bugzilla 节点 #39455, bugs.eclipse.org/bugs/show_bug.cgi?id=39455), 它被认可并被集成到Eclipse 3.0 和 3.1。这是一个很好的样例,用户可以反过来为Eclipse做贡献,使得Eclipse更好的适合每个人。 (见 20.6.4节,提交针对Eclipse的改变)。
其他的选择还有绑定你的顶级菜单或action集到特定的透视图 (见 10.2.3节, 添加action集)。使用这种方式,菜单和action只有在特定的透视图活动的时候才可见。如果一个或多个透视图都要适配你的插件加入的功能,那么这是最好的方法。
那么action是编辑器关联的呢?(见6.5.2节, 定义一个编辑器上下文action 和 6.5.5节, 定义一个编辑器顶级菜单)介绍了为特定类型的编辑器添加菜单和action。使用这种方法,顶级菜单只有在特定类型编辑器打开的情况下在可见。
org.eclipse.ui.actionSetPartAssociations 扩展点提供了另一种选择,当特定的视图或者编辑器打开的时候,不管它们所处的透视图是否打开,都允许一个action集处于可见状态。这是一种极好的方式来保证特定的action出现大范围的透视图中,而不是明确的为这些透视图添加action。
此节的最后关注在视图特定菜单中供action,作用在特定对象上,而不是顶级菜单上。这样,action只在需要的时候和在它应用的对象上时,才是可见的。这种方法,避免了顶级菜单问题,防止Eclipse变得混乱不堪。对于本地范围action的多种方法将在随后章节中讲到。