LUMIERA.clone/tests/core/steam/mobject/mobject-ref-test.cpp

384 lines
14 KiB
C++
Raw Normal View History

2009-06-02 03:31:52 +02:00
/*
MObjectRef(Test) - validating basic properties of the external MObject reference tag
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
Copyright (C)
2009, Hermann Vosseler <Ichthyostega@web.de>
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
Copyright: clarify and simplify the file headers * Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
* *****************************************************************/
2009-06-02 03:31:52 +02:00
/** @file mobject-ref-test.cpp
** unit test \ref MObjectRef_test
*/
2009-06-02 03:31:52 +02:00
#include "lib/test/run.hpp"
#include "steam/asset/media.hpp"
#include "steam/mobject/mobject.hpp"
#include "steam/mobject/mobject-ref.hpp"
#include "steam/mobject/placement.hpp"
#include "steam/mobject/placement-ref.hpp"
#include "steam/mobject/session/placement-index.hpp"
#include "steam/mobject/session/session-service-mock-index.hpp"
#include "steam/mobject/session/clip.hpp"
#include "steam/mobject/explicitplacement.hpp"
#include "steam/mobject/test-dummy-mobject.hpp"
#include "vault/media-access-mock.hpp"
#include "lib/depend-inject.hpp"
2009-06-07 03:12:07 +02:00
#include "lib/test/test-helper.hpp"
#include "lib/time/timevalue.hpp"
#include "lib/format-cout.hpp"
#include "lib/util.hpp"
2009-06-02 03:31:52 +02:00
#include <string>
2009-06-02 03:31:52 +02:00
using lib::test::showSizeof;
using lib::time::Duration;
using lib::time::FSecs;
using lib::time::Time;
using util::isnil;
2009-06-02 03:31:52 +02:00
using std::string;
namespace steam {
2009-06-02 03:31:52 +02:00
namespace mobject {
namespace test {
namespace { // shortcut for checking use-counts
bool
checkUseCount (size_t cnt, uint additional=0)
{
static uint base_count=0;
if (!additional) // called for init
base_count = cnt;
return cnt == base_count + additional;
}
template<class REF>
bool
checkUseCount (REF const& ref, uint additional)
{
return checkUseCount(ref.use_count(), additional);
}
}
using LERR_(INVALID_PLACEMENTREF);
using LERR_(BOTTOM_MOBJECTREF);
using session::Clip;
using session::PMedia;
using session::SessionServiceMockIndex;
using session::PPIdx;
using MediaAccessMock = lib::DependInject<vault::MediaAccessFacade>
::Local<vault::test::MediaAccessMock>;
2009-06-02 03:31:52 +02:00
/***********************************************************************//**
2009-06-02 03:31:52 +02:00
* @test properties and behaviour of the external reference-mechanism for
* MObjects placed into the session. We create a simple, simulated
* "session" (no real session datastructure) and a mock PlacementIndex.
* Then we add two Placements, which can be used then to build
* MObjectRef instances to validate their behaviour.
2009-06-02 03:31:52 +02:00
* @see mobject::Placement
* @see mobject::MObject
* @see mobject::PlacementRef_test
*/
class MObjectRef_test : public Test
{
typedef Placement<MObject> PMObj;
typedef Placement<Clip> PClip;
typedef TestPlacement<TestSubMO1> PSub1;
2009-06-02 03:31:52 +02:00
virtual void
run (Arg)
2009-06-02 03:31:52 +02:00
{
MediaAccessMock useMockMedia;
// create data simulating a "Session"
PMObj testClip1 = asset::Media::create("test-1", asset::VIDEO)->createClip();
PMObj testClip2 = asset::Media::create("test-2", asset::VIDEO)->createClip();
// set up a tie to fixed start positions (i.e. "properties of placement")
testClip1.chain (Time(FSecs(10)));
testClip2.chain (Time(FSecs(20)));
CHECK (testClip1->isValid());
CHECK (testClip2->isValid());
CHECK (2 == testClip1.use_count()); // one by the placement and one by the clip-Asset
CHECK (2 == testClip2.use_count());
// Prepare an (test)Index
PPIdx index = SessionServiceMockIndex::install();
PMO& root = index->getRoot();
// Add the Clips to "session" (here: dummy index)
PMObj::ID clip1ID = index->insert (testClip1, root);
PMObj::ID clip2ID = index->insert (testClip2, root);
CHECK (2 == index->size());
// use the IDs returned on insertion to fetch
// language references to the placement instance within the index
// we'll use these language refs as base to create MObejctRef handles.
PMObj& pClip1 = index->find(clip1ID);
PMObj& pClip2 = index->find(clip2ID);
CHECK (3 == pClip1.use_count()); // clip-Asset, original placement, new placement in index
CHECK (3 == pClip2.use_count());
checkUseCount(pClip1.use_count()); // set ref point for later checks
// extract various kinds of IDs and refs
PMObj & rP1 (pClip1);
PMObj const& rP2 (pClip2);
PMObj::ID id1 = pClip1.getID();
PMObj::Id<Clip> id2 = pClip2.recastID<Clip>(); // explicit unchecked re-declaration of target type
LumieraUid luid = id1.get();
///////////////////////////////////////////TODO: check the C-API representation here
PlacementRef<Clip> ref1 (id1);
PlacementRef<MObject> ref2 (pClip2);
// -----Tests------------------
checkBuildMObjectRef (rP1, &pClip1);
checkBuildMObjectRef (rP2, &pClip2);
checkBuildMObjectRef (id1, &pClip1);
checkBuildMObjectRef (id2, &pClip2);
checkBuildMObjectRef (luid, &pClip1);
///////////////////////////////////////////TODO: check the C-API representation here
checkBuildMObjectRef (ref1, &pClip1);
checkBuildMObjectRef (ref2, &pClip2);
2009-06-07 03:12:07 +02:00
checkComparison(rP1,rP2);
2009-06-07 03:12:07 +02:00
checkLifecycle (rP1,rP2);
checkTypeHandling (luid);
// -----Tests------------------
// verify clean state
index->remove (pClip1);
index->remove (pClip2); // note: this invalidates pClip1 and pClip2;
CHECK (!ref1);
CHECK (!ref2);
CHECK (0 == index->size());
CHECK (1 == index.use_count());
CHECK (2 == testClip1.use_count());
CHECK (2 == testClip2.use_count());
index.reset();
2009-06-02 03:31:52 +02:00
}
template<typename REF>
void
checkBuildMObjectRef (REF& refObj, void* placementAdr)
{
MORef<Clip> rMO;
CHECK (!rMO); // still empty (not bound)
CHECK (0==rMO.use_count());
cout << rMO << endl; /////////////////////TICKET #527
cout << showSizeof(rMO) << endl;
// activate by binding to provided ref
rMO.activate(refObj);
CHECK (rMO); // now bound
cout << rMO << endl; /////////////////////TICKET #527
// access MObject (Clip API)
// cout << rMO->operator string() << endl; /////////////////////TICKET #428
PMedia media = rMO->getMedia();
cout << media << endl; /////////////////////TICKET #520
Duration mediaLength = media->getLength();
CHECK (!isnil (mediaLength));
CHECK (rMO->isValid());
// access the Placement-API
CHECK (checkUseCount(rMO, 1)); // now rMO shares ownership with the Placement --> use-count += 1
CHECK (Time::ZERO < rMO.getStartTime()); // (internally, this resolves to an ExplicitPlacement) /////////TICKET #332
CHECK ( rMO.isCompatible<MObject>());
CHECK ( rMO.isCompatible<Clip>());
CHECK (!rMO.isCompatible<TestSubMO1>());
Time start = rMO.getStartTime();
// re-link to the Placement (note we get the Clip API!)
Placement<Clip> & refP = rMO.getPlacement();
CHECK (refP.isValid());
CHECK (refP.use_count() == rMO.use_count());
CHECK (checkUseCount(refP, 1)); // use count not changed
CHECK (&refP == placementAdr); // actually denotes the address of the original Placement in the "session"
cout << refP << endl;
2009-06-07 03:12:07 +02:00
ExplicitPlacement exPla = refP.resolve();
CHECK (exPla.time == start); // recovered Placement resolves to the same time as provided by the proxied API
CHECK (checkUseCount(refP, 2)); // but now we've indeed created an additional owner (exPla)
CHECK (checkUseCount(rMO, 2));
2009-06-07 03:12:07 +02:00
}
void
checkComparison (PMO const& p1, PMO const& p2)
{
PlacementRef<Clip> pRef1 (p1);
PlacementRef<MObject> pRef2 (p2);
MORef<MObject> rM;
MORef<Clip> rC;
rM.activate (p1);
rC.activate (p2);
CHECK (rM && rC);
CHECK (!(rM == rC) && !(rC == rM));
CHECK ( (rM != rC) && (rC != rM));
// mixed comparisons
CHECK ( (rM == pRef1) && (pRef1 == rM));
CHECK ( (rC == pRef2) && (pRef2 == rC));
CHECK (!(rM != pRef1) && !(pRef1 != rM));
CHECK (!(rC != pRef2) && !(pRef2 != rC));
CHECK ( (rM != pRef2) && (pRef2 != rM));
CHECK ( (rC != pRef1) && (pRef1 != rC));
CHECK (!(rM == pRef2) && !(pRef2 == rM));
CHECK (!(rC == pRef1) && !(pRef1 == rC));
CHECK ( (rM == p1.getID()) );
CHECK ( (rC == p2.getID()) );
CHECK (!(rM != p1.getID()) );
CHECK (!(rC != p2.getID()) );
CHECK ( (rM != p2.getID()) );
CHECK ( (rC != p1.getID()) );
CHECK (!(rM == p2.getID()) );
CHECK (!(rC == p1.getID()) );
rC.activate (pRef1);
CHECK ( (rM == rC) && (rC == rM));
CHECK (!(rM != rC) && !(rC != rM));
CHECK ( (rC == pRef1) && (pRef1 == rC));
CHECK (!(rC != pRef1) && !(pRef1 != rC));
CHECK ( (rC != pRef2) && (pRef2 != rC));
CHECK (!(rC == pRef2) && !(pRef2 == rC));
CHECK ( (rC == p1.getID()) );
CHECK (!(rC != p1.getID()) );
CHECK ( (rC != p2.getID()) );
CHECK (!(rC == p2.getID()) );
rM.close();
CHECK (!rM);
CHECK (!(rM == rC) && !(rC == rM));
CHECK ( (rM != rC) && (rC != rM));
CHECK (!(rM == pRef1) && !(pRef1 == rM));
CHECK ( (rM != pRef1) && (pRef1 != rM));
CHECK ( (rM != pRef2) && (pRef2 != rM));
CHECK (!(rM == pRef2) && !(pRef2 == rM));
CHECK (!(rM == p1.getID()) );
CHECK ( (rM != p1.getID()) );
CHECK ( (rM != p2.getID()) );
CHECK (!(rM == p2.getID()) );
}
2009-06-07 03:12:07 +02:00
void
checkLifecycle (PMObj const& p1, PMObj const& p2)
2009-06-07 03:12:07 +02:00
{
CHECK (checkUseCount(p1, 0));
CHECK (checkUseCount(p2, 0));
2009-06-07 03:12:07 +02:00
MORef<Clip> rMO;
CHECK (!rMO);
CHECK (0 == rMO.use_count());
2009-06-07 03:12:07 +02:00
rMO.activate(p1);
CHECK (rMO);
CHECK (rMO->getMedia()->getFilename() == "test-1");
CHECK (checkUseCount(rMO, 1));
CHECK (checkUseCount(p1, 1)); // sharing ownership
CHECK (checkUseCount(p2, 0));
2009-06-07 03:12:07 +02:00
rMO.activate(p2);
CHECK (rMO);
CHECK (rMO->getMedia()->getFilename() == "test-2");
CHECK (checkUseCount(rMO, 1));
CHECK (checkUseCount(p1, 0)); // detached, use count dropped to previous value
CHECK (checkUseCount(p2, 1)); // sharing ownership
2009-06-07 03:12:07 +02:00
rMO.activate(p2);
CHECK (checkUseCount(rMO, 1)); // no change
2009-06-07 03:12:07 +02:00
rMO.close();
CHECK (!rMO);
CHECK (checkUseCount(p1, 0));
CHECK (checkUseCount(p2, 0));
2009-06-07 03:12:07 +02:00
VERIFY_ERROR (BOTTOM_MOBJECTREF, rMO.getPlacement() );
VERIFY_ERROR (BOTTOM_MOBJECTREF, rMO->getMedia() );
2009-06-07 03:12:07 +02:00
}
void
checkTypeHandling (LumieraUid luid)
{
MObjectRef rMObj;
MORef<Clip> rClip;
MORef<TestSubMO1> rSub1;
2009-06-07 03:12:07 +02:00
CHECK ( ! rMObj.use_count());
CHECK ( ! rClip.use_count());
CHECK ( ! rSub1.use_count());
rMObj.activate(luid);
CHECK (checkUseCount(rMObj, 1));
CHECK ( ! rClip.use_count());
CHECK ( ! rSub1.use_count());
rClip.activate(rMObj); // attach on existing MObjectRef
CHECK (checkUseCount(rMObj, 2));
CHECK (checkUseCount(rClip, 2));
CHECK ( ! rSub1.use_count());
// impossible, because Clip isn't a subclass of TestSubMO1:
VERIFY_ERROR (INVALID_PLACEMENTREF, rSub1.activate(luid) );
VERIFY_ERROR (INVALID_PLACEMENTREF, rSub1 = rMObj );
CHECK (rMObj->isValid());
CHECK (rClip->isValid());
CHECK (rMObj.getPlacement().getID() == rClip.getPlacement().getID());
// doesn't compile, because the function isn't on MObject API:
// rMObj->getMedia();
rClip.close();
CHECK (checkUseCount(rMObj, 1));
CHECK ( ! rClip.use_count());
// can assign, because the actual type is checked:
rClip = rMObj;
CHECK (checkUseCount(rMObj, 2));
CHECK (checkUseCount(rClip, 2));
cout << rClip << endl; //////////TICKET #527
cout << rClip->getMedia()->ident << endl; //////////TICKET #520
}
2009-06-02 03:31:52 +02:00
};
/** Register this test class... */
LAUNCHER (MObjectRef_test, "unit session");
}}} // namespace steam::mobject::test