wow-addons/tests/test_sr.lua

356 lines
15 KiB
Lua
Raw Permalink Normal View History

-- Runs ShipRoutes against a mock of the WoW map API.
-- arg[1] = addon folder (ShipRoutes)
local dir = arg[1] or "ShipRoutes"
local failures = 0
local function check(cond, msg)
if not cond then
failures = failures + 1
io.write("FAIL: ", msg, "\n")
end
end
-- Map rects from the client's UiMapAssignment.db2. top/bottom are world X, left/right world Y.
local RECTS = {
[1415] = { inst = 0, top = 7467, bottom = -16000, left = 16000, right = -19200, type = 2 },
[1414] = { inst = 1, top = 12800, bottom = -11733, left = 17067, right = -19733, type = 2 },
[1437] = { inst = 0, top = -2148, bottom = -4904, left = -390, right = -4525, type = 3 }, -- Wetlands
[1431] = { inst = 0, top = -9716, bottom = -11516, left = 833, right = -1866, type = 3 }, -- "Duskwood"
[2521] = { inst = 2991, top = 4956, bottom = 1248, left = 4231, right = -1331, type = 3 },
}
-- how each continent's world area sits on the Azeroth map (947): u0, u1, v0, v1, xmin, xmax, ymin, ymax
local ON_947 = {
[0] = { 0.5505, 0.8966, 0.0994, 0.8691, -16000, 6933, -7467, 8000 },
[1] = { 0.0399, 0.4083, 0.0855, 0.9234, -12800, 12267, -9600, 6933 },
}
local function to947(inst, x, y)
local a = ON_947[inst]
return a[1] + (a[8] - y) / (a[8] - a[7]) * (a[2] - a[1]), a[3] + (a[6] - x) / (a[6] - a[5]) * (a[4] - a[3])
end
function InCombatLockdown() return false end
Enum = { UIMapType = { Cosmic = 0, World = 1, Continent = 2, Zone = 3 } }
function CreateVector2D(x, y)
return { x = x, y = y, GetXY = function(self) return self.x, self.y end }
end
C_Map = {
GetMapInfo = function(id)
if id == 947 then return { mapID = 947, mapType = 1 } end
local r = RECTS[id]
return r and { mapID = id, mapType = r.type }
end,
GetWorldPosFromMapPos = function(id, v)
local r = RECTS[id]
if not r then return nil end
return r.inst, CreateVector2D(r.top + (r.bottom - r.top) * v.y, r.left + (r.right - r.left) * v.x)
end,
GetMapRectOnMap = function(id, top)
local r = RECTS[id]
if top ~= 947 or not r or not ON_947[r.inst] then return nil end
local u1, v1 = to947(r.inst, r.top, r.left)
local u2, v2 = to947(r.inst, r.bottom, r.right)
return u1, u2, v1, v2
end,
GetMapInfoAtPosition = function(id, u, v)
return { mapID = id * 10 }
end,
}
function CreateFromMixins(...)
local t = {}
for i = 1, select("#", ...) do
for k, v in pairs((select(i, ...))) do t[k] = v end
end
return t
end
MapCanvasDataProviderMixin = {
OnAdded = function(self, map) self.owningMap = map end,
GetMap = function(self) return self.owningMap end,
}
MapCanvasPinMixin = {
UseFrameLevelType = function() end,
SetScalingLimits = function() end,
CheckMouseButtonPassthrough = function(self, b) self.passthrough = b end,
GetMap = function(self) return self.map end,
}
local function region()
local r = { shown = true }
function r:Show() self.shown = true end
function r:Hide() self.shown = false end
function r:SetThickness(t) self.thickness = t end
function r:SetColorTexture(cr, cg, cb, ca) self.color = { cr, cg, cb, ca } end
function r:SetStartPoint(_, _, x, y) self.x1, self.y1 = x, y end
function r:SetEndPoint(_, _, x, y) self.x2, self.y2 = x, y end
function r:SetVertexColor(cr, cg, cb, ca) self.vertex = { cr, cg, cb, ca } end
function r:SetTexture(p) self.texture = p end
function r:SetRotation(a) self.rotation = a end
return r
end
local eventFrame
local allLines = {}
function CreateFrame(kind, _, parent)
local f = { scripts = {} }
function f:RegisterEvent() end
function f:SetScript(name, fn) self.scripts[name] = fn end
function f:SetAllPoints() end
function f:SetFrameLevel(l) self.level = l end
function f:CreateLine(_, layer)
local l = region()
l.layer = layer
allLines[#allLines + 1] = l
return l
end
if not parent then eventFrame = f end
return f
end
local canvas = { GetWidth = function() return 1000 end, GetHeight = function() return 700 end }
local pins = {}
local mapID = 1415
local shownMap
WorldMapFrame = {
AddDataProvider = function(self, p) p:OnAdded(self); self.provider = p end,
GetCanvas = function() return canvas end,
GetMapID = function() return mapID end,
GetCanvasScale = function() return 2 end,
GetPinFrameLevelsManager = function()
return { GetValidFrameLevel = function() return 100 end }
end,
RemoveAllPinsByTemplate = function() pins = {} end,
SetMapID = function(_, id) shownMap = id end,
AcquirePin = function(self, template, ...)
assert(template == "ShipRoutesPinTemplate")
local pin = CreateFromMixins(ShipRoutesPinMixin)
pin.map = self
pin.Disc, pin.Ring, pin.Glyph = region(), region(), region()
local label = region()
function label:GetFont() return "font", 10 end
function label:SetFont(_, _, flags) self.flags = flags end
function label:ClearAllPoints() self.point = nil end
function label:SetPoint(p, _, rp) self.point = { p, rp } end
function label:SetJustifyH(j) self.justify = j end
function label:SetText(t) self.text = t end
pin.Label = label
function pin:SetSize(w) self.size = w end
function pin:SetPosition(u, v) self.u, self.v = u, v end
pin:OnLoad()
pin:OnAcquired(...)
pins[#pins + 1] = pin
return pin
end,
}
local tipLines = {}
GameTooltip = {
SetOwner = function() tipLines = {} end,
SetText = function(_, t) tipLines[#tipLines + 1] = t end,
AddLine = function(_, t) tipLines[#tipLines + 1] = t end,
Show = function() end,
Hide = function() end,
}
local faction = "Alliance"
function UnitFactionGroup() return faction end
local realPrint = print
local printed
function print(msg) printed = msg end
SlashCmdList = {}
local ns = {}
assert(loadfile(dir .. "/Data.lua"))("ShipRoutes", ns)
assert(loadfile(dir .. "/ShipRoutes.lua"))("ShipRoutes", ns)
eventFrame.scripts.OnEvent(eventFrame, "ADDON_LOADED", "ShipRoutes")
local provider = WorldMapFrame.provider
check(provider ~= nil, "provider attached to the world map")
local function find(kind, name)
for _, p in ipairs(pins) do
if p.kind == kind and kind == "dock" and p.route.stops[p.index][1] == name then
return p
end
if p.kind == kind and kind == "exit" then
for _, e in ipairs(p.exits) do
if e.route.stops[e.far][1] == name then return p end
end
end
end
end
local function count(kind)
local n = 0
for _, p in ipairs(pins) do if p.kind == kind then n = n + 1 end end
return n
end
local function visibleLines()
local n = 0
for _, l in ipairs(allLines) do if l.shown then n = n + 1 end end
return n
end
-- the data itself
check(#ns.routes == 12, "twelve routes, got " .. #ns.routes)
for _, r in ipairs(ns.routes) do
check(#r.stops >= 2, "route " .. r.path .. " has two or more stops")
check(#r.legs == (#r.stops == 2 and 1 or #r.stops), "route " .. r.path .. " has one leg per stop pair")
for _, leg in ipairs(r.legs) do
check(#leg >= 6 and #leg % 3 == 0, "route " .. r.path .. " leg has point triples")
check(leg[1] == r.stops[leg.from][2] and leg[2] == math.floor(r.stops[leg.from][3] + 0.5) or not leg.smooth,
"route " .. r.path .. " lane starts at its dock")
end
end
-- projection: EK map corner and centre, a world map point, and the world map itself
local u, v = ns.Project(1415, 0, 7467, 16000)
check(math.abs(u) < 1e-9 and math.abs(v) < 1e-9, "top left projects to 0,0")
u, v = ns.Project(1415, 0, (7467 - 16000) / 2, (16000 - 19200) / 2)
check(math.abs(u - 0.5) < 1e-9 and math.abs(v - 0.5) < 1e-9, "centre projects to 0.5,0.5")
local wu, wv = to947(0, -14278, 583)
u, v = ns.Project(947, 0, -14278, 583)
check(math.abs(u - wu) < 1e-9 and math.abs(v - wv) < 1e-9, "Booty Bay lands where the client puts it on the world map")
u, v = ns.Project(1415, -1, wu, wv)
local bu, bv = ns.Project(1415, 0, -14278, 583)
check(math.abs(u - bu) < 1e-9 and math.abs(v - bv) < 1e-9, "a world map point maps back onto the continent")
u = ns.Project(1415, 1, -4016, -4741)
check(u and u < 0, "Theramore lies off the west edge of the EK map")
check(ns.Project(947, 2991, 1848, 569) == nil, "Zephras Isle has no place on the world map")
-- Eastern Kingdoms
mapID = 1415
provider:RefreshAllData()
check(find("dock", "Stormwind Harbor") ~= nil, "Stormwind Harbor dock on EK")
check(find("dock", "Southshore") ~= nil, "Southshore dock on EK")
check(find("dock", "Riverglades") ~= nil, "Riverglades dock on EK")
check(find("dock", "Theramore Isle") == nil, "no Kalimdor docks on EK")
local toTheramore = find("exit", "Theramore Isle")
check(toTheramore ~= nil, "edge marker toward Theramore on EK")
check(toTheramore and toTheramore.Glyph.rotation ~= nil, "edge arrow is rotated")
check(toTheramore and toTheramore.u <= 0.03, "Theramore marker sits on the west edge")
check(toTheramore and toTheramore.Label.text:find("Theramore Isle", 1, true), "edge marker is labelled")
check(toTheramore and toTheramore.Label.point[1] == "LEFT", "west edge label reads inward")
check(find("exit", "Zephras Isle") == nil, "no edge marker toward Zephras Isle")
check(find("exit", "Grom'gol") == nil, "no edge marker for a route that stays on the map")
check(visibleLines() > 50, "route lines drawn on EK, got " .. visibleLines())
local resting = true
for _, l in ipairs(allLines) do
if l.shown and l.layer == "ARTWORK" and l.color[4] >= 0.5 then resting = false end
end
check(resting, "routes rest faded")
check(provider.overlay.level == 100, "overlay frame level set")
for _, l in ipairs(allLines) do
if l.shown then
check(math.abs(l.thickness - l.px / 2) < 1e-9, "line thickness divides by canvas scale")
break
end
end
for _, p in ipairs(pins) do
check(p.u >= 0 and p.u <= 1 and p.v >= 0 and p.v <= 1, "pins sit on the map")
end
-- tooltip at Menethil's Southshore pier
local menethil
for _, p in ipairs(pins) do
if p.kind == "dock" and p.route.path == 11167 and p.route.stops[p.index][1] == "Menethil Harbor" then menethil = p end
end
check(menethil ~= nil, "Menethil pier of the Southshore boat")
menethil:OnMouseEnter()
local tip = table.concat(tipLines, "\n")
check(tip:find("Boat to Southshore, then Auberdine", 1, true), "tooltip lists next stops in order:\n" .. tip)
check(tip:find("Also from here:", 1, true) and tip:find("Boat to Theramore Isle", 1, true), "tooltip lists the other pier:\n" .. tip)
check(provider.highlight and provider.highlight[menethil.route], "hover highlights the route")
local dim, bright = 0, 0
for _, l in ipairs(allLines) do
if l.shown and l.layer == "ARTWORK" then
if l.color[4] < 0.5 then dim = dim + 1 else bright = bright + 1 end
end
end
check(dim > 0 and bright > 0, "hovered route stands out from the faded rest")
local full = false
for _, l in ipairs(allLines) do
if l.shown and l.layer == "ARTWORK" and l.color[4] == 1 then full = true end
end
check(full, "hovered route is fully opaque")
menethil:OnMouseLeave()
check(provider.highlight == nil, "leaving clears the highlight")
-- click opens the next stop's zone
menethil:OnMouseClickAction("LeftButton")
check(shownMap == 14150, "click shows Southshore's zone, got " .. tostring(shownMap))
shownMap = nil
menethil:OnMouseClickAction("RightButton")
check(shownMap == nil, "right click does nothing")
check(menethil.passthrough == "RightButton", "right click passes through to the map")
-- Kalimdor: the Southshore and Menethil lanes leave the east edge together, Stormwind's apart
mapID = 1414
provider:RefreshAllData()
local lane = find("exit", "Southshore")
check(lane ~= nil, "edge marker toward Southshore on Kalimdor")
local onLane = {}
for _, e in ipairs(lane and lane.exits or {}) do onLane[e.route.stops[e.far][1]] = true end
check(onLane["Menethil Harbor"], "Southshore and Menethil lanes share one marker")
check(find("exit", "Stormwind Harbor") ~= nil and find("exit", "Stormwind Harbor") ~= lane, "Stormwind lane has its own marker")
check(lane and lane.u >= 0.97, "Auberdine lane marker on the east edge")
check(lane and lane.Label.point[1] == "RIGHT", "east edge label reads inward")
lane:OnMouseEnter()
tip = table.concat(tipLines, "\n")
check(tip:find("Boat from Southshore", 1, true) and tip:find("Boat to Menethil Harbor", 1, true),
"edge tooltip names each lane:\n" .. tip)
local lit = 0
for r in pairs(provider.highlight or {}) do lit = lit + 1 end
check(lit == 2, "hovering the lane lights both routes on it, got " .. lit)
lane:OnMouseLeave()
lane:OnMouseClickAction("LeftButton")
check(shownMap ~= nil, "clicking an edge marker opens a map")
-- world map: everything on it, nothing to point off-map
mapID = 947
provider:RefreshAllData()
check(find("dock", "Theramore Isle") ~= nil and find("dock", "Menethil Harbor") ~= nil, "both continents on the world map")
check(count("exit") == 0, "no edge markers on the world map, got " .. count("exit"))
check(find("dock", "Zephras Isle") == nil, "Zephras Isle not on the world map")
-- zone map: docks outside it are left out, its lanes are labelled at the edge
mapID = 1437
provider:RefreshAllData()
check(find("dock", "Menethil Harbor") ~= nil, "Menethil on its zone map")
check(find("dock", "Stormwind Harbor") == nil, "Stormwind not on the Wetlands map")
local menethilPiers = 0
for _, p in ipairs(pins) do
if p.kind == "dock" and p.route.stops[p.index][1] == "Menethil Harbor" then menethilPiers = menethilPiers + 1 end
end
check(menethilPiers == 2, "both Menethil piers on Wetlands, got " .. menethilPiers)
check(count("exit") == 0, "no edge marker on top of Menethil's piers, which sit by the map edge")
check(visibleLines() > 0, "Wetlands draws the lanes from its docks")
-- a zone with no docks shows no lanes, even the ones flying over it
mapID = 1431
provider:RefreshAllData()
check(count("dock") == 0 and count("exit") == 0, "no markers on a zone without docks")
check(visibleLines() == 0, "no lanes over a zone without docks, got " .. visibleLines())
-- faction filter
faction = "Horde"
mapID = 1415
SlashCmdList.SHIPROUTES("mine")
check(find("dock", "Stormwind Harbor") == nil, "Alliance docks hidden for Horde with /routes mine")
check(find("dock", "Grom'gol") ~= nil, "Horde docks kept")
check(find("dock", "Booty Bay") ~= nil, "neutral docks kept")
check(find("dock", "Dalaran") ~= nil, "unknown-faction docks kept")
SlashCmdList.SHIPROUTES("all")
check(find("dock", "Stormwind Harbor") ~= nil, "/routes all shows Alliance docks again")
-- toggle
SlashCmdList.SHIPROUTES("")
check(#pins == 0 and visibleLines() == 0, "/routes hides everything")
check(ShipRoutesDB.hidden == true, "hidden is saved")
SlashCmdList.SHIPROUTES("")
check(#pins > 0, "/routes shows it again")
if failures > 0 then
realPrint(failures .. " failure(s)")
os.exit(1)
end
realPrint("ok")