* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "engine_factory.h"
#include "i_engine.h"
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#include <thread>
#include <mutex>
#include <functional>
namespace OHOS::Render3D {
class EngineFactoryTest : public testing::Test {
public:
static void SetUpTestCase()
{}
static void TearDownTestCase()
{}
void SetUp() override
{}
void TearDown() override
{}
};
* @tc.name: CreateEngine_LumeType_Success
* @tc.desc: Verify creating LUME engine type returns valid engine instance
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_LumeType_Success, testing::ext::TestSize.Level1)
{
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
EXPECT_NE(engine.get(), nullptr);
}
* @tc.name: CreateEngine_DefaultType_Success
* @tc.desc: Verify creating default engine type returns valid engine instance
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_DefaultType_Success, testing::ext::TestSize.Level1)
{
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
EXPECT_TRUE(engine != nullptr);
}
* @tc.name: CreateEngine_MultipleInstances_Success
* @tc.desc: Verify creating multiple engine instances works correctly
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_MultipleInstances_Success, testing::ext::TestSize.Level1)
{
auto engine1 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine1, nullptr);
auto engine2 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine2, nullptr);
EXPECT_NE(engine1.get(), nullptr);
EXPECT_NE(engine2.get(), nullptr);
EXPECT_NE(engine1.get(), engine2.get());
}
* @tc.name: CreateEngine_UniquePtr_ValidOwnership
* @tc.desc: Verify engine is created as unique_ptr with proper ownership
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_UniquePtr_ValidOwnership, testing::ext::TestSize.Level1)
{
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
std::unique_ptr<IEngine> engineMoved = std::move(engine);
EXPECT_EQ(engine, nullptr);
EXPECT_NE(engineMoved, nullptr);
}
* @tc.name: CreateEngine_Loop_MemoryStability
* @tc.desc: Verify creating and destroying engines in a loop doesn't cause memory leaks
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_Loop_MemoryStability, testing::ext::TestSize.Level1)
{
const int iterationCount = 100;
int validEngineCount = 0;
for (int i = 0; i < iterationCount; ++i) {
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
EXPECT_NE(engine.get(), nullptr);
if (engine && engine.get()) {
validEngineCount++;
}
}
EXPECT_EQ(validEngineCount, iterationCount);
}
* @tc.name: CreateEngine_Reset_SafeDestruction
* @tc.desc: Verify resetting engine pointer correctly destroys the engine
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_Reset_SafeDestruction, testing::ext::TestSize.Level1)
{
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
engine.reset();
EXPECT_EQ(engine, nullptr);
}
* @tc.name: CreateEngine_Release_ManualDestruction
* @tc.desc: Verify manually releasing engine pointer works correctly
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_Release_ManualDestruction, testing::ext::TestSize.Level1)
{
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
IEngine* rawPtr = engine.get();
ASSERT_NE(rawPtr, nullptr);
IEngine* releasedPtr = engine.release();
EXPECT_EQ(releasedPtr, rawPtr);
EXPECT_EQ(engine, nullptr);
delete releasedPtr;
}
* @tc.name: CreateEngine_Scope_AutoDestruction
* @tc.desc: Verify engine is automatically destroyed when going out of scope
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_Scope_AutoDestruction, testing::ext::TestSize.Level1)
{
IEngine* rawPtr = nullptr;
auto engine = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine, nullptr);
ASSERT_NE(engine.get(), nullptr);
rawPtr = engine.get();
auto engine2 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
EXPECT_NE(engine2, nullptr);
if (rawPtr != nullptr) {
EXPECT_NE(engine2.get(), rawPtr);
}
}
* @tc.name: CreateEngine_Swap_ValidExchange
* @tc.desc: Verify swapping two engine instances works correctly
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_Swap_ValidExchange, testing::ext::TestSize.Level1)
{
auto engine1 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
auto engine2 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
auto* ptr1 = engine1.get();
auto* ptr2 = engine2.get();
engine1.swap(engine2);
EXPECT_EQ(engine1.get(), ptr2);
EXPECT_EQ(engine2.get(), ptr1);
}
* @tc.name: CreateEngine_MoveAssignment_ValidTransfer
* @tc.desc: Verify move assignment transfers ownership correctly
* @tc.type: FUNC
*/
HWTEST_F(EngineFactoryTest, CreateEngine_MoveAssignment_ValidTransfer, testing::ext::TestSize.Level1)
{
auto engine1 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
auto* originalPtr = engine1.get();
ASSERT_NE(originalPtr, nullptr);
auto engine2 = EngineFactory::CreateEngine(EngineFactory::EngineType::LUME);
ASSERT_NE(engine2.get(), nullptr);
engine1 = std::move(engine2);
EXPECT_NE(engine1.get(), originalPtr);
EXPECT_EQ(engine2, nullptr);
}
}