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)
5  2008, 2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
19 #include "lib/test/run.hpp"
20 
21 #include "lib/element-tracker.hpp"
22 
23 #include "steam/assetmanager.hpp"
25 #include "steam/asset/timeline.hpp"
26 #include "steam/asset/sequence.hpp"
27 #include "common/query.hpp"
28 #include "lib/p.hpp"
29 
30 
31 
32 namespace steam {
33 namespace mobject {
34 namespace session {
35 namespace test {
36 
37  namespace { // yet another accounting dummy
38 
39  uint instance = 0;
40  int checksum = 0;
41 
42  using lib::AutoRegistered;
43 
51  struct Dummy
52  : AutoRegistered<Dummy>
53  {
54  const uint id_;
55 
56  Dummy()
57  : id_(++instance)
58  {
59  checksum += id_;
60  }
61  // to be created by factory...
62  friend class AutoRegistered<Dummy>;
63 
64  public:
65  void
66  detach() // demonstrates how to hook into the cleanup-operation
67  {
69  checksum -= id_;
70  }
71  };
72 
73  bool
74  operator== (Dummy const& d1, Dummy const& d2)
75  {
76  return util::isSameObject (d1, d2);
77  }
78 
79  } // (End) test dummy and helper
80 
81 
82 
83  using lib::P;
84  using lumiera::Query;
85  using asset::Timeline;
86  using asset::PTimeline;
87  using asset::AssetManager;
88 
89 
90  /****************************************************************************/
104  class SessionElementTracker_test : public Test
105  {
106  virtual void
107  run (Arg)
108  {
109  verify_trackingMechanism();
110  verify_integration();
111  }
112 
113 
114  void
115  verify_trackingMechanism()
116  {
117  instance = 0;
118  checksum = 0;
119  {
120  typedef Dummy AutoRegisteringDummy;
122  typedef lib::ElementTracker<Dummy> DummyRegistry;
123 
124  DummyRegistry trackedDummies;
125 
126  CHECK (0 == checksum);
127  CHECK (0 == trackedDummies.size());
128 
129  AutoRegisteringDummy::setRegistryInstance (trackedDummies);
130  PDummy dummy1 = AutoRegisteringDummy::create();
131  PDummy dummy2 = AutoRegisteringDummy::create();
132 
133  CHECK (2 == trackedDummies.size());
134  CHECK (dummy1 == trackedDummies[0]);
135  CHECK (dummy2 == trackedDummies[1]);
136 
137  PDummy dummy3 = AutoRegisteringDummy::create();
138  CHECK (3 == trackedDummies.size());
139  CHECK (dummy3 == trackedDummies[2]);
140 
141  CHECK (1+2+3 == checksum);
142 
143  dummy2->detach();
144  CHECK (1 + 3 == checksum);
145  CHECK (2 == trackedDummies.size());
146  CHECK (dummy1 == trackedDummies[0]);
147  CHECK (dummy3 == trackedDummies[1]);
148 
149  CHECK (1 == dummy2.use_count());
150  CHECK (2 == dummy1.use_count());
151  CHECK (2 == dummy3.use_count());
152 
153  // deliberately discard our reference,
154  // so the only remaining ref is within the registry
155  dummy1.reset();
156  dummy3.reset();
157  CHECK (!dummy1);
158  CHECK ( dummy2);
159  CHECK (!dummy3);
160  CHECK (1 == trackedDummies[0].use_count());
161  CHECK (1 == trackedDummies[1].use_count());
162  CHECK (1 + 3 == checksum);
163 
164  // now the tracker goes out of scope...
165  }
166  CHECK (0 == checksum); // ...remaining elements have been unlinked
167  }
168 
169 
170  void
171  verify_integration()
172  {
174  CHECK (Session::current.isUp());
175 
176  PSess sess = Session::current;
177  CHECK (sess->isValid());
178 
179  uint num_timelines = sess->timelines.size();
180  CHECK (0 < num_timelines);
181 
182  PTimeline specialTimeline (asset::Struct::retrieve.newInstance<Timeline> ("testical"));
183  CHECK (specialTimeline);
184  CHECK (num_timelines + 1 == sess->timelines.size());
185  CHECK (specialTimeline == sess->timelines[num_timelines]); // got appended at the end of the tracking table
186  CHECK (specialTimeline.use_count() == 3); // we, the AssetManager and the session
187 
188  PTimeline anotherTimeline (asset::Struct::retrieve.newInstance<Timeline>());
189  CHECK (num_timelines + 2 == sess->timelines.size());
190  CHECK (specialTimeline == sess->timelines[num_timelines]);
191  CHECK (anotherTimeline == sess->timelines[num_timelines+1]); // new one got appended at the end
192 
194  CHECK (assetM.known (specialTimeline->getID()));
195  assetM.remove (specialTimeline->getID());
196  CHECK (!assetM.known (specialTimeline->getID()));
197 
198  CHECK (num_timelines + 1 == sess->timelines.size());
199  CHECK (anotherTimeline == sess->timelines[num_timelines]); // moved to the previous slot
200  CHECK (specialTimeline.use_count() == 1); // we're holding the last reference
201 
202  verify_cleanup (anotherTimeline);
203  }
204 
205 
208  void
209  verify_cleanup (PTimeline const& aTimeline_in_session)
210  {
211  CHECK (1 < aTimeline_in_session.use_count(), "test object should still be attached to session");
213  CHECK (1 == aTimeline_in_session.use_count(), "session reset should have de-registered the test object");
214  }
215  };
216 
217 
219  LAUNCHER (SessionElementTracker_test, "unit session");
220 
221 
222 
223 }}}} // 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:40
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:120
Steam-Layer implementation namespace root.
Namespace of Session and user visible high-level objects.
Definition: sequence.hpp:65
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:107
Structural building block of the session: a sequence of clips.
Simplistic test class runner.
creation, access and Session lifecycle Interface.
Definition: session.hpp:152
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:71
Primary Interface to the current Session.
Generic interface to express a query for specifically typed result elements exposing some capabilitie...
Definition: query.hpp:270
Top level structural element within the session.
TimelineAccess timelines
collection of timelines (top level)
Definition: session.hpp:124