PrefabBuilderTests.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #pragma once
  9. #include <PrefabBuilderComponent.h>
  10. #include <AzTest/AzTest.h>
  11. #include <Application/ToolsApplication.h>
  12. #include <AzCore/Asset/AssetManager.h>
  13. #include <AzCore/Component/ComponentApplication.h>
  14. #include <AzCore/Asset/AssetSerializer.h>
  15. namespace UnitTest
  16. {
  17. struct VersionChangingData final : AZ::Data::AssetData
  18. {
  19. AZ_RTTI(VersionChangingData, "{E3A37E19-AE61-4C2F-809E-03B4D83261E8}", AZ::Data::AssetData);
  20. static void Reflect(AZ::ReflectContext* context)
  21. {
  22. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<VersionChangingData>()->Version(m_version);
  25. }
  26. }
  27. inline static int m_version = 0;
  28. };
  29. struct TestAsset final : AZ::Data::AssetData
  30. {
  31. AZ_RTTI(TestAsset, "{8E736462-5424-4720-A2D9-F71DFC5905E3}", AZ::Data::AssetData);
  32. static void Reflect(AZ::ReflectContext* context)
  33. {
  34. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  35. {
  36. serializeContext->Class<TestAsset>();
  37. }
  38. }
  39. };
  40. struct TestAssetHandler final : AZ::Data::AssetHandler
  41. {
  42. public:
  43. AZ::Data::AssetPtr CreateAsset(
  44. [[maybe_unused]] const AZ::Data::AssetId& id, [[maybe_unused]] const AZ::Data::AssetType& type) override
  45. {
  46. return aznew TestAsset();
  47. }
  48. void DestroyAsset(AZ::Data::AssetPtr ptr) override
  49. {
  50. delete ptr;
  51. }
  52. void GetHandledAssetTypes(AZStd::vector<AZ::Data::AssetType>& assetTypes) override
  53. {
  54. assetTypes.push_back(azrtti_typeid<TestAsset>());
  55. }
  56. AZ::Data::AssetHandler::LoadResult LoadAssetData(
  57. [[maybe_unused]] const AZ::Data::Asset<AZ::Data::AssetData>& asset,
  58. [[maybe_unused]] AZStd::shared_ptr<AZ::Data::AssetDataStream> stream,
  59. [[maybe_unused]] const AZ::Data::AssetFilterCB& assetLoadFilterCB) override
  60. {
  61. return AZ::Data::AssetHandler::LoadResult::LoadComplete;
  62. }
  63. };
  64. struct TestComponent final : AZ::Component
  65. {
  66. AZ_COMPONENT(TestComponent, "{E3982C6A-0B01-4B04-A3E2-D95729D4B9C6}");
  67. static void Reflect(AZ::ReflectContext* context)
  68. {
  69. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  70. {
  71. serializeContext->Class<TestComponent, AZ::Component>()
  72. ->Field("Asset", &TestComponent::m_asset)
  73. ->Field("Version", &TestComponent::m_version);
  74. }
  75. }
  76. void Activate() override
  77. {
  78. }
  79. void Deactivate() override
  80. {
  81. }
  82. AZ::Data::Asset<TestAsset> m_asset{};
  83. VersionChangingData m_version;
  84. };
  85. struct SelfContainedMemoryStream : AZ::IO::ByteContainerStream<AZStd::vector<char>>
  86. {
  87. SelfContainedMemoryStream() : ByteContainerStream<AZStd::vector<char>>{&m_bufferData}
  88. {
  89. }
  90. AZStd::vector<char> m_bufferData;
  91. };
  92. struct TestPrefabBuilderComponent final : AZ::Prefab::PrefabBuilderComponent
  93. {
  94. protected:
  95. AZStd::unique_ptr<AZ::IO::GenericStream> GetOutputStream(const AZ::IO::Path& path) const override;
  96. };
  97. struct PrefabBuilderTests : ::testing::Test
  98. {
  99. protected:
  100. void SetUp() override;
  101. void TearDown() override;
  102. AzToolsFramework::ToolsApplication m_app;
  103. AZStd::unique_ptr<AZ::ComponentDescriptor> m_testComponentDescriptor{};
  104. TestAssetHandler m_assetHandler;
  105. };
  106. }