How to getImage from SWTBotTreeItem?

26 Views Asked by At

This seemed the straightforward way to do this

Image treeItemImage = mySWTBotTreeItem.getNode("my tree item label").getImage();

However, there is no getImage method. The "widget" field in the parent AbstractSWTBot does not seem accessible.

I tried the above two things. The question "How to get Image from SWTBotTreeItem?" was posted 6 years ago, and no one has answered that. Has SWTBot been updated so it is possible to test whether the correct image is being shown in a tree item?

1

There are 1 best solutions below

0
Steve Vestal On

I found a way to do this.

Referencing the widget field adds a dependency on org.apache.log4j, so make sure that is an Import-Package in the plugin manifest.

Invoking methods of the widget field, in particular getImage(), causes thread exceptions. The following is what worked for me. The images are compared using ==, so the pair should be fetched from the same cache such as an ImageRegistry.

    class CompareImages implements SwtCallable<Boolean, Exception> {
        final TreeItem treeItem;
        final Image desired;
        CompareImages (Image desired, TreeItem treeItem) {
            this.desired = desired;
            this.treeItem = treeItem;
        }
        @Override
        public Boolean call()  {
            Image itemImage = treeItem.getImage();  
            return desired == itemImage;
        }
    }
    
    void assertImage(Image desired, SWTBotTreeItem botTreeItem) {
        TreeItem treeItem = botTreeItem.widget;
        try {
            Boolean isEqual = Display.getDefault().syncCall((SwtCallable<Boolean, Exception>) new CompareImages(desired, treeItem));
            assertTrue(isEqual);
        } catch (Exception e) {
            assertTrue(false);
        }