2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/spec/unit/autosuspend_spec.lua
Frans de Jonge e257c4e45e
[feat, Kobo] Autoshutdown (#5335)
The methods used here will likely work on most embedded devices, which is why I put them in their own WakeupMgr interface/scheduler module, separate from Kobo.

See https://www.mobileread.com/forums/showthread.php?p=3886403#post3886403 for more context.

Fixes #3806.
2019-09-12 14:15:08 +02:00

121 lines
4.7 KiB
Lua

describe("AutoSuspend", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("document/canvascontext"):init(require("device"))
end)
describe("suspend", function()
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() --luacheck: ignore
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 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)
describe("shutdown", function()
--- @todo duplicate with above, elegant way to DRY?
before_each(function()
local Device = require("device")
stub(Device, "isKobo")
Device.isKobo.returns(true)
stub(Device, "canPowerOff")
Device.canPowerOff.returns(true)
Device.input.waitEvent = function() end
local UIManager = require("ui/uimanager")
stub(UIManager, "poweroff_action")
UIManager._run_forever = true
G_reader_settings:saveSetting("autoshutdown_timeout_seconds", 10)
require("mock_time"):install()
end)
after_each(function()
require("device").isKobo:revert()
require("ui/uimanager").poweroff_action:revert()
G_reader_settings:delSetting("autoshutdown_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() --luacheck: ignore
local UIManager = require("ui/uimanager")
mock_time:increase(5)
UIManager:handleInput()
assert.stub(UIManager.poweroff_action).was.called(0)
mock_time:increase(6)
UIManager:handleInput()
assert.stub(UIManager.poweroff_action).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.poweroff_action).was.called(0)
widget:onInputEvent()
widget:onSuspend()
widget:onResume()
mock_time:increase(6)
UIManager:handleInput()
assert.stub(UIManager.poweroff_action).was.called(0)
mock_time:increase(5)
UIManager:handleInput()
assert.stub(UIManager.poweroff_action).was.called(1)
mock_time:uninstall()
end)
end)
end)