3
0

SpriteTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "LyShineTest.h"
  9. #include <Mocks/ISystemMock.h>
  10. #include <Sprite.h>
  11. namespace UnitTest
  12. {
  13. class LyShineSpriteTest
  14. : public LyShineTest
  15. {
  16. protected:
  17. void SetupApplication() override
  18. {
  19. AZ::ComponentApplication::Descriptor appDesc;
  20. appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024;
  21. appDesc.m_recordingMode = AZ::Debug::AllocationRecords::Mode::RECORD_FULL;
  22. AZ::ComponentApplication::StartupParameters appStartup;
  23. appStartup.m_createStaticModulesCallback =
  24. [](AZStd::vector<AZ::Module*>& modules)
  25. {
  26. modules.emplace_back(new LyShine::LyShineModule);
  27. };
  28. m_application = aznew AZ::ComponentApplication();
  29. m_systemEntity = m_application->Create(appDesc, appStartup);
  30. m_systemEntity->Init();
  31. m_systemEntity->Activate();
  32. }
  33. void SetupEnvironment() override
  34. {
  35. LyShineTest::SetupEnvironment();
  36. }
  37. void TearDown() override
  38. {
  39. LyShineTest::TearDown();
  40. }
  41. };
  42. #ifdef LYSHINE_ATOM_TODO // [GHI #6270] Support RTT using Atom
  43. TEST_F(LyShineSpriteTest, Sprite_CanAcquireRenderTarget)
  44. {
  45. // initialize to create the static sprite cache
  46. CSprite::Initialize();
  47. const char* renderTargetName = "$test";
  48. EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillRepeatedly(testing::Return(nullptr));
  49. CSprite* sprite = CSprite::CreateSprite(renderTargetName);
  50. ASSERT_NE(sprite, nullptr);
  51. ITexture* texture = sprite->GetTexture();
  52. EXPECT_EQ(texture, nullptr);
  53. ITextureMock* mockTexture = new testing::NiceMock<ITextureMock>;
  54. // expect the sprite acquires the texture and increments the reference count
  55. EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillOnce(testing::Return(mockTexture));
  56. EXPECT_CALL(*mockTexture, AddRef()).Times(1);
  57. texture = sprite->GetTexture();
  58. EXPECT_EQ(texture, mockTexture);
  59. // the sprite should attempt to release the texture by calling ReleaseResourceAsync
  60. // Using the A<type> matcher to force the type to be AZStd::unique_ptr<SResourceAsync>, but allow any value to be used for that type
  61. EXPECT_CALL(m_data->m_renderer, ReleaseResourceAsync(testing::A<AZStd::unique_ptr<SResourceAsync>>())).Times(1);
  62. delete sprite;
  63. sprite = nullptr;
  64. CSprite::Shutdown();
  65. delete mockTexture;
  66. }
  67. TEST_F(LyShineSpriteTest, Sprite_CanCreateWithExistingRenderTarget)
  68. {
  69. // initialize to create the static sprite cache
  70. CSprite::Initialize();
  71. ITextureMock* mockTexture = new testing::NiceMock<ITextureMock>;
  72. const char* renderTargetName = "$test";
  73. EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillRepeatedly(testing::Return(mockTexture));
  74. // the sprite should increment the ref count on the texture
  75. EXPECT_CALL(*mockTexture, AddRef()).Times(1);
  76. CSprite* sprite = CSprite::CreateSprite(renderTargetName);
  77. ASSERT_NE(sprite, nullptr);
  78. ITexture* texture = sprite->GetTexture();
  79. EXPECT_EQ(texture, mockTexture);
  80. // the sprite should attempt to release the texture by calling ReleaseResourceAsync
  81. EXPECT_CALL(m_data->m_renderer, ReleaseResourceAsync(testing::A<AZStd::unique_ptr<SResourceAsync>>())).Times(1);
  82. delete sprite;
  83. sprite = nullptr;
  84. CSprite::Shutdown();
  85. delete mockTexture;
  86. }
  87. #endif
  88. } //namespace UnitTest
  89. AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);