| [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 | /*
 | 
|---|
 | 9 |  * ActionSequenzTest.cpp
 | 
|---|
 | 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 | 
 | 
|---|
 | 24 | #include "unittests/ActionSequenceTest.hpp"
 | 
|---|
 | 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 | 
 | 
|---|
| [112f90] | 31 | #include "DummyUI.hpp"
 | 
|---|
 | 32 | 
 | 
|---|
| [9b6b2f] | 33 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 34 | #include "UnitTestMain.hpp"
 | 
|---|
 | 35 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | /********************************************** Test classes **************************************/
 | 
|---|
 | 38 | 
 | 
|---|
| [147339] | 39 | // Registers the fixture into the 'registry'
 | 
|---|
 | 40 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | /* some neccessary stubs for tests */
 | 
|---|
 | 43 | class canUndoActionStub : public Action
 | 
|---|
 | 44 | {
 | 
|---|
 | 45 | public:
 | 
|---|
| [e4afb4] | 46 |   canUndoActionStub(const ActionTraits &_trait):
 | 
|---|
 | 47 |     Action(_trait,false){}
 | 
|---|
| [147339] | 48 |   virtual ~canUndoActionStub(){}
 | 
|---|
 | 49 | 
 | 
|---|
| [0b2ce9] | 50 |   virtual void getParametersfromValueStorage(){
 | 
|---|
 | 51 |   }
 | 
|---|
 | 52 | 
 | 
|---|
| [047878] | 53 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 54 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 55 |     return dialog;
 | 
|---|
| [80951de] | 56 |   }
 | 
|---|
 | 57 | 
 | 
|---|
| [5b0b98] | 58 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [67e2b3] | 59 |     return Action::success;
 | 
|---|
 | 60 |   }
 | 
|---|
| [5b0b98] | 61 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 62 |     return Action::success;
 | 
|---|
 | 63 |   }
 | 
|---|
| [5b0b98] | 64 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 65 |     return Action::success;
 | 
|---|
 | 66 |   }
 | 
|---|
| [147339] | 67 |   virtual bool canUndo(){
 | 
|---|
 | 68 |     return true;
 | 
|---|
 | 69 |   }
 | 
|---|
| [67e2b3] | 70 |   virtual bool shouldUndo(){
 | 
|---|
 | 71 |     return true;
 | 
|---|
 | 72 |   }
 | 
|---|
| [147339] | 73 | };
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 | class cannotUndoActionStub : public Action
 | 
|---|
 | 76 | {
 | 
|---|
 | 77 | public:
 | 
|---|
| [e4afb4] | 78 |   cannotUndoActionStub(const ActionTraits &_trait) :
 | 
|---|
 | 79 |     Action(_trait,false){}
 | 
|---|
| [147339] | 80 |   virtual ~cannotUndoActionStub(){}
 | 
|---|
 | 81 | 
 | 
|---|
| [0b2ce9] | 82 |   virtual void getParametersfromValueStorage(){
 | 
|---|
 | 83 |   }
 | 
|---|
 | 84 | 
 | 
|---|
| [047878] | 85 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 86 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 87 |     return dialog;
 | 
|---|
| [80951de] | 88 |   }
 | 
|---|
 | 89 | 
 | 
|---|
| [5b0b98] | 90 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [67e2b3] | 91 |     return Action::success;
 | 
|---|
 | 92 |   }
 | 
|---|
| [5b0b98] | 93 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 94 |     return Action::success;
 | 
|---|
 | 95 |   }
 | 
|---|
| [5b0b98] | 96 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 97 |     return Action::success;
 | 
|---|
 | 98 |   }
 | 
|---|
| [147339] | 99 |   virtual bool canUndo(){
 | 
|---|
 | 100 |     return false;
 | 
|---|
 | 101 |   }
 | 
|---|
| [67e2b3] | 102 |   virtual bool shouldUndo(){
 | 
|---|
 | 103 |    return true;
 | 
|---|
 | 104 |   }
 | 
|---|
| [147339] | 105 | };
 | 
|---|
 | 106 | 
 | 
|---|
| [0229f9] | 107 | class wasCalledActionStub : public Action
 | 
|---|
 | 108 | {
 | 
|---|
 | 109 | public:
 | 
|---|
| [e4afb4] | 110 |   wasCalledActionStub(const ActionTraits &_trait) :
 | 
|---|
 | 111 |       Action(_trait,false),
 | 
|---|
| [0229f9] | 112 |       called(false)
 | 
|---|
 | 113 |   {}
 | 
|---|
 | 114 |   virtual ~wasCalledActionStub(){}
 | 
|---|
| [147339] | 115 | 
 | 
|---|
| [0b2ce9] | 116 |   virtual void getParametersfromValueStorage(){
 | 
|---|
 | 117 |   }
 | 
|---|
 | 118 | 
 | 
|---|
| [047878] | 119 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 120 |     return dialog;
 | 
|---|
| [80951de] | 121 |   }
 | 
|---|
| [5b0b98] | 122 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [0229f9] | 123 |     called = true;
 | 
|---|
| [67e2b3] | 124 |     return Action::success;
 | 
|---|
| [0229f9] | 125 |   }
 | 
|---|
| [5b0b98] | 126 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [0229f9] | 127 |     called = false;
 | 
|---|
| [67e2b3] | 128 |     return Action::success;
 | 
|---|
 | 129 |   }
 | 
|---|
| [5b0b98] | 130 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 131 |     called = true;
 | 
|---|
 | 132 |     return Action::success;
 | 
|---|
| [0229f9] | 133 |   }
 | 
|---|
 | 134 |   virtual bool canUndo(){
 | 
|---|
 | 135 |     return true;
 | 
|---|
 | 136 |   }
 | 
|---|
| [67e2b3] | 137 |   virtual bool shouldUndo(){
 | 
|---|
 | 138 |     return true;
 | 
|---|
 | 139 |   }
 | 
|---|
| [0229f9] | 140 |   bool wasCalled(){
 | 
|---|
 | 141 |     return called;
 | 
|---|
 | 142 |   }
 | 
|---|
 | 143 | private:
 | 
|---|
 | 144 |   bool called;
 | 
|---|
 | 145 | };
 | 
|---|
| [147339] | 146 | 
 | 
|---|
| [0229f9] | 147 | void ActionSequenceTest::setUp(){
 | 
|---|
| [112f90] | 148 |   static bool hasDescriptor = false;
 | 
|---|
| [2efa90] | 149 |   ActionHistory::init();
 | 
|---|
| [112f90] | 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");
 | 
|---|
| [147339] | 156 |   // create some necessary stubs used in this test
 | 
|---|
| [e4afb4] | 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);
 | 
|---|
| [0229f9] | 169 | 
 | 
|---|
 | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 | void ActionSequenceTest::tearDown(){
 | 
|---|
 | 173 |   delete positive1;
 | 
|---|
 | 174 |   delete positive2;
 | 
|---|
 | 175 |   delete negative1;
 | 
|---|
 | 176 |   delete negative2;
 | 
|---|
| [147339] | 177 | 
 | 
|---|
| [0229f9] | 178 |   delete shouldCall1;
 | 
|---|
 | 179 |   delete shouldCall2;
 | 
|---|
 | 180 |   delete shouldNotCall1;
 | 
|---|
 | 181 |   delete shouldNotCall2;
 | 
|---|
 | 182 | 
 | 
|---|
| [2efa90] | 183 |   ActionHistory::purgeInstance();
 | 
|---|
 | 184 |   ActionRegistry::purgeInstance();
 | 
|---|
| [112f90] | 185 |   UIFactory::purgeInstance();
 | 
|---|
| [0229f9] | 186 | }
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 | void ActionSequenceTest::canUndoTest(){
 | 
|---|
| [147339] | 189 |   // first section:
 | 
|---|
 | 190 |   {
 | 
|---|
 | 191 |     // test some combinations
 | 
|---|
 | 192 |     {
 | 
|---|
 | 193 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 194 |       sequence->addAction(positive1);
 | 
|---|
 | 195 |       sequence->addAction(positive2);
 | 
|---|
 | 196 |       CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 197 |       delete sequence;
 | 
|---|
 | 198 |     }
 | 
|---|
 | 199 |     {
 | 
|---|
 | 200 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 201 |       sequence->addAction(positive1);
 | 
|---|
 | 202 |       sequence->addAction(negative2);
 | 
|---|
 | 203 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 204 |       delete sequence;
 | 
|---|
 | 205 |     }
 | 
|---|
 | 206 |     {
 | 
|---|
 | 207 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 208 |       sequence->addAction(negative1);
 | 
|---|
 | 209 |       sequence->addAction(positive2);
 | 
|---|
 | 210 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 211 |       delete sequence;
 | 
|---|
 | 212 |     }
 | 
|---|
 | 213 |     {
 | 
|---|
 | 214 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 215 |       sequence->addAction(negative1);
 | 
|---|
 | 216 |       sequence->addAction(negative2);
 | 
|---|
 | 217 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 218 |       delete sequence;
 | 
|---|
 | 219 |     }
 | 
|---|
 | 220 |   }
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 |   // second section:
 | 
|---|
 | 223 |   {
 | 
|---|
 | 224 |     // empty sequence can be undone
 | 
|---|
 | 225 |     ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 226 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 227 |     // if only a positive action is contained it can be undone
 | 
|---|
 | 228 |     sequence->addAction(positive1);
 | 
|---|
 | 229 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 230 |     // the single negative action should block the process
 | 
|---|
 | 231 |     sequence->addAction(negative1);
 | 
|---|
 | 232 |     CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 233 |     // after removing the negative action all is well again
 | 
|---|
 | 234 |     sequence->removeLastAction();
 | 
|---|
 | 235 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 236 |     delete sequence;
 | 
|---|
 | 237 |   }
 | 
|---|
| [0229f9] | 238 | }
 | 
|---|
| [147339] | 239 | 
 | 
|---|
| [0229f9] | 240 | void ActionSequenceTest::doesCallTest(){
 | 
|---|
 | 241 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 242 |   sequence->addAction(shouldCall1);
 | 
|---|
 | 243 |   sequence->addAction(shouldCall2);
 | 
|---|
 | 244 |   sequence->addAction(shouldNotCall1);
 | 
|---|
 | 245 |   sequence->addAction(shouldNotCall2);
 | 
|---|
 | 246 |   sequence->removeLastAction();
 | 
|---|
 | 247 |   sequence->removeLastAction();
 | 
|---|
 | 248 | 
 | 
|---|
 | 249 |   sequence->callAll();
 | 
|---|
 | 250 | 
 | 
|---|
 | 251 |   CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
 | 
|---|
 | 252 |   CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
 | 
|---|
 | 253 |   CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
 | 
|---|
 | 254 |   CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
 | 
|---|
 | 255 | 
 | 
|---|
| [f59d81] | 256 |   delete sequence;
 | 
|---|
| [0229f9] | 257 | }
 | 
|---|
 | 258 | 
 | 
|---|
 | 259 | void ActionSequenceTest::doesUndoTest(){
 | 
|---|
 | 260 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
| [e4afb4] | 261 |   ActionTraits wasCalledTrait("wasCalledActionStub");
 | 
|---|
 | 262 |   wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 263 |   wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| [2efa90] | 264 |   sequence->addAction(wasCalled1);
 | 
|---|
 | 265 |   sequence->addAction(wasCalled2);
 | 
|---|
| [0229f9] | 266 | 
 | 
|---|
| [e4afb4] | 267 |   ActionTraits MakroTrait("Test MakroAction");
 | 
|---|
 | 268 |   MakroAction act(MakroTrait,sequence,false);
 | 
|---|
| [0229f9] | 269 | 
 | 
|---|
| [2efa90] | 270 |   act.call();
 | 
|---|
| [0229f9] | 271 | 
 | 
|---|
| [2efa90] | 272 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 273 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
 | 
|---|
| [0229f9] | 274 | 
 | 
|---|
| [2efa90] | 275 |   ActionHistory::getInstance().undoLast();
 | 
|---|
 | 276 | 
 | 
|---|
 | 277 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 278 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
 | 
|---|
| [147339] | 279 | 
 | 
|---|
 | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 | 
 | 
|---|