1 2 package org.netbeans.modules.editor; 3 4 import javax.swing.undo.UndoManager ; 5 import org.netbeans.editor.BaseDocument; 6 7 12 public class DocumentUndoTest extends BaseDocumentUnitTestCase { 13 14 private UndoManager undoManager; 15 16 public DocumentUndoTest(String testMethodName) { 17 super(testMethodName); 18 19 } 20 21 protected void setUp() throws Exception { 22 super.setUp(); 23 24 undoManager = new UndoManager (); 25 getDocument().addUndoableEditListener(undoManager); 26 } 27 28 public void testUndoWordAtOnce() throws Exception { 29 insertByAtomicChars(0, "abc"); 30 undoManager.undo(); 31 assertDocumentText("Expected empty document", ""); 32 } 33 34 public void testUndoSecondWordFromTwo() throws Exception { 35 insertByAtomicChars(0, "abc def"); 36 undoManager.undo(); 37 assertDocumentText("Expected second word undone", "abc "); 38 } 39 40 public void testUndoAtomicThenNonAtomic() throws Exception { 41 insertByAtomicChars(0, "a b"); 42 getDocument().insertString(3, "c", null); 43 undoManager.undo(); 44 assertDocumentText("Expected second word undone", "a "); 45 } 46 47 public void testUndoNonAtomicThenAtomic() throws Exception { 48 getDocument().insertString(0, "a", null); 49 insertByAtomicChars(1, "bc"); 50 undoManager.undo(); 51 assertDocumentText("Expected empty document", ""); 52 } 53 54 public void testUndoTwoNonAtomic() throws Exception { 55 getDocument().insertString(0, "a", null); 56 getDocument().insertString(1, "b", null); 57 undoManager.undo(); 58 assertDocumentText("Expected empty document", ""); 59 } 60 61 private void insertByAtomicChars(int offset, String text) throws Exception { 62 BaseDocument doc = getDocument(); 63 for (int i = 0; i < text.length(); i++) { 64 doc.atomicLock(); 65 try { 66 doc.insertString(offset + i, text.substring(i, i + 1), null); 67 } finally { 68 doc.atomicUnlock(); 69 } 70 } 71 } 72 73 } 74 | Popular Tags |