moreAdvancedComponent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "component/moreAdvancedComponent.h"
  23. #include "unit/test.h"
  24. // unitTest_runTests("Component/MoreAdvancedComponent");
  25. //////////////////////////////////////////////////////////////////////////
  26. IMPLEMENT_CONOBJECT(MoreAdvancedComponent);
  27. ConsoleDocClass( MoreAdvancedComponent,
  28. "@brief This is a slightly more advanced component which will be used to demonstrate "
  29. "components which are dependent on other components.\n\n"
  30. "Not intended for game development, for editors or internal use only.\n\n "
  31. "@internal");
  32. bool MoreAdvancedComponent::onComponentRegister( SimComponent *owner )
  33. {
  34. if( !Parent::onComponentRegister( owner ) )
  35. return false;
  36. // This will return the first interface of type SimpleComponent that is cached
  37. // on the parent object.
  38. mSCInterface = owner->getInterface<SimpleComponentInterface>();
  39. // If we can't find this interface, our component can't function, so false
  40. // will be returned, and this will signify, to the top-level component, that it
  41. // should fail the onAdd call.
  42. return ( mSCInterface != NULL );
  43. }
  44. bool MoreAdvancedComponent::testDependentInterface()
  45. {
  46. // These two requirements must be met in order for the test to proceed, so
  47. // lets check them.
  48. if( mSCInterface == NULL || !mSCInterface->isValid() )
  49. return false;
  50. return mSCInterface->isFortyTwo( 42 );
  51. }
  52. //////////////////////////////////////////////////////////////////////////
  53. using namespace UnitTesting;
  54. CreateUnitTest(MoreAdvancedComponentTest, "Component/MoreAdvancedComponent")
  55. {
  56. void run()
  57. {
  58. // Create component instances and compose them.
  59. SimComponent *parentComponent = new SimComponent();
  60. SimpleComponent *simpleComponent = new SimpleComponent();
  61. MoreAdvancedComponent *moreAdvComponent = new MoreAdvancedComponent();
  62. // CodeReview note that the interface pointer isn't initialized in a ctor
  63. // on the components, so it's bad memory against which you might
  64. // be checking in testDependentInterface [3/3/2007 justind]
  65. parentComponent->addComponent( simpleComponent );
  66. parentComponent->addComponent( moreAdvComponent );
  67. simpleComponent->registerObject();
  68. moreAdvComponent->registerObject();
  69. // Put a break-point here, follow the onAdd call, and observe the order in
  70. // which the SimComponent::onAdd function executes. You will see the interfaces
  71. // get cached, and the dependent interface query being made.
  72. parentComponent->registerObject();
  73. // If the MoreAdvancedComponent found an interface, than the parentComponent
  74. // should have returned true, from onAdd, and should therefore be registered
  75. // properly with the Sim
  76. test( parentComponent->isProperlyAdded(), "Parent component not properly added!" );
  77. // Now lets test the interface. You can step through this, as well.
  78. test( moreAdvComponent->testDependentInterface(), "Dependent interface test failed." );
  79. // CodeReview is there a reason we can't just delete the parentComponent here? [3/3/2007 justind]
  80. //
  81. // Clean up
  82. parentComponent->removeComponent( simpleComponent );
  83. parentComponent->removeComponent( moreAdvComponent );
  84. parentComponent->deleteObject();
  85. moreAdvComponent->deleteObject();
  86. simpleComponent->deleteObject();
  87. }
  88. };