KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > looks > TestBaseEvents


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.spi.looks;
21
22 import java.awt.image.BufferedImage JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Collection JavaDoc;
26
27 import org.openide.nodes.*;
28
29 import org.netbeans.junit.*;
30
31 import org.netbeans.spi.looks.*;
32
33 public class TestBaseEvents extends NbTestCase {
34
35     // Sample look we test against
36
protected SampleLook sampleLook;
37
38     // The node to test on
39
protected Node node;
40
41     // Represented object of the tested node
42
protected SampleRepObject representedObject;
43
44     // The test listener used for the node
45
protected GoldenEvent.Listener testNodeListener;
46
47     // Property change listener
48
protected GoldenEvent.Listener testPcl;
49
50     // Golden values
51
protected GoldenValue[] goldenValues;
52
53     // Methods of testCase -----------------------------------------------------
54

55     public TestBaseEvents(java.lang.String JavaDoc testName) {
56         super(testName);
57     }
58
59     public static void main(java.lang.String JavaDoc[] args) {
60         junit.textui.TestRunner.run(suite());
61     }
62
63     public static NbTest suite() {
64         NbTestSuite suite = new NbTestSuite(TestBaseEvents.class);
65         return suite;
66     }
67
68     protected void setUp() throws Exception JavaDoc {
69         super.setUp();
70     }
71
72     protected void tearDown() throws Exception JavaDoc {
73         node = null;
74         representedObject = null;
75
76         super.tearDown();
77     }
78
79
80     // Methods for setting up the test case ------------------------------------
81

82     protected void setTarget( Node node, SampleRepObject representedObject ) {
83         this.node = node;
84         this.representedObject = representedObject;
85
86         testNodeListener = new GoldenEvent.Listener();
87         node.addNodeListener( testNodeListener );
88         testPcl = new GoldenEvent.Listener();
89         node.addPropertyChangeListener( testPcl );
90
91     }
92
93     protected void setGoldenValues( GoldenValue[] goldenValues ) {
94         this.goldenValues = goldenValues;
95     }
96
97     // Test methods ------------------------------------------------------------
98

99
100     public void testFirePropertyChange() {
101
102         representedObject.setProperty( "MY_PROP", "something" );
103         representedObject.setProperty( "MY_PROP", "something else" );
104
105         GoldenEvent[] goldenEvents = new GoldenEvent[] {
106             new GoldenEvent( node,
107                              "MY_PROP",
108                              null, null ),
109
110             new GoldenEvent( node,
111                              "MY_PROP",
112                              null, null )
113         };
114
115         assertTrue( GoldenEvent.compare( testPcl.getEvents(),
116                                          goldenEvents,
117                                          null ) );
118
119         assertTrue( "Unexpected events in NodeListsner: " + testNodeListener.getEvents().size(),
120                     testNodeListener.getEvents().size() == 0 );
121     }
122
123
124     public void testFireNameChange() {
125
126         String JavaDoc oldValue = (String JavaDoc)GoldenValue.get( ProxyLook.GET_NAME, goldenValues );
127
128         representedObject.setValue( ProxyLook.GET_NAME, "New name" );
129
130         GoldenEvent[] goldenEvents = new GoldenEvent[] {
131             new GoldenEvent( node,
132                              Node.PROP_NAME,
133                              null, null ),
134         };
135
136         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
137                                          goldenEvents,
138                                          null ) );
139     }
140
141     public void testFireDisplayNameChange() {
142         String JavaDoc oldValue = (String JavaDoc)GoldenValue.get( ProxyLook.GET_DISPLAY_NAME, goldenValues );
143
144         representedObject.setValue( ProxyLook.GET_DISPLAY_NAME, "New display name" );
145
146         GoldenEvent[] goldenEvents = new GoldenEvent[] {
147             new GoldenEvent( node,
148                              Node.PROP_DISPLAY_NAME,
149                              null, null ),
150         };
151
152         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
153                                          goldenEvents,
154                                          null ) );
155     }
156
157     public void testFireShortDescriptionChange() {
158         String JavaDoc oldValue = (String JavaDoc)GoldenValue.get( ProxyLook.GET_SHORT_DESCRIPTION, goldenValues );
159
160         representedObject.setValue( ProxyLook.GET_SHORT_DESCRIPTION, "New short description" );
161
162         GoldenEvent[] goldenEvents = new GoldenEvent[] {
163             new GoldenEvent( node,
164                              Node.PROP_SHORT_DESCRIPTION,
165                              null, null ),
166         };
167
168         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
169                                          goldenEvents,
170                                          null ) );
171     }
172
173
174     public void testFireIconChange() {
175         representedObject.setValue( ProxyLook.GET_ICON,
176                                     new BufferedImage JavaDoc( 16, 16, BufferedImage.TYPE_INT_RGB ) );
177
178         GoldenEvent[] goldenEvents = new GoldenEvent[] {
179             new GoldenEvent( node,
180                              Node.PROP_ICON,
181                              null, null ),
182         };
183
184         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
185                                          goldenEvents,
186                                          null ) );
187
188     }
189
190     public void testFireOpenedIconChange() {
191         representedObject.setValue( ProxyLook.GET_OPENED_ICON,
192                                     new BufferedImage JavaDoc( 16, 16, BufferedImage.TYPE_INT_RGB ) );
193
194         GoldenEvent[] goldenEvents = new GoldenEvent[] {
195             new GoldenEvent( node,
196                              Node.PROP_OPENED_ICON,
197                              null, null ),
198         };
199
200         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
201                                          goldenEvents,
202                                          null ) );
203     }
204
205     public void testFirePropertySetsChange() {
206         Node.PropertySet[] oldValue = (Node.PropertySet[])GoldenValue.get( ProxyLook.GET_PROPERTY_SETS, goldenValues );
207         Node.PropertySet[] newValue = new Node.PropertySet[] {
208                                             new Sheet.Set(),
209                                             new Sheet.Set()
210                                         };
211
212         representedObject.setValue( ProxyLook.GET_PROPERTY_SETS, newValue );
213
214         GoldenEvent[] goldenEvents = new GoldenEvent[] {
215             new GoldenEvent( node,
216                              Node.PROP_PROPERTY_SETS,
217                              null, null ),
218         };
219
220         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
221                                          goldenEvents,
222                                          null ) );
223
224     }
225
226
227     public void testFireCookieChange() {
228
229         Collection JavaDoc oldItems = (Collection JavaDoc)GoldenValue.get( ProxyLook.GET_LOOKUP_ITEMS, goldenValues );
230
231         node.getCookie( org.openide.cookies.CloseCookie.class ); // To make lookup fire
232

233
234         Collection JavaDoc items = new ArrayList JavaDoc( oldItems ); // Needed to make propertySupport to fire
235

236         items.add(
237             new GoldenValue.TestLookupItem (
238                 new org.openide.cookies.ViewCookie() {
239                     public void view() {}
240                 }
241             )
242         );
243         items.add( new GoldenValue.TestLookupItem ( new javax.swing.JPanel JavaDoc() ) );
244
245         representedObject.setValue( ProxyLook.GET_LOOKUP_ITEMS, items );
246
247         GoldenEvent[] goldenEvents = new GoldenEvent[] {
248             new GoldenEvent( node,
249                              Node.PROP_COOKIE,
250                              null, null ),
251         };
252
253         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
254                                          goldenEvents,
255                                          null ) );
256
257     }
258
259
260     public void testFireNodeDestroyed() {
261         representedObject.setProperty( SampleRepObject.DESTROY, "kill" );
262
263         assertTrue( "Bad number of events: " + testNodeListener.getEvents().size(),
264                     testNodeListener.getEvents().size() == 1 );
265
266     }
267
268
269     public void testAddChildren() {
270         List JavaDoc oldValue = (List JavaDoc)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );;
271         List JavaDoc newValue = new ArrayList JavaDoc( oldValue );
272         newValue.add( "Additional child" );
273
274         // Workaround for Children.Keys behavior
275
node.removeNodeListener( testNodeListener );
276         Node oldNodes[] = node.getChildren().getNodes();
277         node.addNodeListener( testNodeListener );
278
279         representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue );
280         Node newNodes[] = node.getChildren().getNodes();
281
282         assertEquals(3, newNodes.length);
283         GoldenEvent[] goldenEvents = new GoldenEvent[] {
284             new GoldenEvent( node,
285                              true,
286                              new Node[] { newNodes[2] },
287                              new int[] { 2 } ),
288         };
289
290         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
291                                          goldenEvents,
292                                          null ) );
293     }
294
295     public void testRemoveChildren() {
296         List JavaDoc oldValue = (List JavaDoc)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );
297         List JavaDoc newValue = new ArrayList JavaDoc( oldValue );
298         newValue.remove( 0 );
299
300         // Workaround for Children.Keys behavior
301
node.removeNodeListener( testNodeListener );
302         Node oldNodes[] = node.getChildren().getNodes();
303         node.addNodeListener( testNodeListener );
304
305         representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue );
306         Node newNodes[] = node.getChildren().getNodes();
307
308         GoldenEvent[] goldenEvents = new GoldenEvent[] {
309             new GoldenEvent( node,
310                              false,
311                              new Node[] { oldNodes[0] },
312                              new int[] { 0 } ),
313         };
314
315         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
316                                          goldenEvents,
317                                          null ) );
318
319     }
320
321
322     public void testReorderChildren() {
323         List JavaDoc oldValue = (List JavaDoc)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );
324         List JavaDoc newValue = new ArrayList JavaDoc( oldValue );
325         Object JavaDoc o0 = newValue.get( 0 );
326         Object JavaDoc o1 = newValue.get( 1 );
327         newValue.set( 0, o1 );
328         newValue.set( 1, o0 );
329
330         // Workaround for Children.Keys behavior
331
node.removeNodeListener( testNodeListener );
332         Node oldNodes[] = node.getChildren().getNodes();
333         node.addNodeListener( testNodeListener );
334
335         representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue );
336         Node newNodes[] = node.getChildren().getNodes();
337
338         GoldenEvent[] goldenEvents = new GoldenEvent[] {
339             new GoldenEvent( node,
340                              new int[] { 1, 0 } )
341         };
342
343         assertTrue( GoldenEvent.compare( testNodeListener.getEvents(),
344                                          goldenEvents,
345                                          null ) );
346     }
347
348
349     public void testNoChildrenChange() {
350         List JavaDoc oldValue = (List JavaDoc)GoldenValue.get( ProxyLook.GET_CHILD_OBJECTS, goldenValues );
351         List JavaDoc newValue = new ArrayList JavaDoc( oldValue );
352
353         // Workaround for Children.Keys behavior
354
node.removeNodeListener( testNodeListener );
355         node.addNodeListener( testNodeListener );
356
357         representedObject.setValue( ProxyLook.GET_CHILD_OBJECTS, newValue );
358
359
360         assertTrue( "Bad number of events : " + testNodeListener.getEvents().size(),
361                     testNodeListener.getEvents().size() == 0 );
362
363
364     }
365
366
367 }
368
369
Popular Tags