2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/spec/unit/autofrontlight_spec.lua
NiLuJe bf6c0cdd6c
LuaSettings: Add a method to initialize a setting properly (#7371)
* LuaSettings/DocSettings: Updated readSetting API to allow proper initialization to default.
Use it to initialize tables, e.g., fixing corner-cases in readerFooter that could prevent settings from being saved.
(Fixes an issue reported on Gitter).
* LuaSettings/DocSettings: Add simpler API than the the flip* ones to toggle boolean settings.
* Update LuaSettings/DocSettigns usage throughout the codebase to use the dedicated boolean methods wher appropriate, and clean up some of the more mind-bending uses.
* FileChooser: Implement an extended default exclusion list (fix #2360)
* ScreenSaver: Refactor to avoid the pile of kludges this was threatening to become. Code should be easier to follow and use, and fallbacks now behave as expected (fix #4418).
2021-03-06 22:44:18 +01:00

144 lines
4.8 KiB
Lua

describe("AutoFrontlight widget tests", function()
local Device, PowerD, MockTime, class, AutoFrontlight, UIManager
setup(function()
require("commonrequire")
package.unloadAll()
require("document/canvascontext"):init(require("device"))
MockTime = require("mock_time")
MockTime:install()
PowerD = require("device/generic/powerd"):new{
frontlight = 0,
}
PowerD.frontlightIntensityHW = function()
return 2
end
PowerD.setIntensityHW = function(self, intensity)
self.frontlight = intensity
end
end)
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("document/canvascontext"):init(require("device"))
end)
before_each(function()
Device = require("device")
Device.isKindle = function() return true end
Device.model = "KindleVoyage"
Device.brightness = 0
Device.hasFrontlight = function() return true end
Device.powerd = PowerD:new{
device = Device,
}
Device.ambientBrightnessLevel = function(self)
return self.brightness
end
Device.input.waitEvent = function() end
require("luasettings"):
open(require("datastorage"):getSettingsDir() .. "/autofrontlight.lua"):
saveSetting("enable", true):
close()
UIManager = require("ui/uimanager")
UIManager._run_forever = true
requireBackgroundRunner()
class = dofile("plugins/autofrontlight.koplugin/main.lua")
notifyBackgroundJobsUpdated()
-- Ensure the background runner has succeeded set the job.insert_sec.
MockTime:increase(2)
UIManager:handleInput()
end)
after_each(function()
AutoFrontlight:deprecateLastTask()
-- Ensure the scheduled task from this test case won't impact others.
MockTime:increase(2)
UIManager:handleInput()
AutoFrontlight = nil
stopBackgroundRunner()
end)
it("should automatically turn on or off frontlight", function()
AutoFrontlight = class:new()
Device.brightness = 3
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
Device.brightness = 0
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
Device.brightness = 1
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
Device.brightness = 2
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
Device.brightness = 3
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
Device.brightness = 4
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
Device.brightness = 3
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
Device.brightness = 2
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
Device.brightness = 1
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
Device.brightness = 0
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
end)
it("should turn on frontlight at the begining", function()
Device:getPowerDevice():turnOffFrontlight()
Device.brightness = 0
AutoFrontlight = class:new()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
end)
it("should turn off frontlight at the begining", function()
Device:getPowerDevice():turnOnFrontlight()
Device.brightness = 3
AutoFrontlight = class:new()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(0, Device:getPowerDevice().frontlight)
end)
it("should handle configuration update", function()
Device:getPowerDevice():turnOffFrontlight()
Device.brightness = 0
AutoFrontlight = class:new()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
AutoFrontlight:flipSetting()
Device.brightness = 3
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, Device:getPowerDevice().frontlight)
end)
end)