Browse Source

Merge branch 'client-handler'

main
Valentin Spiroski 11 months ago
parent
commit
9c44958b73
  1. 21
      pom.xml
  2. 47
      src/main/java/ChatClient.java
  3. 67
      src/main/java/EmojiPicker.java
  4. 30
      src/test/java/TestClientHandlerConstructor.java
  5. 93
      src/test/java/TestEmojiPicker.java

21
pom.xml

@ -17,13 +17,20 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>5.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>

47
src/main/java/ChatClient.java

@ -77,6 +77,10 @@ public class ChatClient extends JFrame implements KeyListener {
private Timer timer;
private JLabel timeLabel;
// Emoji
private JButton emojiButton;
private EmojiPicker emojiPicker;
public ChatClient() {
super("Chat");
address = JOptionPane.showInputDialog("bitte IP-Adresse");
@ -106,9 +110,39 @@ public class ChatClient extends JFrame implements KeyListener {
prepareOutputTextArea();
createInputTextField();
showCurrentTime();
outputTextArea = new JTextArea();
outputTextArea.setEditable(false);
outputTextArea.setBorder(BorderFactory.createTitledBorder("Chat"));
outputTextArea.setBackground(Color.lightGray);
outputScrollPane = new JScrollPane(outputTextArea);
timeLabel = new JLabel();
updateTime();
add(timeLabel, BorderLayout.NORTH);
inputTextField = new JTextField();
inputTextField.setBorder(BorderFactory.createTitledBorder("Nachricht eingeben"));
inputTextField.addKeyListener(this);
// Create a panel to hold input text field and emoji picker
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.add(inputTextField, BorderLayout.CENTER); // Add input text field to the center of the panel
// Initialize EmojiPicker and add it to the input text field
emojiPicker = new EmojiPicker(inputTextField);
// Create a panel to hold emoji button
JPanel emojiPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
emojiButton = new JButton("😀"); // Create emoji button with a smiley face
emojiButton.setPreferredSize(new Dimension(50, 50));// Set preferred size for the emoji button
emojiButton.addActionListener(e -> showEmojiPicker());// Add action listener to show emoji picker when button is clicked
emojiPanel.add(emojiButton); // Add emoji button to panel
inputPanel.add(emojiPanel, BorderLayout.LINE_END);// Add emoji panel to the right side of the input panel
add(outputScrollPane, BorderLayout.CENTER);
add(inputTextField, BorderLayout.SOUTH);
add(inputTextField, BorderLayout.PAGE_END);
add(timeLabel, BorderLayout.NORTH);
setVisible(true);
@ -137,6 +171,17 @@ public class ChatClient extends JFrame implements KeyListener {
updateTime();
}
private void showEmojiPicker() {
// Create a dialog to display the emoji picker
JDialog emojiDialog = new JDialog(this, "Emoji Picker", true); // Create a modal dialog with a title "Emoji Picker"
emojiDialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // Set close operation to dispose the dialog when closed
emojiDialog.getContentPane().add(emojiPicker); // Add the emoji picker component to the content pane of the dialog
emojiDialog.pack(); // Pack the dialog to fit the preferred size of its components
emojiDialog.setLocationRelativeTo(this);// Set the location of the dialog relative to the parent frame
emojiDialog.setVisible(true); // Make the dialog visible
}
private void receiveMessages() {
try {
connectionToServer = new Socket(address, Constants.PORT);

67
src/main/java/EmojiPicker.java

@ -0,0 +1,67 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* A panel containing a selection of emoji buttons to be used in conjunction with a JTextField.
*/
public class EmojiPicker extends JPanel {
private JTextField textField; // Text field to append selected emoji
// Array containing a selection of emojis
private static final String[] emojis = {"😀", "😃", "😄", "😁", "😆", "😅",
"😂", "🤣", "😊", "😇", "🙂", "🙃"};
/**
* Constructs an EmojiPicker with a specified JTextField.
* @param textField The JTextField to append selected emojis.
*/
public EmojiPicker(JTextField textField) {
setLayout(new GridLayout(4, 3, 5, 5)); // Set grid layout for the emoji buttons
// Create and add buttons for each emoji
for (String emoji : emojis) {
JButton emojiButton = new JButton(emoji);
emojiButton.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 20)); // Set font for emoji buttons
emojiButton.addActionListener(new EmojiActionListener(emoji)); // Add action listener to each button
add(emojiButton); // Add emoji button to the panel
}
setPreferredSize(new Dimension(300, 200)); // Set preferred size for the EmojiPicker panel
}
/**
* ActionListener for handling emoji button clicks.
*/
class EmojiActionListener implements ActionListener {
private String emoji; // Selected emoji
/**
* Constructs an EmojiActionListener with the specified emoji.
* @param emoji The emoji associated with this listener.
*/
public EmojiActionListener(String emoji) {
this.emoji = emoji;
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Selected emoji: " + emoji); // Print selected emoji to console
System.out.println("textField: " + textField); // Print the value of textField
textField.setText(textField.getText() + emoji); // Append selected emoji to text field
}
}
}

30
src/test/java/TestClientHandlerConstructor.java

@ -0,0 +1,30 @@
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.*;
import java.net.*;
public class TestClientHandlerConstructor {
@Test
public void testConstructor() {
// Create a mock Socket
Socket mockSocket = new Socket();
// Create a ChatServer instance
int port = 12345; // Use any available port for testing
ChatServer mockChatServer = new ChatServer(port);
// Create a ClientHandler instance using the constructor
ClientHandler clientHandler = new ClientHandler(mockChatServer, mockSocket);
// Wait for a short time to allow the ClientHandler to be added to the ChatServer's list of clients
try {
Thread.sleep(1000); // Adjust the sleep duration as needed
} catch (InterruptedException e) {
e.printStackTrace();
}
// Assert that the clientHandler was added to the list of clients in the ChatServer
assertTrue(mockChatServer.getClients().contains(clientHandler));
}
}

93
src/test/java/TestEmojiPicker.java

@ -0,0 +1,93 @@
import org.junit.jupiter.api.Test;
import javax.swing.*;
import java.awt.*;
import static org.junit.jupiter.api.Assertions.*;
public class TestEmojiPicker {
//Test to ensure the EmojiPicker constructor initializes without errors
@Test
public void testEmojiPickerConstructor() {
// Arrange
JTextField textField = new JTextField();
// Act
EmojiPicker emojiPicker = new EmojiPicker(textField);
// Assert
assertNotNull(emojiPicker);
}
//Test to verify the correct number of emoji buttons in the EmojiPicker
@Test
public void testEmojiPickerButtonCount() {
// Arrange
JTextField textField = new JTextField();
EmojiPicker emojiPicker = new EmojiPicker(textField);
// Act
Component[] components = emojiPicker.getComponents();
// Assert
assertEquals(12, components.length); // 12 buttons for 12 emojis
}
//Test to check if each emoji button contains valid emoji text
@Test
public void testEmojiPickerButtonText() {
// Arrange
JTextField textField = new JTextField();
EmojiPicker emojiPicker = new EmojiPicker(textField);
// Act
Component[] components = emojiPicker.getComponents();
// Assert
for (Component component : components) {
assertTrue(component instanceof JButton);
JButton button = (JButton) component;
assertTrue(button.getText().matches("[\uD83C-\uDBFF\uDC00-\uDFFF]+")); // Check if button text is an emoji
}
}
// Test to ensure each emoji button has an EmojiActionListener attached
@Test
public void testEmojiPickerActionListener() {
// Arrange
JTextField textField = new JTextField();
EmojiPicker emojiPicker = new EmojiPicker(textField);
// Act
Component[] components = emojiPicker.getComponents();
// Assert
for (Component component : components) {
assertTrue(component instanceof JButton);
JButton button = (JButton) component;
assertNotNull(button.getActionListeners());
assertEquals(1, button.getActionListeners().length);
assertTrue(button.getActionListeners()[0] instanceof EmojiPicker.EmojiActionListener);
}
}
//Test to verify the functionality of EmojiActionListener for each emoji button
@Test
public void testEmojiPickerActionListenerFunctionality() {
// Arrange
JTextField textField = new JTextField(); // Initialize JTextField
EmojiPicker emojiPicker = new EmojiPicker(textField); // Pass the initialized JTextField to the EmojiPicker constructor
Component[] components = emojiPicker.getComponents();
// Act & Assert
for (Component component : components) {
assertTrue(component instanceof JButton);
JButton button = (JButton) component;
EmojiPicker.EmojiActionListener actionListener = (EmojiPicker.EmojiActionListener) button.getActionListeners()[0];
assertNotNull(actionListener);
actionListener.actionPerformed(null);
String expectedText = textField.getText() + button.getText(); // Expected text after appending emoji
assertEquals(expectedText, textField.getText(), "Failed for emoji: " + button.getText()); // Check if emoji is appended to the text field
}
}
}
Loading…
Cancel
Save