KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.*;
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JMenuItem JavaDoc;
25
26 import junit.framework.*;
27
28 import org.netbeans.junit.*;
29
30 /** Tests of actions related methods in Utilities class.
31  */

32 public class UtilitiesActionsTest extends NbTestCase {
33
34     public UtilitiesActionsTest(java.lang.String JavaDoc testName) {
35         super(testName);
36     }
37
38     public static void main(java.lang.String JavaDoc[] args) {
39         junit.textui.TestRunner.run(new NbTestSuite(UtilitiesActionsTest.class));
40     }
41
42     public void testActionClone () throws Exception JavaDoc {
43         CloneAction original = new CloneAction ();
44         
45         javax.swing.Action JavaDoc clone = original.createContextAwareInstance(Lookup.EMPTY);
46
47         assertTrue ("Clone is instance of desired class", clone instanceof CloneAction);
48         assertNull ("Original has empty lookup", original.lookup);
49         assertEquals ("Clone has lookup assigned", Lookup.EMPTY, ((CloneAction)clone).lookup);
50     }
51     
52     
53     public void testActionToPopupForLookup () throws Exception JavaDoc {
54         class MyAction extends CloneAction
55         implements org.openide.util.actions.Presenter.Popup {
56             public JMenuItem JavaDoc item = new JMenuItem JavaDoc ("Ahoj");
57             public int called;
58             
59             protected CloneAction cloneAction () {
60                 return new MyAction ();
61             }
62
63             
64             public JMenuItem JavaDoc getPopupPresenter () {
65                 called++;
66                 return item;
67             }
68         }
69         
70         final CloneAction[] arr = {
71             new CloneAction (), new MyAction ()
72         };
73         
74         final javax.swing.JPopupMenu JavaDoc menu = org.openide.util.Utilities.actionsToPopup (arr, Lookup.EMPTY);
75         
76         assertEquals ("Presenter called", 1, ((MyAction)arr[1].clone).called);
77         assertEquals ("Lookup is fine", ((CloneAction)arr[0].clone).lookup, Lookup.EMPTY);
78         assertEquals ("The other as well", ((CloneAction)arr[1].clone).lookup, Lookup.EMPTY);
79         
80         // We need to do this stuff in AWT; cf. Actions.Bridge.propertyChange.
81
final AssertionFailedError[] err = new AssertionFailedError[1];
82         Mutex.EVENT.readAccess(new Runnable JavaDoc() {
83             public void run() {
84                  try {
85         
86         javax.swing.MenuElement JavaDoc[] elem = menu.getSubElements ();
87         assertEquals ("Two subitems", 2, elem.length);
88
89         // Otherwise there may be no listener:
90
elem[0].getComponent().addNotify();
91         
92         arr[0].clone.setEnabled(false);
93         assertTrue ("Menu item is disabled", !((JMenuItem JavaDoc)elem[0].getComponent()).isEnabled ());
94         arr[0].clone.setEnabled(true);
95         assertTrue ("Menu item is enabled", ((JMenuItem JavaDoc)elem[0].getComponent()).isEnabled ());
96         
97         assertEquals ("The presenter is really used", ((MyAction)arr[1].clone).item, elem[1].getComponent ());
98         
99                  } catch (AssertionFailedError e) {
100                      err[0] = e;
101                  }
102             }
103         });
104         if (err[0] != null) throw err[0];
105     }
106     
107     public void testActionsToPopupFromComponent () throws Exception JavaDoc {
108         Lookup lookup = org.openide.util.lookup.Lookups.singleton (this);
109         final Lookup[] returnedLookup = {lookup};
110         
111         class Provider extends javax.swing.JComponent JavaDoc implements Lookup.Provider {
112             public Lookup getLookup () {
113                 return returnedLookup[0];
114             }
115         }
116         
117         CloneAction sample = new CloneAction ();
118         
119         
120         javax.swing.JComponent JavaDoc outerMost = new javax.swing.JPanel JavaDoc ();
121         outerMost.getActionMap().put ("1", sample);
122         
123         javax.swing.JComponent JavaDoc provider = new Provider ();
124         outerMost.add (provider);
125         provider.getActionMap().put ("2", sample);
126         
127         javax.swing.JComponent JavaDoc child = new javax.swing.JPanel JavaDoc ();
128         provider.add (child);
129         child.getActionMap().put ("3", sample);
130         javax.swing.JComponent JavaDoc menuOwner = new javax.swing.JPanel JavaDoc ();
131         child.add (menuOwner);
132         menuOwner.getActionMap().put ("4", sample);
133         
134         javax.swing.JComponent JavaDoc sibling = new javax.swing.JPanel JavaDoc ();
135         provider.add (child);
136         sibling.getActionMap().put ("5", sample);
137
138         
139         CloneAction[] arr = { new CloneAction () };
140         
141         javax.swing.JPopupMenu JavaDoc menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner);
142         
143         Lookup actionLookup = arr[0].clone.lookup;
144         assertNotNull("Clone lookup is not null", actionLookup);
145         assertEquals ("The lookup returned by 'provider' is assigned to the clonned actions",
146             this, actionLookup.lookup (this.getClass ())
147         );
148         
149         // Now try it without a (valid) Lookup.Provider in the component hierarchy.
150
returnedLookup[0] = null;
151         arr[0].clone = null;
152         menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner);
153         
154         actionLookup = arr[0].clone.lookup;
155         assertNotNull("Clone lookup is not null", actionLookup);
156         
157         javax.swing.ActionMap JavaDoc map = (javax.swing.ActionMap JavaDoc)actionLookup.lookup (javax.swing.ActionMap JavaDoc.class);
158         assertNotNull ("Action map is in the lookup", map);
159         assertNull ("ActionMap of parent of Lookup.Provider is not included", map.get ("1"));
160         assertNotNull ("ActionMap of Lookup.Provider is included", map.get ("2"));
161         assertNotNull ("So is the child's one", map.get ("3"));
162         assertNotNull ("And also of the menuOwner", map.get ("4"));
163         assertNull ("But of course the sibling's one is not", map.get ("5"));
164     }
165     
166     class CloneAction extends javax.swing.AbstractAction JavaDoc implements ContextAwareAction {
167         public Lookup lookup;
168         public CloneAction clone;
169
170         public void actionPerformed (java.awt.event.ActionEvent JavaDoc ev) {
171         }
172         
173         protected CloneAction cloneAction () {
174             return new CloneAction ();
175         }
176
177         public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
178             assertNull ("We support just one clone right now", clone);
179             clone = cloneAction ();
180             clone.lookup = actionContext;
181             return clone;
182         }
183         
184     } // end of CloneAction
185

186 }
187
Popular Tags