Skip to content

Commit

Permalink
Adds appearance tests. Fixed up saving the background colour as a res…
Browse files Browse the repository at this point in the history
…ult of the tests.
  • Loading branch information
matty-r committed Feb 6, 2024
1 parent 9d9c28c commit a295547
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/urChatBasic/base/IRCChannelBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/urChatBasic/frontend/LineFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/urChatBasic/frontend/UserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand Down
25 changes: 15 additions & 10 deletions src/urChatBasic/frontend/components/FontPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

Expand All @@ -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]);
}
}

Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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 ()
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -336,7 +341,7 @@ public void setSettingsPath (Preferences settingsPath)
this.settingsPath = settingsPath;
loadStyle();

colourDialog.getColourPanel().setSettingsPath(settingsPath);
getColourPanel().setSettingsPath(settingsPath);
}

public Preferences getSettingsPath ()
Expand Down
4 changes: 2 additions & 2 deletions src/urChatBasic/frontend/dialogs/FontDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
82 changes: 78 additions & 4 deletions tests/frontend/AppearanceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import urChatBasic.frontend.UserGUI;
import urChatBasic.frontend.utils.URColour;
import utils.TestDriverGUI;
import utils.TestUtils;

public class AppearanceTests
{
Expand Down Expand Up @@ -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();
Expand All @@ -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("<someuser> 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);
Expand All @@ -129,19 +201,21 @@ 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();
assertEquals("Channel 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("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);
}
}
}
16 changes: 16 additions & 0 deletions tests/utils/TestUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a295547

Please sign in to comment.