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

179 lines
12 KiB
Lua

local source = assert(arg[1])
local secret = {}
function issecretvalue(v) return rawequal(v, secret) end
local function immutable(t)
return setmetatable({}, {__index=t, __newindex=function(_,key) error("write on stock object: "..key) end})
end
local combat, readers, dataEvents, timerCount, containerPasses = false, 0, 0, 0, 0
local timers, events, globals, callbacks, owned = {}, {}, {}, {}, {}
function InCombatLockdown() return combat end
C_Timer={After=function(delay,fn) assert(delay==0);timerCount=timerCount+1;timers[#timers+1]=fn end}
function CreateFrame(kind,name)
assert(kind=="Frame" and not name)
return {RegisterEvent=function(_,ev)events[ev]=true end,SetScript=function(_,event,fn) assert(event=="OnEvent");events.handle=fn end}
end
EventRegistry={RegisterCallback=function(_,name,fn,owner)assert(owner);callbacks[name]=fn end}
function hooksecurefunc(key,observer)
assert(type(key)=="string" and not globals[key],"shared table, mixin or duplicate hook")
assert(key=="SetItemButtonTexture" or key=="MerchantFrame_UpdateMerchantInfo" or key=="MerchantFrame_UpdateBuybackInfo")
globals[key]=observer
end
local function drain()
while #timers>0 do local batch=timers;timers={};for _,fn in ipairs(batch) do fn()end end
end
local function event(name,...)
assert(events[name],name);events.handle(nil,name,...)
end
local function noop()end
local function stockWrite()error("attempted to mutate original region",2)end
local function frame(shown) return immutable({IsVisible=function()return shown~=false end}) end
local function button(fields)
fields=fields or {}
local id=fields.id
fields.GetID=fields.GetID or function()return id end
fields.GetName=fields.GetName or function()return fields.name end
fields.IsVisible=fields.IsVisible or function()return fields.shown~=false end
local native=immutable({IsShown=function()return fields.iconShown~=false end,GetDrawLayer=function()return "BORDER",0 end,GetVertexColor=function()return fields.red or 1,1,1,1 end,
IsDesaturated=function()return fields.locked or false end,SetTexture=stockWrite,Hide=stockWrite,Show=stockWrite,SetText=stockWrite,
SetVertexColor=stockWrite,SetDesaturated=stockWrite})
fields.Icon=native
fields.Count=immutable({IsShown=function()return fields.countShown end,GetText=function()return fields.countText end,SetText=stockWrite,Hide=stockWrite,Show=stockWrite})
local stock=immutable(fields)
fields.CreateTexture=function(_,key,layer,template,level)
assert(key==nil and template==nil and layer=="BORDER" and level==1 and not combat)
local rec=owned[stock] or {};owned[stock]=rec;assert(not rec.art)
rec.art={SetAllPoints=function(_,anchor)assert(anchor==native)end,SetTexture=function(self,v)self.value=v end,
GetTexture=function(self)return self.value end,Show=function(self)self.shown=true end,Hide=function(self)self.shown=false end,
SetVertexColor=function(self,r) self.red=r end,SetDesaturated=function(self,v)self.locked=v end}
return rec.art
end
fields.CreateFontString=function(_,key,layer,font)
assert(not combat and not key and layer=="OVERLAY" and font=="NumberFontNormal")
local rec=assert(owned[stock]);assert(not rec.count)
rec.count={SetPoint=noop,SetText=function(self,v)self.text=v end,Show=function(self)self.shown=true end,Hide=function(self)self.shown=false end}
return rec.count
end
return stock,fields
end
local function icon(b)
local rec=owned[b]
return rec and rec.art.shown and rec.art.value or nil
end
local function same(b,v) assert(icon(b)==v,"unexpected overlay") end
GWSourceItemIconData={version=1,items={[118]="potion.blp",[119]="other.blp",[120]="missing.blp"}}
local files={["potion.blp"]=-608,["other.blp"]=-609}
function GetFileIDFromPath(path) readers=readers+1;return files[path] end
local bag,slot=0,1
local bagItem,bagFields=button({GetBagID=function()return bag end,GetID=function()return slot end})
local getBag={itemID=118}
C_Container={GetContainerItemInfo=function(id,index)
assert(id==0 and index==1);containerPasses=containerPasses+1;return getBag
end}
local function lazyContainerRead()error("container helper may initialize tainted stock state")end
local bagFrame=immutable({IsVisible=function()return true end,
EnumerateValidItems=lazyContainerRead,GetBagSize=lazyContainerRead})
local bagItems={bagItem}
-- Native frames own Items directly; a proxy-only field is invisible to rawget.
rawset(bagFrame,"Items",bagItems)
ContainerFrameCombinedBags=bagFrame
local splitBagItem=button({GetBagID=function()return bag end,GetID=function()return slot end})
local splitFrame=immutable({IsVisible=function()return true end,
EnumerateValidItems=lazyContainerRead,GetBagSize=lazyContainerRead})
local splitItems={splitBagItem}
rawset(splitFrame,"Items",splitItems)
local containerFrames={[3]=splitFrame} -- native pools can have empty indices
ContainerFrameContainer=immutable({})
rawset(ContainerFrameContainer,"ContainerFrames",containerFrames)
ContainerFrameUtil_EnumerateContainerFrames=lazyContainerRead
local bagbar=button({IsBackpack=function()return false end})
CharacterBag0Slot=bagbar
local backpack=button({IsBackpack=function()return true end,GetItemID=function()error("backpack identity")end})
local gear=button({id=1}); CharacterHeadSlot=gear
local ammo,ammoFields=button({id=0,iconShown=false}); CharacterAmmoSlot=ammo
local extra=button({name="Other",GetItemID=function()return 119 end})
local currentAmmo, ammoLocation, ammoQty=118,"INVTYPE_AMMO",179
function GetInventoryItemID(_,id)if id==1 or id==20 then return 118 end end
C_TooltipInfo={GetInventoryItem=function()return {type=0,id=currentAmmo}end}
C_Item={GetItemInfoInstant=function(id)return id,"Projectile","Bullet",ammoLocation,0,6,3 end,GetItemCount=function()return ammoQty end}
local kind,item="item",118
ActionButton1=button({action=1});ActionButton2=button({action=2})
function GetActionInfo(id)dataEvents=dataEvents+1;if id==1 then return kind,item end;return "spell",118 end
local merchantPage,tab,buyItem,buyReads=2,1,118,0
MERCHANT_ITEMS_PER_PAGE,BUYBACK_ITEMS_PER_PAGE=2,2
local merchantTable={IsVisible=function()return true end,page=2,selectedTab=1}
MerchantFrame=immutable(merchantTable)
MerchantItem1ItemButton=button()
function GetMerchantItemID(id)assert(id==(merchantPage-1)*2+1);return buyItem end
function GetBuybackItemLink(id)buyReads=buyReads+1;assert(id==1);return "item:119" end
function SetItemButtonTexture()return "original",nil,17 end
function MerchantFrame_UpdateMerchantInfo()end
function MerchantFrame_UpdateBuybackInfo()end
local genericOriginal=SetItemButtonTexture
local bankButton,bankFields=button({itemInfo={itemID=119,stackCount=3}})
local bankPanel=immutable({IsVisible=function()return true end,EnumerateValidItems=function()
local sent=false;return function() if not sent then sent=true;return bankButton end end
end})
BankFrame=immutable({BankPanel=bankPanel})
local output,outputItem=button(),118
local txn=immutable({GetRecipeID=function()return 55 end,CreateCraftingReagentInfoTbl=function()return {} end,GetAllocationItemGUID=function()end})
local form=immutable({OutputIcon=output,IsVisible=function()return true end,transaction=txn})
ProfessionsFrame=immutable({IsVisible=function()return true end,CraftingPage=immutable({SchematicForm=form})})
C_TradeSkillUI={GetRecipeOutputItemData=function(id)assert(id==55);return {hyperlink=outputItem and "item:"..outputItem}end}
local function identity(v)return v end
ItemButtonMixin=immutable({SetItemButtonTexture=identity})
BankPanelItemButtonMixin=immutable({Refresh=identity})
Professions=immutable({SetupOutputIconCommon=identity})
dofile(source)
drain()
local scheduledBeforeAddonLoad=timerCount
event("ADDON_LOADED","Blizzard_Bags")
assert(timerCount==scheduledBeforeAddonLoad and #timers==0,"ADDON_LOADED must not trigger a full UI refresh")
event("PLAYER_LOGIN")
assert(timerCount==scheduledBeforeAddonLoad+1,"PLAYER_LOGIN should schedule the initial refresh")
drain()
for _,b in ipairs({gear,ammo,bagbar,bagItem,splitBagItem,ActionButton1,MerchantItem1ItemButton,output})do same(b,-608)end
same(bankButton,-609);same(ActionButton2,nil);assert(owned[ammo].count.text==179)
local initialContainerPasses=containerPasses
for _,addon in ipairs({"Blizzard_One","Blizzard_Two","ExampleAddon"}) do event("ADDON_LOADED",addon);drain() end
assert(containerPasses==initialContainerPasses,"ADDON_LOADED must not rescan every item surface")
event("PLAYER_REGEN_ENABLED");drain()
assert(containerPasses==initialContainerPasses,"idle PLAYER_REGEN_ENABLED must not rescan every item surface")
ammoFields.countShown,ammoFields.countText=true,"179";event("PLAYER_EQUIPMENT_CHANGED",0);drain();assert(not owned[ammo].count.shown)
ammoFields.countShown=false;event("PLAYER_EQUIPMENT_CHANGED",0);drain();assert(owned[ammo].count.shown)
assert(ItemButtonMixin.SetItemButtonTexture==identity and BankPanelItemButtonMixin.Refresh==identity and Professions.SetupOutputIconCommon==identity)
assert(SetItemButtonTexture==genericOriginal and readers==2)
local a,b,c=SetItemButtonTexture();assert(a=="original" and b==nil and c==17)
globals.SetItemButtonTexture(backpack);globals.SetItemButtonTexture(extra);drain();same(backpack,nil);same(extra,-609)
kind="spell";event("ACTIONBAR_SLOT_CHANGED",1);drain();same(ActionButton1,nil)
kind,item="item",secret;event("ACTIONBAR_PAGE_CHANGED");drain();same(ActionButton1,nil)
kind,item="item",119;event("ACTIONBAR_PAGE_CHANGED");drain();same(ActionButton1,-609)
getBag={itemID=secret};event("BAG_UPDATE_DELAYED");drain();same(bagItem,nil)
getBag=nil;event("BAG_UPDATE_DELAYED");drain();same(bagItem,nil)
getBag={itemID=120};event("BAG_UPDATE_DELAYED");drain();same(bagItem,nil)
getBag={itemID=119};bagFields.locked=true;event("ITEM_LOCK_CHANGED");drain();same(bagItem,-609);assert(owned[bagItem].art.locked)
bagFields.red=secret;event("ITEM_LOCK_CHANGED");drain();same(bagItem,nil);bagFields.red=nil
bagFields.iconShown=false;event("BAG_UPDATE_DELAYED");drain();same(bagItem,nil);bagFields.iconShown=true
slot=secret;event("BAG_UPDATE_DELAYED");drain();same(bagItem,nil);slot=1
merchantPage=1;merchantTable.page=1;buyItem=119;globals.MerchantFrame_UpdateMerchantInfo();drain();same(MerchantItem1ItemButton,-609)
merchantTable.selectedTab=2;globals.MerchantFrame_UpdateBuybackInfo();drain();assert(buyReads==1)
bankFields.itemInfo={itemID=secret};callbacks["BankPanelMixin.PageSelected"]();drain();same(bankButton,nil)
outputItem=119;callbacks["ProfessionsRecipeListMixin.Event.OnRecipeSelected"]();drain();same(output,-609)
outputItem=nil;callbacks["ProfessionsRecipeListMixin.Event.OnRecipeSelected"]();drain();same(output,nil)
currentAmmo=secret;event("PLAYER_EQUIPMENT_CHANGED",0);drain();same(ammo,nil);assert(not owned[ammo].count.shown)
currentAmmo=118;ammoLocation="INVTYPE_RANGEDRIGHT";event("PLAYER_EQUIPMENT_CHANGED",0);drain();same(ammo,nil)
ammoLocation="INVTYPE_AMMO";ammoQty=0;event("PLAYER_EQUIPMENT_CHANGED",0);drain();same(ammo,-608);assert(not owned[ammo].count.shown)
local later=button({id=1});CharacterHeadSlot=later;combat=true
event("PLAYER_EQUIPMENT_CHANGED",1);drain();assert(not owned[later])
local lateGeneric=button({GetItemID=function()return 118 end})
globals.SetItemButtonTexture(lateGeneric);drain();assert(not owned[lateGeneric])
combat=false;event("PLAYER_REGEN_ENABLED");drain();same(later,-608);same(lateGeneric,-608)
local steps,timer=dataEvents,timerCount
for id=20000,20500 do event("GET_ITEM_INFO_RECEIVED",id,true) end
for n=1,12 do event("BAG_UPDATE_DELAYED");event("GET_ITEM_INFO_RECEIVED",118,true)end
assert(timerCount==timer+1);drain();assert(dataEvents==steps)
assert(readers==3 and GWSourceItemIcons.samples[118].displayed==-608 and GWSourceItemIcons.TextureForItem(secret)==nil)
assert(rawget(bagFrame,"Items")==bagItems and bagItems[1]==bagItem)
assert(rawget(splitFrame,"Items")==splitItems and splitItems[1]==splitBagItem)
assert(rawget(ContainerFrameContainer,"ContainerFrames")==containerFrames and containerFrames[3]==splitFrame)
print("PASS addon-owned anonymous art and ammo count only; immutable stock methods, regions and data; bag, bank, gear, action/spell, ammo, merchant, profession, generic item, secrets, reuse, combat and event coalescing")