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