KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > AbstractMultiViewTopComponentTestCase


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
21 package org.netbeans.core.multiview;
22
23 import org.netbeans.core.api.multiview.MultiViewHandler;
24 import org.netbeans.core.api.multiview.MultiViews;
25 import org.netbeans.core.spi.multiview.MultiViewDescription;
26 import org.netbeans.core.spi.multiview.MultiViewFactory;
27
28 import java.awt.Image JavaDoc;
29 import java.io.DataOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.Serializable JavaDoc;
37 import java.util.Collection JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40 import javax.swing.JToolBar JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import junit.framework.*;
43 import org.netbeans.core.api.multiview.MultiViewPerspective;
44 import org.netbeans.core.spi.multiview.CloseOperationHandler;
45 import org.netbeans.core.spi.multiview.MultiViewElement;
46 import org.netbeans.junit.*;
47 import org.openide.awt.UndoRedo;
48 import org.openide.util.HelpCtx;
49 import org.openide.util.io.NbMarshalledObject;
50 import org.openide.util.lookup.Lookups;
51
52 import org.openide.windows.*;
53
54
55 /**
56  *
57  * @author Milos Kleint
58  */

59 public abstract class AbstractMultiViewTopComponentTestCase extends NbTestCase {
60     
61     /** Creates a new instance of SFSTest */
62     public AbstractMultiViewTopComponentTestCase(String JavaDoc name) {
63         super (name);
64     }
65     
66
67     protected boolean runInEQ () {
68         return true;
69     }
70     
71     
72     protected abstract TopComponent callFactory(MultiViewDescription[] desc, MultiViewDescription def);
73
74     protected abstract TopComponent callFactory(MultiViewDescription[] desc, MultiViewDescription def, CloseOperationHandler close);
75     
76     protected abstract Class JavaDoc getTopComponentClass();
77     
78     public void testTopComponentOpen () throws Exception JavaDoc {
79         final MVElem elem1 = new MVElem();
80         final MVElem elem2 = new MVElem();
81         final MVElem elem3 = new MVElem();
82         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
83         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
84         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
85         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
86         TopComponent tc = callFactory(descs, desc2);
87         // NOT OPENED YET.
88
assertEquals("",elem1.getLog());
89         assertEquals("",elem2.getLog());
90         
91         tc.open();
92         assertEquals("",elem1.getLog());
93         assertEquals("componentOpened-componentShowing-", elem2.getLog());
94         assertEquals("",elem3.getLog());
95         
96         tc.requestActive();
97         assertEquals("componentOpened-componentShowing-componentActivated-", elem2.getLog());
98         
99         tc.close();
100         //TODO shall the winsys also call deactivate on TC? the Dummy one doens't do it..
101
assertEquals("componentOpened-componentShowing-componentActivated-componentHidden-componentClosed-componentDeactivated-", elem2.getLog());
102     }
103
104     
105     public void testTopComponentSwitching () throws Exception JavaDoc {
106         final MVElem elem1 = new MVElem();
107         final MVElem elem2 = new MVElem();
108         final MVElem elem3 = new MVElem();
109         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
110         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
111         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
112         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
113         TopComponent tc = callFactory(descs, desc2);
114         
115         tc.open();
116         tc.requestActive();
117         assertEquals("componentOpened-componentShowing-componentActivated-", elem2.getLog());
118         // reset log to make the asserts shorter..
119
elem2.resetLog();
120         
121         TopComponent tc2 = new TopComponent();
122         tc2.open();
123         tc2.requestActive();
124         assertEquals("componentDeactivated-", elem2.getLog());
125
126         tc.requestActive();
127         assertEquals("componentDeactivated-componentActivated-", elem2.getLog());
128         
129     }
130     
131     public void testActions() throws Exception JavaDoc {
132         final MVElem elem1 = new MVElem(new Action JavaDoc[] {new Act1("act1")} );
133         final MVElem elem2 = new MVElem(new Action JavaDoc[] {new Act1("act2")} );
134         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
135         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
136         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
137         TopComponent tc = callFactory(descs, desc2);
138         
139         tc.open();
140         tc.requestActive();
141         
142         TopComponent result = TopComponent.getRegistry().getActivated();
143         Action JavaDoc[] acts = result.getActions();
144         assertNotNull(acts);
145         assertEquals(1, acts.length);
146         Object JavaDoc name = acts[0].getValue(Action.NAME);
147         assertEquals("act2", name);
148         
149         MultiViewHandler hand = MultiViews.findMultiViewHandler(tc);
150         
151         // test related hack, easy establishing a connection from Desc->perspective
152
hand.requestActive(Accessor.DEFAULT.createPerspective(desc1));
153         acts = result.getActions();
154         assertNotNull(acts);
155         assertEquals(1, acts.length);
156         name = acts[0].getValue(Action.NAME);
157         assertEquals("act1", name);
158         
159     }
160     
161     public void testGetHelpCtx() throws Exception JavaDoc {
162         final MVElem elem1 = new MVElem(new Action JavaDoc[] {new Act1("act1")} );
163         final MVElem elem2 = new MVElem(new Action JavaDoc[] {new Act1("act2")} );
164         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
165         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
166         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
167         TopComponent tc = callFactory(descs, desc2);
168         
169         tc.open();
170         HelpCtx help = tc.getHelpCtx();
171         MultiViewHandler hand = MultiViews.findMultiViewHandler(tc);
172         
173         assertNotNull(help);
174         Object JavaDoc name = help.getHelpID();
175         assertEquals(desc2, Accessor.DEFAULT.extractDescription(hand.getSelectedPerspective()));
176         assertEquals("desc2", name);
177         
178         hand.requestActive(Accessor.DEFAULT.createPerspective(desc1));
179         help = tc.getHelpCtx();
180         assertNotNull(help);
181         name = help.getHelpID();
182         assertEquals("desc1", name);
183         
184     }
185
186     public void testGetLookup() throws Exception JavaDoc {
187         final MVElem elem1 = new MVElem(new Action JavaDoc[] {new Act1("act1")} );
188         final MVElem elem2 = new MVElem(new Action JavaDoc[] {new Act1("act2")} );
189         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
190         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
191         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
192         TopComponent tc = callFactory(descs, desc2);
193         
194         tc.open();
195         Object JavaDoc result = tc.getLookup().lookup(MVElem.class);
196         assertNotNull(result);
197         assertEquals(result, elem2);
198         MultiViewHandler hand = MultiViews.findMultiViewHandler(tc);
199         
200         hand.requestActive(Accessor.DEFAULT.createPerspective(desc1));
201         result = tc.getLookup().lookup(MVElem.class);
202         assertNotNull(result);
203         assertEquals(result, elem1);
204         
205     }
206     
207     
208     
209     public void testGetUndoRedo() throws Exception JavaDoc {
210         final MVElem elem1 = new MVElem(new Action JavaDoc[] {new Act1("act1")} );
211         UndoRedo redo = new UndoRedo.Empty();
212         elem1.setUndoRedo(redo);
213         final MVElem elem2 = new MVElem(new Action JavaDoc[] {new Act1("act2")} );
214         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
215         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
216         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
217         TopComponent tc = callFactory(descs, desc2);
218         
219         tc.open();
220         UndoRedo result = tc.getUndoRedo();
221         MultiViewHandler hand = MultiViews.findMultiViewHandler(tc);
222         
223         assertNotNull(result);
224         assertTrue(redo != result);
225         
226         hand.requestActive(Accessor.DEFAULT.createPerspective(desc1));
227         result = tc.getUndoRedo();
228         assertFalse(redo.canRedo());
229         assertFalse(redo.canUndo());
230     }
231     
232     
233
234     public void testPersistenceType() throws Exception JavaDoc {
235         final MVElem elem1 = new MVElem(new Action JavaDoc[] {new Act1("act1")} );
236         final MVElem elem2 = new MVElem(new Action JavaDoc[] {new Act1("act2")} );
237         MultiViewDescription desc1 = new MVDesc("desc1", null, TopComponent.PERSISTENCE_NEVER, elem1);
238         MultiViewDescription desc2 = new MVDesc("desc2", null, TopComponent.PERSISTENCE_ONLY_OPENED, elem2);
239         MultiViewDescription desc3 = new MVDesc("desc3", null, TopComponent.PERSISTENCE_ALWAYS, elem2);
240         
241         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
242         TopComponent tc = callFactory(descs, desc2);
243         // is never persisted because the Descriptions are not serializable
244
assertEquals(TopComponent.PERSISTENCE_NEVER, tc.getPersistenceType());
245         
246
247         descs = new MultiViewDescription[] { desc1, desc2, desc3 };
248         // is never persisted because the Descriptions are not serializable
249
tc = callFactory(descs, desc2);
250         assertEquals(TopComponent.PERSISTENCE_NEVER, tc.getPersistenceType());
251
252         MultiViewDescription desc4 = new SerMVDesc("desc1", null, TopComponent.PERSISTENCE_NEVER, elem1);
253         MultiViewDescription desc5 = new SerMVDesc("desc2", null, TopComponent.PERSISTENCE_ONLY_OPENED, elem2);
254         MultiViewDescription desc6 = new SerMVDesc("desc3", null, TopComponent.PERSISTENCE_ALWAYS, elem2);
255
256         descs = new MultiViewDescription[] { desc4, desc5 };
257         tc = callFactory(descs, desc5);
258         assertEquals(TopComponent.PERSISTENCE_ONLY_OPENED, tc.getPersistenceType());
259         
260         descs = new MultiViewDescription[] { desc4, desc5, desc6 };
261         tc = callFactory(descs, desc6);
262         assertEquals(TopComponent.PERSISTENCE_ALWAYS, tc.getPersistenceType());
263
264     }
265     
266     public void testPrefferedId() throws Exception JavaDoc {
267         //how to test? cannot really access the ID..
268

269 // final MVElem elem1 = new MVElem(new Action[] {new Act1("act1")} );
270
// final MVElem elem2 = new MVElem(new Action[] {new Act1("act2")} );
271
// MultiViewDescription desc1 = new MVDesc("desc1", null, TopComponent.PERSISTENCE_NEVER, elem1);
272
// MultiViewDescription desc2 = new MVDesc("desc2", null, TopComponent.PERSISTENCE_ONLY_OPENED, elem2);
273
// MultiViewDescription desc3 = new MVDesc("desc3", null, TopComponent.PERSISTENCE_ALWAYS, elem2);
274
//
275
// MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2 };
276
// TopComponent tc = callFactory(descs, desc2);
277

278     }
279
280     
281     
282
283     
284     
285     
286 // -------------------------------------------------------------------------------
287
// *******************************************************************************
288
// subclasses
289
// *******************************************************************************
290
// -------------------------------------------------------------------------------
291

292     public static class SerMVDesc extends MVDesc implements Serializable JavaDoc {
293         private static final long serialVersionUID =-3126744916624172415L;
294         
295         public SerMVDesc() {
296             super();
297         }
298         
299         SerMVDesc(String JavaDoc name, Image JavaDoc img, int perstype, MultiViewElement elem) {
300             super(name, img, perstype, elem);
301         }
302         
303         private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
304             out.writeUTF(name);
305             out.writeInt(type);
306             out.writeObject(img);
307         }
308         private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
309             name = in.readUTF();
310             type = in.readInt();
311             img = (Image JavaDoc)in.readObject();
312         }
313         
314     }
315     
316     public static class SerMVElem extends MVElem implements Serializable JavaDoc {
317         private static final long serialVersionUID =-3126744316624172415L;
318         
319         public String JavaDoc deserializeTest;
320         
321         public SerMVElem() {
322             super();
323         }
324         
325         private void writeObject(java.io.ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
326             out.writeUTF(deserializeTest);
327         }
328         private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
329             deserializeTest = in.readUTF();
330         }
331         
332     }
333  
334     
335     public static class SerCloseHandler implements Serializable JavaDoc, CloseOperationHandler {
336         private static final long serialVersionUID =-3126744916624172415L;
337         
338         public String JavaDoc serValue;
339         
340         private SerCloseHandler() {
341         }
342         
343         public SerCloseHandler(String JavaDoc value) {
344             serValue = value;
345         }
346         
347         public boolean resolveCloseOperation(org.netbeans.core.spi.multiview.CloseOperationState[] elements) {
348             if (serValue != null) {
349                 return true;
350             }
351             else throw new IllegalStateException JavaDoc("Badly initialized or deserialized");
352         }
353         
354     }
355     
356     protected static class Act1 implements Action JavaDoc {
357         private String JavaDoc name;
358         
359         Act1(String JavaDoc name) {
360             this.name = name;
361         }
362         
363         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
364         }
365         
366         public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
367         }
368         
369         public Object JavaDoc getValue(String JavaDoc key) {
370             if (Action.NAME.equals(key)) {
371                 return name;
372             }
373             return null;
374         }
375         
376         public boolean isEnabled() {
377             return true;
378         }
379         
380         public void putValue(String JavaDoc key, Object JavaDoc value) {
381         }
382         
383         public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
384         }
385         
386         public void setEnabled(boolean b) {
387         }
388         
389     }
390     
391 }
392
393
Popular Tags