From 3aefb14fafc195bf4ede22378255eafcf7cc9590 Mon Sep 17 00:00:00 2001 From: Calum Date: Thu, 4 May 2017 12:46:01 +1200 Subject: [PATCH] Added testing. --- src/main/java/seng302/App.java | 3 +- src/main/java/seng302/models/Boat.java | 4 + src/main/java/seng302/models/BoatGroup.java | 30 ++---- src/main/java/seng302/models/Wake.java | 6 +- .../java/seng302/models/mark/MarkGroup.java | 15 +-- .../java/seng302/models/BoatGroupTest.java | 98 +++++++++++++++++++ .../java/seng302/models/MarkGroupTest.java | 64 ++++++++++++ 7 files changed, 180 insertions(+), 40 deletions(-) create mode 100644 src/test/java/seng302/models/BoatGroupTest.java create mode 100644 src/test/java/seng302/models/MarkGroupTest.java diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 742b6032..ebad60ab 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -53,8 +53,9 @@ public class App extends Application break; } } + //Change the StreamReceiver in this else block to change the default data source. else{ - sr = new StreamReceiver("localhost", 8085, "RaceStream"); + sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); } sr.start(); diff --git a/src/main/java/seng302/models/Boat.java b/src/main/java/seng302/models/Boat.java index d275091f..0ea427fd 100644 --- a/src/main/java/seng302/models/Boat.java +++ b/src/main/java/seng302/models/Boat.java @@ -22,6 +22,10 @@ public class Boat { private String shortName; private int id; + /** + * For testing only. + * @param teamName Boat team name. + */ public Boat(String teamName) { this.teamName = teamName; this.velocity = 10; // Default velocity diff --git a/src/main/java/seng302/models/BoatGroup.java b/src/main/java/seng302/models/BoatGroup.java index 2f923318..190c537b 100644 --- a/src/main/java/seng302/models/BoatGroup.java +++ b/src/main/java/seng302/models/BoatGroup.java @@ -202,39 +202,21 @@ public class BoatGroup extends RaceObject{ if (currentRotation < 0) currentRotation = 360 - currentRotation; double dx = newXValue - boatPoly.getLayoutX(); -// if ((dx > 0 && pixelVelocityX < 0) || (dx < 0 && pixelVelocityX > 0)) { -// pixelVelocityX = 0; -// } else { - pixelVelocityX = dx / expectedUpdateInterval; -// } double dy = newYValue - boatPoly.getLayoutY(); //Check movement is reasonable. Assumes a 1000 * 1000 canvas if (Math.abs(dx) > 50 || Math.abs(dy) > 50) { -// System.out.println("dx = " + dx); -// System.out.println("dy = " + dy); dx = 0; dy = 0; moveTo(newXValue, newYValue); } - //Slight delay on changing X/Y direction that could help jitter. Disabled since there was an issue with - //packets that might be causing it. -// if ((dx > 0 && pixelVelocityX < 0) || (dx < 0 && pixelVelocityX > 0)) { -// pixelVelocityX = 0; -// } else { -// pixelVelocityX = dx / expectedUpdateInterval; -// } -// if ((dy > 0 && pixelVelocityY < 0) || (dy < 0 && pixelVelocityY > 0)) { -// pixelVelocityY = 0; -// } else { -// pixelVelocityY = dy / expectedUpdateInterval; -// } + pixelVelocityX = dx / expectedUpdateInterval; pixelVelocityY = dy / expectedUpdateInterval; rotationalGoal = rotation; calculateRotationalVelocity(); if (wakeGenerationDelay > 0) { wake.rotate(rotationalGoal); - rotateTo(rotationalGoal); + rotateTo(rotationalGoal); //Need to test with this removed. rotationalVelocity = 0; wakeGenerationDelay--; } else { @@ -243,7 +225,7 @@ public class BoatGroup extends RaceObject{ velocityObject.setText(String.format("%.2f m/s", boat.getVelocity())); } else { setToInitialLocation = true; - rotationalGoal = rotation; + rotationalGoal = rotation;; moveTo(newXValue, newYValue, rotation); } } @@ -288,8 +270,7 @@ public class BoatGroup extends RaceObject{ public void rotateTo (double rotation) { currentRotation = rotation; - boatPoly.getTransforms().clear(); - boatPoly.getTransforms().add(new Rotate(rotation)); + boatPoly.getTransforms().setAll(new Rotate(rotation)); } public void forceRotation () { @@ -361,6 +342,9 @@ public class BoatGroup extends RaceObject{ * @param stage The stage that the BoatGroup is added to. */ public void setStage (Stage stage) { + /* TODO: 4/05/17 cir27 - Find a way to get the stage to this point. Need to pass it through multiple controllers. + App.start() -> Controller.setContentPane -> RaceViewController -> CanvasController + */ this.stage = stage; this.stage.iconifiedProperty().addListener(e -> { isMaximized = !stage.isIconified(); diff --git a/src/main/java/seng302/models/Wake.java b/src/main/java/seng302/models/Wake.java index 19df8c8e..eaf28ba5 100644 --- a/src/main/java/seng302/models/Wake.java +++ b/src/main/java/seng302/models/Wake.java @@ -60,9 +60,9 @@ class Wake extends Group { if (sum < (max / 3)) rotate (rotationGoal); //In relatively straight segments the wake snaps to match the boats current position. //This stops the wake from eventually becoming out of sync with the boat. -// if (Math.abs(rotationalVelocity) > 0.5) { -// rotationalVelocity = 0; -// } + if (Math.abs(rotationalVelocity) > 0.5) { + rotationalVelocity = 0; + } //Update the index of the array of recent velocities that each wake uses. Each wake is 3 velocities behind the //next smallest wake. velocityIndices[0] = (13 + (velocityIndices[0] - 1) % 13) % 13; diff --git a/src/main/java/seng302/models/mark/MarkGroup.java b/src/main/java/seng302/models/mark/MarkGroup.java index 79b1759f..29931e01 100644 --- a/src/main/java/seng302/models/mark/MarkGroup.java +++ b/src/main/java/seng302/models/mark/MarkGroup.java @@ -58,12 +58,7 @@ public class MarkGroup extends RaceObject { nodePixelVelocitiesX = new double[]{0d,0d}; nodePixelVelocitiesY = new double[]{0d,0d}; nodeDestinations = new Point2D[2]; -// markCircle = new Circle( -// (points[1].getX() - points[0].getX()) / 2d, -// (points[1].getY() - points[0].getY()) / 2d, -// MARK_RADIUS, -// color -// + markCircle = new Circle( points[0].getX(), points[0].getY(), @@ -72,12 +67,7 @@ public class MarkGroup extends RaceObject { ); nodeDestinations[0] = new Point2D(markCircle.getCenterX(), markCircle.getCenterY()); super.getChildren().add(markCircle); -// markCircle = new Circle( -// -(points[1].getX() - points[0].getX()) / 2d, -// -(points[1].getY() - points[0].getY()) / 2d, -// MARK_RADIUS, -// color -// ); + markCircle = new Circle( points[1].getX(), points[1].getY(), @@ -99,7 +89,6 @@ public class MarkGroup extends RaceObject { } super.getChildren().add(line); } - //moveTo(points[0].getX(), points[0].getY()); } public void setDestination (double x, double y, double rotation, double groundSpeed, int... raceIds) { diff --git a/src/test/java/seng302/models/BoatGroupTest.java b/src/test/java/seng302/models/BoatGroupTest.java new file mode 100644 index 00000000..441a0429 --- /dev/null +++ b/src/test/java/seng302/models/BoatGroupTest.java @@ -0,0 +1,98 @@ +package seng302.models; + +import javafx.scene.paint.*; +import javafx.scene.paint.Color; +import javafx.scene.shape.Polygon; +import javafx.scene.transform.Rotate; +import javafx.scene.transform.Transform; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * Created by cir27 on 4/05/17. + */ +public class BoatGroupTest { + BoatGroup boatGroup; + @Before + public void setUp () { + Boat b = new Boat("TEST", 0.0, "T" ,0); + boatGroup = new BoatGroup(b, Color.BLACK); + } + + @Test + public void setDestinationFirstUseForcesLocationUpdate () { + boatGroup.setDestination(10, 10, 90, 0); + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + Assert.assertTrue(10 == bp.getLayoutX()); + Assert.assertTrue(10 == bp.getLayoutY()); + } + + @Test + public void setDestinationFutureUseDoesntForce () { + for (int i = 0; i < 60; i++) { + boatGroup.setDestination(200, 200, 90, 0); + } + boatGroup.setDestination(210, 210, 90, 0); + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + Assert.assertTrue(200 == bp.getLayoutX()); + Assert.assertTrue(200 == bp.getLayoutY()); + } + + @Test + public void setDestinationUnrealisticMovementForceUpdate () { + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + double xLocation = bp.getLayoutX(); + double yLocation = bp.getLayoutY(); + boatGroup.setDestination(xLocation + 500, yLocation + 500, 90, 0); + Assert.assertTrue(xLocation + 500 == bp.getLayoutX()); + Assert.assertTrue(yLocation + 500 == bp.getLayoutY()); + } + + @Test + public void setDestinationUnrealisticNegativeForceUpdate () { + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + double xLocation = bp.getLayoutX(); + double yLocation = bp.getLayoutY(); + boatGroup.setDestination(xLocation - 500, yLocation - 500, 90, 0); + Assert.assertTrue(xLocation - 500 == bp.getLayoutX()); + Assert.assertTrue(yLocation - 500 == bp.getLayoutY()); + } + + @Test + public void updatePositionGeneratesExpectedMovement () { + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + double xLocation = bp.getLayoutX(); + double yLocation = bp.getLayoutY(); + int movement = 10; + double delay = RaceObject.getExpectedUpdateInterval(); + double defaultTimePeriod = 1000 / 60; + double expectedMovement = movement / delay * defaultTimePeriod; + for (int i = 0; i < 60; i++) { + boatGroup.setDestination(xLocation, yLocation, 90, 0); + } + boatGroup.setDestination(xLocation + 10, yLocation + 10, 90, 0); + boatGroup.updatePosition(1000/60); + Assert.assertEquals(expectedMovement, bp.getLayoutX() - xLocation, 0.0); + } + + @Test + public void correctRaceID () { + Assert.assertTrue(boatGroup.hasRaceId(0)); + } + + @Test + public void incorrectRaceID () { + Assert.assertTrue(!boatGroup.hasRaceId(2)); + } + + @Test + public void nothingOnWrongId () { + Polygon bp = (Polygon) boatGroup.getChildren().get(2); + double originalX = bp.getLayoutX(); + double originalY = bp.getLayoutY(); + boatGroup.setDestination(10, 10, 90, 12); + Assert.assertTrue(originalX == bp.getLayoutX()); + Assert.assertTrue(originalY == bp.getLayoutY()); + } +} diff --git a/src/test/java/seng302/models/MarkGroupTest.java b/src/test/java/seng302/models/MarkGroupTest.java new file mode 100644 index 00000000..ea31bdeb --- /dev/null +++ b/src/test/java/seng302/models/MarkGroupTest.java @@ -0,0 +1,64 @@ +package seng302.models; + +import javafx.geometry.Point2D; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import seng302.models.mark.*; + +/** + * Created by cir27 on 4/05/17. + */ +public class MarkGroupTest { + private MarkGroup gateMG; + private MarkGroup singleMG; + + @Before + public void setUp () { + Mark single = new SingleMark("SM", 0, 0 , 0); + Mark gate = new GateMark( + "GM", + MarkType.OPEN_GATE, + new SingleMark("GM1", 0, 0, 1), + new SingleMark("GM2", 0, 0, 2), + 0, + 0); + gateMG = new MarkGroup(gate, new Point2D(10, 10), new Point2D(20, 20)); + singleMG = new MarkGroup(single, new Point2D(0, 0)); + } + + @Test + public void hasIDSingle () { + Assert.assertTrue(singleMG.hasRaceId(0)); + Assert.assertTrue(!singleMG.hasRaceId(100,12)); + } + + @Test + public void hasIdGate () { + Assert.assertTrue(gateMG.hasRaceId(1)); + Assert.assertTrue(gateMG.hasRaceId(2)); + Assert.assertTrue(!gateMG.hasRaceId(100,12)); + } + + @Test + public void nothingOnWrongId () { + double originalX = singleMG.getChildren().get(0).getLayoutX(); + double originalY = singleMG.getChildren().get(0).getLayoutY(); + singleMG.setDestination(10, 10, 0, 4); + singleMG.updatePosition(400); + Assert.assertTrue(originalX == singleMG.getChildren().get(0).getLayoutX()); + Assert.assertTrue(originalY == singleMG.getChildren().get(0).getLayoutY()); + } + + @Test + public void correctMovementCorrectId () { + double originalX = singleMG.getChildren().get(0).getLayoutX(); + double originalY = singleMG.getChildren().get(0).getLayoutY(); + long timeinterval = 100/60; + double expectedChange = 10 / 200 * timeinterval; + singleMG.setDestination(originalX + 10, originalY + 10, 0, 0); + singleMG.updatePosition(timeinterval); + Assert.assertTrue(originalX == singleMG.getChildren().get(0).getLayoutX()); + Assert.assertTrue(originalY == singleMG.getChildren().get(0).getLayoutY()); + } +}