001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.lang.reflect.Field;
023: import javax.swing.BasicSwingTestCase;
024: import javax.swing.undo.CannotRedoException;
025: import javax.swing.undo.CannotUndoException;
026: import javax.swing.undo.UndoableEdit;
027: import junit.framework.TestCase;
028:
029: public class GapContent_UndoTest extends TestCase {
030: protected AbstractDocument.Content content;
031:
032: protected UndoableEdit insertEdit;
033:
034: @Override
035: protected void setUp() throws Exception {
036: content = new GapContent();
037: insertEdit = content.insertString(0, "01234");
038: }
039:
040: public void testUndoInsertEnd() throws BadLocationException {
041: UndoableEdit ue = content.insertString(5, "567");
042: ue.undo();
043: assertEquals("01234\n", content.getString(0, content.length()));
044: }
045:
046: public void testUndoInsertStart() throws BadLocationException {
047: UndoableEdit ue = content.insertString(0, "321");
048: ue.undo();
049: assertEquals("01234\n", content.getString(0, content.length()));
050: }
051:
052: public void testUndoInsertMiddle() throws BadLocationException {
053: UndoableEdit ue = content.insertString(3, "999");
054: ue.undo();
055: assertEquals("01234\n", content.getString(0, content.length()));
056: }
057:
058: public void testUndoRemoveEnd() throws BadLocationException {
059: UndoableEdit ue = content.remove(3, 2);
060: ue.undo();
061: assertEquals("01234\n", content.getString(0, content.length()));
062: }
063:
064: public void testUndoRemoveStart() throws BadLocationException {
065: UndoableEdit ue = content.remove(0, 2);
066: ue.undo();
067: assertEquals("01234\n", content.getString(0, content.length()));
068: }
069:
070: public void testUndoRemoveMiddle() throws BadLocationException {
071: UndoableEdit ue = content.remove(3, 2);
072: ue.undo();
073: assertEquals("01234\n", content.getString(0, content.length()));
074: }
075:
076: public void testUndoPresentationInsertName() {
077: final String name = BasicSwingTestCase.isHarmony() ? "addition"
078: : "";
079: assertEquals(name, insertEdit.getPresentationName());
080: }
081:
082: public void testUndoPresentationRemoveName()
083: throws BadLocationException {
084: final UndoableEdit ue = content.remove(0, 1);
085: final String name = BasicSwingTestCase.isHarmony() ? "deletion"
086: : "";
087: assertEquals(name, ue.getPresentationName());
088: }
089:
090: /**
091: * @return DocumentEdit.text
092: */
093: private static String getDEText(final UndoableEdit ue) {
094: try {
095: Field f = ue.getClass().getSuperclass().getDeclaredField(
096: "text");
097: f.setAccessible(true);
098: return (String) (f.get(ue));
099: } catch (IllegalAccessException e) {
100: fail(e.getMessage());
101: } catch (NoSuchFieldException e) {
102: fail(e.getMessage());
103: }
104: return null;
105: }
106:
107: private static Object getDEUndoPos(final UndoableEdit ue) {
108: try {
109: Field f = ue.getClass().getSuperclass().getDeclaredField(
110: "undoPos");
111: f.setAccessible(true);
112: return f.get(ue);
113: } catch (IllegalAccessException e) {
114: fail(e.getMessage());
115: } catch (NoSuchFieldException e) {
116: fail(e.getMessage());
117: }
118: return null;
119: }
120:
121: public void testDieText() {
122: if (BasicSwingTestCase.isHarmony()) {
123: insertEdit.die();
124: assertNull(getDEText(insertEdit));
125: }
126: }
127:
128: public void testDiePos() {
129: if (BasicSwingTestCase.isHarmony()) {
130: insertEdit.die();
131: assertNull(getDEUndoPos(insertEdit));
132: }
133: }
134:
135: public void testDieCanRedo() {
136: insertEdit.die();
137: assertFalse(insertEdit.canRedo());
138: }
139:
140: public void testDieCanUndo() {
141: insertEdit.die();
142: assertFalse(insertEdit.canUndo());
143: }
144:
145: public void testCanRedo() {
146: assertFalse(insertEdit.canRedo());
147: }
148:
149: public void testCanRedoUndone() {
150: insertEdit.undo();
151: assertTrue(insertEdit.canRedo());
152: }
153:
154: public void testCanUndo() {
155: assertTrue(insertEdit.canUndo());
156: }
157:
158: public void testCanUndoUndone() {
159: insertEdit.undo();
160: assertFalse(insertEdit.canUndo());
161: }
162:
163: public void testCantUndo() {
164: insertEdit.undo();
165: try {
166: insertEdit.undo();
167: fail("CannotUndoException should be thrown");
168: } catch (CannotUndoException e) {
169: }
170: }
171:
172: public void testCantRedo() {
173: try {
174: insertEdit.redo();
175: fail("CannotRedoException should be thrown");
176: } catch (CannotRedoException e) {
177: }
178: }
179: }
|