-
Notifications
You must be signed in to change notification settings - Fork 1
ProximityComponent Tests
LJ edited this page Aug 29, 2024
·
4 revisions
@ExtendWith(GameExtension.class)
class ProximityComponentTest {
/**
* Sets up the test environment before each test.
*/
@BeforeEach
void beforeEach() {
ServiceLocator.registerPhysicsService(new PhysicsService());
GameTime gameTime = mock(GameTime.class);
when(gameTime.getTime()).thenReturn(0L);
ServiceLocator.registerTimeSource(gameTime);
}
/**
* Tests that the ProximityComponent correctly initializes the entity as disabled.
*/
@Test
void shouldDisableEntityInitially() {
Entity target = mock(Entity.class);
Entity entity = mock(Entity.class);
ProximityComponent proximityComponent = new ProximityComponent(target, 10f);
proximityComponent.setEntity(entity);
assertFalse(entity.getEnabled());
}
/**
* Tests that the ProximityComponent enables the entity when the proximity condition is met.
*/
@Test
void shouldEnableOnSpawn() {
Entity entity = spy(new Entity());
new ProximityComponent(entity, 10f);
assertTrue(entity.getEnabled());
}
/**
* Tests that the ProximityComponent updates the entity's state to enabled and triggers the proximity event.
*/
@Test
void shouldUpdateToEnabled() {
Entity target = mock(Entity.class);
Entity entity = mock(Entity.class);
EventHandler events = mock(EventHandler.class);
when(target.getPosition()).thenReturn(new Vector2(0, 0));
when(entity.getPosition()).thenReturn(new Vector2(5, 5));
when(entity.getEvents()).thenReturn(events);
entity.setEnabled(false); // Entity is initially disabled
ProximityComponent proximityComponent = new ProximityComponent(target, 10f);
proximityComponent.setEntity(entity);
proximityComponent.update();
assertFalse(entity.getEnabled());
verify(events).trigger("proximityTriggered"); // Verify the trigger method was called on the events instance
}
/**
* Tests that the ProximityComponent correctly sets up the spawn animation when proximity is triggered.
*/
@Test
void shouldStartSpawnAnimationWhenProximityTriggered() {
Entity entity = mock(Entity.class);
AnimationRenderComponent animationRenderComponent = mock(AnimationRenderComponent.class);
EventHandler events = mock(EventHandler.class);
when(entity.getComponent(AnimationRenderComponent.class)).thenReturn(animationRenderComponent);
when(entity.getEvents()).thenReturn(events);
ProximityComponent proximityComponent = new ProximityComponent(entity, 10f);
proximityComponent.setupSpawnAnimation(entity);
ArgumentCaptor<EventListener0> proximityListenerCaptor = ArgumentCaptor.forClass(EventListener0.class);
verify(events).addListener(eq("proximityTriggered"), proximityListenerCaptor.capture());
proximityListenerCaptor.getValue().handle();
// Verify that the "spawn" animation starts
verify(animationRenderComponent).startAnimation("spawn");
ArgumentCaptor<EventListener1<String>> animationEndCaptor = ArgumentCaptor.forClass(EventListener1.class);
verify(events).addListener(eq("animationEnd"), animationEndCaptor.capture());
animationEndCaptor.getValue().handle("spawn");
// Verify that the "walk" animation starts after the "spawn" animation ends
verify(animationRenderComponent).startAnimation("walk");
}
}```