Valentin Spiroski
11 months ago
5 changed files with 250 additions and 8 deletions
-
21pom.xml
-
47src/main/java/ChatClient.java
-
67src/main/java/EmojiPicker.java
-
30src/test/java/TestClientHandlerConstructor.java
-
93src/test/java/TestEmojiPicker.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 |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
|||
|
@ -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)); |
|||
} |
|||
} |
@ -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 |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue