Files
WoW-Hybrid-gateway/tests/addon_text_size_test.lua

85 lines
4.7 KiB
Lua

local unpack = table.unpack or unpack
local scale, combat = 1, false
local created = {}
local nativeFont = {GetName=function() return "UserScaledTestFont" end}
function GetFontInfo(name) return {canBeUserScaled=name=="UserScaledTestFont"} end
function GetCVar() return scale end
function InCombatLockdown() return combat end
function hooksecurefunc(object, method, callback)
local original = object[method]
object[method] = function(...)
local values = {original(...)}
callback(...)
return unpack(values)
end
end
local function font(kind)
local object = {kind=kind or "FontString", file="test.ttf", size=12, flags="", writes=0, html={}}
function object:GetObjectType() return self.kind end
function object:GetFont(element)
if self.kind=="SimpleHTML" then local v=self.html[(element or "p"):lower()];if v then return v[1],v[2],v[3] end;return end
return self.file,self.size,self.flags
end
function object:SetFont(first, second, third, fourth)
self.writes=self.writes+1
if self.kind=="SimpleHTML" then self.html[first:lower()]={second,third,fourth};return true end
self.file,self.size,self.flags=first,second,third;return true
end
function object:GetFontObject() return self.fontObject end
function object:SetFontObject(value)
self.fontObject=value;self.size=value==nativeFont and 12*scale or 12
end
function object:SetFontHeight(value) self.size=value end
return object
end
function CreateFrame(kind, name, parent)
local f=font(kind);f.regions={};f.children={};f.events={}
function f:GetRegions() return unpack(self.regions) end
function f:GetChildren() return unpack(self.children) end
function f:CreateFontString(_, _, template)
local value=font();if template=="UserScaledTestFont" then value:SetFontObject(nativeFont) end
self.regions[#self.regions+1]=value;return value
end
function f:RegisterEvent(name) self.events[name]=true end
function f:SetScript(_, callback) self.callback=callback end
if parent then parent.children[#parent.children+1]=f end
if name then _G[name]=f end
created[#created+1]=f;return f
end
local originalCreateFrame=CreateFrame
TextSizeManager={}
function TextSizeManager:GetScale() return math.max(.8,scale) end
function TextSizeManager:RegisterObject(object) self.observer=object;object:OnTextScaleUpdated(self:GetScale()) end
local function update(value) scale=value;TextSizeManager.observer:OnTextScaleUpdated(TextSizeManager:GetScale()) end
local function near(a,b) assert(math.abs(a-b)<.00001,tostring(a).." != "..tostring(b)) end
local untouched=CreateFrame("Frame","BlizzardUntouched"):CreateFontString()
dofile(arg[1]);local events=created[#created]
assert(CreateFrame==originalCreateFrame)
local f=OctoTextSize.CreateFrame("Frame")
local label=f:CreateFontString();label:SetFont("test.ttf",10,"OUTLINE")
update(1.4);near(label.size,14);assert(label.flags=="OUTLINE")
update(1.4);near(label.size,14)
local file, logical, flags=label:GetFont();near(logical,10)
label:SetFont(file,logical,flags);near(label.size,14)
local copy=f:CreateFontString();copy:SetFont(label:GetFont());near(copy.size,14)
label:SetFont("other.ttf",20);near(label.size,28);assert(label.flags=="")
update(.7);near(label.size,16)
update(1.4);label:SetFontHeight(15);near(label.size,21)
local late=f:CreateFontString();late:SetFont("late.ttf",9,"");near(late.size,12.6)
local inherited=f:CreateFontString(nil,nil,"UserScaledTestFont")
near(inherited.size,16.8);assert(inherited.writes==0)
update(1.2);near(inherited.size,16.8);assert(inherited.writes==0) -- native manager owns its actual update
inherited:SetFont("explicit.ttf",10,"");near(inherited.size,12)
local edit=OctoTextSize.CreateFrame("EditBox");edit:SetFont("edit.ttf",11,"");near(edit.size,13.2)
local html=OctoTextSize.CreateFrame("SimpleHTML");html:SetFont("P","html.ttf",10);html:SetFont("H1","html.ttf",20,"OUTLINE")
near(html.html.p[2],12);near(html.html.h1[2],24);assert(html.html.p[3]=="")
local factoryCalls=0
local private=OctoTextSize.WrapFactory(function(...) factoryCalls=factoryCalls+1;local x=originalCreateFrame(...);x.compat=true;return x end)
local privateFrame=private("Frame");assert(privateFrame.compat and factoryCalls==1)
local child=privateFrame:CreateFontString();child:SetFont("child.ttf",10,"");near(child.size,12)
combat=true;update(1.5);near(label.size,18)
combat=false;events.callback(events,"PLAYER_REGEN_ENABLED");near(label.size,22.5);near(html.html.p[2],15)
local writes=label.writes;events.callback(events,"CVAR_UPDATE","unrelated");assert(label.writes==writes)
assert(untouched.writes==0 and CreateFrame==originalCreateFrame)
print("PASS addon text size: live updates, no compounding, late fonts, native inheritance, HTML, private factories, combat defer, Blizzard untouched")