KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > nodes2looks > LookNodeTest


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.netbeans.api.nodes2looks;
21
22
23 import org.openide.nodes.*;
24 import org.openide.cookies.InstanceCookie;
25 import org.openide.util.Lookup;
26 import org.openide.util.actions.SystemAction;
27
28 import org.netbeans.junit.*;
29
30 import junit.framework.*;
31
32 import org.netbeans.spi.looks.*;
33
34 import javax.swing.*;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.util.Collections JavaDoc;
37
38
39 /** Tests methods of the LookNode. Notice that the communication with look
40  * is tested using. LookNodeValuesTest. This class only tests additional
41  * methods.
42  */

43 public class LookNodeTest extends NbTestCase {
44
45     // Sample looks
46
SampleLook sampleLook1;
47     SampleLook sampleLook2;
48
49     // The lookNode
50
Node lookNode;
51
52     private static final String JavaDoc NODE_NAME_1 = "Node name 1";
53     private static final String JavaDoc NODE_NAME_2 = "Node name 2";
54
55     // Represented object
56
SampleRepObject representedObject;
57
58     // Methods of testCase -----------------------------------------------------
59

60     public LookNodeTest(java.lang.String JavaDoc testName) {
61         super(testName);
62     }
63
64     public static void main(java.lang.String JavaDoc[] args) {
65         junit.textui.TestRunner.run(suite());
66     }
67
68     public static Test suite() {
69         TestSuite suite = new NbTestSuite(LookNodeTest.class);
70
71         return suite;
72     }
73
74     protected void setUp() throws Exception JavaDoc {
75         super.setUp();
76
77         sampleLook1 = new SampleLook( "First sample look" ) {
78             public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
79                 return NODE_NAME_1;
80             }
81         };
82
83         sampleLook2 = new SampleLook( "Second sample look" ) {
84             public String JavaDoc getName( Object JavaDoc representedObject, Lookup env ) {
85                 return NODE_NAME_2;
86             }
87         };
88         representedObject = new SampleRepObject( createGoldenValues() );
89         lookNode = Nodes.node( representedObject,
90                                sampleLook1,
91                                Selectors.selector( new SampleProvider( sampleLook1 ) ) );
92
93     }
94
95     protected void tearDown() throws Exception JavaDoc {
96
97         sampleLook1 = sampleLook2 = null;
98
99         lookNode = null;
100         representedObject = null;
101
102         super.tearDown();
103
104     }
105
106     // Test methods ------------------------------------------------------------
107

108     public void testGetRepresentedObject() {
109
110         SampleRepObject ro = (SampleRepObject)TestUtil.getRepresentedObject( lookNode );
111         if ( ro != representedObject ) {
112             fail("Bad represented object.");
113         }
114
115     }
116
117     public void testGetRepresentedObjectByInstanceCookie() throws Exception JavaDoc {
118         InstanceCookie.Of ic = (InstanceCookie.Of) lookNode.getLookup().lookup( InstanceCookie.Of.class );
119
120         assertTrue( "Bad represented object. ", ic.instanceCreate() == representedObject );
121     }
122
123     public void testSetLook() {
124
125         TestUtil.setLook( lookNode, sampleLook2 );
126         if ( TestUtil.getLook( lookNode ) != sampleLook2 ) {
127             fail( "Bad or no look not set." );
128         }
129         if ( lookNode.getName() != NODE_NAME_2 ) {
130             fail( "Bad name from look: " + lookNode.getName() );
131         }
132
133         TestUtil.setLook( lookNode, sampleLook1 );
134         if ( TestUtil.getLook( lookNode ) != sampleLook1 ) {
135             fail( "Bad or no look not set." );
136         }
137         if ( lookNode.getName() != NODE_NAME_1 ) {
138             fail( "Bad name from look: " + lookNode.getName() );
139         }
140
141     }
142
143     public void testGetLook() {
144
145         Look look = TestUtil.getLook( lookNode );
146         if ( look != sampleLook1 ) {
147             fail("Bad or no look on node");
148         }
149
150     }
151
152     public void testCloneNode() {
153
154         Look look = sampleLook2;
155         Node lookNode = Nodes.node(new Object JavaDoc(), look, Selectors.selector( new SampleProvider( look ) ) );
156
157         Node clonedNode = lookNode.cloneNode();
158         Node secondLevelClone = clonedNode.cloneNode();
159
160         assertTrue ("Cloned node must not be null!", clonedNode != null );
161         //assertTrue ("Cloned node must be a chameleon!", clonedNode.getBaseLook() == lookNode.getBaseLook ());
162
//assertTrue ("Second level clone has different hard look!", clonedNode.getBaseLook() == secondLevelClone.getBaseLook());
163
// assert("Second level clone has different hard look candidate!", clonedNode.getHardLookCandidate() == secondLevelClone.getHardLookCandidate());
164

165         // we do not have any parent node so we do not use chameleon look but rather hard look candidate
166

167         assertTrue ("Both clones must result in same getLook()!",
168             ((org.netbeans.api.nodes2looks.LookNode)clonedNode).getLook() ==
169             ((org.netbeans.api.nodes2looks.LookNode)secondLevelClone).getLook());
170
171     }
172
173     public void testGetActions() {
174
175         Action[] actions = lookNode.getActions();
176
177         assertEquals( "There should be 3 items", 3, actions.length );
178         assertNull( "First item is not SystemAction.", actions[0] );
179         assertNull( "Second item is separator", actions[1] );
180         assertEquals( "Third item is SystemAction.", GoldenValue.TestingAction1.class, actions[2].getClass() );
181
182         actions = lookNode.getActions( false );
183
184         assertEquals( "There should be 3 items", 3, actions.length );
185         assertEquals( "First item is Action.", TestBaseAction.class, actions[0].getClass() );
186         assertNull( "Second item is separator", actions[1] );
187         assertEquals( "Third item is SystemAction.", GoldenValue.TestingAction1.class, actions[2].getClass() );
188
189
190     }
191
192     public void testGetContextActions() {
193
194         Action[] actions = lookNode.getContextActions();
195
196         assertEquals( "There should be 3 items", 3, actions.length );
197         assertNull( "First item is not SystemAction.", actions[0] );
198         assertNull( "Second item is separator", actions[1] );
199         assertEquals( "Third item is SystemAction.", GoldenValue.TestingAction1.class, actions[2].getClass() );
200
201         actions = lookNode.getActions( true );
202
203         assertEquals( "There should be 3 items", 3, actions.length );
204         assertEquals( "First item is Action.", TestBaseAction.class, actions[0].getClass() );
205         assertNull( "Second item is separator", actions[1] );
206         assertEquals( "Third item is SystemAction.", GoldenValue.TestingAction1.class, actions[2].getClass() );
207
208
209     }
210
211
212     public void testGetDefaultAction() {
213
214         Action action = lookNode.getDefaultAction();
215
216         assertNull( "Default action is not SystemAction.", action );
217
218         action = lookNode.getPreferredAction();
219
220         assertEquals( "Default Action is Action.", TestBaseAction.class, action.getClass() );
221
222
223     }
224     
225     public void testProperLookSelected() {
226         
227         ProxyLookTest.ExceptionLook e1 =
228             new ProxyLookTest.ExceptionLook( "e1", IllegalArgumentException JavaDoc.class );
229         ProxyLookTest.ExceptionLook e2 =
230             new ProxyLookTest.ExceptionLook( "e2", ClassCastException JavaDoc.class );
231         ProxyLookTest.ExceptionLook e3 =
232             new ProxyLookTest.ExceptionLook( "e3", null );
233         
234         LookSelector s1 = Selectors.array( new Look[] { e1, e2, e3 } );
235         LookSelector s2 = Selectors.array( new Look[] { e1, e2 } );
236         
237         Node n1 = Nodes.node( "RO1", null, s1 );
238         Look l1 = TestUtil.getLook( n1 );
239         
240         Node n2 = Nodes.node( "RO1", null, s2 );
241         Look l2 = TestUtil.getLook( n2 );
242         
243         assertTrue( "Node shuld have the look e3 set. Was " + l1, l1 == e3 );
244         assertTrue( "Node shuld have the beanLook set. Was " + l2, l2 == Looks.bean() );
245         
246     }
247     
248     public void testSize() {
249         
250         Look look = new SampleLook( "Second sample look" );
251         LookSelector selector = Selectors.selector( new SampleProvider( sampleLook1 ) );
252         SampleRepObject ro = new SampleRepObject( createGoldenValues() );
253         Node n = Nodes.node( ro, look, selector );
254         
255         
256         Object JavaDoc subtract[] = new Object JavaDoc[] {
257             look,
258             selector,
259             ro,
260             org.openide.util.Utilities.activeReferenceQueue(),
261             ((LookNode)n).getCache(),
262             Collections.EMPTY_LIST,
263             Children.LEAF
264         };
265         
266         assertSize( "Size of the node " , Collections.singleton( n ), 168, subtract );
267
268         n.getIcon( 0 );
269
270         assertSize( "Size of the node " , Collections.singleton( n ), 168, subtract );
271                 
272         n.getLookup ();
273
274         assertSize( "Size of the node " , Collections.singleton( n ), 504, subtract );
275     }
276
277     // Private helper methods --------------------------------------------------
278

279
280     private static GoldenValue[] createGoldenValues() {
281
282         GoldenValue goldenValues[] = new GoldenValue[] {
283             new GoldenValue( Look.GET_LOOKUP_ITEMS, GoldenValue.createGoldenLookupItems() ),
284             new GoldenValue( Look.GET_ACTIONS,
285                 new Action[] { new TestBaseAction(), null, SystemAction.get( GoldenValue.TestingAction1.class ) } ),
286             new GoldenValue( Look.GET_CONTEXT_ACTIONS,
287                 new Action[] { new TestBaseAction(), null, SystemAction.get( GoldenValue.TestingAction1.class ) } ),
288             new GoldenValue( Look.GET_DEFAULT_ACTION, new TestBaseAction() )
289         };
290
291         return goldenValues;
292
293     }
294
295
296     public static final class TestBaseAction extends AbstractAction {
297
298         public void actionPerformed(ActionEvent JavaDoc e) {
299         }
300
301     }
302
303
304 }
305
306
Popular Tags