source: src/Actions/unittests/ActionSequenceUnitTest.cpp@ 76096d

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 76096d was 126867, checked in by Frederik Heber <heber@…>, 11 years ago

Actions no longer register themselves, ActionQueue::addAction() introduced.

  • eventually, Actions should be constrained to the ActionQueue alone. They may be created externally but their control has to be delivered to the ActionQueue.
  • Hence, we reversed the registering process: Actions do not register themselves with the ActionRegistry but as almost all are created in ActionRegistry::fillRegistry() they are registered there as well.
  • Actions cstor and dstor are now protected, ActionRegistry is friend.
  • Action needs to befriend Registry<T> and ActionSequenceTest to grant access to dstor.
  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * ActionSequenceUnitTest.cpp
26 *
27 * Created on: Dec 17, 2009
28 * Author: crueger
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include <cppunit/CompilerOutputter.h>
37#include <cppunit/extensions/TestFactoryRegistry.h>
38#include <cppunit/ui/text/TestRunner.h>
39
40#include "ActionSequenceUnitTest.hpp"
41#include "Actions/Action.hpp"
42#include "Actions/ActionQueue.hpp"
43#include "Actions/ActionSequence.hpp"
44#include "Actions/MakroAction.hpp"
45
46#include "stubs/DummyUI.hpp"
47
48using namespace MoleCuilder;
49
50#ifdef HAVE_TESTRUNNER
51#include "UnitTestMain.hpp"
52#endif /*HAVE_TESTRUNNER*/
53
54/********************************************** Test classes **************************************/
55
56// Registers the fixture into the 'registry'
57CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
58
59/* some neccessary stubs for tests */
60class canUndoActionStub : public Action
61{
62public:
63 canUndoActionStub(const ActionTrait &_trait):
64 Action(_trait){}
65 virtual ~canUndoActionStub(){}
66
67 virtual Dialog* fillDialog(Dialog *dialog){
68 ASSERT(dialog,"No Dialog given when filling action dialog");
69 return dialog;
70 }
71
72 virtual ActionState::ptr performCall(){
73 return Action::success;
74 }
75 virtual ActionState::ptr performUndo(ActionState::ptr){
76 return Action::success;
77 }
78 virtual ActionState::ptr performRedo(ActionState::ptr){
79 return Action::success;
80 }
81 virtual bool canUndo(){
82 return true;
83 }
84 virtual bool shouldUndo(){
85 return true;
86 }
87};
88
89class cannotUndoActionStub : public Action
90{
91public:
92 cannotUndoActionStub(const ActionTrait &_trait) :
93 Action(_trait){}
94 virtual ~cannotUndoActionStub(){}
95
96 virtual Dialog* fillDialog(Dialog *dialog){
97 ASSERT(dialog,"No Dialog given when filling action dialog");
98 return dialog;
99 }
100
101 virtual ActionState::ptr performCall(){
102 return Action::success;
103 }
104 virtual ActionState::ptr performUndo(ActionState::ptr){
105 return Action::success;
106 }
107 virtual ActionState::ptr performRedo(ActionState::ptr){
108 return Action::success;
109 }
110 virtual bool canUndo(){
111 return false;
112 }
113 virtual bool shouldUndo(){
114 return true;
115 }
116};
117
118class wasCalledActionStub : public Action
119{
120public:
121 wasCalledActionStub(const ActionTrait &_trait) :
122 Action(_trait),
123 called(false)
124 {}
125 virtual ~wasCalledActionStub(){}
126
127 virtual Dialog* fillDialog(Dialog *dialog){
128 return dialog;
129 }
130 virtual ActionState::ptr performCall(){
131 called = true;
132 return Action::success;
133 }
134 virtual ActionState::ptr performUndo(ActionState::ptr){
135 called = false;
136 return Action::success;
137 }
138 virtual ActionState::ptr performRedo(ActionState::ptr){
139 called = true;
140 return Action::success;
141 }
142 virtual bool canUndo(){
143 return true;
144 }
145 virtual bool shouldUndo(){
146 return true;
147 }
148 bool wasCalled(){
149 return called;
150 }
151private:
152 bool called;
153};
154
155void ActionSequenceTest::setUp(){
156 hasDescriptor = false;
157 // setup ActionHistory
158 ActionQueue::getInstance();
159 // TODO: find a way to really reset the factory to a clean state in tear-down
160 if(!hasDescriptor){
161 UIFactory::registerFactory(new DummyUIFactory::description());
162 hasDescriptor = true;
163 }
164 UIFactory::makeUserInterface("Dummy");
165 // create some necessary stubs used in this test
166 ActionTrait canUndoTrait("canUndoActionStub");
167 ActionTrait cannotUndoTrait("cannotUndoActionStub");
168 positive1 = new canUndoActionStub(canUndoTrait);
169 positive2 = new canUndoActionStub(canUndoTrait);
170 negative1 = new cannotUndoActionStub(cannotUndoTrait);
171 negative2 = new cannotUndoActionStub(cannotUndoTrait);
172
173 ActionTrait wasCalledTrait("wasCalledActionStub");
174 shouldCall1 = new wasCalledActionStub(wasCalledTrait);
175 shouldCall2 = new wasCalledActionStub(wasCalledTrait);
176 shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
177 shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
178
179}
180
181void ActionSequenceTest::tearDown(){
182 delete positive1;
183 delete positive2;
184 delete negative1;
185 delete negative2;
186
187 delete shouldCall1;
188 delete shouldCall2;
189 delete shouldNotCall1;
190 delete shouldNotCall2;
191
192 ActionQueue::purgeInstance();
193 {
194 UIFactory::purgeInstance();
195 hasDescriptor = false;
196 }
197}
198
199void ActionSequenceTest::canUndoTest(){
200 // first section:
201 {
202 // test some combinations
203 {
204 ActionSequence *sequence = new ActionSequence();
205 sequence->addAction(positive1);
206 sequence->addAction(positive2);
207 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
208 delete sequence;
209 }
210 {
211 ActionSequence *sequence = new ActionSequence();
212 sequence->addAction(positive1);
213 sequence->addAction(negative2);
214 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
215 delete sequence;
216 }
217 {
218 ActionSequence *sequence = new ActionSequence();
219 sequence->addAction(negative1);
220 sequence->addAction(positive2);
221 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
222 delete sequence;
223 }
224 {
225 ActionSequence *sequence = new ActionSequence();
226 sequence->addAction(negative1);
227 sequence->addAction(negative2);
228 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
229 delete sequence;
230 }
231 }
232
233 // second section:
234 {
235 // empty sequence can be undone
236 ActionSequence *sequence = new ActionSequence();
237 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
238 // if only a positive action is contained it can be undone
239 sequence->addAction(positive1);
240 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
241 // the single negative action should block the process
242 sequence->addAction(negative1);
243 CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
244 // after removing the negative action all is well again
245 sequence->removeLastAction();
246 CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
247 delete sequence;
248 }
249}
250
251void ActionSequenceTest::doesCallTest(){
252 ActionSequence *sequence = new ActionSequence();
253 sequence->addAction(shouldCall1);
254 sequence->addAction(shouldCall2);
255 sequence->addAction(shouldNotCall1);
256 sequence->addAction(shouldNotCall2);
257 sequence->removeLastAction();
258 sequence->removeLastAction();
259
260 sequence->callAll();
261
262 CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
263 CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
264 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
265 CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
266
267 delete sequence;
268}
269
270void ActionSequenceTest::doesUndoTest(){
271 ActionSequence *sequence = new ActionSequence();
272 ActionTrait wasCalledTrait("wasCalledActionStub");
273 wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
274 wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
275 sequence->addAction(wasCalled1);
276 sequence->addAction(wasCalled2);
277
278 ActionTrait MakroTrait("Test MakroAction");
279 MakroAction act(MakroTrait,*sequence);
280
281 act.call();
282
283 CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
284 CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
285
286 ActionQueue::getInstance().undoLast();
287
288 CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
289 CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
290
291 delete sequence;
292}
293
294
Note: See TracBrowser for help on using the repository browser.