KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > MenuBarTest


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.awt;
21
22 import java.awt.event.ContainerEvent JavaDoc;
23 import java.awt.event.ContainerListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import javax.swing.JMenu JavaDoc;
28 import org.netbeans.junit.Log;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.actions.OpenAction;
31 import org.openide.cookies.InstanceCookie;
32 import org.openide.filesystems.FileSystem;
33 import org.openide.loaders.*;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.Repository;
37 import org.openide.nodes.Node;
38 import org.openide.util.HelpCtx;
39 import org.openide.util.actions.CallbackSystemAction;
40
41 /**
42  *
43  * @author Jaroslav Tulach
44  */

45 public class MenuBarTest extends NbTestCase implements ContainerListener JavaDoc {
46     private DataFolder df;
47     private MenuBar mb;
48     
49     private int add;
50     private int remove;
51     
52     public MenuBarTest(String JavaDoc testName) {
53         super(testName);
54     }
55     
56     protected Level JavaDoc logLevel() {
57         return Level.FINE;
58     }
59
60     protected void setUp() throws Exception JavaDoc {
61         FileObject fo = FileUtil.createFolder(
62             Repository.getDefault().getDefaultFileSystem().getRoot(),
63             "Folder" + getName()
64         );
65         df = DataFolder.findFolder(fo);
66         mb = new MenuBar(df);
67         mb.waitFinished();
68     }
69
70     protected void tearDown() throws Exception JavaDoc {
71     }
72     
73     public void testAllInstances() throws Exception JavaDoc {
74         InstanceCookie[] ics = new InstanceCookie[] {
75             new IC(false),
76             new IC(true)
77         };
78         MenuBar.allInstances(ics, new ArrayList JavaDoc<Object JavaDoc>());
79     }
80     
81     private static class IC implements InstanceCookie {
82         private boolean throwing;
83         IC(boolean throwing) {
84             this.throwing = throwing;
85         }
86         public String JavaDoc instanceName() {
87             return "dummy";
88         }
89
90         public Class JavaDoc<?> instanceClass() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
91             return Object JavaDoc.class;
92         }
93
94         public Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
95             if (throwing) {
96                 Exception JavaDoc e = new Exception JavaDoc("original");
97                 throw (IOException JavaDoc) new IOException JavaDoc("inited").initCause(e);
98             }
99             return new Object JavaDoc();
100         }
101     }
102
103     public void testHowManyRepaintsPerOneChangeAreThere() throws Exception JavaDoc {
104         mb.addContainerListener(this);
105         assertEquals("No children now", 0, mb.getComponentCount());
106         
107         class Atom implements FileSystem.AtomicAction {
108             FileObject m1, m2;
109             
110             public void run() throws IOException JavaDoc {
111                 m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1");
112                 m2 = FileUtil.createFolder(df.getPrimaryFile(), "m2");
113             }
114         }
115         Atom atom = new Atom();
116         df.getPrimaryFile().getFileSystem().runAtomicAction(atom);
117         mb.waitFinished();
118         
119         assertEquals("Two children there", 2, mb.getComponentCount());
120         
121         assertEquals("No removals", 0, remove);
122         assertEquals("Two additions", 2, add);
123         
124         DataFolder f1 = DataFolder.findFolder(atom.m1);
125         InstanceDataObject.create(f1, "Kuk", OpenAction.class);
126         mb.waitFinished();
127         
128         assertEquals("Two children there", 2, mb.getComponentCount());
129         Object JavaDoc o1 = mb.getComponent(0);
130         if (!(o1 instanceof JMenu JavaDoc)) {
131             fail("It has to be menu: " + o1);
132         }
133         JMenu JavaDoc m1 = (JMenu JavaDoc)o1;
134         // simulate expansion in the menu
135
m1.setSelected(true);
136         java.awt.Component JavaDoc[] content = m1.getPopupMenu().getComponents();
137         assertEquals("Now it has one child", 1, content.length);
138         
139         assertEquals("Still No removals in MenuBar", 0, remove);
140         assertEquals("Still Two additions in MenuBar", 2, add);
141     }
142
143     
144     public void testMenusAreResolvedLazilyUntilTheyAreReallyNeeded() throws Exception JavaDoc {
145         mb.addContainerListener(this);
146         assertEquals("No children now", 0, mb.getComponentCount());
147         
148         class Atom implements FileSystem.AtomicAction {
149             FileObject m1, m2;
150             
151             public void run() throws IOException JavaDoc {
152                 m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1");
153                 DataFolder f1 = DataFolder.findFolder(m1);
154                 InstanceDataObject.create(f1, "X", MyAction.class);
155             }
156         }
157         Atom atom = new Atom();
158         df.getPrimaryFile().getFileSystem().runAtomicAction(atom);
159         mb.waitFinished();
160         
161         assertEquals("One submenu is there", 1, mb.getComponentCount());
162         
163         assertEquals("No removals", 0, remove);
164         assertEquals("One addition", 1, add);
165         
166         Object JavaDoc o1 = mb.getComponent(0);
167         if (!(o1 instanceof JMenu JavaDoc)) {
168             fail("It has to be menu: " + o1);
169         }
170         JMenu JavaDoc m1 = (JMenu JavaDoc)o1;
171         
172         assertEquals("We have the menu, but the content is still not computed", 0, MyAction.counter);
173         
174         // simulate expansion in the menu
175
m1.setSelected(true);
176         java.awt.Component JavaDoc[] content = m1.getPopupMenu().getComponents();
177         assertEquals("Now it has one child", 1, content.length);
178         
179         assertEquals("Still No removals in MenuBar", 0, remove);
180         assertEquals("Still one addition in MenuBar", 1, add);
181         
182         assertEquals("And now the action is created", 1, MyAction.counter);
183     }
184     
185     public void testSurviveInvalidationOfAFolder() throws Exception JavaDoc {
186         CharSequence JavaDoc seq = Log.enable("", Level.ALL);
187         
188         
189         FileObject m1 = FileUtil.createFolder(df.getPrimaryFile(), "m1");
190         final DataFolder f1 = DataFolder.findFolder(m1);
191
192         mb.waitFinished();
193
194         JMenu JavaDoc menu;
195         {
196             Object JavaDoc o1 = mb.getComponent(0);
197             if (!(o1 instanceof JMenu JavaDoc)) {
198                 fail("It has to be menu: " + o1);
199             }
200             menu = (JMenu JavaDoc)o1;
201             assertEquals("simple name ", "m1", menu.getText());
202         }
203         
204         Node n = f1.getNodeDelegate();
205         f1.setValid(false);
206         mb.waitFinished();
207         
208         n.setName("othername");
209
210         mb.waitFinished();
211         
212         assertEquals("updated the folder is deleted now", f1.getName(), menu.getText());
213         
214         
215         
216         if (seq.toString().indexOf("fix your code") >= 0) {
217             fail("There were warnings about the use of invalid nodes: " + seq);
218         }
219     }
220     
221     public void componentAdded(ContainerEvent JavaDoc e) {
222         add++;
223     }
224
225     public void componentRemoved(ContainerEvent JavaDoc e) {
226         remove++;
227     }
228     
229     public static final class MyAction extends CallbackSystemAction {
230         public static int counter;
231         
232         public MyAction() {
233             counter++;
234         }
235
236         public String JavaDoc getName() {
237             return "MyAction";
238         }
239
240         public HelpCtx getHelpCtx() {
241             return HelpCtx.DEFAULT_HELP;
242         }
243         
244         
245     }
246 }
247
Popular Tags