Feat/chat tests #317

Merged
m.ginkel merged 14 commits from feat/chat-tests into main 2026-04-27 00:52:31 +02:00
5 changed files with 31 additions and 29 deletions
Showing only changes of commit e9b40e9d04 - Show all commits
@@ -2,9 +2,7 @@ package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import java.util.List; import java.util.List;
/** /** Interface used to create test instances of the ChatClient */
* Interface used to create test instances of the ChatClient
*/
public interface ChatClientInterface { public interface ChatClientInterface {
List<Message> getMessages(); List<Message> getMessages();
@@ -12,5 +10,4 @@ public interface ChatClientInterface {
void sendMessage(Message message); void sendMessage(Message message);
List<String> getUsers(); List<String> getUsers();
} }
@@ -113,7 +113,9 @@ public class ChatController {
} }
/** /**
* Another constructor to give the ChatClient directly instead of the Client Service used only for test purposes * Another constructor to give the ChatClient directly instead of the Client Service used only
* for test purposes
*
* @param username * @param username
*/ */
public ChatController(String username, ChatClientInterface chatClient) { public ChatController(String username, ChatClientInterface chatClient) {
@@ -166,17 +166,17 @@ public class ChatBoxController {
if (!activeWhisperChats.contains(target)) { if (!activeWhisperChats.contains(target)) {
activeWhisperChats.add(target); activeWhisperChats.add(target);
ChatType whisper = ChatType.WHISPER; ChatType whisper = ChatType.WHISPER;
chatController chatController.getChatModelMap().put(new ChatKey(whisper, target), chatModel);
.getChatModelMap()
.put(new ChatKey(whisper, target), chatModel);
ChatViewController chatViewController = addChatTab(target, chatModel, whisper); ChatViewController chatViewController = addChatTab(target, chatModel, whisper);
Tab chatTab = chatViewController.getChatTab(); Tab chatTab = chatViewController.getChatTab();
chatTab.setOnCloseRequest(_ -> { chatTab.setOnCloseRequest(
_ -> {
chatViewController.tabIsOpen = false; chatViewController.tabIsOpen = false;
chatTabPane.getTabs().remove(chatTab); chatTabPane.getTabs().remove(chatTab);
}); });
} else { } else {
ChatViewController chatViewController = chatController.activeChatControllers.get(new ChatKey(ChatType.WHISPER, target)); ChatViewController chatViewController =
chatController.activeChatControllers.get(new ChatKey(ChatType.WHISPER, target));
if (!chatViewController.tabIsOpen) { if (!chatViewController.tabIsOpen) {
reopenChatTab(chatViewController.getChatTab()); reopenChatTab(chatViewController.getChatTab());
chatViewController.tabIsOpen = true; chatViewController.tabIsOpen = true;
@@ -211,8 +211,7 @@ public class ChatBoxController {
} else { } else {
chatKey = new ChatKey(chatType); chatKey = new ChatKey(chatType);
} }
chatController.activeChatControllers.put( chatController.activeChatControllers.put(chatKey, chatViewController);
chatKey, chatViewController);
fxmlLoader.setController(chatViewController); fxmlLoader.setController(chatViewController);
Node load = fxmlLoader.load(); Node load = fxmlLoader.load();
VBox.setVgrow(load, Priority.ALWAYS); VBox.setVgrow(load, Priority.ALWAYS);
@@ -291,7 +290,8 @@ public class ChatBoxController {
} }
public void reopenChatTab(Tab chatTab) { public void reopenChatTab(Tab chatTab) {
Platform.runLater(() -> { Platform.runLater(
() -> {
chatTabPane.getTabs().add(chatTab); chatTabPane.getTabs().add(chatTab);
chatTabPane.getSelectionModel().select(chatTab); chatTabPane.getSelectionModel().select(chatTab);
}); });
@@ -2,10 +2,8 @@ package ch.unibas.dmi.dbis.cs108.casono.client.ui.chatui;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatController;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel; import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatModel;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.ChatType;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message; import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@@ -50,7 +48,11 @@ public class ChatViewController implements Initializable {
private static final int CHAT_PADDING = 20; private static final int CHAT_PADDING = 20;
public ChatViewController(ChatController chatController, ChatModel chatModel, String username, ChatBoxController chatBoxController) { public ChatViewController(
ChatController chatController,
ChatModel chatModel,
String username,
ChatBoxController chatBoxController) {
this.controller = chatController; this.controller = chatController;
this.username = username; this.username = username;
this.chatModel = chatModel; this.chatModel = chatModel;
@@ -1,14 +1,13 @@
package ch.unibas.dmi.dbis.cs108.casono.client.chat; package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.util.ArrayList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ChatControllerTest { public class ChatControllerTest {
ChatController chatController1; ChatController chatController1;
@@ -117,7 +116,9 @@ public class ChatControllerTest {
@Test @Test
public void sendWhisperMessage() { public void sendWhisperMessage() {
ChatModel whisper = new ChatModel(ChatType.WHISPER, name2, -1, name1); ChatModel whisper = new ChatModel(ChatType.WHISPER, name2, -1, name1);
chatController2.getChatModelMap().put(new ChatController.ChatKey(ChatType.WHISPER, name1), whisper); chatController2
.getChatModelMap()
.put(new ChatController.ChatKey(ChatType.WHISPER, name1), whisper);
Message msg1 = new Message(ChatType.WHISPER, -1, name1, name2, "Hallo Welt"); Message msg1 = new Message(ChatType.WHISPER, -1, name1, name2, "Hallo Welt");
Message msg2 = new Message(ChatType.WHISPER, -1, name1, name2, "$$Hallo$$Welt$$"); Message msg2 = new Message(ChatType.WHISPER, -1, name1, name2, "$$Hallo$$Welt$$");