KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > windows > GlobalContextImplTest


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.windows;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import javax.swing.ActionMap JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.openide.nodes.Node;
27 import org.openide.util.Lookup;
28 import org.openide.util.lookup.AbstractLookup;
29 import org.openide.util.lookup.InstanceContent;
30
31 /** Tests behaviour of GlobalContextProviderImpl
32  * and its cooperation with activated and current nodes.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class GlobalContextImplTest extends NbTestCase
37 implements org.openide.util.LookupListener {
38
39     private javax.swing.Action JavaDoc sampleAction = new javax.swing.AbstractAction JavaDoc () {
40         public void actionPerformed (java.awt.event.ActionEvent JavaDoc ev) {
41         }
42     };
43     private TopComponent tc;
44     private Lookup lookup;
45     private Lookup.Result result;
46     private int cnt;
47     
48     
49     public GlobalContextImplTest(java.lang.String JavaDoc testName) {
50         super(testName);
51     }
52     
53     protected void setUp () throws Exception JavaDoc {
54         tc = new TopComponent ();
55         tc.getActionMap ().put (this, sampleAction);
56         tc.requestActive();
57         
58         
59         Lookup global = Lookup.getDefault();
60         
61         Object JavaDoc p = global.lookup (org.openide.util.ContextGlobalProvider.class);
62         assertNotNull ("There is one", p);
63         assertEquals ("Action context provider is our as well", org.netbeans.modules.openide.windows.GlobalActionContextImpl.class, p.getClass ());
64         
65         
66         lookup = org.openide.util.Utilities.actionsGlobalContext();
67         result = lookup.lookup (new Lookup.Template<Node> (Node.class));
68         result.addLookupListener (this);
69         result.allItems();
70     }
71     
72     private void assertActionMap () {
73         ActionMap JavaDoc map = (ActionMap JavaDoc)lookup.lookup (ActionMap JavaDoc.class);
74         assertNotNull ("Map has to be there", map);
75         
76         javax.swing.Action JavaDoc action = map.get (this);
77         assertEquals ("It is really our action", sampleAction, action);
78     }
79         
80     
81     public void testCurrentNodes () throws Exception JavaDoc {
82         tc.setActivatedNodes(new Node[] {Node.EMPTY});
83
84         assertEquals ("This fires change", 1, cnt);
85
86         assertEquals ("One item in result", 1, result.allItems ().size ());
87         Lookup.Item item = (Lookup.Item)result.allItems ().iterator ().next ();
88         assertEquals ("Item should return Node.EMPTY", Node.EMPTY, item.getInstance());
89         assertActionMap ();
90         
91         tc.setActivatedNodes (null);
92         assertEquals ("One change", 2, cnt);
93         
94         assertEquals ("One empty item in result", 1, result.allItems ().size ());
95         item = (Lookup.Item)result.allItems ().iterator ().next ();
96         assertEquals ("Item should return null", null, item.getInstance());
97         assertEquals ("Name is null", "none", item.getId ());
98         assertActionMap ();
99         
100         tc.setActivatedNodes (new Node[0]);
101         assertEquals ("No change", 3, cnt);
102         
103         assertEquals ("No items in lookup", 0, result.allItems ().size ());
104         assertActionMap ();
105     }
106     
107     public void testRequestVisibleBlinksTheActionMapForAWhile () throws Exception JavaDoc {
108         final org.openide.nodes.Node n = new org.openide.nodes.AbstractNode (org.openide.nodes.Children.LEAF);
109         tc.setActivatedNodes(new Node[] { n });
110         
111         assertActionMap ();
112         final Lookup.Result<ActionMap JavaDoc> res = lookup.lookup (new Lookup.Template<ActionMap JavaDoc> (ActionMap JavaDoc.class));
113         assertEquals ("One action map", 1, res.allItems ().size ());
114         
115         class L implements org.openide.util.LookupListener {
116             ArrayList JavaDoc<ActionMap JavaDoc> maps = new ArrayList JavaDoc<ActionMap JavaDoc> ();
117             
118             public void resultChanged (org.openide.util.LookupEvent ev) {
119                 assertEquals ("Still only one", 1, res.allItems ().size ());
120                 Lookup.Item<ActionMap JavaDoc> i = res.allItems ().iterator ().next ();
121                 assertNotNull (i);
122                 
123                 maps.add (i.getInstance ());
124                 
125                 assertNode ();
126             }
127             
128             public void assertNode () {
129                 assertEquals ("The node is available", n, lookup.lookup (Node.class));
130             }
131         }
132         L myListener = new L ();
133         myListener.assertNode ();
134         
135         res.addLookupListener (myListener);
136                 
137         TopComponent my = new TopComponent ();
138         my.requestVisible ();
139         
140         if (myListener.maps.size () != 2) {
141             fail ("Expected two changes in the ActionMaps: " + myListener.maps);
142         }
143
144         myListener.assertNode ();
145
146         ActionMap JavaDoc m1 = (ActionMap JavaDoc)myListener.maps.get (0);
147         ActionMap JavaDoc m2 = (ActionMap JavaDoc)myListener.maps.get (1);
148         
149         assertNull ("Our action is not in first map", m1.get (this));
150         assertEquals ("Our action is in second map", sampleAction, m2.get (this));
151
152         assertActionMap ();
153         
154         res.removeLookupListener(myListener);
155         
156     }
157     
158     public void testComponentChangeActionMapIsPropagatedToGlobalLookup() throws Exception JavaDoc {
159         assertEquals("test1", 0, cnt);
160
161         
162         InstanceContent ic = new InstanceContent();
163         AbstractLookup al = new AbstractLookup(ic);
164         tc = new TopComponent (al);
165
166         assertEquals("test2", 0, cnt);
167         
168         
169         ActionMap JavaDoc myMap = new ActionMap JavaDoc();
170         myMap.put (this, sampleAction);
171         assertEquals("test3", 0, cnt);
172
173         tc.requestActive();
174         
175         assertEquals("test4", 1, cnt);
176         
177         result = lookup.lookup (new Lookup.Template<ActionMap JavaDoc> (ActionMap JavaDoc.class));
178         result.addLookupListener (this);
179         result.allItems();
180         
181         assertEquals("test5", 1, cnt);
182         
183         ic.set(Collections.singleton(new ActionMap JavaDoc()), null);
184         
185         assertEquals("One change in ActiomMap delivered", 2, cnt);
186     }
187     
188     
189     public void resultChanged(org.openide.util.LookupEvent ev) {
190         cnt++;
191     }
192
193 }
194
Popular Tags