StartingPointInputTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzTest/AzTest.h>
  9. #include <InputEventMap.h>
  10. #include <InputEventGroup.h>
  11. #include <InputEventBindings.h>
  12. class StartingPointInputTest
  13. : public ::testing::Test
  14. {
  15. };
  16. TEST_F(StartingPointInputTest, CopyingInputEventGroupDoesDeepCopy)
  17. {
  18. struct TestInputEventBindings : public StartingPointInput::InputEventBindings
  19. {
  20. void AddInputEventGroup(StartingPointInput::InputEventGroup eventGroup)
  21. {
  22. m_inputEventGroups.push_back(eventGroup);
  23. }
  24. AZStd::vector<StartingPointInput::InputEventGroup> GetInputEventGroups() const
  25. {
  26. return m_inputEventGroups;
  27. }
  28. };
  29. struct TestInputEventMap : public StartingPointInput::InputEventMap
  30. {
  31. void Activate([[maybe_unused]] const StartingPointInput::InputEventNotificationId& channel) {}
  32. void Deactivate([[maybe_unused]] const StartingPointInput::InputEventNotificationId& channel) {}
  33. int m_testData = -1;
  34. };
  35. struct TestInputEventGroup : public StartingPointInput::InputEventGroup
  36. {
  37. void AddInputEventMap(StartingPointInput::InputEventMap* inputEventMap)
  38. {
  39. m_inputHandlers.push_back(inputEventMap);
  40. }
  41. bool IsSame(const TestInputEventGroup& other)
  42. {
  43. if (m_inputHandlers.size() != other.m_inputHandlers.size()) { return false; }
  44. for (size_t i = 0; i < m_inputHandlers.size(); ++i)
  45. {
  46. if (m_inputHandlers[i] != other.m_inputHandlers[i]) { return false; }
  47. }
  48. return true;
  49. }
  50. bool IsSameData(const TestInputEventGroup& other)
  51. {
  52. if (m_inputHandlers.size() != other.m_inputHandlers.size()) { return false; }
  53. for (size_t i = 0; i < m_inputHandlers.size(); ++i)
  54. {
  55. TestInputEventMap* lhs = static_cast<TestInputEventMap*>(m_inputHandlers[i]);
  56. TestInputEventMap* rhs = static_cast<TestInputEventMap*>(other.m_inputHandlers[i]);
  57. if (lhs->m_testData != rhs->m_testData) { return false; }
  58. }
  59. return true;
  60. }
  61. };
  62. TestInputEventBindings testBindings1;
  63. TestInputEventBindings testBindings2;
  64. TestInputEventGroup testEventGroup1;
  65. TestInputEventGroup testEventGroup2;
  66. TestInputEventMap testInput1;
  67. TestInputEventMap testInput2;
  68. // Set up the test case with some pieces of data for each input.
  69. testInput1.m_testData = 5;
  70. testInput2.m_testData = 37;
  71. // Assign those separate input sub components to the groups.
  72. testEventGroup1.AddInputEventMap(&testInput1);
  73. testEventGroup2.AddInputEventMap(&testInput2);
  74. // Set up the bindings that will be swapped.
  75. testBindings1.AddInputEventGroup(testEventGroup1);
  76. testBindings2.AddInputEventGroup(testEventGroup2);
  77. // Perform the swap, which is the primary thing being tested here.
  78. testBindings1.Swap(&testBindings2);
  79. // After the swap, make sure they aren't the same.
  80. // This just checks pointers, and not the actual data.
  81. EXPECT_FALSE(testEventGroup1.IsSame(testEventGroup2));
  82. // Verify the data is different
  83. EXPECT_FALSE(testEventGroup1.IsSameData(testEventGroup2));
  84. // Make sure the swapped data matches the control data.
  85. AZStd::vector<StartingPointInput::InputEventGroup> swappedGroup1 = testBindings1.GetInputEventGroups();
  86. AZStd::vector<StartingPointInput::InputEventGroup> swappedGroup2 = testBindings2.GetInputEventGroups();
  87. ASSERT_TRUE(swappedGroup1.size() == 1);
  88. ASSERT_TRUE(swappedGroup2.size() == 1);
  89. EXPECT_TRUE(testEventGroup1.IsSameData(*(static_cast<TestInputEventGroup*>(&swappedGroup2[0]))));
  90. EXPECT_TRUE(testEventGroup2.IsSameData(*(static_cast<TestInputEventGroup*>(&swappedGroup1[0]))));
  91. }
  92. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);