KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.event.ChangeEvent JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import javax.swing.undo.CannotRedoException JavaDoc;
28 import javax.swing.undo.CannotUndoException JavaDoc;
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31 import junit.textui.TestRunner;
32 import org.netbeans.core.api.multiview.MultiViewHandler;
33 import org.netbeans.core.api.multiview.MultiViews;
34 import org.netbeans.core.spi.multiview.MultiViewDescription;
35 import org.netbeans.core.spi.multiview.MultiViewFactory;
36 import org.netbeans.junit.NbTestCase;
37 import org.netbeans.junit.NbTestSuite;
38 import org.openide.awt.UndoRedo;
39 import org.openide.windows.TopComponent;
40
41
42
43 /**
44  *
45  * @author Milos Kleint
46  */

47 public class MultiViewElementTest extends NbTestCase {
48     
49     /** Creates a new instance of SFSTest */
50     public MultiViewElementTest(String JavaDoc name) {
51         super (name);
52     }
53     
54     /**
55      * @param args the command line arguments
56      */

57     public static void main(java.lang.String JavaDoc[] args) {
58         TestRunner.run(suite());
59     }
60     
61     public static Test suite() {
62         TestSuite suite = new NbTestSuite(MultiViewElementTest.class);
63         
64         return suite;
65     }
66
67     protected boolean runInEQ () {
68         return true;
69     }
70     
71     
72     public void testRequestVisible() throws Exception JavaDoc {
73         MVElem elem1 = new MVElem();
74         MVElem elem2 = new MVElem();
75         MVElem elem3 = new MVElem();
76         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
77         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
78         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
79         
80         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
81         TopComponent tc = MultiViewFactory.createMultiView(descs, desc1);
82         
83         // NOT OPENED YET.
84
assertEquals("",elem1.getLog());
85         assertEquals("",elem2.getLog());
86         
87         tc.open();
88         assertEquals("componentOpened-componentShowing-", elem1.getLog());
89         assertEquals("",elem2.getLog());
90
91         // initilize the elements..
92
MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
93         
94         // test related hack, easy establishing a connection from Desc->perspective
95
Accessor.DEFAULT.createPerspective(desc2);
96         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc2));
97         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
98         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc1));
99         elem1.resetLog();
100         elem2.resetLog();
101         elem3.resetLog();
102         
103         elem2.doRequestVisible();
104         assertEquals("componentHidden-", elem1.getLog());
105         assertEquals("componentShowing-", elem2.getLog());
106         assertEquals("", elem3.getLog());
107         
108         elem3.doRequestVisible();
109         assertEquals("componentHidden-", elem1.getLog());
110         assertEquals("componentShowing-componentHidden-", elem2.getLog());
111         assertEquals("componentShowing-", elem3.getLog());
112         
113         elem1.doRequestVisible();
114         assertEquals("componentShowing-componentHidden-", elem3.getLog());
115         assertEquals("componentHidden-componentShowing-", elem1.getLog());
116         
117     }
118
119     
120     public void testRequestActive() throws Exception JavaDoc {
121         MVElem elem1 = new MVElem();
122         MVElem elem2 = new MVElem();
123         MVElem elem3 = new MVElem();
124         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
125         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
126         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
127         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
128         TopComponent tc = MultiViewFactory.createMultiView(descs, desc2);
129
130         // NOT OPENED YET.
131
assertEquals("",elem1.getLog());
132         assertEquals("",elem2.getLog());
133         
134         tc.open();
135         tc.requestActive();
136         assertEquals("",elem1.getLog());
137         assertEquals("componentOpened-componentShowing-componentActivated-", elem2.getLog());
138         assertEquals("",elem3.getLog());
139         
140         // initilize the elements..
141
// test related hack, easy establishing a connection from Desc->perspective
142
MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
143         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc1));
144         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
145         handler.requestActive(Accessor.DEFAULT.createPerspective(desc2));
146         elem1.resetLog();
147         elem2.resetLog();
148         elem3.resetLog();
149         
150         elem1.doRequestActive();
151         assertEquals("componentShowing-componentActivated-", elem1.getLog());
152         assertEquals("componentDeactivated-componentHidden-", elem2.getLog());
153         assertEquals("",elem3.getLog());
154         
155         // do request active the same component, nothing should happen.
156
elem1.doRequestActive();
157         assertEquals("componentShowing-componentActivated-", elem1.getLog());
158         assertEquals("componentDeactivated-componentHidden-", elem2.getLog());
159         assertEquals("",elem3.getLog());
160         
161     }
162     
163     public void testUndoRedo() throws Exception JavaDoc {
164         UndoRedoImpl redo1 = new UndoRedoImpl();
165         redo1.undo = false;
166         UndoRedoImpl redo2 = new UndoRedoImpl();
167         redo2.redo = false;
168         ChangeListenerImpl changeList = new ChangeListenerImpl();
169         MVElem elem1 = new MVElem();
170         elem1.setUndoRedo(redo1);
171         MVElem elem2 = new MVElem();
172         elem2.setUndoRedo(redo2);
173         MVElem elem3 = new MVElem();
174         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
175         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
176         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
177         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
178         TopComponent tc = MultiViewFactory.createMultiView(descs, desc2);
179
180         UndoRedo result = tc.getUndoRedo();
181         assertNotNull(result);
182         assertFalse(result.canRedo());
183         assertTrue(result.canUndo());
184         result.addChangeListener(changeList);
185         assertEquals(1, redo2.listeners.size());
186         tc.open();
187         tc.requestActive();
188         assertEquals(0, changeList.count);
189         
190         MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
191         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc1));
192         
193         assertTrue(result.canRedo());
194         assertFalse(result.canUndo());
195         assertEquals(1, redo1.listeners.size());
196         assertEquals(0, redo2.listeners.size());
197         assertEquals(1, changeList.count);
198         
199         handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
200         assertFalse(result.canRedo());
201         assertFalse(result.canUndo());
202         assertEquals(0, redo2.listeners.size());
203         assertEquals(0, redo1.listeners.size());
204         assertEquals(2, changeList.count);
205         
206     }
207     
208     public void testUpdateTitle() throws Exception JavaDoc {
209         MVElem elem1 = new MVElem();
210         MVElem elem2 = new MVElem();
211         MVElem elem3 = new MVElem();
212         MultiViewDescription desc1 = new MVDesc("desc1", null, 0, elem1);
213         MultiViewDescription desc2 = new MVDesc("desc2", null, 0, elem2);
214         MultiViewDescription desc3 = new MVDesc("desc3", null, 0, elem3);
215         MultiViewDescription[] descs = new MultiViewDescription[] { desc1, desc2, desc3 };
216         TopComponent tc = MultiViewFactory.createMultiView(descs, desc2);
217
218         tc.open();
219         assertEquals(null, tc.getDisplayName());
220         
221         
222         elem2.observer.updateTitle("test1");
223         assertEquals("test1", tc.getDisplayName());
224         
225         // switch to desc3 to initilize the element..
226
MultiViewHandler handler = MultiViews.findMultiViewHandler(tc);
227
228         // test related hack, easy establishing a connection from Desc->perspective
229
handler.requestVisible(Accessor.DEFAULT.createPerspective(desc3));
230         
231         elem3.observer.updateTitle("test2");
232         assertEquals("test2", tc.getDisplayName());
233         
234     }
235     
236     private class UndoRedoImpl implements UndoRedo {
237         public List JavaDoc listeners = new ArrayList JavaDoc();
238         public boolean undo = true;
239         public boolean redo = true;
240         
241         public void addChangeListener(ChangeListener JavaDoc l) {
242             listeners.add(l);
243         }
244         public boolean canRedo() {
245             return redo;
246         }
247         public boolean canUndo() {
248             return undo;
249         }
250         public String JavaDoc getRedoPresentationName() {
251             return "String";
252         }
253         public String JavaDoc getUndoPresentationName() {
254             return "String2";
255         }
256         public void redo() throws CannotRedoException JavaDoc {
257         }
258         public void removeChangeListener(ChangeListener JavaDoc l) {
259             listeners.remove(l);
260         }
261         public void undo() throws CannotUndoException JavaDoc {
262         }
263     }
264     
265     private class ChangeListenerImpl implements ChangeListener JavaDoc {
266         public int count = 0;
267         public void stateChanged(ChangeEvent JavaDoc e) {
268             count++;
269         }
270         
271     }
272     
273 }
274
275
Popular Tags