| [b2cfdb] | 1 | /*
 | 
|---|
 | 2 |  * manipulateAtomsTest.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Feb 18, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [bf3817] | 8 | // include config.h
 | 
|---|
 | 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 10 | #include <config.h>
 | 
|---|
 | 11 | #endif
 | 
|---|
 | 12 | 
 | 
|---|
| [b2cfdb] | 13 | #include "manipulateAtomsTest.hpp"
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | #include <cppunit/CompilerOutputter.h>
 | 
|---|
 | 16 | #include <cppunit/extensions/TestFactoryRegistry.h>
 | 
|---|
 | 17 | #include <cppunit/ui/text/TestRunner.h>
 | 
|---|
 | 18 | #include <iostream>
 | 
|---|
 | 19 | #include <boost/bind.hpp>
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | #include "Descriptors/AtomDescriptor.hpp"
 | 
|---|
 | 22 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
 | 23 | #include "Actions/ManipulateAtomsProcess.hpp"
 | 
|---|
 | 24 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [975a11] | 25 | #include "Actions/ActionHistory.hpp"
 | 
|---|
| [b2cfdb] | 26 | 
 | 
|---|
 | 27 | #include "World.hpp"
 | 
|---|
 | 28 | #include "atom.hpp"
 | 
|---|
 | 29 | 
 | 
|---|
| [112f90] | 30 | #include "DummyUI.hpp"
 | 
|---|
 | 31 | 
 | 
|---|
| [7ca806] | 32 | #ifdef HAVE_TESTRUNNER
 | 
|---|
 | 33 | #include "UnitTestMain.hpp"
 | 
|---|
 | 34 | #endif /*HAVE_TESTRUNNER*/
 | 
|---|
 | 35 | 
 | 
|---|
| [b2cfdb] | 36 | // Registers the fixture into the 'registry'
 | 
|---|
 | 37 | CPPUNIT_TEST_SUITE_REGISTRATION( manipulateAtomsTest );
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | // some stubs
 | 
|---|
 | 40 | class AtomStub : public atom {
 | 
|---|
 | 41 | public:
 | 
|---|
 | 42 |   AtomStub(int _id) :
 | 
|---|
 | 43 |   atom(),
 | 
|---|
| [23b547] | 44 |   manipulated(false),
 | 
|---|
 | 45 |   id(_id)
 | 
|---|
| [b2cfdb] | 46 |   {}
 | 
|---|
 | 47 | 
 | 
|---|
| [57adc7] | 48 |   virtual atomId_t getId(){
 | 
|---|
| [b2cfdb] | 49 |     return id;
 | 
|---|
 | 50 |   }
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |   virtual void doSomething(){
 | 
|---|
 | 53 |     manipulated = true;
 | 
|---|
 | 54 |   }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |   bool manipulated;
 | 
|---|
 | 57 | private:
 | 
|---|
| [57adc7] | 58 |   atomId_t id;
 | 
|---|
| [b2cfdb] | 59 | };
 | 
|---|
 | 60 | 
 | 
|---|
| [afb47f] | 61 | class countObserver : public Observer{
 | 
|---|
 | 62 | public:
 | 
|---|
 | 63 |   countObserver() :
 | 
|---|
| [cd5047] | 64 |     Observer("countObserver"),
 | 
|---|
| [afb47f] | 65 |     count(0)
 | 
|---|
 | 66 |     {}
 | 
|---|
 | 67 |   virtual ~countObserver(){}
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |   void update(Observable *){
 | 
|---|
 | 70 |     count++;
 | 
|---|
 | 71 |   }
 | 
|---|
 | 72 | 
 | 
|---|
 | 73 |   void subjectKilled(Observable *)
 | 
|---|
 | 74 |   {}
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |   int count;
 | 
|---|
 | 77 | };
 | 
|---|
| [b2cfdb] | 78 | 
 | 
|---|
 | 79 | // set up and tear down
 | 
|---|
 | 80 | void manipulateAtomsTest::setUp(){
 | 
|---|
| [112f90] | 81 |   static bool hasDescriptor = false;
 | 
|---|
| [975a11] | 82 |   ActionHistory::init();
 | 
|---|
| [23b547] | 83 |   World::getInstance();
 | 
|---|
| [112f90] | 84 |   // TODO: find a way to really reset the factory to a clean state in tear-down
 | 
|---|
 | 85 |   if(!hasDescriptor){
 | 
|---|
 | 86 |     UIFactory::registerFactory(new DummyUIFactory::description());
 | 
|---|
 | 87 |     hasDescriptor = true;
 | 
|---|
 | 88 |   }
 | 
|---|
 | 89 |   UIFactory::makeUserInterface("Dummy");
 | 
|---|
| [b2cfdb] | 90 |   for(int i=0;i<ATOM_COUNT;++i){
 | 
|---|
 | 91 |     atoms[i]= new AtomStub(i);
 | 
|---|
| [23b547] | 92 |     World::getInstance().registerAtom(atoms[i]);
 | 
|---|
| [b2cfdb] | 93 |   }
 | 
|---|
 | 94 | }
 | 
|---|
 | 95 | void manipulateAtomsTest::tearDown(){
 | 
|---|
| [23b547] | 96 |   World::purgeInstance();
 | 
|---|
| [e73a8a2] | 97 |   ActionRegistry::purgeInstance();
 | 
|---|
| [975a11] | 98 |   ActionHistory::purgeInstance();
 | 
|---|
| [112f90] | 99 |   UIFactory::purgeInstance();
 | 
|---|
| [b2cfdb] | 100 | }
 | 
|---|
 | 101 | 
 | 
|---|
| [a1510d] | 102 | static void operation(atom* _atom){
 | 
|---|
| [b2cfdb] | 103 |   AtomStub *atom = dynamic_cast<AtomStub*>(_atom);
 | 
|---|
| [6d574a] | 104 |   CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 105 |   atom->doSomething();
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 | void manipulateAtomsTest::testManipulateSimple(){
 | 
|---|
| [23b547] | 110 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| [b2cfdb] | 111 |   proc->call();
 | 
|---|
| [23b547] | 112 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| [b2cfdb] | 113 |   std::vector<atom*>::iterator iter;
 | 
|---|
 | 114 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
 | 115 |     AtomStub *atom;
 | 
|---|
 | 116 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| [6d574a] | 117 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 118 |     CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
 | 119 |   }
 | 
|---|
 | 120 | }
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | void manipulateAtomsTest::testManipulateExcluded(){
 | 
|---|
| [229e3c] | 123 | 
 | 
|---|
| [23b547] | 124 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms() && !AtomById(ATOM_COUNT/2));
 | 
|---|
| [b2cfdb] | 125 |   proc->call();
 | 
|---|
| [23b547] | 126 |   std::vector<atom*> allAtoms = World::getInstance().getAllAtoms(AllAtoms());
 | 
|---|
| [b2cfdb] | 127 |   std::vector<atom*>::iterator iter;
 | 
|---|
 | 128 |   for(iter=allAtoms.begin();iter!=allAtoms.end();++iter){
 | 
|---|
 | 129 |     AtomStub *atom;
 | 
|---|
 | 130 |     atom = dynamic_cast<AtomStub*>(*iter);
 | 
|---|
| [6d574a] | 131 |     CPPUNIT_ASSERT(atom);
 | 
|---|
| [b2cfdb] | 132 |     if(atom->getId()!=(int)ATOM_COUNT/2)
 | 
|---|
 | 133 |       CPPUNIT_ASSERT(atom->manipulated);
 | 
|---|
 | 134 |     else
 | 
|---|
 | 135 |       CPPUNIT_ASSERT(!atom->manipulated);
 | 
|---|
 | 136 |   }
 | 
|---|
 | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
| [afb47f] | 139 | void manipulateAtomsTest::testObserver(){
 | 
|---|
 | 140 |   countObserver *obs = new countObserver();
 | 
|---|
| [23b547] | 141 |   World::getInstance().signOn(obs);
 | 
|---|
| [6e97e5] | 142 |   ManipulateAtomsProcess *proc = World::getInstance().manipulateAtoms(boost::bind(operation,_1),"FOO",AllAtoms());
 | 
|---|
| [afb47f] | 143 |   proc->call();
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 |   CPPUNIT_ASSERT_EQUAL(1,obs->count);
 | 
|---|
| [23b547] | 146 |   World::getInstance().signOff(obs);
 | 
|---|
| [afb47f] | 147 |   delete obs;
 | 
|---|
 | 148 | }
 | 
|---|