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