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