2017-08-16 15:15:12 +00:00
|
|
|
describe("BatteryState plugin tests #nocov", function()
|
2022-06-27 19:16:51 +00:00
|
|
|
local MockTime, module, time
|
2017-08-08 06:29:57 +00:00
|
|
|
|
2017-08-08 20:35:40 +00:00
|
|
|
local stat = function() --luacheck: ignore
|
2017-08-08 06:29:57 +00:00
|
|
|
return module:new():stat()
|
|
|
|
end,
|
|
|
|
|
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
package.unloadAll()
|
2019-02-18 16:01:00 +00:00
|
|
|
require("document/canvascontext"):init(require("device"))
|
2022-06-27 19:16:51 +00:00
|
|
|
time = require("ui/time")
|
2017-08-08 06:29:57 +00:00
|
|
|
MockTime = require("mock_time")
|
|
|
|
MockTime:install()
|
|
|
|
end)
|
|
|
|
|
|
|
|
teardown(function()
|
|
|
|
MockTime:uninstall()
|
|
|
|
package.unloadAll()
|
2019-02-18 16:01:00 +00:00
|
|
|
require("document/canvascontext"):init(require("device"))
|
2017-08-08 06:29:57 +00:00
|
|
|
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)
|
2018-06-23 22:08:11 +00:00
|
|
|
widget:resetAll()
|
2017-08-08 06:29:57 +00:00
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
-- Awake charging & discharging time should be reset.
|
|
|
|
assert.are.equal(0, widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
assert.are.equal(0, widget.discharging.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.charging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
|
|
|
|
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.
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.discharging.time)
|
|
|
|
assert.are.equal(time.s(1), widget.charging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
|
|
|
|
widget:onCharging()
|
|
|
|
assert.is_true(widget.was_charging)
|
|
|
|
assert.is_false(widget.was_suspending)
|
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
-- Awake charging & discharging time should be reset.
|
|
|
|
assert.are.equal(0, widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
assert.are.equal(0, widget.discharging.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.charging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
it("should record suspending time", function()
|
|
|
|
local widget = stat()
|
|
|
|
assert.is_false(widget.was_charging)
|
|
|
|
assert.is_false(widget.was_suspending)
|
2018-06-23 22:08:11 +00:00
|
|
|
widget:resetAll()
|
2017-08-08 06:29:57 +00:00
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(1), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(2), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(2), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(1), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(3), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(2), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(2), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(4), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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)
|
2018-06-23 22:08:11 +00:00
|
|
|
widget:resetAll()
|
2017-08-08 06:29:57 +00:00
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
-- Awake charging & discharging time should be reset.
|
|
|
|
assert.are.equal(0, widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
assert.are.equal(0, widget.discharging.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.charging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
|
|
|
|
widget:onCharging()
|
|
|
|
assert.is_true(widget.was_charging)
|
|
|
|
assert.is_false(widget.was_suspending)
|
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
assert.are.equal(0, widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
Battery stats plugin: fix and improvements (#5626)
- fix incorrectly shown awake, sleeping, charging and discharging,
- remove unneeded debug mode and logging to external file,
- prevent showing values like inf, -inf, nan in estimated times
(we now show "n/a"), and values below zero,
- show extra confirm box when we want to reset data,
- show time in format xxhxxm instead of only pure minutes,
- check at initialization that the device was charging when it was
turned off (battery level larger than at the time of exit).
2019-11-26 13:21:14 +00:00
|
|
|
assert.are.equal(0, widget.discharging.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(2), widget.charging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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)
|
2018-06-23 22:08:11 +00:00
|
|
|
widget:resetAll()
|
2017-08-08 06:29:57 +00:00
|
|
|
MockTime:increase(1)
|
|
|
|
widget:accumulate()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.sleeping.time)
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(1), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(2), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(2), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(3), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
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()
|
2022-06-27 19:16:51 +00:00
|
|
|
assert.are.equal(time.s(1), widget.awake.time)
|
|
|
|
assert.are.equal(time.s(3), widget.sleeping.time)
|
|
|
|
assert.are.equal(time.s(4), widget.discharging.time)
|
2017-08-08 06:29:57 +00:00
|
|
|
assert.are.equal(0, widget.charging.time)
|
|
|
|
end)
|
|
|
|
end)
|