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