2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/spec/unit/focusmanager_spec.lua
Philip Chan 107156c0a8
[feat] Non-touch improvements (#8859)
FocusManager: fix round x use y layout
FocusManager: add tab and shift tab focus navigation support
FocusManager: handle Press key by default
FocusManager: make sure selected in instance level
FocusManager: add hold event support
FocusManager: Half move instead of edge move
FocusManager: add keymap override support
FocusManager: refocusWidget will delegate to parent FocusManager
Focusmanager: refocusWidget can execute on next tick
inputtext: can move out of focus on back
inputtext: fix cannot exit for non-touch device
inputtext: fix cannot input text with kindle dx physical keyboard
fontlightwidget: add non-touch support
datetimewidget: add non-touch support
datetimewidget: fix set date failed in kindle DX, fix datetimewidget month range to 1~23 by default
datetimewidget: make hour max value to 23
multiinputdialog: add non-touch support
checkbox: focusable and focus style
virtualkeyboard: no need to press two back to unfocus inputtext
virtualkeyboard: collect FocusManager event key names to let VirtualKeyboard disable them
openwithdialog: add non-touch support
inputdialog: can close via back button
enable all InputDialog and MultiInputDialog can be close by back
keyboardlayoutdialog: non-touch support
readertoc: non touch device can expand/collapse in toc
bookstatuswidget: non touch support
keyvaluepage: non-touch support
calendarview: non-touch support
2022-03-04 21:20:00 +01:00

233 lines
8.4 KiB
Lua

describe("FocusManager module", function()
local FocusManager
local layout, big_layout
local Key
local Input
local Up = function(self) self:onFocusMove({0, -1}) end
local Down = function(self) self:onFocusMove({0, 1}) end
local Left = function(self) self:onFocusMove({-1, 0}) end
local Right = function(self) self:onFocusMove({1, 0}) end
local Next = function(self) self:onFocusNext() end
local Previous = function(self) self:onFocusPrevious() end
local HalfMoveUp = function(self) self:onFocusHalfMove({"up"}) end
local HalfMoveDown = function(self) self:onFocusHalfMove({"down"}) end
local HalfMoveLeft = function(self) self:onFocusHalfMove({"left"}) end
local HalfMoveRight = function(self) self:onFocusHalfMove({"right"}) end
local MoveTo = function(self, x, y) self:moveFocusTo(x, y) end
setup(function()
require("commonrequire")
FocusManager = require("ui/widget/focusmanager")
Key = require("device/key")
Input = require("device/input")
local Widget = require("ui/widget/textwidget")
local w = Widget:new{}
layout= {
{w, w, w},
{nil,w,nil},
{nil,w,nil},
}
big_layout = {
{w, w, w, w, w},
{w, w, w, w, w},
{w, w, w, w, w},
{w, w, w, w, w},
{w, w, w, w, w},
}
end)
it("should go right", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 1}
Right(focusmanager)
assert.are.same({y = 1,x = 2}, focusmanager.selected)
end)
it("should go left", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 2}
Left(focusmanager)
assert.are.same({y = 1,x = 1}, focusmanager.selected)
end)
it("should go up", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 2,x = 2}
Up(focusmanager)
assert.are.same({y = 1,x = 2}, focusmanager.selected)
end)
it("should go down", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 2,x = 2}
Down(focusmanager)
assert.are.same({y = 3,x = 2}, focusmanager.selected)
end)
it("should vertical wrapAround up", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 1}
Up(focusmanager)
assert.are.same({y = 3,x = 2}, focusmanager.selected)
end)
it("should vertical wrapAround down", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 3,x = 2}
Down(focusmanager)
assert.are.same({y = 1,x = 2}, focusmanager.selected)
end)
it("should do vertical step to the right", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 1}
Down(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should do vertical step to the left", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 3}
Down(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should respect left limit", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 2,x = 2}
Left(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should respect right limit", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 2,x = 2}
Right(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should move next right", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 2}
Next(focusmanager)
assert.are.same({y = 1,x = 3}, focusmanager.selected)
end)
it("should move next row at end of row", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 3}
Next(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should move next left", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 1,x = 2}
Previous(focusmanager)
assert.are.same({y = 1,x = 1}, focusmanager.selected)
end)
it("should move previous at start of row", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager.selected = {y = 3,x = 2}
Previous(focusmanager)
assert.are.same({y = 2,x = 2}, focusmanager.selected)
end)
it("should move half rows or columns", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = big_layout
focusmanager.selected = {x = 1, y = 1}
HalfMoveRight(focusmanager)
assert.are.same({y = 1,x = 3}, focusmanager.selected)
HalfMoveDown(focusmanager)
assert.are.same({y = 3,x = 3}, focusmanager.selected)
HalfMoveLeft(focusmanager)
assert.are.same({y = 3,x = 1}, focusmanager.selected)
HalfMoveUp(focusmanager)
assert.are.same({y = 1,x = 1}, focusmanager.selected)
end)
it("should move to specified position", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = big_layout
focusmanager.selected = {x = 1, y = 1}
MoveTo(focusmanager, 3, 4)
assert.are.same({y = 4,x = 3}, focusmanager.selected)
end)
it("should set layout to nil", function()
local focusmanager = FocusManager:new{}
focusmanager.layout = layout
focusmanager:disableFocusManagement()
assert.is_nil(focusmanager.layout)
end)
it("should merge into rows", function()
local w = layout[1][1]
local fm1 = FocusManager:new{}
fm1.layout = {
{w, w, w}
}
local fm2 = FocusManager:new{}
fm2.layout = {
{w, w},
}
fm1:mergeLayoutInVertical(fm2)
local expected = {
{w, w, w},
{w, w}
}
assert.are.same(expected, fm1.layout)
end)
it("should merge into rows at specified position", function()
local w = layout[1][1]
local fm1 = FocusManager:new{}
fm1.layout = {
{w, w, w},
{w, w, w},
}
local fm2 = FocusManager:new{}
fm2.layout = {
{w, w},
}
fm1:mergeLayoutInVertical(fm2, 2)
local expected = {
{w, w, w},
{w, w},
{w, w, w},
}
assert.are.same(expected, fm1.layout)
end)
it("should merge into columns", function()
local w = layout[1][1]
local fm1 = FocusManager:new{}
fm1.layout = {
{w},
{w},
}
local fm2 = FocusManager:new{}
fm2.layout = {
{w, w},
{w},
}
fm1:mergeLayoutInHorizontal(fm2, 2)
local expected = {
{w, w, w},
{w, w},
}
assert.are.same(expected, fm1.layout)
end)
it("alternative key", function()
local focusmanager = FocusManager:new{}
focusmanager.extra_key_events = {
Hold = { {"Sym", "AA"}, doc = "tap and hold the widget", event="Hold" },
HalfFocusUp = { {"Alt", "Up"}, doc = "move focus half columns up", event = "FocusHalfMove", args = {"up"} },
}
local m = Input.modifiers;
m.Sym = true
assert.is_true(focusmanager:isAlternativeKey(Key:new("AA", m)))
m.Sym = false
m.Alt = true
assert.is_true(focusmanager:isAlternativeKey(Key:new("Up", m)))
m.Alt = false
assert.is_false(focusmanager:isAlternativeKey(Key:new("AA", m)))
assert.is_false(focusmanager:isAlternativeKey(Key:new("Up", m)))
end)
end)