Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* Contributor(s): |
28 |
* |
29 |
* Portions Copyrighted 2007 Sun Microsystems, Inc. |
30 |
*/ |
31 |
package org.netbeans.editor; |
32 |
|
33 |
import javax.swing.text.BadLocationException; |
34 |
import javax.swing.undo.UndoManager; |
35 |
import javax.swing.undo.UndoableEdit; |
36 |
import org.netbeans.junit.NbTestCase; |
37 |
import org.openide.util.Exceptions; |
38 |
|
39 |
/** |
40 |
* @author Jan Becicka |
41 |
*/ |
42 |
public class IntegratedUndoTest extends NbTestCase { |
43 |
|
44 |
private static final String TEXT = "test"; |
45 |
|
46 |
public IntegratedUndoTest(String testName) { |
47 |
super(testName); |
48 |
} |
49 |
|
50 |
public void testUndoManager() throws Exception { |
51 |
final BaseDocument doc = new BaseDocument(BaseKit.class, false); |
52 |
UndoManagerImpl undoManager = new UndoManagerImpl(); |
53 |
doc.addUndoableEditListener(undoManager); |
54 |
doc.putProperty(UndoManager.class, undoManager); |
55 |
|
56 |
final UndoableWrapper edit = new UndoableWrapper(doc); |
57 |
doc.runAtomic(new Runnable() { |
58 |
|
59 |
@Override |
60 |
public void run() { |
61 |
doc.addUndoableEdit(edit.first()); |
62 |
try { |
63 |
//edit |
64 |
doc.insertString(0, TEXT, null); |
65 |
} catch (BadLocationException ex) { |
66 |
Exceptions.printStackTrace(ex); |
67 |
} |
68 |
doc.addUndoableEdit(edit.last()); |
69 |
} |
70 |
}); |
71 |
|
72 |
assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); |
73 |
//undo is available |
74 |
//assertEquals(true, undoManager.canUndoOrRedo()); |
75 |
assertEquals(true, undoManager.canUndo()); |
76 |
|
77 |
//redo is not |
78 |
assertEquals(false, undoManager.canRedo()); |
79 |
|
80 |
//last edit is mine |
81 |
assertEquals(edit.last(), undoManager.editToBeUndone()); |
82 |
|
83 |
assertEquals("MyUndo", undoManager.getUndoPresentationName()); |
84 |
undoManager.undo(); |
85 |
|
86 |
|
87 |
assertEquals("", org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); |
88 |
//undo is not available |
89 |
//assertEquals(true, undoManager.canUndoOrRedo()); |
90 |
assertEquals(false, undoManager.canUndo()); |
91 |
|
92 |
//redo is available |
93 |
assertEquals(true, undoManager.canRedo()); |
94 |
|
95 |
//now, there should not be any edit |
96 |
assertEquals(null, undoManager.editToBeUndone()); |
97 |
assertEquals(edit.first(), undoManager.editToBeRedone()); |
98 |
|
99 |
assertEquals("MyRedo", undoManager.getUndoOrRedoPresentationName()); |
100 |
undoManager.redo(); |
101 |
|
102 |
assertEquals(TEXT, org.netbeans.lib.editor.util.swing.DocumentUtilities.getText(doc).toString().trim()); |
103 |
//undo is available |
104 |
assertEquals(true, undoManager.canUndoOrRedo()); |
105 |
assertEquals(true, undoManager.canUndo()); |
106 |
|
107 |
//redo is not |
108 |
assertEquals(false, undoManager.canRedo()); |
109 |
|
110 |
//last edit is mine |
111 |
assertEquals(edit.last(), undoManager.editToBeUndone()); |
112 |
assertEquals("MyUndo", undoManager.getUndoPresentationName()); |
113 |
|
114 |
} |
115 |
|
116 |
private class UndoManagerImpl extends UndoManager { |
117 |
|
118 |
@Override |
119 |
public UndoableEdit editToBeRedone() { |
120 |
return super.editToBeRedone(); |
121 |
} |
122 |
|
123 |
@Override |
124 |
public UndoableEdit editToBeUndone() { |
125 |
return super.editToBeUndone(); |
126 |
} |
127 |
} |
128 |
} |
129 |
|