/* * Copyright (c) 2006-2015 DMDirc Developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.dmdirc.ui.messages; import com.dmdirc.DMDircMBassador; import com.dmdirc.interfaces.EventBus; import com.dmdirc.interfaces.config.AggregateConfigProvider; import java.awt.Color; import java.awt.font.TextAttribute; import java.text.AttributedCharacterIterator; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultStyledDocument; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; @RunWith(Parameterized.class) @Ignore("Doesn't work in a headless environment (initialises an IRCDocument)") public class StyliserStylesTest { private EventBus eventBus; protected String input, output; public StyliserStylesTest(final String input, final String output) { this.input = input; this.output = output; eventBus = mock(DMDircMBassador.class); } @Test public void testStyle() throws BadLocationException { assertEquals(output, style(input, eventBus)); } protected static String style(final String input, final EventBus eventBus) throws BadLocationException{ final DefaultStyledDocument doc = new DefaultStyledDocument(); final StringBuilder builder = new StringBuilder(); final AggregateConfigProvider manager = mock(AggregateConfigProvider.class); final Styliser styliser = new Styliser(null, manager, new ColourManagerImpl(manager)); styliser.addStyledString(null, input); // TODO... final AttributedCharacterIterator aci = null; // TODO... Map map = null; char chr = aci.current(); while (aci.getIndex() < aci.getEndIndex()) { if (!aci.getAttributes().equals(map)) { style(aci.getAttributes(), builder); map = aci.getAttributes(); } builder.append(chr); chr = aci.next(); } return builder.toString(); } protected static void style(final Map map, final StringBuilder builder) { builder.append('<'); final String[] entries = new String[9]; for (Map.Entry entry : map.entrySet()) { if (entry.getKey().equals(TextAttribute.FOREGROUND)) { entries[0] = "color=" + toColour(entry.getValue()); } else if (entry.getKey().equals(TextAttribute.BACKGROUND)) { entries[1] = "background=" + toColour(entry.getValue()); } else if (entry.getKey().equals(TextAttribute.WEIGHT)) { entries[2] = "bold"; } else if (entry.getKey().equals(TextAttribute.FAMILY) && entry.getValue().equals("monospaced")) { entries[3] = "monospace"; } else if (entry.getKey().equals(TextAttribute.POSTURE)) { entries[4] = "italic"; } else if (entry.getKey().equals(TextAttribute.UNDERLINE)) { entries[5] = "underline"; } else if (entry.getKey().equals(IRCTextAttribute.HYPERLINK)) { entries[6] = "hyperlink"; } else if (entry.getKey().equals(IRCTextAttribute.CHANNEL)) { entries[7] = "channel"; } else if (entry.getKey().equals(IRCTextAttribute.NICKNAME)) { entries[8] = "nickname"; } } int count = 0; for (String entry : entries) { if (entry != null) { builder.append(entry); builder.append(','); count++; } } if (count > 0) { builder.deleteCharAt(builder.length() - 1); } builder.append('>'); } protected static String toColour(final Object object) { final Color colour = (Color) object; return colour.getRed() + "," + colour.getGreen() + ',' + colour.getBlue(); } @Parameterized.Parameters public static List data() { final String[][] tests = { // No style {"Blah blah blah", "<>Blah blah blah"}, // Bold {"Blahblah", "<>Blahblah"}, {"Blahblah", "<>Blahb<>lah"}, {"Blahblah", "<>Blahb<>lah"}, // Bold + Underline {"Blahblah", "<>Blahblah"}, {"Blahblah", "<>Blahblah"}, {"Blahblah", "<>Blahbl<>ah"}, {"Blahblah", "<>Bl<>ahbl<>ah"}, // IRC colours {"4moo", "moo"}, {"4moo", "m<>oo"}, {"4moo", "m<>oo"}, {"20moo", "m<>oo"}, // Colours wrap around {"4,4moo", "m<>oo"}, {"4,4m4,4oo", "moo"}, // Persistant irc colours {"4m0oo", "moo"}, {"4moo", "moo"}, {"4,0moo", "moo"}, // Hex colours {"FF0000moo", "moo"}, {"FF0000moo", "m<>oo"}, {"FF0000moo", "m<>oo"}, {"QUXmoo", "<>QUXmoo"}, {"FFFFFQUXmoo", "<>FFFFFQUXmoo"}, {"FF0000,FF0000moo", "m<>oo"}, {"xFF0000", "<>x"}, // Issue 3248 // Persistant hex colours {"FF0000mFFFFFFoo", "moo"}, {"FF0000moo", "moo"}, {"FF0000,FFFFFFmoo", "moo"}, // Fixed width {"Blahblah", "<>Blahb<>lah"}, {"Blahblah", "<>Blahb<>lah"}, // Issue 1413 // Italics {"Blahblah", "<>Blahb<>lah"}, {"Blahblah", "<>Blahb<>lah"}, // Nesting {"Blahblahblahblah", "<>Blahblahblahb<>lah"}, // Negation {"\u0012Blah4FF0000moo", "<>Blahmoo"}, {"\u0012Blah4FF0000moo\u0012foo", "<>Blahmoofoo"}, {"Blah 4\u0012BlahBlah", "<>Blah BlahBlah"}, // Stop {"Blah 4\u0012BlahBlah", "<>Blah BlahBlah"}, // Empty colour {"Blah 4\u0012BlahBlah", "<>Blah BlahBlah"}, // Empty hex colour }; return Arrays.asList(tests); } }