| 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/ActionSequence.hpp"
 | 
|---|
| 43 | #include "Actions/MakroAction.hpp"
 | 
|---|
| 44 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| 45 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| 46 | 
 | 
|---|
| 47 | #include "stubs/DummyUI.hpp"
 | 
|---|
| 48 | 
 | 
|---|
| 49 | using namespace MoleCuilder;
 | 
|---|
| 50 | 
 | 
|---|
| 51 | #ifdef HAVE_TESTRUNNER
 | 
|---|
| 52 | #include "UnitTestMain.hpp"
 | 
|---|
| 53 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
| 54 | 
 | 
|---|
| 55 | /********************************************** Test classes **************************************/
 | 
|---|
| 56 | 
 | 
|---|
| 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:
 | 
|---|
| 64 |   canUndoActionStub(const ActionTrait &_trait):
 | 
|---|
| 65 |     Action(_trait,false){}
 | 
|---|
| 66 |   virtual ~canUndoActionStub(){}
 | 
|---|
| 67 | 
 | 
|---|
| 68 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
| 69 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 70 |     return dialog;
 | 
|---|
| 71 |   }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |   virtual Action::state_ptr performCall(){
 | 
|---|
| 74 |     return Action::success;
 | 
|---|
| 75 |   }
 | 
|---|
| 76 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| 77 |     return Action::success;
 | 
|---|
| 78 |   }
 | 
|---|
| 79 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| 80 |     return Action::success;
 | 
|---|
| 81 |   }
 | 
|---|
| 82 |   virtual bool canUndo(){
 | 
|---|
| 83 |     return true;
 | 
|---|
| 84 |   }
 | 
|---|
| 85 |   virtual bool shouldUndo(){
 | 
|---|
| 86 |     return true;
 | 
|---|
| 87 |   }
 | 
|---|
| 88 | };
 | 
|---|
| 89 | 
 | 
|---|
| 90 | class cannotUndoActionStub : public Action
 | 
|---|
| 91 | {
 | 
|---|
| 92 | public:
 | 
|---|
| 93 |   cannotUndoActionStub(const ActionTrait &_trait) :
 | 
|---|
| 94 |     Action(_trait,false){}
 | 
|---|
| 95 |   virtual ~cannotUndoActionStub(){}
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
| 98 |     ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| 99 |     return dialog;
 | 
|---|
| 100 |   }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   virtual Action::state_ptr performCall(){
 | 
|---|
| 103 |     return Action::success;
 | 
|---|
| 104 |   }
 | 
|---|
| 105 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| 106 |     return Action::success;
 | 
|---|
| 107 |   }
 | 
|---|
| 108 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| 109 |     return Action::success;
 | 
|---|
| 110 |   }
 | 
|---|
| 111 |   virtual bool canUndo(){
 | 
|---|
| 112 |     return false;
 | 
|---|
| 113 |   }
 | 
|---|
| 114 |   virtual bool shouldUndo(){
 | 
|---|
| 115 |    return true;
 | 
|---|
| 116 |   }
 | 
|---|
| 117 | };
 | 
|---|
| 118 | 
 | 
|---|
| 119 | class wasCalledActionStub : public Action
 | 
|---|
| 120 | {
 | 
|---|
| 121 | public:
 | 
|---|
| 122 |   wasCalledActionStub(const ActionTrait &_trait) :
 | 
|---|
| 123 |       Action(_trait,false),
 | 
|---|
| 124 |       called(false)
 | 
|---|
| 125 |   {}
 | 
|---|
| 126 |   virtual ~wasCalledActionStub(){}
 | 
|---|
| 127 | 
 | 
|---|
| 128 |   virtual Dialog* fillDialog(Dialog *dialog){
 | 
|---|
| 129 |     return dialog;
 | 
|---|
| 130 |   }
 | 
|---|
| 131 |   virtual Action::state_ptr performCall(){
 | 
|---|
| 132 |     called = true;
 | 
|---|
| 133 |     return Action::success;
 | 
|---|
| 134 |   }
 | 
|---|
| 135 |   virtual Action::state_ptr performUndo(Action::state_ptr){
 | 
|---|
| 136 |     called = false;
 | 
|---|
| 137 |     return Action::success;
 | 
|---|
| 138 |   }
 | 
|---|
| 139 |   virtual Action::state_ptr performRedo(Action::state_ptr){
 | 
|---|
| 140 |     called = true;
 | 
|---|
| 141 |     return Action::success;
 | 
|---|
| 142 |   }
 | 
|---|
| 143 |   virtual bool canUndo(){
 | 
|---|
| 144 |     return true;
 | 
|---|
| 145 |   }
 | 
|---|
| 146 |   virtual bool shouldUndo(){
 | 
|---|
| 147 |     return true;
 | 
|---|
| 148 |   }
 | 
|---|
| 149 |   bool wasCalled(){
 | 
|---|
| 150 |     return called;
 | 
|---|
| 151 |   }
 | 
|---|
| 152 | private:
 | 
|---|
| 153 |   bool called;
 | 
|---|
| 154 | };
 | 
|---|
| 155 | 
 | 
|---|
| 156 | void ActionSequenceTest::setUp(){
 | 
|---|
| 157 |   hasDescriptor = false;
 | 
|---|
| 158 |   ActionHistory::init();
 | 
|---|
| 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 | 
 | 
|---|
| 181 | void 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 |   ActionHistory::purgeInstance();
 | 
|---|
| 193 |   ActionRegistry::purgeInstance();
 | 
|---|
| 194 |   {
 | 
|---|
| 195 |     UIFactory::purgeInstance();
 | 
|---|
| 196 |     hasDescriptor = false;
 | 
|---|
| 197 |   }
 | 
|---|
| 198 | }
 | 
|---|
| 199 | 
 | 
|---|
| 200 | void ActionSequenceTest::canUndoTest(){
 | 
|---|
| 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 |   }
 | 
|---|
| 250 | }
 | 
|---|
| 251 | 
 | 
|---|
| 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 | 
 | 
|---|
| 268 |   delete sequence;
 | 
|---|
| 269 | }
 | 
|---|
| 270 | 
 | 
|---|
| 271 | void ActionSequenceTest::doesUndoTest(){
 | 
|---|
| 272 |   ActionSequence *sequence = new ActionSequence();
 | 
|---|
| 273 |   ActionTrait wasCalledTrait("wasCalledActionStub");
 | 
|---|
| 274 |   wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| 275 |   wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait);
 | 
|---|
| 276 |   sequence->addAction(wasCalled1);
 | 
|---|
| 277 |   sequence->addAction(wasCalled2);
 | 
|---|
| 278 | 
 | 
|---|
| 279 |   ActionTrait MakroTrait("Test MakroAction");
 | 
|---|
| 280 |   MakroAction act(MakroTrait,*sequence,false);
 | 
|---|
| 281 | 
 | 
|---|
| 282 |   act.call();
 | 
|---|
| 283 | 
 | 
|---|
| 284 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
 | 
|---|
| 285 |   CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled());
 | 
|---|
| 286 | 
 | 
|---|
| 287 |   ActionHistory::getInstance().undoLast();
 | 
|---|
| 288 | 
 | 
|---|
| 289 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
 | 
|---|
| 290 |   CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled());
 | 
|---|
| 291 | 
 | 
|---|
| 292 |   delete sequence;
 | 
|---|
| 293 | }
 | 
|---|
| 294 | 
 | 
|---|
| 295 | 
 | 
|---|