[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 |
|
---|
| 37 | virtual void call(){}
|
---|
| 38 | virtual void undo(){}
|
---|
| 39 | virtual bool canUndo(){
|
---|
| 40 | return true;
|
---|
| 41 | }
|
---|
| 42 | };
|
---|
| 43 |
|
---|
| 44 | class cannotUndoActionStub : public Action
|
---|
| 45 | {
|
---|
| 46 | public:
|
---|
[cc04b7] | 47 | cannotUndoActionStub() : Action("cannotUndoActionStub",false){}
|
---|
[147339] | 48 | virtual ~cannotUndoActionStub(){}
|
---|
| 49 |
|
---|
| 50 | virtual void call(){}
|
---|
| 51 | virtual void undo(){}
|
---|
| 52 | virtual bool canUndo(){
|
---|
| 53 | return false;
|
---|
| 54 | }
|
---|
| 55 | };
|
---|
| 56 |
|
---|
[0229f9] | 57 | class wasCalledActionStub : public Action
|
---|
| 58 | {
|
---|
| 59 | public:
|
---|
| 60 | wasCalledActionStub() :
|
---|
[cc04b7] | 61 | Action("wasCalledActionStub",false),
|
---|
[0229f9] | 62 | called(false)
|
---|
| 63 | {}
|
---|
| 64 | virtual ~wasCalledActionStub(){}
|
---|
[147339] | 65 |
|
---|
[0229f9] | 66 | virtual void call(){
|
---|
| 67 | called = true;
|
---|
| 68 | }
|
---|
| 69 | virtual void undo(){
|
---|
| 70 | called = false;
|
---|
| 71 | }
|
---|
| 72 | virtual bool canUndo(){
|
---|
| 73 | return true;
|
---|
| 74 | }
|
---|
| 75 | bool wasCalled(){
|
---|
| 76 | return called;
|
---|
| 77 | }
|
---|
| 78 | private:
|
---|
| 79 | bool called;
|
---|
| 80 | };
|
---|
[147339] | 81 |
|
---|
[0229f9] | 82 | void ActionSequenceTest::setUp(){
|
---|
[147339] | 83 | // create some necessary stubs used in this test
|
---|
[0229f9] | 84 | positive1 = new canUndoActionStub();
|
---|
| 85 | positive2 = new canUndoActionStub();
|
---|
| 86 | negative1 = new cannotUndoActionStub();
|
---|
| 87 | negative2 = new cannotUndoActionStub();
|
---|
| 88 |
|
---|
| 89 | shouldCall1 = new wasCalledActionStub();
|
---|
| 90 | shouldCall2 = new wasCalledActionStub();
|
---|
| 91 | shouldNotCall1 = new wasCalledActionStub();
|
---|
| 92 | shouldNotCall2 = new wasCalledActionStub();
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void ActionSequenceTest::tearDown(){
|
---|
| 97 | delete positive1;
|
---|
| 98 | delete positive2;
|
---|
| 99 | delete negative1;
|
---|
| 100 | delete negative2;
|
---|
[147339] | 101 |
|
---|
[0229f9] | 102 | delete shouldCall1;
|
---|
| 103 | delete shouldCall2;
|
---|
| 104 | delete shouldNotCall1;
|
---|
| 105 | delete shouldNotCall2;
|
---|
| 106 |
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void ActionSequenceTest::canUndoTest(){
|
---|
[147339] | 110 | // first section:
|
---|
| 111 | {
|
---|
| 112 | // test some combinations
|
---|
| 113 | {
|
---|
| 114 | ActionSequence *sequence = new ActionSequence();
|
---|
| 115 | sequence->addAction(positive1);
|
---|
| 116 | sequence->addAction(positive2);
|
---|
| 117 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 118 | delete sequence;
|
---|
| 119 | }
|
---|
| 120 | {
|
---|
| 121 | ActionSequence *sequence = new ActionSequence();
|
---|
| 122 | sequence->addAction(positive1);
|
---|
| 123 | sequence->addAction(negative2);
|
---|
| 124 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 125 | delete sequence;
|
---|
| 126 | }
|
---|
| 127 | {
|
---|
| 128 | ActionSequence *sequence = new ActionSequence();
|
---|
| 129 | sequence->addAction(negative1);
|
---|
| 130 | sequence->addAction(positive2);
|
---|
| 131 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 132 | delete sequence;
|
---|
| 133 | }
|
---|
| 134 | {
|
---|
| 135 | ActionSequence *sequence = new ActionSequence();
|
---|
| 136 | sequence->addAction(negative1);
|
---|
| 137 | sequence->addAction(negative2);
|
---|
| 138 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 139 | delete sequence;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | // second section:
|
---|
| 144 | {
|
---|
| 145 | // empty sequence can be undone
|
---|
| 146 | ActionSequence *sequence = new ActionSequence();
|
---|
| 147 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 148 | // if only a positive action is contained it can be undone
|
---|
| 149 | sequence->addAction(positive1);
|
---|
| 150 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 151 | // the single negative action should block the process
|
---|
| 152 | sequence->addAction(negative1);
|
---|
| 153 | CPPUNIT_ASSERT_EQUAL( false, sequence->canUndo() );
|
---|
| 154 | // after removing the negative action all is well again
|
---|
| 155 | sequence->removeLastAction();
|
---|
| 156 | CPPUNIT_ASSERT_EQUAL( true, sequence->canUndo() );
|
---|
| 157 | delete sequence;
|
---|
| 158 | }
|
---|
[0229f9] | 159 | }
|
---|
[147339] | 160 |
|
---|
[0229f9] | 161 | void ActionSequenceTest::doesCallTest(){
|
---|
| 162 | ActionSequence *sequence = new ActionSequence();
|
---|
| 163 | sequence->addAction(shouldCall1);
|
---|
| 164 | sequence->addAction(shouldCall2);
|
---|
| 165 | sequence->addAction(shouldNotCall1);
|
---|
| 166 | sequence->addAction(shouldNotCall2);
|
---|
| 167 | sequence->removeLastAction();
|
---|
| 168 | sequence->removeLastAction();
|
---|
| 169 |
|
---|
| 170 | sequence->callAll();
|
---|
| 171 |
|
---|
| 172 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
| 173 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
| 174 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
| 175 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
| 176 |
|
---|
[f59d81] | 177 | delete sequence;
|
---|
[0229f9] | 178 | }
|
---|
| 179 |
|
---|
| 180 | void ActionSequenceTest::doesUndoTest(){
|
---|
| 181 | ActionSequence *sequence = new ActionSequence();
|
---|
| 182 | sequence->addAction(shouldNotCall1);
|
---|
| 183 | sequence->addAction(shouldNotCall2);
|
---|
| 184 | sequence->addAction(shouldCall1);
|
---|
| 185 | sequence->addAction(shouldCall2);
|
---|
| 186 |
|
---|
| 187 | sequence->callAll();
|
---|
| 188 |
|
---|
| 189 | sequence->removeLastAction();
|
---|
| 190 | sequence->removeLastAction();
|
---|
| 191 |
|
---|
| 192 | sequence->undoAll();
|
---|
| 193 |
|
---|
| 194 | CPPUNIT_ASSERT_EQUAL(true,shouldCall1->wasCalled());
|
---|
| 195 | CPPUNIT_ASSERT_EQUAL(true,shouldCall2->wasCalled());
|
---|
| 196 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall1->wasCalled());
|
---|
| 197 | CPPUNIT_ASSERT_EQUAL(false,shouldNotCall2->wasCalled());
|
---|
[147339] | 198 |
|
---|
[f59d81] | 199 | delete sequence;
|
---|
[147339] | 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|