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.2KB

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