1 19 20 package org.netbeans.modules.editor.settings.storage; 21 22 import java.awt.Color ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import javax.swing.KeyStroke ; 30 import javax.swing.text.AttributeSet ; 31 import javax.swing.text.SimpleAttributeSet ; 32 import javax.swing.text.StyleConstants ; 33 import org.netbeans.api.editor.mimelookup.MimeLookup; 34 import org.netbeans.api.editor.mimelookup.MimePath; 35 import org.netbeans.api.editor.settings.EditorStyleConstants; 36 import org.netbeans.api.editor.settings.FontColorSettings; 37 import org.netbeans.api.editor.settings.KeyBindingSettings; 38 import org.netbeans.api.editor.settings.MultiKeyBinding; 39 import org.netbeans.core.startup.Main; 40 import org.netbeans.junit.NbTestCase; 41 import org.netbeans.modules.editor.settings.storage.api.FontColorSettingsFactory; 42 import org.netbeans.modules.editor.settings.storage.api.KeyBindingSettingsFactory; 43 import org.openide.util.Lookup; 44 import org.openide.util.LookupEvent; 45 import org.openide.util.LookupListener; 46 import org.openide.util.Utilities; 47 48 52 public class EditorSettingsStorageTest extends NbTestCase { 53 54 public EditorSettingsStorageTest(String testName) { 55 super(testName); 56 } 57 58 protected void setUp() throws Exception { 59 super.setUp(); 60 61 EditorTestLookup.setLookup( 62 new URL [] { 63 getClass().getClassLoader().getResource( 64 "org/netbeans/modules/editor/settings/storage/test-layer.xml"), 65 getClass().getClassLoader().getResource( 66 "org/netbeans/modules/editor/settings/storage/layer.xml"), 67 }, 68 getWorkDir(), 69 new Object [] {}, 70 getClass().getClassLoader() 71 ); 72 73 Main.initializeURLFactory(); 77 } 78 79 public void testBgColor() { 80 checkSingleAttribute("test-bgColor", StyleConstants.Background, 0xAA0000); 81 } 82 83 public void testForeColor() { 84 checkSingleAttribute("test-foreColor", StyleConstants.Foreground, 0x00BB00); 85 } 86 87 public void testUnderlineColor() { 88 checkSingleAttribute("test-underline", StyleConstants.Underline, 0x0000CC); 89 } 90 91 public void testStrikeThroughColor() { 92 checkSingleAttribute("test-strikeThrough", StyleConstants.StrikeThrough, 0xDD0000); 93 } 94 95 public void testWaveUnderlinedColor() { 96 checkSingleAttribute("test-waveUnderlined", EditorStyleConstants.WaveUnderlineColor, 0x00EE00); 97 } 98 99 public void testAllColors() { 100 MimePath mimePath = MimePath.parse("text/x-type-A"); 101 Lookup lookup = MimeLookup.getLookup(mimePath); 102 103 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 104 assertNotNull("Can't find FontColorSettings", fcs); 105 106 AttributeSet attribs = fcs.getTokenFontColors("test-all"); 107 assertNotNull("Can't find test-all coloring", attribs); 108 assertEquals("Wrong background color", 109 new Color (0x0A0B0C), attribs.getAttribute(StyleConstants.Background)); 110 assertEquals("Wrong foreground color", 111 new Color (0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground)); 112 assertEquals("Wrong underline color", 113 new Color (0x010203), attribs.getAttribute(StyleConstants.Underline)); 114 assertEquals("Wrong strikeThrough color", 115 new Color (0x040506), attribs.getAttribute(StyleConstants.StrikeThrough)); 116 assertEquals("Wrong waveUnderline color", 117 new Color (0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor)); 118 } 119 120 public void testFont() { 121 MimePath mimePath = MimePath.parse("text/x-type-A"); 122 Lookup lookup = MimeLookup.getLookup(mimePath); 123 124 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 125 assertNotNull("Can't find FontColorSettings", fcs); 126 127 { 128 AttributeSet attribs = fcs.getTokenFontColors("test-font-cute-A"); 129 assertNotNull("Can't find test-font-cute-A coloring", attribs); 130 assertEquals("Wrong font family", 131 "Test Font Cute A", attribs.getAttribute(StyleConstants.FontFamily)); 132 assertEquals("Wrong font size", 133 new Integer (17), attribs.getAttribute(StyleConstants.FontSize)); 134 assertEquals("Wrong font style (bold)", 135 Boolean.TRUE, attribs.getAttribute(StyleConstants.Bold)); 136 assertEquals("Wrong font style (italic)", 137 Boolean.FALSE, attribs.getAttribute(StyleConstants.Italic)); 138 } 139 { 140 AttributeSet attribs = fcs.getTokenFontColors("test-font-cute-B"); 141 assertNotNull("Can't find test-font-cute-B coloring", attribs); 142 assertEquals("Wrong font family", 143 "Test Font Cute B", attribs.getAttribute(StyleConstants.FontFamily)); 144 assertEquals("Wrong font size", 145 new Integer (13), attribs.getAttribute(StyleConstants.FontSize)); 146 assertEquals("Wrong font style (bold)", 147 Boolean.FALSE, attribs.getAttribute(StyleConstants.Bold)); 148 assertEquals("Wrong font style (italic)", 149 Boolean.TRUE, attribs.getAttribute(StyleConstants.Italic)); 150 } 151 } 152 153 private void checkSingleAttribute(String coloringName, Object attributeKey, int rgb) { 154 MimePath mimePath = MimePath.parse("text/x-type-A"); 155 Lookup lookup = MimeLookup.getLookup(mimePath); 156 157 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 158 assertNotNull("Can't find FontColorSettings", fcs); 159 160 AttributeSet attribs = fcs.getTokenFontColors(coloringName); 161 assertNotNull("Can't find " + coloringName + " coloring", attribs); 162 assertEquals("Wrong bgColor", new Color (rgb), attribs.getAttribute(attributeKey)); 163 } 164 165 public void testSetColors() { 166 MimePath mimePath = MimePath.parse("text/x-type-A"); 167 Lookup lookup = MimeLookup.getLookup(mimePath); 168 169 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 170 assertNotNull("Can't find FontColorSettings", fcs); 171 172 AttributeSet attribs = fcs.getTokenFontColors("test-set-all"); 174 assertNotNull("Can't find test-set-all coloring", attribs); 175 assertEquals("Wrong background color", 176 new Color (0x0A0B0C), attribs.getAttribute(StyleConstants.Background)); 177 assertEquals("Wrong foreground color", 178 new Color (0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground)); 179 assertEquals("Wrong underline color", 180 new Color (0x010203), attribs.getAttribute(StyleConstants.Underline)); 181 assertEquals("Wrong strikeThrough color", 182 new Color (0x040506), attribs.getAttribute(StyleConstants.StrikeThrough)); 183 assertEquals("Wrong waveUnderline color", 184 new Color (0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor)); 185 186 SimpleAttributeSet newAttribs = new SimpleAttributeSet (); 188 newAttribs.addAttribute(StyleConstants.NameAttribute, "test-set-all"); 189 newAttribs.addAttribute(StyleConstants.Background, new Color (0xFFFFF0)); 190 newAttribs.addAttribute(StyleConstants.Foreground, new Color (0xFFFFF1)); 191 newAttribs.addAttribute(StyleConstants.Underline, new Color (0xFFFFF2)); 192 newAttribs.addAttribute(StyleConstants.StrikeThrough, new Color (0xFFFFF3)); 193 newAttribs.addAttribute(EditorStyleConstants.WaveUnderlineColor, new Color (0xFFFFF4)); 194 195 setOneColoring("text/x-type-A", newAttribs); 197 198 fcs = lookup.lookup(FontColorSettings.class); 200 assertNotNull("Can't find FontColorSettings", fcs); 201 attribs = fcs.getTokenFontColors("test-set-all"); 202 assertNotNull("Can't find test-set-all coloring", attribs); 203 assertEquals("Wrong background color", 204 new Color (0xFFFFF0), attribs.getAttribute(StyleConstants.Background)); 205 assertEquals("Wrong foreground color", 206 new Color (0xFFFFF1), attribs.getAttribute(StyleConstants.Foreground)); 207 assertEquals("Wrong underline color", 208 new Color (0xFFFFF2), attribs.getAttribute(StyleConstants.Underline)); 209 assertEquals("Wrong strikeThrough color", 210 new Color (0xFFFFF3), attribs.getAttribute(StyleConstants.StrikeThrough)); 211 assertEquals("Wrong waveUnderline color", 212 new Color (0xFFFFF4), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor)); 213 } 214 215 public void testEvents() { 216 MimePath mimePath = MimePath.parse("text/x-type-A"); 217 Lookup lookup = MimeLookup.getLookup(mimePath); 218 Lookup.Result<FontColorSettings> result = lookup.lookup(new Lookup.Template<FontColorSettings>(FontColorSettings.class)); 219 Listener listener = new Listener(); 220 221 result.addLookupListener(listener); 222 223 Collection <? extends FontColorSettings> instances = result.allInstances(); 224 assertEquals("Wrong number of FontColorSettings instances", 1, instances.size()); 225 226 FontColorSettings fcs = instances.iterator().next(); 227 assertNotNull("Can't find FontColorSettings", fcs); 228 229 AttributeSet attribs = fcs.getTokenFontColors("test-events"); 231 assertNotNull("Can't find test-events coloring", attribs); 232 assertEquals("Wrong background color", 233 new Color (0x0A0B0C), attribs.getAttribute(StyleConstants.Background)); 234 assertEquals("There should be no events yet", 0, listener.eventsCnt); 235 236 SimpleAttributeSet newAttribs = new SimpleAttributeSet (); 238 newAttribs.addAttribute(StyleConstants.NameAttribute, "test-events"); 239 newAttribs.addAttribute(StyleConstants.Background, new Color (0xFFFFF0)); 240 241 setOneColoring("text/x-type-A", newAttribs); 243 244 assertEquals("Wrong number of events", 1, listener.eventsCnt); 246 247 fcs = lookup.lookup(FontColorSettings.class); 249 assertNotNull("Can't find FontColorSettings", fcs); 250 attribs = fcs.getTokenFontColors("test-events"); 251 assertNotNull("Can't find test-events coloring", attribs); 252 assertEquals("Wrong background color", 253 new Color (0xFFFFF0), attribs.getAttribute(StyleConstants.Background)); 254 } 255 256 public void testColoringsImmutability() { 257 MimePath mimePath = MimePath.parse("text/x-type-A"); 258 Lookup lookup = MimeLookup.getLookup(mimePath); 259 260 FontColorSettings fcs = lookup.lookup(FontColorSettings.class); 261 assertNotNull("Can't find FontColorSettings", fcs); 262 263 AttributeSet attribs = fcs.getTokenFontColors("test-immutable"); 265 assertNotNull("Can't find test-immutable coloring", attribs); 266 assertEquals("Wrong background color", 267 new Color (0x0A0B0C), attribs.getAttribute(StyleConstants.Background)); 268 269 SimpleAttributeSet newAttribs = new SimpleAttributeSet (); 271 newAttribs.addAttribute(StyleConstants.NameAttribute, "test-immutable"); 272 newAttribs.addAttribute(StyleConstants.Background, new Color (0xFFFFF0)); 273 274 setOneColoring("text/x-type-A", newAttribs); 276 277 attribs = fcs.getTokenFontColors("test-immutable"); 279 assertNotNull("Can't find test-immutable coloring", attribs); 280 assertEquals("Wrong background color", 281 new Color (0x0A0B0C), attribs.getAttribute(StyleConstants.Background)); 282 283 fcs = lookup.lookup(FontColorSettings.class); 285 assertNotNull("Can't find FontColorSettings", fcs); 286 attribs = fcs.getTokenFontColors("test-immutable"); 287 assertNotNull("Can't find test-immutable coloring", attribs); 288 assertEquals("Wrong background color", 289 new Color (0xFFFFF0), attribs.getAttribute(StyleConstants.Background)); 290 } 291 292 public void testKeyBindingsImmutability() { 293 MimePath mimePath = MimePath.parse("text/x-type-A"); 294 Lookup lookup = MimeLookup.getLookup(mimePath); 295 296 KeyBindingSettings kbs = lookup.lookup(KeyBindingSettings.class); 297 assertNotNull("Can't find KeyBindingSettings", kbs); 298 299 List <MultiKeyBinding> bindings = kbs.getKeyBindings(); 301 assertNotNull("Key bindings should not be null", bindings); 302 MultiKeyBinding kb = findBindingForAction("test-action-1", bindings); 303 checkKeyBinding(kb, "O-O"); 304 305 MultiKeyBinding newKb = new MultiKeyBinding(Utils.stringToKeyStrokes("DS-D"), "test-action-1"); 307 setOneKeyBinding("text/x-type-A", newKb); 308 309 bindings = kbs.getKeyBindings(); 311 assertNotNull("Key bindings should not be null", bindings); 312 kb = findBindingForAction("test-action-1", bindings); 313 checkKeyBinding(kb, "O-O"); 314 315 kbs = lookup.lookup(KeyBindingSettings.class); 317 assertNotNull("Can't find KeyBindingSettings", kbs); 318 bindings = kbs.getKeyBindings(); 319 assertNotNull("Key bindings should not be null", bindings); 320 kb = findBindingForAction("test-action-1", bindings); 321 checkKeyBinding(kb, "DS-D"); 322 } 323 324 private void setOneColoring(String mimeType, AttributeSet coloring) { 325 String coloringName = (String ) coloring.getAttribute(StyleConstants.NameAttribute); 326 FontColorSettingsFactory fcsf = EditorSettingsImpl.getDefault().getFontColorSettings(new String [] { mimeType }); 327 Collection <AttributeSet > all = new ArrayList <AttributeSet >(fcsf.getAllFontColors(EditorSettingsImpl.DEFAULT_PROFILE)); 328 329 for(Iterator <AttributeSet > i = all.iterator(); i.hasNext(); ) { 330 AttributeSet attribs = (AttributeSet ) i.next(); 331 String name = (String ) attribs.getAttribute(StyleConstants.NameAttribute); 332 if (Utilities.compareObjects(name, coloringName)) { 333 i.remove(); 334 break; 335 } 336 } 337 338 all.add(coloring); 339 fcsf.setAllFontColors(EditorSettingsImpl.DEFAULT_PROFILE, all); 340 } 341 342 private void setOneKeyBinding(String mimeType, MultiKeyBinding keyBinding) { 343 KeyBindingSettingsFactory kbsf = EditorSettingsImpl.getDefault().getKeyBindingSettings(new String [] { mimeType }); 344 List <MultiKeyBinding> all = new ArrayList <MultiKeyBinding>(kbsf.getKeyBindingDefaults(EditorSettingsImpl.DEFAULT_PROFILE)); 345 346 for(Iterator <MultiKeyBinding> i = all.iterator(); i.hasNext(); ) { 347 MultiKeyBinding kb = (MultiKeyBinding) i.next(); 348 if (Utilities.compareObjects(kb.getActionName(), keyBinding.getActionName())) { 349 i.remove(); 350 break; 351 } 352 } 353 354 all.add(keyBinding); 355 kbsf.setKeyBindings(EditorSettingsImpl.DEFAULT_PROFILE, all); 356 } 357 358 private void checkKeyBinding(MultiKeyBinding kb, String ... keyStrokes) { 359 assertNotNull("Key binding should not be null", kb); 360 361 ArrayList <KeyStroke > list = new ArrayList <KeyStroke >(); 362 for(String s : keyStrokes) { 363 KeyStroke [] strokes = Utils.stringToKeyStrokes(s); 364 if (strokes != null && strokes.length > 0) { 365 list.addAll(Arrays.asList(strokes)); 366 } 367 } 368 369 assertEquals("Wrong number of key strokes", list.size(), kb.getKeyStrokeCount()); 370 for(int i = 0; i < list.size(); i++) { 371 assertEquals("KeyStroke[" + i + "] is different", 372 list.get(i), kb.getKeyStroke(i)); 373 } 374 } 375 376 private MultiKeyBinding findBindingForAction(String actionName, List <MultiKeyBinding> list){ 377 for (MultiKeyBinding kb : list){ 378 if (actionName.equals(kb.getActionName())) { 379 return kb; 380 } 381 } 382 return null; 383 } 384 385 private static final class Listener implements LookupListener { 386 387 public int eventsCnt = 0; 388 389 public Listener() { 390 391 } 392 393 public void resultChanged(LookupEvent ev) { 394 eventsCnt++; 395 } 396 } } 398 | Popular Tags |