Files
UnrealLuaDemo/Script/lua/tests/ue/run_ue_tests.lua
T

411 lines
19 KiB
Lua

local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__framework_context_game_context = __MODULE__.Import("framework.context.game_context", __module)
local GetGameInstanceContext = M__framework_context_game_context.GetGameInstanceContext
local Json = __MODULE__.Import("framework.core.json", __module)
local M__framework_resource_asset_catalog = __MODULE__.Import("framework.resource.asset_catalog", __module)
local ASSET_KEYS = M__framework_resource_asset_catalog.ASSET_KEYS
local BASE_ASSET_PATHS = M__framework_resource_asset_catalog.BASE_ASSET_PATHS
local M__framework_storage_local_save = __MODULE__.Import("framework.storage.local_save", __module)
local LocalSaveManager = M__framework_storage_local_save.LocalSaveManager
local M__framework_storage_storage_backend = __MODULE__.Import("framework.storage.storage_backend", __module)
local UEStorageBackend = M__framework_storage_storage_backend.UEStorageBackend
local M__game_setting_mini_settings_window = __MODULE__.Import("game.setting.mini_settings_window", __module)
local MiniSettingsWindow = M__game_setting_mini_settings_window.MiniSettingsWindow
local M__game_setting_setting_service = __MODULE__.Import("game.setting.setting_service", __module)
local SettingService = M__game_setting_setting_service.SettingService
local M__game_startup = __MODULE__.Import("game.startup", __module)
local GetPrimaryUIManager = M__game_startup.GetPrimaryUIManager
local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module)
local ScopedObject = M__share_base_scoped_object.ScopedObject
local M__tests_harness_test_runner = __MODULE__.Import("tests.harness.test_runner", __module)
local Assert = M__tests_harness_test_runner.Assert
local AssertEqual = M__tests_harness_test_runner.AssertEqual
local FormatReport = M__tests_harness_test_runner.FormatReport
local RunSuites = M__tests_harness_test_runner.RunSuites
local function PumpUEFrames(frames, dt)
if dt == nil then
dt = 0.02
end
if __UE_TEST_TICK == nil then
return
end
do
local i = 0
while i < frames do
__UE_TEST_TICK(dt)
i = i + 1
end
end
end
function ____exports.RunAllUETests(gameInstance, world)
local suites = {
{
name = "UE.NativeJson",
cases = {{
name = "ue_json native C++ module is active and preserves 64-bit integers and UTF-8",
fn = function()
Assert(
Json.IsNativeBackend(),
"expected native ue_json backend inside UE"
)
local bigInt = tonumber("9007199254740993")
AssertEqual(
math.type(bigInt),
"integer",
"bigInt is Lua 5.4 integer"
)
local input = {
version = 2,
roleId = bigInt,
title = "UE5.8中文测试",
enabled = true,
list = {10, 20, 30},
empty = Json.GetEmptyArray()
}
local encoded, encErr = Json.Encode(input)
AssertEqual(encErr, nil, "encode error")
Assert(
type(encoded) == "string",
"encoded string"
)
local decoded, decErr = Json.Decode(encoded)
AssertEqual(decErr, nil, "decode error")
local obj = decoded
AssertEqual(obj.version, 2)
AssertEqual(obj.roleId, bigInt, "int64 lossless round-trip")
AssertEqual(obj.title, "UE5.8中文测试")
AssertEqual(obj.enabled, true)
local list = obj.list
AssertEqual(list[1], 10)
AssertEqual(list[3], 30)
local badVal, badErr = Json.Decode("{broken")
AssertEqual(badVal, nil)
Assert(badErr ~= nil and badErr ~= "", "malformed JSON returns error")
end
}}
},
{
name = "UE.LocalStorage",
cases = {{
name = "ULuaStorageLibrary + ULuaSaveGame: debounce save, cold-start readback, and corruption recovery",
fn = function()
Assert(gameInstance ~= nil, "gameInstance required")
local ctx = GetGameInstanceContext(gameInstance)
Assert(ctx ~= nil, "GameInstanceContext must be initialized")
local slotId = "ue_test_slot"
local fullSlotName = "sdata_" .. slotId
UE.ULuaStorageLibrary.DeleteSlot(fullSlotName, 0, true)
local backend = __TS__New(UEStorageBackend)
local saveMgr = __TS__New(LocalSaveManager, backend, ctx.scheduler)
local settings = __TS__New(SettingService, saveMgr, slotId)
AssertEqual(
settings:GetStore():GetLastLoadStatus(),
"not_found"
)
AssertEqual(
settings:GetSoundEnabled(),
true,
"missing slot returns default"
)
settings:SetSoundEnabled(false)
PumpUEFrames(8, 0.02)
AssertEqual(
settings:GetStore():IsDirty(),
false,
"debounced save flushed to UE SaveGame"
)
Assert(
UE.ULuaStorageLibrary.DoesSlotExist(fullSlotName, 0),
"SaveGame file created on disk"
)
settings:SetSoundEnabled(true)
AssertEqual(
settings:Flush(),
"ok"
)
settings:Dispose()
saveMgr:Dispose()
local saveMgr2 = __TS__New(LocalSaveManager, backend, ctx.scheduler)
local settings2 = __TS__New(SettingService, saveMgr2, slotId)
AssertEqual(
settings2:GetStore():GetLastLoadStatus(),
"ok"
)
AssertEqual(
settings2:GetSoundEnabled(),
true,
"cold-start reads latest value"
)
settings2:Dispose()
saveMgr2:Dispose()
Assert(
UE.ULuaStorageLibrary.WriteCorruptRawSlotForTest(fullSlotName, "CORRUPTED_SAV_DATA", 0),
"corrupt injection succeeded"
)
local saveMgr3 = __TS__New(LocalSaveManager, backend, ctx.scheduler)
local settings3 = __TS__New(SettingService, saveMgr3, slotId)
AssertEqual(
settings3:GetStore():GetLastLoadStatus(),
"ok"
)
Assert(
settings3:GetStore():WasRecoveredFromBackup(),
"recovered from .bak"
)
AssertEqual(
settings3:GetSoundEnabled(),
false,
"recovered previous valid value"
)
Assert(
UE.ULuaStorageLibrary.DoesCorruptBackupExist(fullSlotName, 0),
".corrupt evidence preserved"
)
settings3:Dispose()
saveMgr3:Dispose()
UE.ULuaStorageLibrary.DeleteSlot(fullSlotName, 0, true)
end
}}
},
{
name = "UE.ResourceManager",
cases = {{
name = "ULuaResourceSubsystem: PIE Content load, PatchManifest override, coalesced cancel, and GC ref-count",
fn = function()
Assert(gameInstance ~= nil, "gameInstance required")
local ctx = GetGameInstanceContext(gameInstance)
local resSvc = ctx:GetService("resource")
local sub = UE.ULuaResourceSubsystem.GetFromGameInstance(gameInstance)
Assert(resSvc ~= nil and sub ~= nil, "ResourceService & Subsystem ready")
Assert(
resSvc:DoesAssetExist(ASSET_KEYS.MINI_SETTINGS_ICON),
"T_MiniSettingsIcon exists"
)
Assert(
resSvc:DoesAssetExist(ASSET_KEYS.MINI_SETTINGS_WIDGET),
"WBP_MiniSettings exists"
)
Assert(
not resSvc:DoesAssetExist("/Game/DoesNotExist/MissingAsset"),
"missing asset returns false"
)
local aliasKey = "tex.patch_alias_icon"
Assert(
sub:ApplyPatchManifest(string.format("{\"%s\":\"%s\"}", aliasKey, BASE_ASSET_PATHS[ASSET_KEYS.MINI_SETTINGS_ICON])),
"ApplyPatchManifest succeeded"
)
AssertEqual(
resSvc:ResolveAssetPath(aliasKey),
BASE_ASSET_PATHS[ASSET_KEYS.MINI_SETTINGS_ICON],
"PatchOverlay resolves alias key"
)
local owner1 = __TS__New(ScopedObject)
local owner2 = __TS__New(ScopedObject)
local owner3 = __TS__New(ScopedObject)
local tex1
local tex2
local cancelledFired = false
local widgetCls
local missingCalls = 0
local missingOk = true
resSvc:LoadTextureAsync(
ASSET_KEYS.MINI_SETTINGS_ICON,
owner1,
function(ok, asset)
if ok then
tex1 = asset
end
end
)
local hCancel = resSvc:LoadTextureAsync(
aliasKey,
owner3,
function()
cancelledFired = true
end
)
resSvc:LoadTextureAsync(
aliasKey,
owner2,
function(ok, asset)
if ok then
tex2 = asset
end
end
)
resSvc:LoadClassAsync(
ASSET_KEYS.MINI_SETTINGS_WIDGET,
owner1,
function(ok, cls)
if ok then
widgetCls = cls
end
end
)
resSvc:LoadObjectAsync(
"/Game/DoesNotExist/MissingAsset",
owner1,
function(ok)
missingCalls = missingCalls + 1
missingOk = ok
end
)
hCancel:Cancel()
PumpUEFrames(15, 0.02)
Assert(not cancelledFired, "cancelled caller must not receive callback")
Assert(
tex1 ~= nil and UE.UKismetSystemLibrary.IsValid(tex1),
"tex1 loaded & valid"
)
Assert(
tex2 ~= nil and UE.UKismetSystemLibrary.IsValid(tex2),
"tex2 loaded & valid"
)
Assert(
widgetCls ~= nil and UE.UKismetSystemLibrary.IsValid(widgetCls),
"WBP_MiniSettings class loaded"
)
AssertEqual(missingCalls, 1, "missing asset delivers exactly once")
AssertEqual(missingOk, false, "missing asset reports ok=false")
owner1:Dispose()
if __UE_TEST_GC ~= nil then
__UE_TEST_GC()
end
Assert(
UE.UKismetSystemLibrary.IsValid(tex2),
"tex2 survives GC while owner2 holds ref"
)
AssertEqual(
sub:GetAssetRefCount(ASSET_KEYS.MINI_SETTINGS_ICON),
1,
"refcount decremented to 1"
)
owner2:Dispose()
owner3:Dispose()
sub:ClearPatchManifest()
if __UE_TEST_GC ~= nil then
__UE_TEST_GC()
end
local diag = resSvc:GetDiagnostics()
AssertEqual(diag.tsPendingRequests, 0, "tsPending back to 0")
AssertEqual(diag.nativeInFlight, 0, "nativeInFlight back to 0")
AssertEqual(diag.nativeRetainedAssets, 0, "nativeRetained back to 0")
AssertEqual(diag.nativeTotalRefCount, 0, "nativeTotalRefCount back to 0")
end
}}
},
{
name = "UE.MiniSettingsUMG",
cases = {{
name = "Real WBP_MiniSettings open, async icon, button click delegate, SaveGame persist, and clean baseline teardown",
fn = function()
Assert(gameInstance ~= nil and world ~= nil, "GI & World required")
local ctx = GetGameInstanceContext(gameInstance)
local uiMgr = GetPrimaryUIManager(ctx)
local settingSvc = ctx:GetService("setting")
local resSvc = ctx:GetService("resource")
local uiBridge = ctx:GetService("uiDelegateBridge")
UE.ULuaStorageLibrary.DeleteSlot("sdata", 0, true)
settingSvc:GetStore():Reload()
AssertEqual(
settingSvc:GetSoundEnabled(),
true,
"initial sound ON"
)
ctx:OnWorldCleanup(world)
local openedOk = false
local win = uiMgr:ShowUI(
MiniSettingsWindow,
nil,
function(ok)
openedOk = ok
end
)
Assert(win ~= nil, "MiniSettingsWindow controller created")
PumpUEFrames(20, 0.02)
Assert(openedOk, "MiniSettingsWindow opened callback fired")
Assert(
win:IsLoaded(),
"widget loaded"
)
Assert(win.iconLoaded, "T_MiniSettingsIcon loaded and applied to ImgIcon")
Assert(win.btnToggle ~= nil and win.btnClose ~= nil, "named UMG buttons bound")
Assert(
UE.ULuaUIEventSubsystem.SimulateButtonClick(win.btnToggle),
"SimulateButtonClick BtnToggle"
)
AssertEqual(win.toggleClickCount, 1, "toggle click handled exactly once")
AssertEqual(
settingSvc:GetSoundEnabled(),
false,
"sound toggled OFF"
)
PumpUEFrames(8, 0.02)
AssertEqual(
settingSvc:GetStore():IsDirty(),
false,
"setting persisted to UE SaveGame"
)
Assert(
UE.ULuaUIEventSubsystem.SimulateButtonClick(win.btnClose),
"SimulateButtonClick BtnClose"
)
Assert(
win:IsDisposed(),
"MiniSettingsWindow disposed after close"
)
AssertEqual(
uiMgr:GetDiagnostics().openWindows,
0,
"openWindows back to 0"
)
AssertEqual(
uiBridge:GetActiveBindingCount(),
0,
"button delegate bindings back to 0"
)
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0,
"resource refcount back to 0"
)
local freshStore = __TS__New(
LocalSaveManager,
__TS__New(UEStorageBackend),
ctx.scheduler
)
local freshSettings = __TS__New(SettingService, freshStore)
AssertEqual(
freshSettings:GetSoundEnabled(),
false,
"cold readback reads OFF"
)
freshSettings:Dispose()
freshStore:Dispose()
UE.ULuaStorageLibrary.DeleteSlot("sdata", 0, true)
end
}}
}
}
local summary = RunSuites(suites)
print(FormatReport(summary))
return summary
end
function __module.UpdateImports()
GetGameInstanceContext = M__framework_context_game_context.GetGameInstanceContext
ASSET_KEYS = M__framework_resource_asset_catalog.ASSET_KEYS
BASE_ASSET_PATHS = M__framework_resource_asset_catalog.BASE_ASSET_PATHS
LocalSaveManager = M__framework_storage_local_save.LocalSaveManager
UEStorageBackend = M__framework_storage_storage_backend.UEStorageBackend
MiniSettingsWindow = M__game_setting_mini_settings_window.MiniSettingsWindow
SettingService = M__game_setting_setting_service.SettingService
GetPrimaryUIManager = M__game_startup.GetPrimaryUIManager
ScopedObject = M__share_base_scoped_object.ScopedObject
Assert = M__tests_harness_test_runner.Assert
AssertEqual = M__tests_harness_test_runner.AssertEqual
FormatReport = M__tests_harness_test_runner.FormatReport
RunSuites = M__tests_harness_test_runner.RunSuites
end