blob: fcd6e79b9e516ed1eb52ebf15d420a49f0e1d79c [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2010 SAP AG, Walldorf.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* SAP AG - initial API and implementation
*******************************************************************************/
package org.eclipse.platform.discovery.testutils.utils.abbot;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.Assertions;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.ConditionCheckException;
import org.eclipse.platform.discovery.testutils.utils.junit.util.assertions.IWaitCondition;
import org.eclipse.swt.widgets.Widget;
import abbot.swt.finder.WidgetFinder;
import abbot.swt.finder.generic.Matcher;
import abbot.swt.finder.generic.MultipleFoundException;
import abbot.swt.finder.generic.NotFoundException;
/**
* A finder implementation which would perform several attempts to find a widget prior failing
*
* @author Danail Branekov
*/
public class WaitingFinder implements WidgetFinder
{
private final WidgetFinder delegate;
public WaitingFinder(final WidgetFinder delegate)
{
this.delegate = delegate;
}
@Override
public Widget find(final Matcher<Widget> matcher) throws NotFoundException, MultipleFoundException
{
final Widget[] result = new Widget[1];
final IWaitCondition condition = new FindWaitCondition()
{
@Override
protected void find() throws NotFoundException, MultipleFoundException
{
result[0] = delegate.find(matcher);
}
};
Assertions.waitAssert(condition, "Widget not found");
return result[0];
}
@Override
public Widget find(final Widget node, final Matcher<Widget> matcher) throws NotFoundException, MultipleFoundException
{
final Widget[] result = new Widget[1];
final IWaitCondition condition = new FindWaitCondition()
{
@Override
protected void find() throws NotFoundException, MultipleFoundException
{
result[0] = delegate.find(node, matcher);
}
};
Assertions.waitAssert(condition, "Widget not found");
return result[0];
}
private abstract class FindWaitCondition implements IWaitCondition
{
@Override
public boolean checkCondition() throws ConditionCheckException
{
try
{
find();
return true;
} catch (NotFoundException e)
{
return false;
} catch (MultipleFoundException e)
{
throw new ConditionCheckException(e);
}
}
protected abstract void find() throws NotFoundException, MultipleFoundException;
}
}