FactoryTest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <AZTestShared/Math/MathTestHelpers.h>
  9. #include <AzCore/Interface/Interface.h>
  10. #include <UnitTestHelper.h>
  11. #include <TriangleInputHelper.h>
  12. #include <System/Factory.h>
  13. #include <System/Solver.h>
  14. #include <System/Fabric.h>
  15. #include <System/FabricCooker.h>
  16. #include <System/Cloth.h>
  17. namespace UnitTest
  18. {
  19. //! Sets up a factory for each test case.
  20. class NvClothSystemFactory
  21. : public ::testing::Test
  22. {
  23. protected:
  24. // ::testing::Test overrides ...
  25. void SetUp() override;
  26. void TearDown() override;
  27. NvCloth::Factory m_factory;
  28. };
  29. void NvClothSystemFactory::SetUp()
  30. {
  31. m_factory.Init();
  32. }
  33. void NvClothSystemFactory::TearDown()
  34. {
  35. m_factory.Destroy();
  36. }
  37. TEST_F(NvClothSystemFactory, Factory_CreateSolverEmptyName_ReturnsNull)
  38. {
  39. AZStd::unique_ptr<NvCloth::Solver> solver = m_factory.CreateSolver("");
  40. EXPECT_TRUE(solver.get() == nullptr);
  41. }
  42. TEST_F(NvClothSystemFactory, Factory_CreateSolver_ReturnsValidSolver)
  43. {
  44. const AZStd::string solverName = "NewSolver";
  45. AZStd::unique_ptr<NvCloth::Solver> solver = m_factory.CreateSolver(solverName);
  46. EXPECT_TRUE(solver.get() != nullptr);
  47. EXPECT_EQ(solver->GetName(), solverName);
  48. }
  49. TEST_F(NvClothSystemFactory, Factory_CreateFabricInvalidId_ReturnsNull)
  50. {
  51. NvCloth::FabricCookedData emptyFabricCookedData;
  52. EXPECT_FALSE(emptyFabricCookedData.m_id.IsValid());
  53. AZStd::unique_ptr<NvCloth::Fabric> fabric = m_factory.CreateFabric(emptyFabricCookedData);
  54. EXPECT_TRUE(fabric.get() == nullptr);
  55. }
  56. TEST_F(NvClothSystemFactory, Factory_CreateFabric_ReturnsValidFabric)
  57. {
  58. const NvCloth::FabricCookedData fabricCookedData = CreateTestFabricCookedData();
  59. AZStd::unique_ptr<NvCloth::Fabric> fabric = m_factory.CreateFabric(fabricCookedData);
  60. EXPECT_TRUE(fabric.get() != nullptr);
  61. EXPECT_TRUE(fabric->m_id.IsValid());
  62. EXPECT_TRUE(fabric->m_nvFabric.get() != nullptr);
  63. EXPECT_TRUE(fabric->m_numClothsUsingFabric == 0);
  64. EXPECT_EQ(fabric->m_id, fabricCookedData.m_id);
  65. ExpectEq(fabric->m_cookedData, fabricCookedData);
  66. }
  67. TEST_F(NvClothSystemFactory, Factory_CreateClothNoInitialParticles_ReturnsNull)
  68. {
  69. AZStd::unique_ptr<NvCloth::Cloth> cloth = m_factory.CreateCloth({}, nullptr);
  70. EXPECT_TRUE(cloth.get() == nullptr);
  71. }
  72. TEST_F(NvClothSystemFactory, Factory_CreateClothInvalidFabric_ReturnsNull)
  73. {
  74. const float width = 1.0f;
  75. const float height = 1.0f;
  76. const AZ::u32 segmentsX = 5;
  77. const AZ::u32 segmentsY = 5;
  78. const TriangleInput planeXY = CreatePlane(width, height, segmentsX, segmentsY);
  79. AZStd::unique_ptr<NvCloth::Cloth> cloth = m_factory.CreateCloth(planeXY.m_vertices, nullptr);
  80. EXPECT_TRUE(cloth.get() == nullptr);
  81. }
  82. TEST_F(NvClothSystemFactory, Factory_CreateClothInitialParticlesMismatchFabricNumParticles_ReturnsNull)
  83. {
  84. const NvCloth::FabricCookedData fabricCookedData = CreateTestFabricCookedData();
  85. auto otherVertices = fabricCookedData.m_particles;
  86. otherVertices.resize(otherVertices.size() / 2);
  87. AZStd::unique_ptr<NvCloth::Fabric> fabric = m_factory.CreateFabric(fabricCookedData);
  88. AZStd::unique_ptr<NvCloth::Cloth> cloth = m_factory.CreateCloth(
  89. otherVertices, // otherVertices has a different number of vertices than fabric
  90. fabric.get());
  91. EXPECT_TRUE(cloth.get() == nullptr);
  92. }
  93. TEST_F(NvClothSystemFactory, Factory_CreateCloth_ReturnsValidCloth)
  94. {
  95. const NvCloth::FabricCookedData fabricCookedData = CreateTestFabricCookedData();
  96. AZStd::unique_ptr<NvCloth::Fabric> fabric = m_factory.CreateFabric(fabricCookedData);
  97. AZStd::unique_ptr<NvCloth::Cloth> cloth = m_factory.CreateCloth(fabricCookedData.m_particles, fabric.get());
  98. EXPECT_TRUE(cloth.get() != nullptr);
  99. EXPECT_TRUE(cloth->GetId().IsValid());
  100. EXPECT_EQ(cloth->GetFabric(), fabric.get());
  101. EXPECT_EQ(cloth->GetSolver(), nullptr);
  102. EXPECT_THAT(cloth->GetInitialParticles(), ::testing::Pointwise(ContainerIsCloseTolerance(Tolerance), fabricCookedData.m_particles));
  103. EXPECT_EQ(cloth->GetInitialIndices(), fabricCookedData.m_indices);
  104. EXPECT_THAT(cloth->GetParticles(), ::testing::Pointwise(ContainerIsCloseTolerance(Tolerance), fabricCookedData.m_particles));
  105. EXPECT_NE(cloth->GetClothConfigurator(), nullptr);
  106. ExpectEq(cloth->GetFabricCookedData(), fabricCookedData);
  107. }
  108. } // namespace UnitTest