KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > PasteActionTest


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.actions;
21
22
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import javax.swing.AbstractAction JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.ActionMap JavaDoc;
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JMenuItem JavaDoc;
30
31 import junit.textui.TestRunner;
32
33 import org.netbeans.junit.*;
34 import org.openide.actions.*;
35 import org.openide.actions.ActionsInfraHid.UsefulThings;
36 import org.openide.awt.DynamicMenuContent;
37 import org.openide.util.Lookup;
38 import org.openide.util.datatransfer.PasteType;
39 import org.openide.windows.TopComponent;
40
41
42 /** Test behaviour of PasteAction intogether with clonning.
43  */

44 public class PasteActionTest extends AbstractCallbackActionTestHidden {
45     static {
46             ActionsInfraHid.UT.setActivated (null);
47     }
48     
49     public PasteActionTest(String JavaDoc name) {
50         super(name);
51     }
52     
53     public static void main(String JavaDoc[] args) {
54         TestRunner.run(new NbTestSuite(PasteActionTest.class));
55     }
56     
57     protected Class JavaDoc actionClass () {
58         return PasteAction.class;
59     }
60     
61     protected String JavaDoc actionKey () {
62         return javax.swing.text.DefaultEditorKit.pasteAction;
63     }
64
65     public void testListenersAreUnregisteredBug32073 () throws Exception JavaDoc {
66         action.assertListeners ("When we created clone, we added a listener", 1);
67         
68         java.lang.ref.WeakReference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc (clone);
69         clone = null;
70         assertGC ("Clone can disappear", ref);
71         action.assertListeners ("No listeners, as the clone has been GCed", 0);
72     }
73     
74     public void testPresenterCanBeGCedIssue47314 () throws Exception JavaDoc {
75         javax.swing.JMenuItem JavaDoc item = ((org.openide.util.actions.Presenter.Popup)clone).getPopupPresenter ();
76         
77         java.lang.ref.WeakReference JavaDoc itemref = new java.lang.ref.WeakReference JavaDoc (item);
78         item = null;
79         java.lang.ref.WeakReference JavaDoc ref = new java.lang.ref.WeakReference JavaDoc (clone);
80         clone = null;
81         assertGC ("Item can disappear", itemref);
82         assertGC ("Clone can disappear", ref);
83     }
84     
85     
86     public void testDelegatesAsOneAction() {
87         OurAction[] arr = {
88             new OurAction ()
89         };
90
91         action.setEnabled (true);
92         assertTrue ("Now is the action enabled", clone.isEnabled());
93         listener.assertCnt ("No change fired as it was enabled by default", 0);
94         
95         arr[0].setEnabled (false);
96         action.putValue ("delegates", arr);
97         
98         assertTrue ("Clone should be disabled as the only action we delegate is", !clone.isEnabled ());
99         listener.assertCnt ("That means one change should be fired", 1);
100         
101         action.setEnabled (false);
102         assertTrue ("No influence on enabled state in delegate mode", !clone.isEnabled ());
103         listener.assertCnt ("No changes fired", 0);
104         
105         arr[0].setEnabled (true);
106         assertTrue ("State of clone changed to be enabled", clone.isEnabled ());
107         listener.assertCnt ("Change fired", 1);
108
109         arr[0].setEnabled (false);
110         assertTrue ("Disabled again", !clone.isEnabled ());
111         listener.assertCnt ("Changed delivered", 1);
112         
113         action.putValue ("delegates", null);
114         assertTrue ("Still disabled, because action itself is disabled", !clone.isEnabled ());
115         listener.assertCnt ("No changes due to that", 0);
116         
117         action.setEnabled (true);
118         assertTrue ("Now we are listening to just the one action", clone.isEnabled ());
119         listener.assertCnt ("And that is why we should be enabled with one event change", 1);
120         
121         arr[0].setEnabled (false);
122         action.putValue ("delegates", arr);
123         assertTrue ("Now we have delegates again, thus we are disabled", !clone.isEnabled ());
124         listener.assertCnt ("One change delivered", 1);
125     }
126     
127     public void testDelegatesAsArrayOfAction () throws Exception JavaDoc {
128         OurAction[] arr = {
129             new OurAction ()
130         };
131         action.putValue ("delegates", arr);
132         //arr[0].setEnabled (true);
133

134         TopComponent tc = new TopComponent();
135         tc.getActionMap ().put(javax.swing.text.DefaultEditorKit.pasteAction, action);
136         ActionsInfraHid.UT.setActivated (tc);
137         global.actionPerformed (new ActionEvent JavaDoc (this, 0, "waitFinished"));
138         
139         arr[0].assertCnt ("Performed on delegate", 1);
140         action.assertCnt ("Not performed on action", 0);
141     }
142     
143     public void testDelegatesAsArrayOfPasteType () throws Exception JavaDoc {
144         OurPasteType [] arr = {
145             new OurPasteType ()
146         };
147         action.putValue ("delegates", arr);
148         //arr[0].setEnabled (true);
149

150         TopComponent tc = new TopComponent();
151         tc.getActionMap ().put(javax.swing.text.DefaultEditorKit.pasteAction, action);
152         ActionsInfraHid.UT.setActivated (tc);
153         global.actionPerformed (new ActionEvent JavaDoc (this, 0, "waitFinished"));
154         
155         action.assertCnt ("Not performed on action", 0);
156         arr[0].assertCnt ("Performed on delegate", 1);
157     }
158     
159     public void testDelegatesAsMoreActions () throws Exception JavaDoc {
160         action.setEnabled (false);
161         listener.assertCnt ("One changed now", 1);
162         
163         OurAction[] arr = {
164             new OurAction (),
165             new OurAction ()
166         };
167         
168         
169         action.putValue ("delegates", arr);
170         assertTrue ("Enabled because it has more than one action", clone.isEnabled ());
171         listener.assertCnt ("One changes since that", 1);
172         
173         action.putValue ("delegates", new Object JavaDoc[0]);
174         assertTrue ("Disabled as no delegates", !clone.isEnabled ());
175         listener.assertCnt ("One changes since that", 1);
176
177         action.putValue ("delegates", arr);
178         assertTrue ("Enabled again", clone.isEnabled ());
179         
180         
181         clone.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
182         arr[0].assertCnt ("First delegate invoked", 1);
183     }
184     
185     public void testDelegatesAsMorePasteTypes () throws Exception JavaDoc {
186         action.setEnabled (false);
187         listener.assertCnt ("One changed now", 1);
188         
189         OurPasteType[] arr = {
190             new OurPasteType(),
191             new OurPasteType()
192         };
193         
194         
195         action.putValue ("delegates", arr);
196         assertTrue ("Enabled because it has more than one action", clone.isEnabled ());
197         listener.assertCnt ("One changes since that", 1);
198         
199         action.putValue ("delegates", new Object JavaDoc[0]);
200         assertTrue ("Disabled as no delegates", !clone.isEnabled ());
201         listener.assertCnt ("One changes since that", 1);
202
203         action.putValue ("delegates", arr);
204         assertTrue ("Enabled again", clone.isEnabled ());
205         
206         clone.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
207         arr[0].assertCnt ("First delegate invoked", 1);
208
209         arr = new OurPasteType[] { new OurPasteType () };
210         
211         action.putValue ("delegates", arr);
212         assertTrue ("Enabled still", clone.isEnabled ());
213         
214         clone.actionPerformed (new java.awt.event.ActionEvent JavaDoc (this, 0, "waitFinished"));
215         arr[0].assertCnt ("First delegate invoked", 1);
216     }
217     
218     public void testDisableIsOk() throws Exception JavaDoc {
219         PasteAction p = (PasteAction)PasteAction.get(PasteAction.class);
220         
221         class A extends AbstractAction JavaDoc {
222             public void actionPerformed(ActionEvent JavaDoc e) {
223             }
224         }
225         A action = new A();
226         action.setEnabled(false);
227 // action.putValue("delegates", new A[0]);
228

229         TopComponent td = new TopComponent();
230         td.getActionMap().put(javax.swing.text.DefaultEditorKit.pasteAction, action);
231         
232         ActionsInfraHid.UT.setActivated(td);
233         
234         assertFalse("Disabled", p.isEnabled());
235         JMenuItem JavaDoc item = p.getMenuPresenter();
236         assertTrue("Dynamic one: " + item, item instanceof DynamicMenuContent);
237         DynamicMenuContent d = (DynamicMenuContent)item;
238         JComponent JavaDoc[] items = d.getMenuPresenters();
239         items = d.synchMenuPresenters(items);
240         assertEquals("One item", 1, items.length);
241         assertTrue("One jmenu item", items[0] instanceof JMenuItem JavaDoc);
242         JMenuItem JavaDoc one = (JMenuItem JavaDoc)items[0];
243         assertFalse("And is disabled", one.getModel().isEnabled());
244     }
245     
246     private static final class OurPasteType extends org.openide.util.datatransfer.PasteType {
247         private int cnt;
248         
249         public java.awt.datatransfer.Transferable JavaDoc paste() throws java.io.IOException JavaDoc {
250             cnt++;
251             return null;
252         }
253         
254         public void assertCnt (String JavaDoc msg, int count) {
255             assertEquals (msg, count, this.cnt);
256             this.cnt = 0;
257         }
258     } // end of OurPasteType
259

260 }
261
Popular Tags