You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StyliserStylesTest.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.messages;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.ui.core.util.Utils;
  26. import java.awt.Color;
  27. import java.awt.font.TextAttribute;
  28. import java.text.AttributedCharacterIterator;
  29. import java.util.Arrays;
  30. import java.util.List;
  31. import java.util.Map;
  32. import javax.swing.text.DefaultStyledDocument;
  33. import org.junit.Ignore;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.junit.runners.Parameterized;
  37. import static org.junit.Assert.*;
  38. import static org.mockito.Mockito.*;
  39. @RunWith(Parameterized.class)
  40. @Ignore("Doesn't work in a headless environment (initialises an IRCDocument)")
  41. public class StyliserStylesTest {
  42. protected String input, output;
  43. public StyliserStylesTest(final String input, final String output) {
  44. this.input = input;
  45. this.output = output;
  46. }
  47. @Test
  48. public void testStyle() {
  49. assertEquals(output, style(input));
  50. }
  51. protected static String style(final String input) {
  52. final DefaultStyledDocument doc = new DefaultStyledDocument();
  53. final StringBuilder builder = new StringBuilder();
  54. final AggregateConfigProvider manager = mock(AggregateConfigProvider.class);
  55. final Styliser styliser = new Styliser(null, manager, new ColourManager(manager,
  56. mock(DMDircMBassador.class)));
  57. styliser.addStyledString(doc, new String[]{input});
  58. final AttributedCharacterIterator aci = Utils.getAttributedString(styliser,
  59. new String[]{input, }, "dialog", 12).
  60. getAttributedString().getIterator();
  61. Map<AttributedCharacterIterator.Attribute, Object> map = null;
  62. char chr = aci.current();
  63. while (aci.getIndex() < aci.getEndIndex()) {
  64. if (!aci.getAttributes().equals(map)) {
  65. style(aci.getAttributes(), builder);
  66. map = aci.getAttributes();
  67. }
  68. builder.append(chr);
  69. chr = aci.next();
  70. }
  71. return builder.toString();
  72. }
  73. protected static void style(final Map<AttributedCharacterIterator.Attribute, Object> map,
  74. final StringBuilder builder) {
  75. builder.append('<');
  76. final String[] entries = new String[9];
  77. for (Map.Entry<AttributedCharacterIterator.Attribute, Object> entry : map.entrySet()) {
  78. if (entry.getKey().equals(TextAttribute.FOREGROUND)) {
  79. entries[0] = "color=" + toColour(entry.getValue());
  80. } else if (entry.getKey().equals(TextAttribute.BACKGROUND)) {
  81. entries[1] = "background=" + toColour(entry.getValue());
  82. } else if (entry.getKey().equals(TextAttribute.WEIGHT)) {
  83. entries[2] = "bold";
  84. } else if (entry.getKey().equals(TextAttribute.FAMILY)
  85. && entry.getValue().equals("monospaced")) {
  86. entries[3] = "monospace";
  87. } else if (entry.getKey().equals(TextAttribute.POSTURE)) {
  88. entries[4] = "italic";
  89. } else if (entry.getKey().equals(TextAttribute.UNDERLINE)) {
  90. entries[5] = "underline";
  91. } else if (entry.getKey().equals(IRCTextAttribute.HYPERLINK)) {
  92. entries[6] = "hyperlink";
  93. } else if (entry.getKey().equals(IRCTextAttribute.CHANNEL)) {
  94. entries[7] = "channel";
  95. } else if (entry.getKey().equals(IRCTextAttribute.NICKNAME)) {
  96. entries[8] = "nickname";
  97. }
  98. }
  99. int count = 0;
  100. for (String entry : entries) {
  101. if (entry != null) {
  102. builder.append(entry);
  103. builder.append(',');
  104. count++;
  105. }
  106. }
  107. if (count > 0) {
  108. builder.deleteCharAt(builder.length() - 1);
  109. }
  110. builder.append('>');
  111. }
  112. protected static String toColour(final Object object) {
  113. final Color colour = (Color) object;
  114. return colour.getRed() + "," + colour.getGreen() + ','
  115. + colour.getBlue();
  116. }
  117. @Parameterized.Parameters
  118. public static List<String[]> data() {
  119. final String[][] tests = {
  120. // No style
  121. {"Blah blah blah", "<>Blah blah blah"},
  122. // Bold
  123. {"Blahblah", "<>Blah<bold>blah"},
  124. {"Blahblah", "<>Blah<bold>b<>lah"},
  125. {"Blahblah", "<>Blah<bold>b<>lah"},
  126. // Bold + Underline
  127. {"Blahblah", "<>B<underline>lah<bold,underline>b<underline>lah"},
  128. {"Blahblah", "<>B<underline>lahb<bold>lah"},
  129. {"Blahblah", "<>B<underline>lahb<bold>l<>ah"},
  130. {"Blahblah", "<>B<underline>l<>ahb<bold,underline>l<>ah"},
  131. // IRC colours
  132. {"4moo", "<color=255,0,0>moo"},
  133. {"4moo", "<color=255,0,0>m<>oo"},
  134. {"4moo", "<color=255,0,0>m<>oo"},
  135. {"20moo", "<color=255,0,0>m<>oo"}, // Colours wrap around
  136. {"4,4moo", "<color=255,0,0,background=255,0,0>m<>oo"},
  137. {"4,4m4,4oo", "<color=255,0,0,background=255,0,0>moo"},
  138. // Persistant irc colours
  139. {"4m0oo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
  140. {"4moo", "<color=255,0,0>moo"},
  141. {"4,0moo", "<color=255,0,0,background=255,255,255>moo"},
  142. // Hex colours
  143. {"FF0000moo", "<color=255,0,0>moo"},
  144. {"FF0000moo", "<color=255,0,0>m<>oo"},
  145. {"FF0000moo", "<color=255,0,0>m<>oo"},
  146. {"QUXmoo", "<>QUXmoo"},
  147. {"FFFFFQUXmoo", "<>FFFFFQUXmoo"},
  148. {"FF0000,FF0000moo", "<color=255,0,0,background=255,0,0>m<>oo"},
  149. {"xFF0000", "<>x"}, // Issue 3248
  150. // Persistant hex colours
  151. {"FF0000mFFFFFFoo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
  152. {"FF0000moo", "<color=255,0,0>moo"},
  153. {"FF0000,FFFFFFmoo", "<color=255,0,0,background=255,255,255>moo"},
  154. // Fixed width
  155. {"Blahblah", "<>Blah<monospace>b<>lah"},
  156. {"Blahblah", "<>Blah<monospace>b<>lah"}, // Issue 1413
  157. // Italics
  158. {"Blahblah", "<>Blah<italic>b<>lah"},
  159. {"Blahblah", "<>Blah<italic>b<>lah"},
  160. // Nesting
  161. {"Blahblahblahblah", "<>Blah<italic>b<bold,italic>lahblah<bold>b<>lah"},
  162. // Negation
  163. {"\u0012Blah4FF0000moo", "<>Blahmoo"},
  164. {"\u0012Blah4FF0000moo\u0012foo", "<>Blahmoo<bold>foo"},
  165. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Stop
  166. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty colour
  167. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty hex colour
  168. };
  169. return Arrays.asList(tests);
  170. }
  171. }