Files
slate32/test/keyboard.lua
T

89 lines
2.8 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].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")
-- 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")