1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 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 "stubs/DummyUI.hpp"
|
---|
32 |
|
---|
33 | using namespace MoleCuilder;
|
---|
34 |
|
---|
35 | #ifdef HAVE_TESTRUNNER
|
---|
36 | #include "UnitTestMain.hpp"
|
---|
37 | #endif /*HAVE_TESTRUNNER*/
|
---|
38 |
|
---|
39 | /********************************************** Test classes **************************************/
|
---|
40 |
|
---|
41 | // Registers the fixture into the 'registry'
|
---|
42 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
43 |
|
---|
44 | /* some neccessary stubs for tests */
|
---|
45 | class canUndoActionStub : public Action
|
---|
46 | {
|
---|
47 | public:
|
---|
48 | canUndoActionStub(const ActionTrait &_trait):
|
---|
49 | Action(_trait,false){}
|
---|
50 | virtual ~canUndoActionStub(){}
|
---|
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(const ActionTrait &_trait) :
|
---|
78 | Action(_trait,false){}
|
---|
79 | virtual ~cannotUndoActionStub(){}
|
---|
80 |
|
---|
81 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
82 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
---|
83 | return dialog;
|
---|
84 | }
|
---|
85 |
|
---|
86 | virtual Action::state_ptr performCall(){
|
---|
87 | return Action::success;
|
---|
88 | }
|
---|
89 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
90 | return Action::success;
|
---|
91 | }
|
---|
92 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
93 | return Action::success;
|
---|
94 | }
|
---|
95 | virtual bool canUndo(){
|
---|
96 | return false;
|
---|
97 | }
|
---|
98 | virtual bool shouldUndo(){
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | };
|
---|
102 |
|
---|
103 | class wasCalledActionStub : public Action
|
---|
104 | {
|
---|
105 | public:
|
---|
106 | wasCalledActionStub(const ActionTrait &_trait) :
|
---|
107 | Action(_trait,false),
|
---|
108 | called(false)
|
---|
109 | {}
|
---|
110 | virtual ~wasCalledActionStub(){}
|
---|
111 |
|
---|
112 | virtual Dialog* fillDialog(Dialog *dialog){
|
---|
113 | return dialog;
|
---|
114 | }
|
---|
115 | virtual Action::state_ptr performCall(){
|
---|
116 | called = true;
|
---|
117 | return Action::success;
|
---|
118 | }
|
---|
119 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
120 | called = false;
|
---|
121 | return Action::success;
|
---|
122 | }
|
---|
123 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
124 | called = true;
|
---|
125 | return Action::success;
|
---|
126 | }
|
---|
127 | virtual bool canUndo(){
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | virtual bool shouldUndo(){
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 | bool wasCalled(){
|
---|
134 | return called;
|
---|
135 | }
|
---|
136 | private:
|
---|
137 | bool called;
|
---|
138 | };
|
---|
139 |
|
---|
140 | void ActionSequenceTest::setUp(){
|
---|
141 | hasDescriptor = false;
|
---|
142 | ActionHistory::init();
|
---|
143 | // TODO: find a way to really reset the factory to a clean state in tear-down
|
---|
144 | if(!hasDescriptor){
|
---|
145 | UIFactory::registerFactory(new DummyUIFactory::description());
|
---|
146 | hasDescriptor = true;
|
---|
147 | }
|
---|
148 | UIFactory::makeUserInterface("Dummy");
|
---|
149 | // create some necessary stubs used in this test
|
---|
150 | ActionTrait canUndoTrait("canUndoActionStub");
|
---|
151 | ActionTrait cannotUndoTrait("cannotUndoActionStub");
|
---|
152 | positive1 = new canUndoActionStub(canUndoTrait);
|
---|
153 | positive2 = new canUndoActionStub(canUndoTrait);
|
---|
154 | negative1 = new cannotUndoActionStub(cannotUndoTrait);
|
---|
155 | negative2 = new cannotUndoActionStub(cannotUndoTrait);
|
---|
156 |
|
---|
157 | ActionTrait wasCalledTrait("wasCalledActionStub");
|
---|
158 | shouldCall1 = new wasCalledActionStub(wasCalledTrait);
|
---|
159 | shouldCall2 = new wasCalledActionStub(wasCalledTrait);
|
---|
160 | shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
|
---|
161 | shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
|
---|
162 |
|
---|
163 | }
|
---|
164 |
|
---|
165 | void ActionSequenceTest::tearDown(){
|
---|
166 | delete positive1;
|
---|
167 | delete positive2;
|
---|
168 | delete negative1;
|
---|
169 | delete negative2;
|
---|
170 |
|
---|
171 | delete shouldCall1;
|
---|
172 | delete shouldCall2;
|
---|
173 | delete shouldNotCall1;
|
---|
174 | delete shouldNotCall2;
|
---|
175 |
|
---|
176 | ActionHistory::purgeInstance();
|
---|
177 | ActionRegistry::purgeInstance();
|
---|
178 | {
|
---|
179 | UIFactory::purgeInstance();
|
---|
180 | hasDescriptor = false;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | void ActionSequenceTest::canUndoTest(){
|
---|
185 | // first section:
|
---|
186 | {
|
---|
187 | // test some combinations
|
---|
188 | {
|
---|
189 | ActionSequence *sequence = new ActionSequence();
|
---|
190 | sequence->addAction(positive1);
|
---|
191 | sequence->addAction(positive2);
|
---|
192 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
193 | delete sequence;
|
---|
194 | }
|
---|
195 | {
|
---|
196 | ActionSequence *sequence = new ActionSequence();
|
---|
197 | sequence->addAction(positive1);
|
---|
198 | sequence->addAction(negative2);
|
---|
199 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
200 | delete sequence;
|
---|
201 | }
|
---|
202 | {
|
---|
203 | ActionSequence *sequence = new ActionSequence();
|
---|
204 | sequence->addAction(negative1);
|
---|
205 | sequence->addAction(positive2);
|
---|
206 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
207 | delete sequence;
|
---|
208 | }
|
---|
209 | {
|
---|
210 | ActionSequence *sequence = new ActionSequence();
|
---|
211 | sequence->addAction(negative1);
|
---|
212 | sequence->addAction(negative2);
|
---|
213 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
214 | delete sequence;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | // second section:
|
---|
219 | {
|
---|
220 | // empty sequence can be undone
|
---|
221 | ActionSequence *sequence = new ActionSequence();
|
---|
222 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
223 | // if only a positive action is contained it can be undone
|
---|
224 | sequence->addAction(positive1);
|
---|
225 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
226 | // the single negative action should block the process
|
---|
227 | sequence->addAction(negative1);
|
---|
228 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
229 | // after removing the negative action all is well again
|
---|
230 | sequence->removeLastAction();
|
---|
231 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
232 | delete sequence;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | void ActionSequenceTest::doesCallTest(){
|
---|
237 | ActionSequence *sequence = new ActionSequence();
|
---|
238 | sequence->addAction(shouldCall1);
|
---|
239 | sequence->addAction(shouldCall2);
|
---|
240 | sequence->addAction(shouldNotCall1);
|
---|
241 | sequence->addAction(shouldNotCall2);
|
---|
242 | sequence->removeLastAction();
|
---|
243 | sequence->removeLastAction();
|
---|
244 |
|
---|
245 | sequence->callAll();
|
---|
246 |
|
---|
247 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
248 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
249 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
250 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
251 |
|
---|
252 | delete sequence;
|
---|
253 | }
|
---|
254 |
|
---|
255 | void ActionSequenceTest::doesUndoTest(){
|
---|
256 | ActionSequence *sequence = new ActionSequence();
|
---|
257 | ActionTrait wasCalledTrait("wasCalledActionStub");
|
---|
258 | wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
|
---|
259 | wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
|
---|
260 | sequence->addAction(wasCalled1);
|
---|
261 | sequence->addAction(wasCalled2);
|
---|
262 |
|
---|
263 | ActionTrait MakroTrait("Test MakroAction");
|
---|
264 | MakroAction act(MakroTrait,sequence,false);
|
---|
265 |
|
---|
266 | act.call();
|
---|
267 |
|
---|
268 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
|
---|
269 | CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
|
---|
270 |
|
---|
271 | ActionHistory::getInstance().undoLast();
|
---|
272 |
|
---|
273 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
|
---|
274 | CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
|
---|
275 |
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|