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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2006-2011 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.FrameContainer;
  24. import com.dmdirc.config.ConfigManager;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.config.InvalidIdentityFileException;
  27. import com.dmdirc.ui.core.util.Utils;
  28. import java.awt.Color;
  29. import java.awt.font.TextAttribute;
  30. import java.text.AttributedCharacterIterator;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. import java.util.Map;
  34. import javax.swing.text.DefaultStyledDocument;
  35. import org.junit.BeforeClass;
  36. import org.junit.Test;
  37. import org.junit.runner.RunWith;
  38. import org.junit.runners.Parameterized;
  39. import static org.junit.Assert.*;
  40. import static org.mockito.Mockito.*;
  41. @RunWith(Parameterized.class)
  42. public class StyliserStylesTest {
  43. protected String input, output;
  44. public StyliserStylesTest(String input, String output) {
  45. this.input = input;
  46. this.output = output;
  47. }
  48. @BeforeClass
  49. public static void setUp() throws InvalidIdentityFileException {
  50. IdentityManager.load();
  51. }
  52. @Test
  53. public void testStyle() {
  54. assertEquals(output, style(input));
  55. }
  56. protected static String style(final String input) {
  57. final DefaultStyledDocument doc = new DefaultStyledDocument();
  58. final StringBuilder builder = new StringBuilder();
  59. final FrameContainer container = mock(FrameContainer.class);
  60. final ConfigManager manager = mock(ConfigManager.class);
  61. when(container.getConfigManager()).thenReturn(manager);
  62. final Styliser styliser = new Styliser(container);
  63. styliser.addStyledString(doc, new String[]{input});
  64. final AttributedCharacterIterator aci = Utils.getAttributedString(styliser,
  65. new String[]{input, }, "dialog", 12).
  66. getAttributedString().getIterator();
  67. Map<AttributedCharacterIterator.Attribute, Object> map = null;
  68. char chr = aci.current();
  69. while (aci.getIndex() < aci.getEndIndex()) {
  70. if (!aci.getAttributes().equals(map)) {
  71. style(aci.getAttributes(), builder);
  72. map = aci.getAttributes();
  73. }
  74. builder.append(chr);
  75. chr = aci.next();
  76. }
  77. return builder.toString();
  78. }
  79. protected static void style(final Map<AttributedCharacterIterator.Attribute, Object> map,
  80. final StringBuilder builder) {
  81. builder.append('<');
  82. String[] entries = new String[9];
  83. for (Map.Entry<AttributedCharacterIterator.Attribute, Object> entry : map.entrySet()) {
  84. if (entry.getKey().equals(TextAttribute.FOREGROUND)) {
  85. entries[0] = "color=" + toColour(entry.getValue());
  86. } else if (entry.getKey().equals(TextAttribute.BACKGROUND)) {
  87. entries[1] = "background=" + toColour(entry.getValue());
  88. } else if (entry.getKey().equals(TextAttribute.WEIGHT)) {
  89. entries[2] = "bold";
  90. } else if (entry.getKey().equals(TextAttribute.FAMILY)
  91. && entry.getValue().equals("monospaced")) {
  92. entries[3] = "monospace";
  93. } else if (entry.getKey().equals(TextAttribute.POSTURE)) {
  94. entries[4] = "italic";
  95. } else if (entry.getKey().equals(TextAttribute.UNDERLINE)) {
  96. entries[5] = "underline";
  97. } else if (entry.getKey().equals(IRCTextAttribute.HYPERLINK)) {
  98. entries[6] = "hyperlink";
  99. } else if (entry.getKey().equals(IRCTextAttribute.CHANNEL)) {
  100. entries[7] = "channel";
  101. } else if (entry.getKey().equals(IRCTextAttribute.NICKNAME)) {
  102. entries[8] = "nickname";
  103. }
  104. }
  105. int count = 0;
  106. for (String entry : entries) {
  107. if (entry != null) {
  108. builder.append(entry);
  109. builder.append(',');
  110. count++;
  111. }
  112. }
  113. if (count > 0) {
  114. builder.deleteCharAt(builder.length() - 1);
  115. }
  116. builder.append('>');
  117. }
  118. protected static String toColour(final Object object) {
  119. final Color colour = (Color) object;
  120. return colour.getRed() + "," + colour.getGreen() + ","
  121. + colour.getBlue();
  122. }
  123. @Parameterized.Parameters
  124. public static List<String[]> data() {
  125. final String[][] tests = {
  126. // No style
  127. {"Blah blah blah", "<>Blah blah blah"},
  128. // Bold
  129. {"Blahblah", "<>Blah<bold>blah"},
  130. {"Blahblah", "<>Blah<bold>b<>lah"},
  131. {"Blahblah", "<>Blah<bold>b<>lah"},
  132. // Bold + Underline
  133. {"Blahblah", "<>B<underline>lah<bold,underline>b<underline>lah"},
  134. {"Blahblah", "<>B<underline>lahb<bold>lah"},
  135. {"Blahblah", "<>B<underline>lahb<bold>l<>ah"},
  136. {"Blahblah", "<>B<underline>l<>ahb<bold,underline>l<>ah"},
  137. // IRC colours
  138. {"4moo", "<color=255,0,0>moo"},
  139. {"4moo", "<color=255,0,0>m<>oo"},
  140. {"4moo", "<color=255,0,0>m<>oo"},
  141. {"20moo", "<color=255,0,0>m<>oo"}, // Colours wrap around
  142. {"4,4moo", "<color=255,0,0,background=255,0,0>m<>oo"},
  143. {"4,4m4,4oo", "<color=255,0,0,background=255,0,0>moo"},
  144. // Persistant irc colours
  145. {"4m0oo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
  146. {"4moo", "<color=255,0,0>moo"},
  147. {"4,0moo", "<color=255,0,0,background=255,255,255>moo"},
  148. // Hex colours
  149. {"FF0000moo", "<color=255,0,0>moo"},
  150. {"FF0000moo", "<color=255,0,0>m<>oo"},
  151. {"FF0000moo", "<color=255,0,0>m<>oo"},
  152. {"QUXmoo", "<>QUXmoo"},
  153. {"FFFFFQUXmoo", "<>FFFFFQUXmoo"},
  154. {"FF0000,FF0000moo", "<color=255,0,0,background=255,0,0>m<>oo"},
  155. {"xFF0000", "<>x"}, // Issue 3248
  156. // Persistant hex colours
  157. {"FF0000mFFFFFFoo", "<color=255,0,0>m<color=255,255,255>o<color=255,0,0>o"},
  158. {"FF0000moo", "<color=255,0,0>moo"},
  159. {"FF0000,FFFFFFmoo", "<color=255,0,0,background=255,255,255>moo"},
  160. // Fixed width
  161. {"Blahblah", "<>Blah<monospace>b<>lah"},
  162. {"Blahblah", "<>Blah<monospace>b<>lah"}, // Issue 1413
  163. // Italics
  164. {"Blahblah", "<>Blah<italic>b<>lah"},
  165. {"Blahblah", "<>Blah<italic>b<>lah"},
  166. // Nesting
  167. {"Blahblahblahblah", "<>Blah<italic>b<bold,italic>lahblah<bold>b<>lah"},
  168. // Negation
  169. {"\u0012Blah4FF0000moo", "<>Blahmoo"},
  170. {"\u0012Blah4FF0000moo\u0012foo", "<>Blahmoo<bold>foo"},
  171. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Stop
  172. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty colour
  173. {"Blah 4\u0012BlahBlah", "<>Blah <color=255,0,0>BlahBlah"}, // Empty hex colour
  174. };
  175. return Arrays.asList(tests);
  176. }
  177. }