1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Copyright (C) 2013 Frederik Heber. All rights reserved.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * This file is part of MoleCuilder.
|
---|
9 | *
|
---|
10 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
11 | * it under the terms of the GNU General Public License as published by
|
---|
12 | * the Free Software Foundation, either version 2 of the License, or
|
---|
13 | * (at your option) any later version.
|
---|
14 | *
|
---|
15 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
22 | */
|
---|
23 |
|
---|
24 | /*
|
---|
25 | * ActionSequenceUnitTest.cpp
|
---|
26 | *
|
---|
27 | * Created on: Dec 17, 2009
|
---|
28 | * Author: crueger
|
---|
29 | */
|
---|
30 |
|
---|
31 | // include config.h
|
---|
32 | #ifdef HAVE_CONFIG_H
|
---|
33 | #include <config.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <cppunit/CompilerOutputter.h>
|
---|
37 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
38 | #include <cppunit/ui/text/TestRunner.h>
|
---|
39 |
|
---|
40 | #include "ActionSequenceUnitTest.hpp"
|
---|
41 | #include "Actions/Action.hpp"
|
---|
42 | #include "Actions/ActionQueue.hpp"
|
---|
43 | #include "Actions/ActionSequence.hpp"
|
---|
44 | #include "Actions/MakroAction.hpp"
|
---|
45 |
|
---|
46 | #include "stubs/DummyUI.hpp"
|
---|
47 |
|
---|
48 | using namespace MoleCuilder;
|
---|
49 |
|
---|
50 | #ifdef HAVE_TESTRUNNER
|
---|
51 | #include "UnitTestMain.hpp"
|
---|
52 | #endif /*HAVE_TESTRUNNER*/
|
---|
53 |
|
---|
54 | /********************************************** Test classes **************************************/
|
---|
55 |
|
---|
56 | // Registers the fixture into the 'registry'
|
---|
57 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
58 |
|
---|
59 | /* some neccessary stubs for tests */
|
---|
60 | class canUndoActionStub : public Action
|
---|
61 | {
|
---|
62 | public:
|
---|
63 | canUndoActionStub(const ActionTrait &_trait):
|
---|
64 | Action(_trait){}
|
---|
65 | virtual ~canUndoActionStub(){}
|
---|
66 |
|
---|
67 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
68 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
69 | return dialog;
|
---|
70 | }
|
---|
71 |
|
---|
72 | virtual ActionState::ptr performCall(){
|
---|
73 | return Action::success;
|
---|
74 | }
|
---|
75 | virtual ActionState::ptr performUndo(ActionState::ptr){
|
---|
76 | return Action::success;
|
---|
77 | }
|
---|
78 | virtual ActionState::ptr performRedo(ActionState::ptr){
|
---|
79 | return Action::success;
|
---|
80 | }
|
---|
81 | virtual bool canUndo(){
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | virtual bool shouldUndo(){
|
---|
85 | return true;
|
---|
86 | }
|
---|
87 | void outputAsCLI(std::ostream &ost) const
|
---|
88 | {}
|
---|
89 | };
|
---|
90 |
|
---|
91 | class cannotUndoActionStub : public Action
|
---|
92 | {
|
---|
93 | public:
|
---|
94 | cannotUndoActionStub(const ActionTrait &_trait) :
|
---|
95 | Action(_trait){}
|
---|
96 | virtual ~cannotUndoActionStub(){}
|
---|
97 |
|
---|
98 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
99 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
100 | return dialog;
|
---|
101 | }
|
---|
102 |
|
---|
103 | virtual ActionState::ptr performCall(){
|
---|
104 | return Action::success;
|
---|
105 | }
|
---|
106 | virtual ActionState::ptr performUndo(ActionState::ptr){
|
---|
107 | return Action::success;
|
---|
108 | }
|
---|
109 | virtual ActionState::ptr performRedo(ActionState::ptr){
|
---|
110 | return Action::success;
|
---|
111 | }
|
---|
112 | virtual bool canUndo(){
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | virtual bool shouldUndo(){
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 | void outputAsCLI(std::ostream &ost) const
|
---|
119 | {}
|
---|
120 | };
|
---|
121 |
|
---|
122 | class wasCalledActionStub : public Action
|
---|
123 | {
|
---|
124 | public:
|
---|
125 | wasCalledActionStub(const ActionTrait &_trait) :
|
---|
126 | Action(_trait),
|
---|
127 | called(false)
|
---|
128 | {}
|
---|
129 | virtual ~wasCalledActionStub(){}
|
---|
130 |
|
---|
131 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
132 | return dialog;
|
---|
133 | }
|
---|
134 | virtual ActionState::ptr performCall(){
|
---|
135 | called = true;
|
---|
136 | return Action::success;
|
---|
137 | }
|
---|
138 | virtual ActionState::ptr performUndo(ActionState::ptr){
|
---|
139 | called = false;
|
---|
140 | return Action::success;
|
---|
141 | }
|
---|
142 | virtual ActionState::ptr performRedo(ActionState::ptr){
|
---|
143 | called = true;
|
---|
144 | return Action::success;
|
---|
145 | }
|
---|
146 | virtual bool canUndo(){
|
---|
147 | return true;
|
---|
148 | }
|
---|
149 | virtual bool shouldUndo(){
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 | void outputAsCLI(std::ostream &ost) const
|
---|
153 | {}
|
---|
154 | bool wasCalled(){
|
---|
155 | return called;
|
---|
156 | }
|
---|
157 | private:
|
---|
158 | bool called;
|
---|
159 | };
|
---|
160 |
|
---|
161 | void ActionSequenceTest::setUp(){
|
---|
162 | hasDescriptor = false;
|
---|
163 | // setup ActionHistory
|
---|
164 | ActionQueue::getInstance();
|
---|
165 | // TODO: find a way to really reset the factory to a clean state in tear-down
|
---|
166 | if(!hasDescriptor){
|
---|
167 | UIFactory::registerFactory(new DummyUIFactory::description());
|
---|
168 | hasDescriptor = true;
|
---|
169 | }
|
---|
170 | UIFactory::makeUserInterface("Dummy");
|
---|
171 | // create some necessary stubs used in this test
|
---|
172 | ActionTrait canUndoTrait("canUndoActionStub");
|
---|
173 | ActionTrait cannotUndoTrait("cannotUndoActionStub");
|
---|
174 | positive1 = new canUndoActionStub(canUndoTrait);
|
---|
175 | positive2 = new canUndoActionStub(canUndoTrait);
|
---|
176 | negative1 = new cannotUndoActionStub(cannotUndoTrait);
|
---|
177 | negative2 = new cannotUndoActionStub(cannotUndoTrait);
|
---|
178 |
|
---|
179 | ActionTrait wasCalledTrait("wasCalledActionStub");
|
---|
180 | shouldCall1 = new wasCalledActionStub(wasCalledTrait);
|
---|
181 | shouldCall2 = new wasCalledActionStub(wasCalledTrait);
|
---|
182 | shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
|
---|
183 | shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
|
---|
184 |
|
---|
185 | }
|
---|
186 |
|
---|
187 | void ActionSequenceTest::tearDown(){
|
---|
188 | delete positive1;
|
---|
189 | delete positive2;
|
---|
190 | delete negative1;
|
---|
191 | delete negative2;
|
---|
192 |
|
---|
193 | delete shouldCall1;
|
---|
194 | delete shouldCall2;
|
---|
195 | delete shouldNotCall1;
|
---|
196 | delete shouldNotCall2;
|
---|
197 |
|
---|
198 | ActionQueue::purgeInstance();
|
---|
199 | {
|
---|
200 | UIFactory::purgeInstance();
|
---|
201 | hasDescriptor = false;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | void ActionSequenceTest::canUndoTest(){
|
---|
206 | // first section:
|
---|
207 | {
|
---|
208 | // test some combinations
|
---|
209 | {
|
---|
210 | ActionSequence *sequence = new ActionSequence();
|
---|
211 | sequence->addAction(positive1);
|
---|
212 | sequence->addAction(positive2);
|
---|
213 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
214 | delete sequence;
|
---|
215 | }
|
---|
216 | {
|
---|
217 | ActionSequence *sequence = new ActionSequence();
|
---|
218 | sequence->addAction(positive1);
|
---|
219 | sequence->addAction(negative2);
|
---|
220 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
221 | delete sequence;
|
---|
222 | }
|
---|
223 | {
|
---|
224 | ActionSequence *sequence = new ActionSequence();
|
---|
225 | sequence->addAction(negative1);
|
---|
226 | sequence->addAction(positive2);
|
---|
227 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
228 | delete sequence;
|
---|
229 | }
|
---|
230 | {
|
---|
231 | ActionSequence *sequence = new ActionSequence();
|
---|
232 | sequence->addAction(negative1);
|
---|
233 | sequence->addAction(negative2);
|
---|
234 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
235 | delete sequence;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | // second section:
|
---|
240 | {
|
---|
241 | // empty sequence can be undone
|
---|
242 | ActionSequence *sequence = new ActionSequence();
|
---|
243 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
244 | // if only a positive action is contained it can be undone
|
---|
245 | sequence->addAction(positive1);
|
---|
246 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
247 | // the single negative action should block the process
|
---|
248 | sequence->addAction(negative1);
|
---|
249 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
250 | // after removing the negative action all is well again
|
---|
251 | sequence->removeLastAction();
|
---|
252 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
253 | delete sequence;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | void ActionSequenceTest::doesCallTest(){
|
---|
258 | ActionSequence *sequence = new ActionSequence();
|
---|
259 | sequence->addAction(shouldCall1);
|
---|
260 | sequence->addAction(shouldCall2);
|
---|
261 | sequence->addAction(shouldNotCall1);
|
---|
262 | sequence->addAction(shouldNotCall2);
|
---|
263 | sequence->removeLastAction();
|
---|
264 | sequence->removeLastAction();
|
---|
265 |
|
---|
266 | sequence->callAll();
|
---|
267 |
|
---|
268 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
269 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
270 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
271 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
272 |
|
---|
273 | delete sequence;
|
---|
274 | }
|
---|
275 |
|
---|
276 | void ActionSequenceTest::doesUndoTest(){
|
---|
277 | ActionSequence *sequence = new ActionSequence();
|
---|
278 | ActionTrait wasCalledTrait("wasCalledActionStub");
|
---|
279 | wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
|
---|
280 | wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
|
---|
281 | sequence->addAction(wasCalled1);
|
---|
282 | sequence->addAction(wasCalled2);
|
---|
283 |
|
---|
284 | ActionTrait MakroTrait("Test MakroAction");
|
---|
285 | MakroAction act(MakroTrait,*sequence);
|
---|
286 |
|
---|
287 | act.call();
|
---|
288 |
|
---|
289 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
|
---|
290 | CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
|
---|
291 |
|
---|
292 | ActionQueue::getInstance().undoLast();
|
---|
293 |
|
---|
294 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
|
---|
295 | CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
|
---|
296 |
|
---|
297 | delete sequence;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|