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