Worker Idle Testing - UQdeco2800/2022-studio-3 GitHub Wiki

Overview

These JUnit test cases aim to check if the WorkerIdleTask passes the following functionality tests:

  1. shouldNotMoveOnStart: when the start method is called on the task and updated, the unit should not be moving
void shouldNotMoveOnStart() {
        Vector2 startPosition = new Vector2(10.0f, 10.0f);
        WorkerIdleTask task = new WorkerIdleTask();
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.create();
        entity.setPosition(startPosition.x, startPosition.y);

        task.create(() -> entity);
        task.start();
        task.update();
        // Check to make sure that entity hasn't moved
        assertEquals(Task.Status.FINISHED, task.getMovementTask().getStatus());
        assertEquals(entity.getPosition(), startPosition);
    }
  1. shouldBeIdle: when the task is started and the unit is not currently moving, the idling attribute of the task should be set as false
void shouldBeIdle() {
        WorkerIdleTask task = new WorkerIdleTask();
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.create();

        task.create(() -> entity);
        task.start();
        task.update(); // Should have stopped
        task.update(); // Should have updated idling to true
        assertTrue(task.isIdling());
    }
  1. shouldNotBeIdle: when the task is started and the unit is currently moving, the idling attribute of the task should be set as true
void shouldNotBeIdle() {
        Vector2 startPosition = new Vector2(10.0f, 10.0f);
        WorkerIdleTask task = new WorkerIdleTask();
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.create();
        entity.setPosition(startPosition);

        task.create(() -> entity);
        task.start();
        task.update(); // Should have stopped
        task.update(); // Should have updated idling to true
        assertTrue(task.isIdling()); // Should be idle

        task.startMoving(new Vector2(15.0f, 15.0f));
        assertFalse(task.isIdling()); // Should be moving
        task.update();
        assertFalse(task.isIdling()); // Should still be moving
    }