Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:44

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     unittest_changemanager
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Jan 18 10:19:07 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <boost/test/unit_test.hpp>
0015 #include <boost/test/test_tools.hpp>
0016 
0017 #include "TClass.h"
0018 
0019 // user include files
0020 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0021 #include "Fireworks/Core/interface/FWEventItem.h"
0022 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0023 #include "DataFormats/TrackReco/interface/Track.h"
0024 
0025 #include "Fireworks/Core/interface/FWItemAccessorBase.h"
0026 
0027 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0028 
0029 //
0030 // constants, enums and typedefs
0031 //
0032 namespace {
0033   struct Listener {
0034     Listener() : nHeard_(0) {}
0035     int nHeard_;
0036 
0037     void listen(const std::set<FWModelId>& iIds) { nHeard_ += iIds.size(); }
0038   };
0039 
0040   struct ItemListener {
0041     ItemListener() : nHeard_(0) {}
0042     int nHeard_;
0043 
0044     void listen(const FWEventItem* iItem) { ++nHeard_; }
0045   };
0046 
0047   class TestAccessor : public FWItemAccessorBase {
0048   public:
0049     TestAccessor(const reco::TrackCollection* iCollection) : m_collection(iCollection) {}
0050     virtual const void* modelData(int iIndex) const { return &((*m_collection)[iIndex]); }
0051     virtual const void* data() const { return m_collection; }
0052     virtual unsigned int size() const { return m_collection->size(); }
0053     virtual const TClass* modelType() const { return TClass::GetClass("reco::Track"); }
0054     virtual const TClass* type() const { return TClass::GetClass("std::vector<reco::Track>"); }
0055 
0056     virtual bool isCollection() const { return true; }
0057 
0058     ///override if id of an object should be different than the index
0059     //virtual std::string idForIndex(int iIndex) const;
0060     // ---------- member functions ---------------------------
0061     virtual void setData(const edm::ObjectWithDict&) {}
0062     virtual void reset() {}
0063 
0064   private:
0065     const reco::TrackCollection* m_collection;
0066   };
0067 }  // namespace
0068 
0069 BOOST_AUTO_TEST_CASE(changemanager) {
0070   FWModelChangeManager cm;
0071 
0072   //create an item
0073   reco::TrackCollection fVector;
0074   fVector.push_back(reco::Track());
0075   fVector.push_back(reco::Track());
0076   fVector.push_back(reco::Track());
0077 
0078   TClass* cls = TClass::GetClass("std::vector<reco::Track>");
0079   assert(0 != cls);
0080 
0081   fireworks::Context context(&cm, 0, 0, 0, 0);
0082 
0083   auto accessor = std::make_shared<TestAccessor>(&fVector);
0084   FWPhysicsObjectDesc pObj("Tracks", cls, "Tracks");
0085 
0086   FWEventItem item(&context, 0, accessor, pObj);
0087   //hack to force update of data
0088   edm::ObjectWithDict dummy;
0089   item.setData(dummy);
0090 
0091   cm.newItemSlot(&item);
0092 
0093   Listener listener;
0094   ItemListener iListener;
0095   //NOTE: have to pass a pointer to the listener else the bind will
0096   // create a copy of the listener and the original one will never
0097   // 'hear' any signal
0098   item.changed_.connect(std::bind(&Listener::listen, &listener, std::placeholders::_1));
0099   item.itemChanged_.connect(std::bind(&ItemListener::listen, &iListener, std::placeholders::_1));
0100 
0101   BOOST_CHECK(listener.nHeard_ == 0);
0102   BOOST_CHECK(iListener.nHeard_ == 0);
0103   cm.changed(FWModelId(&item, 0));
0104   BOOST_CHECK(listener.nHeard_ == 1);
0105   BOOST_CHECK(iListener.nHeard_ == 0);
0106 
0107   listener.nHeard_ = 0;
0108   cm.beginChanges();
0109   cm.changed(FWModelId(&item, 0));
0110   BOOST_CHECK(listener.nHeard_ == 0);
0111   cm.endChanges();
0112   BOOST_CHECK(listener.nHeard_ == 1);
0113 
0114   //sending same ID twice should give only 1 message
0115   listener.nHeard_ = 0;
0116   cm.beginChanges();
0117   cm.changed(FWModelId(&item, 0));
0118   BOOST_CHECK(listener.nHeard_ == 0);
0119   cm.changed(FWModelId(&item, 0));
0120   BOOST_CHECK(listener.nHeard_ == 0);
0121   cm.endChanges();
0122   BOOST_CHECK(listener.nHeard_ == 1);
0123 
0124   listener.nHeard_ = 0;
0125   cm.beginChanges();
0126   cm.changed(FWModelId(&item, 1));
0127   BOOST_CHECK(listener.nHeard_ == 0);
0128   cm.changed(FWModelId(&item, 2));
0129   BOOST_CHECK(listener.nHeard_ == 0);
0130   cm.endChanges();
0131   BOOST_CHECK(listener.nHeard_ == 2);
0132 
0133   listener.nHeard_ = 0;
0134   {
0135     FWChangeSentry sentry(cm);
0136     cm.changed(FWModelId(&item, 1));
0137     BOOST_CHECK(listener.nHeard_ == 0);
0138   }
0139   BOOST_CHECK(listener.nHeard_ == 1);
0140 
0141   BOOST_CHECK(iListener.nHeard_ == 0);
0142   item.setEvent(0);
0143   BOOST_CHECK(iListener.nHeard_ == 1);
0144 
0145   iListener.nHeard_ = 0;
0146   {
0147     FWChangeSentry sentry(cm);
0148     item.setEvent(0);
0149     BOOST_CHECK(iListener.nHeard_ == 0);
0150   }
0151   BOOST_CHECK(iListener.nHeard_ == 1);
0152 }