Lumiera  0.pre.03
»edit your freedom«
session-element-tracker-test.cpp
Go to the documentation of this file.
1 /*
2  SessionElementTracker(Test) - check the facility to track and expose selected model elements
3 
4  Copyright (C) Lumiera.org
5  2008, 2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 
30 #include "lib/element-tracker.hpp"
31 
32 #include "steam/assetmanager.hpp"
34 #include "steam/asset/timeline.hpp"
35 #include "steam/asset/sequence.hpp"
36 #include "common/query.hpp"
37 #include "lib/p.hpp"
38 
39 
40 
41 namespace steam {
42 namespace mobject {
43 namespace session {
44 namespace test {
45 
46  namespace { // yet another accounting dummy
47 
48  uint instance = 0;
49  int checksum = 0;
50 
51  using lib::AutoRegistered;
52 
60  struct Dummy
61  : AutoRegistered<Dummy>
62  {
63  const uint id_;
64 
65  Dummy()
66  : id_(++instance)
67  {
68  checksum += id_;
69  }
70  // to be created by factory...
71  friend class AutoRegistered<Dummy>;
72 
73  public:
74  void
75  detach() // demonstrates how to hook into the cleanup-operation
76  {
78  checksum -= id_;
79  }
80  };
81 
82  bool
83  operator== (Dummy const& d1, Dummy const& d2)
84  {
85  return util::isSameObject (d1, d2);
86  }
87 
88  } // (End) test dummy and helper
89 
90 
91 
92  using lib::P;
93  using lumiera::Query;
94  using asset::Timeline;
95  using asset::PTimeline;
96  using asset::AssetManager;
97 
98 
99  /****************************************************************************/
113  class SessionElementTracker_test : public Test
114  {
115  virtual void
116  run (Arg)
117  {
118  verify_trackingMechanism();
119  verify_integration();
120  }
121 
122 
123  void
124  verify_trackingMechanism()
125  {
126  instance = 0;
127  checksum = 0;
128  {
129  typedef Dummy AutoRegisteringDummy;
131  typedef lib::ElementTracker<Dummy> DummyRegistry;
132 
133  DummyRegistry trackedDummies;
134 
135  CHECK (0 == checksum);
136  CHECK (0 == trackedDummies.size());
137 
138  AutoRegisteringDummy::setRegistryInstance (trackedDummies);
139  PDummy dummy1 = AutoRegisteringDummy::create();
140  PDummy dummy2 = AutoRegisteringDummy::create();
141 
142  CHECK (2 == trackedDummies.size());
143  CHECK (dummy1 == trackedDummies[0]);
144  CHECK (dummy2 == trackedDummies[1]);
145 
146  PDummy dummy3 = AutoRegisteringDummy::create();
147  CHECK (3 == trackedDummies.size());
148  CHECK (dummy3 == trackedDummies[2]);
149 
150  CHECK (1+2+3 == checksum);
151 
152  dummy2->detach();
153  CHECK (1 + 3 == checksum);
154  CHECK (2 == trackedDummies.size());
155  CHECK (dummy1 == trackedDummies[0]);
156  CHECK (dummy3 == trackedDummies[1]);
157 
158  CHECK (1 == dummy2.use_count());
159  CHECK (2 == dummy1.use_count());
160  CHECK (2 == dummy3.use_count());
161 
162  // deliberately discard our reference,
163  // so the only remaining ref is within the registry
164  dummy1.reset();
165  dummy3.reset();
166  CHECK (!dummy1);
167  CHECK ( dummy2);
168  CHECK (!dummy3);
169  CHECK (1 == trackedDummies[0].use_count());
170  CHECK (1 == trackedDummies[1].use_count());
171  CHECK (1 + 3 == checksum);
172 
173  // now the tracker goes out of scope...
174  }
175  CHECK (0 == checksum); // ...remaining elements have been unlinked
176  }
177 
178 
179  void
180  verify_integration()
181  {
183  CHECK (Session::current.isUp());
184 
185  PSess sess = Session::current;
186  CHECK (sess->isValid());
187 
188  uint num_timelines = sess->timelines.size();
189  CHECK (0 < num_timelines);
190 
191  PTimeline specialTimeline (asset::Struct::retrieve.newInstance<Timeline> ("testical"));
192  CHECK (specialTimeline);
193  CHECK (num_timelines + 1 == sess->timelines.size());
194  CHECK (specialTimeline == sess->timelines[num_timelines]); // got appended at the end of the tracking table
195  CHECK (specialTimeline.use_count() == 3); // we, the AssetManager and the session
196 
197  PTimeline anotherTimeline (asset::Struct::retrieve.newInstance<Timeline>());
198  CHECK (num_timelines + 2 == sess->timelines.size());
199  CHECK (specialTimeline == sess->timelines[num_timelines]);
200  CHECK (anotherTimeline == sess->timelines[num_timelines+1]); // new one got appended at the end
201 
203  CHECK (assetM.known (specialTimeline->getID()));
204  assetM.remove (specialTimeline->getID());
205  CHECK (!assetM.known (specialTimeline->getID()));
206 
207  CHECK (num_timelines + 1 == sess->timelines.size());
208  CHECK (anotherTimeline == sess->timelines[num_timelines]); // moved to the previous slot
209  CHECK (specialTimeline.use_count() == 1); // we're holding the last reference
210 
211  verify_cleanup (anotherTimeline);
212  }
213 
214 
217  void
218  verify_cleanup (PTimeline const& aTimeline_in_session)
219  {
220  CHECK (1 < aTimeline_in_session.use_count(), "test object should still be attached to session");
222  CHECK (1 == aTimeline_in_session.use_count(), "session reset should have de-registered the test object");
223  }
224  };
225 
226 
228  LAUNCHER (SessionElementTracker_test, "unit session");
229 
230 
231 
232 }}}} // namespace steam::mobject::session::test
Steam-Layer Interface: Asset Lookup and Organisation.
virtual void reset()=0
reset all session config and start with a pristine default session.
Basic and generic representation of an internal query.
Test Dummy: to be created through the inherited static create(), managed by smart-ptr.
Definition: run.hpp:49
void remove(IDA id)
remove the given asset from the internal DB.
Customised refcounting smart pointer.
Facade for the Asset subsystem.
static session::SessManager & current
access point to the current Session
Definition: session.hpp:129
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:74
static lib::Depend< AssetManager > instance
get at the system-wide asset manager instance.
Registry for tracking object instances.
Helper mixin template for implementing a type intended to participate in automatic element tracking...
static StructFactory retrieve
storage for the static StructFactory instance
Definition: struct.hpp:116
Structural building block of the session: a sequence of clips.
Simple test class runner.
creation, access and Session lifecycle Interface.
Definition: session.hpp:161
bool operator==(LocatingPin const &pin1, LocatingPin const &pin2)
check for equivalent definition of a complete locating chain
Tracking instances automatically on creation and disposal.
Customised refcounting smart pointer template, built upon std::shared_ptr, but forwarding type relati...
Definition: trait.hpp:80
Primary Interface to the current Session.
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:279
Top level structural element within the session.
TimelineAccess timelines
collection of timelines (top level)
Definition: session.hpp:133