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