Files
UnrealLuaDemo/Script/lua/tests/unit/ui_and_resource_test.lua
T

624 lines
25 KiB
Lua
Raw Normal View History

local ____lualib = require("lualib_bundle")
local __TS__ClassExtends = ____lualib.__TS__ClassExtends
local __TS__New = ____lualib.__TS__New
local ____exports = __module.env
local M__framework_context_game_context = __MODULE__.Import("framework.context.game_context", __module)
local GameInstanceContext = M__framework_context_game_context.GameInstanceContext
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_resource_resource_service = __MODULE__.Import("framework.resource.resource_service", __module)
local ResourceService = M__framework_resource_resource_service.ResourceService
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 MemoryStorageBackend = M__framework_storage_storage_backend.MemoryStorageBackend
local M__framework_ui_ui_config = __MODULE__.Import("framework.ui.ui_config", __module)
local RegisterUIConfig = M__framework_ui_ui_config.RegisterUIConfig
local M__framework_ui_ui_delegate_bridge = __MODULE__.Import("framework.ui.ui_delegate_bridge", __module)
local UIDelegateBridge = M__framework_ui_ui_delegate_bridge.UIDelegateBridge
local M__framework_ui_ui_manager = __MODULE__.Import("framework.ui.ui_manager", __module)
local UIManager = M__framework_ui_ui_manager.UIManager
local M__framework_ui_ui_window = __MODULE__.Import("framework.ui.ui_window", __module)
local UIWindow = M__framework_ui_ui_window.UIWindow
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__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 MockResourceBackend = __TS__DefineClass("MockResourceBackend", __module)
MockResourceBackend.name = "MockResourceBackend"
function MockResourceBackend.prototype.____constructor(self)
self.nextReqId = 0
self.pending = {}
self.refCounts = {}
self.loadRequestsCount = 0
end
function MockResourceBackend.prototype.ResolveAssetPath(self, keyOrPath)
return BASE_ASSET_PATHS[keyOrPath] or keyOrPath
end
function MockResourceBackend.prototype.DoesAssetExist(self, keyOrPath)
local resolved = self:ResolveAssetPath(keyOrPath)
return resolved ~= "/Game/Missing/BadAsset"
end
function MockResourceBackend.prototype.IsPackagedMode(self)
return false
end
function MockResourceBackend.prototype.RequestLoadObjectAsync(self, keyOrPath, worldGen)
self.nextReqId = self.nextReqId + 1
self.loadRequestsCount = self.loadRequestsCount + 1
self.pending[self.nextReqId] = {keyOrPath = keyOrPath, worldGen = worldGen, isClass = false}
return self.nextReqId
end
function MockResourceBackend.prototype.RequestLoadClassAsync(self, keyOrPath, worldGen)
self.nextReqId = self.nextReqId + 1
self.loadRequestsCount = self.loadRequestsCount + 1
self.pending[self.nextReqId] = {keyOrPath = keyOrPath, worldGen = worldGen, isClass = true}
return self.nextReqId
end
function MockResourceBackend.prototype.FlushAll(self)
local ids = {}
for id in pairs(self.pending) do
ids[#ids + 1] = id
end
table.sort(ids)
do
local i = 0
while i < #ids do
local id = ids[i + 1]
local req = self.pending[id]
if req ~= nil then
self.pending[id] = nil
local resolved = self:ResolveAssetPath(req.keyOrPath)
local exists = self:DoesAssetExist(req.keyOrPath)
if exists then
self.refCounts[resolved] = (self.refCounts[resolved] or 0) + 1
local ____opt_0 = self.service
if ____opt_0 ~= nil then
____opt_0:HandleNativeLoadComplete(
id,
true,
{mockAsset = resolved},
resolved,
""
)
end
else
local ____opt_2 = self.service
if ____opt_2 ~= nil then
____opt_2:HandleNativeLoadComplete(
id,
false,
nil,
resolved,
"Asset not found"
)
end
end
end
i = i + 1
end
end
end
function MockResourceBackend.prototype.CancelRequest(self, requestId)
if self.pending[requestId] == nil then
return false
end
self.pending[requestId] = nil
return true
end
function MockResourceBackend.prototype.CancelRequestsForWorld(self, worldGen)
local count = 0
for id, req in pairs(self.pending) do
if req ~= nil and req.worldGen == worldGen then
self.pending[id] = nil
count = count + 1
end
end
return count
end
function MockResourceBackend.prototype.ReleaseAsset(self, resolvedSoftPath)
local cur = self.refCounts[resolvedSoftPath] or 0
if cur <= 1 then
self.refCounts[resolvedSoftPath] = nil
else
self.refCounts[resolvedSoftPath] = cur - 1
end
end
function MockResourceBackend.prototype.GetResourceStats(self)
local inFlight = 0
for _, v in pairs(self.pending) do
if v ~= nil then
inFlight = inFlight + 1
end
end
local retained = 0
local totalRefs = 0
for _, refs in pairs(self.refCounts) do
if refs ~= nil and refs > 0 then
retained = retained + 1
totalRefs = totalRefs + refs
end
end
return inFlight, retained, totalRefs
end
local MockButtonBackend = __TS__DefineClass("MockButtonBackend", __module)
MockButtonBackend.name = "MockButtonBackend"
function MockButtonBackend.prototype.____constructor(self)
self.nextId = 0
self.buttonToIds = {}
self.idToButton = {}
end
function MockButtonBackend.prototype.BindButtonClicked(self, button)
self.nextId = self.nextId + 1
local id = self.nextId
self.idToButton[id] = button
local list = self.buttonToIds[button]
if list == nil then
list = {}
self.buttonToIds[button] = list
end
list[#list + 1] = id
return id
end
function MockButtonBackend.prototype.UnbindButtonClicked(self, bindingId)
local btn = self.idToButton[bindingId]
if btn == nil then
return false
end
self.idToButton[bindingId] = nil
local list = self.buttonToIds[btn]
if list ~= nil then
do
local i = #list - 1
while i >= 0 do
if list[i + 1] == bindingId then
table.remove(list, i + 1)
end
i = i - 1
end
end
end
return true
end
function MockButtonBackend.prototype.Click(self, button)
local list = self.buttonToIds[button]
if list == nil then
return
end
local snapshot = {}
do
local i = 0
while i < #list do
snapshot[i + 1] = list[i + 1]
i = i + 1
end
end
do
local i = 0
while i < #snapshot do
local ____opt_4 = self.bridge
if ____opt_4 ~= nil then
____opt_4:DispatchButtonClicked(snapshot[i + 1])
end
i = i + 1
end
end
end
function MockButtonBackend.prototype.GetActiveBindingCount(self)
local count = 0
for _, btn in pairs(self.idToButton) do
if btn ~= nil then
count = count + 1
end
end
return count
end
local MockWidgetFactory = __TS__DefineClass("MockWidgetFactory", __module)
MockWidgetFactory.name = "MockWidgetFactory"
function MockWidgetFactory.prototype.____constructor(self)
self.createdCount = 0
end
function MockWidgetFactory.prototype.CreateWidget(self)
self.createdCount = self.createdCount + 1
local txtStatus = {
text = "",
SetText = function(self, t)
self.text = t
end
}
local txtToggleLabel = {
text = "",
SetText = function(self, t)
self.text = t
end
}
local imgIcon = {
texture = nil,
SetBrushFromTexture = function(self, tex)
self.texture = tex
end
}
return {
valid = true,
inViewport = false,
visible = false,
zOrder = 0,
children = {
ImgIcon = imgIcon,
TxtStatus = txtStatus,
BtnToggle = {name = "BtnToggle"},
TxtToggleLabel = txtToggleLabel,
BtnClose = {name = "BtnClose"}
}
}
end
local MockWidgetAdapter = __TS__DefineClass("MockWidgetAdapter", __module)
MockWidgetAdapter.name = "MockWidgetAdapter"
function MockWidgetAdapter.prototype.____constructor(self)
end
function MockWidgetAdapter.prototype.FindChild(self, widget, name)
return widget.children[name]
end
function MockWidgetAdapter.prototype.AddToViewport(self, widget, zOrder)
widget.inViewport = true
widget.zOrder = zOrder
end
function MockWidgetAdapter.prototype.RemoveFromParent(self, widget)
widget.inViewport = false
end
function MockWidgetAdapter.prototype.SetVisible(self, widget, visible)
widget.visible = visible
end
function MockWidgetAdapter.prototype.IsWidgetValid(self, widget)
return widget ~= nil and widget.valid
end
local CachedTestWindow = __TS__DefineClass("CachedTestWindow", __module)
CachedTestWindow.name = "CachedTestWindow"
__TS__ClassExtends(CachedTestWindow, UIWindow)
function CachedTestWindow.prototype.____constructor(self, ...)
UIWindow.prototype.____constructor(self, ...)
self.reuseCount = 0
self.clickCount = 0
end
function CachedTestWindow.prototype.OnBindWidgets(self)
self.btn = self:RequireWidget("BtnToggle")
self:BindButtonClick(
self.btn,
function()
self.clickCount = self.clickCount + 1
end
)
end
function CachedTestWindow.prototype.OnReuseUI(self)
self.reuseCount = self.reuseCount + 1
end
RegisterUIConfig(CachedTestWindow, {
name = "CachedTestWindow",
assetKey = ASSET_KEYS.MINI_SETTINGS_WIDGET,
singleton = true,
layer = "normal",
cacheTag = "cache"
})
local FullscreenTestWindow = __TS__DefineClass("FullscreenTestWindow", __module)
FullscreenTestWindow.name = "FullscreenTestWindow"
__TS__ClassExtends(FullscreenTestWindow, UIWindow)
function FullscreenTestWindow.prototype.OnBindWidgets(self)
self:RequireWidget("BtnClose")
end
RegisterUIConfig(FullscreenTestWindow, {
name = "FullscreenTestWindow",
assetKey = ASSET_KEYS.MINI_SETTINGS_WIDGET,
singleton = true,
layer = "normal",
type = "fullscreen",
cacheTag = "no_cache",
blockWorldClick = true
})
local ToastTestWindow = __TS__DefineClass("ToastTestWindow", __module)
ToastTestWindow.name = "ToastTestWindow"
__TS__ClassExtends(ToastTestWindow, UIWindow)
function ToastTestWindow.prototype.OnBindWidgets(self)
self:RequireWidget("TxtStatus")
end
RegisterUIConfig(ToastTestWindow, {
name = "ToastTestWindow",
assetKey = ASSET_KEYS.MINI_SETTINGS_WIDGET,
singleton = false,
layer = "tips",
type = "popup",
cacheTag = "no_cache",
blockWorldClick = false
})
local SystemTestWindow = __TS__DefineClass("SystemTestWindow", __module)
SystemTestWindow.name = "SystemTestWindow"
__TS__ClassExtends(SystemTestWindow, UIWindow)
function SystemTestWindow.prototype.OnBindWidgets(self)
self:RequireWidget("BtnClose")
end
RegisterUIConfig(SystemTestWindow, {
name = "SystemTestWindow",
assetKey = ASSET_KEYS.MINI_SETTINGS_WIDGET,
singleton = true,
layer = "system",
type = "popup",
cacheTag = "no_cache",
blockWorldClick = true
})
function ____exports.CreateUIAndResourceSuite()
return {
name = "Step2B_3.UIAndResource",
cases = {
{
name = "MiniSettingsWindow full lifecycle: open, load icon, click once, persist, close, reopen, zero leaks",
fn = function()
local ctx = __TS__New(GameInstanceContext)
local storageBackend = __TS__New(MemoryStorageBackend)
local saveMgr = __TS__New(LocalSaveManager, storageBackend, ctx.scheduler)
local settingSvc = __TS__New(SettingService, saveMgr, "ui_test")
local resBackend = __TS__New(MockResourceBackend)
local resSvc = __TS__New(ResourceService, ctx, resBackend)
resBackend.service = resSvc
local btnBackend = __TS__New(MockButtonBackend)
local btnBridge = __TS__New(UIDelegateBridge, ctx, btnBackend)
btnBackend.bridge = btnBridge
ctx:RegisterService("saveManager", saveMgr)
ctx:RegisterService("setting", settingSvc)
ctx:RegisterService("resource", resSvc)
ctx:RegisterService("uiDelegateBridge", btnBridge)
local widgetFactory = __TS__New(MockWidgetFactory)
local lp = ctx:GetOrCreateLocalPlayer(0)
local uiMgr = lp:RegisterService(
"uiManager",
__TS__New(
UIManager,
ctx,
lp,
resSvc,
widgetFactory,
__TS__New(MockWidgetAdapter)
)
)
local w1 = uiMgr:ShowUI(MiniSettingsWindow)
local w2 = uiMgr:ShowUI(MiniSettingsWindow)
AssertEqual(w1, w2, "concurrent singleton returns same controller")
resBackend:FlushAll()
resBackend:FlushAll()
AssertEqual(widgetFactory.createdCount, 1, "only 1 widget created")
Assert(w1 ~= nil and w1.iconLoaded, "icon loaded")
AssertEqual(w1.txtStatus.text, "Sound: ON")
AssertEqual(
uiMgr:GetDiagnostics().inputMode,
"ui"
)
btnBackend:Click(w1.btnToggle)
AssertEqual(w1.toggleClickCount, 1, "toggle click fired once")
AssertEqual(
settingSvc:GetSoundEnabled(),
false
)
AssertEqual(w1.txtStatus.text, "Sound: OFF")
ctx:Tick(0.12)
AssertEqual(storageBackend.writeCount, 1)
btnBackend:Click(w1.btnClose)
w1:Close()
Assert(
w1:IsDisposed(),
"window disposed"
)
AssertEqual(
btnBackend:GetActiveBindingCount(),
0,
"button bindings back to 0"
)
AssertEqual(
uiMgr:GetDiagnostics().openWindows,
0
)
AssertEqual(
uiMgr:GetDiagnostics().inputMode,
"game",
"focus restored to game"
)
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0,
"resource refs back to 0"
)
local w3 = uiMgr:ShowUI(MiniSettingsWindow)
resBackend:FlushAll()
resBackend:FlushAll()
AssertEqual(w3.txtStatus.text, "Sound: OFF", "reopened window reflects persisted setting")
ctx:Dispose()
AssertEqual(
btnBackend:GetActiveBindingCount(),
0
)
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0
)
end
},
{
name = "Close while loading cancels request and cached window reuse does not duplicate clicks",
fn = function()
local ctx = __TS__New(GameInstanceContext)
local resBackend = __TS__New(MockResourceBackend)
local resSvc = __TS__New(ResourceService, ctx, resBackend)
resBackend.service = resSvc
local btnBackend = __TS__New(MockButtonBackend)
local btnBridge = __TS__New(UIDelegateBridge, ctx, btnBackend)
btnBackend.bridge = btnBridge
ctx:RegisterService("resource", resSvc)
ctx:RegisterService("uiDelegateBridge", btnBridge)
local widgetFactory = __TS__New(MockWidgetFactory)
local lp = ctx:GetOrCreateLocalPlayer(0)
local uiMgr = lp:RegisterService(
"uiManager",
__TS__New(
UIManager,
ctx,
lp,
resSvc,
widgetFactory,
__TS__New(MockWidgetAdapter)
)
)
local loadingWin = uiMgr:ShowUI(CachedTestWindow)
loadingWin:Close()
resBackend:FlushAll()
AssertEqual(widgetFactory.createdCount, 0, "cancelled load never creates widget")
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0
)
local win = uiMgr:ShowUI(CachedTestWindow)
resBackend:FlushAll()
AssertEqual(widgetFactory.createdCount, 1)
btnBackend:Click(win.btn)
AssertEqual(win.clickCount, 1)
win:Close()
AssertEqual(
uiMgr:GetDiagnostics().cachedWindows,
1,
"window moved to cache"
)
AssertEqual(
btnBackend:GetActiveBindingCount(),
0,
"cached window unbound buttons"
)
local reused = uiMgr:ShowUI(CachedTestWindow)
AssertEqual(reused, win, "reused cached controller")
AssertEqual(widgetFactory.createdCount, 1, "no second widget instantiated")
AssertEqual(reused.reuseCount, 1)
btnBackend:Click(reused.btn)
AssertEqual(reused.clickCount, 2, "single click after reuse increments by exactly 1")
uiMgr:CloseAll(true)
AssertEqual(
uiMgr:GetDiagnostics().cachedWindows,
0
)
AssertEqual(
btnBackend:GetActiveBindingCount(),
0
)
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0
)
ctx:Dispose()
end
},
{
name = "UIManager fullscreen auto-hide/restore, non-blocking tips focus preservation, and layer ZOrder priority",
fn = function()
local ctx = __TS__New(GameInstanceContext)
local resBackend = __TS__New(MockResourceBackend)
local resSvc = __TS__New(ResourceService, ctx, resBackend)
resBackend.service = resSvc
local btnBackend = __TS__New(MockButtonBackend)
local btnBridge = __TS__New(UIDelegateBridge, ctx, btnBackend)
btnBackend.bridge = btnBridge
ctx:RegisterService("resource", resSvc)
ctx:RegisterService("uiDelegateBridge", btnBridge)
local widgetFactory = __TS__New(MockWidgetFactory)
local lp = ctx:GetOrCreateLocalPlayer(0)
local uiMgr = lp:RegisterService(
"uiManager",
__TS__New(
UIManager,
ctx,
lp,
resSvc,
widgetFactory,
__TS__New(MockWidgetAdapter)
)
)
local baseWin = uiMgr:ShowUI(CachedTestWindow)
resBackend:FlushAll()
Assert(
baseWin:IsVisible(),
"base window visible"
)
AssertEqual(
uiMgr:GetDiagnostics().inputMode,
"ui"
)
local toast1 = uiMgr:ShowUI(ToastTestWindow)
resBackend:FlushAll()
AssertEqual(
uiMgr:GetDiagnostics().inputMode,
"ui",
"non-blocking tips preserves underlying UI input mode"
)
AssertEqual(
uiMgr:GetDiagnostics().topWindowName,
"ToastTestWindow",
"tips has higher ZOrder than normal"
)
toast1:Close()
local fullWin = uiMgr:ShowUI(FullscreenTestWindow)
resBackend:FlushAll()
Assert(
not baseWin:IsVisible(),
"base window hidden while fullscreen is active"
)
Assert(
baseWin:IsHiddenByFullscreen(),
"base window marked hiddenByFullscreen"
)
local sysWin = uiMgr:ShowUI(SystemTestWindow)
resBackend:FlushAll()
AssertEqual(
uiMgr:GetDiagnostics().topWindowName,
"SystemTestWindow"
)
local closedCount = uiMgr:CloseAllExcept(baseWin, false)
AssertEqual(closedCount, 2, "closed fullscreen and system windows")
Assert(
baseWin:IsVisible(),
"base window restored after fullscreen closed"
)
Assert(
not baseWin:IsHiddenByFullscreen(),
"hiddenByFullscreen cleared"
)
uiMgr:CloseAll(true)
AssertEqual(
uiMgr:GetDiagnostics().openWindows,
0
)
AssertEqual(
btnBackend:GetActiveBindingCount(),
0
)
AssertEqual(
resSvc:GetDiagnostics().nativeTotalRefCount,
0
)
ctx:Dispose()
end
}
}
}
end
function __module.UpdateImports()
GameInstanceContext = M__framework_context_game_context.GameInstanceContext
ASSET_KEYS = M__framework_resource_asset_catalog.ASSET_KEYS
BASE_ASSET_PATHS = M__framework_resource_asset_catalog.BASE_ASSET_PATHS
ResourceService = M__framework_resource_resource_service.ResourceService
LocalSaveManager = M__framework_storage_local_save.LocalSaveManager
MemoryStorageBackend = M__framework_storage_storage_backend.MemoryStorageBackend
RegisterUIConfig = M__framework_ui_ui_config.RegisterUIConfig
UIDelegateBridge = M__framework_ui_ui_delegate_bridge.UIDelegateBridge
UIManager = M__framework_ui_ui_manager.UIManager
UIWindow = M__framework_ui_ui_window.UIWindow
MiniSettingsWindow = M__game_setting_mini_settings_window.MiniSettingsWindow
SettingService = M__game_setting_setting_service.SettingService
Assert = M__tests_harness_test_runner.Assert
AssertEqual = M__tests_harness_test_runner.AssertEqual
end