Search code examples
eclipsedrag-and-dropeclipse-rcpgui-testingswtbot

SWTBot test - Drag and Drop from tree to editor


Good Morning all (or afternoon or evening respectively),

I am trying to build a SWTBot test that drags a node from a Tree Viewer into a diagram editor (using Graphiti, don't think that matters). The node to be dragged is in it's own view, not in the toolbar, so the standard way of doing it won't work:

SWTBotGefEditor editor = gefBot.gefEditor(editorName);
editor.activateTool(functionName);
editor.drag(20, 20, 20, 20);

I also saw that TreeItem has a dragAndDrop function, but unless I'm wrong (totally possible), I think that only works when dragging to another tree.

Is there a way to drag directly from a tree to a diagram editor?


Solution

  • Should have updated this a while back, but don't want to leave a thread hanging so I'll add my findings:

    I ended up having to use reflection to get the canvas object from my viewer, and then was able to pass that into the dragAndDrop() method that SWTBot has for SWTBotTreeItems. Worked in a pinch, though you can't give it x/y-coordinates (like you can when bringing from the palette), so it is still not ideal.

    SWTBotView view = gefBot.viewByTitle("My View");
    SWTBotTree tree = view.bot().tree();
    SWTBotTreeItem treeItem = tree.expandNode("Parent Node Name", targetNodeName);
    
    SWTBotGefViewer viewer = editor.getSWTBotGefViewer();
    SWTBotGefFigureCanvas canvas = null;
    
    for (Field f : viewer.getClass().getDeclaredFields()) {
        if ("canvas".equals(f.getName())) {
            f.setAccessible(true);
            try {
                canvas = (SWTBotGefFigureCanvas) f.get(viewer);
            } catch (IllegalArgumentException e) {
                e.printStackTrace(); 
            } catch (IllegalAccessException e) {
                e.printStackTrace(); 
            }
        }
    }
    
    Assert.assertNotNull(canvas);
    treeItem.dragAndDrop(canvas);