2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/spec/unit/mock_time.lua
Hzj_jie 53eb4dee50 AutoFrontlight plugin (#2941)
* Add AutoFrontlight plugin

* Add configuration to control autofrontlight feature
2017-06-14 19:32:16 +02:00

46 lines
1.0 KiB
Lua

local MockTime = {
original_os_time = os.time,
original_util_time = nil,
value = os.time(),
}
function MockTime:install()
assert(self ~= nil)
local util = require("ffi/util")
if self.original_util_time == nil then
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
end
function MockTime:uninstall()
assert(self ~= nil)
local util = require("ffi/util")
os.time = self.original_os_time
if self.original_util_time ~= nil then
util.gettime = self.original_util_time
end
end
function MockTime:set(value)
assert(self ~= nil)
if type(value) ~= "number" then
return false
end
self.value = math.floor(value)
return true
end
function MockTime:increase(value)
assert(self ~= nil)
if type(value) ~= "number" then
return false
end
self.value = math.floor(self.value + value)
return true
end
return MockTime