Files
slate32/test/keyboard.lua
T

120 lines
3.9 KiB
Lua

-- Run: lua test/keyboard.lua
package.path = "sdcard/lib/?.lua;test/?.lua;" .. package.path
local device = require("fake_device").install()
local ui = require("ui")
local keyboard = require("keyboard")
local changes, submitted = {}, nil
local board = keyboard.new{
on_change = function(value) changes[#changes + 1] = value end,
on_submit = function(value) submitted = value end,
}
local screen = ui.screen(ui.box{pad = 8, board})
local rounds = {}
gui.roundRect = function(x, y, w, h, radius, bg, top, bottom, border)
rounds[#rounds + 1] = {x = x, y = y, w = w, top = top, bottom = bottom, border = border}
end
local function draw()
rounds = {}
screen:draw()
end
local function tap(x, y)
screen:down(x, y)
screen:up(x, y)
device.now = device.now + 100
screen:draw()
screen:draw()
end
local row1X = board.rect.x + (board.rect.w - 296) // 2 + 13
local row2X = board.rect.x + (board.rect.w - 266) // 2 + 13
local row3Y = board.rect.y + 85
local bottomY = board.rect.y + 120
local qx, qy = row1X, board.rect.y + 15
-- Rows are staggered and centered like a physical keyboard.
draw()
assert(rounds[1].x == board.rect.x + 4, "top row was not centered")
assert(rounds[11].x == board.rect.x + 19, "home row was not centered")
-- Only the touched key uses the pressed palette.
local normalQ = rounds[1]
screen:down(qx, qy)
draw()
assert(#rounds == 1, "pressing q repainted " .. #rounds .. " keys")
assert(rounds[1].top ~= normalQ.top or rounds[1].bottom ~= normalQ.bottom,
"pressed q kept its normal gradient")
screen:up(qx, qy)
assert(changes[#changes] == "q", "q was not entered")
device.now = 80
draw()
assert(#rounds == 1, "releasing q repainted " .. #rounds .. " keys")
-- Starting another tap during the hold clears the old key without redrawing the board.
local rapidBoard = keyboard.new{}
local rapidScreen = ui.screen(ui.box{pad = 8, rapidBoard})
rounds = {}
rapidScreen:draw()
local rapidNormal = rounds[1].top
local rapidQx, rapidY = rapidBoard.rect.x + 17, rapidBoard.rect.y + 15
local rapidWx = rapidQx + 30
rapidScreen:down(rapidQx, rapidY)
rounds = {}
rapidScreen:draw()
rapidScreen:up(rapidQx, rapidY)
device.now = 90
rapidScreen:down(rapidWx, rapidY)
rounds = {}
rapidScreen:draw()
assert(#rounds == 2, "overlapping taps repainted " .. #rounds .. " keys")
local states = {}
for _, key in ipairs(rounds) do states[key.x] = key.top end
assert(states[rapidQx - 13] == rapidNormal, "previous key stayed pressed")
assert(states[rapidWx - 13] ~= rapidNormal, "new key was not pressed")
rapidScreen:up(rapidWx, rapidY)
device.now = 170
rounds = {}
rapidScreen:draw()
assert(#rounds == 1 and rounds[1].top == rapidNormal, "new key stayed pressed")
-- Sliding onto another key cancels instead of entering the release position.
screen:draw()
screen:draw()
screen:down(qx, qy)
screen:up(qx + 30, qy)
assert(#changes == 1, "sliding from q to w entered a key")
-- Shift toggles case without replacing the keyboard.
local shiftX = board.rect.x + (board.rect.w - 294) // 2 + 20
tap(shiftX, row3Y)
tap(qx, qy)
assert(changes[#changes] == "qQ", "shift did not enter uppercase Q")
tap(shiftX, row3Y)
-- 123 opens numbers, #+= opens symbols, and backspace preserves the value.
local modeX = board.rect.x + (board.rect.w - 294) // 2 + 27
tap(modeX, bottomY)
tap(qx, qy)
assert(changes[#changes] == "qQ1", "number page did not enter 1")
local symbolX = board.rect.x + (board.rect.w - 234) // 2 + 20
tap(symbolX, row3Y)
tap(qx, qy)
assert(changes[#changes] == "qQ1[", "symbol page did not enter [")
local backspaceX = board.rect.x + (board.rect.w - 234) // 2 + 214
tap(backspaceX, row3Y)
assert(changes[#changes] == "qQ1", "backspace did not remove the symbol")
-- Bottom row is ABC, a wide spacebar, then OK.
tap(modeX, bottomY)
local spaceX = board.rect.x + (board.rect.w - 294) // 2 + 145
tap(spaceX, bottomY)
local okX = board.rect.x + (board.rect.w - 294) // 2 + 265
tap(okX, bottomY)
assert(submitted == "qQ1 ", "OK lost the keyboard value")
print("ok")