1 | /*
|
---|
2 | * ActionSequenzTest.cpp
|
---|
3 | *
|
---|
4 | * Created on: Dec 17, 2009
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include <cppunit/CompilerOutputter.h>
|
---|
9 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
10 | #include <cppunit/ui/text/TestRunner.h>
|
---|
11 |
|
---|
12 | #include "unittests/ActionSequenceTest.hpp"
|
---|
13 | #include "Actions/Action.hpp"
|
---|
14 | #include "Actions/ActionSequence.hpp"
|
---|
15 |
|
---|
16 | #ifdef HAVE_TESTRUNNER
|
---|
17 | #include "UnitTestMain.hpp"
|
---|
18 | #endif /*HAVE_TESTRUNNER*/
|
---|
19 |
|
---|
20 | /********************************************** Test classes **************************************/
|
---|
21 |
|
---|
22 | // Registers the fixture into the 'registry'
|
---|
23 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
24 |
|
---|
25 | void setUp();
|
---|
26 | void tearDown();
|
---|
27 |
|
---|
28 | void canUndoTest();
|
---|
29 |
|
---|
30 | /* some neccessary stubs for tests */
|
---|
31 | class canUndoActionStub : public Action
|
---|
32 | {
|
---|
33 | public:
|
---|
34 | canUndoActionStub(): Action("canUndoActionStub",false){}
|
---|
35 | virtual ~canUndoActionStub(){}
|
---|
36 |
|
---|
37 | virtual Action::state_ptr performCall(){
|
---|
38 | return Action::success;
|
---|
39 | }
|
---|
40 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
41 | return Action::success;
|
---|
42 | }
|
---|
43 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
44 | return Action::success;
|
---|
45 | }
|
---|
46 | virtual bool canUndo(){
|
---|
47 | return true;
|
---|
48 | }
|
---|
49 | virtual bool shouldUndo(){
|
---|
50 | return true;
|
---|
51 | }
|
---|
52 | };
|
---|
53 |
|
---|
54 | class cannotUndoActionStub : public Action
|
---|
55 | {
|
---|
56 | public:
|
---|
57 | cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
|
---|
58 | virtual ~cannotUndoActionStub(){}
|
---|
59 |
|
---|
60 | virtual Action::state_ptr performCall(){
|
---|
61 | return Action::success;
|
---|
62 | }
|
---|
63 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
64 | return Action::success;
|
---|
65 | }
|
---|
66 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
67 | return Action::success;
|
---|
68 | }
|
---|
69 | virtual bool canUndo(){
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | virtual bool shouldUndo(){
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 | };
|
---|
76 |
|
---|
77 | class wasCalledActionStub : public Action
|
---|
78 | {
|
---|
79 | public:
|
---|
80 | wasCalledActionStub() :
|
---|
81 | Action("wasCalledActionStub",false),
|
---|
82 | called(false)
|
---|
83 | {}
|
---|
84 | virtual ~wasCalledActionStub(){}
|
---|
85 |
|
---|
86 | virtual Action::state_ptr performCall(){
|
---|
87 | called = true;
|
---|
88 | return Action::success;
|
---|
89 | }
|
---|
90 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
91 | called = false;
|
---|
92 | return Action::success;
|
---|
93 | }
|
---|
94 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
95 | called = true;
|
---|
96 | return Action::success;
|
---|
97 | }
|
---|
98 | virtual bool canUndo(){
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | virtual bool shouldUndo(){
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 | bool wasCalled(){
|
---|
105 | return called;
|
---|
106 | }
|
---|
107 | private:
|
---|
108 | bool called;
|
---|
109 | };
|
---|
110 |
|
---|
111 | void ActionSequenceTest::setUp(){
|
---|
112 | // create some necessary stubs used in this test
|
---|
113 | positive1 = new canUndoActionStub();
|
---|
114 | positive2 = new canUndoActionStub();
|
---|
115 | negative1 = new cannotUndoActionStub();
|
---|
116 | negative2 = new cannotUndoActionStub();
|
---|
117 |
|
---|
118 | shouldCall1 = new wasCalledActionStub();
|
---|
119 | shouldCall2 = new wasCalledActionStub();
|
---|
120 | shouldNotCall1 = new wasCalledActionStub();
|
---|
121 | shouldNotCall2 = new wasCalledActionStub();
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | void ActionSequenceTest::tearDown(){
|
---|
126 | delete positive1;
|
---|
127 | delete positive2;
|
---|
128 | delete negative1;
|
---|
129 | delete negative2;
|
---|
130 |
|
---|
131 | delete shouldCall1;
|
---|
132 | delete shouldCall2;
|
---|
133 | delete shouldNotCall1;
|
---|
134 | delete shouldNotCall2;
|
---|
135 |
|
---|
136 | }
|
---|
137 |
|
---|
138 | void ActionSequenceTest::canUndoTest(){
|
---|
139 | // first section:
|
---|
140 | {
|
---|
141 | // test some combinations
|
---|
142 | {
|
---|
143 | ActionSequence *sequence = new ActionSequence();
|
---|
144 | sequence->addAction(positive1);
|
---|
145 | sequence->addAction(positive2);
|
---|
146 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
147 | delete sequence;
|
---|
148 | }
|
---|
149 | {
|
---|
150 | ActionSequence *sequence = new ActionSequence();
|
---|
151 | sequence->addAction(positive1);
|
---|
152 | sequence->addAction(negative2);
|
---|
153 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
154 | delete sequence;
|
---|
155 | }
|
---|
156 | {
|
---|
157 | ActionSequence *sequence = new ActionSequence();
|
---|
158 | sequence->addAction(negative1);
|
---|
159 | sequence->addAction(positive2);
|
---|
160 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
161 | delete sequence;
|
---|
162 | }
|
---|
163 | {
|
---|
164 | ActionSequence *sequence = new ActionSequence();
|
---|
165 | sequence->addAction(negative1);
|
---|
166 | sequence->addAction(negative2);
|
---|
167 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
168 | delete sequence;
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | // second section:
|
---|
173 | {
|
---|
174 | // empty sequence can be undone
|
---|
175 | ActionSequence *sequence = new ActionSequence();
|
---|
176 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
177 | // if only a positive action is contained it can be undone
|
---|
178 | sequence->addAction(positive1);
|
---|
179 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
180 | // the single negative action should block the process
|
---|
181 | sequence->addAction(negative1);
|
---|
182 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
183 | // after removing the negative action all is well again
|
---|
184 | sequence->removeLastAction();
|
---|
185 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
186 | delete sequence;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | void ActionSequenceTest::doesCallTest(){
|
---|
191 | ActionSequence *sequence = new ActionSequence();
|
---|
192 | sequence->addAction(shouldCall1);
|
---|
193 | sequence->addAction(shouldCall2);
|
---|
194 | sequence->addAction(shouldNotCall1);
|
---|
195 | sequence->addAction(shouldNotCall2);
|
---|
196 | sequence->removeLastAction();
|
---|
197 | sequence->removeLastAction();
|
---|
198 |
|
---|
199 | sequence->callAll();
|
---|
200 |
|
---|
201 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
202 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
203 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
204 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
205 |
|
---|
206 | delete sequence;
|
---|
207 | }
|
---|
208 |
|
---|
209 | void ActionSequenceTest::doesUndoTest(){
|
---|
210 | ActionSequence *sequence = new ActionSequence();
|
---|
211 | sequence->addAction(shouldNotCall1);
|
---|
212 | sequence->addAction(shouldNotCall2);
|
---|
213 | sequence->addAction(shouldCall1);
|
---|
214 | sequence->addAction(shouldCall2);
|
---|
215 |
|
---|
216 | ActionSequence::stateSet states = sequence->callAll();
|
---|
217 |
|
---|
218 | sequence->removeLastAction();
|
---|
219 | sequence->removeLastAction();
|
---|
220 |
|
---|
221 | sequence->undoAll(states);
|
---|
222 |
|
---|
223 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
224 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
225 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
226 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
227 |
|
---|
228 | delete sequence;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|