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: package javax.swing.undo;
018:
019: import javax.swing.BasicSwingTestCase;
020: import javax.swing.UIManager;
021: import javax.swing.event.ChangeListener;
022: import javax.swing.event.EventListenerList;
023:
024: public class CompoundEditTest extends AbstractUndoableEditTest {
025: protected CompoundEdit ce;
026:
027: /*
028: * @see AbstractUndoableEditTest#setUp()
029: */
030: @Override
031: protected void setUp() throws Exception {
032: super .setUp();
033: ce = new CompoundEdit();
034: obj = ce;
035: }
036:
037: /*
038: * Class under test for java.lang.String toString()
039: */
040: @Override
041: public void testToString() {
042: assertNotNull(ce.toString());
043: }
044:
045: @Override
046: public void testIsSignificant() {
047: assertFalse(ce.isSignificant());
048: ce.addEdit(new TestUndoableEdit(
049: TestUndoableEdit.IS_SIGNIFICANT_FALSE));
050: assertFalse(ce.isSignificant());
051: ce.addEdit(new TestUndoableEdit(
052: TestUndoableEdit.IS_SIGNIFICANT_TRUE));
053: assertTrue(ce.isSignificant());
054: }
055:
056: @Override
057: public void testCanUndo() {
058: if (BasicSwingTestCase.isHarmony()) {
059: assertEquals(!ce.isInProgress()
060: && (isAlive(ce) && hasBeenDone(ce)), ce.canUndo());
061: }
062: assertFalse(ce.canUndo());
063: ce.end();
064: assertTrue(ce.canUndo());
065: ce.die();
066: assertFalse(ce.canUndo());
067: }
068:
069: @Override
070: public void testCanRedo() {
071: if (BasicSwingTestCase.isHarmony()) {
072: assertEquals(!ce.isInProgress()
073: && (isAlive(ce) && !hasBeenDone(ce)), ce.canRedo());
074: }
075: assertFalse(ce.canRedo());
076: ce.end();
077: ce.undo();
078: assertTrue(ce.canRedo());
079: ce.die();
080: assertFalse(ce.canRedo());
081: }
082:
083: /**
084: * Behaviour of class depends from the flag.
085: * UNDO, DIE: Checks that undo and die were called in the reverse of the
086: * order they were created.
087: * REDO: Checks that redo were called in the order they were created.
088: * NAME: getPresentationName returns string with id.
089: * UNDO_NAME: getUndoPresentationName returns string with id.
090: * REDO_NAME: getRedoPresentationName returns string with id.
091: * ADD_EDIT_TRUE: addEdit returns true.
092: * ADD_EDIT_FALSE: addEdit returns false.
093: * REPLACE_EDIT_FALSE: replaceEdit returns false.
094: * REPLACE_EDIT_TRUE: replaceEdit returns true.
095: * IS_SIGNIFICANT_TRUE: isSignificant returns true.
096: * IS_SIGNIFICANT_FALSE: isSignificant returns false.
097: * DISCARD: Sets flag dieCalled to true.
098: * UNDO_THROW_EXCEPTION: Undo throws CannotUndoException.
099: * CAN_UNDO_FALSE: canUndo returns false.
100: * CAN_REDO_FALSE: canRedo returns false.
101: */
102: protected static class TestUndoableEdit extends
103: AbstractUndoableEdit {
104: private static final long serialVersionUID = 1L;
105:
106: public static int counter = 0;
107:
108: int id;
109:
110: public static final int DIE = 1 << 0;
111:
112: public static final int UNDO = 1 << 1;
113:
114: public static final int REDO = 1 << 2;
115:
116: public static final int NAME = 1 << 3;
117:
118: public static final int UNDO_NAME = 1 << 4;
119:
120: public static final int REDO_NAME = 1 << 5;
121:
122: public static final int ADD_EDIT_TRUE = 1 << 6;
123:
124: public static final int ADD_EDIT_FALSE = 1 << 7;
125:
126: public static final int REPLACE_EDIT_TRUE = 1 << 8;
127:
128: public static final int REPLACE_EDIT_FALSE = 1 << 9;
129:
130: public static final int IS_SIGNIFICANT_TRUE = 1 << 10;
131:
132: public static final int IS_SIGNIFICANT_FALSE = 1 << 11;
133:
134: public static final int DISCARD = 1 << 12;
135:
136: public static final int UNDO_THROW_EXCEPTION = 1 << 13;
137:
138: public static final int CAN_UNDO_FALSE = 1 << 14;
139:
140: public static final int CAN_REDO_FALSE = 1 << 15;
141:
142: /**
143: * If the method die is called and flag is set to DISCARD
144: * then it equals to true.
145: */
146: protected boolean dieCalled = false;
147:
148: public int flag;
149:
150: /**
151: * If the method replaceEdit was called it equals to true.
152: */
153: public boolean isReplaceEditCalled = false;
154:
155: /**
156: * Contains all registered change listeners.
157: */
158: private EventListenerList listeners;
159:
160: public TestUndoableEdit() {
161: id = 0;
162: flag = 0;
163: }
164:
165: public TestUndoableEdit(final int flag) {
166: if ((flag & DIE) != 0 || (flag & UNDO) != 0
167: || (flag & REDO) != 0 || (flag & NAME) != 0
168: || (flag & REDO_NAME) != 0
169: || (flag & UNDO_NAME) != 0) {
170: id = counter++;
171: } else {
172: id = 0;
173: }
174: this .flag = flag;
175: listeners = new EventListenerList();
176: }
177:
178: public void addChangeListener(final ChangeListener listener) {
179: listeners.add(ChangeListener.class, listener);
180: }
181:
182: @Override
183: public void die() {
184: //System.out.println("die " + id);
185: super .die();
186: if ((flag & DIE) != 0) {
187: assertEquals(--counter, id);
188: dieCalled = true;
189: }
190: if ((flag & DISCARD) != 0) {
191: dieCalled = true;
192: }
193: }
194:
195: /**
196: * Returns the value of flag dieCalled.
197: *
198: */
199: public boolean isDieCalled() {
200: return dieCalled;
201: }
202:
203: @Override
204: public void undo() {
205: //System.out.println("undo " + id);
206: super .undo();
207: if ((flag & UNDO) != 0) {
208: assertEquals(--counter, id);
209: } else if ((flag & UNDO_THROW_EXCEPTION) != 0) {
210: throw new CannotUndoException();
211: }
212: }
213:
214: @Override
215: public void redo() {
216: //System.out.println("redo " + id);
217: super .redo();
218: if ((flag & REDO) != 0) {
219: assertEquals(counter++, id);
220: }
221: }
222:
223: @Override
224: public String getPresentationName() {
225: if ((flag & NAME) != 0) {
226: return String.valueOf(id);
227: }
228: return "";
229: }
230:
231: @Override
232: public String getRedoPresentationName() {
233: if ((flag & REDO_NAME) != 0) {
234: return String.valueOf(id);
235: }
236: return "";
237: }
238:
239: @Override
240: public String getUndoPresentationName() {
241: if ((flag & UNDO_NAME) != 0) {
242: return String.valueOf(id);
243: }
244: return "";
245: }
246:
247: @Override
248: public boolean addEdit(final UndoableEdit edit) {
249: return ((flag & ADD_EDIT_TRUE) != 0);
250: }
251:
252: @Override
253: public boolean replaceEdit(final UndoableEdit edit) {
254: isReplaceEditCalled = true;
255: return ((flag & REPLACE_EDIT_TRUE) != 0);
256: }
257:
258: @Override
259: public boolean isSignificant() {
260: //System.out.println("isSignificant " + id);
261: return (flag & IS_SIGNIFICANT_FALSE) == 0;
262: }
263:
264: @Override
265: public boolean canUndo() {
266: //System.out.println("canUndo " + id);
267: //TODO: remove super.canUndo and add UNDO flag where it necessary
268: return (flag & CAN_UNDO_FALSE) == 0
269: && ((flag & UNDO) != 0 || super .canUndo());
270: }
271:
272: @Override
273: public boolean canRedo() {
274: //System.out.println("canRedo " + id);
275: //TODO: remove super.canRedo and add REDO flag where it necessary
276: return (flag & CAN_REDO_FALSE) == 0
277: && ((flag & REDO) != 0 || super .canRedo());
278: }
279: }
280:
281: @Override
282: public void testUndo() {
283: boolean bWasException = false;
284: try {
285: ce.undo();
286: } catch (CannotUndoException e) {
287: bWasException = true;
288: }
289: assertTrue("CannotUndoException must be thrown", bWasException);
290: TestUndoableEdit.counter = 0;
291: for (int i = 0; i < 10; i++) {
292: ce.addEdit(new TestUndoableEdit(TestUndoableEdit.UNDO));
293: }
294: ce.end();
295: ce.undo();
296: }
297:
298: @Override
299: public void testRedo() {
300: boolean bWasException = false;
301: try {
302: ce.redo();
303: } catch (CannotRedoException e) {
304: bWasException = true;
305: }
306: assertTrue("CannotRedoException must be thrown", bWasException);
307: TestUndoableEdit.counter = 0;
308: for (int i = 0; i < 10; i++) {
309: ce.addEdit(new TestUndoableEdit(TestUndoableEdit.REDO));
310: }
311: ce.end();
312: ce.undo();
313: TestUndoableEdit.counter = 0;
314: ce.redo();
315: }
316:
317: @Override
318: public void testDie() {
319: ce.die();
320: TestUndoableEdit.counter = 0;
321: final int count = 10;
322: TestUndoableEdit[] edits = new TestUndoableEdit[count];
323: for (int i = 0; i < count; i++) {
324: edits[i] = new TestUndoableEdit(TestUndoableEdit.DIE);
325: ce.addEdit(edits[i]);
326: }
327: ce.die();
328: for (int i = 0; i < count; i++) {
329: assertTrue(edits[i].isDieCalled());
330: }
331: }
332:
333: public void testCompoundEdit() {
334: assertTrue(ce.isInProgress());
335: }
336:
337: public void testEnd() {
338: ce.end();
339: assertFalse(ce.isInProgress());
340: }
341:
342: public void testIsInProgress() {
343: assertTrue(ce.isInProgress());
344: ce.end();
345: assertFalse(ce.isInProgress());
346: }
347:
348: /**
349: * Original test.
350: */
351: public void testGetPresentationName01() {
352: assertEquals("", ce.getPresentationName());
353: TestUndoableEdit.counter = 0;
354: final int n = 10;
355: for (int i = 0; i < n; i++) {
356: ce.addEdit(new TestUndoableEdit(TestUndoableEdit.NAME));
357: }
358: assertEquals(String.valueOf(n - 1), ce.getPresentationName());
359: }
360:
361: /**
362: * Additional test.
363: * Tests that whatever getPresentationName() of the last edit returns -
364: * empty string in particular - the result doesn't change.
365: */
366: public void testGetPresentationName02() {
367: assertEquals("", ce.getPresentationName());
368: TestUndoableEdit.counter = 1;
369: ce.addEdit(new TestUndoableEdit(TestUndoableEdit.NAME));
370: assertEquals(String.valueOf(1), ce.getPresentationName());
371: ce.addEdit(new TestUndoableEdit());
372: assertEquals("", ce.getPresentationName());
373: }
374:
375: /**
376: * Tests that if <code>getPresentationName</code> of the last edit returns
377: * <code>null</code>, the result is empty string.
378: *
379: */
380: // Regression for HARMONY-2603
381: public void testGetPresentationName03() {
382: ce.addEdit(new TestUndoableEdit() {
383: private static final long serialVersionUID = 1L;
384:
385: @Override
386: public String getPresentationName() {
387: return null;
388: }
389: });
390: assertEquals("", ce.getPresentationName());
391: }
392:
393: @Override
394: public void testGetRedoPresentationName() {
395: assertEquals(UIManager
396: .getString("AbstractUndoableEdit.redoText"), ce
397: .getRedoPresentationName());
398: TestUndoableEdit.counter = 0;
399: final int n = 10;
400: for (int i = 0; i < n; i++) {
401: ce
402: .addEdit(new TestUndoableEdit(
403: TestUndoableEdit.REDO_NAME));
404: }
405: assertEquals(String.valueOf(n - 1), ce
406: .getRedoPresentationName());
407: }
408:
409: @Override
410: public void testGetUndoPresentationName() {
411: assertEquals(UIManager
412: .getString("AbstractUndoableEdit.undoText"), ce
413: .getUndoPresentationName());
414: TestUndoableEdit.counter = 0;
415: int n = 10;
416: for (int i = 0; i < n; i++) {
417: ce
418: .addEdit(new TestUndoableEdit(
419: TestUndoableEdit.UNDO_NAME));
420: }
421: assertEquals(String.valueOf(n - 1), ce
422: .getUndoPresentationName());
423: }
424:
425: public void testLastEdit() {
426: assertNull(ce.lastEdit());
427: TestUndoableEdit.counter = 0;
428: final int n = 10;
429: for (int i = 0; i < n; i++) {
430: ce.addEdit(new TestUndoableEdit(TestUndoableEdit.DIE));
431: }
432: assertEquals(n - 1, ((TestUndoableEdit) ce.lastEdit()).id);
433: }
434:
435: @Override
436: public void testAddEdit() {
437: TestUndoableEdit ue = new TestUndoableEdit(
438: TestUndoableEdit.ADD_EDIT_FALSE | TestUndoableEdit.UNDO
439: | TestUndoableEdit.REDO);
440: assertTrue(ce.addEdit(ue));
441: assertEquals(ue, ce.edits.elementAt(0));
442: ce.end();
443: assertEquals(ue.canUndo(), ce.canUndo());
444: assertEquals(ue.canRedo(), ue.canRedo());
445: ue.flag = TestUndoableEdit.UNDO;
446: assertEquals(ue.canUndo(), ce.canUndo());
447: assertEquals(ue.canRedo(), ue.canRedo());
448: ce = new CompoundEdit();
449: ue = new TestUndoableEdit(TestUndoableEdit.ADD_EDIT_TRUE);
450: assertTrue(ce.addEdit(ue));
451: assertEquals(ue, ce.edits.elementAt(0));
452: ue = new TestUndoableEdit(TestUndoableEdit.REPLACE_EDIT_FALSE);
453: assertTrue(ce.addEdit(ue));
454: assertTrue(ce.edits.size() == 1);
455: ((TestUndoableEdit) ce.lastEdit()).flag = TestUndoableEdit.ADD_EDIT_FALSE;
456: ue = new TestUndoableEdit(TestUndoableEdit.REPLACE_EDIT_FALSE);
457: assertTrue(ce.addEdit(ue));
458: assertEquals(ue, ce.edits.elementAt(1));
459: ue = new TestUndoableEdit(TestUndoableEdit.REPLACE_EDIT_TRUE);
460: assertTrue(ce.addEdit(ue));
461: assertTrue(ce.edits.size() == 2);
462: assertTrue(ue.isReplaceEditCalled);
463: ce.end();
464: assertTrue(ce.canUndo());
465: ce.undo();
466: assertTrue(ce.canRedo());
467: }
468:
469: public void testEditsCapacity() { // Regression for HARMONY-2649
470: assertEquals(10, ce.edits.capacity());
471: }
472: }
|