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