Skip to content

Commit

Permalink
Remove eventbus from UserButtonController (#2971)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 authored May 10, 2023
1 parent c0a936c commit ed3b029
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 37 deletions.
23 changes: 8 additions & 15 deletions src/main/java/com/faforever/client/main/UserButtonController.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.faforever.client.main;

import com.faforever.client.domain.PlayerBean;
import com.faforever.client.fx.Controller;
import com.faforever.client.fx.JavaFxUtil;
import com.faforever.client.player.PlayerInfoWindowController;
import com.faforever.client.player.PlayerService;
import com.faforever.client.preferences.PreferencesService;
import com.faforever.client.reporting.ReportDialogController;
import com.faforever.client.theme.UiService;
import com.faforever.client.user.UserService;
import com.faforever.client.user.event.LogOutRequestEvent;
import com.faforever.client.user.event.LoginSuccessEvent;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import javafx.event.ActionEvent;
import javafx.beans.value.ObservableValue;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
Expand All @@ -29,25 +26,21 @@ public class UserButtonController implements Controller<Node> {
private final EventBus eventBus;
private final PlayerService playerService;
private final UiService uiService;
private final UserService userService;
private final PreferencesService preferencesService;
public MenuButton userMenuButtonRoot;

public void initialize() {
eventBus.register(this);
}
ObservableValue<Boolean> showing = JavaFxUtil.showingProperty(getRoot());

@Subscribe
public void onLoginSuccessEvent(LoginSuccessEvent event) {
JavaFxUtil.runLater(() -> userMenuButtonRoot.setText(userService.getUsername()));
userMenuButtonRoot.textProperty()
.bind(playerService.currentPlayerProperty().flatMap(PlayerBean::usernameProperty).when(showing));
}

@Override
public Node getRoot() {
return userMenuButtonRoot;
}

public void onShowProfile(ActionEvent event) {
public void onShowProfile() {
PlayerInfoWindowController playerInfoWindowController = uiService.loadFxml("theme/user_info_window.fxml");
playerInfoWindowController.setPlayer(playerService.getCurrentPlayer());
Scene scene = userMenuButtonRoot.getScene();
Expand All @@ -57,7 +50,7 @@ public void onShowProfile(ActionEvent event) {
playerInfoWindowController.show();
}

public void onReport(ActionEvent event) {
public void onReport() {
ReportDialogController reportDialogController = uiService.loadFxml("theme/reporting/report_dialog.fxml");
reportDialogController.setAutoCompleteWithOnlinePlayers();
Scene scene = userMenuButtonRoot.getScene();
Expand All @@ -67,7 +60,7 @@ public void onReport(ActionEvent event) {
reportDialogController.show();
}

public void onLogOut(ActionEvent actionEvent) {
public void onLogOut() {
eventBus.post(new LogOutRequestEvent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.faforever.client.theme.UiService;
import com.faforever.commons.lobby.GameStatus;
import com.google.common.eventbus.EventBus;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.control.ContextMenu;
import javafx.scene.image.Image;
Expand Down Expand Up @@ -90,6 +91,7 @@ public class ChatUserItemControllerTest extends UITest {
public void setUp() throws Exception {
defaultUser = ChatChannelUserBuilder.create(USER_NAME, CHANNEL_NAME).defaultValues().get();

when(mapService.isInstalledBinding(anyString())).thenReturn(new SimpleBooleanProperty());
when(i18n.get(eq("clan.messageLeader"))).thenReturn("Message clan leader");
when(i18n.get(eq("clan.visitPage"))).thenReturn("Visit clan website");
doAnswer(invocation -> new SimpleObjectProperty<>(invocation.getArgument(0))).when(imageViewHelper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
import com.faforever.client.reporting.ReportDialogController;
import com.faforever.client.test.UITest;
import com.faforever.client.theme.UiService;
import com.faforever.client.user.UserService;
import com.faforever.client.user.event.LogOutRequestEvent;
import com.faforever.client.user.event.LoginSuccessEvent;
import com.google.common.eventbus.EventBus;
import javafx.beans.property.SimpleObjectProperty;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.testfx.util.WaitForAsyncUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -35,8 +32,6 @@ public class UserButtonControllerTest extends UITest {
private ReportDialogController reportDialogController;
@Mock
private PlayerInfoWindowController playerInfoWindowController;
@Mock
private UserService userService;


@InjectMocks
Expand All @@ -50,43 +45,32 @@ public void setUp() throws Exception {

player = PlayerBeanBuilder.create().defaultValues().username(TEST_USER_NAME).get();
when(playerService.getCurrentPlayer()).thenReturn(player);
when(userService.getUsername()).thenReturn(TEST_USER_NAME);
when(playerService.currentPlayerProperty()).thenReturn(new SimpleObjectProperty<>(player));

loadFxml("theme/user_button.fxml", clazz -> instance);
}

@Test
public void testOnLoginSuccess() {
instance.onLoginSuccessEvent(new LoginSuccessEvent());
WaitForAsyncUtils.waitForFxEvents();

assertEquals(TEST_USER_NAME, instance.userMenuButtonRoot.getText());
}

@Test
public void testOnGetRoot() {
assertEquals(instance.userMenuButtonRoot, instance.getRoot());
runOnFxThreadAndWait(() -> getRoot().getChildren().add(instance.getRoot()));
}

@Test
public void testShowProfile() {
instance.onShowProfile(null);
instance.onShowProfile();

verify(playerInfoWindowController).setPlayer(player);
verify(playerInfoWindowController).show();
}

@Test
public void testReport() {
instance.onReport(null);
instance.onReport();

verify(reportDialogController).setAutoCompleteWithOnlinePlayers();
verify(reportDialogController).show();
}

@Test
public void testLogOut() {
instance.onLogOut(null);
instance.onLogOut();

verify(eventBus).post(any(LogOutRequestEvent.class));
}
Expand Down

0 comments on commit ed3b029

Please sign in to comment.