From 08935f3889149adb3c29343578a9a419ee0c9788 Mon Sep 17 00:00:00 2001 From: Valentin Spiroski Date: Fri, 9 Feb 2024 23:16:45 +0100 Subject: [PATCH] test_EmojiPickerActionListenerFunctionality --- src/main/java/EmojiPicker.java | 1 + src/test/java/TestEmojiPicker.java | 32 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/EmojiPicker.java b/src/main/java/EmojiPicker.java index 17a7f7a..dc991e1 100644 --- a/src/main/java/EmojiPicker.java +++ b/src/main/java/EmojiPicker.java @@ -52,6 +52,7 @@ public class EmojiPicker extends JPanel { @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 } } diff --git a/src/test/java/TestEmojiPicker.java b/src/test/java/TestEmojiPicker.java index 1ad2632..1279081 100644 --- a/src/test/java/TestEmojiPicker.java +++ b/src/test/java/TestEmojiPicker.java @@ -71,23 +71,23 @@ public class TestEmojiPicker { } } - //Test to verify the functionality of EmojiActionListener for each emoji button +//Test to verify the functionality of EmojiActionListener for each emoji button @Test public void testEmojiPickerActionListenerFunctionality() { - // Arrange - JTextField textField = new JTextField(); - EmojiPicker emojiPicker = new EmojiPicker(textField); - 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); - assertEquals(button.getText(), textField.getText()); // Check if emoji is appended to the text field - } + // 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 + } } } -