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