Files
koreader/spec/unit/batterystat_spec.lua
Hzj_jie a8513c95b6 Several minor fixes (#3057)
1. Android.getScreenBrightness() is used in frontend/device/android/powerd.lua to retrieve the frontlight intensity.
2. kosync.koplugin won't save settings for whisper sync.
3. batterstat.koplugin won't work correctly if several on* events are fired.
2017-08-08 08:29:57 +02:00

186 lines
6.5 KiB
Lua

describe("BatteryState plugin tests", function()
local MockTime, module
local stat = function()
return module:new():stat()
end,
setup(function()
require("commonrequire")
package.unloadAll()
MockTime = require("mock_time")
MockTime:install()
end)
teardown(function()
MockTime:uninstall()
package.unloadAll()
end)
before_each(function()
module = dofile("plugins/batterystat.koplugin/main.lua")
end)
it("should record charging time", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onNotCharging()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & discharging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
end)
it("should record suspending time", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onResume()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
it("should not swap the state when several charging events fired", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
-- awake & charging time should be reset.
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(1, widget.charging.time)
widget:onCharging()
assert.is_true(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(2, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(2, widget.charging.time)
end)
it("should not swap the state when several suspending events fired", function()
local widget = stat()
assert.is_false(widget.was_charging)
assert.is_false(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(0, widget.sleeping.time)
assert.are.equal(1, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(1, widget.sleeping.time)
assert.are.equal(2, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(2, widget.sleeping.time)
assert.are.equal(3, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
widget:onSuspend()
assert.is_false(widget.was_charging)
assert.is_true(widget.was_suspending)
MockTime:increase(1)
widget:accumulate()
assert.are.equal(1, widget.awake.time)
assert.are.equal(3, widget.sleeping.time)
assert.are.equal(4, widget.discharging.time)
assert.are.equal(0, widget.charging.time)
end)
end)