KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > StatusLineElementProviderTest


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.core.windows.view.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collection JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import junit.textui.TestRunner;
28 import java.net.*;
29 import org.openide.util.Lookup;
30 import org.openide.util.lookup.AbstractLookup;
31 import org.openide.util.lookup.InstanceContent;
32 import org.openide.util.lookup.Lookups;
33
34 import org.netbeans.junit.*;
35 import org.netbeans.core.*;
36 import org.openide.awt.StatusLineElementProvider;
37
38
39 /** Test what MainWindow returns the Status Line Elements and
40  * a listener on lookup.result changes.
41  * @author Jiri Rechtacek
42  */

43 public class StatusLineElementProviderTest extends NbTestCase {
44     
45     static {
46         System.setProperty ("org.openide.util.Lookup", "org.netbeans.core.windows.view.ui.StatusLineElementProviderTest$Lkp");
47     }
48     
49     static private InstanceContent ic = null;
50     static private Impl impl1, impl2;
51     static private JPanel JavaDoc statusLine;
52     
53     public StatusLineElementProviderTest(String JavaDoc name) {
54         super(name);
55     }
56     
57     public static void main(String JavaDoc[] args) {
58         TestRunner.run(new NbTestSuite(StatusLineElementProviderTest.class));
59     }
60     
61     protected boolean runInEQ () {
62         return true;
63     }
64     
65     protected void setUp() {
66         impl1 = new Impl ("First");
67         impl2 = new Impl ("Second");
68         statusLine = new JPanel JavaDoc ();
69         Lookup.getDefault ();
70     }
71     
72     public void testGetStatusLineElements () {
73         // initialy contains comp1
74
JPanel JavaDoc panel = MainWindow.getStatusLineElements (statusLine);
75         assertNotNull ("getStatusLineElements() returns a panel.", panel);
76         assertEquals ("Panel contains only one component.", 1, panel.getComponentCount ());
77         assertTrue ("Panel contains the component Comp1.", Arrays.asList (panel.getComponents ()).contains (impl1.myComponent));
78         
79         // remove impl1 from lookup
80
ic.remove (impl1);
81         panel = MainWindow.getStatusLineElements (statusLine);
82         assertNull ("getStatusLineElements() returns null, panel: " + panel, panel);
83         
84         // add impl1 back
85
ic.add (impl1);
86         panel = MainWindow.getStatusLineElements (statusLine);
87         assertNotNull ("getStatusLineElements() returns a panel.", panel);
88         assertEquals ("Panel contains only one component.", 1, panel.getComponentCount ());
89         assertTrue ("Panel contains the component Comp1.", Arrays.asList (panel.getComponents ()).contains (impl1.myComponent));
90         
91         // add impl2
92
ic.add (impl2);
93         panel = MainWindow.getStatusLineElements (statusLine);
94         assertNotNull ("getStatusLineElements() returns a panel.", panel);
95         assertEquals ("Panel contains two components.", 2, panel.getComponentCount ());
96         assertTrue ("Panel contains the component from impl1.", Arrays.asList (panel.getComponents ()).contains (impl1.myComponent));
97         assertTrue ("Panel contains the component from impl2.", Arrays.asList (panel.getComponents ()).contains (impl2.myComponent));
98     }
99     
100
101     public static final class Lkp extends AbstractLookup {
102         public Lkp () {
103             this (new InstanceContent ());
104         }
105
106         private Lkp (InstanceContent instanceContent) {
107             super (instanceContent);
108             ic = instanceContent;
109             ic.add (impl1);
110         }
111     }
112
113      private static class Impl implements StatusLineElementProvider {
114         public Component JavaDoc myComponent;
115         private String JavaDoc myId;
116         public Impl (String JavaDoc id) {
117             myId = id;
118         }
119         public Component JavaDoc getStatusLineElement() {
120             myComponent = new JLabel JavaDoc (myId);
121             return myComponent;
122         }
123      }
124      
125 }
Popular Tags