KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.Action JavaDoc;
24 import javax.swing.JPopupMenu JavaDoc;
25 import junit.textui.TestRunner;
26
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29
30 import org.openide.loaders.TemplateWizard;
31 import org.openide.nodes.AbstractNode;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.Node;
34 import org.openide.util.Lookup;
35 import org.openide.util.Utilities;
36 import org.openide.util.lookup.*;
37 /** Test creating NewTemplateAction by context.
38  * See issue 28785.
39  */

40 public class NewTemplateActionTest extends NbTestCase {
41     public NewTemplateActionTest(String JavaDoc name) {
42         super(name);
43     }
44     
45     public void testContextAware () {
46         NewTemplateAction global = (NewTemplateAction)NewTemplateAction.get (NewTemplateAction.class);
47         
48         CookieNode node = new CookieNode ();
49         JPopupMenu JavaDoc popup = Utilities.actionsToPopup (new Action JavaDoc[] {
50             global
51         }, node.getLookup ());
52         
53         assertTrue ("NewTemplateAction's cookie must be called.", node.counter > 0);
54         
55         global.getPopupPresenter ();
56         
57         assertTrue ("When calling wizard on global action, the CookieNode's cookie is not " +
58             "as it is not selected", node.counter > 0
59         );
60     }
61
62     public void testContextAwareWithChanges () {
63         doContextAwareWithChanges (false);
64     }
65     public void testContextAwareWithChangesWithDeepGC () {
66         doContextAwareWithChanges (true);
67     }
68     
69     private void doContextAwareWithChanges (boolean withGC) {
70         class P implements Lookup.Provider {
71             private Lookup lookup = Lookup.EMPTY;
72             
73             public Lookup getLookup () {
74                 return lookup;
75             }
76         }
77         P provider = new P ();
78         Lookup lookup = Lookups.proxy (provider);
79         
80         NewTemplateAction global = (NewTemplateAction)NewTemplateAction.get (NewTemplateAction.class);
81         Action JavaDoc clone = global.createContextAwareInstance (lookup);
82         CookieNode node = new CookieNode ();
83         
84         //assertTrue ("Global is enabled", global.isEnabled ());
85
assertFalse ("Local is not enabled if no nodes provided", clone.isEnabled ());
86         
87         JPopupMenu JavaDoc popup = Utilities.actionsToPopup (new Action JavaDoc[] {
88             global
89         }, lookup);
90         
91         if (withGC) {
92             try {
93                 assertGC ("Will fail", new java.lang.ref.WeakReference JavaDoc (this));
94             } catch (Throwable JavaDoc t) {
95             }
96         }
97         
98         assertFalse ("No node selected, no query", node.counter > 0);
99         
100         provider.lookup = node.getLookup ();
101         lookup.lookup (Object JavaDoc.class); // does refresh
102

103         assertTrue ("After change of Lookup the CookieNode is queried for cookie", node.counter > 0);
104         assertTrue ("Local is enabled if a node is provided", clone.isEnabled ());
105     }
106     
107     private static class CookieNode extends AbstractNode implements NewTemplateAction.Cookie {
108         public CookieNode () {
109             super (Children.LEAF);
110             getCookieSet ().add (this);
111         }
112         
113         int counter = 0;
114         public TemplateWizard getTemplateWizard () {
115             counter ++;
116             return new TemplateWizard ();
117         }
118     }
119 }
120
Popular Tags