Skip to content
Permalink
Browse files

Tests use wall clock to speed up, test end of 2:00 cycle. Still a bit…

… flaky because of all the threading involved
  • Loading branch information
David Tanzer committed May 15, 2018
1 parent 2927e0b commit 203558eb166edcb572a52aef94e1b95dee185913
@@ -26,9 +26,9 @@
import javax.swing.event.HyperlinkListener;

public class BabystepsTimer {
private static final String BACKGROUND_COLOR_NEUTRAL = "#ffffff";
private static final String BACKGROUND_COLOR_FAILED = "#ffcccc";
private static final String BACKGROUND_COLOR_PASSED = "#ccffcc";
static final String BACKGROUND_COLOR_NEUTRAL = "#ffffff";
static final String BACKGROUND_COLOR_FAILED = "#ffcccc";
static final String BACKGROUND_COLOR_PASSED = "#ccffcc";

private static final long SECONDS_IN_CYCLE = 120;

@@ -10,6 +10,8 @@
import static org.junit.Assume.assumeTrue;

public class BabystepsTimerTest {
private TestingWallClock wallClock;

@Test
public void shows_2_00_atStartup() throws Exception {
startTimerApp();
@@ -25,6 +27,7 @@ public void startsCountingDownWhenStartWasPressed() throws Exception {
waitFor(1000L);

assertTimerShows("1:59");
press("stop");
}

@Test
@@ -39,6 +42,7 @@ public void stopsCountingDownWhenStopWasPressed() throws Exception {
assertTimerShows("2:00");
waitFor(2000L);
assertTimerShows("2:00");
press("stop");
}

@Test
@@ -53,6 +57,84 @@ public void startsAt_2_00_AgainWhenResetWasPressed() throws Exception {
assertTimerShows("2:00");
waitFor(1000L);
assertTimerShows("1:59");
press("stop");
}

@Test
public void startsAt_2_00_AgainWhenTimeIsUp() throws Exception {
startTimerApp();

press("start");
waitFor(119000L);
assumeTimerShows("0:01");

//Advance after end of cycle, so wall clock starts a new cycle
waitFor(2000L);

waitFor(1000L);
assertTimerShows("1:59");
press("stop");
}

@Test
public void showsRedBackgroundWhenTimeIsUp() throws Exception {
startTimerApp();

press("start");
waitFor(119000L);
assumeTimerShows("0:01");

//Advance *exactly to* end of cycle, so timer sets the background
waitFor(1000L);

assertBackgroundIs(BabystepsTimer.BACKGROUND_COLOR_FAILED);
press("stop");
}

@Test
public void backgroundTurnsGreenWhenResetInTime() throws Exception {
startTimerApp();

press("start");
waitFor(119000L);
assumeTimerShows("0:01");

press("reset");

assertBackgroundIs(BabystepsTimer.BACKGROUND_COLOR_PASSED);
press("stop");
}

@Test
public void backgroundTurnsWhiteAgainAfterSomeTime() throws Exception {
startTimerApp();

press("start");
waitFor(119000L);
assumeTimerShows("0:01");

press("reset");

assumeBackgroundIs(BabystepsTimer.BACKGROUND_COLOR_PASSED);
//Changes background back between 5 and six seconds
waitFor(5500);
assertBackgroundIs(BabystepsTimer.BACKGROUND_COLOR_NEUTRAL);

press("stop");
}

private void assertBackgroundIs(String backgroundColor) {
final String appText = BabystepsTimer.timerPane.getText();
assertTrue(
"Expected timer background to be \""+backgroundColor+"\" (Full window content: \""+appText+"\")",
appText.contains("background-color: "+backgroundColor));
}

private void assumeBackgroundIs(String backgroundColor) {
final String appText = BabystepsTimer.timerPane.getText();
assumeTrue(
"Expected timer background to be \""+backgroundColor+"\" (Full window content: \""+appText+"\")",
appText.contains("background-color: "+backgroundColor));
}

private void assertTimerShows(String time) {
@@ -70,18 +152,44 @@ private void assumeTimerShows(String time) {
}

private void startTimerApp() throws Exception {
wallClock = new TestingWallClock();

BabystepsTimer.wallclock = wallClock;
BabystepsTimer.main(null);
Thread.sleep(100L);

Thread.yield();
Thread.sleep(50L);
wallClock.advanceBy(100);
}

private void press(String button) throws MalformedURLException, InterruptedException {
final HyperlinkEvent event = new HyperlinkEvent(BabystepsTimer.timerPane, HyperlinkEvent.EventType.ACTIVATED, null, "command://"+button);
BabystepsTimer.timerPane.getHyperlinkListeners()[0].hyperlinkUpdate(event);
Thread.sleep(50L);
waitFor(0);
}

private void waitFor(long millis) throws InterruptedException {
Thread.sleep(millis);
wallClock.advanceBy(millis);
Thread.yield();
Thread.sleep(50L);
}

private class TestingWallClock implements WallClock {
private long currentTime;

@Override
public long currentTimeMillis() {
return currentTime;
}

@Override
public synchronized void nextTick() throws InterruptedException {
wait();
}

public synchronized void advanceBy(long millis) {
currentTime += millis;
notify();
}
}
}

0 comments on commit 203558e

Please sign in to comment.