Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

StyliserStylesTest.java 8.0KB

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