From d73ba86f73c9f794057393a0cafdc020d6a4a16f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Capello?= Date: Thu, 20 May 2021 16:37:33 -0300 Subject: [PATCH] Add pixel perfect tests --- scripts/pixel_perfect.lua | 361 ++++++++++++++++++++++++++++++++++++++ scripts/test_utils.lua | 10 ++ 2 files changed, 371 insertions(+) create mode 100644 scripts/pixel_perfect.lua diff --git a/scripts/pixel_perfect.lua b/scripts/pixel_perfect.lua new file mode 100644 index 000000000..391dd8644 --- /dev/null +++ b/scripts/pixel_perfect.lua @@ -0,0 +1,361 @@ +-- Copyright (C) 2019-2021 Igara Studio S.A. +-- +-- This file is released under the terms of the MIT license. +-- Read LICENSE.txt for more information. + +dofile('./test_utils.lua') + +local spr = Sprite(6, 6) +local cel = spr.cels[1] + +-- Point size 1px, solid color, no symmetry, no tiled mode +do + local title = '1px, solid, no symmetry, no tiled' + local red = Color{ r=255, g=0, b=0 } + local r = red.rgbaPixel + local pixel=Brush{ size=1, type=BrushType.CIRCLE } + local testData = { + { + id='1 - ' .. title .. ': right then down', + points={ Point(2, 2), Point(3, 2), Point(3, 3) }, + expected={ r, 0, + 0, r } + }, + { + id='2 - ' .. title .. ': down then right', + points={ Point(2, 2), Point(2, 3), Point(3, 3) }, + expected={ r, 0, + 0, r } + }, + { + id='3 - ' .. title .. ': left then up', + points={ Point(2, 2), Point(1, 2), Point(1, 1) }, + expected={ r, 0, + 0, r } + }, + { + id='4 - ' .. title .. ': up then left', + points={ Point(2, 2), Point(2, 1), Point(1, 1) }, + expected={ r, 0, + 0, r } + } + } + + for i,v in ipairs(testData) do + app.useTool{ + tool='pencil', + freehandAlgorithm=1, + brush=pixel, + color=red, + points=v.points} + expect_img_msg(cel.image, v.expected, '\nTest \'' .. v.id .. '\' failed') + cel.image:clear(0) + end +end + +-- Point size 2px, translucent color, no symmetry, no tiled mode +do + local title = '2px, translucent, no symmetry, no tiled' + local red = Color{ r=255, g=0, b=0, a=127 } + local r = red.rgbaPixel + local square=Brush{ size=2, type=BrushType.SQUARE } + local testData = { + { + id='1 - ' .. title .. ': right then down', + points={ Point(2, 2), Point(3, 2), Point(3, 3) }, + expected={ r, r, 0, + r, r, r, + 0, r, r } + }, + { + id='2 - ' .. title .. ': down then right', + points={ Point(2, 2), Point(2, 3), Point(3, 3) }, + expected={ r, r, 0, + r, r, r, + 0, r, r } + }, + { + id='3 - ' .. title .. ': left then up', + points={ Point(2, 2), Point(1, 2), Point(1, 1) }, + expected={ r, r, 0, + r, r, r, + 0, r, r } + }, + { + id='4 - ' .. title .. ': up then left', + points={ Point(2, 2), Point(2, 1), Point(1, 1) }, + expected={ r, r, 0, + r, r, r, + 0, r, r } + } + } + + for i,v in ipairs(testData) do + app.useTool{ + tool='pencil', + freehandAlgorithm=1, + brush=square, + color=red, + points=v.points} + expect_img_msg(cel.image, v.expected, '\nTest \'' .. v.id .. '\' failed') + cel.image:clear(0) + end +end + +-- Point size 2px, translucent color, symmetry, no tiled mode +do + local pref = app.preferences + local docPref = pref.document(spr) + pref.symmetry_mode.enabled = true + docPref.symmetry.mode = 3 + docPref.symmetry.x_axis = 3 + docPref.symmetry.y_axis = 3 + + local title = '2px, translucent, symmetry on, no tiled' + local red = Color{ r=255, g=0, b=0, a=127 } + local r = red.rgbaPixel + local square=Brush{ size=2, type=BrushType.SQUARE } + local testData = { + { + id='1 - ' .. title .. ': right then down', + points={ Point(1, 1), Point(2, 1), Point(2, 2) }, + expected={ r, r, 0, 0, r, r, + r, r, r, r, r, r, + 0, r, r, r, r, 0, + 0, r, r, r, r, 0, + r, r, r, r, r, r, + r, r, 0, 0, r, r } + }, + { + id='2 - ' .. title .. ': down then right', + points={ Point(1, 1), Point(1, 2), Point(2, 2) }, + expected={ r, r, 0, 0, r, r, + r, r, r, r, r, r, + 0, r, r, r, r, 0, + 0, r, r, r, r, 0, + r, r, r, r, r, r, + r, r, 0, 0, r, r } + }, + { + id='3 - ' .. title .. ': left then up', + points={ Point(2, 2), Point(1, 2), Point(1, 1) }, + expected={ r, r, 0, 0, r, r, + r, r, r, r, r, r, + 0, r, r, r, r, 0, + 0, r, r, r, r, 0, + r, r, r, r, r, r, + r, r, 0, 0, r, r } + }, + { + id='4 - ' .. title .. ': up then left', + points={ Point(2, 2), Point(2, 1), Point(1, 1) }, + expected={ r, r, 0, 0, r, r, + r, r, r, r, r, r, + 0, r, r, r, r, 0, + 0, r, r, r, r, 0, + r, r, r, r, r, r, + r, r, 0, 0, r, r } + } + } + + for i,v in ipairs(testData) do + app.useTool{ + tool='pencil', + freehandAlgorithm=1, + brush=square, + color=red, + points=v.points} + expect_img_msg(cel.image, v.expected, '\nTest \'' .. v.id .. '\' failed') + cel.image:clear(0) + end +end + +-- Point size 2px, translucent color, no symmetry, tiled mode on +do + local pref = app.preferences + local docPref = pref.document(spr) + pref.symmetry_mode.enabled = false + docPref.tiled.mode = 3 + + local title = '2px, translucent, no symmetry, tiled' + local red = Color{ r=255, g=0, b=0, a=127 } + local r = red.rgbaPixel + local square=Brush{ size=2, type=BrushType.SQUARE } + local testData = { + -- Top left corner + { + id='1 - ' .. title .. ': on top left corner, right then down', + points={ Point(0, 0), Point(1, 0), Point(1, 1) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='2 - ' .. title .. ': on top left corner, down then right', + points={ Point(0, 0), Point(0, 1), Point(1, 1) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='3 - ' .. title .. ': on top left corner, left then up', + points={ Point(0, 0), Point(-1, 0), Point(-1, -1) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + { + id='4 - ' .. title .. ': on top left corner, up then left', + points={ Point(0, 0), Point(0, -1), Point(-1, -1) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + -- Top right corner + { + id='5 - ' .. title .. ': on top right corner, right then down', + points={ Point(6, 0), Point(7, 0), Point(7, 1) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='6 - ' .. title .. ': on top right corner, down then right', + points={ Point(6, 0), Point(6, 1), Point(7, 1) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='7 - ' .. title .. ': on top right corner, left then up', + points={ Point(6, 0), Point(5, 0), Point(5, -1) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + { + id='8 - ' .. title .. ': on top right corner, up then left', + points={ Point(6, 0), Point(5, 0), Point(5, -1) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + -- Bottom left corner + { + id='9 - ' .. title .. ': on bottom left corner, right then down', + points={ Point(0, 6), Point(1, 6), Point(1, 7) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='10 - ' .. title .. ': on bottom left corner, down then right', + points={ Point(0, 6), Point(0, 7), Point(1, 7) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='11 - ' .. title .. ': on bottom left corner, left then up', + points={ Point(0, 6), Point(-1, 6), Point(-1, 5) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + { + id='12 - ' .. title .. ': on bottom left corner, up then left', + points={ Point(0, 6), Point(0, 5), Point(-1, 5) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + -- Botomm right corner + { + id='13 - ' .. title .. ': on bottom right corner, right then down', + points={ Point(6, 6), Point(7, 6), Point(7, 7) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='14 - ' .. title .. ': on bottom right corner, down then right', + points={ Point(6, 6), Point(6, 7), Point(7, 7) }, + expected={ r, r, 0, 0, 0, r, + r, r, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + r, 0, 0, 0, 0, r } + }, + { + id='15 - ' .. title .. ': on bottom right corner, left then up', + points={ Point(6, 6), Point(5, 6), Point(5, 5) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + { + id='16 - ' .. title .. ': on bottom right corner, up then left', + points={ Point(6, 6), Point(6, 5), Point(5, 5) }, + expected={ r, 0, 0, 0, 0, r, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, r, r, + r, 0, 0, 0, r, r } + }, + } + + for i,v in ipairs(testData) do + app.useTool{ + tool='pencil', + freehandAlgorithm=1, + brush=square, + color=red, + points=v.points} + expect_img_msg(cel.image, v.expected, '\nTest \'' .. v.id .. '\' failed') + cel.image:clear(0) + end +end diff --git a/scripts/test_utils.lua b/scripts/test_utils.lua index 0f8029109..28e208fad 100644 --- a/scripts/test_utils.lua +++ b/scripts/test_utils.lua @@ -32,6 +32,8 @@ function expect_img(image, expectedPixels) local h = image.height if w*h ~= #expectedPixels then print(debug.traceback()) + print('Expected pixels: #=' .. #expectedPixels) + print('Image size: w=' .. w .. ' h=' .. h .. ' #=' .. w*h) dump_img(image) assert(w*h == #expectedPixels) end @@ -75,6 +77,14 @@ function expect_img(image, expectedPixels) end end +function expect_img_msg(image, expectedPixels, msg) + local status, err = pcall(expect_img, image, expectedPixels) + if not status then + print(msg) + error(err) + end +end + function array_to_pixels(array, image) local w = image.width local h = image.height