KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > project > ui > actions > LookupSensitiveActionUILogTest


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.project.ui.actions;
21
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Handler JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.util.logging.LogRecord JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JMenuItem JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36 import javax.swing.JToolBar JavaDoc;
37 import javax.swing.KeyStroke JavaDoc;
38 import org.netbeans.junit.NbTestCase;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataObject;
42 import org.openide.util.Lookup;
43 import org.openide.util.Utilities;
44
45 public class LookupSensitiveActionUILogTest extends NbTestCase {
46
47     public LookupSensitiveActionUILogTest(String JavaDoc name) {
48         super( name );
49     }
50             
51     private FileObject dir, f1, f2, f3, f4;
52     private DataObject d1, d2, d3, d4;
53     
54     private MyHandler my;
55         
56     protected void setUp() throws Exception JavaDoc {
57         super.setUp();
58         clearWorkDir();
59         dir = FileUtil.toFileObject(getWorkDir());
60         f1 = dir.createData("f1.java");
61         f2 = dir.createData("f2.java");
62         f3 = dir.createData("f3.properties");
63         f4 = dir.createData("f4.xml");
64         d1 = DataObject.find(f1);
65         d2 = DataObject.find(f2);
66         d3 = DataObject.find(f3);
67         d4 = DataObject.find(f4);
68         
69         my = new MyHandler();
70         Logger.getLogger("org.netbeans.ui.actions").addHandler(my);
71         Logger.getLogger("org.netbeans.ui.actions").setLevel(Level.FINE);
72     }
73     
74     protected void tearDown() throws Exception JavaDoc {
75         clearWorkDir();
76         super.tearDown();
77     }
78     
79     public boolean runInEQ () {
80         return true;
81     }
82     
83     public void testMenuPushIsNotified() throws Exception JavaDoc {
84         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
85         TestLSA tlsa = new TestLSA( lookup );
86     assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ());
87     tlsa.refreshCounter = 0;
88         TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
89         tlsa.addPropertyChangeListener( tpcl );
90         lookup.change(d2);
91         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
92         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
93         assertTrue("Action is enabled", tlsa.isEnabled());
94
95         tlsa.setDisplayName("Jarda");
96      
97         
98         JMenuItem JavaDoc item = new JMenuItem JavaDoc(tlsa);
99         item.doClick();
100         
101         assertEquals("One record logged:\n" + my.recs, 1, my.recs.size());
102         LogRecord JavaDoc r = my.recs.get(0);
103         assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage());
104         assertEquals("four args", 5, r.getParameters().length);
105         assertEquals("first is the menu item", item, r.getParameters()[0]);
106         assertEquals("second is its class", JMenuItem JavaDoc.class.getName(), r.getParameters()[1]);
107         assertEquals("3rd is action", tlsa, r.getParameters()[2]);
108         assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]);
109         assertEquals("5th name", "Jarda", r.getParameters()[4]);
110         
111         tlsa.clear();
112         tpcl.clear();
113         lookup.change(d3);
114         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
115         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
116     }
117
118     public void testToolbarPushIsNotified() throws Exception JavaDoc {
119         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
120         TestLSA tlsa = new TestLSA( lookup );
121     assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ());
122     tlsa.refreshCounter = 0;
123         TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
124         tlsa.addPropertyChangeListener( tpcl );
125         lookup.change(d2);
126         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
127         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
128         assertTrue("Action is enabled", tlsa.isEnabled());
129
130         tlsa.setDisplayName("Jarda");
131      
132         JToolBar JavaDoc bar = new JToolBar JavaDoc();
133         JButton JavaDoc item = bar.add(tlsa);
134         item.doClick();
135         
136         assertEquals("One record logged:\n" + my.recs, 1, my.recs.size());
137         LogRecord JavaDoc r = my.recs.get(0);
138         assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage());
139         assertEquals("four args", 5, r.getParameters().length);
140         assertEquals("first is the menu item", item, r.getParameters()[0]);
141         assertEquals("second is its class", item.getClass().getName(), r.getParameters()[1]);
142         assertEquals("3rd is action", tlsa, r.getParameters()[2]);
143         assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]);
144         assertEquals("5th name", "Jarda", r.getParameters()[4]);
145         
146         tlsa.clear();
147         tpcl.clear();
148         lookup.change(d3);
149         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
150         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
151     }
152     
153     public void testKeyEventIsNotified() throws Exception JavaDoc {
154         TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
155         TestLSA tlsa = new TestLSA( lookup );
156     assertTrue ("TestLSA action is enabled.", tlsa.isEnabled ());
157     tlsa.refreshCounter = 0;
158         TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
159         tlsa.addPropertyChangeListener( tpcl );
160         lookup.change(d2);
161         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
162         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
163         assertTrue("Action is enabled", tlsa.isEnabled());
164
165         tlsa.setDisplayName("Jarda");
166      
167         KeyStroke JavaDoc ks = Utilities.stringToKey("C-S");
168         
169         class MyPanel extends JPanel JavaDoc {
170             public void doEvent(KeyEvent JavaDoc ev, KeyStroke JavaDoc ks) {
171             
172                 super.processKeyBinding(ks, ev, JPanel.WHEN_FOCUSED, true);
173             }
174         }
175         
176         MyPanel p = new MyPanel();
177         p.getInputMap(JComponent.WHEN_FOCUSED).put(ks, "save");
178         p.getActionMap().put("save", tlsa);
179         KeyEvent JavaDoc ev = new KeyEvent JavaDoc(p, KeyEvent.KEY_TYPED, System.currentTimeMillis(), KeyEvent.CTRL_DOWN_MASK, KeyEvent.VK_UNDEFINED, 'S');
180         p.doEvent(ev, ks);
181         
182         assertEquals("No record logged:\n" + my.recs, 0, my.recs.size());
183         /*
184         LogRecord r = my.recs.get(0);
185         assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage());
186         assertEquals("four args", 5, r.getParameters().length);
187 // assertEquals("first is the menu item", item, r.getParameters()[0]);
188 // assertEquals("second is its class", item.getClass().getName(), r.getParameters()[1]);
189         assertEquals("3rd is action", tlsa, r.getParameters()[2]);
190         assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]);
191         assertEquals("5th name", "Jarda", r.getParameters()[4]);
192         
193         tlsa.clear();
194         tpcl.clear();
195         lookup.change(d3);
196         assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
197         assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
198          */

199     }
200     
201     private static class TestLSA extends LookupSensitiveAction {
202         
203         private int performCounter;
204         private int refreshCounter;
205                
206         public TestLSA( Lookup lookup ) {
207             super( null, lookup, new Class JavaDoc[] { DataObject.class } );
208         }
209         
210         protected void actionPerformed( Lookup context ) {
211             performCounter++;
212         }
213            
214         protected void refresh( Lookup context ) {
215             refreshCounter++;
216             
217             DataObject dobj = context.lookup(DataObject.class);
218             
219             if (dobj != null) {
220         putValue( Action.NAME, dobj.getName() );
221         }
222             
223         }
224         
225         public void clear() {
226             performCounter = refreshCounter = 0;
227         }
228         
229         
230     }
231     
232     
233     private static class TestPropertyChangeListener implements PropertyChangeListener JavaDoc {
234         
235         List JavaDoc<PropertyChangeEvent JavaDoc> events = new ArrayList JavaDoc<PropertyChangeEvent JavaDoc>();
236         
237         public void propertyChange( PropertyChangeEvent JavaDoc e ) {
238             events.add( e );
239         }
240         
241         void clear() {
242             events.clear();
243         }
244         
245         List JavaDoc<PropertyChangeEvent JavaDoc> getEvents() {
246             return events;
247         }
248                 
249     }
250         
251
252     private static final class MyHandler extends Handler JavaDoc {
253         public final List JavaDoc<LogRecord JavaDoc> recs = new ArrayList JavaDoc<LogRecord JavaDoc>();
254         
255         public void publish(LogRecord JavaDoc record) {
256             recs.add(record);
257         }
258
259         public void flush() {
260         }
261
262         public void close() throws SecurityException JavaDoc {
263         }
264     } // end of MyHandler
265
}
266
Popular Tags