| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [bcf653] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
| [147339] | 23 | /*
 | 
|---|
| [f7c0c4] | 24 |  * ActionSequenceUnitTest.cpp
 | 
|---|
| [147339] | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Dec 17, 2009
 | 
|---|
 | 27 |  *      Author: crueger
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
| [bf3817] | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
| [147339] | 35 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 36 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 37 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 38 | 
 | 
|---|
| [f7c0c4] | 39 | #include "ActionSequenceUnitTest.hpp"
 | 
|---|
| [147339] | 40 | #include "Actions/Action.hpp"
 | 
|---|
 | 41 | #include "Actions/ActionSequence.hpp"
 | 
|---|
| [2efa90] | 42 | #include "Actions/MakroAction.hpp"
 | 
|---|
 | 43 | #include "Actions/ActionHistory.hpp"
 | 
|---|
 | 44 | #include "Actions/ActionRegistry.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):
 | 
|---|
| [e4afb4] | 64 |     Action(_trait,false){}
 | 
|---|
| [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 | 
 | 
|---|
| [5b0b98] | 72 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [67e2b3] | 73 |     return Action::success;
 | 
|---|
 | 74 |   }
 | 
|---|
| [5b0b98] | 75 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 76 |     return Action::success;
 | 
|---|
 | 77 |   }
 | 
|---|
| [5b0b98] | 78 |   virtual Action::state_ptr performRedo(Action::state_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 |   }
 | 
|---|
| [147339] | 87 | };
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 | class cannotUndoActionStub : public Action
 | 
|---|
 | 90 | {
 | 
|---|
 | 91 | public:
 | 
|---|
| [3139b2] | 92 |   cannotUndoActionStub(const ActionTrait &_trait) :
 | 
|---|
| [e4afb4] | 93 |     Action(_trait,false){}
 | 
|---|
| [147339] | 94 |   virtual ~cannotUndoActionStub(){}
 | 
|---|
 | 95 | 
 | 
|---|
| [047878] | 96 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 97 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
 | 98 |     return dialog;
 | 
|---|
| [80951de] | 99 |   }
 | 
|---|
 | 100 | 
 | 
|---|
| [5b0b98] | 101 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [67e2b3] | 102 |     return Action::success;
 | 
|---|
 | 103 |   }
 | 
|---|
| [5b0b98] | 104 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 105 |     return Action::success;
 | 
|---|
 | 106 |   }
 | 
|---|
| [5b0b98] | 107 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 108 |     return Action::success;
 | 
|---|
 | 109 |   }
 | 
|---|
| [147339] | 110 |   virtual bool canUndo(){
 | 
|---|
 | 111 |     return false;
 | 
|---|
 | 112 |   }
 | 
|---|
| [67e2b3] | 113 |   virtual bool shouldUndo(){
 | 
|---|
 | 114 |    return true;
 | 
|---|
 | 115 |   }
 | 
|---|
| [147339] | 116 | };
 | 
|---|
 | 117 | 
 | 
|---|
| [0229f9] | 118 | class wasCalledActionStub : public Action
 | 
|---|
 | 119 | {
 | 
|---|
 | 120 | public:
 | 
|---|
| [3139b2] | 121 |   wasCalledActionStub(const ActionTrait &_trait) :
 | 
|---|
| [e4afb4] | 122 |       Action(_trait,false),
 | 
|---|
| [0229f9] | 123 |       called(false)
 | 
|---|
 | 124 |   {}
 | 
|---|
 | 125 |   virtual ~wasCalledActionStub(){}
 | 
|---|
| [147339] | 126 | 
 | 
|---|
| [047878] | 127 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
 | 128 |     return dialog;
 | 
|---|
| [80951de] | 129 |   }
 | 
|---|
| [5b0b98] | 130 |   virtual Action::state_ptr performCall(){
 | 
|---|
| [0229f9] | 131 |     called = true;
 | 
|---|
| [67e2b3] | 132 |     return Action::success;
 | 
|---|
| [0229f9] | 133 |   }
 | 
|---|
| [5b0b98] | 134 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| [0229f9] | 135 |     called = false;
 | 
|---|
| [67e2b3] | 136 |     return Action::success;
 | 
|---|
 | 137 |   }
 | 
|---|
| [5b0b98] | 138 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| [67e2b3] | 139 |     called = true;
 | 
|---|
 | 140 |     return Action::success;
 | 
|---|
| [0229f9] | 141 |   }
 | 
|---|
 | 142 |   virtual bool canUndo(){
 | 
|---|
 | 143 |     return true;
 | 
|---|
 | 144 |   }
 | 
|---|
| [67e2b3] | 145 |   virtual bool shouldUndo(){
 | 
|---|
 | 146 |     return true;
 | 
|---|
 | 147 |   }
 | 
|---|
| [0229f9] | 148 |   bool wasCalled(){
 | 
|---|
 | 149 |     return called;
 | 
|---|
 | 150 |   }
 | 
|---|
 | 151 | private:
 | 
|---|
 | 152 |   bool called;
 | 
|---|
 | 153 | };
 | 
|---|
| [147339] | 154 | 
 | 
|---|
| [0229f9] | 155 | void ActionSequenceTest::setUp(){
 | 
|---|
| [694d84] | 156 |   hasDescriptor = false;
 | 
|---|
| [2efa90] | 157 |   ActionHistory::init();
 | 
|---|
| [112f90] | 158 |   // TODO: find a way to really reset the factory to a clean state in tear-down
 | 
|---|
 | 159 |   if(!hasDescriptor){
 | 
|---|
 | 160 |     UIFactory::registerFactory(new DummyUIFactory::description());
 | 
|---|
 | 161 |     hasDescriptor = true;
 | 
|---|
 | 162 |   }
 | 
|---|
 | 163 |   UIFactory::makeUserInterface("Dummy");
 | 
|---|
| [147339] | 164 |   // create some necessary stubs used in this test
 | 
|---|
| [3139b2] | 165 |   ActionTrait canUndoTrait("canUndoActionStub");
 | 
|---|
 | 166 |   ActionTrait cannotUndoTrait("cannotUndoActionStub");
 | 
|---|
| [e4afb4] | 167 |   positive1 = new canUndoActionStub(canUndoTrait);
 | 
|---|
 | 168 |   positive2 = new canUndoActionStub(canUndoTrait);
 | 
|---|
 | 169 |   negative1 = new cannotUndoActionStub(cannotUndoTrait);
 | 
|---|
 | 170 |   negative2 = new cannotUndoActionStub(cannotUndoTrait);
 | 
|---|
 | 171 | 
 | 
|---|
| [3139b2] | 172 |   ActionTrait wasCalledTrait("wasCalledActionStub");
 | 
|---|
| [e4afb4] | 173 |   shouldCall1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 174 |   shouldCall2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 175 |   shouldNotCall1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 176 |   shouldNotCall2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| [0229f9] | 177 | 
 | 
|---|
 | 178 | }
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 | void ActionSequenceTest::tearDown(){
 | 
|---|
 | 181 |   delete positive1;
 | 
|---|
 | 182 |   delete positive2;
 | 
|---|
 | 183 |   delete negative1;
 | 
|---|
 | 184 |   delete negative2;
 | 
|---|
| [147339] | 185 | 
 | 
|---|
| [0229f9] | 186 |   delete shouldCall1;
 | 
|---|
 | 187 |   delete shouldCall2;
 | 
|---|
 | 188 |   delete shouldNotCall1;
 | 
|---|
 | 189 |   delete shouldNotCall2;
 | 
|---|
 | 190 | 
 | 
|---|
| [2efa90] | 191 |   ActionHistory::purgeInstance();
 | 
|---|
 | 192 |   ActionRegistry::purgeInstance();
 | 
|---|
| [694d84] | 193 |   {
 | 
|---|
 | 194 |     UIFactory::purgeInstance();
 | 
|---|
 | 195 |     hasDescriptor = false;
 | 
|---|
 | 196 |   }
 | 
|---|
| [0229f9] | 197 | }
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 | void ActionSequenceTest::canUndoTest(){
 | 
|---|
| [147339] | 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 |   }
 | 
|---|
| [0229f9] | 249 | }
 | 
|---|
| [147339] | 250 | 
 | 
|---|
| [0229f9] | 251 | void 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 | 
 | 
|---|
| [f59d81] | 267 |   delete sequence;
 | 
|---|
| [0229f9] | 268 | }
 | 
|---|
 | 269 | 
 | 
|---|
 | 270 | void ActionSequenceTest::doesUndoTest(){
 | 
|---|
 | 271 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
| [3139b2] | 272 |   ActionTrait wasCalledTrait("wasCalledActionStub");
 | 
|---|
| [e4afb4] | 273 |   wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
 | 274 |   wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| [2efa90] | 275 |   sequence->addAction(wasCalled1);
 | 
|---|
 | 276 |   sequence->addAction(wasCalled2);
 | 
|---|
| [0229f9] | 277 | 
 | 
|---|
| [3139b2] | 278 |   ActionTrait MakroTrait("Test MakroAction");
 | 
|---|
| [e4afb4] | 279 |   MakroAction act(MakroTrait,sequence,false);
 | 
|---|
| [0229f9] | 280 | 
 | 
|---|
| [2efa90] | 281 |   act.call();
 | 
|---|
| [0229f9] | 282 | 
 | 
|---|
| [2efa90] | 283 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 284 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
 | 
|---|
| [0229f9] | 285 | 
 | 
|---|
| [2efa90] | 286 |   ActionHistory::getInstance().undoLast();
 | 
|---|
 | 287 | 
 | 
|---|
 | 288 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
 | 
|---|
| [ec149d] | 289 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
 | 
|---|
| [147339] | 290 | 
 | 
|---|
 | 291 | }
 | 
|---|
 | 292 | 
 | 
|---|
 | 293 | 
 | 
|---|