KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > UtilitiesTest


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.openide.util;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.AbstractButton JavaDoc;
32 import javax.swing.Action JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
35 import javax.swing.JPopupMenu JavaDoc;
36 import javax.swing.KeyStroke JavaDoc;
37 import junit.framework.TestCase;
38 import org.netbeans.junit.MockServices;
39 import org.netbeans.modules.openide.util.AWTBridge;
40 import org.openide.util.actions.Presenter;
41 import org.openide.util.lookup.Lookups;
42
43 /**
44  * @author Jiri Rechtacek et al.
45  */

46 public class UtilitiesTest extends TestCase {
47
48     public UtilitiesTest (String JavaDoc testName) {
49         super (testName);
50     }
51     
52     private String JavaDoc originalOsName;
53
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         Utilities.resetOperatingSystem ();
57         originalOsName = System.getProperty("os.name");
58     }
59     
60     protected void tearDown() throws Exception JavaDoc {
61         System.setProperty("os.name", originalOsName);
62         super.tearDown();
63     }
64
65     public void testGetOperatingSystemWinNT () {
66         System.setProperty ("os.name", "Windows NT");
67         //assertEquals ("System.getProperty (os.name) returns Windows NT", "Windows NT", System.getProperty ("os.name"));
68
assertEquals ("Windows NT recognized as OS_WINNT", Utilities.OS_WINNT, Utilities.getOperatingSystem ());
69     }
70
71     public void testGetOperatingSystemFreebsd () {
72         System.setProperty ("os.name", "FreeBSD");
73         assertEquals ("System.getProperty (os.name) returns FreeBSD", "FreeBSD", System.getProperty ("os.name"));
74         assertEquals ("System.getProperty (os.name) returns freebsd", "freebsd", System.getProperty ("os.name").toLowerCase (Locale.US));
75         assertEquals ("FreeBSD recognized as OS_FREEBSD", Utilities.OS_FREEBSD, Utilities.getOperatingSystem ());
76     }
77
78     public void testGetOperatingSystemFreeBSDLowerCase () {
79         System.setProperty ("os.name", "freebsd");
80         assertEquals ("FreeBSD recognized as OS_FREEBSD", Utilities.OS_FREEBSD, Utilities.getOperatingSystem ());
81     }
82
83     public void testGetUnknownOperatingSystem () {
84         System.setProperty ("os.name", "Unknown");
85         assertEquals ("Windows NT recognized as Unknown", Utilities.OS_OTHER, Utilities.getOperatingSystem ());
86     }
87
88     public void testWhatIsWinXP () {
89         System.setProperty ("os.name", "Windows XP");
90         assertTrue ("Windows XP isWindows", Utilities.isWindows ());
91         assertFalse ("Windows XP not isUnix", Utilities.isUnix ());
92     }
93
94     public void testWhatIsLinux () {
95         System.setProperty ("os.name", "Linux");
96         assertFalse ("Linux not isWindows", Utilities.isWindows ());
97         assertTrue ("Linux isUnix", Utilities.isUnix ());
98     }
99
100     public void testWhatIsMac () {
101         System.setProperty ("os.name", "Mac OS X");
102         assertFalse ("Mac not isWindows", Utilities.isWindows ());
103         assertTrue ("Mac isMac", Utilities.isMac ());
104     }
105
106     public void testWhatIsFreeBSD () {
107         System.setProperty ("os.name", "freebsd");
108         assertFalse ("freebsd is not isWindows", Utilities.isWindows ());
109         assertTrue ("freebsd isUnix", Utilities.isUnix ());
110     }
111
112     // XXX sorry, but NoCustomCursorToolkit does not compile on JDK 6:
113
// org.openide.util.UtilitiesTest.NoCustomCursorToolkit is not abstract and does not override abstract method isModalExclusionTypeSupported(java.awt.Dialog.ModalExclusionType) in java.awt.Toolkit
114
// and since Toolkit is not an interface, we can't use java.lang.reflect.Proxy to solve the problem...
115
// Filed as #6313637.
116
/*
117     public void testCustomCursorNotSupported() {
118         NoCustomCursorToolkit toolkit = new NoCustomCursorToolkit();
119         CustomToolkitComponent c = new CustomToolkitComponent( toolkit );
120         Image icon = new BufferedImage( 16, 16, BufferedImage.TYPE_BYTE_BINARY );
121         Cursor cursor = Utilities.createCustomCursor( c, icon, "junittest" );
122         assertTrue( "fallback to wait cursor", Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR ).equals( cursor ) );
123         assertTrue( "getBestCursorSize was called", toolkit.getBestCursorSizeCalled );
124         assertFalse( "no custom cursor created", toolkit.createCustomCursorCalled );
125     }
126      */

127     
128     public void testSpecialKeyworksOn14AsWell15 () throws Exception JavaDoc {
129         KeyStroke JavaDoc ks = Utilities.stringToKey("C-CONTEXT_MENU");
130         assertNotNull ("key stroke created", ks);
131         KeyStroke JavaDoc alt = ks.getKeyStroke(ks.getKeyCode(), KeyEvent.ALT_MASK);
132         String JavaDoc s = Utilities.keyToString(alt);
133         assertEquals ("Correctly converted", "A-CONTEXT_MENU", s);
134     }
135     
136     public void testSpecialKeyworksOn14AsWell15WithoutModificators () throws Exception JavaDoc {
137         KeyStroke JavaDoc ks = Utilities.stringToKey("CONTEXT_MENU");
138         assertNotNull ("key stroke created", ks);
139         String JavaDoc s = Utilities.keyToString(ks);
140         assertEquals ("Correctly converted", "CONTEXT_MENU", s);
141     }
142     
143     public void testActionsToPopupWithLookup() throws Exception JavaDoc {
144         MockServices.setServices(AwtBridgeImpl.class);
145         final List JavaDoc<String JavaDoc> commands = new ArrayList JavaDoc<String JavaDoc>();
146         class BasicAction extends AbstractAction JavaDoc {
147             public BasicAction(String JavaDoc name) {
148                 super(name);
149             }
150             public void actionPerformed(ActionEvent JavaDoc e) {
151                 commands.add((String JavaDoc) getValue(Action.NAME));
152             }
153         }
154         class ContextAction extends BasicAction implements ContextAwareAction {
155             public ContextAction(String JavaDoc name) {
156                 super(name);
157             }
158             public Action JavaDoc createContextAwareInstance(final Lookup actionContext) {
159                 return new AbstractAction JavaDoc() {
160                     public void actionPerformed(ActionEvent JavaDoc e) {
161                         commands.add(ContextAction.this.getValue(Action.NAME) + "/" + actionContext.lookup(String JavaDoc.class));
162                     }
163                 };
164             }
165         }
166         class SpecialMenuAction extends BasicAction implements Presenter.Popup {
167             public SpecialMenuAction(String JavaDoc name) {
168                 super(name);
169             }
170             public JMenuItem JavaDoc getPopupPresenter() {
171                 JMenuItem JavaDoc item = new JMenuItem JavaDoc((String JavaDoc) getValue(Action.NAME));
172                 item.addActionListener(new ActionListener JavaDoc() {
173                     public void actionPerformed(ActionEvent JavaDoc e) {
174                         commands.add(((String JavaDoc) getValue(Action.NAME)) + "/popup");
175                     }
176                 });
177                 return item;
178             }
179         }
180         Action JavaDoc duplicated = new BasicAction("duplicated");
181         Action JavaDoc[] actions = new Action JavaDoc[] {
182             null,
183             null,
184             new BasicAction("first"),
185             duplicated,
186             null,
187             null,
188             new BasicAction("second"),
189             duplicated,
190             null,
191             new ContextAction("context"),
192             new SpecialMenuAction("presenter"),
193             null,
194             new BasicAction("top"),
195             new BasicAction("HIDDEN"),
196             null,
197             new BasicAction("bottom"),
198             null,
199             null,
200         };
201         Lookup l = Lookups.singleton("thing");
202         JPopupMenu JavaDoc menu = Utilities.actionsToPopup(actions, l);
203         for (Component JavaDoc element : menu.getComponents()) { // including separators
204
if (element instanceof AbstractButton JavaDoc) {
205                 ((AbstractButton JavaDoc) element).doClick();
206             } else {
207                 commands.add(null);
208             }
209         }
210         String JavaDoc[] expectedCommands = new String JavaDoc[] {
211             // leading separators must be stripped
212
"first",
213             "duplicated",
214             null, // adjacent separators must be collapsed
215
"second",
216             // do not add the same action twice
217
null,
218             "context/thing", // ContextAwareAction was checked for
219
"presenter/popup", // Presenter.Popup was checked for
220
null,
221             "top",
222             // exclude HIDDEN because of AwtBridgeImpl.convertComponents
223
// separator should however remain
224
null,
225             "bottom",
226             // trailing separators must be stripped
227
};
228         assertEquals("correct generated menu", Arrays.asList(expectedCommands), commands);
229     }
230
231     /*
232     private static class CustomToolkitComponent extends Component {
233         private Toolkit customToolkit;
234         
235         public CustomToolkitComponent( Toolkit t ) {
236             this.customToolkit = t;
237         }
238         
239         public Toolkit getToolkit() {
240             return customToolkit;
241         }
242     }
243     
244     private static class NoCustomCursorToolkit extends Toolkit {
245         public FontMetrics getFontMetrics(Font font) {
246             return Toolkit.getDefaultToolkit().getFontMetrics( font );
247         }
248
249         protected TextFieldPeer createTextField(TextField target) throws HeadlessException {
250             throw new IllegalStateException("Method not implemented");
251         }
252
253         protected ListPeer createList(java.awt.List target) throws HeadlessException {
254             throw new IllegalStateException("Method not implemented");
255         }
256
257         protected MenuBarPeer createMenuBar(MenuBar target) throws HeadlessException {
258             throw new IllegalStateException("Method not implemented");
259         }
260
261         public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent dge) throws InvalidDnDOperationException {
262             return Toolkit.getDefaultToolkit().createDragSourceContextPeer( dge );
263         }
264
265         public boolean prepareImage(Image image, int width, int height, ImageObserver observer) {
266             return Toolkit.getDefaultToolkit().prepareImage( image, width, height, observer );
267         }
268
269         public int checkImage(Image image, int width, int height, ImageObserver observer) {
270             return Toolkit.getDefaultToolkit().checkImage( image, width, height, observer );
271         }
272
273         protected PopupMenuPeer createPopupMenu(PopupMenu target) throws HeadlessException {
274             throw new IllegalStateException("Method not implemented");
275         }
276
277         public PrintJob getPrintJob(Frame frame, String jobtitle, Properties props) {
278             return Toolkit.getDefaultToolkit().getPrintJob( frame, jobtitle, props );
279         }
280
281         protected ButtonPeer createButton(Button target) throws HeadlessException {
282             throw new IllegalStateException("Method not implemented");
283         }
284
285         public Image createImage(ImageProducer producer) {
286             return Toolkit.getDefaultToolkit().createImage( producer );
287         }
288
289         protected CanvasPeer createCanvas(Canvas target) {
290             throw new IllegalStateException("Method not implemented");
291         }
292
293         protected ScrollbarPeer createScrollbar(Scrollbar target) throws HeadlessException {
294             throw new IllegalStateException("Method not implemented");
295         }
296
297         public Image getImage(String filename) {
298             return Toolkit.getDefaultToolkit().getImage( filename );
299         }
300
301         public Image createImage(String filename) {
302             return Toolkit.getDefaultToolkit().createImage( filename );
303         }
304
305         protected MenuPeer createMenu(Menu target) throws HeadlessException {
306             throw new IllegalStateException("Method not implemented");
307         }
308
309         protected MenuItemPeer createMenuItem(MenuItem target) throws HeadlessException {
310             throw new IllegalStateException("Method not implemented");
311         }
312
313         public Map mapInputMethodHighlight(InputMethodHighlight highlight) throws HeadlessException {
314             return Toolkit.getDefaultToolkit().mapInputMethodHighlight( highlight );
315         }
316
317         public Image createImage(byte[] imagedata, int imageoffset, int imagelength) {
318             return Toolkit.getDefaultToolkit().createImage( imagedata, imageoffset, imagelength );
319         }
320
321         public Image getImage(URL url) {
322             return Toolkit.getDefaultToolkit().getImage( url );
323         }
324
325         protected CheckboxPeer createCheckbox(Checkbox target) throws HeadlessException {
326             throw new IllegalStateException("Method not implemented");
327         }
328
329         public Image createImage(URL url) {
330             return Toolkit.getDefaultToolkit().createImage( url );
331         }
332
333         protected TextAreaPeer createTextArea(TextArea target) throws HeadlessException {
334             throw new IllegalStateException("Method not implemented");
335         }
336
337         protected FileDialogPeer createFileDialog(FileDialog target) throws HeadlessException {
338             throw new IllegalStateException("Method not implemented");
339         }
340
341         protected ScrollPanePeer createScrollPane(ScrollPane target) throws HeadlessException {
342             throw new IllegalStateException("Method not implemented");
343         }
344
345         protected DialogPeer createDialog(Dialog target) throws HeadlessException {
346             throw new IllegalStateException("Method not implemented");
347         }
348
349         protected PanelPeer createPanel(Panel target) {
350             throw new IllegalStateException("Method not implemented");
351         }
352
353         protected ChoicePeer createChoice(Choice target) throws HeadlessException {
354             throw new IllegalStateException("Method not implemented");
355         }
356
357         protected FramePeer createFrame(Frame target) throws HeadlessException {
358             throw new IllegalStateException("Method not implemented");
359         }
360
361         protected LabelPeer createLabel(Label target) throws HeadlessException {
362             throw new IllegalStateException("Method not implemented");
363         }
364
365         protected FontPeer getFontPeer(String name, int style) {
366             throw new IllegalStateException("Method not implemented");
367         }
368
369         protected CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem target) throws HeadlessException {
370             throw new IllegalStateException("Method not implemented");
371         }
372
373         protected WindowPeer createWindow(Window target) throws HeadlessException {
374             throw new IllegalStateException("Method not implemented");
375         }
376
377         public void sync() {
378             Toolkit.getDefaultToolkit().sync();
379         }
380
381         protected EventQueue getSystemEventQueueImpl() {
382             return Toolkit.getDefaultToolkit().getSystemEventQueue();
383         }
384
385         public Clipboard getSystemClipboard() throws HeadlessException {
386             return Toolkit.getDefaultToolkit().getSystemClipboard();
387         }
388
389         public Dimension getScreenSize() throws HeadlessException {
390             return Toolkit.getDefaultToolkit().getScreenSize();
391         }
392
393         public int getScreenResolution() throws HeadlessException {
394             return Toolkit.getDefaultToolkit().getScreenResolution();
395         }
396
397         public String[] getFontList() {
398             return Toolkit.getDefaultToolkit().getFontList();
399         }
400
401         public ColorModel getColorModel() throws HeadlessException {
402             return Toolkit.getDefaultToolkit().getColorModel();
403         }
404
405         public void beep() {
406             Toolkit.getDefaultToolkit().beep();
407         }
408
409         boolean createCustomCursorCalled = false;
410         public Cursor createCustomCursor(Image cursor, Point hotSpot, String name) throws IndexOutOfBoundsException, HeadlessException {
411
412             createCustomCursorCalled = true;
413             return Toolkit.getDefaultToolkit().createCustomCursor(cursor, hotSpot, name);
414         }
415
416         boolean getBestCursorSizeCalled = false;
417         public Dimension getBestCursorSize(int preferredWidth, int preferredHeight) throws HeadlessException {
418             getBestCursorSizeCalled = true;
419             return new Dimension(0,0);
420         }
421     }
422      */

423
424     public static final class AwtBridgeImpl extends AWTBridge {
425         public JPopupMenu JavaDoc createEmptyPopup() {
426             return new JPopupMenu JavaDoc();
427         }
428         public JMenuItem JavaDoc createMenuPresenter(Action JavaDoc action) {
429             return new JMenuItem JavaDoc(action);
430         }
431         public JMenuItem JavaDoc createPopupPresenter(Action JavaDoc action) {
432             return new JMenuItem JavaDoc(action);
433         }
434         public Component JavaDoc createToolbarPresenter(Action JavaDoc action) {
435             return new JButton JavaDoc(action);
436         }
437         public Component JavaDoc[] convertComponents(Component JavaDoc comp) {
438             if (comp instanceof JMenuItem JavaDoc && "HIDDEN".equals(((JMenuItem JavaDoc) comp).getText())) {
439                 return new Component JavaDoc[0];
440             } else {
441                 return new Component JavaDoc[] {comp};
442             }
443         }
444     }
445     
446 }
447
Popular Tags