001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.project.ui.actions;
043:
044: import java.awt.event.KeyEvent;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.util.ArrayList;
048: import java.util.List;
049: import java.util.logging.Handler;
050: import java.util.logging.Level;
051: import java.util.logging.LogRecord;
052: import java.util.logging.Logger;
053: import javax.swing.Action;
054: import javax.swing.JButton;
055: import javax.swing.JComponent;
056: import javax.swing.JMenuItem;
057: import javax.swing.JPanel;
058: import javax.swing.JToolBar;
059: import javax.swing.KeyStroke;
060: import org.netbeans.junit.NbTestCase;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.loaders.DataObject;
064: import org.openide.util.Lookup;
065: import org.openide.util.Utilities;
066:
067: public class LookupSensitiveActionUILogTest extends NbTestCase {
068:
069: public LookupSensitiveActionUILogTest(String name) {
070: super (name);
071: }
072:
073: private FileObject dir, f1, f2, f3, f4;
074: private DataObject d1, d2, d3, d4;
075:
076: private MyHandler my;
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080: clearWorkDir();
081: dir = FileUtil.toFileObject(getWorkDir());
082: f1 = dir.createData("f1.java");
083: f2 = dir.createData("f2.java");
084: f3 = dir.createData("f3.properties");
085: f4 = dir.createData("f4.xml");
086: d1 = DataObject.find(f1);
087: d2 = DataObject.find(f2);
088: d3 = DataObject.find(f3);
089: d4 = DataObject.find(f4);
090:
091: my = new MyHandler();
092: Logger.getLogger("org.netbeans.ui.actions").addHandler(my);
093: Logger.getLogger("org.netbeans.ui.actions")
094: .setLevel(Level.FINE);
095: }
096:
097: protected void tearDown() throws Exception {
098: clearWorkDir();
099: super .tearDown();
100: }
101:
102: public boolean runInEQ() {
103: return true;
104: }
105:
106: public void testMenuPushIsNotified() throws Exception {
107: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
108: TestLSA tlsa = new TestLSA(lookup);
109: assertTrue("TestLSA action is enabled.", tlsa.isEnabled());
110: tlsa.refreshCounter = 0;
111: TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
112: tlsa.addPropertyChangeListener(tpcl);
113: lookup.change(d2);
114: assertEquals("Refresh should be called once", 1,
115: tlsa.refreshCounter);
116: assertEquals("One event should be fired", 1, tpcl.getEvents()
117: .size());
118: assertTrue("Action is enabled", tlsa.isEnabled());
119:
120: tlsa.setDisplayName("Jarda");
121:
122: JMenuItem item = new JMenuItem(tlsa);
123: item.doClick();
124:
125: assertEquals("One record logged:\n" + my.recs, 1, my.recs
126: .size());
127: LogRecord r = my.recs.get(0);
128: assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r
129: .getMessage());
130: assertEquals("four args", 5, r.getParameters().length);
131: assertEquals("first is the menu item", item,
132: r.getParameters()[0]);
133: assertEquals("second is its class", JMenuItem.class.getName(),
134: r.getParameters()[1]);
135: assertEquals("3rd is action", tlsa, r.getParameters()[2]);
136: assertEquals("4th its class", tlsa.getClass().getName(), r
137: .getParameters()[3]);
138: assertEquals("5th name", "Jarda", r.getParameters()[4]);
139:
140: tlsa.clear();
141: tpcl.clear();
142: lookup.change(d3);
143: assertEquals("Refresh should be called once", 1,
144: tlsa.refreshCounter);
145: assertEquals("One event should be fired", 1, tpcl.getEvents()
146: .size());
147: }
148:
149: public void testToolbarPushIsNotified() throws Exception {
150: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
151: TestLSA tlsa = new TestLSA(lookup);
152: assertTrue("TestLSA action is enabled.", tlsa.isEnabled());
153: tlsa.refreshCounter = 0;
154: TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
155: tlsa.addPropertyChangeListener(tpcl);
156: lookup.change(d2);
157: assertEquals("Refresh should be called once", 1,
158: tlsa.refreshCounter);
159: assertEquals("One event should be fired", 1, tpcl.getEvents()
160: .size());
161: assertTrue("Action is enabled", tlsa.isEnabled());
162:
163: tlsa.setDisplayName("Jarda");
164:
165: JToolBar bar = new JToolBar();
166: JButton item = bar.add(tlsa);
167: item.doClick();
168:
169: assertEquals("One record logged:\n" + my.recs, 1, my.recs
170: .size());
171: LogRecord r = my.recs.get(0);
172: assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r
173: .getMessage());
174: assertEquals("four args", 5, r.getParameters().length);
175: assertEquals("first is the menu item", item,
176: r.getParameters()[0]);
177: assertEquals("second is its class", item.getClass().getName(),
178: r.getParameters()[1]);
179: assertEquals("3rd is action", tlsa, r.getParameters()[2]);
180: assertEquals("4th its class", tlsa.getClass().getName(), r
181: .getParameters()[3]);
182: assertEquals("5th name", "Jarda", r.getParameters()[4]);
183:
184: tlsa.clear();
185: tpcl.clear();
186: lookup.change(d3);
187: assertEquals("Refresh should be called once", 1,
188: tlsa.refreshCounter);
189: assertEquals("One event should be fired", 1, tpcl.getEvents()
190: .size());
191: }
192:
193: public void testKeyEventIsNotified() throws Exception {
194: TestSupport.ChangeableLookup lookup = new TestSupport.ChangeableLookup();
195: TestLSA tlsa = new TestLSA(lookup);
196: assertTrue("TestLSA action is enabled.", tlsa.isEnabled());
197: tlsa.refreshCounter = 0;
198: TestPropertyChangeListener tpcl = new TestPropertyChangeListener();
199: tlsa.addPropertyChangeListener(tpcl);
200: lookup.change(d2);
201: assertEquals("Refresh should be called once", 1,
202: tlsa.refreshCounter);
203: assertEquals("One event should be fired", 1, tpcl.getEvents()
204: .size());
205: assertTrue("Action is enabled", tlsa.isEnabled());
206:
207: tlsa.setDisplayName("Jarda");
208:
209: KeyStroke ks = Utilities.stringToKey("C-S");
210:
211: class MyPanel extends JPanel {
212: public void doEvent(KeyEvent ev, KeyStroke ks) {
213:
214: super .processKeyBinding(ks, ev, JPanel.WHEN_FOCUSED,
215: true);
216: }
217: }
218:
219: MyPanel p = new MyPanel();
220: p.getInputMap(JComponent.WHEN_FOCUSED).put(ks, "save");
221: p.getActionMap().put("save", tlsa);
222: KeyEvent ev = new KeyEvent(p, KeyEvent.KEY_TYPED, System
223: .currentTimeMillis(), KeyEvent.CTRL_DOWN_MASK,
224: KeyEvent.VK_UNDEFINED, 'S');
225: p.doEvent(ev, ks);
226:
227: assertEquals("No record logged:\n" + my.recs, 0, my.recs.size());
228: /*
229: LogRecord r = my.recs.get(0);
230: assertEquals("Menu push", "UI_ACTION_BUTTON_PRESS", r.getMessage());
231: assertEquals("four args", 5, r.getParameters().length);
232: // assertEquals("first is the menu item", item, r.getParameters()[0]);
233: // assertEquals("second is its class", item.getClass().getName(), r.getParameters()[1]);
234: assertEquals("3rd is action", tlsa, r.getParameters()[2]);
235: assertEquals("4th its class", tlsa.getClass().getName(), r.getParameters()[3]);
236: assertEquals("5th name", "Jarda", r.getParameters()[4]);
237:
238: tlsa.clear();
239: tpcl.clear();
240: lookup.change(d3);
241: assertEquals( "Refresh should be called once", 1, tlsa.refreshCounter );
242: assertEquals( "One event should be fired", 1, tpcl.getEvents().size() );
243: */
244: }
245:
246: private static class TestLSA extends LookupSensitiveAction {
247:
248: private int performCounter;
249: private int refreshCounter;
250:
251: public TestLSA(Lookup lookup) {
252: super (null, lookup, new Class[] { DataObject.class });
253: }
254:
255: protected void actionPerformed(Lookup context) {
256: performCounter++;
257: }
258:
259: protected void refresh(Lookup context) {
260: refreshCounter++;
261:
262: DataObject dobj = context.lookup(DataObject.class);
263:
264: if (dobj != null) {
265: putValue(Action.NAME, dobj.getName());
266: }
267:
268: }
269:
270: public void clear() {
271: performCounter = refreshCounter = 0;
272: }
273:
274: }
275:
276: private static class TestPropertyChangeListener implements
277: PropertyChangeListener {
278:
279: List<PropertyChangeEvent> events = new ArrayList<PropertyChangeEvent>();
280:
281: public void propertyChange(PropertyChangeEvent e) {
282: events.add(e);
283: }
284:
285: void clear() {
286: events.clear();
287: }
288:
289: List<PropertyChangeEvent> getEvents() {
290: return events;
291: }
292:
293: }
294:
295: private static final class MyHandler extends Handler {
296: public final List<LogRecord> recs = new ArrayList<LogRecord>();
297:
298: public void publish(LogRecord record) {
299: recs.add(record);
300: }
301:
302: public void flush() {
303: }
304:
305: public void close() throws SecurityException {
306: }
307: } // end of MyHandler
308: }
|