Skip to content

Commit

Permalink
Merge 828bf6f into 2f537b1
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-Spector committed Nov 15, 2020
2 parents 2f537b1 + 828bf6f commit 8301287
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,12 @@ protected List<CategoryOrChatUserListItem> getChatUserItemsByCategory(ChatUserCa
}
return users;
}

@VisibleForTesting
protected boolean checkUsersAreInList(ChatUserCategory category, String... usernames) {
List<String> names = Arrays.asList(usernames);
long foundItems = getChatUserItemsByCategory(category).stream()
.map(userItem -> userItem.getUser().getUsername()).filter(names::contains).count();
return foundItems == names.size();
}
}
17 changes: 13 additions & 4 deletions src/main/java/com/faforever/client/chat/UserFilterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,18 @@ public void filterUsers() {
}

private boolean filterUser(CategoryOrChatUserListItem userListItem) {
if (userListItem.getCategory() != null) {

// The categories should display in the list independently of a filter
return true;
}

ChatChannelUser user = userListItem.getUser();
return userListItem.getCategory() != null
|| (channelTabController.isUsernameMatch(user)
return channelTabController.isUsernameMatch(user)
&& isInClan(user)
&& isBoundByRating(user)
&& isGameStatusMatch(user)
&& isCountryMatch(user));
&& isCountryMatch(user);
}

private void filterCountry() {
Expand Down Expand Up @@ -177,8 +182,12 @@ boolean isGameStatusMatch(ChatChannelUser chatUser) {
}

boolean isCountryMatch(ChatChannelUser chatUser) {
if (countryFilterField.getText().isEmpty()) {
return true;
}

Optional<Player> playerOptional = chatUser.getPlayer();
if (!playerOptional.isPresent()) {
if (playerOptional.isEmpty()) {
return false;
}

Expand Down
127 changes: 122 additions & 5 deletions src/test/java/com/faforever/client/chat/ChannelTabControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.faforever.client.user.UserService;
import com.faforever.client.util.TimeService;
import com.google.common.eventbus.EventBus;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.MapChangeListener.Change;
Expand All @@ -34,9 +33,12 @@
import org.testfx.util.WaitForAsyncUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.faforever.client.player.SocialStatus.FOE;
import static com.faforever.client.player.SocialStatus.FRIEND;
Expand All @@ -62,6 +64,7 @@ public class ChannelTabControllerTest extends AbstractPlainJavaFxTest {
private static final String CHANNEL_NAME = "#testChannel";

private ChannelTabController instance;
private UserFilterController userFilterController;

@Mock
private ChatService chatService;
Expand All @@ -82,8 +85,6 @@ public class ChannelTabControllerTest extends AbstractPlainJavaFxTest {
@Mock
private UiService uiService;
@Mock
private UserFilterController userFilterController;
@Mock
private ChatUserItemController chatUserItemController;
@Mock
private ChatUserItemCategoryController chatUserItemCategoryController;
Expand All @@ -110,6 +111,7 @@ public void setUp() throws Exception {
notificationService, reportingService,
uiService, eventBus, webViewConfigurer, countryFlagService,
platformService);
userFilterController = new UserFilterController(i18n, countryFlagService);

defaultChannel = new Channel(CHANNEL_NAME);
preferences = new Preferences();
Expand All @@ -118,11 +120,10 @@ public void setUp() throws Exception {
when(uiService.loadFxml("theme/chat/user_filter.fxml")).thenReturn(userFilterController);
when(uiService.loadFxml("theme/chat/chat_user_item.fxml")).thenReturn(chatUserItemController);
when(uiService.loadFxml("theme/chat/chat_user_category.fxml")).thenReturn(chatUserItemCategoryController);
when(userFilterController.getRoot()).thenReturn(new Pane());
when(userFilterController.filterAppliedProperty()).thenReturn(new SimpleBooleanProperty(false));
when(chatUserItemController.getRoot()).thenReturn(new Pane());
when(uiService.getThemeFileUrl(CHAT_CONTAINER)).thenReturn(getClass().getResource("/theme/chat/chat_container.html"));

loadFxml("theme/chat/user_filter.fxml", clazz -> userFilterController);
loadFxml("theme/chat/channel_tab.fxml", clazz -> instance);

TabPane tabPane = new TabPane();
Expand Down Expand Up @@ -670,4 +671,120 @@ public void testWhenUserAddPlayerToFoeListByAccidentallyAndCancelsActionImmediat
assertEquals(0, enemies.size());
assertEquals(2, otherUsers.size());
}

@Test
public void testFindModeratorByName() {
defaultChannel.addUsers(createUserList(Map.of(
ChatUserCategory.MODERATOR, Arrays.asList("1Moderator", "12Moderator", "123Moderator")
)));

runOnFxThreadAndWait(() -> instance.initialize());
runOnFxThreadAndWait(() -> instance.setChannel(defaultChannel));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("12"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.MODERATOR, "12Moderator", "123Moderator"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("123"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.MODERATOR, "123Moderator"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText(""));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.MODERATOR, "1Moderator", "12Moderator", "123Moderator"));
}

@Test
public void testFindOtherUserByName() {
defaultChannel.addUsers(createUserList(Map.of(
ChatUserCategory.OTHER, Arrays.asList("MarcSpector", "Lenkin")
)));

runOnFxThreadAndWait(() -> instance.initialize());
runOnFxThreadAndWait(() -> instance.setChannel(defaultChannel));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("Marc"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.OTHER, "MarcSpector"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("Lenkin"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.OTHER, "Lenkin"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText(""));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.OTHER, "MarcSpector", "Lenkin"));
}

@Test
public void testFindEnemyUserByName() {
defaultChannel.addUsers(createUserList(Map.of(
ChatUserCategory.FOE, Arrays.asList("EnemyPlayer", "RandomFoePlayer")
)));

runOnFxThreadAndWait(() -> instance.initialize());
runOnFxThreadAndWait(() -> instance.setChannel(defaultChannel));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("Random"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.FOE, "RandomFoePlayer"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText(""));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.FOE, "EnemyPlayer", "RandomFoePlayer"));
}

@Test
public void testFindChatOnlyUserByName() {
defaultChannel.addUsers(createUserList(Map.of(
ChatUserCategory.CHAT_ONLY, Arrays.asList("ChatOnlyPlayer", "ExamplePlayer")
)));

runOnFxThreadAndWait(() -> instance.initialize());
runOnFxThreadAndWait(() -> instance.setChannel(defaultChannel));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("Only"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.CHAT_ONLY, "ChatOnlyPlayer"));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText(""));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.CHAT_ONLY, "ChatOnlyPlayer", "ExamplePlayer"));
}

@Test
public void testFindSimilarNamesOfUsersInList() {
defaultChannel.addUsers(createUserList(Map.of(
ChatUserCategory.MODERATOR, Arrays.asList("1_NAme_1", "Moderator"),
ChatUserCategory.CHAT_ONLY, Arrays.asList("ChatOnlyNameOlolo", "Bingo"),
ChatUserCategory.OTHER, Arrays.asList("Player", "PlayerNamEEE"),
ChatUserCategory.FRIEND, Arrays.asList("Friend", "NaMeFriend"),
ChatUserCategory.FOE, Arrays.asList("FOE", "For______NAME")
)));

runOnFxThreadAndWait(() -> instance.initialize());
runOnFxThreadAndWait(() -> instance.setChannel(defaultChannel));

runOnFxThreadAndWait(() -> instance.userSearchTextField.setText("name"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.MODERATOR, "1_NAme_1"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.CHAT_ONLY, "ChatOnlyNameOlolo"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.OTHER, "PlayerNamEEE"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.FRIEND, "NaMeFriend"));
assertTrue(instance.checkUsersAreInList(ChatUserCategory.FOE, "For______NAME"));
}

private List<ChatChannelUser> createUserList(Map<ChatUserCategory, List<String>> map) {
List<ChatChannelUser> list = new ArrayList<>();
map.forEach((category, usernames) -> list.addAll(prepareUserList(category, usernames)));
return list;
}

private List<ChatChannelUser> prepareUserList(ChatUserCategory category, List<String> usernames) {
return usernames.stream().map(name -> {
ChatChannelUserBuilder builder = ChatChannelUserBuilder.create(name);
PlayerBuilder playerBuilder = PlayerBuilder.create(name);
switch (category) {
case OTHER -> builder.player(playerBuilder.socialStatus(OTHER).get());
case FRIEND -> builder.player(playerBuilder.socialStatus(FRIEND).get());
case FOE -> builder.player(playerBuilder.socialStatus(FOE).get());
case MODERATOR -> builder.player(playerBuilder.socialStatus(OTHER).get()).moderator(true);
case CHAT_ONLY -> builder.player(null);
}
return builder.get();
}).peek(user -> {
if (user.getPlayer().isPresent()) {
when(playerService.getPlayerForUsername(user.getUsername())).thenReturn(user.getPlayer());
}
}).collect(Collectors.toList());
}
}

0 comments on commit 8301287

Please sign in to comment.