-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ondrej Dockal <odockal@redhat.com>
- Loading branch information
Showing
6 changed files
with
395 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...pse.reddeer.common.test/src/org/eclipse/reddeer/common/test/wait/CustomWaitCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2017 Red Hat, Inc and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.reddeer.common.test.wait; | ||
|
||
import org.eclipse.reddeer.common.condition.AbstractWaitCondition; | ||
import org.eclipse.reddeer.common.util.Display; | ||
|
||
/** | ||
* Customizable wait condition | ||
* @author odockal | ||
* | ||
*/ | ||
public class CustomWaitCondition extends AbstractWaitCondition { | ||
|
||
private Runnable runnable = null; | ||
private int counter = 0; | ||
private int treshold = 5; | ||
private boolean initialState = false; | ||
private boolean conditionChanged = false; | ||
|
||
public CustomWaitCondition(boolean initialState, int treshold, Runnable runnable) { | ||
this.runnable = runnable; | ||
this.treshold = treshold; | ||
this.initialState = initialState; | ||
} | ||
|
||
public CustomWaitCondition(boolean initialState, int treshold) { | ||
this(initialState, treshold, null); | ||
} | ||
|
||
@Override | ||
public boolean test() { | ||
if (runnable != null) { | ||
runnable.run(); | ||
} | ||
counter++; | ||
if ( counter >= treshold ) { | ||
this.conditionChanged = true; | ||
return !initialState; | ||
} else { | ||
return initialState; | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Boolean getResult() { | ||
return conditionChanged; | ||
} | ||
|
||
public static void sleep(long milliseconds) { | ||
org.eclipse.swt.widgets.Display display = Display.getDisplay(); | ||
if(display != null && Thread.currentThread().equals(display.getThread())) { | ||
throw new RuntimeException("Tried to execute sleep in UI thread!"); | ||
} | ||
try { | ||
Thread.sleep(milliseconds); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException("Sleep interrupted", e); | ||
} | ||
} | ||
|
||
} |
115 changes: 115 additions & 0 deletions
115
...g.eclipse.reddeer.common.test/src/org/eclipse/reddeer/common/test/wait/GroupWaitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 Red Hat, Inc and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.reddeer.common.test.wait; | ||
|
||
import static org.eclipse.reddeer.common.wait.WaitProvider.waitUntil; | ||
import static org.eclipse.reddeer.common.wait.WaitProvider.waitWhile; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
import static org.eclipse.reddeer.common.test.wait.CustomWaitCondition.sleep; | ||
|
||
import org.eclipse.reddeer.common.condition.WaitCondition; | ||
import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException; | ||
import org.eclipse.reddeer.common.wait.GroupWait; | ||
import org.eclipse.reddeer.common.wait.TimePeriod; | ||
import org.eclipse.reddeer.common.wait.WaitWrapper; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author odockal | ||
* | ||
*/ | ||
public class GroupWaitTest { | ||
|
||
private WaitCondition falseCondition; | ||
private WaitCondition trueCondition; | ||
private WaitWrapper waitUntil; | ||
private WaitWrapper waitWhile; | ||
|
||
@Before | ||
public void setup() { | ||
falseCondition = new CustomWaitCondition(false, 3, () -> sleep(1000)); | ||
trueCondition = new CustomWaitCondition(true, 3, () -> sleep(1000)); | ||
waitUntil = waitUntil(falseCondition); | ||
waitWhile = waitWhile(trueCondition); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_groupWaitNullWaitCondition() { | ||
new GroupWait(TimePeriod.SHORT, (WaitWrapper[]) null); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_groupWaitValidAndNullWaitCondition() { | ||
new GroupWait(waitUntil, null); | ||
} | ||
|
||
@Test | ||
public void test_groupWaitTimeoutDefault() { | ||
try { | ||
new GroupWait(waitUntil); | ||
assertTrue(waitUntil.getWaitCondition().getResult()); | ||
} catch (WaitTimeoutExpiredException waitExc) { | ||
fail("GroupWait should have not thrown WaitTimeExpiredException"); | ||
} | ||
} | ||
|
||
@Test | ||
public void test_groupWaitTimeoutMedium() { | ||
try { | ||
new GroupWait(TimePeriod.SHORT, waitUntil); | ||
fail("GroupWait should have thrown WaitTimeExpiredException"); | ||
} catch (WaitTimeoutExpiredException waitExc) { | ||
assertFalse(waitUntil.getWaitCondition().getResult()); | ||
} | ||
} | ||
|
||
@Test | ||
public void test_groupWaitWaitUntilAndWhileFulfilled() { | ||
waitUntil = waitUntil(new CustomWaitCondition(false, 3, () -> sleep(1000))); | ||
waitWhile = waitWhile(new CustomWaitCondition(true, 5, () -> sleep(1000))); | ||
try { | ||
new GroupWait(waitUntil, waitWhile); | ||
assertTrue(waitUntil.getWaitCondition().getResult()); | ||
assertTrue(waitWhile.getWaitCondition().getResult()); | ||
} catch (WaitTimeoutExpiredException waitExc) { | ||
fail("GroupWait should have not thrown WaitTimeExpiredException"); | ||
} | ||
} | ||
|
||
@Test | ||
public void test_groupWaitWaitWhileFailed() { | ||
waitUntil = waitUntil(new CustomWaitCondition(false, 1, () -> sleep(1000))); | ||
waitWhile = waitWhile(new CustomWaitCondition(true, 5, () -> sleep(1000))); | ||
try { | ||
new GroupWait(TimePeriod.MEDIUM, waitUntil, waitWhile); | ||
} catch (WaitTimeoutExpiredException waitExc) { | ||
assertTrue(waitUntil.getWaitCondition().getResult()); | ||
assertFalse(waitWhile.getWaitCondition().getResult()); | ||
} | ||
} | ||
|
||
@Test(expected=WaitTimeoutExpiredException.class) | ||
public void test_groupWaitBothConditionsFailed() { | ||
new GroupWait(TimePeriod.MEDIUM, waitUntil, waitWhile); | ||
} | ||
|
||
@Test(expected=WaitTimeoutExpiredException.class) | ||
public void test_groupWaitOneFailed() { | ||
waitWhile = waitWhile(new CustomWaitCondition(true, 1, () -> sleep(1000))); | ||
waitUntil = waitUntil(new CustomWaitCondition(false, 5, () -> sleep(1000))); | ||
new GroupWait(TimePeriod.MEDIUM, waitUntil, waitWhile); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...clipse.reddeer.common.test/src/org/eclipse/reddeer/common/test/wait/WaitProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 Red Hat, Inc and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.reddeer.common.test.wait; | ||
|
||
import static org.eclipse.reddeer.common.wait.WaitProvider.waitUntil; | ||
import static org.eclipse.reddeer.common.wait.WaitProvider.waitWhile; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.eclipse.reddeer.common.wait.WaitType; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author odockal | ||
* | ||
*/ | ||
public class WaitProviderTest { | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_nullCondition() { | ||
waitUntil(null); | ||
} | ||
|
||
@Test | ||
public void test_WaitTypeUntil() { | ||
assertTrue(waitUntil(new CustomWaitCondition(true, 1)).getWaitType().equals(WaitType.UNTIL)); | ||
} | ||
|
||
@Test | ||
public void test_WaitTypeWhile() { | ||
assertTrue(waitWhile(new CustomWaitCondition(true, 1)).getWaitType().equals(WaitType.WHILE)); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...g.eclipse.reddeer.common.test/src/org/eclipse/reddeer/common/test/wait/WaitUntilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2018 Red Hat, Inc and others. | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.reddeer.common.test.wait; | ||
|
||
import static org.junit.Assert.fail; | ||
import static org.eclipse.reddeer.common.test.wait.CustomWaitCondition.sleep; | ||
|
||
import org.eclipse.reddeer.common.condition.WaitCondition; | ||
import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException; | ||
import org.eclipse.reddeer.common.wait.TimePeriod; | ||
import org.eclipse.reddeer.common.wait.WaitUntil; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* | ||
* @author odockal | ||
* | ||
*/ | ||
public class WaitUntilTest { | ||
|
||
private WaitCondition falseCondition; | ||
|
||
@Before | ||
public void setupCondition() { | ||
falseCondition = new CustomWaitCondition(false, 5, () -> sleep(1000)); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_NullCondition() { | ||
new WaitUntil(null); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_NullTimePeriod() { | ||
new WaitUntil(falseCondition, null); | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void test_IllegalTimePeriod() { | ||
new WaitUntil(falseCondition, TimePeriod.getCustom(-1)); | ||
} | ||
|
||
@Test(expected=WaitTimeoutExpiredException.class) | ||
public void test_ThrowingException() { | ||
new WaitUntil(falseCondition, TimePeriod.MEDIUM); | ||
} | ||
|
||
@Test | ||
public void test_NotThrowingException() { | ||
try { | ||
new WaitUntil(falseCondition, TimePeriod.MEDIUM, false); | ||
} catch (WaitTimeoutExpiredException exc) { | ||
fail("WaitTimeoutExpiredException should not be thrown"); | ||
} | ||
} | ||
|
||
@Test | ||
public void test_WaitUntilFulFilled() { | ||
try { | ||
new WaitUntil(falseCondition, TimePeriod.DEFAULT); | ||
} catch (WaitTimeoutExpiredException exc) { | ||
fail("WaitTimeoutExpiredException should not be thrown"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.