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