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

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