mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
Move kobo auto-suspension logic out of UIManager (#2933)
This commit is contained in:
74
spec/unit/autosuspend_spec.lua
Normal file
74
spec/unit/autosuspend_spec.lua
Normal file
@@ -0,0 +1,74 @@
|
||||
describe("AutoSuspend widget tests", function()
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
end)
|
||||
|
||||
before_each(function()
|
||||
local Device = require("device")
|
||||
stub(Device, "isKobo")
|
||||
Device.isKobo.returns(true)
|
||||
Device.input.waitEvent = function() end
|
||||
local UIManager = require("ui/uimanager")
|
||||
stub(UIManager, "suspend")
|
||||
UIManager._run_forever = true
|
||||
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 10)
|
||||
require("mock_time"):install()
|
||||
end)
|
||||
|
||||
after_each(function()
|
||||
require("device").isKobo:revert()
|
||||
require("ui/uimanager").suspend:revert()
|
||||
G_reader_settings:delSetting("auto_suspend_timeout_seconds")
|
||||
require("mock_time"):uninstall()
|
||||
end)
|
||||
|
||||
it("should be able to execute suspend when timing out", function()
|
||||
local mock_time = require("mock_time")
|
||||
local widget_class = dofile("plugins/autosuspend.koplugin/main.lua")
|
||||
local widget = widget_class:new()
|
||||
local UIManager = require("ui/uimanager")
|
||||
mock_time:increase(5)
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(0)
|
||||
mock_time:increase(6)
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(1)
|
||||
mock_time:uninstall()
|
||||
end)
|
||||
|
||||
it("should be able to initialize several times", function()
|
||||
local mock_time = require("mock_time")
|
||||
-- AutoSuspend plugin set the last_action_sec each time it is initialized.
|
||||
local widget_class = dofile("plugins/autosuspend.koplugin/main.lua")
|
||||
local widget1 = widget_class:new()
|
||||
-- So if one more initialization happens, it won't sleep after another 5 seconds.
|
||||
mock_time:increase(5)
|
||||
local widget2 = widget_class:new()
|
||||
local UIManager = require("ui/uimanager")
|
||||
mock_time:increase(6)
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(1)
|
||||
mock_time:uninstall()
|
||||
end)
|
||||
|
||||
it("should be able to deprecate last task", function()
|
||||
local mock_time = require("mock_time")
|
||||
local widget_class = dofile("plugins/autosuspend.koplugin/main.lua")
|
||||
local widget = widget_class:new()
|
||||
mock_time:increase(5)
|
||||
local UIManager = require("ui/uimanager")
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(0)
|
||||
widget:onInputEvent()
|
||||
widget:onSuspend()
|
||||
widget:onResume()
|
||||
mock_time:increase(6)
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(0)
|
||||
mock_time:increase(5)
|
||||
UIManager:handleInput()
|
||||
assert.stub(UIManager.suspend).was.called(1)
|
||||
mock_time:uninstall()
|
||||
end)
|
||||
end)
|
||||
@@ -18,12 +18,11 @@ describe("device module", function()
|
||||
end
|
||||
}
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
end)
|
||||
|
||||
before_each(function()
|
||||
package.loaded['ffi/framebuffer_mxcfb'] = mock_fb
|
||||
package.loaded['device/kindle/device'] = nil
|
||||
package.loaded['device/kobo/device'] = nil
|
||||
mock_input = require('device/input')
|
||||
stub(mock_input, "open")
|
||||
stub(os, "getenv")
|
||||
@@ -171,7 +170,6 @@ describe("device module", function()
|
||||
stub(Device, "isKobo")
|
||||
|
||||
Device.isKobo.returns(true)
|
||||
local saved_noop = UIManager._resetAutoSuspendTimer
|
||||
UIManager:init()
|
||||
|
||||
ReaderUI:doShowReader(sample_pdf)
|
||||
@@ -185,9 +183,6 @@ describe("device module", function()
|
||||
Device.powerd.beforeSuspend:revert()
|
||||
Device.isKobo:revert()
|
||||
readerui.onFlushSettings:revert()
|
||||
UIManager._startAutoSuspend = nil
|
||||
UIManager._stopAutoSuspend = nil
|
||||
UIManager._resetAutoSuspendTimer = saved_noop
|
||||
readerui:onClose()
|
||||
end)
|
||||
end)
|
||||
@@ -251,6 +246,7 @@ describe("device module", function()
|
||||
end)
|
||||
|
||||
it("oasis should interpret orientation event", function()
|
||||
package.unload('device/kindle/device')
|
||||
io.open = function(filename, mode)
|
||||
if filename == "/proc/usid" then
|
||||
return {
|
||||
|
||||
@@ -2,11 +2,12 @@ describe("FileManager module", function()
|
||||
local FileManager, lfs, docsettings, UIManager, Screen, util
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
FileManager = require("apps/filemanager/filemanager")
|
||||
lfs = require("libs/libkoreader-lfs")
|
||||
docsettings = require("docsettings")
|
||||
UIManager = require("ui/uimanager")
|
||||
Screen = require("device").screen
|
||||
UIManager = require("ui/uimanager")
|
||||
docsettings = require("docsettings")
|
||||
lfs = require("libs/libkoreader-lfs")
|
||||
util = require("ffi/util")
|
||||
end)
|
||||
it("should show file manager", function()
|
||||
|
||||
72
spec/unit/hook_container_spec.lua
Normal file
72
spec/unit/hook_container_spec.lua
Normal file
@@ -0,0 +1,72 @@
|
||||
describe("HookContainer tests", function()
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
end)
|
||||
|
||||
it("should register and unregister functions", function()
|
||||
local HookContainer = require("ui/hook_container"):new()
|
||||
local f1 = spy.new(function() end)
|
||||
local f2 = spy.new(function() end)
|
||||
local f3 = spy.new(function() end)
|
||||
HookContainer:register("a", f1)
|
||||
HookContainer:register("a", f2)
|
||||
HookContainer:register("b", f3)
|
||||
assert.are.equal(HookContainer:execute("a", 100), 2)
|
||||
assert.are.equal(HookContainer:execute("b", 200), 1)
|
||||
assert.spy(f1).was_called(1)
|
||||
assert.spy(f1).was_called_with(100)
|
||||
assert.spy(f2).was_called(1)
|
||||
assert.spy(f2).was_called_with(100)
|
||||
assert.spy(f3).was_called(1)
|
||||
assert.spy(f3).was_called_with(200)
|
||||
|
||||
assert.is.truthy(HookContainer:unregister("a", f1))
|
||||
assert.is.falsy(HookContainer:unregister("b", f2))
|
||||
|
||||
assert.are.equal(HookContainer:execute("a", 300), 1)
|
||||
assert.are.equal(HookContainer:execute("b", 400), 1)
|
||||
assert.spy(f1).was_called(1)
|
||||
assert.spy(f1).was_called_with(100)
|
||||
assert.spy(f2).was_called(2)
|
||||
assert.spy(f2).was_called_with(300)
|
||||
assert.spy(f3).was_called(2)
|
||||
assert.spy(f3).was_called_with(400)
|
||||
end)
|
||||
|
||||
it("should register and automatically unregister widget", function()
|
||||
local HookContainer = require("ui/hook_container"):new()
|
||||
local widget = require("ui/widget/widget"):new()
|
||||
widget.onEvent = spy.new(function() end)
|
||||
local close_widget = spy.new(function() end)
|
||||
widget.onCloseWidget = close_widget
|
||||
HookContainer:registerWidget("Event", widget)
|
||||
assert.are.equal(HookContainer:execute("Event", { a = 100, b = 200 }), 1)
|
||||
assert.spy(widget.onEvent).was_called(1)
|
||||
assert.spy(widget.onEvent).was_called_with(widget, { a = 100, b = 200 })
|
||||
|
||||
widget:onCloseWidget()
|
||||
assert.spy(close_widget).was_called(1)
|
||||
assert.spy(close_widget).was_called_with(widget)
|
||||
end)
|
||||
|
||||
it("should pass widget itself", function()
|
||||
local HookContainer = require("ui/hook_container"):new()
|
||||
local widget = require("ui/widget/widget"):new()
|
||||
local onEvent_called = false
|
||||
local onCloseWidget_called = false
|
||||
function widget:onEvent(args)
|
||||
assert.is.truthy(self ~= nil)
|
||||
assert.are.same(args, { c = 300, d = 400 })
|
||||
onEvent_called = true
|
||||
end
|
||||
function widget:onCloseWidget()
|
||||
assert.is.truthy(self ~= nil)
|
||||
onCloseWidget_called = true
|
||||
end
|
||||
HookContainer:registerWidget("Event", widget)
|
||||
assert.are.equal(HookContainer:execute("Event", { c = 300, d = 400 }), 1)
|
||||
widget:onCloseWidget()
|
||||
assert.is.truthy(onEvent_called)
|
||||
assert.is.truthy(onCloseWidget_called)
|
||||
end)
|
||||
end)
|
||||
@@ -1,3 +1,5 @@
|
||||
local logger = require("logger")
|
||||
|
||||
local MockTime = {
|
||||
original_os_time = os.time,
|
||||
original_util_time = nil,
|
||||
@@ -11,8 +13,14 @@ function MockTime:install()
|
||||
self.original_util_time = util.gettime
|
||||
assert(self.original_util_time ~= nil)
|
||||
end
|
||||
os.time = function() return self.value end
|
||||
util.gettime = function() return self.value, 0 end
|
||||
os.time = function()
|
||||
logger.dbg("MockTime:os.time: ", self.value)
|
||||
return self.value
|
||||
end
|
||||
util.gettime = function()
|
||||
logger.dbg("MockTime:util.gettime: ", self.value)
|
||||
return self.value, 0
|
||||
end
|
||||
end
|
||||
|
||||
function MockTime:uninstall()
|
||||
@@ -30,6 +38,7 @@ function MockTime:set(value)
|
||||
return false
|
||||
end
|
||||
self.value = math.floor(value)
|
||||
logger.dbg("MockTime:set ", self.value)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -39,6 +48,7 @@ function MockTime:increase(value)
|
||||
return false
|
||||
end
|
||||
self.value = math.floor(self.value + value)
|
||||
logger.dbg("MockTime:increase ", self.value)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
52
spec/unit/mock_time_spec.lua
Normal file
52
spec/unit/mock_time_spec.lua
Normal file
@@ -0,0 +1,52 @@
|
||||
describe("MockTime tests", function()
|
||||
teardown(function()
|
||||
require("mock_time"):uninstall()
|
||||
end)
|
||||
|
||||
it("should be able to install and uninstall", function()
|
||||
local mock_time = require("mock_time")
|
||||
local util = require("ffi/util")
|
||||
local current_time = os.time()
|
||||
local current_highres_sec = util.gettime()
|
||||
mock_time:install()
|
||||
assert.is.truthy(mock_time:set(10))
|
||||
assert.are.equal(os.time(), 10)
|
||||
local sec, usec = util.gettime()
|
||||
assert.are.equal(sec, 10)
|
||||
assert.are.equal(usec, 0)
|
||||
mock_time:uninstall()
|
||||
assert.is.truthy(os.time() >= current_time)
|
||||
assert.is.truthy(util.gettime() >= current_highres_sec)
|
||||
end)
|
||||
|
||||
it("should be able to install several times", function()
|
||||
local mock_time = require("mock_time")
|
||||
local util = require("ffi/util")
|
||||
local current_time = os.time()
|
||||
local current_highres_sec = util.gettime()
|
||||
mock_time:install()
|
||||
mock_time:install()
|
||||
mock_time:uninstall()
|
||||
assert.is.truthy(os.time() >= current_time)
|
||||
assert.is.truthy(util.gettime() >= current_highres_sec)
|
||||
end)
|
||||
|
||||
it("should reject invalid value", function()
|
||||
local mock_time = require("mock_time")
|
||||
assert.is.falsy(mock_time:set("100"))
|
||||
assert.is.falsy(mock_time:set(true))
|
||||
assert.is.falsy(mock_time:set(nil))
|
||||
assert.is.falsy(mock_time:set(function() end))
|
||||
end)
|
||||
|
||||
it("should increase time", function()
|
||||
local mock_time = require("mock_time")
|
||||
local current_time = os.time()
|
||||
mock_time:install()
|
||||
assert.is.truthy(mock_time:set(10.1))
|
||||
assert.are.equal(os.time(), 10)
|
||||
mock_time:increase(1)
|
||||
assert.are.equal(os.time(), 11)
|
||||
mock_time:uninstall()
|
||||
end)
|
||||
end)
|
||||
@@ -4,13 +4,13 @@ describe("Readerfooter module", function()
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
DocSettings = require("docsettings")
|
||||
UIManager = require("ui/uimanager")
|
||||
MenuSorter = require("ui/menusorter")
|
||||
package.unloadAll()
|
||||
DEBUG = require("dbg")
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
DocSettings = require("docsettings")
|
||||
MenuSorter = require("ui/menusorter")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
UIManager = require("ui/uimanager")
|
||||
purgeDir = require("ffi/util").purgeDir
|
||||
Screen = require("device").screen
|
||||
|
||||
|
||||
@@ -2,13 +2,14 @@ describe("Readerhighlight module", function()
|
||||
local DocumentRegistry, ReaderUI, UIManager, Screen, Geom, dbg, Event
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
UIManager = require("ui/uimanager")
|
||||
Screen = require("device").screen
|
||||
Geom = require("ui/geometry")
|
||||
dbg = require("dbg")
|
||||
Event = require("ui/event")
|
||||
Geom = require("ui/geometry")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
Screen = require("device").screen
|
||||
UIManager = require("ui/uimanager")
|
||||
dbg = require("dbg")
|
||||
end)
|
||||
|
||||
local function highlight_single_word(readerui, pos0)
|
||||
|
||||
@@ -3,6 +3,7 @@ describe("ReaderLink module", function()
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
Event = require("ui/event")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
|
||||
@@ -3,6 +3,7 @@ describe("Readerview module", function()
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
package.unloadAll()
|
||||
DocumentRegistry = require("document/documentregistry")
|
||||
Blitbuffer = require("ffi/blitbuffer")
|
||||
ReaderUI = require("apps/reader/readerui")
|
||||
|
||||
@@ -164,35 +164,6 @@ describe("UIManager spec", function()
|
||||
assert.is_true(UIManager._task_queue_dirty)
|
||||
end)
|
||||
|
||||
it("should setup auto suspend on kobo", function()
|
||||
local old_reset_timer = UIManager._resetAutoSuspendTimer
|
||||
local noop = old_reset_timer
|
||||
assert.falsy(UIManager._startAutoSuspend)
|
||||
assert.falsy(UIManager._stopAutoSuspend)
|
||||
assert.truthy(old_reset_timer)
|
||||
G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 3600)
|
||||
|
||||
UIManager:run()
|
||||
UIManager:quit()
|
||||
-- should skip on non-kobo devices
|
||||
UIManager:_initAutoSuspend()
|
||||
assert.is.same(noop, UIManager._startAutoSuspend)
|
||||
assert.is.same(noop, UIManager._stopAutoSuspend)
|
||||
assert.truthy(old_reset_timer)
|
||||
assert.is.same(#UIManager._task_queue, 0)
|
||||
-- now test kobo devices
|
||||
local old_is_kobo = Device.isKobo
|
||||
Device.isKobo = function() return true end
|
||||
UIManager:_initAutoSuspend()
|
||||
assert.truthy(UIManager._startAutoSuspend)
|
||||
assert.truthy(UIManager._stopAutoSuspend)
|
||||
assert.is_not.same(UIManager._resetAutoSuspendTimer, old_reset_timer)
|
||||
assert.is.same(#UIManager._task_queue, 1)
|
||||
assert.is.same(UIManager._task_queue[1].action,
|
||||
UIManager.auto_suspend_action)
|
||||
Device.isKobo = old_is_kobo
|
||||
end)
|
||||
|
||||
it("should check active widgets in order", function()
|
||||
local call_signals = {false, false, false}
|
||||
UIManager._window_stack = {
|
||||
|
||||
Reference in New Issue
Block a user