You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
koreader/spec/unit/device_spec.lua

504 lines
18 KiB
Lua

describe("device module", function()
-- luacheck: push ignore
local mock_fb, mock_input
local iopen = io.open
local ipopen = io.popen
local osgetenv = os.getenv
local ffi, C
setup(function()
local fb = require("ffi/framebuffer")
mock_fb = {
new = function()
return {
device = package.loaded.device,
bb = require("ffi/blitbuffer").new(600, 800, 1),
getRawSize = function() return {w = 600, h = 800} end,
getWidth = function() return 600 end,
getHeight = function() return 800 end,
getDPI = function() return 72 end,
setViewport = function() end,
getRotationMode = function() return 0 end,
getScreenMode = function() return "portrait" end,
setRotationMode = function() end,
scaleByDPI = fb.scaleByDPI,
scaleBySize = fb.scaleBySize,
setWindowTitle = function() end,
refreshFull = function() end,
getHWNightmode = function() return false end,
setupDithering = function() end,
}
end
}
require("commonrequire")
package.unloadAll()
ffi = require("ffi")
C = ffi.C
require("ffi/linux_input_h")
require("document/canvascontext"):init(require("device"))
end)
before_each(function()
package.loaded["ffi/framebuffer_mxcfb"] = mock_fb
mock_input = require("device/input")
stub(mock_input, "open")
stub(os, "getenv")
stub(os, "execute")
end)
after_each(function()
-- Don't let UIManager hang on to a stale Device reference, and vice-versa...
package.unload("device")
package.unload("device/generic/device")
package.unload("device/generic/powerd")
package.unload("ui/uimanager")
package.unload("apps/reader/readerui")
mock_input.open:revert()
os.getenv:revert()
os.execute:revert()
os.getenv = osgetenv
io.open = iopen
io.popen = ipopen
end)
describe("kobo", function()
local time
local NickelConf
setup(function()
time = require("ui/time")
NickelConf = require("device/kobo/nickel_conf")
end)
before_each(function()
stub(NickelConf.frontLightLevel, "get")
NickelConf.frontLightLevel.get.returns(0)
stub(NickelConf.frontLightState, "get")
end)
after_each(function()
NickelConf.frontLightLevel.get:revert()
NickelConf.frontLightState.get:revert()
end)
it("should initialize properly on Kobo dahlia", function()
os.getenv.returns("dahlia")
local kobo_dev = require("device/kobo/device")
kobo_dev:init()
assert.is.same("Kobo_dahlia", kobo_dev.model)
end)
it("should setup eventAdjustHooks properly for input on trilogy C", function()
os.getenv.invokes(function(key)
if key == "PRODUCT" then
return "trilogy"
elseif key == "MODEL_NUMBER" then
return "320"
else
return osgetenv(key)
end
end)
package.loaded["device/kobo/device"] = nil
local kobo_dev = require("device/kobo/device")
kobo_dev:init()
local Screen = kobo_dev.screen
assert.is.same("Kobo_trilogy_C", kobo_dev.model)
local x, y = Screen:getWidth()-5, 10
-- mirror x, then switch_xy
local ev_x = {
type = C.EV_ABS,
code = C.ABS_X,
value = y,
time = time:realtime(),
}
local ev_y = {
type = C.EV_ABS,
code = C.ABS_Y,
value = Screen:getWidth() - 1 - x,
time = time:realtime(),
}
kobo_dev.input:eventAdjustHook(ev_x)
kobo_dev.input:eventAdjustHook(ev_y)
assert.is.same(x, ev_y.value)
assert.is.same(C.ABS_X, ev_y.code)
assert.is.same(y, ev_x.value)
assert.is.same(C.ABS_Y, ev_x.code)
-- reset eventAdjustHook
kobo_dev.input.eventAdjustHook = function() end
end)
it("should setup eventAdjustHooks properly for trilogy with non-epoch ev time", function()
-- This has no more value since #6798 as ev time can now stay
-- non-epoch. Adjustments are made on first event handled, and
-- have only effects when handling long-press (so, the long-press
-- for dict lookup tests with test this).
-- We just check here it still works with non-epoch ev time, as previous test
os.getenv.invokes(function(key)
if key == "PRODUCT" then
return "trilogy"
elseif key == "MODEL_NUMBER" then
return "320"
else
return osgetenv(key)
end
end)
package.loaded["device/kobo/device"] = nil
local kobo_dev = require("device/kobo/device")
kobo_dev:init()
local Screen = kobo_dev.screen
assert.is.same("Kobo_trilogy_C", kobo_dev.model)
local x, y = Screen:getWidth()-5, 10
local ev_x = {
type = C.EV_ABS,
code = C.ABS_X,
value = y,
time = {sec = 1000}
}
local ev_y = {
type = C.EV_ABS,
code = C.ABS_Y,
value = Screen:getWidth() - 1 - x,
time = {sec = 1000}
}
kobo_dev.input:eventAdjustHook(ev_x)
kobo_dev.input:eventAdjustHook(ev_y)
assert.is.same(x, ev_y.value)
assert.is.same(C.ABS_X, ev_y.code)
assert.is.same(y, ev_x.value)
assert.is.same(C.ABS_Y, ev_x.code)
-- reset eventAdjustHook
kobo_dev.input.eventAdjustHook = function() end
end)
end)
describe("kindle", function()
local function make_io_open_kindle_model_override(model_no)
return function(filename, mode)
if filename == "/proc/usid" then
return {
read = function() return model_no end,
close = function() end
}
else
return iopen(filename, mode)
end
end
end
insulate("without framework", function()
local mock_lipc = {
init = function()
return {
set_int_property = mock(function() end),
get_int_property = function() return 0 end,
get_string_property = function() return "string prop" end,
set_string_property = function() end,
register_int_property = function() return {} end,
close = function () end,
}
end
}
package.loaded["liblipclua"] = mock_lipc
before_each(function()
os.getenv.invokes(function(e)
if e == "STOP_FRAMEWORK" then
return "yes"
else
return osgetenv(e)
end
end)
end)
it("sets framework_lipc_handle", function ()
io.open = make_io_open_kindle_model_override("B013XX")
local kindle_dev = require("device/kindle/device")
assert.is.truthy(kindle_dev.framework_lipc_handle)
end)
it("reactivates voyage whispertouch keys", function ()
io.open = make_io_open_kindle_model_override("B013XX")
local kindle_dev = require("device/kindle/device")
local fw_lipc_handle = kindle_dev.framework_lipc_handle
kindle_dev:init()
for _, fsr_prop in pairs{
"fsrkeypadEnable",
"fsrkeypadPrevEnable",
"fsrkeypadNextEnable"
} do
assert.stub(fw_lipc_handle.set_int_property).was.called_with(
fw_lipc_handle, "com.lab126.deviced", fsr_prop, 1
)
end
end)
end)
insulate("with framework", function()
it("does not set framework_lipc_handle", function ()
io.open = make_io_open_kindle_model_override("B013XX")
local kindle_dev = require("device/kindle/device")
assert.is.falsy(kindle_dev.framework_lipc_handle)
end)
end)
it("should initialize voyage without error", function()
io.open = make_io_open_kindle_model_override("B013XX")
local kindle_dev = require("device/kindle/device")
assert.is.same(kindle_dev.model, "KindleVoyage")
kindle_dev:init()
assert.is.same(kindle_dev.input.event_map[104], "LPgBack")
assert.is.same(kindle_dev.input.event_map[109], "LPgFwd")
assert.is.same(kindle_dev.powerd.fl_min, 0)
-- NOTE: fl_max + 1 since #5989
assert.is.same(kindle_dev.powerd.fl_max, 25)
end)
it("should toggle frontlight", function()
io.open = function(filename, mode)
if filename == "/proc/usid" then
return {
read = function() return "B013XX" end,
close = function() end
}
elseif filename == "/sys/class/backlight/max77696-bl/brightness" then
return {
read = function() return 12 end,
close = function() end
}
else
return iopen(filename, mode)
end
end
local kindle_dev = require("device/kindle/device")
kindle_dev:init()
assert.is.same(kindle_dev.powerd.fl_intensity, 12)
kindle_dev.powerd:setIntensity(5)
assert.is.same(kindle_dev.powerd.fl_intensity, 5)
kindle_dev.powerd:toggleFrontlight()
-- Here be shenanigans: we don't override powerd's fl_intensity when we turn the light off,
-- so that we can properly turn it back on at the previous intensity ;)
assert.is.same(kindle_dev.powerd.fl_intensity, 5)
-- But if we were to cat /sys/class/backlight/max77696-bl/brightness, it should now be 0.
kindle_dev.powerd:toggleFrontlight()
assert.is.same(kindle_dev.powerd.fl_intensity, 5)
-- And /sys/class/backlight/max77696-bl/brightness is now !0
-- (exact value is HW-dependent, each model has a different curve, we let lipc do the work for us).
end)
it("oasis should interpret orientation event", function()
package.unload("device/kindle/device")
io.open = make_io_open_kindle_model_override("G0B0GCXXX")
mock_ffi_input = require("ffi/input")
stub(mock_ffi_input, "waitForEvent")
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415) * ReaderDictionary: Port delay computations to TimeVal * ReaderHighlight: Port delay computations to TimeVal * ReaderView: Port delay computations to TimeVal * Android: Reset gesture detection state on APP_CMD_TERM_WINDOW. This prevents potentially being stuck in bogus gesture states when switching apps. * GestureDetector: * Port delay computations to TimeVal * Fixed delay computations to handle time warps (large and negative deltas). * Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture. * Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1. * Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy. * The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events. The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use a clock source that is *NOT* MONOTONIC. AFAICT, that's pretty much... PocketBook, and that's it? * Input: * Use the <linux/input.h> FFI module instead of re-declaring every constant * Fixed (verbose) debug logging of input events to actually translate said constants properly. * Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume. * Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient. Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts, as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector. * reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary. * TimeVal: * Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>). * Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC. * Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence. * New methods: * Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime * Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero * UIManager: * Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base. This ensures reliable and consistent scheduling, as time is ensured never to go backwards. * Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame. It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value, because very few time has passed. The only code left that does live syscalls does it because it's actually necessary for accuracy, and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics). * DictQuickLookup: Port delay computations to TimeVal * FootNoteWidget: Port delay computations to TimeVal * HTMLBoxWidget: Port delay computations to TimeVal * Notification: Port delay computations to TimeVal * TextBoxWidget: Port delay computations to TimeVal * AutoSuspend: Port to TimeVal * AutoTurn: * Fix it so that settings are actually honored. * Port to TimeVal * BackgroundRunner: Port to TimeVal * Calibre: Port benchmarking code to TimeVal * BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly. * All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
3 years ago
mock_ffi_input.waitForEvent.returns(true, {
{
type = C.EV_ABS,
time = {
usec = 450565,
sec = 1471081881
},
code = 24, -- C.ABS_PRESSURE
value = 16
}
})
local UIManager = require("ui/uimanager")
stub(UIManager, "onRotation")
local kindle_dev = require("device/kindle/device")
assert.is.same("KindleOasis", kindle_dev.model)
kindle_dev:init()
kindle_dev:lockGSensor(true)
kindle_dev.input:waitEvent()
assert.stub(UIManager.onRotation).was_called()
mock_ffi_input.waitForEvent:revert()
UIManager.onRotation:revert()
end)
end)
describe("Flush book Settings for", function()
it("Kobo", function()
os.getenv.invokes(function(key)
if key == "PRODUCT" then
return "trilogy"
elseif key == "MODEL_NUMBER" then
return "320"
else
return osgetenv(key)
end
end)
-- Bypass frontend/device probeDevice, while making sure that it points to the right implementation
local Device = require("device/kobo/device")
-- Apparently common isn't setup properly in the testsuite, so we can't have nice things
stub(Device, "initNetworkManager")
stub(Device, "suspend")
Device:init()
-- Don't poke the RTC
Device.wakeup_mgr = require("device/wakeupmgr"):new{rtc = require("device/kindle/mockrtc")}
-- Don't poke the fl
Device.powerd.fl = nil
package.loaded.device = Device
local UIManager = require("ui/uimanager")
-- Generic's onPowerEvent may request a repaint, but we can't do that
stub(UIManager, "forceRePaint")
UIManager:init()
local sample_pdf = "spec/front/unit/data/tall.pdf"
local ReaderUI = require("apps/reader/readerui")
ReaderUI:doShowReader(sample_pdf)
local readerui = ReaderUI.instance
stub(readerui, "onFlushSettings")
Assorted bag'o tweaks & fixes (#9569) * UIManager: Support more specialized update modes for corner-cases: * A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor). * NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*). * UIManager: Assorted code cleanups & simplifications. * Logger & dbg: Unify logging style, and code cleanups. * SDL: Unbreak suspend/resume outside of the emulator (fix #9567). * NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay). * OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573) * WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!). * Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit. * UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo). * UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes). * Kindle: ID the upcoming Scribe. * Bump base (https://github.com/koreader/koreader-base/pull/1524)
2 years ago
UIManager.event_handlers.PowerPress()
UIManager.event_handlers.PowerRelease()
assert.stub(readerui.onFlushSettings).was_called()
UIManager.forceRePaint:revert()
Device.initNetworkManager:revert()
Device.suspend:revert()
readerui.onFlushSettings:revert()
Device.screen_saver_mode = false
readerui:onClose()
end)
it("Cervantes", function()
io.popen = function(filename, mode)
if filename:find("/usr/bin/ntxinfo") then
return {
read = function()
return 68 -- Cervantes4
end,
close = function() end
}
else
return ipopen(filename, mode)
end
end
local Device = require("device/cervantes/device")
stub(Device, "initNetworkManager")
stub(Device, "suspend")
Device:init()
Device.powerd.fl = nil
package.loaded.device = Device
local UIManager = require("ui/uimanager")
stub(UIManager, "forceRePaint")
UIManager:init()
local sample_pdf = "spec/front/unit/data/tall.pdf"
local ReaderUI = require("apps/reader/readerui")
ReaderUI:doShowReader(sample_pdf)
local readerui = ReaderUI.instance
stub(readerui, "onFlushSettings")
Assorted bag'o tweaks & fixes (#9569) * UIManager: Support more specialized update modes for corner-cases: * A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor). * NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*). * UIManager: Assorted code cleanups & simplifications. * Logger & dbg: Unify logging style, and code cleanups. * SDL: Unbreak suspend/resume outside of the emulator (fix #9567). * NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay). * OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573) * WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!). * Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit. * UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo). * UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes). * Kindle: ID the upcoming Scribe. * Bump base (https://github.com/koreader/koreader-base/pull/1524)
2 years ago
UIManager.event_handlers.PowerPress()
UIManager.event_handlers.PowerRelease()
assert.stub(readerui.onFlushSettings).was_called()
UIManager.forceRePaint:revert()
Device.initNetworkManager:revert()
Device.suspend:revert()
readerui.onFlushSettings:revert()
Device.screen_saver_mode = false
readerui:onClose()
end)
it("Remarkable", function()
io.open = function(filename, mode)
if filename == "/usr/bin/xochitl" then
return {
read = function()
return true
end,
close = function() end
}
elseif filename == "/sys/devices/soc0/machine" then
return {
read = function()
return "reMarkable", "generic"
end,
close = function() end
}
else
return iopen(filename, mode)
end
end
local Device = require("device/remarkable/device")
stub(Device, "initNetworkManager")
stub(Device, "suspend")
Device:init()
Device.powerd.fl = nil
package.loaded.device = Device
local UIManager = require("ui/uimanager")
stub(UIManager, "forceRePaint")
UIManager:init()
local sample_pdf = "spec/front/unit/data/tall.pdf"
local ReaderUI = require("apps/reader/readerui")
ReaderUI:doShowReader(sample_pdf)
local readerui = ReaderUI.instance
stub(readerui, "onFlushSettings")
UIManager.event_handlers.PowerPress()
UIManager.event_handlers.PowerRelease()
assert.stub(readerui.onFlushSettings).was_called()
UIManager.forceRePaint:revert()
Device.initNetworkManager:revert()
Device.suspend:revert()
readerui.onFlushSettings:revert()
Device.screen_saver_mode = false
readerui:onClose()
end)
it("SDL", function()
local Device = require("device/sdl/device")
stub(Device, "initNetworkManager")
stub(Device, "suspend")
Device:init()
package.loaded.device = Device
local UIManager = require("ui/uimanager")
UIManager:init()
local sample_pdf = "spec/front/unit/data/tall.pdf"
local ReaderUI = require("apps/reader/readerui")
ReaderUI:doShowReader(sample_pdf)
local readerui = ReaderUI.instance
stub(readerui, "onFlushSettings")
-- UIManager.event_handlers.PowerPress() -- We only fake a Release event on the Emu
Assorted bag'o tweaks & fixes (#9569) * UIManager: Support more specialized update modes for corner-cases: * A2, which we'll use for the VirtualKeyboards keys (they'd... inadvertently switched to UI with the highlight refactor). * NO_MERGE variants of ui & partial (for sunxi). Use `[ui]` in ReaderHighlight's popup, because of a Sage kernel bug that could otherwise make it translucent, sometimes completely so (*sigh*). * UIManager: Assorted code cleanups & simplifications. * Logger & dbg: Unify logging style, and code cleanups. * SDL: Unbreak suspend/resume outside of the emulator (fix #9567). * NetworkMgr: Cache the network status, and allow it to be queried. (Used by AutoSuspend to avoid repeatedly poking the system when computing the standby schedule delay). * OneTimeMigration: Don't forget about `NETWORK_PROXY` & `STARDICT_DATA_DIR` when migrating `defaults.persistent.lua` (fix #9573) * WakeupMgr: Workaround an apparent limitation of the RTC found on i.MX5 Kobo devices, where setting a wakealarm further than UINT16_MAX seconds in the future would apparently overflow and wraparound... (fix #8039, many thanks to @yfede for the extensive deep-dive and for actually accurately pinpointing the issue!). * Kobo: Handle standby transitions at full CPU clock speeds, in order to limit the latency hit. * UIManager: Properly quit on reboot & exit. This ensures our exit code is preserved, as we exit on our own terms (instead of being killed by the init system). This is important on platforms where exit codes are semantically meaningful (e.g., Kobo). * UIManager: Speaking of reboot & exit, make sure the Screensaver shows in all circumstances (e.g., autoshutdown, re: #9542)), and that there aren't any extraneous refreshes triggered. (Additionally, fix a minor regression since #9448 about tracking this very transient state on Kobo & Cervantes). * Kindle: ID the upcoming Scribe. * Bump base (https://github.com/koreader/koreader-base/pull/1524)
2 years ago
UIManager.event_handlers.PowerRelease()
assert.stub(readerui.onFlushSettings).was_called()
Device.initNetworkManager:revert()
Device.suspend:revert()
readerui.onFlushSettings:revert()
Device.screen_saver_mode = false
readerui:onClose()
end)
end)
-- luacheck: pop
end)