| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [94d5ac6] | 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/>.
 | 
|---|
| [bcf653] | 22 |  */
 | 
|---|
 | 23 | 
 | 
|---|
| [147339] | 24 | /*
 | 
|---|
| [f7c0c4] | 25 |  * ActionSequenceUnitTest.cpp
 | 
|---|
| [147339] | 26 |  *
 | 
|---|
 | 27 |  *  Created on: Dec 17, 2009
 | 
|---|
 | 28 |  *      Author: crueger
 | 
|---|
 | 29 |  */
 | 
|---|
 | 30 | 
 | 
|---|
| [bf3817] | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
| [147339] | 36 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 37 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 38 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 39 | 
 | 
|---|
| [f7c0c4] | 40 | #include "ActionSequenceUnitTest.hpp"
 | 
|---|
| [147339] | 41 | #include "Actions/Action.hpp"
 | 
|---|
| [628577] | 42 | #include "Actions/ActionQueue.hpp"
 | 
|---|
| [147339] | 43 | #include "Actions/ActionSequence.hpp"
 | 
|---|
| [2efa90] | 44 | #include "Actions/MakroAction.hpp"
 | 
|---|
| [147339] | 45 | 
 | 
|---|
| [fdcd1b] | 46 | #include "stubs/DummyUI.hpp"
 | 
|---|
| [112f90] | 47 | 
 | 
|---|
| [ce7fdc] | 48 | using namespace MoleCuilder;
 | 
|---|
 | 49 | 
 | 
|---|
| [9b6b2f] | 50 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 51 | #include "UnitTestMain.hpp"
 | 
|---|
 | 52 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | /********************************************** Test classes **************************************/
 | 
|---|
 | 55 | 
 | 
|---|
| [147339] | 56 | // Registers the fixture into the 'registry'
 | 
|---|
 | 57 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | /* some neccessary stubs for tests */
 | 
|---|
 | 60 | class canUndoActionStub : public Action
 | 
|---|
 | 61 | {
 | 
|---|
 | 62 | public:
 | 
|---|
| [3139b2] | 63 |   canUndoActionStub(const ActionTrait &_trait):
 | 
|---|
| [126867] | 64 |     Action(_trait){}
 | 
|---|
| [147339] | 65 |   virtual ~canUndoActionStub(){}
 | 
|---|
 | 66 | 
 | 
|---|
| [047878] | 67 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 68 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 69 |     return dialog;
 | 
|---|
| [80951de] | 70 |   }
 | 
|---|
 | 71 | 
 | 
|---|
| [b5b01e] | 72 |   virtual ActionState::ptr performCall(){
 | 
|---|
| [67e2b3] | 73 |     return Action::success;
 | 
|---|
 | 74 |   }
 | 
|---|
| [b5b01e] | 75 |   virtual ActionState::ptr performUndo(ActionState::ptr){
 | 
|---|
| [67e2b3] | 76 |     return Action::success;
 | 
|---|
 | 77 |   }
 | 
|---|
| [b5b01e] | 78 |   virtual ActionState::ptr performRedo(ActionState::ptr){
 | 
|---|
| [67e2b3] | 79 |     return Action::success;
 | 
|---|
 | 80 |   }
 | 
|---|
| [147339] | 81 |   virtual bool canUndo(){
 | 
|---|
 | 82 |     return true;
 | 
|---|
 | 83 |   }
 | 
|---|
| [67e2b3] | 84 |   virtual bool shouldUndo(){
 | 
|---|
 | 85 |     return true;
 | 
|---|
 | 86 |   }
 | 
|---|
| [46b181] | 87 |   void outputAsCLI(std::ostream &ost) const
 | 
|---|
 | 88 |   {}
 | 
|---|
| [477012] | 89 |   void outputAsPython(std::ostream &ost, const std::string &prefix) const
 | 
|---|
 | 90 |   {}
 | 
|---|
| [147339] | 91 | };
 | 
|---|
 | 92 | 
 | 
|---|
 | 93 | class cannotUndoActionStub : public Action
 | 
|---|
 | 94 | {
 | 
|---|
 | 95 | public:
 | 
|---|
| [3139b2] | 96 |   cannotUndoActionStub(const ActionTrait &_trait) :
 | 
|---|
| [126867] | 97 |     Action(_trait){}
 | 
|---|
| [147339] | 98 |   virtual ~cannotUndoActionStub(){}
 | 
|---|
 | 99 | 
 | 
|---|
| [047878] | 100 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 101 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 102 |     return dialog;
 | 
|---|
| [80951de] | 103 |   }
 | 
|---|
 | 104 | 
 | 
|---|
| [b5b01e] | 105 |   virtual ActionState::ptr performCall(){
 | 
|---|
| [67e2b3] | 106 |     return Action::success;
 | 
|---|
 | 107 |   }
 | 
|---|
| [b5b01e] | 108 |   virtual ActionState::ptr performUndo(ActionState::ptr){
 | 
|---|
| [67e2b3] | 109 |     return Action::success;
 | 
|---|
 | 110 |   }
 | 
|---|
| [b5b01e] | 111 |   virtual ActionState::ptr performRedo(ActionState::ptr){
 | 
|---|
| [67e2b3] | 112 |     return Action::success;
 | 
|---|
 | 113 |   }
 | 
|---|
| [147339] | 114 |   virtual bool canUndo(){
 | 
|---|
 | 115 |     return false;
 | 
|---|
 | 116 |   }
 | 
|---|
| [67e2b3] | 117 |   virtual bool shouldUndo(){
 | 
|---|
 | 118 |    return true;
 | 
|---|
 | 119 |   }
 | 
|---|
| [46b181] | 120 |   void outputAsCLI(std::ostream &ost) const
 | 
|---|
 | 121 |   {}
 | 
|---|
| [477012] | 122 |   void outputAsPython(std::ostream &ost, const std::string &prefix) const
 | 
|---|
 | 123 |   {}
 | 
|---|
| [147339] | 124 | };
 | 
|---|
 | 125 | 
 | 
|---|
| [0229f9] | 126 | class wasCalledActionStub : public Action
 | 
|---|
 | 127 | {
 | 
|---|
 | 128 | public:
 | 
|---|
| [3139b2] | 129 |   wasCalledActionStub(const ActionTrait &_trait) :
 | 
|---|
| [126867] | 130 |       Action(_trait),
 | 
|---|
| [0229f9] | 131 |       called(false)
 | 
|---|
 | 132 |   {}
 | 
|---|
 | 133 |   virtual ~wasCalledActionStub(){}
 | 
|---|
| [147339] | 134 | 
 | 
|---|
| [047878] | 135 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 136 |     return dialog;
 | 
|---|
| [80951de] | 137 |   }
 | 
|---|
| [b5b01e] | 138 |   virtual ActionState::ptr performCall(){
 | 
|---|
| [0229f9] | 139 |     called = true;
 | 
|---|
| [67e2b3] | 140 |     return Action::success;
 | 
|---|
| [0229f9] | 141 |   }
 | 
|---|
| [b5b01e] | 142 |   virtual ActionState::ptr performUndo(ActionState::ptr){
 | 
|---|
| [0229f9] | 143 |     called = false;
 | 
|---|
| [67e2b3] | 144 |     return Action::success;
 | 
|---|
 | 145 |   }
 | 
|---|
| [b5b01e] | 146 |   virtual ActionState::ptr performRedo(ActionState::ptr){
 | 
|---|
| [67e2b3] | 147 |     called = true;
 | 
|---|
 | 148 |     return Action::success;
 | 
|---|
| [0229f9] | 149 |   }
 | 
|---|
 | 150 |   virtual bool canUndo(){
 | 
|---|
 | 151 |     return true;
 | 
|---|
 | 152 |   }
 | 
|---|
| [67e2b3] | 153 |   virtual bool shouldUndo(){
 | 
|---|
 | 154 |     return true;
 | 
|---|
 | 155 |   }
 | 
|---|
| [46b181] | 156 |   void outputAsCLI(std::ostream &ost) const
 | 
|---|
 | 157 |   {}
 | 
|---|
| [477012] | 158 |   void outputAsPython(std::ostream &ost, const std::string &prefix) const
 | 
|---|
 | 159 |   {}
 | 
|---|
| [0229f9] | 160 |   bool wasCalled(){
 | 
|---|
 | 161 |     return called;
 | 
|---|
 | 162 |   }
 | 
|---|
 | 163 | private:
 | 
|---|
 | 164 |   bool called;
 | 
|---|
 | 165 | };
 | 
|---|
| [147339] | 166 | 
 | 
|---|
| [0229f9] | 167 | void ActionSequenceTest::setUp(){
 | 
|---|
| [694d84] | 168 |   hasDescriptor = false;
 | 
|---|
| [6367dd] | 169 |   // setup ActionHistory
 | 
|---|
 | 170 |   ActionQueue::getInstance();
 | 
|---|
| [112f90] | 171 |   // TODO: find a way to really reset the factory to a clean state in tear-down
 | 
|---|
 | 172 |   if(!hasDescriptor){
 | 
|---|
 | 173 |     UIFactory::registerFactory(new DummyUIFactory::description());
 | 
|---|
 | 174 |     hasDescriptor = true;
 | 
|---|
 | 175 |   }
 | 
|---|
 | 176 |   UIFactory::makeUserInterface("Dummy");
 | 
|---|
| [147339] | 177 |   // create some necessary stubs used in this test
 | 
|---|
| [3139b2] | 178 |   ActionTrait canUndoTrait("canUndoActionStub");
 | 
|---|
 | 179 |   ActionTrait cannotUndoTrait("cannotUndoActionStub");
 | 
|---|
| [e4afb4] | 180 |   positive1 = new canUndoActionStub(canUndoTrait);
 | 
|---|
 | 181 |   positive2 = new canUndoActionStub(canUndoTrait);
 | 
|---|
 | 182 |   negative1 = new cannotUndoActionStub(cannotUndoTrait);
 | 
|---|
 | 183 |   negative2 = new cannotUndoActionStub(cannotUndoTrait);
 | 
|---|
 | 184 | 
 | 
|---|
| [3139b2] | 185 |   ActionTrait wasCalledTrait("wasCalledActionStub");
 | 
|---|
| [e4afb4] | 186 |   shouldCall1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 187 |   shouldCall2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 188 |   shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 189 |   shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| [0229f9] | 190 | 
 | 
|---|
 | 191 | }
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 | void ActionSequenceTest::tearDown(){
 | 
|---|
 | 194 |   delete positive1;
 | 
|---|
 | 195 |   delete positive2;
 | 
|---|
 | 196 |   delete negative1;
 | 
|---|
 | 197 |   delete negative2;
 | 
|---|
| [147339] | 198 | 
 | 
|---|
| [0229f9] | 199 |   delete shouldCall1;
 | 
|---|
 | 200 |   delete shouldCall2;
 | 
|---|
 | 201 |   delete shouldNotCall1;
 | 
|---|
 | 202 |   delete shouldNotCall2;
 | 
|---|
 | 203 | 
 | 
|---|
| [628577] | 204 |   ActionQueue::purgeInstance();
 | 
|---|
| [694d84] | 205 |   {
 | 
|---|
 | 206 |     UIFactory::purgeInstance();
 | 
|---|
 | 207 |     hasDescriptor = false;
 | 
|---|
 | 208 |   }
 | 
|---|
| [0229f9] | 209 | }
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 | void ActionSequenceTest::canUndoTest(){
 | 
|---|
| [147339] | 212 |   // first section:
 | 
|---|
 | 213 |   {
 | 
|---|
 | 214 |     // test some combinations
 | 
|---|
 | 215 |     {
 | 
|---|
 | 216 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 217 |       sequence->addAction(positive1);
 | 
|---|
 | 218 |       sequence->addAction(positive2);
 | 
|---|
 | 219 |       CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 220 |       delete sequence;
 | 
|---|
 | 221 |     }
 | 
|---|
 | 222 |     {
 | 
|---|
 | 223 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 224 |       sequence->addAction(positive1);
 | 
|---|
 | 225 |       sequence->addAction(negative2);
 | 
|---|
 | 226 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 227 |       delete sequence;
 | 
|---|
 | 228 |     }
 | 
|---|
 | 229 |     {
 | 
|---|
 | 230 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 231 |       sequence->addAction(negative1);
 | 
|---|
 | 232 |       sequence->addAction(positive2);
 | 
|---|
 | 233 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 234 |       delete sequence;
 | 
|---|
 | 235 |     }
 | 
|---|
 | 236 |     {
 | 
|---|
 | 237 |       ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 238 |       sequence->addAction(negative1);
 | 
|---|
 | 239 |       sequence->addAction(negative2);
 | 
|---|
 | 240 |       CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 241 |       delete sequence;
 | 
|---|
 | 242 |     }
 | 
|---|
 | 243 |   }
 | 
|---|
 | 244 | 
 | 
|---|
 | 245 |   // second section:
 | 
|---|
 | 246 |   {
 | 
|---|
 | 247 |     // empty sequence can be undone
 | 
|---|
 | 248 |     ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 249 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 250 |     // if only a positive action is contained it can be undone
 | 
|---|
 | 251 |     sequence->addAction(positive1);
 | 
|---|
 | 252 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 253 |     // the single negative action should block the process
 | 
|---|
 | 254 |     sequence->addAction(negative1);
 | 
|---|
 | 255 |     CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
 | 
|---|
 | 256 |     // after removing the negative action all is well again
 | 
|---|
 | 257 |     sequence->removeLastAction();
 | 
|---|
 | 258 |     CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
 | 
|---|
 | 259 |     delete sequence;
 | 
|---|
 | 260 |   }
 | 
|---|
| [0229f9] | 261 | }
 | 
|---|
| [147339] | 262 | 
 | 
|---|
| [0229f9] | 263 | void ActionSequenceTest::doesCallTest(){
 | 
|---|
 | 264 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
 | 265 |   sequence->addAction(shouldCall1);
 | 
|---|
 | 266 |   sequence->addAction(shouldCall2);
 | 
|---|
 | 267 |   sequence->addAction(shouldNotCall1);
 | 
|---|
 | 268 |   sequence->addAction(shouldNotCall2);
 | 
|---|
 | 269 |   sequence->removeLastAction();
 | 
|---|
 | 270 |   sequence->removeLastAction();
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 |   sequence->callAll();
 | 
|---|
 | 273 | 
 | 
|---|
 | 274 |   CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
 | 
|---|
 | 275 |   CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
 | 
|---|
 | 276 |   CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
 | 
|---|
 | 277 |   CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
 | 
|---|
 | 278 | 
 | 
|---|
| [f59d81] | 279 |   delete sequence;
 | 
|---|
| [0229f9] | 280 | }
 | 
|---|
 | 281 | 
 | 
|---|
 | 282 | void ActionSequenceTest::doesUndoTest(){
 | 
|---|
 | 283 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
| [3139b2] | 284 |   ActionTrait wasCalledTrait("wasCalledActionStub");
 | 
|---|
| [e4afb4] | 285 |   wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 286 |   wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| [2efa90] | 287 |   sequence->addAction(wasCalled1);
 | 
|---|
 | 288 |   sequence->addAction(wasCalled2);
 | 
|---|
| [0229f9] | 289 | 
 | 
|---|
| [3139b2] | 290 |   ActionTrait MakroTrait("Test MakroAction");
 | 
|---|
| [126867] | 291 |   MakroAction act(MakroTrait,*sequence);
 | 
|---|
| [0229f9] | 292 | 
 | 
|---|
| [2efa90] | 293 |   act.call();
 | 
|---|
| [0229f9] | 294 | 
 | 
|---|
| [2efa90] | 295 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 296 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
 | 
|---|
| [0229f9] | 297 | 
 | 
|---|
| [6367dd] | 298 |   ActionQueue::getInstance().undoLast();
 | 
|---|
| [2efa90] | 299 | 
 | 
|---|
 | 300 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 301 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
 | 
|---|
| [147339] | 302 | 
 | 
|---|
| [22b786] | 303 |   delete sequence;
 | 
|---|
| [147339] | 304 | }
 | 
|---|
 | 305 | 
 | 
|---|
 | 306 | 
 | 
|---|