KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > navigator > NavigatorTCTest


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.modules.navigator;
21
22 import java.util.List JavaDoc;
23 import javax.swing.JComboBox JavaDoc;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import org.netbeans.junit.NbTest;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.junit.NbTestSuite;
29 import org.netbeans.spi.navigator.NavigatorHandler;
30 import org.netbeans.spi.navigator.NavigatorLookupHint;
31 import org.netbeans.spi.navigator.NavigatorPanel;
32 import org.openide.util.ContextGlobalProvider;
33 import org.openide.util.Lookup;
34 import org.openide.util.lookup.AbstractLookup;
35 import org.openide.util.lookup.InstanceContent;
36 import org.openide.util.lookup.Lookups;
37
38
39 /**
40  *
41  * @author Dafe Simonek
42  */

43 public class NavigatorTCTest extends NbTestCase {
44     
45     /** Creates a new instance of ProviderRegistryTest */
46     public NavigatorTCTest() {
47         super("");
48     }
49     
50     public NavigatorTCTest(String JavaDoc testName) {
51         super(testName);
52     }
53     
54     public static void main(java.lang.String JavaDoc[] args) {
55         junit.textui.TestRunner.run(suite());
56     }
57     
58     public static NbTest suite() {
59         NbTestSuite suite = new NbTestSuite(NavigatorTCTest.class);
60         return suite;
61     }
62     
63     
64     public void testCorrectCallsOfNavigatorPanelMethods () throws Exception JavaDoc {
65         System.out.println("Testing correct calls of NavigatorPanel methods...");
66         InstanceContent ic = getInstanceContent();
67
68         TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd");
69         //nodesLkp.setNodes(new Node[]{ostravskiNode});
70
ic.add(ostravskiHint);
71             
72         NavigatorTC navTC = NavigatorTC.getInstance();
73         navTC.componentOpened();
74
75         NavigatorPanel selPanel = navTC.getSelectedPanel();
76         
77         assertNotNull("Selected panel is null", selPanel);
78         assertTrue("Panel class not expected", selPanel instanceof OstravskiGyzdProvider);
79         OstravskiGyzdProvider ostravak = (OstravskiGyzdProvider)selPanel;
80         assertEquals("panelActivated calls count invalid: " + ostravak.getPanelActivatedCallsCount(),
81                         1, ostravak.getPanelActivatedCallsCount());
82         assertEquals(0, ostravak.getPanelDeactivatedCallsCount());
83         
84         TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik");
85         ic.add(prazskyHint);
86         ic.remove(ostravskiHint);
87         
88         // wait for selected node change to be applied, because changes are
89
// reflected with little delay
90
waitForChange();
91         
92         selPanel = navTC.getSelectedPanel();
93         assertNotNull(selPanel);
94         assertTrue(selPanel instanceof PrazskyPepikProvider);
95         PrazskyPepikProvider prazak = (PrazskyPepikProvider)selPanel;
96         
97         assertEquals(1, ostravak.getPanelDeactivatedCallsCount());
98         assertTrue(ostravak.wasGetCompBetween());
99         assertFalse(ostravak.wasActCalledOnActive());
100         assertFalse(ostravak.wasDeactCalledOnInactive());
101         
102         assertEquals(1, prazak.getPanelActivatedCallsCount());
103         assertEquals(0, prazak.getPanelDeactivatedCallsCount());
104
105         ic.remove(prazskyHint);
106         ic.add(ostravskiHint);
107         // wait for selected node change to be applied, because changes are
108
// reflected with little delay
109
waitForChange();
110         
111         selPanel = navTC.getSelectedPanel();
112         assertNotNull("Selected panel is null", selPanel);
113         
114         assertEquals(1, prazak.getPanelDeactivatedCallsCount());
115         assertTrue(prazak.wasGetCompBetween());
116         assertFalse(prazak.wasActCalledOnActive());
117         assertFalse(prazak.wasDeactCalledOnInactive());
118         
119         navTC.componentClosed();
120
121         selPanel = navTC.getSelectedPanel();
122         assertNull("Selected panel should be null", selPanel);
123         assertNull("Set of panels should be null", navTC.getPanels());
124         
125         // clean
126
ic.remove(ostravskiHint);
127     }
128     
129     public void testBugfix80155_NotEmptyOnProperties () throws Exception JavaDoc {
130         System.out.println("Testing bugfix 80155, keeping content on Properties window and similar...");
131         InstanceContent ic = getInstanceContent();
132
133         TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd");
134         ic.add(ostravskiHint);
135             
136         NavigatorTC navTC = NavigatorTC.getInstance();
137         navTC.componentOpened();
138
139         NavigatorPanel selPanel = navTC.getSelectedPanel();
140         
141         assertNotNull("Selected panel is null", selPanel);
142         
143         ic.remove(ostravskiHint);
144         
145         // wait for selected node change to be applied, because changes are
146
// reflected with little delay
147
waitForChange();
148
149         // after 80155 fix, previous navigator should keep its content even when
150
// new component was activated, but didn't contain any activated nodes or navigator lookup hint
151
selPanel = navTC.getSelectedPanel();
152         assertNotNull("Selected panel is null", selPanel);
153         assertTrue("Panel class not expected", selPanel instanceof OstravskiGyzdProvider);
154     }
155     
156     public void testBugfix93123_RefreshCombo () throws Exception JavaDoc {
157         System.out.println("Testing bugfix 93123, correct refreshing of combo box with providers list...");
158
159         InstanceContent ic = getInstanceContent();
160         
161         TestLookupHint ostravskiHint = new TestLookupHint("ostravski/gyzd");
162         ic.add(ostravskiHint);
163         TestLookupHint prazskyHint = new TestLookupHint("prazsky/pepik");
164         ic.add(prazskyHint);
165             
166         NavigatorTC navTC = NavigatorTC.getInstance();
167         navTC.componentOpened();
168
169         List JavaDoc<NavigatorPanel> panels = navTC.getPanels();
170         
171         assertTrue("Expected 2 provider panels, but got " + panels.size(), panels.size() == 2);
172         
173         NavigatorHandler.activatePanel(panels.get(1));
174         
175         NavigatorPanel selPanel = navTC.getSelectedPanel();
176         int selIdx = panels.indexOf(selPanel);
177         
178         assertTrue("Expected selected provider #2, but got #1", selIdx == 1);
179         
180         TestLookupHint prazskyHint2 = new TestLookupHint("prazsky/pepik");
181         ic.add(prazskyHint2);
182         
183         // wait for selected node change to be applied, because changes are
184
// reflected with little delay
185
waitForChange();
186
187         panels = navTC.getPanels();
188         assertTrue("Expected 3 provider panels, but got " + panels.size(), panels.size() == 3);
189         
190         JComboBox JavaDoc combo = navTC.getPanelSelector();
191         assertTrue("Expected 3 combo items, but got " + combo.getItemCount(), combo.getItemCount() == 3);
192      
193         assertTrue("Expected the same selection", selPanel.equals(navTC.getSelectedPanel()));
194         
195         selIdx = panels.indexOf(selPanel);
196         assertTrue("Expected the same selection in combo, sel panel index: "
197                 + selIdx + ", sel in combo index: " +
198                 combo.getSelectedIndex(), selIdx == combo.getSelectedIndex());
199         
200     }
201     
202     /** Singleton global lookup. Lookup change notification won't come
203      * if setting global lookup (UnitTestUtils.prepareTest) is called
204      * multiple times.
205      */

206     private static InstanceContent getInstanceContent () throws Exception JavaDoc {
207         if (instanceContent == null) {
208             instanceContent = new InstanceContent();
209             GlobalLookup4TestImpl nodesLkp = new GlobalLookup4TestImpl(instanceContent);
210             UnitTestUtils.prepareTest(new String JavaDoc [] {
211                 "/org/netbeans/modules/navigator/resources/testCorrectCallsOfNavigatorPanelMethodsLayer.xml" },
212                 Lookups.singleton(nodesLkp)
213             );
214         }
215         return instanceContent;
216     }
217     
218     private void waitForChange () {
219         synchronized (this) {
220             try {
221                 wait(NavigatorController.COALESCE_TIME + 500);
222             } catch (InterruptedException JavaDoc exc) {
223                 System.out.println("waiting interrupted...");
224             }
225         }
226     }
227     
228
229     /** Test provider base, to test that infrastucture calls correct
230      * methods in correct order.
231      */

232     private static abstract class CorrectCallsProvider implements NavigatorPanel {
233         
234         private int panelActCalls = 0;
235         private int panelDeactCalls = 0;
236         
237         private boolean wasGetCompBetween = true;
238         
239         private boolean wasActCalledOnActive = false;
240         private boolean wasDeactCalledOnInactive = false;
241         
242         private boolean activated = false;
243         
244         public JComponent JavaDoc getComponent () {
245             if (!activated) {
246                 wasGetCompBetween = false;
247             }
248             return null;
249         }
250
251         public void panelActivated (Lookup context) {
252             if (activated) {
253                 wasActCalledOnActive = true;
254             }
255             panelActCalls++;
256             activated = true;
257         }
258
259         public void panelDeactivated () {
260             if (!activated) {
261                 wasDeactCalledOnInactive = true;
262             }
263             panelDeactCalls++;
264             activated = false;
265         }
266         
267         public Lookup getLookup () {
268             return null;
269         }
270         
271         public int getPanelActivatedCallsCount () {
272             return panelActCalls;
273         }
274         
275         public int getPanelDeactivatedCallsCount () {
276             return panelDeactCalls;
277         }
278         
279         public boolean wasGetCompBetween () {
280             return wasGetCompBetween;
281         }
282         
283         public boolean wasActCalledOnActive () {
284             return wasActCalledOnActive;
285         }
286         
287         public boolean wasDeactCalledOnInactive () {
288             return wasDeactCalledOnInactive;
289         }
290         
291     }
292
293     public static final class OstravskiGyzdProvider extends CorrectCallsProvider {
294         
295         public String JavaDoc getDisplayName () {
296             return "Ostravski Gyzd";
297         }
298     
299         public String JavaDoc getDisplayHint () {
300             return null;
301         }
302         
303         public JComponent JavaDoc getComponent () {
304             // ensure call is counted by superclass
305
super.getComponent();
306             return new JLabel JavaDoc(getDisplayName());
307         }
308
309     }
310     
311     public static final class PrazskyPepikProvider extends CorrectCallsProvider {
312         
313         public String JavaDoc getDisplayName () {
314             return "Prazsky Pepik";
315         }
316     
317         public String JavaDoc getDisplayHint () {
318             return null;
319         }
320         
321         public JComponent JavaDoc getComponent () {
322             // ensure call is counted by superclass
323
super.getComponent();
324             return new JLabel JavaDoc(getDisplayName());
325         }
326     
327     }
328
329     /** Envelope for textual (mime-type like) content type to be used in
330      * global lookup
331      */

332     private static final class TestLookupHint implements NavigatorLookupHint {
333         
334         private final String JavaDoc contentType;
335                 
336         public TestLookupHint (String JavaDoc contentType) {
337             this.contentType = contentType;
338         }
339         
340         public String JavaDoc getContentType () {
341             return contentType;
342         }
343
344     }
345             
346     
347     private static final class GlobalLookup4TestImpl extends AbstractLookup implements ContextGlobalProvider {
348         
349         public GlobalLookup4TestImpl (AbstractLookup.Content content) {
350             super(content);
351         }
352         
353         public Lookup createGlobalContext() {
354             return this;
355         }
356         
357         /*public GlobalLookup4Test() {
358             super(new Lookup[0]);
359         }
360         
361         public void setNodes(Node[] nodes) {
362             setLookups(new Lookup[] {Lookups.fixed(nodes)});
363         }*/

364     }
365     private static InstanceContent instanceContent;
366     
367 }
368
Popular Tags