Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Unit test program for DMX testing (testing the Notify subsystem)
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "unitlib/unitlib.h"
  8. #include "datamodel/dmelement.h"
  9. #include "datamodel/idatamodel.h"
  10. #include "tier1/utlbuffer.h"
  11. #include "filesystem.h"
  12. #include "datamodel/dmehandle.h"
  13. #include "tier2/tier2.h"
  14. class CNotifyTest : public IDmNotify
  15. {
  16. public:
  17. CNotifyTest() : m_nValueCount(0), m_nTopologyCount(0), m_nArrayCount(0) {}
  18. virtual void NotifyDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
  19. {
  20. if ( nNotifyFlags & NOTIFY_CHANGE_ATTRIBUTE_VALUE )
  21. {
  22. m_nValueCount++;
  23. }
  24. if ( nNotifyFlags & NOTIFY_CHANGE_ATTRIBUTE_ARRAY_SIZE )
  25. {
  26. m_nArrayCount++;
  27. }
  28. if ( nNotifyFlags & NOTIFY_CHANGE_TOPOLOGICAL )
  29. {
  30. m_nTopologyCount++;
  31. }
  32. }
  33. int m_nTopologyCount;
  34. int m_nArrayCount;
  35. int m_nValueCount;
  36. };
  37. DEFINE_TESTCASE_NOSUITE( DmxNotifyTest )
  38. {
  39. Msg( "Running dmx notify tests...\n" );
  40. CNotifyTest test1, test2;
  41. DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( "<RunNotifyTests>" );
  42. g_pDataModel->InstallNotificationCallback( &test1 );
  43. CDmElement *element = NULL;
  44. {
  45. CUndoScopeGuard guard( NOTIFY_SOURCE_APPLICATION, 0, "create" );
  46. element = CreateElement< CDmElement >( "test", fileid );
  47. }
  48. Shipping_Assert( test1.m_nTopologyCount == 1 );
  49. Shipping_Assert( test1.m_nArrayCount == 0 );
  50. g_pDataModel->Undo();
  51. Shipping_Assert( test1.m_nTopologyCount == 2 );
  52. Shipping_Assert( test1.m_nArrayCount == 0 );
  53. {
  54. CNotifyScopeGuard notify( "test1", NOTIFY_SOURCE_APPLICATION, 0, &test2 );
  55. CDisableUndoScopeGuard guard;
  56. element = CreateElement< CDmElement >( "test", fileid );
  57. }
  58. Shipping_Assert( test1.m_nTopologyCount == 3 );
  59. Shipping_Assert( test1.m_nArrayCount == 0 );
  60. Shipping_Assert( test2.m_nTopologyCount == 1 );
  61. Shipping_Assert( test2.m_nArrayCount == 0 );
  62. {
  63. CDisableUndoScopeGuard guard;
  64. // NOTE: Nested scope guards referring to the same callback shouldn't double call it
  65. CNotifyScopeGuard notify( "test2", NOTIFY_SOURCE_APPLICATION, 0, &test2 );
  66. {
  67. CNotifyScopeGuard notify( "test3", NOTIFY_SOURCE_APPLICATION, 0, &test2 );
  68. DestroyElement( element );
  69. }
  70. }
  71. Shipping_Assert( test1.m_nTopologyCount == 4 );
  72. Shipping_Assert( test1.m_nArrayCount == 0 );
  73. Shipping_Assert( test2.m_nTopologyCount == 2 );
  74. Shipping_Assert( test2.m_nArrayCount == 0 );
  75. {
  76. CUndoScopeGuard guard( NOTIFY_SOURCE_APPLICATION, 0, "create" );
  77. {
  78. element = CreateElement< CDmElement >( "test", fileid );
  79. element->SetValue( "test", 1.0f );
  80. }
  81. guard.Abort();
  82. }
  83. Shipping_Assert( test1.m_nTopologyCount == 4 );
  84. Shipping_Assert( test1.m_nArrayCount == 0 );
  85. Shipping_Assert( test2.m_nTopologyCount == 2 );
  86. Shipping_Assert( test2.m_nArrayCount == 0 );
  87. g_pDataModel->RemoveNotificationCallback( &test1 );
  88. g_pDataModel->RemoveFileId( fileid );
  89. }