From a295547be1764df4eb07f5925182267276f4b27d Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 7 Feb 2024 06:26:19 +1000 Subject: [PATCH] Adds appearance tests. Fixed up saving the background colour as a result of the tests. --- src/urChatBasic/base/IRCChannelBase.java | 5 +- src/urChatBasic/frontend/LineFormatter.java | 9 +- src/urChatBasic/frontend/UserGUI.java | 4 +- .../frontend/components/FontPanel.java | 25 +++--- .../frontend/dialogs/FontDialog.java | 4 +- tests/frontend/AppearanceTests.java | 82 ++++++++++++++++++- tests/utils/TestUtils.java | 16 ++++ 7 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 tests/utils/TestUtils.java diff --git a/src/urChatBasic/base/IRCChannelBase.java b/src/urChatBasic/base/IRCChannelBase.java index 34ed5ba..891eb9e 100644 --- a/src/urChatBasic/base/IRCChannelBase.java +++ b/src/urChatBasic/base/IRCChannelBase.java @@ -215,7 +215,7 @@ private void initChannel() // this.myMenu = new ChannelPopUp(); createChannelPopUp(); fontDialog.setVisible(false); - fontDialog.addSaveListener(new SaveFontListener()); + fontDialog.addFontSaveListener(new SaveFontListener()); myActions = new IRCActions(this); } @@ -1014,7 +1014,8 @@ public void setFont(Font f) { public void run() { - lineFormatter.setFont(fontDialog.getFontPanel().getFont()); + // lineFormatter.setFont(fontDialog.getFontPanel().getFont()); + lineFormatter.setStyle(fontDialog.getFontPanel().getStyle()); } }); } else diff --git a/src/urChatBasic/frontend/LineFormatter.java b/src/urChatBasic/frontend/LineFormatter.java index 91232b2..50d337b 100644 --- a/src/urChatBasic/frontend/LineFormatter.java +++ b/src/urChatBasic/frontend/LineFormatter.java @@ -84,6 +84,13 @@ public LineFormatter(URStyle baseStyle, JTextPane docOwner ,final IRCServerBase initStyles(baseStyle); } + public void setStyle (URStyle newStyle) + { + targetStyle = newStyle.clone(); + if (doc.getLength() > 0) + updateStyles(targetStyle); + } + public void setFont (Font newFont) { targetStyle.setFont(newFont); @@ -156,6 +163,7 @@ public URStyle defaultStyle(String name, boolean load) StyleConstants.setItalic(tempStyle, targetStyle.getFont().isItalic()); StyleConstants.setForeground(tempStyle, myForeground); + StyleConstants.setBackground(tempStyle, myBackground); if (load) tempStyle.load(settingsPath); @@ -783,7 +791,6 @@ public URStyle getStyleAtPosition(int position, String relativeLine) Constants.LOGGER.error(ble.getLocalizedMessage()); } AttributeSet textStyle = doc.getCharacterElement(position).getAttributes(); - return new URStyle(new SimpleAttributeSet(textStyle)); } diff --git a/src/urChatBasic/frontend/UserGUI.java b/src/urChatBasic/frontend/UserGUI.java index e4c9882..7859604 100644 --- a/src/urChatBasic/frontend/UserGUI.java +++ b/src/urChatBasic/frontend/UserGUI.java @@ -271,7 +271,7 @@ public Component getListCellRendererComponent (JList list, Object value, int }); // clientFontPanel.getSaveButton().addActionListener(new SaveFontListener()); - clientFontPanel.addSaveListener(new SaveFontListener()); + clientFontPanel.addFontSaveListener(new SaveFontListener()); // clientFontPanel.getResetButton().addActionListener(new ResetFontListener()); previewTextScroll.setPreferredSize(new Dimension(700, 150)); @@ -382,7 +382,7 @@ public void mouseClicked (MouseEvent mouseEvent) FontDialog styleFontDialog = new FontDialog(styleName, previewLineFormatter.getStyleDefault(styleName), URProfilesUtil.getActiveProfilePath()); - styleFontDialog.addSaveListener(new SaveFontListener()); + styleFontDialog.addFontSaveListener(new SaveFontListener()); styleFontDialog.setVisible(true); } else if (SwingUtilities.isLeftMouseButton(mouseEvent) && null != isClickableText) { diff --git a/src/urChatBasic/frontend/components/FontPanel.java b/src/urChatBasic/frontend/components/FontPanel.java index d03b02f..6a46f42 100644 --- a/src/urChatBasic/frontend/components/FontPanel.java +++ b/src/urChatBasic/frontend/components/FontPanel.java @@ -62,7 +62,7 @@ public void actionPerformed (ActionEvent arg0) { colourDialog = new ColourDialog(styleName, getDefaultStyle(), getSettingsPath()); - colourDialog.getColourPanel().addSaveListener(e -> { + getColourPanel().addSaveListener(e -> { Constants.LOGGER.info( "Font Panel says: Save Colour pressed"); }); @@ -73,7 +73,7 @@ public void actionPerformed (ActionEvent arg0) { if (listeners[i] == ActionListener.class) { - colourDialog.getColourPanel().addSaveListener((ActionListener) listenerList.getListeners(ActionListener.class)[i]); + getColourPanel().addSaveListener((ActionListener) listenerList.getListeners(ActionListener.class)[i]); } } @@ -88,7 +88,7 @@ public void actionPerformed (ActionEvent arg0) setFont(targetStyle, true); // now fire the rest of the save listeners - fireSaveListeners(); + fireFontSaveListeners(); }); FONT_COMBO_BOX.addItemListener(new FontSelectionChange()); @@ -154,6 +154,11 @@ public void actionPerformed (ActionEvent arg0) add(MAKE_UNDERLINE, c); } + public ColourPanel getColourPanel () + { + return colourDialog.getColourPanel(); + } + public JButton getSaveButton () { return SAVE_BUTTON; @@ -168,9 +173,9 @@ public void setDefaultStyle (final URStyle f) { // defaultFont = f; defaultStyle = f.clone(); - colourDialog.getColourPanel().setDefaultStyle(defaultStyle); + getColourPanel().setDefaultStyle(defaultStyle); loadStyle(); - fireSaveListeners(); + fireFontSaveListeners(); } private URStyle getDefaultStyle () @@ -212,7 +217,7 @@ public void setFont (Font f) public void setStyle (final URStyle newStyle) { targetStyle = newStyle.clone(); - colourDialog.getColourPanel().setStyle(targetStyle); + getColourPanel().setStyle(targetStyle); setFont(targetStyle, false); } @@ -240,7 +245,7 @@ public void setFont (final URStyle newStyle, Boolean saveToSettings) if (saveToSettings) { - URStyle colourPanelStyle = colourDialog.getColourPanel().getStyle(); + URStyle colourPanelStyle = getColourPanel().getStyle(); colourPanelStyle.getForeground().ifPresent(fg -> targetStyle.setForeground(fg)); colourPanelStyle.getBackground().ifPresent(bg -> targetStyle.setBackground(bg)); URPreferencesUtil.saveStyle(defaultStyle, targetStyle, settingsPath); @@ -271,12 +276,12 @@ private void previewFont () targetStyle.setFont(getFont()); } - public void addSaveListener (ActionListener actionListener) + public void addFontSaveListener (ActionListener actionListener) { listenerList.add(ActionListener.class, actionListener); } - protected void fireSaveListeners () + protected void fireFontSaveListeners () { Object[] listeners = this.listenerList.getListenerList(); @@ -336,7 +341,7 @@ public void setSettingsPath (Preferences settingsPath) this.settingsPath = settingsPath; loadStyle(); - colourDialog.getColourPanel().setSettingsPath(settingsPath); + getColourPanel().setSettingsPath(settingsPath); } public Preferences getSettingsPath () diff --git a/src/urChatBasic/frontend/dialogs/FontDialog.java b/src/urChatBasic/frontend/dialogs/FontDialog.java index 60b749a..9c3586c 100644 --- a/src/urChatBasic/frontend/dialogs/FontDialog.java +++ b/src/urChatBasic/frontend/dialogs/FontDialog.java @@ -52,10 +52,10 @@ public void setVisible(boolean b) super.setVisible(b); } - public void addSaveListener(ActionListener newActionListener) + public void addFontSaveListener(ActionListener newActionListener) { // fontPanel.getSaveButton().addActionListener(newActionListener); - fontPanel.addSaveListener(newActionListener); + fontPanel.addFontSaveListener(newActionListener); } public void addResetListener(ActionListener newActionListener) diff --git a/tests/frontend/AppearanceTests.java b/tests/frontend/AppearanceTests.java index 276c575..7f2625f 100644 --- a/tests/frontend/AppearanceTests.java +++ b/tests/frontend/AppearanceTests.java @@ -26,6 +26,7 @@ import urChatBasic.frontend.UserGUI; import urChatBasic.frontend.utils.URColour; import utils.TestDriverGUI; +import utils.TestUtils; public class AppearanceTests { @@ -89,7 +90,7 @@ public void tearDown () throws Exception */ @Test - public void changeDefaultFontTest () throws BadLocationException, InterruptedException + public void changeDefaultFontAndSizeTest () throws BadLocationException, InterruptedException { // Get Current Font in Appearance panel URStyle guiStyle = testGUI.getStyle(); @@ -115,6 +116,77 @@ public void changeDefaultFontTest () throws BadLocationException, InterruptedExc testGUI.getFontPanel().setDefaultStyle(newStyle); TestDriverGUI.waitForEverything(testGUI); guiStyle = testGUI.getStyle(); + + + assertEquals(newStyle, guiStyle); + + for (String pubChannelName : PUB_CHANNEL_NAMES) + { + IRCChannel pubChannel = testServer.getCreatedChannel(pubChannelName); + String welcomeMessage = pubChannel.getLineFormatter().getLineAtPosition(13).split("] ")[1].trim(); + log("Check current style has updated.", true); + + URStyle channelStyle = pubChannel.getLineFormatter().getStyleAtPosition(22, welcomeMessage); + + log("Test Style: " + guiStyle, true); + log("Channel Style: " + channelStyle, true); + + String testStyleFont = guiStyle.getFamily().get(); + String channelStyleFont = channelStyle.getFamily().get(); + TestDriverGUI.waitForEverything(testGUI); + log("Checking "+pubChannelName+" formatting...", true); + + assertEquals(pubChannelName + " font family doesn't match GUI font family.", testStyleFont, channelStyleFont); + + int testStyleSize = guiStyle.getSize().get(); + int channelStyleSize = channelStyle.getSize().get(); + assertEquals(pubChannelName + " font size doesn't match GUI font size.", testStyleSize, channelStyleSize); + + String testStyleForeground = URColour.hexEncode(guiStyle.getForeground().get()); + String channelStyleForeground = URColour.hexEncode(channelStyle.getForeground().get()); + assertEquals(pubChannelName + " foreground doesn't match GUI font foreground.", testStyleForeground, channelStyleForeground); + + String testStyleBackground = URColour.hexEncode(guiStyle.getBackground().get()); + String channelStyleBackground = URColour.hexEncode(channelStyle.getBackground().get()); + assertEquals(pubChannelName + " background doesn't match GUI font background.", testStyleBackground, channelStyleBackground); + + } + } + + @Test + public void changeDefaultForegroundAndBackgroundTest () throws BadLocationException, InterruptedException + { + // Get Current Font in Appearance panel + URStyle guiStyle = testGUI.getStyle(); + + // Get Current Font in all rooms + for (String pubChannelName : PUB_CHANNEL_NAMES) + { + IRCChannel pubChannel = testServer.getCreatedChannel(pubChannelName); + log("Have joined " + pubChannelName + " successfully?", true); + String welcomeMessage = pubChannel.getLineFormatter().getLineAtPosition(13).split("] ")[1].trim(); + assertEquals(" Welcome to " + pubChannelName, welcomeMessage); + + log("Check current style in the channel is correct.", true); + URStyle channelStyle = pubChannel.getLineFormatter().getStyleAtPosition(22, welcomeMessage); + + assertTrue(guiStyle.equals(channelStyle)); + } + + URStyle newStyle = guiStyle.clone(); + newStyle.setForeground(TestUtils.getRandomColour()); + + log("Set foreground to " +URColour.hexEncode(newStyle.getForeground().get()), true); + + newStyle.setBackground(TestUtils.getRandomColour()); + + log("Set background to " +URColour.hexEncode(newStyle.getBackground().get()), true); + testGUI.getFontPanel().setDefaultStyle(newStyle); + TestDriverGUI.waitForEverything(testGUI); + guiStyle = testGUI.getStyle(); + + assertEquals(newStyle, guiStyle); + for (String pubChannelName : PUB_CHANNEL_NAMES) { IRCChannel pubChannel = testServer.getCreatedChannel(pubChannelName); @@ -129,6 +201,7 @@ public void changeDefaultFontTest () throws BadLocationException, InterruptedExc String testStyleFont = guiStyle.getFamily().get(); String channelStyleFont = channelStyle.getFamily().get(); assertEquals("Channel font family doesn't match GUI font family.", testStyleFont, channelStyleFont); + log(pubChannelName + " font is good.", true); int testStyleSize = guiStyle.getSize().get(); int channelStyleSize = channelStyle.getSize().get(); @@ -136,12 +209,13 @@ public void changeDefaultFontTest () throws BadLocationException, InterruptedExc String testStyleForeground = URColour.hexEncode(guiStyle.getForeground().get()); String channelStyleForeground = URColour.hexEncode(channelStyle.getForeground().get()); - assertEquals("Channel foreground doesn't match GUI font foreground.", testStyleForeground, channelStyleForeground); + assertEquals(pubChannelName + " foreground doesn't match GUI font foreground.", testStyleForeground, channelStyleForeground); + log(pubChannelName + " Foreground is good.", true); String testStyleBackground = URColour.hexEncode(guiStyle.getBackground().get()); String channelStyleBackground = URColour.hexEncode(channelStyle.getBackground().get()); - assertEquals("Channel background doesn't match GUI font background.", testStyleBackground, channelStyleBackground); - + assertEquals(pubChannelName + " background doesn't match GUI font background.", testStyleBackground, channelStyleBackground); + log(pubChannelName + " background is good.", true); } } } diff --git a/tests/utils/TestUtils.java b/tests/utils/TestUtils.java new file mode 100644 index 0000000..3439c92 --- /dev/null +++ b/tests/utils/TestUtils.java @@ -0,0 +1,16 @@ +package utils; + +import java.awt.Color; +import java.util.Random; + +public class TestUtils { + + public static Color getRandomColour () + { + int red = new Random().nextInt(256); + int green = new Random().nextInt(256); + int blue = new Random().nextInt(256); + + return new Color(red, green, blue); + } +}