[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 |
|
---|
[9b6b2f] | 19 | #ifdef HAVE_TESTRUNNER
|
---|
| 20 | #include "UnitTestMain.hpp"
|
---|
| 21 | #endif /*HAVE_TESTRUNNER*/
|
---|
| 22 |
|
---|
| 23 | /********************************************** Test classes **************************************/
|
---|
| 24 |
|
---|
[147339] | 25 | // Registers the fixture into the 'registry'
|
---|
| 26 | CPPUNIT_TEST_SUITE_REGISTRATION( ActionSequenceTest );
|
---|
| 27 |
|
---|
| 28 | /* some neccessary stubs for tests */
|
---|
| 29 | class canUndoActionStub : public Action
|
---|
| 30 | {
|
---|
| 31 | public:
|
---|
[cc04b7] | 32 | canUndoActionStub(): Action("canUndoActionStub",false){}
|
---|
[147339] | 33 | virtual ~canUndoActionStub(){}
|
---|
| 34 |
|
---|
[5b0b98] | 35 | virtual Action::state_ptr performCall(){
|
---|
[67e2b3] | 36 | return Action::success;
|
---|
| 37 | }
|
---|
[5b0b98] | 38 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
[67e2b3] | 39 | return Action::success;
|
---|
| 40 | }
|
---|
[5b0b98] | 41 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
[67e2b3] | 42 | return Action::success;
|
---|
| 43 | }
|
---|
[147339] | 44 | virtual bool canUndo(){
|
---|
| 45 | return true;
|
---|
| 46 | }
|
---|
[67e2b3] | 47 | virtual bool shouldUndo(){
|
---|
| 48 | return true;
|
---|
| 49 | }
|
---|
[147339] | 50 | };
|
---|
| 51 |
|
---|
| 52 | class cannotUndoActionStub : public Action
|
---|
| 53 | {
|
---|
| 54 | public:
|
---|
[cc04b7] | 55 | cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
|
---|
[147339] | 56 | virtual ~cannotUndoActionStub(){}
|
---|
| 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 false;
|
---|
| 69 | }
|
---|
[67e2b3] | 70 | virtual bool shouldUndo(){
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
[147339] | 73 | };
|
---|
| 74 |
|
---|
[0229f9] | 75 | class wasCalledActionStub : public Action
|
---|
| 76 | {
|
---|
| 77 | public:
|
---|
| 78 | wasCalledActionStub() :
|
---|
[cc04b7] | 79 | Action("wasCalledActionStub",false),
|
---|
[0229f9] | 80 | called(false)
|
---|
| 81 | {}
|
---|
| 82 | virtual ~wasCalledActionStub(){}
|
---|
[147339] | 83 |
|
---|
[5b0b98] | 84 | virtual Action::state_ptr performCall(){
|
---|
[0229f9] | 85 | called = true;
|
---|
[67e2b3] | 86 | return Action::success;
|
---|
[0229f9] | 87 | }
|
---|
[5b0b98] | 88 | virtual Action::state_ptr performUndo(Action::state_ptr){
|
---|
[0229f9] | 89 | called = false;
|
---|
[67e2b3] | 90 | return Action::success;
|
---|
| 91 | }
|
---|
[5b0b98] | 92 | virtual Action::state_ptr performRedo(Action::state_ptr){
|
---|
[67e2b3] | 93 | called = true;
|
---|
| 94 | return Action::success;
|
---|
[0229f9] | 95 | }
|
---|
| 96 | virtual bool canUndo(){
|
---|
| 97 | return true;
|
---|
| 98 | }
|
---|
[67e2b3] | 99 | virtual bool shouldUndo(){
|
---|
| 100 | return true;
|
---|
| 101 | }
|
---|
[0229f9] | 102 | bool wasCalled(){
|
---|
| 103 | return called;
|
---|
| 104 | }
|
---|
| 105 | private:
|
---|
| 106 | bool called;
|
---|
| 107 | };
|
---|
[147339] | 108 |
|
---|
[0229f9] | 109 | void ActionSequenceTest::setUp(){
|
---|
[2efa90] | 110 | ActionHistory::init();
|
---|
[147339] | 111 | // create some necessary stubs used in this test
|
---|
[0229f9] | 112 | positive1 = new canUndoActionStub();
|
---|
| 113 | positive2 = new canUndoActionStub();
|
---|
| 114 | negative1 = new cannotUndoActionStub();
|
---|
| 115 | negative2 = new cannotUndoActionStub();
|
---|
| 116 |
|
---|
| 117 | shouldCall1 = new wasCalledActionStub();
|
---|
| 118 | shouldCall2 = new wasCalledActionStub();
|
---|
| 119 | shouldNotCall1 = new wasCalledActionStub();
|
---|
| 120 | shouldNotCall2 = new wasCalledActionStub();
|
---|
| 121 |
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | void ActionSequenceTest::tearDown(){
|
---|
| 125 | delete positive1;
|
---|
| 126 | delete positive2;
|
---|
| 127 | delete negative1;
|
---|
| 128 | delete negative2;
|
---|
[147339] | 129 |
|
---|
[0229f9] | 130 | delete shouldCall1;
|
---|
| 131 | delete shouldCall2;
|
---|
| 132 | delete shouldNotCall1;
|
---|
| 133 | delete shouldNotCall2;
|
---|
| 134 |
|
---|
[2efa90] | 135 | ActionHistory::purgeInstance();
|
---|
| 136 | ActionRegistry::purgeInstance();
|
---|
[0229f9] | 137 | }
|
---|
| 138 |
|
---|
| 139 | void ActionSequenceTest::canUndoTest(){
|
---|
[147339] | 140 | // first section:
|
---|
| 141 | {
|
---|
| 142 | // test some combinations
|
---|
| 143 | {
|
---|
| 144 | ActionSequence *sequence = new ActionSequence();
|
---|
| 145 | sequence->addAction(positive1);
|
---|
| 146 | sequence->addAction(positive2);
|
---|
| 147 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 148 | delete sequence;
|
---|
| 149 | }
|
---|
| 150 | {
|
---|
| 151 | ActionSequence *sequence = new ActionSequence();
|
---|
| 152 | sequence->addAction(positive1);
|
---|
| 153 | sequence->addAction(negative2);
|
---|
| 154 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 155 | delete sequence;
|
---|
| 156 | }
|
---|
| 157 | {
|
---|
| 158 | ActionSequence *sequence = new ActionSequence();
|
---|
| 159 | sequence->addAction(negative1);
|
---|
| 160 | sequence->addAction(positive2);
|
---|
| 161 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 162 | delete sequence;
|
---|
| 163 | }
|
---|
| 164 | {
|
---|
| 165 | ActionSequence *sequence = new ActionSequence();
|
---|
| 166 | sequence->addAction(negative1);
|
---|
| 167 | sequence->addAction(negative2);
|
---|
| 168 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 169 | delete sequence;
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | // second section:
|
---|
| 174 | {
|
---|
| 175 | // empty sequence can be undone
|
---|
| 176 | ActionSequence *sequence = new ActionSequence();
|
---|
| 177 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 178 | // if only a positive action is contained it can be undone
|
---|
| 179 | sequence->addAction(positive1);
|
---|
| 180 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 181 | // the single negative action should block the process
|
---|
| 182 | sequence->addAction(negative1);
|
---|
| 183 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 184 | // after removing the negative action all is well again
|
---|
| 185 | sequence->removeLastAction();
|
---|
| 186 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 187 | delete sequence;
|
---|
| 188 | }
|
---|
[0229f9] | 189 | }
|
---|
[147339] | 190 |
|
---|
[0229f9] | 191 | void ActionSequenceTest::doesCallTest(){
|
---|
| 192 | ActionSequence *sequence = new ActionSequence();
|
---|
| 193 | sequence->addAction(shouldCall1);
|
---|
| 194 | sequence->addAction(shouldCall2);
|
---|
| 195 | sequence->addAction(shouldNotCall1);
|
---|
| 196 | sequence->addAction(shouldNotCall2);
|
---|
| 197 | sequence->removeLastAction();
|
---|
| 198 | sequence->removeLastAction();
|
---|
| 199 |
|
---|
| 200 | sequence->callAll();
|
---|
| 201 |
|
---|
| 202 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
| 203 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
| 204 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
| 205 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
| 206 |
|
---|
[f59d81] | 207 | delete sequence;
|
---|
[0229f9] | 208 | }
|
---|
| 209 |
|
---|
| 210 | void ActionSequenceTest::doesUndoTest(){
|
---|
| 211 | ActionSequence *sequence = new ActionSequence();
|
---|
[2efa90] | 212 | wasCalledActionStub *wasCalled1 = new wasCalledActionStub();
|
---|
| 213 | wasCalledActionStub *wasCalled2 = new wasCalledActionStub();
|
---|
| 214 | sequence->addAction(wasCalled1);
|
---|
| 215 | sequence->addAction(wasCalled2);
|
---|
[0229f9] | 216 |
|
---|
[2efa90] | 217 | MakroAction act("Test MakroAction",sequence,false);
|
---|
[0229f9] | 218 |
|
---|
[2efa90] | 219 | act.call();
|
---|
[0229f9] | 220 |
|
---|
[2efa90] | 221 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
|
---|
| 222 | CPPUNIT_ASSERT_EQUAL(true,wasCalled1->wasCalled());
|
---|
[0229f9] | 223 |
|
---|
[2efa90] | 224 | ActionHistory::getInstance().undoLast();
|
---|
| 225 |
|
---|
| 226 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
|
---|
| 227 | CPPUNIT_ASSERT_EQUAL(false,wasCalled1->wasCalled());
|
---|
[147339] | 228 |
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 |
|
---|