source: src/Actions/unittests/ActionSequenceUnitTest.cpp@ fdcd1b

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since fdcd1b was fdcd1b, checked in by Frederik Heber <heber@…>, 14 years ago

Moved DummyUI.hpp from unittests to Actions/unittests/stubs.

  • Property mode set to 100644
File size: 7.5 KB
Line 
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 "stubs/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'
40CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
41
42/* some neccessary stubs for tests */
43class canUndoActionStub : public Action
44{
45public:
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
75class cannotUndoActionStub : public Action
76{
77public:
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
107class wasCalledActionStub : public Action
108{
109public:
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 }
143private:
144 bool called;
145};
146
147void 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
172void 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
191void 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
243void 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
262void 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
Note: See TracBrowser for help on using the repository browser.