1 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 ; 29 import java.io.DataOutputStream ; 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.ObjectInputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.io.Serializable ; 37 import java.util.Collection ; 38 import javax.swing.Action ; 39 import javax.swing.JPanel ; 40 import javax.swing.JToolBar ; 41 import javax.swing.SwingUtilities ; 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 59 public abstract class AbstractMultiViewTopComponentTestCase extends NbTestCase { 60 61 62 public AbstractMultiViewTopComponentTestCase(String 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 getTopComponentClass(); 77 78 public void testTopComponentOpen () throws Exception { 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 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 assertEquals("componentOpened-componentShowing-componentActivated-componentHidden-componentClosed-componentDeactivated-", elem2.getLog()); 102 } 103 104 105 public void testTopComponentSwitching () throws Exception { 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 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 { 132 final MVElem elem1 = new MVElem(new Action [] {new Act1("act1")} ); 133 final MVElem elem2 = new MVElem(new Action [] {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 [] acts = result.getActions(); 144 assertNotNull(acts); 145 assertEquals(1, acts.length); 146 Object name = acts[0].getValue(Action.NAME); 147 assertEquals("act2", name); 148 149 MultiViewHandler hand = MultiViews.findMultiViewHandler(tc); 150 151 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 { 162 final MVElem elem1 = new MVElem(new Action [] {new Act1("act1")} ); 163 final MVElem elem2 = new MVElem(new Action [] {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 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 { 187 final MVElem elem1 = new MVElem(new Action [] {new Act1("act1")} ); 188 final MVElem elem2 = new MVElem(new Action [] {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 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 { 210 final MVElem elem1 = new MVElem(new Action [] {new Act1("act1")} ); 211 UndoRedo redo = new UndoRedo.Empty(); 212 elem1.setUndoRedo(redo); 213 final MVElem elem2 = new MVElem(new Action [] {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 { 235 final MVElem elem1 = new MVElem(new Action [] {new Act1("act1")} ); 236 final MVElem elem2 = new MVElem(new Action [] {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 assertEquals(TopComponent.PERSISTENCE_NEVER, tc.getPersistenceType()); 245 246 247 descs = new MultiViewDescription[] { desc1, desc2, desc3 }; 248 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 { 267 269 278 } 279 280 281 282 283 284 285 286 292 public static class SerMVDesc extends MVDesc implements Serializable { 293 private static final long serialVersionUID =-3126744916624172415L; 294 295 public SerMVDesc() { 296 super(); 297 } 298 299 SerMVDesc(String name, Image img, int perstype, MultiViewElement elem) { 300 super(name, img, perstype, elem); 301 } 302 303 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 304 out.writeUTF(name); 305 out.writeInt(type); 306 out.writeObject(img); 307 } 308 private void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException { 309 name = in.readUTF(); 310 type = in.readInt(); 311 img = (Image )in.readObject(); 312 } 313 314 } 315 316 public static class SerMVElem extends MVElem implements Serializable { 317 private static final long serialVersionUID =-3126744316624172415L; 318 319 public String deserializeTest; 320 321 public SerMVElem() { 322 super(); 323 } 324 325 private void writeObject(java.io.ObjectOutputStream out) throws IOException { 326 out.writeUTF(deserializeTest); 327 } 328 private void readObject(java.io.ObjectInputStream in) throws IOException , ClassNotFoundException { 329 deserializeTest = in.readUTF(); 330 } 331 332 } 333 334 335 public static class SerCloseHandler implements Serializable , CloseOperationHandler { 336 private static final long serialVersionUID =-3126744916624172415L; 337 338 public String serValue; 339 340 private SerCloseHandler() { 341 } 342 343 public SerCloseHandler(String 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 ("Badly initialized or deserialized"); 352 } 353 354 } 355 356 protected static class Act1 implements Action { 357 private String name; 358 359 Act1(String name) { 360 this.name = name; 361 } 362 363 public void actionPerformed(java.awt.event.ActionEvent e) { 364 } 365 366 public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) { 367 } 368 369 public Object getValue(String 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 key, Object value) { 381 } 382 383 public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) { 384 } 385 386 public void setEnabled(boolean b) { 387 } 388 389 } 390 391 } 392 393 | Popular Tags |