从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=%sn",selection));
assertNotNull(dmo.eContainer());
action.run();
EcorePlugin.INSTANCE.log(String.format("removing(1) %sn",dmo));
//dmo.removeZentaElementFromModel();
assertNull(dmo.eContainer());
}
编辑器的操作可以通过 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());
}