testComponents.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "unit/test.h"
  23. #include "unit/memoryTester.h"
  24. #include "component/simComponent.h"
  25. using namespace UnitTesting;
  26. //////////////////////////////////////////////////////////////////////////
  27. class CachedInterfaceExampleComponent : public SimComponent
  28. {
  29. typedef SimComponent Parent;
  30. ComponentProperty<U32> mMyId;
  31. static U32 smNumInstances;
  32. ComponentProperty<U32> *mpU32; // CodeReview [patw, 2, 17, 2007] Make ref objects when this is in Jugg
  33. public:
  34. DECLARE_CONOBJECT( CachedInterfaceExampleComponent );
  35. CachedInterfaceExampleComponent() : mpU32( NULL )
  36. {
  37. mMyId = ( ( 1 << 24 ) | smNumInstances++ );
  38. }
  39. virtual ~CachedInterfaceExampleComponent()
  40. {
  41. smNumInstances--;
  42. }
  43. public:
  44. //////////////////////////////////////////////////////////////////////////
  45. virtual void registerInterfaces( const SimComponent *owner )
  46. {
  47. // Register a cached interface for this
  48. registerCachedInterface( NULL, "aU32", this, &mMyId );
  49. }
  50. //////////////////////////////////////////////////////////////////////////
  51. bool onComponentRegister( SimComponent *owner )
  52. {
  53. // Call up to the parent first
  54. if( !Parent::onComponentRegister( owner ) )
  55. return false;
  56. // We want to get an interface from another object in our containing component
  57. // to simulate component interdependency.
  58. ComponentInterfaceList list;
  59. // Enumerate the interfaces on the owner, only ignore interfaces that this object owns
  60. if( !_getOwner()->getInterfaces( &list, NULL, "aU32", this, true ) )
  61. return false;
  62. // Sanity check before just assigning all willy-nilly
  63. for( ComponentInterfaceListIterator i = list.begin(); i != list.end(); i++ )
  64. {
  65. mpU32 = dynamic_cast<ComponentProperty<U32> *>( (*i) );
  66. if( mpU32 != NULL )
  67. return true;
  68. }
  69. return false;
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. // CodeReview [patw, 2, 17, 2007] I'm going to make another lightweight interface
  73. // for this functionality later
  74. void unit_test( UnitTest *test )
  75. {
  76. AssertFatal(test, "CachedInterfaceExampleComponent::unit_test - NULL UnitTest");
  77. if( !test )
  78. return;
  79. test->test( mpU32 != NULL, "Pointer to dependent interface is NULL" );
  80. if( mpU32 )
  81. {
  82. test->test( *(*mpU32) & ( 1 << 24 ), "Pointer to interface data is bogus." );
  83. test->test( *(*mpU32) != *mMyId, "Two of me have the same ID, bad!" );
  84. }
  85. }
  86. };
  87. IMPLEMENT_CONOBJECT( CachedInterfaceExampleComponent );
  88. U32 CachedInterfaceExampleComponent::smNumInstances = 0;
  89. ConsoleDocClass( CachedInterfaceExampleComponent,
  90. "@brief Legacy from older component system.\n\n"
  91. "Not intended for game development, for editors or internal use only.\n\n "
  92. "@internal");
  93. //////////////////////////////////////////////////////////////////////////
  94. CreateUnitTest(TestComponentInterfacing, "Components/Composition")
  95. {
  96. void run()
  97. {
  98. MemoryTester m;
  99. m.mark();
  100. SimComponent *testComponent = new SimComponent();
  101. CachedInterfaceExampleComponent *componentA = new CachedInterfaceExampleComponent();
  102. CachedInterfaceExampleComponent *componentB = new CachedInterfaceExampleComponent();
  103. // Register sub-components
  104. test( componentA->registerObject(), "Failed to register componentA" );
  105. test( componentB->registerObject(), "Failed to register componentB" );
  106. // Add the components
  107. test( testComponent->addComponent( componentA ), "Failed to add component a to testComponent" );
  108. test( testComponent->addComponent( componentB ), "Failed to add component b to testComponent" );
  109. test( componentA->getOwner() == testComponent, "testComponent did not properly set the mOwner field of componentA to NULL." );
  110. test( componentB->getOwner() == testComponent, "testComponent did not properly set the mOwner field of componentB to NULL." );
  111. // Register the object with the simulation, kicking off the interface registration
  112. const bool registered = testComponent->registerObject();
  113. test( registered, "Failed to register testComponent" );
  114. // Interface tests
  115. if( registered )
  116. {
  117. componentA->unit_test( this );
  118. componentB->unit_test( this );
  119. testComponent->deleteObject();
  120. }
  121. test( m.check(), "Component composition test leaked memory." );
  122. }
  123. };