From 4c82a4594e14c763b8dd8c0208a1604c16b9b4e3 Mon Sep 17 00:00:00 2001 From: Erika Savell Date: Thu, 23 Mar 2017 13:41:14 +1300 Subject: [PATCH] Added a small test for the timer function -More needed once scaling is in effect #test #story[16] --- src/main/java/seng302/Model/Race.java | 12 +++++++----- src/test/java/seng302/Model/RaceTest.java | 12 ++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/seng302/Model/Race.java b/src/main/java/seng302/Model/Race.java index b44a0193..ed5131a0 100644 --- a/src/main/java/seng302/Model/Race.java +++ b/src/main/java/seng302/Model/Race.java @@ -25,7 +25,7 @@ public abstract class Race implements Runnable { private int SLEEP_TIME = 100; //time in milliseconds to pause in a paced loop - private int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race + protected int PRERACE_TIME = 10000;//Integer.MAX_VALUE; //time in milliseconds to pause during pre-race private boolean timerEnabled = true; /** @@ -80,7 +80,7 @@ public abstract class Race implements Runnable { } - private void countdownTimer() { + protected void countdownTimer() { long currentTime = System.currentTimeMillis(); long startTime = currentTime + PRERACE_TIME; long minutes; @@ -97,7 +97,9 @@ public abstract class Race implements Runnable { remainingSeconds = currentTimeInSeconds % 60; hours = minutes / 60; minutes = minutes % 60; - updateTime(String.format("Time until race starts: %02d:%02d:%02d", hours, minutes, remainingSeconds)); + if (controller != null) { + updateTime(String.format("Time until race starts: %02d:%02d:%02d", hours, minutes, remainingSeconds)); + } try { timeLoopEnded = System.currentTimeMillis(); Thread.sleep(SLEEP_TIME - (timeLoopEnded - currentTime)); @@ -108,7 +110,7 @@ public abstract class Race implements Runnable { } } - private String calcTimer() { + protected String calcTimer() { long minutes; long currentTimeInSeconds; long remainingSeconds; @@ -122,7 +124,7 @@ public abstract class Race implements Runnable { return String.format("Race clock: %02d:%02d:%02d", hours, minutes, remainingSeconds); } - private void updateTime(String time){ + protected void updateTime(String time){ Platform.runLater(() -> {controller.setTimer(time);}); } diff --git a/src/test/java/seng302/Model/RaceTest.java b/src/test/java/seng302/Model/RaceTest.java index efce5caf..5f71f764 100644 --- a/src/test/java/seng302/Model/RaceTest.java +++ b/src/test/java/seng302/Model/RaceTest.java @@ -98,4 +98,16 @@ public class RaceTest { assertTrue(unFinishedBoat.getDistanceTravelledInLeg() < 100); } + + @Test + public void timerDelaysByHalfSecond() { + ConstantVelocityRace race = new ConstantVelocityRace(new BoatInRace[1], new ArrayList<>()); + race.PRERACE_TIME = 500; + + long timeStarted = System.currentTimeMillis(); + race.countdownTimer(); + + assertTrue(System.currentTimeMillis() - timeStarted > 500); + + } }