如何测试eclipse上下文菜单操作?

时间:2016-01-25 00:18:10

标签: java eclipse mockito eclipse-gef

从Junit插件测试我想测试“从模型中删除”,它有相关的DeleteFromModelAction。由于动作无法正常实例化,我试图嘲笑它。我想出了以下代码。 但它实际上从未调用底层代码(dmo.removeZentaElementFromModel()),它实际上会删除图模型对象(我从日志中看到它)。

我做错了什么?

@Test
public void It_is_possible_to_delete_from_model_through_a_diagram_object() {
    IZentaElement element = testdata.createClassedTestElement();
    IZentaDiagramModel dia = testdata.getNonTemplateDiagramModel();
    IDiagramModelZentaObject dmo = ModelAndEditPartTestData.createDMOFor(element);
    dia.getChildren().add(dmo);
    testdata.focusOnDiagram(dia.getId());
    BasicObjectEditPart editPart = (BasicObjectEditPart) testdata.getEditPartFor(dmo.getId());
    assertNotNull(editPart);
    editPart.getViewer().appendSelection(editPart);
    DeleteFromModelAction action = mock(DeleteFromModelAction.class);
    ISelection selection = editPart.getViewer().getSelection();
    EcorePlugin.INSTANCE.log(String.format("selection=%s\n",selection));

    assertNotNull(dmo.eContainer());
    action.run();
    EcorePlugin.INSTANCE.log(String.format("removing(1) %s\n",dmo));
    //dmo.removeZentaElementFromModel();
    assertNull(dmo.eContainer());
}

1 个答案:

答案 0 :(得分:0)

可以使用getAdapter()调用获取编辑器的操作。 应该使用这些确切的操作,因为它们将获得选择更新。

public void It_is_possible_to_delete_from_model_through_a_diagram_object() {
    //setup model
    IZentaElement element = testdata.createClassedTestElement();
    IZentaDiagramModel dia = testdata.getNonTemplateDiagramModel();
    IDiagramModelZentaObject dmo = ModelAndEditPartTestData.createDMOFor(element);
    dia.getChildren().add(dmo);
    testdata.focusOnDiagram(dia.getId());

    //here we obtain the editpart which will be deleted
    BasicObjectEditPart editPart = (BasicObjectEditPart) testdata.getEditPartFor(dmo.getId());
    EditPartViewer viewer = editPart.getViewer();
    //set the selection on the viewer
    viewer.appendSelection(editPart);

    //obtain the editor, its action registry, and our action from it
    ZentaDiagramEditor editor = testdata.getEditor();
    ActionRegistry actionregistry = (ActionRegistry) editor.getAdapter(ActionRegistry.class);
    IAction action = actionregistry.getAction(DeleteFromModelAction.ID);

    //run the action
    assertNotNull(dmo.eContainer());
    action.run();
    assertNull(dmo.eContainer());
}