1 19 20 21 package org.netbeans.core.multiview; 22 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.event.ChangeEvent ; 26 import javax.swing.event.ChangeListener ; 27 import javax.swing.undo.CannotRedoException ; 28 import javax.swing.undo.CannotUndoException ; 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 47 public class MultiViewElementTest extends NbTestCase { 48 49 50 public MultiViewElementTest(String name) { 51 super (name); 52 } 53 54 57 public static void main(java.lang.String [] 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 { 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 assertEquals("",elem1.getLog()); 85 assertEquals("",elem2.getLog()); 86 87 tc.open(); 88 assertEquals("componentOpened-componentShowing-", elem1.getLog()); 89 assertEquals("",elem2.getLog()); 90 91 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 93 94 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 { 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 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 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 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 { 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 { 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 MultiViewHandler handler = MultiViews.findMultiViewHandler(tc); 227 228 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 listeners = new ArrayList (); 238 public boolean undo = true; 239 public boolean redo = true; 240 241 public void addChangeListener(ChangeListener 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 getRedoPresentationName() { 251 return "String"; 252 } 253 public String getUndoPresentationName() { 254 return "String2"; 255 } 256 public void redo() throws CannotRedoException { 257 } 258 public void removeChangeListener(ChangeListener l) { 259 listeners.remove(l); 260 } 261 public void undo() throws CannotUndoException { 262 } 263 } 264 265 private class ChangeListenerImpl implements ChangeListener { 266 public int count = 0; 267 public void stateChanged(ChangeEvent e) { 268 count++; 269 } 270 271 } 272 273 } 274 275 | Popular Tags |