Files
XCEngine/tests/Components/test_component.cpp

133 lines
3.4 KiB
C++

#include <gtest/gtest.h>
#include <XCEngine/Components/Component.h>
#include <XCEngine/Components/GameObject.h>
using namespace XCEngine::Components;
namespace {
class TestComponent : public Component {
public:
TestComponent() = default;
explicit TestComponent(const std::string& name) : m_customName(name) {}
std::string GetName() const override {
return m_customName.empty() ? "TestComponent" : m_customName;
}
bool m_awakeCalled = false;
bool m_startCalled = false;
bool m_updateCalled = false;
bool m_onEnableCalled = false;
bool m_onDisableCalled = false;
bool m_onDestroyCalled = false;
int m_onEnableCount = 0;
int m_onDisableCount = 0;
void Awake() override { m_awakeCalled = true; }
void Start() override { m_startCalled = true; }
void Update(float deltaTime) override { m_updateCalled = true; }
void OnEnable() override { m_onEnableCalled = true; ++m_onEnableCount; }
void OnDisable() override { m_onDisableCalled = true; ++m_onDisableCount; }
void OnDestroy() override { m_onDestroyCalled = true; }
private:
std::string m_customName;
};
TEST(Component_Test, DefaultConstructor) {
TestComponent comp;
EXPECT_EQ(comp.GetName(), "TestComponent");
}
TEST(Component_Test, GetGameObject_ReturnsNullptr_WhenNotAttached) {
TestComponent comp;
EXPECT_EQ(comp.GetGameObject(), nullptr);
}
TEST(Component_Test, IsEnabled_DefaultTrue) {
TestComponent comp;
EXPECT_TRUE(comp.IsEnabled());
}
TEST(Component_Test, SetEnabled_TrueToFalse) {
TestComponent comp;
EXPECT_TRUE(comp.IsEnabled());
comp.SetEnabled(false);
EXPECT_FALSE(comp.IsEnabled());
}
TEST(Component_Test, SetEnabled_FalseToTrue) {
TestComponent comp;
comp.SetEnabled(false);
EXPECT_FALSE(comp.IsEnabled());
comp.SetEnabled(true);
EXPECT_TRUE(comp.IsEnabled());
}
TEST(Component_Test, Lifecycle_AwakeCalled) {
TestComponent comp;
comp.Awake();
EXPECT_TRUE(comp.m_awakeCalled);
}
TEST(Component_Test, Lifecycle_StartCalled) {
TestComponent comp;
comp.Start();
EXPECT_TRUE(comp.m_startCalled);
}
TEST(Component_Test, Lifecycle_UpdateCalled) {
TestComponent comp;
comp.Update(0.016f);
EXPECT_TRUE(comp.m_updateCalled);
}
TEST(Component_Test, Lifecycle_OnEnableCalled_WhenEnabling) {
TestComponent comp;
comp.SetEnabled(false);
comp.SetEnabled(true);
EXPECT_TRUE(comp.m_onEnableCalled);
}
TEST(Component_Test, Lifecycle_OnDisableCalled_WhenDisabling) {
TestComponent comp;
comp.SetEnabled(false);
EXPECT_TRUE(comp.m_onDisableCalled);
}
TEST(Component_Test, Lifecycle_OnDestroyCalled) {
TestComponent comp;
comp.OnDestroy();
EXPECT_TRUE(comp.m_onDestroyCalled);
}
TEST(Component_Test, SetEnabled_NoCallback_WhenStateUnchanged) {
TestComponent comp;
comp.SetEnabled(true);
EXPECT_FALSE(comp.m_onEnableCalled);
EXPECT_FALSE(comp.m_onDisableCalled);
}
TEST(Component_Test, SetEnabled_AttachedInactiveGameObject_DelaysOnEnableUntilActivation) {
GameObject go;
TestComponent* comp = go.AddComponent<TestComponent>();
go.SetActive(false);
comp->m_onEnableCalled = false;
comp->m_onDisableCalled = false;
comp->m_onEnableCount = 0;
comp->m_onDisableCount = 0;
comp->SetEnabled(false);
EXPECT_EQ(comp->m_onDisableCount, 0);
comp->SetEnabled(true);
EXPECT_EQ(comp->m_onEnableCount, 0);
go.SetActive(true);
EXPECT_EQ(comp->m_onEnableCount, 1);
}
} // namespace