Files
o3de/Gems/LyShine/Code/Tests/SpriteTest.cpp
T
2021-03-08 14:30:57 -08:00

137 lines
4.6 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "LyShine_precompiled.h"
#include "LyShineTest.h"
#include <Mocks/ISystemMock.h>
#include <Mocks/IRendererMock.h>
#include <Mocks/ITextureMock.h>
#include <I3DEngine.h> // needed for SRenderingPassInfo
#include <Sprite.h>
namespace UnitTest
{
class LyShineSpriteTest
: public LyShineTest
{
protected:
void SetupApplication() override
{
AZ::ComponentApplication::Descriptor appDesc;
appDesc.m_memoryBlocksByteSize = 10 * 1024 * 1024;
appDesc.m_recordingMode = AZ::Debug::AllocationRecords::RECORD_FULL;
appDesc.m_stackRecordLevels = 20;
AZ::ComponentApplication::StartupParameters appStartup;
// Module needs to be created this way to create CryString allocator for test
appStartup.m_createStaticModulesCallback =
[](AZStd::vector<AZ::Module*>& modules)
{
modules.emplace_back(new LyShine::LyShineModule);
};
m_systemEntity = m_application.Create(appDesc, appStartup);
m_systemEntity->Init();
m_systemEntity->Activate();
}
void SetupEnvironment() override
{
LyShineTest::SetupEnvironment();
m_data = AZStd::make_unique<DataMembers>();
m_env->m_stubEnv.pRenderer = &m_data->m_renderer;
}
void TearDown() override
{
m_data.reset();
LyShineTest::TearDown();
}
struct DataMembers
{
testing::NiceMock<IRendererMock> m_renderer;
};
AZStd::unique_ptr<DataMembers> m_data;
};
TEST_F(LyShineSpriteTest, Sprite_CanAcquireRenderTarget)
{
// initialize to create the static sprite cache
CSprite::Initialize();
const char* renderTargetName = "$test";
EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillRepeatedly(testing::Return(nullptr));
CSprite* sprite = CSprite::CreateSprite(renderTargetName);
ASSERT_NE(sprite, nullptr);
ITexture* texture = sprite->GetTexture();
EXPECT_EQ(texture, nullptr);
ITextureMock* mockTexture = new testing::NiceMock<ITextureMock>;
// expect the sprite acquires the texture and increments the reference count
EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillOnce(testing::Return(mockTexture));
EXPECT_CALL(*mockTexture, AddRef()).Times(1);
texture = sprite->GetTexture();
EXPECT_EQ(texture, mockTexture);
// the sprite should attempt to release the texture by calling ReleaseResourceAsync
// 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
EXPECT_CALL(m_data->m_renderer, ReleaseResourceAsync(testing::A<AZStd::unique_ptr<SResourceAsync>>())).Times(1);
delete sprite;
sprite = nullptr;
CSprite::Shutdown();
delete mockTexture;
}
TEST_F(LyShineSpriteTest, Sprite_CanCreateWithExistingRenderTarget)
{
// initialize to create the static sprite cache
CSprite::Initialize();
ITextureMock* mockTexture = new testing::NiceMock<ITextureMock>;
const char* renderTargetName = "$test";
EXPECT_CALL(m_data->m_renderer, EF_GetTextureByName(testing::_, testing::_)).WillRepeatedly(testing::Return(mockTexture));
// the sprite should increment the ref count on the texture
EXPECT_CALL(*mockTexture, AddRef()).Times(1);
CSprite* sprite = CSprite::CreateSprite(renderTargetName);
ASSERT_NE(sprite, nullptr);
ITexture* texture = sprite->GetTexture();
EXPECT_EQ(texture, mockTexture);
// the sprite should attempt to release the texture by calling ReleaseResourceAsync
EXPECT_CALL(m_data->m_renderer, ReleaseResourceAsync(testing::A<AZStd::unique_ptr<SResourceAsync>>())).Times(1);
delete sprite;
sprite = nullptr;
CSprite::Shutdown();
delete mockTexture;
}
} //namespace UnitTest
AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);