SwitchPlugin and BackgroundTaskPlugin with tests (#3137)

This commit is contained in:
Hzj_jie
2017-08-31 07:37:39 -07:00
committed by Frans de Jonge
parent 199b6aba51
commit 1ff46a67b4
5 changed files with 451 additions and 1 deletions

View File

@@ -0,0 +1,139 @@
describe("BackgroundTaskPlugin", function()
require("commonrequire")
local BackgroundTaskPlugin = require("ui/plugin/background_task_plugin")
local MockTime = require("mock_time")
local UIManager = require("ui/uimanager")
setup(function()
MockTime:install()
local Device = require("device")
Device.input.waitEvent = function() end
UIManager._run_forever = true
requireBackgroundRunner()
end)
teardown(function()
MockTime:uninstall()
package.unloadAll()
stopBackgroundRunner()
end)
local createTestPlugin = function(executable)
return BackgroundTaskPlugin:new({
name = "test_plugin",
default_enable = true,
when = 2,
executable = executable,
})
end
local TestPlugin2 = BackgroundTaskPlugin:extend()
function TestPlugin2:new(o)
o = o or {}
o.name = "test_plugin2"
o.default_enable = true
o.when = 2
o.executed = 0
o.executable = function()
o.executed = o.executed + 1
end
o = BackgroundTaskPlugin.new(self, o)
return o
end
it("should be able to create a plugin", function()
local executed = 0
local test_plugin = createTestPlugin(function()
executed = executed + 1
end)
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, executed)
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, executed) -- The last job is still pending.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, executed)
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, executed) -- The new job has just been inserted.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(4, executed)
-- Fake a settings_id increment.
test_plugin:_init()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(5, executed) -- The job is from last settings_id.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(5, executed) -- The new job has just been inserted.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(6, executed) -- The job is from current settings_id.
-- Ensure test_plugin is stopped.
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
end)
it("should be able to create a derived plugin", function()
local test_plugin = TestPlugin2:new()
MockTime:increase(2)
UIManager:handleInput()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(1, test_plugin.executed)
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(2, test_plugin.executed)
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, test_plugin.executed) -- The last job is still pending.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, test_plugin.executed)
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(3, test_plugin.executed) -- The new job has just been inserted.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(4, test_plugin.executed)
-- Fake a settings_id increment.
test_plugin:_init()
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(5, test_plugin.executed) -- The job is from last settings_id.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(5, test_plugin.executed) -- The new job has just been inserted.
MockTime:increase(2)
UIManager:handleInput()
assert.are.equal(6, test_plugin.executed) -- The job is from current settings_id.
-- Ensure test_plugin is stopped.
test_plugin:flipSetting()
MockTime:increase(2)
UIManager:handleInput()
end)
end)