simComponentTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 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. #ifdef TORQUE_TESTS_ENABLED
  23. #include "testing/unitTesting.h"
  24. #include "component/simComponent.h"
  25. class CachedInterfaceExampleComponent : public SimComponent
  26. {
  27. typedef SimComponent Parent;
  28. ComponentProperty<U32> mMyId;
  29. static U32 smNumInstances;
  30. ComponentProperty<U32> *mpU32; // CodeReview [patw, 2, 17, 2007] Make ref objects when this is in Jugg
  31. public:
  32. DECLARE_CONOBJECT( CachedInterfaceExampleComponent );
  33. CachedInterfaceExampleComponent() : mpU32( NULL )
  34. {
  35. mMyId = ( ( 1 << 24 ) | smNumInstances++ );
  36. }
  37. virtual ~CachedInterfaceExampleComponent()
  38. {
  39. smNumInstances--;
  40. }
  41. public:
  42. //////////////////////////////////////////////////////////////////////////
  43. virtual void registerInterfaces( SimComponent *owner )
  44. {
  45. // Register a cached interface for this
  46. owner->registerCachedInterface( NULL, "aU32", this, &mMyId );
  47. }
  48. //////////////////////////////////////////////////////////////////////////
  49. bool onComponentRegister( SimComponent *owner )
  50. {
  51. // Call up to the parent first
  52. if( !Parent::onComponentRegister( owner ) )
  53. return false;
  54. // We want to get an interface from another object in our containing component
  55. // to simulate component interdependency.
  56. ComponentInterfaceList list;
  57. // Enumerate the interfaces on the owner, only ignore interfaces that this object owns
  58. if( !owner->getInterfaces( &list, NULL, "aU32", this, true ) )
  59. return false;
  60. // Sanity check before just assigning all willy-nilly
  61. for( ComponentInterfaceListIterator i = list.begin(); i != list.end(); i++ )
  62. {
  63. mpU32 = dynamic_cast<ComponentProperty<U32> *>( (*i) );
  64. if( mpU32 != NULL )
  65. return true;
  66. }
  67. return false;
  68. }
  69. //////////////////////////////////////////////////////////////////////////
  70. // CodeReview [patw, 2, 17, 2007] I'm going to make another lightweight interface
  71. // for this functionality later
  72. void unit_test()
  73. {
  74. EXPECT_TRUE( mpU32 != NULL )
  75. << "Pointer to dependent interface is NULL";
  76. if( mpU32 )
  77. {
  78. EXPECT_TRUE( *(*mpU32) & ( 1 << 24 ) )
  79. << "Pointer to interface data is bogus.";
  80. EXPECT_TRUE( *(*mpU32) != *mMyId )
  81. << "Two of me have the same ID, bad!";
  82. }
  83. }
  84. };
  85. IMPLEMENT_CONOBJECT( CachedInterfaceExampleComponent );
  86. U32 CachedInterfaceExampleComponent::smNumInstances = 0;
  87. ConsoleDocClass( CachedInterfaceExampleComponent,
  88. "@brief Legacy from older component system.\n\n"
  89. "Not intended for game development, for editors or internal use only.\n\n "
  90. "@internal");
  91. TEST(SimComponent, Composition)
  92. {
  93. SimComponent *testComponent = new SimComponent();
  94. CachedInterfaceExampleComponent *componentA = new CachedInterfaceExampleComponent();
  95. CachedInterfaceExampleComponent *componentB = new CachedInterfaceExampleComponent();
  96. // Register sub-components
  97. EXPECT_TRUE( componentA->registerObject() )
  98. << "Failed to register componentA";
  99. EXPECT_TRUE( componentB->registerObject() )
  100. << "Failed to register componentB";
  101. // Add the components
  102. EXPECT_TRUE( testComponent->addComponent( componentA ) )
  103. << "Failed to add component a to testComponent";
  104. EXPECT_TRUE( testComponent->addComponent( componentB ) )
  105. << "Failed to add component b to testComponent";
  106. EXPECT_EQ( componentA->getOwner(), testComponent )
  107. << "testComponent did not properly set the mOwner field of componentA to NULL.";
  108. EXPECT_EQ( componentB->getOwner(), testComponent )
  109. << "testComponent did not properly set the mOwner field of componentB to NULL.";
  110. // Register the object with the simulation, kicking off the interface registration
  111. ASSERT_TRUE( testComponent->registerObject() )
  112. << "Failed to register testComponent";
  113. {
  114. SCOPED_TRACE("componentA");
  115. componentA->unit_test();
  116. }
  117. {
  118. SCOPED_TRACE("componentB");
  119. componentB->unit_test();
  120. }
  121. testComponent->deleteObject();
  122. };
  123. #endif