1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id$
29 */
30
31 package palmed.edit.selection;
32
33 import palmed.edit.selection.IAction;
34 import palmed.edit.selection.UndoManager;
35 import palmed.util.EasyMockTestCase;
36
37 /***
38 * UndoRedoManager test case.
39 *
40 * @author Mathieu Champlon
41 * @version $Revision$ $Date$
42 */
43 public class UndoManagerTest extends EasyMockTestCase
44 {
45 private static final int STACK_SIZE = 2;
46 /***
47 * Tested object.
48 */
49 private UndoManager manager_;
50 /***
51 * Mock objects.
52 */
53 private IAction mockAction_;
54
55 protected void setUp()
56 {
57 manager_ = new UndoManager( STACK_SIZE );
58 mockAction_ = (IAction)createMock( IAction.class );
59 }
60
61 public void testUndoWhileNothingToUndoIsNoOp()
62 {
63 manager_.undo();
64 }
65
66 public void testRedoWhileNothingToRedoIsNoOp()
67 {
68 manager_.redo();
69 }
70
71 public void testAddedActionIsUndoneOnceOnUndo()
72 {
73 manager_.add( mockAction_ );
74 reset();
75 mockAction_.undo();
76 replay();
77 manager_.undo();
78 verify();
79 manager_.undo();
80 }
81
82 public void testUndoneActionIsRedoneOnceOnRedo()
83 {
84 manager_.add( mockAction_ );
85 manager_.undo();
86 reset();
87 mockAction_.redo();
88 replay();
89 manager_.redo();
90 verify();
91 manager_.redo();
92 }
93
94 public void testRedoActionsAreRemovedUponActionAdd()
95 {
96 manager_.add( mockAction_ );
97 manager_.undo();
98 manager_.add( mockAction_ );
99 reset();
100 manager_.redo();
101 }
102
103 public void testAddActionOverStackSizeRemovesTheFirstOne()
104 {
105 manager_.add( (IAction)createMock( IAction.class ) );
106 manager_.add( mockAction_ );
107 manager_.add( mockAction_ );
108 reset();
109 mockAction_.undo();
110 mockAction_.undo();
111 replay();
112 manager_.undo();
113 manager_.undo();
114 manager_.undo();
115 }
116
117 public void testAddTwoMergableActionsDiscardsTheSecondOne()
118 {
119 final IAction mockOtherAction = (IAction)createMock( IAction.class );
120 manager_.add( mockAction_ );
121 reset();
122 mockAction_.merge( mockOtherAction );
123 control( mockAction_ ).setReturnValue( true );
124 replay();
125 manager_.add( mockOtherAction );
126 verify();
127 reset();
128 mockAction_.undo();
129 replay();
130 manager_.undo();
131 manager_.undo();
132 }
133 }