Skip to content

Commit

Permalink
Adding tests for waits
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Dockal <odockal@redhat.com>
  • Loading branch information
odockal authored and rawagner committed May 28, 2018
1 parent 7a82a11 commit 3715e10
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*******************************************************************************/
package org.eclipse.reddeer.common.wait;

import java.util.Arrays;

import org.eclipse.reddeer.common.exception.WaitTimeoutExpiredException;

/**
Expand All @@ -34,8 +36,8 @@ public class GroupWait {
*/
public GroupWait(TimePeriod timeout, WaitWrapper... waitings) {
remainingTimeout = timeout;
if (waitings == null || waitings.length == 0) {
throw new IllegalArgumentException("There are no waiting to wait for, pass one or more waitings "
if (waitings == null || waitings.length == 0 || !noneIsNull(waitings)) {
throw new IllegalArgumentException("There are no waiting to wait for, pass one or more waitings that are not null "
+ "as a constructor arguments to start waiting");
}
for (WaitWrapper waitWrapper: waitings) {
Expand Down Expand Up @@ -77,4 +79,14 @@ private TimePeriod getRemainingTimeoutPeriod(TimePeriod oldTimeout, long startTi
}
return TimePeriod.getCustom(remainingTimeout);
}

/**
* Checks whether none of passed objects (waits) in array is null
*
* @param waits array of wait objects
* @return true if none of passed waits is null
*/
private boolean noneIsNull(WaitWrapper... waits) {
return Arrays.asList(waits).stream().noneMatch(o -> o == null);
}
}
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);
}
}

}
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);
}
}
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));
}
}
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");
}
}

}
Loading

0 comments on commit 3715e10

Please sign in to comment.