KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > settings > storage > EditorSettingsStorageTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.settings.storage;
21
22 import java.awt.Color JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.KeyStroke JavaDoc;
30 import javax.swing.text.AttributeSet JavaDoc;
31 import javax.swing.text.SimpleAttributeSet JavaDoc;
32 import javax.swing.text.StyleConstants JavaDoc;
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 /** Testing basic functionality of Editor Settings Storage friend API
49  *
50  * @author Martin Roskanin
51  */

52 public class EditorSettingsStorageTest extends NbTestCase {
53
54     public EditorSettingsStorageTest(String JavaDoc testName) {
55         super(testName);
56     }
57     
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60     
61         EditorTestLookup.setLookup(
62             new URL JavaDoc[] {
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 JavaDoc[] {},
70             getClass().getClassLoader()
71         );
72
73         // This is here to initialize Nb URL factory (org.netbeans.core.startup),
74
// which is needed by Nb EntityCatalog (org.netbeans.core).
75
// Also see the test dependencies in project.xml
76
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 JavaDoc attribs = fcs.getTokenFontColors("test-all");
107         assertNotNull("Can't find test-all coloring", attribs);
108         assertEquals("Wrong background color",
109             new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
110         assertEquals("Wrong foreground color",
111             new Color JavaDoc(0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground));
112         assertEquals("Wrong underline color",
113             new Color JavaDoc(0x010203), attribs.getAttribute(StyleConstants.Underline));
114         assertEquals("Wrong strikeThrough color",
115             new Color JavaDoc(0x040506), attribs.getAttribute(StyleConstants.StrikeThrough));
116         assertEquals("Wrong waveUnderline color",
117             new Color JavaDoc(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 JavaDoc 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 JavaDoc(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 JavaDoc 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 JavaDoc(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 JavaDoc coloringName, Object JavaDoc 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 JavaDoc attribs = fcs.getTokenFontColors(coloringName);
161         assertNotNull("Can't find " + coloringName + " coloring", attribs);
162         assertEquals("Wrong bgColor", new Color JavaDoc(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         // Check preconditions
173
AttributeSet JavaDoc attribs = fcs.getTokenFontColors("test-set-all");
174         assertNotNull("Can't find test-set-all coloring", attribs);
175         assertEquals("Wrong background color",
176             new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
177         assertEquals("Wrong foreground color",
178             new Color JavaDoc(0x0D0E0F), attribs.getAttribute(StyleConstants.Foreground));
179         assertEquals("Wrong underline color",
180             new Color JavaDoc(0x010203), attribs.getAttribute(StyleConstants.Underline));
181         assertEquals("Wrong strikeThrough color",
182             new Color JavaDoc(0x040506), attribs.getAttribute(StyleConstants.StrikeThrough));
183         assertEquals("Wrong waveUnderline color",
184             new Color JavaDoc(0x070809), attribs.getAttribute(EditorStyleConstants.WaveUnderlineColor));
185         
186         // Prepare new coloring
187
SimpleAttributeSet JavaDoc newAttribs = new SimpleAttributeSet JavaDoc();
188         newAttribs.addAttribute(StyleConstants.NameAttribute, "test-set-all");
189         newAttribs.addAttribute(StyleConstants.Background, new Color JavaDoc(0xFFFFF0));
190         newAttribs.addAttribute(StyleConstants.Foreground, new Color JavaDoc(0xFFFFF1));
191         newAttribs.addAttribute(StyleConstants.Underline, new Color JavaDoc(0xFFFFF2));
192         newAttribs.addAttribute(StyleConstants.StrikeThrough, new Color JavaDoc(0xFFFFF3));
193         newAttribs.addAttribute(EditorStyleConstants.WaveUnderlineColor, new Color JavaDoc(0xFFFFF4));
194         
195         // Change the coloring
196
setOneColoring("text/x-type-A", newAttribs);
197         
198         // Check that the new attributes were set
199
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 JavaDoc(0xFFFFF0), attribs.getAttribute(StyleConstants.Background));
205         assertEquals("Wrong foreground color",
206             new Color JavaDoc(0xFFFFF1), attribs.getAttribute(StyleConstants.Foreground));
207         assertEquals("Wrong underline color",
208             new Color JavaDoc(0xFFFFF2), attribs.getAttribute(StyleConstants.Underline));
209         assertEquals("Wrong strikeThrough color",
210             new Color JavaDoc(0xFFFFF3), attribs.getAttribute(StyleConstants.StrikeThrough));
211         assertEquals("Wrong waveUnderline color",
212             new Color JavaDoc(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 JavaDoc<? 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         // Check preconditions
230
AttributeSet JavaDoc attribs = fcs.getTokenFontColors("test-events");
231         assertNotNull("Can't find test-events coloring", attribs);
232         assertEquals("Wrong background color",
233             new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
234         assertEquals("There should be no events yet", 0, listener.eventsCnt);
235         
236         // Prepare new coloring
237
SimpleAttributeSet JavaDoc newAttribs = new SimpleAttributeSet JavaDoc();
238         newAttribs.addAttribute(StyleConstants.NameAttribute, "test-events");
239         newAttribs.addAttribute(StyleConstants.Background, new Color JavaDoc(0xFFFFF0));
240
241         // Change the coloring
242
setOneColoring("text/x-type-A", newAttribs);
243
244         // Check that the event was fired
245
assertEquals("Wrong number of events", 1, listener.eventsCnt);
246         
247         // Check that the new attributes were set
248
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 JavaDoc(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         // Check preconditions
264
AttributeSet JavaDoc attribs = fcs.getTokenFontColors("test-immutable");
265         assertNotNull("Can't find test-immutable coloring", attribs);
266         assertEquals("Wrong background color",
267             new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
268         
269         // Prepare new coloring
270
SimpleAttributeSet JavaDoc newAttribs = new SimpleAttributeSet JavaDoc();
271         newAttribs.addAttribute(StyleConstants.NameAttribute, "test-immutable");
272         newAttribs.addAttribute(StyleConstants.Background, new Color JavaDoc(0xFFFFF0));
273
274         // Change the coloring
275
setOneColoring("text/x-type-A", newAttribs);
276
277         // Check that the original FontColorSettings has not changed
278
attribs = fcs.getTokenFontColors("test-immutable");
279         assertNotNull("Can't find test-immutable coloring", attribs);
280         assertEquals("Wrong background color",
281             new Color JavaDoc(0x0A0B0C), attribs.getAttribute(StyleConstants.Background));
282         
283         // Check that the new attributes were set
284
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 JavaDoc(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         // Check preconditions
300
List JavaDoc<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         // Change the coloring
306
MultiKeyBinding newKb = new MultiKeyBinding(Utils.stringToKeyStrokes("DS-D"), "test-action-1");
307         setOneKeyBinding("text/x-type-A", newKb);
308
309         // Check that the original KeyBindingSettings has not changed
310
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         // Check that the new attributes were set
316
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 JavaDoc mimeType, AttributeSet JavaDoc coloring) {
325         String JavaDoc coloringName = (String JavaDoc) coloring.getAttribute(StyleConstants.NameAttribute);
326         FontColorSettingsFactory fcsf = EditorSettingsImpl.getDefault().getFontColorSettings(new String JavaDoc [] { mimeType });
327         Collection JavaDoc<AttributeSet JavaDoc> all = new ArrayList JavaDoc<AttributeSet JavaDoc>(fcsf.getAllFontColors(EditorSettingsImpl.DEFAULT_PROFILE));
328         
329         for(Iterator JavaDoc<AttributeSet JavaDoc> i = all.iterator(); i.hasNext(); ) {
330             AttributeSet JavaDoc attribs = (AttributeSet JavaDoc) i.next();
331             String JavaDoc name = (String JavaDoc) 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 JavaDoc mimeType, MultiKeyBinding keyBinding) {
343         KeyBindingSettingsFactory kbsf = EditorSettingsImpl.getDefault().getKeyBindingSettings(new String JavaDoc [] { mimeType });
344         List JavaDoc<MultiKeyBinding> all = new ArrayList JavaDoc<MultiKeyBinding>(kbsf.getKeyBindingDefaults(EditorSettingsImpl.DEFAULT_PROFILE));
345
346         for(Iterator JavaDoc<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 JavaDoc... keyStrokes) {
359         assertNotNull("Key binding should not be null", kb);
360         
361         ArrayList JavaDoc<KeyStroke JavaDoc> list = new ArrayList JavaDoc<KeyStroke JavaDoc>();
362         for(String JavaDoc s : keyStrokes) {
363             KeyStroke JavaDoc [] 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 JavaDoc actionName, List JavaDoc<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     } // End of Listener class
397
}
398
Popular Tags