123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- /*
- * Copyright (c) 2006-2013 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.TestMain;
- import com.dmdirc.FrameContainer;
- import com.dmdirc.config.ConfigManager;
- import com.dmdirc.config.InvalidIdentityFileException;
- import com.dmdirc.ui.core.util.Utils;
-
- 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.DefaultStyledDocument;
-
- import org.junit.BeforeClass;
- import org.junit.Ignore;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized;
-
- import static org.junit.Assert.*;
- import static org.mockito.Mockito.*;
-
- @RunWith(Parameterized.class)
- @Ignore("Doesn't work in a headless environment (initialises an IRCDocument)")
- public class StyliserStylesTest {
-
- protected String input, output;
-
- public StyliserStylesTest(String input, String output) {
- this.input = input;
- this.output = output;
- }
-
- @BeforeClass
- public static void setUp() throws InvalidIdentityFileException {
- TestMain.getTestMain();
- }
-
- @Test
- public void testStyle() {
- assertEquals(output, style(input));
- }
-
- protected static String style(final String input) {
- final DefaultStyledDocument doc = new DefaultStyledDocument();
- final StringBuilder builder = new StringBuilder();
-
- final FrameContainer container = mock(FrameContainer.class);
- final ConfigManager manager = mock(ConfigManager.class);
- when(container.getConfigManager()).thenReturn(manager);
-
- final Styliser styliser = new Styliser(container);
- styliser.addStyledString(doc, new String[]{input});
- final AttributedCharacterIterator aci = Utils.getAttributedString(styliser,
- new String[]{input, }, "dialog", 12).
- getAttributedString().getIterator();
-
- Map<AttributedCharacterIterator.Attribute, Object> 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<AttributedCharacterIterator.Attribute, Object> map,
- final StringBuilder builder) {
- builder.append('<');
-
- String[] entries = new String[9];
-
- for (Map.Entry<AttributedCharacterIterator.Attribute, Object> 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<String[]> data() {
- final String[][] tests = {
- // No style
- {"Blah blah blah", "<>Blah blah blah"},
- // Bold
- {"Blahblah", "<>Blah<bold>blah"},
- {"Blahblah", "<>Blah<bold>b<>lah"},
- {"Blahblah", "<>Blah<bold>b<>lah"},
- // Bold + Underline
- {"Blahblah", "<>B<underline>lah<bold,underline>b<underline>lah"},
- {"Blahblah", "<>B<underline>lahb<bold>lah"},
- {"Blahblah", "<>B<underline>lahb<bold>l<>ah"},
- {"Blahblah", "<>B<underline>l<>ahb<bold,underline>l<>ah"},
- // IRC colours
- {"4moo", "<color=255,0,0>moo"},
- {"4moo", "<color=255,0,0>m<>oo"},
- {"4moo", "<color=255,0,0>m<>oo"},
- {"20moo", "<color=255,0,0>m<>oo"}, // Colours wrap around
- {"4,4moo", "<color=255,0,0,background=255,0,0>m<>oo"},
- {"4,4m4,4oo", "<color=255,0,0,background=255,0,0>moo"},
- // Persistant irc colours
- {"4m0oo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
- {"4moo", "<color=255,0,0>moo"},
- {"4,0moo", "<color=255,0,0,background=255,255,255>moo"},
- // Hex colours
- {"FF0000moo", "<color=255,0,0>moo"},
- {"FF0000moo", "<color=255,0,0>m<>oo"},
- {"FF0000moo", "<color=255,0,0>m<>oo"},
- {"QUXmoo", "<>QUXmoo"},
- {"FFFFFQUXmoo", "<>FFFFFQUXmoo"},
- {"FF0000,FF0000moo", "<color=255,0,0,background=255,0,0>m<>oo"},
- {"xFF0000", "<>x"}, // Issue 3248
- // Persistant hex colours
- {"FF0000mFFFFFFoo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
- {"FF0000moo", "<color=255,0,0>moo"},
- {"FF0000,FFFFFFmoo", "<color=255,0,0,background=255,255,255>moo"},
- // Fixed width
- {"Blahblah", "<>Blah<monospace>b<>lah"},
- {"Blahblah", "<>Blah<monospace>b<>lah"}, // Issue 1413
- // Italics
- {"Blahblah", "<>Blah<italic>b<>lah"},
- {"Blahblah", "<>Blah<italic>b<>lah"},
- // Nesting
- {"Blahblahblahblah", "<>Blah<italic>b<bold,italic>lahblah<bold>b<>lah"},
- // Negation
- {"\u0012Blah4FF0000moo", "<>Blahmoo"},
- {"\u0012Blah4FF0000moo\u0012foo", "<>Blahmoo<bold>foo"},
- {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Stop
- {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty colour
- {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty hex colour
- };
-
- return Arrays.asList(tests);
- }
-
- }
|