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

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 bcfb77 was 3139b2, checked in by Frederik Heber <heber@…>, 13 years ago

Renamed ActionTrait and ActionTraits.

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