Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-29 01:21:25

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 
0016 #define BOOST_TEST_DYN_LINK
0017 #define BOOST_TEST_MAIN
0018 #include <boost/test/unit_test.hpp>
0019 #include "TClass.h"
0020 
0021 // user include files
0022 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0023 #include "DataFormats/TrackReco/interface/Track.h"
0024 #include "FWCore/Reflection/interface/ObjectWithDict.h"
0025 #include "Fireworks/Core/interface/FWEventItem.h"
0026 
0027 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0028 
0029 #include "Fireworks/Core/interface/FWSelectionManager.h"
0030 
0031 #include "Fireworks/Core/interface/FWItemAccessorBase.h"
0032 
0033 //
0034 // constants, enums and typedefs
0035 //
0036 namespace {
0037   struct Listener {
0038     Listener() : nHeard_(0), nMessages_(0) {}
0039     int nHeard_;
0040     int nMessages_;
0041 
0042     void reset() {
0043       nHeard_ = 0;
0044       nMessages_ = 0;
0045     }
0046     void listen(const FWSelectionManager& iSM) {
0047       nHeard_ += iSM.selected().size();
0048       ++nMessages_;
0049     }
0050   };
0051 
0052   class TestAccessor : public FWItemAccessorBase {
0053   public:
0054     TestAccessor(const reco::TrackCollection* iCollection) : m_collection(iCollection) {}
0055     virtual const void* modelData(int iIndex) const { return &((*m_collection)[iIndex]); }
0056     virtual const void* data() const { return m_collection; }
0057     virtual unsigned int size() const { return m_collection->size(); }
0058     virtual const TClass* modelType() const { return TClass::GetClass("reco::Track"); }
0059     virtual const TClass* type() const { return TClass::GetClass("reco::TrackCollection"); }
0060 
0061     virtual bool isCollection() const { return true; }
0062 
0063     ///override if id of an object should be different than the index
0064     //virtual std::string idForIndex(int iIndex) const;
0065     // ---------- member functions ---------------------------
0066     virtual void setData(const edm::ObjectWithDict&) {}
0067     virtual void reset() {}
0068 
0069   private:
0070     const reco::TrackCollection* m_collection;
0071   };
0072 
0073 }  // namespace
0074 
0075 BOOST_AUTO_TEST_CASE(selectionmanager) {
0076   FWModelChangeManager cm;
0077 
0078   FWSelectionManager sm(&cm);
0079 
0080   Listener listener;
0081   //NOTE: have to pass a pointer to the listener else the bind will
0082   // create a copy of the listener and the original one will never
0083   // 'hear' any signal
0084   sm.selectionChanged_.connect(std::bind(&Listener::listen, &listener, std::placeholders::_1));
0085 
0086   reco::TrackCollection fVector;
0087   fVector.push_back(reco::Track());
0088   fVector.push_back(reco::Track());
0089   fVector.push_back(reco::Track());
0090 
0091   TClass* cls = TClass::GetClass("std::vector<reco::Track>");
0092   assert(0 != cls);
0093 
0094   fireworks::Context context(&cm, &sm, 0, 0, 0);
0095 
0096   auto accessor = std::make_shared<TestAccessor>(&fVector);
0097   FWPhysicsObjectDesc pObj("Tracks", cls, "Tracks");
0098 
0099   FWEventItem item(&context, 0, accessor, pObj);
0100   //hack to force update of data
0101   edm::ObjectWithDict dummy;
0102   item.setData(dummy);
0103 
0104   cm.newItemSlot(&item);
0105   BOOST_CHECK(listener.nHeard_ == 0);
0106   BOOST_CHECK(listener.nMessages_ == 0);
0107   BOOST_CHECK(sm.selected().empty());
0108 
0109   item.select(0);
0110   BOOST_CHECK(listener.nHeard_ == 1);
0111   BOOST_CHECK(listener.nMessages_ == 1);
0112   BOOST_CHECK(sm.selected().size() == 1);
0113   BOOST_CHECK(item.modelInfo(0).m_isSelected);
0114 
0115   //selecting the same should not change the state
0116   item.select(0);
0117   BOOST_CHECK(listener.nHeard_ == 1);
0118   BOOST_CHECK(listener.nMessages_ == 1);
0119   BOOST_CHECK(sm.selected().size() == 1);
0120 
0121   listener.reset();
0122   item.unselect(0);
0123   BOOST_CHECK(listener.nHeard_ == 0);
0124   BOOST_CHECK(listener.nMessages_ == 1);
0125   BOOST_CHECK(sm.selected().empty());
0126   BOOST_CHECK(not item.modelInfo(0).m_isSelected);
0127 
0128   item.select(0);
0129   BOOST_CHECK(sm.selected().size() == 1);
0130   listener.reset();
0131   sm.clearSelection();
0132   BOOST_CHECK(listener.nHeard_ == 0);
0133   BOOST_CHECK(listener.nMessages_ == 1);
0134   BOOST_CHECK(sm.selected().empty());
0135   BOOST_CHECK(not item.modelInfo(0).m_isSelected);
0136   {
0137     //test waiting
0138     item.select(0);
0139     listener.reset();
0140 
0141     FWChangeSentry sentry(cm);
0142     sm.clearSelection();
0143     BOOST_CHECK(listener.nHeard_ == 0);
0144     BOOST_CHECK(listener.nMessages_ == 0);
0145     BOOST_CHECK(sm.selected().size() == 1);
0146     item.select(1);
0147     BOOST_CHECK(listener.nHeard_ == 0);
0148     BOOST_CHECK(listener.nMessages_ == 0);
0149     BOOST_CHECK(sm.selected().size() == 1);
0150     item.select(2);
0151     BOOST_CHECK(listener.nHeard_ == 0);
0152     BOOST_CHECK(listener.nMessages_ == 0);
0153     BOOST_CHECK(sm.selected().size() == 1);
0154   }
0155   BOOST_CHECK(listener.nHeard_ == 2);
0156   BOOST_CHECK(listener.nMessages_ == 1);
0157   BOOST_CHECK(sm.selected().size() == 2);
0158   BOOST_CHECK(not item.modelInfo(0).m_isSelected);
0159   BOOST_CHECK(item.modelInfo(1).m_isSelected);
0160   BOOST_CHECK(item.modelInfo(2).m_isSelected);
0161 
0162   sm.clearSelection();
0163   item.select(1);
0164   item.select(2);
0165   BOOST_CHECK(sm.selected().size() == 2);
0166   item.setEvent(0);
0167   BOOST_CHECK(sm.selected().size() == 0);
0168 }