package org.eclipse.platform.discovery.ui.test.comp.internal.pageobjects.swtbot; | |
import java.util.List; | |
import org.eclipse.swt.SWT; | |
import org.eclipse.swt.widgets.Control; | |
import org.eclipse.swt.widgets.Widget; | |
import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; | |
import org.eclipse.swtbot.swt.finder.finders.ChildrenControlFinder; | |
import org.eclipse.swtbot.swt.finder.finders.ControlFinder; | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; | |
import org.eclipse.swtbot.swt.finder.widgets.AbstractSWTBot; | |
import org.hamcrest.BaseMatcher; | |
import org.hamcrest.Description; | |
import org.hamcrest.Matcher; | |
public class SwtBotUtils { | |
public boolean isReadOnly(AbstractSWTBot<? extends Widget> widget) { | |
return SWTUtils.hasStyle(widget.widget, SWT.READ_ONLY); | |
} | |
public void setSize(final AbstractSWTBot<? extends Control> control, final int width, final int height) { | |
control.display.syncExec(new Runnable() { | |
@Override | |
public void run() { | |
control.widget.setSize(width, height); | |
} | |
}); | |
} | |
public <ResultType extends Widget> ResultType findOneChildControlOfType(Widget parent, Class<ResultType> clazz, boolean shouldBeVisible) | |
throws WidgetNotFoundException, MultipleWidgetsFoundException { | |
return findOneChild(new ChildrenControlFinder(parent), new InstanceOf<ResultType>(clazz), shouldBeVisible); | |
} | |
public <ResultType extends Widget> ResultType findOneChildControlOfExactType(Widget parent, Class<ResultType> clazz, boolean shouldBeVisible) | |
throws WidgetNotFoundException, MultipleWidgetsFoundException { | |
return findOneChild(new ChildrenControlFinder(parent), new ClassEquals<ResultType>(clazz), shouldBeVisible); | |
} | |
private <ResultType extends Widget> ResultType findOneChild(ControlFinder finder, Matcher<ResultType> matcher, boolean shouldBeVisible) { | |
finder.shouldFindInVisibleControls = !shouldBeVisible; | |
List<ResultType> result = finder.findControls(matcher); | |
if(result.size()==0) { | |
throw new WidgetNotFoundException(null); | |
} | |
if(result.size()>1) { | |
throw new MultipleWidgetsFoundException(result.toString()); | |
} | |
return result.get(0); | |
} | |
private static class InstanceOf <T extends Widget> extends BaseMatcher<T> { | |
private final Class<T> clazz; | |
public InstanceOf(Class<T> clazz) { | |
this.clazz = clazz; | |
} | |
@Override | |
public boolean matches(Object item) { | |
return clazz.isAssignableFrom(item.getClass()); | |
} | |
@Override | |
public void describeTo(Description description) { | |
} | |
} | |
private static class ClassEquals <T extends Widget> extends BaseMatcher<T> { | |
private final Class<T> clazz; | |
public ClassEquals(Class<T> clazz) { | |
this.clazz = clazz; | |
} | |
@Override | |
public boolean matches(Object item) { | |
return clazz.equals(item.getClass()); | |
} | |
@Override | |
public void describeTo(Description description) { | |
} | |
} | |
} |