1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * ActionSequenzTest.cpp
|
---|
10 | *
|
---|
11 | * Created on: Dec 17, 2009
|
---|
12 | * Author: crueger
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <cppunit/CompilerOutputter.h>
|
---|
21 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
22 | #include <cppunit/ui/text/TestRunner.h>
|
---|
23 |
|
---|
24 | #include "unittests/ActionSequenceTest.hpp"
|
---|
25 | #include "Actions/Action.hpp"
|
---|
26 | #include "Actions/ActionSequence.hpp"
|
---|
27 | #include "Actions/MakroAction.hpp"
|
---|
28 | #include "Actions/ActionHistory.hpp"
|
---|
29 | #include "Actions/ActionRegistry.hpp"
|
---|
30 |
|
---|
31 | #include "DummyUI.hpp"
|
---|
32 |
|
---|
33 | #ifdef HAVE_TESTRUNNER
|
---|
34 | #include "UnitTestMain.hpp"
|
---|
35 | #endif /*HAVE_TESTRUNNER*/
|
---|
36 |
|
---|
37 | /********************************************** Test classes **************************************/
|
---|
38 |
|
---|
39 | // Registers the fixture into the 'registry'
|
---|
40 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
41 |
|
---|
42 | /* some neccessary stubs for tests */
|
---|
43 | class canUndoActionStub : public Action
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | canUndoActionStub(): Action("canUndoActionStub",false){}
|
---|
47 | virtual ~canUndoActionStub(){}
|
---|
48 |
|
---|
49 | virtual void getParametersfromValueStorage(){
|
---|
50 | }
|
---|
51 |
|
---|
52 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
53 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
54 | return dialog;
|
---|
55 | }
|
---|
56 |
|
---|
57 | virtual Action::state_ptr performCall(){
|
---|
58 | return Action::success;
|
---|
59 | }
|
---|
60 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
61 | return Action::success;
|
---|
62 | }
|
---|
63 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
64 | return Action::success;
|
---|
65 | }
|
---|
66 | virtual bool canUndo(){
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 | virtual bool shouldUndo(){
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | };
|
---|
73 |
|
---|
74 | class cannotUndoActionStub : public Action
|
---|
75 | {
|
---|
76 | public:
|
---|
77 | cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
|
---|
78 | virtual ~cannotUndoActionStub(){}
|
---|
79 |
|
---|
80 | virtual void getParametersfromValueStorage(){
|
---|
81 | }
|
---|
82 |
|
---|
83 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
84 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
85 | return dialog;
|
---|
86 | }
|
---|
87 |
|
---|
88 | virtual Action::state_ptr performCall(){
|
---|
89 | return Action::success;
|
---|
90 | }
|
---|
91 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
92 | return Action::success;
|
---|
93 | }
|
---|
94 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
95 | return Action::success;
|
---|
96 | }
|
---|
97 | virtual bool canUndo(){
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 | virtual bool shouldUndo(){
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | };
|
---|
104 |
|
---|
105 | class wasCalledActionStub : public Action
|
---|
106 | {
|
---|
107 | public:
|
---|
108 | wasCalledActionStub() :
|
---|
109 | Action("wasCalledActionStub",false),
|
---|
110 | called(false)
|
---|
111 | {}
|
---|
112 | virtual ~wasCalledActionStub(){}
|
---|
113 |
|
---|
114 | virtual void getParametersfromValueStorage(){
|
---|
115 | }
|
---|
116 |
|
---|
117 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
118 | return dialog;
|
---|
119 | }
|
---|
120 | virtual Action::state_ptr performCall(){
|
---|
121 | called = true;
|
---|
122 | return Action::success;
|
---|
123 | }
|
---|
124 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
125 | called = false;
|
---|
126 | return Action::success;
|
---|
127 | }
|
---|
128 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
129 | called = true;
|
---|
130 | return Action::success;
|
---|
131 | }
|
---|
132 | virtual bool canUndo(){
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 | virtual bool shouldUndo(){
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 | bool wasCalled(){
|
---|
139 | return called;
|
---|
140 | }
|
---|
141 | private:
|
---|
142 | bool called;
|
---|
143 | };
|
---|
144 |
|
---|
145 | void ActionSequenceTest::setUp(){
|
---|
146 | static bool hasDescriptor = false;
|
---|
147 | ActionHistory::init();
|
---|
148 | // TODO: find a way to really reset the factory to a clean state in tear-down
|
---|
149 | if(!hasDescriptor){
|
---|
150 | UIFactory::registerFactory(new DummyUIFactory::description());
|
---|
151 | hasDescriptor = true;
|
---|
152 | }
|
---|
153 | UIFactory::makeUserInterface("Dummy");
|
---|
154 | // create some necessary stubs used in this test
|
---|
155 | positive1 = new canUndoActionStub();
|
---|
156 | positive2 = new canUndoActionStub();
|
---|
157 | negative1 = new cannotUndoActionStub();
|
---|
158 | negative2 = new cannotUndoActionStub();
|
---|
159 |
|
---|
160 | shouldCall1 = new wasCalledActionStub();
|
---|
161 | shouldCall2 = new wasCalledActionStub();
|
---|
162 | shouldNotCall1 = new wasCalledActionStub();
|
---|
163 | shouldNotCall2 = new wasCalledActionStub();
|
---|
164 |
|
---|
165 | }
|
---|
166 |
|
---|
167 | void ActionSequenceTest::tearDown(){
|
---|
168 | delete positive1;
|
---|
169 | delete positive2;
|
---|
170 | delete negative1;
|
---|
171 | delete negative2;
|
---|
172 |
|
---|
173 | delete shouldCall1;
|
---|
174 | delete shouldCall2;
|
---|
175 | delete shouldNotCall1;
|
---|
176 | delete shouldNotCall2;
|
---|
177 |
|
---|
178 | ActionHistory::purgeInstance();
|
---|
179 | ActionRegistry::purgeInstance();
|
---|
180 | UIFactory::purgeInstance();
|
---|
181 | }
|
---|
182 |
|
---|
183 | void ActionSequenceTest::canUndoTest(){
|
---|
184 | // first section:
|
---|
185 | {
|
---|
186 | // test some combinations
|
---|
187 | {
|
---|
188 | ActionSequence *sequence = new ActionSequence();
|
---|
189 | sequence->addAction(positive1);
|
---|
190 | sequence->addAction(positive2);
|
---|
191 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
192 | delete sequence;
|
---|
193 | }
|
---|
194 | {
|
---|
195 | ActionSequence *sequence = new ActionSequence();
|
---|
196 | sequence->addAction(positive1);
|
---|
197 | sequence->addAction(negative2);
|
---|
198 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
199 | delete sequence;
|
---|
200 | }
|
---|
201 | {
|
---|
202 | ActionSequence *sequence = new ActionSequence();
|
---|
203 | sequence->addAction(negative1);
|
---|
204 | sequence->addAction(positive2);
|
---|
205 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
206 | delete sequence;
|
---|
207 | }
|
---|
208 | {
|
---|
209 | ActionSequence *sequence = new ActionSequence();
|
---|
210 | sequence->addAction(negative1);
|
---|
211 | sequence->addAction(negative2);
|
---|
212 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
213 | delete sequence;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | // second section:
|
---|
218 | {
|
---|
219 | // empty sequence can be undone
|
---|
220 | ActionSequence *sequence = new ActionSequence();
|
---|
221 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
222 | // if only a positive action is contained it can be undone
|
---|
223 | sequence->addAction(positive1);
|
---|
224 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
225 | // the single negative action should block the process
|
---|
226 | sequence->addAction(negative1);
|
---|
227 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
228 | // after removing the negative action all is well again
|
---|
229 | sequence->removeLastAction();
|
---|
230 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
231 | delete sequence;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | void ActionSequenceTest::doesCallTest(){
|
---|
236 | ActionSequence *sequence = new ActionSequence();
|
---|
237 | sequence->addAction(shouldCall1);
|
---|
238 | sequence->addAction(shouldCall2);
|
---|
239 | sequence->addAction(shouldNotCall1);
|
---|
240 | sequence->addAction(shouldNotCall2);
|
---|
241 | sequence->removeLastAction();
|
---|
242 | sequence->removeLastAction();
|
---|
243 |
|
---|
244 | sequence->callAll();
|
---|
245 |
|
---|
246 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
247 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
248 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
249 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
250 |
|
---|
251 | delete sequence;
|
---|
252 | }
|
---|
253 |
|
---|
254 | void ActionSequenceTest::doesUndoTest(){
|
---|
255 | ActionSequence *sequence = new ActionSequence();
|
---|
256 | wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
|
---|
257 | wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
|
---|
258 | sequence->addAction(wasCalled1);
|
---|
259 | sequence->addAction(wasCalled2);
|
---|
260 |
|
---|
261 | MakroAction act("Test MakroAction",sequence,false);
|
---|
262 |
|
---|
263 | act.call();
|
---|
264 |
|
---|
265 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
|
---|
266 | CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
|
---|
267 |
|
---|
268 | ActionHistory::getInstance().undoLast();
|
---|
269 |
|
---|
270 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
|
---|
271 | CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
|
---|
272 |
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|