1 19 20 21 package org.openide.text; 22 23 24 import java.lang.reflect.*; 25 import javax.swing.text.StyledDocument ; 26 27 import org.netbeans.junit.NbTestCase; 28 import org.netbeans.junit.NbTestSuite; 29 30 import org.openide.awt.UndoRedo; 31 import org.openide.util.RequestProcessor; 32 33 34 39 public class UndoRedoTest extends NbTestCase implements CloneableEditorSupport.Env { 40 41 private Method method; 42 43 private CES support; 44 private String content = ""; 46 private boolean valid = true; 47 private boolean modified = false; 48 private java.util.Date date = new java.util.Date (); 49 private java.util.List propL = new java.util.ArrayList (); 50 private java.beans.VetoableChangeListener vetoL; 51 52 53 54 public UndoRedoTest(Method m) { 55 super(m.getName ()); 56 method = m; 57 } 58 59 public static junit.framework.TestSuite suite () throws Exception { 60 Method[] arr = UndoRedo.Manager.class.getMethods (); 61 62 NbTestSuite suite = new NbTestSuite (); 63 for (int i = 0; i < arr.length; i++) { 64 if (arr[i].getDeclaringClass () == javax.swing.undo.UndoManager .class) { 65 suite.addTest (new UndoRedoTest (arr[i])); 66 } 67 } 68 return suite; 69 } 70 71 protected void runTest () throws Exception { 72 support = new CES (this, org.openide.util.Lookup.EMPTY); 73 74 assertNull ("The document is closed", support.getDocument ()); 75 76 UndoRedo ur = support.getUndoRedo (); 77 78 Object [] args = new Object [0]; 79 80 if (method.getParameterTypes ().length == 1) { 81 if (method.getParameterTypes ()[0] == javax.swing.undo.UndoableEdit .class) { 82 args = new Object [] { new FakeEdit () }; 83 } 84 if (method.getParameterTypes ()[0] == Integer.TYPE) { 85 args = new Object [] { new Integer (30) }; 86 } 87 } 88 89 if (! "end".equals (method.getName ())) { 90 try { 91 method.invoke (ur, args); 93 } catch (InvocationTargetException ex) { 94 if ( 95 ex.getTargetException () instanceof javax.swing.undo.CannotUndoException || 96 ex.getTargetException () instanceof javax.swing.undo.CannotRedoException 97 ) { 98 } else { 100 throw ex; 101 } 102 } 103 } 104 105 106 final StyledDocument doc = support.openDocument (); 107 doc.insertString (0, "First edit\n", null); 108 doc.insertString (1, "Second", null); 109 doc.remove (0, 5); 110 111 javax.swing.SwingUtilities.invokeAndWait (new Runnable () { 113 public void run () { 114 } 116 }); 117 118 assertTrue ("We did edits, we need to be able to do undo", ur.canUndo ()); 119 120 ur.undo (); 121 122 assertTrue ("There is something to undo", ur.canUndo ()); 123 assertTrue ("There is something to redo", ur.canRedo ()); 124 125 126 class Blocker implements javax.swing.event.DocumentListener , Runnable { 127 public long time; 128 129 public void insertUpdate(javax.swing.event.DocumentEvent e) { 130 synchronized (this) { 131 this.notify (); 132 try { 133 long time = System.currentTimeMillis (); 134 this.wait (100); 135 time = System.currentTimeMillis () - time; 136 } catch (InterruptedException ex) { 137 ex.printStackTrace(); 138 } 139 } 140 } 141 142 public void removeUpdate(javax.swing.event.DocumentEvent e) { 143 } 144 145 public void changedUpdate(javax.swing.event.DocumentEvent e) { 146 } 147 148 public void run () { 149 try { 151 doc.insertString (0, "Kuk", null); 152 } catch (javax.swing.text.BadLocationException ex) { 153 ex.printStackTrace(); 154 } 155 } 156 } 157 Blocker blocker = new Blocker (); 158 doc.addDocumentListener (blocker); 159 synchronized (blocker) { 160 RequestProcessor.getDefault ().post (blocker); 161 blocker.wait (); 162 } 163 164 try { 165 method.invoke (ur, args); 166 } catch (InvocationTargetException ex) { 167 if ( 168 ex.getTargetException () instanceof javax.swing.undo.CannotRedoException 169 ) { 170 } else { 172 throw ex; 173 } 174 } 175 176 synchronized (blocker) { 177 blocker.notify (); 178 } 179 if (blocker.time > 50) { 180 fail ("The method " + method + " should finish sooner than in " + blocker.time + " ms"); 181 } 182 } 183 184 185 189 public synchronized void addPropertyChangeListener(java.beans.PropertyChangeListener l) { 190 propL.add (l); 191 } 192 public synchronized void removePropertyChangeListener(java.beans.PropertyChangeListener l) { 193 propL.remove (l); 194 } 195 196 public synchronized void addVetoableChangeListener(java.beans.VetoableChangeListener l) { 197 assertNull ("This is the first veto listener", vetoL); 198 vetoL = l; 199 } 200 public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) { 201 assertEquals ("Removing the right veto one", vetoL, l); 202 vetoL = null; 203 } 204 205 public org.openide.windows.CloneableOpenSupport findCloneableOpenSupport() { 206 return support; 207 } 208 209 public String getMimeType() { 210 return "text/plain"; 211 } 212 213 public java.util.Date getTime() { 214 return date; 215 } 216 217 public java.io.InputStream inputStream() throws java.io.IOException { 218 return new java.io.ByteArrayInputStream (content.getBytes ()); 219 } 220 public java.io.OutputStream outputStream() throws java.io.IOException { 221 class ContentStream extends java.io.ByteArrayOutputStream { 222 public void close () throws java.io.IOException { 223 super.close (); 224 content = new String (toByteArray ()); 225 } 226 } 227 228 return new ContentStream (); 229 } 230 231 public boolean isValid() { 232 return valid; 233 } 234 235 public boolean isModified() { 236 return modified; 237 } 238 239 public void markModified() throws java.io.IOException { 240 modified = true; 241 } 242 243 public void unmarkModified() { 244 modified = false; 245 } 246 247 248 private final class CES extends CloneableEditorSupport { 249 public boolean plain; 250 251 252 public CES (Env env, org.openide.util.Lookup l) { 253 super (env, l); 254 } 255 256 protected String messageName() { 257 return "Name"; 258 } 259 260 protected String messageOpened() { 261 return "Opened"; 262 } 263 264 protected String messageOpening() { 265 return "Opening"; 266 } 267 268 protected String messageSave() { 269 return "Save"; 270 } 271 272 protected String messageToolTip() { 273 return "ToolTip"; 274 } 275 276 protected javax.swing.text.EditorKit createEditorKit() { 277 if (plain) { 278 return super.createEditorKit (); 279 } else { 280 return new NbLikeEditorKit (); 281 } 282 } 283 } 285 private static final class FakeEdit implements javax.swing.undo.UndoableEdit { 286 public boolean addEdit(javax.swing.undo.UndoableEdit anEdit) { 287 return false; 288 } 289 290 public boolean canRedo() { 291 return true; 292 } 293 294 public boolean canUndo() { 295 return true; 296 } 297 298 public void die() { 299 } 300 301 public java.lang.String getPresentationName() { 302 return ""; 303 } 304 305 public java.lang.String getRedoPresentationName() { 306 return ""; 307 } 308 309 public java.lang.String getUndoPresentationName() { 310 return ""; 311 } 312 313 public boolean isSignificant() { 314 return false; 315 } 316 317 public void redo() throws javax.swing.undo.CannotRedoException { 318 } 319 320 public boolean replaceEdit(javax.swing.undo.UndoableEdit anEdit) { 321 return true; 322 } 323 324 public void undo() throws javax.swing.undo.CannotUndoException { 325 } 326 327 } } 329 | Popular Tags |