Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanPavilionG7 committed Jan 22, 2022
1 parent b549a9f commit 92b9d48
Showing 1 changed file with 84 additions and 25 deletions.
@@ -1,99 +1,158 @@
package com.faforever.client.replay;

import com.faforever.client.builders.GameBeanBuilder;
import com.faforever.client.config.ClientProperties;
import com.faforever.client.domain.GameBean;
import com.faforever.client.i18n.I18n;
import com.faforever.client.replay.LiveReplayService.LiveReplayAction;
import com.faforever.client.test.UITest;
import com.faforever.client.util.TimeService;
import com.faforever.client.vault.replay.WatchButtonController;
import javafx.animation.Timeline;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.util.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class WatchButtonControllerTest extends UITest {

private static final int WATCH_DELAY = 10; // in seconds;

@Mock
private ReplayService replayService;
@Mock
private LiveReplayService liveReplayService;
@Mock
private I18n i18n;
@Mock
private TimeService timeService;
@Spy
private ClientProperties clientProperties = new ClientProperties();

@InjectMocks
private WatchButtonController instance;
private GameBean game;
private final ObjectProperty<Pair<Integer, LiveReplayAction>> trackingReplayProperty = new SimpleObjectProperty<>(null);

@BeforeEach
public void setUp() throws Exception {
clientProperties.getReplay().setWatchDelaySeconds(WATCH_DELAY);
when(liveReplayService.getTrackingReplayProperty()).thenReturn(new SimpleObjectProperty<>(null));
when(liveReplayService.getTrackingReplayProperty()).thenReturn(trackingReplayProperty);
when(i18n.get("vault.liveReplays.contextMenu.notifyMe")).thenReturn("notify me");
when(i18n.get("vault.liveReplays.contextMenu.notifyMe.cancel")).thenReturn("cancel: notify me");
when(i18n.get("vault.liveReplays.contextMenu.runImmediately")).thenReturn("run replay");
when(i18n.get("vault.liveReplays.contextMenu.runImmediately.cancel")).thenReturn("cancel: run replay");

game = GameBeanBuilder.create().defaultValues().get();
loadFxml("theme/vault/replay/watch_button.fxml", clazz -> instance);
loadFxml("theme/vault/replay/watch_button.fxml", clazz -> instance);
}

@Test
public void testButtonWhenWatchNotAllowed() {
game.setStartTime(OffsetDateTime.now().minus(5, ChronoUnit.SECONDS));
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
assertThat(instance.watchButton.getPseudoClassStates().contains(WatchButtonController.AVAILABLE_PSEUDO_CLASS), is(false));
assertFalse(instance.watchButton.getPseudoClassStates().contains(WatchButtonController.AVAILABLE_PSEUDO_CLASS));
assertNull(instance.watchButton.getOnAction());
}

@Test
public void testButtonOnClickedWhenWatchNotAllowed() {
game.setStartTime(OffsetDateTime.now().minus(5, ChronoUnit.SECONDS));

setGame(game);
clickWatchButton();
verify(replayService, never()).runLiveReplay(game.getId());
}

@Test
public void testButtonWhenWatchAllowed() {
game.setStartTime(OffsetDateTime.now().minus(15, ChronoUnit.SECONDS));
when(liveReplayService.canWatchReplay(game)).thenReturn(true);

setGame(game);
assertThat(instance.watchButton.isDisabled(), is(false));
assertTrue(instance.watchButton.getPseudoClassStates().contains(WatchButtonController.AVAILABLE_PSEUDO_CLASS));
}

@Test
public void testButtonOnClickedWhenWatchAllowed() {
when(liveReplayService.canWatchReplay(game)).thenReturn(true);
game.setStartTime(OffsetDateTime.now().minus(15, ChronoUnit.SECONDS));

setGame(game);
clickWatchButton();
verify(replayService).runLiveReplay(game.getId());
}

@Test
public void testOnClickedNotifyMeWhenReplayIsAvailable() {
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
runOnFxThreadAndWait(() -> instance.notifyMeItem.fire());
verify(liveReplayService).performActionWhenAvailable(game, LiveReplayAction.NOTIFY_ME);
}

@Test
public void testOnClickedNotifyMeWhenReplayIsAlreadyTracking() {
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
runOnFxThreadAndWait(() -> {
trackingReplayProperty.set(new Pair<>(1, LiveReplayAction.NOTIFY_ME));
instance.notifyMeItem.fire();
});
verify(liveReplayService).stopTrackingReplay();
}

@Test
public void testOnClickedRunReplayWhenReplayIsAvailable() {
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
runOnFxThreadAndWait(() -> instance.runReplayItem.fire());
verify(liveReplayService).performActionWhenAvailable(game, LiveReplayAction.RUN);
}

@Test
public void testOnClickedRunReplayWhenReplayIsAlreadyTracking() {
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
runOnFxThreadAndWait(() -> {
trackingReplayProperty.set(new Pair<>(1, LiveReplayAction.RUN));
instance.runReplayItem.fire();
});
verify(liveReplayService).stopTrackingReplay();
}

@Test
public void testCheckNotifyMeItemTextWhenTrackingReplayPropertyIsChanged() {
game.setId(1);
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
assertEquals("notify me", instance.notifyMeItem.getText());
runOnFxThreadAndWait(() -> trackingReplayProperty.set(new Pair<>(1, LiveReplayAction.NOTIFY_ME)));
assertEquals("cancel: notify me", instance.notifyMeItem.getText());
}

@Test
public void testCheckRunReplayItemTextWhenTrackingReplayPropertyIsChanged() {
game.setId(1);
when(liveReplayService.canWatchReplay(game)).thenReturn(false);

setGame(game);
assertEquals("run replay", instance.runReplayItem.getText());
runOnFxThreadAndWait(() -> trackingReplayProperty.set(new Pair<>(1, LiveReplayAction.RUN)));
assertEquals("cancel: run replay", instance.runReplayItem.getText());
}

private void setGame(GameBean game) {
runOnFxThreadAndWait(() -> instance.setGame(game));
}

private void clickWatchButton() {
runOnFxThreadAndWait(()-> instance.watchButton.fire());
runOnFxThreadAndWait(() -> instance.watchButton.fire());
}

@AfterEach
Expand Down

0 comments on commit 92b9d48

Please sign in to comment.