Files
koreader/spec/unit/focusmanager_spec.lua
T

281 lines
11 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("should keep released keys released when a keyboard is hot-plugged", function()
local focusmanager = FocusManager:new{}
focusmanager:releaseFocusKeys("FocusLeft", "FocusRight")
assert.is_nil(focusmanager.key_events.FocusLeft)
assert.is_nil(focusmanager.key_events.FocusRight)
-- The handler re-merges the default mappings, which would bring them back.
focusmanager:onPhysicalKeyboardConnected()
assert.is_nil(focusmanager.key_events.FocusLeft)
assert.is_nil(focusmanager.key_events.FocusRight)
end)
it("should let the widget rebind its own keys when the mappings are rebuilt", function()
local focusmanager = FocusManager:new{}
local calls = 0
focusmanager.focus_keys_callback = function(self)
calls = calls + 1
self.key_events.ExpandCurrentNode = { { "Right" } }
end
focusmanager:onPhysicalKeyboardConnected()
assert.are.equal(1, calls)
assert.are.same({ { "Right" } }, focusmanager.key_events.ExpandCurrentNode)
end)
it("should restore its own keys when a widget cleared them on disconnect", function()
local focusmanager = FocusManager:new{}
local focus_left = focusmanager.key_events.FocusLeft
-- What a widget rebuilding its bindings around the hot-plug does.
focusmanager.key_events = {}
focusmanager:onPhysicalKeyboardDisconnected()
assert.are.same(focus_left, focusmanager.key_events.FocusLeft)
end)
it("should keep released keys released across a disconnect", function()
local focusmanager = FocusManager:new{}
focusmanager:releaseFocusKeys("FocusLeft", "FocusRight")
focusmanager:onPhysicalKeyboardDisconnected()
assert.is_nil(focusmanager.key_events.FocusLeft)
assert.is_nil(focusmanager.key_events.FocusRight)
end)
it("should not rebind anything once the device has no keys left", function()
local Device = require("device")
local focusmanager = FocusManager:new{}
local called = false
focusmanager.focus_keys_callback = function() called = true end
stub(Device, "hasKeys")
Device.hasKeys.returns(false)
focusmanager:onPhysicalKeyboardDisconnected()
Device.hasKeys:revert()
assert.is_false(called)
assert.are.same({}, focusmanager.key_events)
end)
it("alternative key", function()
local focusmanager = FocusManager:new{}
focusmanager.extra_key_events = {
Hold = { { "Sym", "AA" }, event="Hold" },
HalfFocusUp = { { "Alt", "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)