MetaCore/tests/MetaCoreTests.cpp
2026-03-19 18:01:50 +08:00

150 lines
6.6 KiB
C++

#include "MetaCore/engine/editor_core/CommandService.h"
#include "MetaCore/engine/editor_core/EditorSession.h"
#include "MetaCore/engine/editor_core/SceneCommands.h"
#include "MetaCore/engine/editor_core/SelectionService.h"
#include "MetaCore/engine/editor_core/TransformGizmoService.h"
#include "MetaCore/engine/editor_core/ViewportService.h"
#include "MetaCore/engine/scene/Project.h"
#include "MetaCore/engine/scene/Scene.h"
#include "MetaCore/engine/scene/SceneSerializer.h"
#include "MetaCore/platform/panda/PandaPlatformContext.h"
#include "MetaCore/platform/panda/PandaRenderBackend.h"
#include <cstdlib>
#include <filesystem>
#include <iostream>
namespace {
void expect(const bool condition, const char* message) {
if (!condition) {
std::cerr << "Test failed: " << message << '\n';
std::exit(1);
}
}
} // namespace
int main() {
namespace fs = std::filesystem;
using metacore::engine::editor_core::Command;
using metacore::engine::editor_core::CommandService;
using metacore::engine::editor_core::EditorSession;
using metacore::engine::editor_core::SceneCommands;
using metacore::engine::editor_core::SelectionService;
using metacore::engine::editor_core::TransformGizmoService;
using metacore::engine::editor_core::ViewportService;
using metacore::platform::panda::PandaPlatformContext;
using metacore::platform::panda::PandaRenderBackend;
using metacore::engine::scene::Project;
using metacore::engine::scene::ProjectService;
using metacore::engine::scene::Scene;
using metacore::engine::scene::SceneSerializer;
using metacore::engine::scene::SceneService;
const fs::path root = "D:/MetaCore/TestProject";
fs::remove_all(root);
ProjectService project_service;
expect(project_service.create_project(root, "MetaCoreTest"), "project should be created");
Project project;
expect(project_service.load_project(root / "MetaCore.project.json", project), "project should load");
expect(project.name == "MetaCoreTest", "project name should persist");
SceneService scene_service;
Scene scene = scene_service.create_default_scene();
auto& cube = scene_service.create_object(scene, "Cube");
cube.has_mesh_renderer = true;
cube.mesh_renderer.mesh_asset_id = "mesh.cube";
SceneSerializer serializer;
expect(serializer.save_scene(root / "Scenes" / "Test.mcscene.json", scene), "scene should save");
Scene loaded_scene;
expect(serializer.load_scene(root / "Scenes" / "Test.mcscene.json", loaded_scene), "scene should load");
expect(loaded_scene.objects.size() == scene.objects.size(), "scene object count should match");
int value = 0;
CommandService commands;
commands.execute(Command{
"increment",
[&value]() { value += 1; },
[&value]() { value -= 1; }
});
expect(value == 1, "command should execute");
commands.undo();
expect(value == 0, "command should undo");
commands.redo();
expect(value == 1, "command should redo");
TransformGizmoService gizmo;
gizmo.set_operation(TransformGizmoService::Operation::Rotate);
gizmo.set_space(TransformGizmoService::Space::World);
gizmo.set_snap_enabled(true);
gizmo.set_snap_values({15.0F, 15.0F, 15.0F});
expect(gizmo.operation() == TransformGizmoService::Operation::Rotate, "gizmo operation should persist");
expect(gizmo.space() == TransformGizmoService::Space::World, "gizmo space should persist");
expect(gizmo.snap_enabled(), "gizmo snap should persist");
expect(gizmo.snap_values()[0] == 15.0F, "gizmo snap values should persist");
PandaPlatformContext platform;
expect(platform.initialize("MetaCoreTests"), "platform should initialize");
platform.pump_frame();
platform.pump_frame();
expect(platform.frame_count() == 2, "platform frame count should increment");
platform.request_close();
expect(platform.should_close(), "platform close request should persist");
platform.shutdown();
PandaRenderBackend render_backend;
expect(render_backend.initialize(), "render backend should initialize");
metacore::engine::render::UiDrawFrame ui_frame;
ui_frame.display_size = {1280.0F, 720.0F};
ui_frame.total_command_count = 2;
ui_frame.total_vertex_count = 6;
ui_frame.total_index_count = 9;
ui_frame.vertices.push_back({{10.0F, 20.0F}, {0.0F, 0.0F}, 0xffffffffu});
ui_frame.indices.push_back(0);
ui_frame.commands.push_back({{0.0F, 0.0F, 100.0F, 50.0F}, 0, 3, 0, 0});
render_backend.set_ui_draw_frame(ui_frame);
expect(render_backend.ui_draw_frame().total_command_count == 2, "ui draw frame command count should persist");
expect(render_backend.ui_draw_frame().vertices.size() == 1, "ui draw frame vertices should persist");
expect(render_backend.submitted_ui_frame_count() == 1, "ui draw frame submissions should be tracked");
render_backend.shutdown();
EditorSession session;
SelectionService selection;
ViewportService viewport;
SceneCommands::create_empty_object(commands, session, selection, scene_service, loaded_scene, "Test Node");
expect(selection.has_selection(), "selection should exist after object creation");
SceneCommands::rename_selected_object(commands, session, selection, scene_service, loaded_scene, "Renamed Node");
expect(scene_service.find_object(loaded_scene, *selection.selection())->get().name == "Renamed Node", "rename should persist");
metacore::engine::scene::TransformComponent edited_transform;
edited_transform.position = {1.0F, 2.0F, 3.0F};
edited_transform.rotation = {10.0F, 20.0F, 30.0F};
edited_transform.scale = {2.0F, 2.0F, 2.0F};
SceneCommands::apply_transform_to_selected_object(commands, session, selection, scene_service, loaded_scene, edited_transform);
expect(
scene_service.find_object(loaded_scene, *selection.selection())->get().transform.position[0] == 1.0F,
"transform apply should persist"
);
commands.undo();
expect(
scene_service.find_object(loaded_scene, *selection.selection())->get().transform.position[0] == 0.0F,
"transform undo should restore"
);
commands.redo();
expect(
scene_service.find_object(loaded_scene, *selection.selection())->get().transform.position[0] == 1.0F,
"transform redo should reapply"
);
SceneCommands::focus_selected_object(session, selection, viewport);
expect(viewport.focus_target().has_value(), "focus target should be set");
SceneCommands::delete_selected_object(commands, session, selection, scene_service, loaded_scene);
expect(!selection.has_selection(), "selection should clear on delete");
std::cout << "MetaCoreTests passed\n";
return 0;
}