simpleComponentTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/simpleComponent.h"
  25. TEST(SimpleComponent, SimpleComponent)
  26. {
  27. // When instantiating, and working with a SimObject in C++ code, such as
  28. // a unit test, you *may not* allocate a SimObject off of the stack.
  29. //
  30. // For example:
  31. // SimpleComponent sc;
  32. // is a stack allocation. This memory is allocated off of the program stack
  33. // when the function is called. SimObject deletion is done via SimObject::deleteObject()
  34. // and the last command of this method is 'delete this;' That command will
  35. // cause an assert if it is called on stack-allocated memory. Therefor, when
  36. // instantiating SimObjects in C++ code, it is imperitive that you keep in
  37. // mind that if any script calls 'delete()' on that SimObject, or any other
  38. // C++ code calls 'deleteObject()' on that SimObject, it will crash.
  39. SimpleComponent *sc = new SimpleComponent();
  40. // SimObject::registerObject must be called on a SimObject before it is
  41. // fully 'hooked in' to the engine.
  42. //
  43. // Tracing execution of this function will let you see onAdd get called on
  44. // the component, and you will see it cache the interface we exposed.
  45. sc->registerObject();
  46. // It is *not* required that a component always be owned by a component (obviously)
  47. // however I am using an owner so that you can trace execution of recursive
  48. // calls to cache interfaces and such.
  49. SimComponent *testOwner = new SimComponent();
  50. // Add the test component to it's owner. This will set the 'mOwner' field
  51. // of 'sc' to the address of 'testOwner'
  52. testOwner->addComponent( sc );
  53. // If you step-into this registerObject the same way as the previous one,
  54. // you will be able to see the recursive caching of the exposed interface.
  55. testOwner->registerObject();
  56. // Now to prove that object composition is working properly, lets ask
  57. // both of these components for their interface lists...
  58. // The ComponentInterfaceList is a typedef for type 'VectorPtr<ComponentInterface *>'
  59. // and it will be used by getInterfaces() to store the results of the interface
  60. // query. This is the "complete" way to obtain an interface, and it is too
  61. // heavy-weight for most cases. A simplified query will be performed next,
  62. // to demonstrate the usage of both.
  63. ComponentInterfaceList iLst;
  64. // This query requests all interfaces, on all components, regardless of name
  65. // or owner.
  66. sc->getInterfaces( &iLst,
  67. // This is the type field. I am passing NULL here to signify that the query
  68. // should match all values of 'type' in the list.
  69. NULL,
  70. // The name field, let's pass NULL again just so when you trace execution
  71. // you can see how queries work in the simple case, first.
  72. NULL );
  73. // Lets process the list that we've gotten back, and find the interface that
  74. // we want.
  75. SimpleComponentInterface *scQueriedInterface = NULL;
  76. for( ComponentInterfaceListIterator i = iLst.begin(); i != iLst.end(); i++ )
  77. {
  78. scQueriedInterface = dynamic_cast<SimpleComponentInterface *>( *i );
  79. if( scQueriedInterface != NULL )
  80. break;
  81. }
  82. AssertFatal( scQueriedInterface != NULL, "No valid SimpleComponentInterface was found in query" );
  83. // Lets do it again, only we will execute the query on the parent instead,
  84. // in a simplified way. Remember the parent component doesn't expose any
  85. // interfaces at all, so the success of this behavior is entirely dependent
  86. // on the recursive registration that occurs in registerInterfaces()
  87. SimpleComponentInterface *ownerQueriedInterface = testOwner->getInterface<SimpleComponentInterface>();
  88. AssertFatal( ownerQueriedInterface != NULL, "No valid SimpleComponentInterface was found in query" );
  89. // We should now have two pointers to the same interface obtained by querying
  90. // different components.
  91. EXPECT_EQ( ownerQueriedInterface, scQueriedInterface )
  92. << "This really shouldn't be possible to fail given the setup of the test";
  93. // Lets call the method that was exposed on the component via the interface.
  94. // Trace the execution of this function, if you wish.
  95. EXPECT_TRUE( ownerQueriedInterface->isFortyTwo( 42 ) )
  96. << "Don't panic, but it's a bad day in the component system.";
  97. EXPECT_TRUE( scQueriedInterface->isFortyTwo( 42 ) )
  98. << "Don't panic, but it's a bad day in the component system.";
  99. // So there you have it. Writing a simple component that exposes a cached
  100. // interface, and testing it. It's time to clean up.
  101. testOwner->removeComponent( sc );
  102. sc->deleteObject();
  103. testOwner->deleteObject();
  104. // Interfaces do not need to be freed. In Juggernaught, these will be ref-counted
  105. // for more robust behavior. Right now, however, the values of our two interface
  106. // pointers, scQueriedInterface and ownerQueriedInterface, reference invalid
  107. // memory.
  108. };
  109. #endif