source: src/unittests/ActionSequenceTest.cpp@ e5c0a1

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 e5c0a1 was 112f90, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Fixed ActionSequenceTest and ManipulateAtomsTest

  • Property mode set to 100644
File size: 6.6 KB
Line 
1/*
2 * ActionSequenzTest.cpp
3 *
4 * Created on: Dec 17, 2009
5 * Author: crueger
6 */
7
8#include <cppunit/CompilerOutputter.h>
9#include <cppunit/extensions/TestFactoryRegistry.h>
10#include <cppunit/ui/text/TestRunner.h>
11
12#include "unittests/ActionSequenceTest.hpp"
13#include "Actions/Action.hpp"
14#include "Actions/ActionSequence.hpp"
15#include "Actions/MakroAction.hpp"
16#include "Actions/ActionHistory.hpp"
17#include "Actions/ActionRegistry.hpp"
18
19#include "DummyUI.hpp"
20
21#ifdef HAVE_TESTRUNNER
22#include "UnitTestMain.hpp"
23#endif /*HAVE_TESTRUNNER*/
24
25/********************************************** Test classes **************************************/
26
27// Registers the fixture into the 'registry'
28CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
29
30/* some neccessary stubs for tests */
31class canUndoActionStub : public Action
32{
33public:
34 canUndoActionStub(): Action("canUndoActionStub",false){}
35 virtual ~canUndoActionStub(){}
36
37 virtual Dialog* fillDialog(Dialog *dialog){
38 ASSERT(dialog,"No Dialog given when filling action dialog");
39 return dialog;
40 }
41
42 virtual Action::state_ptr performCall(){
43 return Action::success;
44 }
45 virtual Action::state_ptr performUndo(Action::state_ptr){
46 return Action::success;
47 }
48 virtual Action::state_ptr performRedo(Action::state_ptr){
49 return Action::success;
50 }
51 virtual bool canUndo(){
52 return true;
53 }
54 virtual bool shouldUndo(){
55 return true;
56 }
57};
58
59class cannotUndoActionStub : public Action
60{
61public:
62 cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
63 virtual ~cannotUndoActionStub(){}
64
65 virtual Dialog* fillDialog(Dialog *dialog){
66 ASSERT(dialog,"No Dialog given when filling action dialog");
67 return dialog;
68 }
69
70 virtual Action::state_ptr performCall(){
71 return Action::success;
72 }
73 virtual Action::state_ptr performUndo(Action::state_ptr){
74 return Action::success;
75 }
76 virtual Action::state_ptr performRedo(Action::state_ptr){
77 return Action::success;
78 }
79 virtual bool canUndo(){
80 return false;
81 }
82 virtual bool shouldUndo(){
83 return true;
84 }
85};
86
87class wasCalledActionStub : public Action
88{
89public:
90 wasCalledActionStub() :
91 Action("wasCalledActionStub",false),
92 called(false)
93 {}
94 virtual ~wasCalledActionStub(){}
95
96 virtual Dialog* fillDialog(Dialog *dialog){
97 return dialog;
98 }
99 virtual Action::state_ptr performCall(){
100 called = true;
101 return Action::success;
102 }
103 virtual Action::state_ptr performUndo(Action::state_ptr){
104 called = false;
105 return Action::success;
106 }
107 virtual Action::state_ptr performRedo(Action::state_ptr){
108 called = true;
109 return Action::success;
110 }
111 virtual bool canUndo(){
112 return true;
113 }
114 virtual bool shouldUndo(){
115 return true;
116 }
117 bool wasCalled(){
118 return called;
119 }
120private:
121 bool called;
122};
123
124void ActionSequenceTest::setUp(){
125 static bool hasDescriptor = false;
126 ActionHistory::init();
127 // TODO: find a way to really reset the factory to a clean state in tear-down
128 if(!hasDescriptor){
129 UIFactory::registerFactory(new DummyUIFactory::description());
130 hasDescriptor = true;
131 }
132 UIFactory::makeUserInterface("Dummy");
133 // create some necessary stubs used in this test
134 positive1 = new canUndoActionStub();
135 positive2 = new canUndoActionStub();
136 negative1 = new cannotUndoActionStub();
137 negative2 = new cannotUndoActionStub();
138
139 shouldCall1 = new wasCalledActionStub();
140 shouldCall2 = new wasCalledActionStub();
141 shouldNotCall1 = new wasCalledActionStub();
142 shouldNotCall2 = new wasCalledActionStub();
143
144}
145
146void ActionSequenceTest::tearDown(){
147 delete positive1;
148 delete positive2;
149 delete negative1;
150 delete negative2;
151
152 delete shouldCall1;
153 delete shouldCall2;
154 delete shouldNotCall1;
155 delete shouldNotCall2;
156
157 ActionHistory::purgeInstance();
158 ActionRegistry::purgeInstance();
159 UIFactory::purgeInstance();
160}
161
162void ActionSequenceTest::canUndoTest(){
163 // first section:
164 {
165 // test some combinations
166 {
167 ActionSequence *sequence = new ActionSequence();
168 sequence->addAction(positive1);
169 sequence->addAction(positive2);
170 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
171 delete sequence;
172 }
173 {
174 ActionSequence *sequence = new ActionSequence();
175 sequence->addAction(positive1);
176 sequence->addAction(negative2);
177 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
178 delete sequence;
179 }
180 {
181 ActionSequence *sequence = new ActionSequence();
182 sequence->addAction(negative1);
183 sequence->addAction(positive2);
184 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
185 delete sequence;
186 }
187 {
188 ActionSequence *sequence = new ActionSequence();
189 sequence->addAction(negative1);
190 sequence->addAction(negative2);
191 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
192 delete sequence;
193 }
194 }
195
196 // second section:
197 {
198 // empty sequence can be undone
199 ActionSequence *sequence = new ActionSequence();
200 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
201 // if only a positive action is contained it can be undone
202 sequence->addAction(positive1);
203 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
204 // the single negative action should block the process
205 sequence->addAction(negative1);
206 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
207 // after removing the negative action all is well again
208 sequence->removeLastAction();
209 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
210 delete sequence;
211 }
212}
213
214void ActionSequenceTest::doesCallTest(){
215 ActionSequence *sequence = new ActionSequence();
216 sequence->addAction(shouldCall1);
217 sequence->addAction(shouldCall2);
218 sequence->addAction(shouldNotCall1);
219 sequence->addAction(shouldNotCall2);
220 sequence->removeLastAction();
221 sequence->removeLastAction();
222
223 sequence->callAll();
224
225 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
226 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
227 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
228 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
229
230 delete sequence;
231}
232
233void ActionSequenceTest::doesUndoTest(){
234 ActionSequence *sequence = new ActionSequence();
235 wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
236 wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
237 sequence->addAction(wasCalled1);
238 sequence->addAction(wasCalled2);
239
240 MakroAction act("Test MakroAction",sequence,false);
241
242 act.call();
243
244 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
245 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
246
247 ActionHistory::getInstance().undoLast();
248
249 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
250 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
251
252}
253
254
Note: See TracBrowser for help on using the repository browser.