source: src/Actions/unittests/ActionSequenceUnitTest.cpp@ 751d7f1

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 751d7f1 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
RevLine 
[bcf653]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
[147339]8/*
[f7c0c4]9 * ActionSequenceUnitTest.cpp
[147339]10 *
11 * Created on: Dec 17, 2009
12 * Author: crueger
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[147339]20#include <cppunit/CompilerOutputter.h>
21#include <cppunit/extensions/TestFactoryRegistry.h>
22#include <cppunit/ui/text/TestRunner.h>
23
[f7c0c4]24#include "ActionSequenceUnitTest.hpp"
[147339]25#include "Actions/Action.hpp"
26#include "Actions/ActionSequence.hpp"
[2efa90]27#include "Actions/MakroAction.hpp"
28#include "Actions/ActionHistory.hpp"
29#include "Actions/ActionRegistry.hpp"
[147339]30
[fdcd1b]31#include "stubs/DummyUI.hpp"
[112f90]32
[ce7fdc]33using namespace MoleCuilder;
34
[9b6b2f]35#ifdef HAVE_TESTRUNNER
36#include "UnitTestMain.hpp"
37#endif /*HAVE_TESTRUNNER*/
38
39/********************************************** Test classes **************************************/
40
[147339]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:
[3139b2]48 canUndoActionStub(const ActionTrait &_trait):
[e4afb4]49 Action(_trait,false){}
[147339]50 virtual ~canUndoActionStub(){}
51
[0b2ce9]52 virtual void getParametersfromValueStorage(){
53 }
54
[047878]55 virtual Dialog* fillDialog(Dialog *dialog){
56 ASSERT(dialog,"No Dialog given when filling action dialog");
57 return dialog;
[80951de]58 }
59
[5b0b98]60 virtual Action::state_ptr performCall(){
[67e2b3]61 return Action::success;
62 }
[5b0b98]63 virtual Action::state_ptr performUndo(Action::state_ptr){
[67e2b3]64 return Action::success;
65 }
[5b0b98]66 virtual Action::state_ptr performRedo(Action::state_ptr){
[67e2b3]67 return Action::success;
68 }
[147339]69 virtual bool canUndo(){
70 return true;
71 }
[67e2b3]72 virtual bool shouldUndo(){
73 return true;
74 }
[147339]75};
76
77class cannotUndoActionStub : public Action
78{
79public:
[3139b2]80 cannotUndoActionStub(const ActionTrait &_trait) :
[e4afb4]81 Action(_trait,false){}
[147339]82 virtual ~cannotUndoActionStub(){}
83
[0b2ce9]84 virtual void getParametersfromValueStorage(){
85 }
86
[047878]87 virtual Dialog* fillDialog(Dialog *dialog){
88 ASSERT(dialog,"No Dialog given when filling action dialog");
89 return dialog;
[80951de]90 }
91
[5b0b98]92 virtual Action::state_ptr performCall(){
[67e2b3]93 return Action::success;
94 }
[5b0b98]95 virtual Action::state_ptr performUndo(Action::state_ptr){
[67e2b3]96 return Action::success;
97 }
[5b0b98]98 virtual Action::state_ptr performRedo(Action::state_ptr){
[67e2b3]99 return Action::success;
100 }
[147339]101 virtual bool canUndo(){
102 return false;
103 }
[67e2b3]104 virtual bool shouldUndo(){
105 return true;
106 }
[147339]107};
108
[0229f9]109class wasCalledActionStub : public Action
110{
111public:
[3139b2]112 wasCalledActionStub(const ActionTrait &_trait) :
[e4afb4]113 Action(_trait,false),
[0229f9]114 called(false)
115 {}
116 virtual ~wasCalledActionStub(){}
[147339]117
[0b2ce9]118 virtual void getParametersfromValueStorage(){
119 }
120
[047878]121 virtual Dialog* fillDialog(Dialog *dialog){
122 return dialog;
[80951de]123 }
[5b0b98]124 virtual Action::state_ptr performCall(){
[0229f9]125 called = true;
[67e2b3]126 return Action::success;
[0229f9]127 }
[5b0b98]128 virtual Action::state_ptr performUndo(Action::state_ptr){
[0229f9]129 called = false;
[67e2b3]130 return Action::success;
131 }
[5b0b98]132 virtual Action::state_ptr performRedo(Action::state_ptr){
[67e2b3]133 called = true;
134 return Action::success;
[0229f9]135 }
136 virtual bool canUndo(){
137 return true;
138 }
[67e2b3]139 virtual bool shouldUndo(){
140 return true;
141 }
[0229f9]142 bool wasCalled(){
143 return called;
144 }
145private:
146 bool called;
147};
[147339]148
[0229f9]149void ActionSequenceTest::setUp(){
[694d84]150 hasDescriptor = false;
[2efa90]151 ActionHistory::init();
[112f90]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");
[147339]158 // create some necessary stubs used in this test
[3139b2]159 ActionTrait canUndoTrait("canUndoActionStub");
160 ActionTrait cannotUndoTrait("cannotUndoActionStub");
[e4afb4]161 positive1 = new canUndoActionStub(canUndoTrait);
162 positive2 = new canUndoActionStub(canUndoTrait);
163 negative1 = new cannotUndoActionStub(cannotUndoTrait);
164 negative2 = new cannotUndoActionStub(cannotUndoTrait);
165
[3139b2]166 ActionTrait wasCalledTrait("wasCalledActionStub");
[e4afb4]167 shouldCall1 = new wasCalledActionStub(wasCalledTrait);
168 shouldCall2 = new wasCalledActionStub(wasCalledTrait);
169 shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
170 shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
[0229f9]171
172}
173
174void ActionSequenceTest::tearDown(){
175 delete positive1;
176 delete positive2;
177 delete negative1;
178 delete negative2;
[147339]179
[0229f9]180 delete shouldCall1;
181 delete shouldCall2;
182 delete shouldNotCall1;
183 delete shouldNotCall2;
184
[2efa90]185 ActionHistory::purgeInstance();
186 ActionRegistry::purgeInstance();
[694d84]187 {
188 UIFactory::purgeInstance();
189 hasDescriptor = false;
190 }
[0229f9]191}
192
193void ActionSequenceTest::canUndoTest(){
[147339]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 }
[0229f9]243}
[147339]244
[0229f9]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
[f59d81]261 delete sequence;
[0229f9]262}
263
264void ActionSequenceTest::doesUndoTest(){
265 ActionSequence *sequence = new ActionSequence();
[3139b2]266 ActionTrait wasCalledTrait("wasCalledActionStub");
[e4afb4]267 wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
268 wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
[2efa90]269 sequence->addAction(wasCalled1);
270 sequence->addAction(wasCalled2);
[0229f9]271
[3139b2]272 ActionTrait MakroTrait("Test MakroAction");
[e4afb4]273 MakroAction act(MakroTrait,sequence,false);
[0229f9]274
[2efa90]275 act.call();
[0229f9]276
[2efa90]277 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
[ec149d]278 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
[0229f9]279
[2efa90]280 ActionHistory::getInstance().undoLast();
281
282 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
[ec149d]283 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
[147339]284
285}
286
287
Note: See TracBrowser for help on using the repository browser.