KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > actions > Issue71764Test


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.util.actions;
21
22 import java.io.IOException JavaDoc;
23 import junit.framework.*;
24 import org.netbeans.junit.*;
25 import org.openide.cookies.SaveCookie;
26 import org.openide.nodes.AbstractNode;
27 import org.openide.nodes.Children;
28 import org.openide.nodes.Node;
29 import org.openide.util.ContextGlobalProvider;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.Lookup;
32 import org.openide.util.actions.NodeAction;
33 import org.openide.util.lookup.Lookups;
34
35 /** Tests if NodeAction initializes its listener list and resposes
36  * to cookie changes. See http://www.netbeans.org/issues/show_bug.cgi?id=71764.
37  */

38 public class Issue71764Test extends NbTestCase {
39     
40     public Issue71764Test(java.lang.String JavaDoc testName) {
41         super(testName);
42     }
43     
44     public static void main(java.lang.String JavaDoc[] args) {
45         junit.textui.TestRunner.run(suite());
46     }
47     
48     public static Test suite() {
49         TestSuite suite = new NbTestSuite(Issue71764Test.class);
50         return suite;
51     }
52     
53     public void test71764() {
54         MockServices.setServices(ContextProvider.class);
55         
56         ContextProvider provider = Lookup.getDefault().lookup(ContextProvider.class);
57         assertNotNull ("ContextProvider is not null.", provider);
58         
59         NodeAction action = (NodeAction) new TestAction ();
60         Node node = new TestNode();
61
62         assertFalse("Global Action should not be enabled yet", action.isEnabled());
63         
64         ContextProvider.current = node;
65         provider.getLookup().lookup(Object JavaDoc.class);
66         
67         assertTrue("Global Action is enabled", action.isEnabled());
68     }
69     
70     class TestNode extends AbstractNode {
71         public TestNode() {
72             super(Children.LEAF);
73             getCookieSet().add(new SaveCookie() {
74                 public void save() throws IOException JavaDoc {
75                     System.out.println("Save cookie called");
76                 }
77             });
78         }
79     }
80     
81     public static class ContextProvider implements ContextGlobalProvider, Lookup.Provider {
82         static Node current;
83         Lookup lookup = Lookups.proxy (this );
84
85         public Lookup createGlobalContext() {
86             return lookup;
87         }
88     
89         public Lookup getLookup() {
90             return current == null ? Lookup.EMPTY : current.getLookup();
91         }
92     }
93     
94     public static class TestAction extends CookieAction {
95         protected int mode() {
96             return MODE_EXACTLY_ONE;
97         }
98         
99         protected Class JavaDoc[] cookieClasses() {
100             return new Class JavaDoc[] {SaveCookie.class};
101         }
102         
103         protected void performAction(Node[] activatedNodes) {
104             assert false;
105         }
106         public String JavaDoc getName() {
107             return "TestAction";
108         }
109         public HelpCtx getHelpCtx() {
110             return null;
111         }
112         protected boolean asynchronous() {
113             return false;
114         }
115     }
116 }
117
Popular Tags