PaintBrushBaseTests.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/UnitTest/TestTypes.h>
  10. #include <AzTest/AzTest.h>
  11. #include <AzFramework/PaintBrush/PaintBrush.h>
  12. #include <AZTestShared/Math/MathTestHelpers.h>
  13. #include <Tests/PaintBrush/MockPaintBrushNotificationHandler.h>
  14. /*
  15. This set of unit tests validates that the basic PaintBrush functionality and notifications work:
  16. - Begin/End Paint Mode
  17. - Begin/End Brush Stroke
  18. - Use Eyedropper
  19. */
  20. namespace UnitTest
  21. {
  22. class PaintBrushTestFixture : public LeakDetectionFixture
  23. {
  24. public:
  25. // Some common default values that we'll use in our tests.
  26. const AZ::EntityComponentIdPair EntityComponentIdPair{ AZ::EntityId(123), AZ::ComponentId(456) };
  27. const AZ::Vector3 TestBrushCenter{ 10.0f, 20.0f, 30.0f };
  28. const AZ::Vector3 TestBrushCenter2d{ 10.0f, 20.0f, 0.0f }; // This should be the same as TestBrushCenter, but with Z=0
  29. const AZ::Color TestColor{ 0.25f, 0.50f, 0.75f, 1.0f };
  30. // Useful helpers for our tests
  31. AzFramework::PaintBrushSettings m_settings;
  32. };
  33. TEST_F(PaintBrushTestFixture, TrivialConstruction)
  34. {
  35. // We can construct / destroy a PaintBrush without any errors.
  36. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  37. }
  38. TEST_F(PaintBrushTestFixture, BeginAndEndPaintModeMustBePairedCorrectly)
  39. {
  40. // BeginPaintMode() / EndPaintMode() can only be called in the correct order in pairs.
  41. // They will error if they're called in the wrong order or more than once in a row.
  42. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  43. // End before Begin should assert.
  44. AZ_TEST_START_TRACE_SUPPRESSION;
  45. paintBrush.EndPaintMode();
  46. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  47. paintBrush.BeginPaintMode();
  48. // Calling Begin twice should assert.
  49. AZ_TEST_START_TRACE_SUPPRESSION;
  50. paintBrush.BeginPaintMode();
  51. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  52. paintBrush.EndPaintMode();
  53. // Calling End twice should assert.
  54. AZ_TEST_START_TRACE_SUPPRESSION;
  55. paintBrush.EndPaintMode();
  56. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  57. }
  58. TEST_F(PaintBrushTestFixture, IsInPaintModeFunctionsCorrectly)
  59. {
  60. // IsInPaintMode() correctly identifies when we're between a BeginPaintMode() and EndPaintMode() call.
  61. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  62. EXPECT_FALSE(paintBrush.IsInPaintMode());
  63. paintBrush.BeginPaintMode();
  64. EXPECT_TRUE(paintBrush.IsInPaintMode());
  65. paintBrush.EndPaintMode();
  66. EXPECT_FALSE(paintBrush.IsInPaintMode());
  67. }
  68. TEST_F(PaintBrushTestFixture, BeginAndEndBrushStrokeMustBePairedCorrectlyInsidePaintMode)
  69. {
  70. // BeginBrushStroke() / EndBrushStroke() can only be called in the correct order in pairs.
  71. // They will error if they're called in the wrong order, more than once in a row, or outside of BeginPaintMode().
  72. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  73. // BeginBrushStroke without BeginPaintMode should assert.
  74. AZ_TEST_START_TRACE_SUPPRESSION;
  75. paintBrush.BeginBrushStroke(m_settings);
  76. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  77. paintBrush.BeginPaintMode();
  78. // End before Begin should assert.
  79. AZ_TEST_START_TRACE_SUPPRESSION;
  80. paintBrush.EndBrushStroke();
  81. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  82. paintBrush.BeginBrushStroke(m_settings);
  83. // Calling Begin twice should assert.
  84. AZ_TEST_START_TRACE_SUPPRESSION;
  85. paintBrush.BeginBrushStroke(m_settings);
  86. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  87. paintBrush.EndBrushStroke();
  88. // Calling End twice should assert.
  89. AZ_TEST_START_TRACE_SUPPRESSION;
  90. paintBrush.EndBrushStroke();
  91. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  92. paintBrush.EndPaintMode();
  93. }
  94. TEST_F(PaintBrushTestFixture, IsInBrushStrokeFunctionsCorrectly)
  95. {
  96. // IsInBrushStroke() correctly identifies when we're between a BeginBrushStroke() and EndBrushStroke() call.
  97. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  98. paintBrush.BeginPaintMode();
  99. EXPECT_FALSE(paintBrush.IsInBrushStroke());
  100. paintBrush.BeginBrushStroke(m_settings);
  101. EXPECT_TRUE(paintBrush.IsInBrushStroke());
  102. paintBrush.EndBrushStroke();
  103. EXPECT_FALSE(paintBrush.IsInBrushStroke());
  104. paintBrush.EndPaintMode();
  105. }
  106. TEST_F(PaintBrushTestFixture, PaintModeNotifiesCorrectly)
  107. {
  108. // BeginPaintMode() / EndPaintMode() should call the PaintBrushNotificationBus with OnPaintModeBegin() / OnPaintModeEnd().
  109. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  110. ::testing::NiceMock<MockPaintBrushNotificationBusHandler> mockHandler(EntityComponentIdPair);
  111. EXPECT_CALL(mockHandler, OnPaintModeBegin()).Times(1);
  112. paintBrush.BeginPaintMode();
  113. EXPECT_CALL(mockHandler, OnPaintModeEnd()).Times(1);
  114. paintBrush.EndPaintMode();
  115. }
  116. TEST_F(PaintBrushTestFixture, BrushStrokeNotifiesCorrectly)
  117. {
  118. // BeginBrushStroke() / EndBrushStroke() should call the PaintBrushNotificationBus with OnBrushStrokeBegin() / OnBrushStrokeEnd().
  119. // OnBrushStrokeBegin should be passed the current color in the PaintBrushSettings that we called BeginBrushStroke() with.
  120. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  121. ::testing::NiceMock<MockPaintBrushNotificationBusHandler> mockHandler(EntityComponentIdPair);
  122. m_settings.SetColor(TestColor);
  123. EXPECT_CALL(mockHandler, OnBrushStrokeBegin(::testing::_)).Times(1);
  124. ON_CALL(mockHandler, OnBrushStrokeBegin)
  125. .WillByDefault(
  126. [this](const AZ::Color& strokeColor)
  127. {
  128. EXPECT_THAT(TestColor, IsClose(strokeColor));
  129. return TestColor;
  130. });
  131. paintBrush.BeginPaintMode();
  132. paintBrush.BeginBrushStroke(m_settings);
  133. EXPECT_CALL(mockHandler, OnBrushStrokeEnd()).Times(1);
  134. paintBrush.EndBrushStroke();
  135. paintBrush.EndPaintMode();
  136. }
  137. TEST_F(PaintBrushTestFixture, UseEyedropperFunctionsCorrectlyOutsideOfPaintMode)
  138. {
  139. // UseEyedropper should return the expected color for the given world position even if we're not currently
  140. // in Paint Mode or inside a Brush Stroke.
  141. // This verifies that PaintBrushNotificationBus::OnGetColor is called with the expected world position and that the color
  142. // returned from UseEyedropper matches the one we returned from OnGetColor.
  143. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  144. ::testing::NiceMock<MockPaintBrushNotificationBusHandler> mockHandler(EntityComponentIdPair);
  145. ON_CALL(mockHandler, OnGetColor)
  146. .WillByDefault(
  147. [this](const AZ::Vector3& brushCenterPoint)
  148. {
  149. // We expect the brush center to match what we passed in but with a Z value of 0
  150. // because we only support painting in 2D for now.
  151. EXPECT_THAT(brushCenterPoint, IsClose(TestBrushCenter2d));
  152. return TestColor;
  153. });
  154. // Note that UseEyedropper() can be called without being in a paint mode or brush stroke.
  155. const AZ::Color color = paintBrush.UseEyedropper(TestBrushCenter);
  156. EXPECT_THAT(color, IsClose(TestColor));
  157. }
  158. TEST_F(PaintBrushTestFixture, UseEyedropperFunctionsCorrectlyInsideOfPaintMode)
  159. {
  160. // UseEyedropper should return the expected color for the given world position even if we are currently
  161. // in Paint Mode and inside a Brush Stroke.
  162. // This verifies that PaintBrushNotificationBus::OnGetColor is called with the expected world position and that the color
  163. // returned from UseEyedropper matches the one we returned from OnGetColor.
  164. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  165. ::testing::NiceMock<MockPaintBrushNotificationBusHandler> mockHandler(EntityComponentIdPair);
  166. ON_CALL(mockHandler, OnGetColor)
  167. .WillByDefault(
  168. [this](const AZ::Vector3& brushCenterPoint)
  169. {
  170. // We expect the brush center to match what we passed in but with a Z value of 0
  171. // because we only support painting in 2D for now.
  172. EXPECT_THAT(brushCenterPoint, IsClose(TestBrushCenter2d));
  173. return TestColor;
  174. });
  175. // UseEyedropper should still work even if a paint mode and brush stroke is active.
  176. paintBrush.BeginPaintMode();
  177. paintBrush.BeginBrushStroke(m_settings);
  178. const AZ::Color color = paintBrush.UseEyedropper(TestBrushCenter);
  179. paintBrush.EndBrushStroke();
  180. paintBrush.EndPaintMode();
  181. EXPECT_THAT(color, IsClose(TestColor));
  182. }
  183. TEST_F(PaintBrushTestFixture, PaintToLocationMustBeUsedWithinBrushStroke)
  184. {
  185. // PaintToLocation() can only be called if we're currently within a brush stroke.
  186. AzFramework::PaintBrush paintBrush(EntityComponentIdPair);
  187. ::testing::NiceMock<MockPaintBrushNotificationBusHandler> mockHandler(EntityComponentIdPair);
  188. AZ_TEST_START_TRACE_SUPPRESSION;
  189. paintBrush.PaintToLocation(TestBrushCenter, m_settings);
  190. AZ_TEST_STOP_TRACE_SUPPRESSION(2);
  191. }
  192. } // namespace UnitTest