| 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/ActionQueue.hpp" | 
|---|
| 43 | #include "Actions/ActionSequence.hpp" | 
|---|
| 44 | #include "Actions/MakroAction.hpp" | 
|---|
| 45 |  | 
|---|
| 46 | #include "stubs/DummyUI.hpp" | 
|---|
| 47 |  | 
|---|
| 48 | using namespace MoleCuilder; | 
|---|
| 49 |  | 
|---|
| 50 | #ifdef HAVE_TESTRUNNER | 
|---|
| 51 | #include "UnitTestMain.hpp" | 
|---|
| 52 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 53 |  | 
|---|
| 54 | /********************************************** Test classes **************************************/ | 
|---|
| 55 |  | 
|---|
| 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: | 
|---|
| 63 | canUndoActionStub(const ActionTrait &_trait): | 
|---|
| 64 | Action(_trait){} | 
|---|
| 65 | virtual ~canUndoActionStub(){} | 
|---|
| 66 |  | 
|---|
| 67 | virtual Dialog* fillDialog(Dialog *dialog){ | 
|---|
| 68 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 69 | return dialog; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | virtual ActionState::ptr performCall(){ | 
|---|
| 73 | return Action::success; | 
|---|
| 74 | } | 
|---|
| 75 | virtual ActionState::ptr performUndo(ActionState::ptr){ | 
|---|
| 76 | return Action::success; | 
|---|
| 77 | } | 
|---|
| 78 | virtual ActionState::ptr performRedo(ActionState::ptr){ | 
|---|
| 79 | return Action::success; | 
|---|
| 80 | } | 
|---|
| 81 | virtual bool canUndo(){ | 
|---|
| 82 | return true; | 
|---|
| 83 | } | 
|---|
| 84 | virtual bool shouldUndo(){ | 
|---|
| 85 | return true; | 
|---|
| 86 | } | 
|---|
| 87 | void outputAsCLI(std::ostream &ost) const | 
|---|
| 88 | {} | 
|---|
| 89 | void outputAsPython(std::ostream &ost, const std::string &prefix) const | 
|---|
| 90 | {} | 
|---|
| 91 | }; | 
|---|
| 92 |  | 
|---|
| 93 | class cannotUndoActionStub : public Action | 
|---|
| 94 | { | 
|---|
| 95 | public: | 
|---|
| 96 | cannotUndoActionStub(const ActionTrait &_trait) : | 
|---|
| 97 | Action(_trait){} | 
|---|
| 98 | virtual ~cannotUndoActionStub(){} | 
|---|
| 99 |  | 
|---|
| 100 | virtual Dialog* fillDialog(Dialog *dialog){ | 
|---|
| 101 | ASSERT(dialog,"No Dialog given when filling action dialog"); | 
|---|
| 102 | return dialog; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | virtual ActionState::ptr performCall(){ | 
|---|
| 106 | return Action::success; | 
|---|
| 107 | } | 
|---|
| 108 | virtual ActionState::ptr performUndo(ActionState::ptr){ | 
|---|
| 109 | return Action::success; | 
|---|
| 110 | } | 
|---|
| 111 | virtual ActionState::ptr performRedo(ActionState::ptr){ | 
|---|
| 112 | return Action::success; | 
|---|
| 113 | } | 
|---|
| 114 | virtual bool canUndo(){ | 
|---|
| 115 | return false; | 
|---|
| 116 | } | 
|---|
| 117 | virtual bool shouldUndo(){ | 
|---|
| 118 | return true; | 
|---|
| 119 | } | 
|---|
| 120 | void outputAsCLI(std::ostream &ost) const | 
|---|
| 121 | {} | 
|---|
| 122 | void outputAsPython(std::ostream &ost, const std::string &prefix) const | 
|---|
| 123 | {} | 
|---|
| 124 | }; | 
|---|
| 125 |  | 
|---|
| 126 | class wasCalledActionStub : public Action | 
|---|
| 127 | { | 
|---|
| 128 | public: | 
|---|
| 129 | wasCalledActionStub(const ActionTrait &_trait) : | 
|---|
| 130 | Action(_trait), | 
|---|
| 131 | called(false) | 
|---|
| 132 | {} | 
|---|
| 133 | virtual ~wasCalledActionStub(){} | 
|---|
| 134 |  | 
|---|
| 135 | virtual Dialog* fillDialog(Dialog *dialog){ | 
|---|
| 136 | return dialog; | 
|---|
| 137 | } | 
|---|
| 138 | virtual ActionState::ptr performCall(){ | 
|---|
| 139 | called = true; | 
|---|
| 140 | return Action::success; | 
|---|
| 141 | } | 
|---|
| 142 | virtual ActionState::ptr performUndo(ActionState::ptr){ | 
|---|
| 143 | called = false; | 
|---|
| 144 | return Action::success; | 
|---|
| 145 | } | 
|---|
| 146 | virtual ActionState::ptr performRedo(ActionState::ptr){ | 
|---|
| 147 | called = true; | 
|---|
| 148 | return Action::success; | 
|---|
| 149 | } | 
|---|
| 150 | virtual bool canUndo(){ | 
|---|
| 151 | return true; | 
|---|
| 152 | } | 
|---|
| 153 | virtual bool shouldUndo(){ | 
|---|
| 154 | return true; | 
|---|
| 155 | } | 
|---|
| 156 | void outputAsCLI(std::ostream &ost) const | 
|---|
| 157 | {} | 
|---|
| 158 | void outputAsPython(std::ostream &ost, const std::string &prefix) const | 
|---|
| 159 | {} | 
|---|
| 160 | bool wasCalled(){ | 
|---|
| 161 | return called; | 
|---|
| 162 | } | 
|---|
| 163 | private: | 
|---|
| 164 | bool called; | 
|---|
| 165 | }; | 
|---|
| 166 |  | 
|---|
| 167 | void ActionSequenceTest::setUp(){ | 
|---|
| 168 | hasDescriptor = false; | 
|---|
| 169 | // setup ActionHistory | 
|---|
| 170 | ActionQueue::getInstance(); | 
|---|
| 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"); | 
|---|
| 177 | // create some necessary stubs used in this test | 
|---|
| 178 | ActionTrait canUndoTrait("canUndoActionStub"); | 
|---|
| 179 | ActionTrait cannotUndoTrait("cannotUndoActionStub"); | 
|---|
| 180 | positive1 = new canUndoActionStub(canUndoTrait); | 
|---|
| 181 | positive2 = new canUndoActionStub(canUndoTrait); | 
|---|
| 182 | negative1 = new cannotUndoActionStub(cannotUndoTrait); | 
|---|
| 183 | negative2 = new cannotUndoActionStub(cannotUndoTrait); | 
|---|
| 184 |  | 
|---|
| 185 | ActionTrait wasCalledTrait("wasCalledActionStub"); | 
|---|
| 186 | shouldCall1 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 187 | shouldCall2 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 188 | shouldNotCall1 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 189 | shouldNotCall2 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 190 |  | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | void ActionSequenceTest::tearDown(){ | 
|---|
| 194 | delete positive1; | 
|---|
| 195 | delete positive2; | 
|---|
| 196 | delete negative1; | 
|---|
| 197 | delete negative2; | 
|---|
| 198 |  | 
|---|
| 199 | delete shouldCall1; | 
|---|
| 200 | delete shouldCall2; | 
|---|
| 201 | delete shouldNotCall1; | 
|---|
| 202 | delete shouldNotCall2; | 
|---|
| 203 |  | 
|---|
| 204 | ActionQueue::purgeInstance(); | 
|---|
| 205 | { | 
|---|
| 206 | UIFactory::purgeInstance(); | 
|---|
| 207 | hasDescriptor = false; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | void ActionSequenceTest::canUndoTest(){ | 
|---|
| 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 | } | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 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 |  | 
|---|
| 279 | delete sequence; | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | void ActionSequenceTest::doesUndoTest(){ | 
|---|
| 283 | ActionSequence *sequence = new ActionSequence(); | 
|---|
| 284 | ActionTrait wasCalledTrait("wasCalledActionStub"); | 
|---|
| 285 | wasCalledActionStub *wasCalled1 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 286 | wasCalledActionStub *wasCalled2 = new wasCalledActionStub(wasCalledTrait); | 
|---|
| 287 | sequence->addAction(wasCalled1); | 
|---|
| 288 | sequence->addAction(wasCalled2); | 
|---|
| 289 |  | 
|---|
| 290 | ActionTrait MakroTrait("Test MakroAction"); | 
|---|
| 291 | MakroAction act(MakroTrait,*sequence); | 
|---|
| 292 |  | 
|---|
| 293 | act.call(); | 
|---|
| 294 |  | 
|---|
| 295 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled()); | 
|---|
| 296 | CPPUNIT_ASSERT_EQUAL(true,wasCalled2->wasCalled()); | 
|---|
| 297 |  | 
|---|
| 298 | ActionQueue::getInstance().undoLast(); | 
|---|
| 299 |  | 
|---|
| 300 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled()); | 
|---|
| 301 | CPPUNIT_ASSERT_EQUAL(false,wasCalled2->wasCalled()); | 
|---|
| 302 |  | 
|---|
| 303 | delete sequence; | 
|---|
| 304 | } | 
|---|
| 305 |  | 
|---|
| 306 |  | 
|---|