|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.util.actions; import javax.swing.ActionMap; import org.netbeans.junit.*; import junit.textui.TestRunner; import org.openide.actions.CopyAction; import org.openide.windows.TopComponent; import org.openide.util.HelpCtx; /** Test CallbackSystemAction: changing performer, focus tracking. * @author Jesse Glick */ public class CallbackSystemActionTest extends NbTestCase { static { // Get Lookup right to begin with. ActionsInfraHid.class.getName(); } public CallbackSystemActionTest(String name) { super(name); } public static void main(String[] args) { TestRunner.run(new NbTestSuite(CallbackSystemActionTest.class)); // May have used AWT thread. System.exit(0); } protected boolean runInEQ () { return true; } /** Make sure that the performer system works and controls enablement. */ public void testPerformer() throws Exception { CallbackSystemAction a = (CallbackSystemAction)SystemAction.get(SimpleCallbackAction.class); assertFalse(a.isEnabled()); Performer p = new Performer(); assertEquals(0, p.count); a.setActionPerformer(p); assertTrue(a.isEnabled()); a.actionPerformed(null); assertEquals(1, p.count); a.setActionPerformer(null); assertFalse(a.isEnabled()); } /** Make sure that focus changes turn on or off actions as appropriate. */ public void testFocusChanges() throws Exception { helperTestFocusChanges(); // CallbackSystemAction keeps a listener separately from the action, // so make sure the actions still work after collected and recreated. // Note that similar code fails to work in NodeActionTest because the // GC will not complete, so if the GC assert here starts to fail, it is // OK to comment out the GC, its assert, and the second call to // helperTestFocusChanges(). ActionsInfraHid.doGC(); assertEquals("Garbage collection actually collected something", 0, SimpleCallbackAction.INSTANCES); helperTestFocusChanges(); } private void helperTestFocusChanges() throws Exception { TopComponent t1 = new TopComponent(); t1.setName("t1"); TopComponent t2 = new TopComponent(); t2.setName("t2"); ActionsInfraHid.UT.setActivated(t1); try { CallbackSystemAction a1 = (CallbackSystemAction)SystemAction.get(SurviveFocusChgCallbackAction.class); assertTrue(a1.getSurviveFocusChange()); CallbackSystemAction a2 = (CallbackSystemAction)SystemAction.get(SimpleCallbackAction.class); assertFalse(a2.getSurviveFocusChange()); CallbackSystemAction a3 = (CallbackSystemAction)SystemAction.get(DoesNotSurviveFocusChgCallbackAction.class); assertFalse(a3.getSurviveFocusChange()); Performer p = new Performer(); a1.setActionPerformer(p); a2.setActionPerformer(p); a3.setActionPerformer(p); assertTrue(a1.isEnabled()); assertTrue(a2.isEnabled()); assertTrue(a3.isEnabled()); ActionsInfraHid.UT.setActivated(t2); assertTrue(a1.isEnabled()); assertEquals(p, a1.getActionPerformer()); assertFalse(a2.isEnabled()); assertEquals(null, a2.getActionPerformer()); assertFalse(a3.isEnabled()); assertEquals(null, a3.getActionPerformer()); } finally { ActionsInfraHid.UT.setActivated(null); } } /* public void testGlobalChanges () throws Exception { class MyAction extends javax.swing.AbstractAction { public int cntEnabled; public int cntPerformed; public boolean isEnabled () { cntEnabled++; return true; } public void actionPerformed (java.awt.event.ActionEvent ev) { cntPerformed++; } } MyAction myAction = new MyAction (); TopComponent tc = new TopComponent (); tc.getActionMap().put (javax.swing.text.DefaultEditorKit.copyAction, myAction); CopyAction a = (CopyAction)CopyAction.get (CopyAction.class); ActionsInfraHid.UT.setActivated(tc); try { assertTrue ("MyAction is enabled", a.isEnabled ()); assertEquals ("isEnabled called once", 1, myAction.cntEnabled); a.setActionPerformer(null); assertEquals ("No enabled called again", 1, myAction.cntEnabled); } finally { ActionsInfraHid.UT.setActivated (null); } } */ /** Action performer that counts invocations. */ public static final class Performer implements ActionPerformer { public int count = 0; public void performAction(SystemAction action) { count++; } } /** Simple callback action. */ public static final class SimpleCallbackAction extends CallbackSystemAction { public String getName() { return "SimpleCallbackAction"; } public HelpCtx getHelpCtx() { return null; } public static int INSTANCES = 0; public SimpleCallbackAction() { INSTANCES++; } protected boolean clearSharedData() { INSTANCES--; return super.clearSharedData(); } protected boolean asynchronous() { return false; } } /** Similar but survives focus changes. */ public static final class SurviveFocusChgCallbackAction extends CallbackSystemAction { protected void initialize() { super.initialize(); setSurviveFocusChange(true); } public String getName() { return "SurviveFocusChgCallbackAction"; } public HelpCtx getHelpCtx() { return null; } protected boolean asynchronous() { return false; } } /** Similar but does not; should behave like SimpleCallbackAction (it just sets the flag explicitly). */ public static final class DoesNotSurviveFocusChgCallbackAction extends CallbackSystemAction { protected void initialize() { super.initialize(); setSurviveFocusChange(false); } public String getName() { return "SurviveFocusChgCallbackAction"; } public HelpCtx getHelpCtx() { return null; } protected boolean asynchronous() { return false; } } // // Set of tests for ActionMap and context // public void testLookupOfStateInActionMap () throws Exception { class MyAction extends javax.swing.AbstractAction implements org.openide.util.actions.ActionPerformer { int actionPerformed; int performAction; public void actionPerformed (java.awt.event.ActionEvent ev) { actionPerformed++; } public void performAction (SystemAction a) { performAction++; } } MyAction action = new MyAction (); ActionMap map = new ActionMap (); CallbackSystemAction system = (CallbackSystemAction)SystemAction.get(SurviveFocusChgCallbackAction.class); system.setActionPerformer (null); map.put (system.getActionMapKey(), action); javax.swing.Action clone; // // Without action map // clone = system.createContextAwareInstance(org.openide.util.Lookup.EMPTY); assertTrue ("Action should not be enabled if no callback provided", !clone.isEnabled()); system.setActionPerformer (action); assertTrue ("Is enabled, because it has a performer", clone.isEnabled()); system.setActionPerformer (null); assertTrue ("Is disabled, because the performer has been unregistered", !clone.isEnabled ()); // // test with actionmap // action.setEnabled (false); org.openide.util.Lookup context = org.openide.util.lookup.Lookups.singleton(map); clone = system.createContextAwareInstance(context); CntListener listener = new CntListener (); clone.addPropertyChangeListener (listener); assertTrue ("Not enabled now", !clone.isEnabled ()); action.setEnabled (true); assertTrue ("Clone is enabled because the action in ActionMap is", clone.isEnabled ()); listener.assertCnt ("One change expected", 1); system.setActionPerformer (action); clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, "")); assertEquals ("MyAction.actionPerformed invoked", 1, action.actionPerformed); assertEquals ("MyAction.performAction is not invoked", 0, action.performAction); action.setEnabled (false); assertTrue ("Clone is disabled because the action in ActionMap is", !clone.isEnabled ()); listener.assertCnt ("Another change expected", 1); clone.actionPerformed(new java.awt.event.ActionEvent (this, 0, "")); assertEquals ("MyAction.actionPerformed invoked again", 2, action.actionPerformed); assertEquals ("MyAction.performAction is not invoked, remains 0", 0, action.performAction); } private static final class CntListener extends Object implements java.beans.PropertyChangeListener { private int cnt; public void propertyChange(java.beans.PropertyChangeEvent evt) { cnt++; } public void assertCnt (String msg, int count) { assertEquals (msg, count, this.cnt); this.cnt = 0; } } // end of CntListener } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.