From 144fd170f1feca3bbcd04e3b0748542360251bdd Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Tue, 29 Mar 2016 00:56:29 -0700 Subject: [PATCH] uimanager(refactor): replace autosuspend if check with noop --- .ci/script.sh | 2 +- .luacheckrc | 2 ++ Makefile | 9 ++++-- frontend/ui/uimanager.lua | 55 +++++++++++++++++++++--------------- spec/unit/uimanager_spec.lua | 42 +++++++++++++++++++++++---- 5 files changed, 77 insertions(+), 33 deletions(-) diff --git a/.ci/script.sh b/.ci/script.sh index a5233bad1..196cec7cd 100755 --- a/.ci/script.sh +++ b/.ci/script.sh @@ -8,4 +8,4 @@ make all retry_cmd 6 make testfront set +o pipefail luajit $(which luacheck) --no-color -q frontend | tee ./luacheck.out -test $(grep Total ./luacheck.out | awk '{print $2}') -le 63 +test $(grep Total ./luacheck.out | awk '{print $2}') -le 59 diff --git a/.luacheckrc b/.luacheckrc index ab850f9bb..729129542 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,5 +1,7 @@ unused_args = false std = "luajit" +-- ignore implicit self +self = false globals = { "G_reader_settings", diff --git a/Makefile b/Makefile index 3525f14b4..ef61be234 100644 --- a/Makefile +++ b/Makefile @@ -340,9 +340,12 @@ po: $(MAKE) -i -C l10n bootstrap pull static-check: - @if which luacheck > /dev/null; then luacheck -q frontend; else \ - echo "[!] luacheck not found. "\ - "you can install it with 'luarocks install luacheck'"; fi + @if which luacheck > /dev/null; then \ + luacheck -q frontend; \ + else \ + echo "[!] luacheck not found. "\ + "you can install it with 'luarocks install luacheck'"; \ + fi doc: make -C doc diff --git a/frontend/ui/uimanager.lua b/frontend/ui/uimanager.lua index 4e8a23f94..af850fec9 100644 --- a/frontend/ui/uimanager.lua +++ b/frontend/ui/uimanager.lua @@ -7,6 +7,7 @@ local util = require("ffi/util") local dbg = require("dbg") local _ = require("gettext") +local noop = function() end local MILLION = 1000000 -- there is only one instance of this @@ -46,17 +47,13 @@ function UIManager:init() -- resume. self:_initAutoSuspend() self.event_handlers["Suspend"] = function(input_event) - if self:_autoSuspendEnabled() then - self:unschedule(self.auto_suspend_action) - end + self:_stopAutoSuspend() Device:onPowerEvent(input_event) end self.event_handlers["Resume"] = function(input_event) Device:onPowerEvent(input_event) self:sendEvent(Event:new("Resume")) - if self:_autoSuspendEnabled() then - self:_startAutoSuspend() - end + self:_startAutoSuspend() end self.event_handlers["Light"] = function() Device:getPowerDevice():toggleFrontlight() @@ -585,9 +582,7 @@ function UIManager:handleInput() -- delegate input_event to handler if input_event then - if self:_autoSuspendEnabled() then - self.last_action_sec = util.gettime() - end + self:_resetAutoSuspendTimer() local handler = self.event_handlers[input_event] if handler then handler(input_event) @@ -647,6 +642,10 @@ end -- Kobo does not have an auto suspend function, so we implement it ourselves. function UIManager:_initAutoSuspend() + local function isAutoSuspendEnabled() + return Device:isKobo() and self.auto_suspend_sec > 0 + end + local sec = G_reader_settings:readSetting("auto_suspend_timeout_seconds") if sec then self.auto_suspend_sec = sec @@ -654,7 +653,8 @@ function UIManager:_initAutoSuspend() -- default setting is 60 minutes self.auto_suspend_sec = 60 * 60 end - if self:_autoSuspendEnabled() then + + if isAutoSuspendEnabled() then self.auto_suspend_action = function() local now = util.gettime() -- Do not repeat auto suspend procedure after suspend. @@ -666,23 +666,32 @@ function UIManager:_initAutoSuspend() self.auto_suspend_action) end end + + function UIManager:_startAutoSuspend() + self.last_action_sec = util.gettime() + self:nextTick(self.auto_suspend_action) + end + dbg:guard(UIManager, '_startAutoSuspend', + function() + assert(isAutoSuspendEnabled()) + end) + + function UIManager:_stopAutoSuspend() + self:unschedule(self.auto_suspend_action) + end + + function UIManager:_resetAutoSuspendTimer() + self.last_action_sec = util.gettime() + end + self:_startAutoSuspend() + else + self._startAutoSuspend = noop + self._stopAutoSuspend = noop end end -function UIManager:_startAutoSuspend() - self.last_action_sec = util.gettime() - self:nextTick(self.auto_suspend_action) -end -dbg:guard(UIManager, '_startAutoSuspend', - function(self) - assert(self:_autoSuspendEnabled()) - end) - -function UIManager:_autoSuspendEnabled() - return Device:isKobo() and self.auto_suspend_sec > 0 -end +UIManager._resetAutoSuspendTimer = noop UIManager:init() return UIManager - diff --git a/spec/unit/uimanager_spec.lua b/spec/unit/uimanager_spec.lua index b36c9c7da..e74ed26ba 100644 --- a/spec/unit/uimanager_spec.lua +++ b/spec/unit/uimanager_spec.lua @@ -1,12 +1,14 @@ -require("commonrequire") -local util = require("ffi/util") -local UIManager = require("ui/uimanager") -local DEBUG = require("dbg") -DEBUG:turnOn() - describe("UIManager spec", function() + local Device, UIManager, util local noop = function() end + setup(function() + require("commonrequire") + util = require("ffi/util") + UIManager = require("ui/uimanager") + Device = require("device") + end) + it("should consume due tasks", function() local now = { util.gettime() } local future = { now[1] + 60000, now[2] } @@ -161,4 +163,32 @@ describe("UIManager spec", function() UIManager:_checkTasks() assert.is_true(UIManager._task_queue_dirty) end) + + it("should setup auto suspend on kobo", function() + local old_reset_timer = UIManager._resetAutoSuspendTimer + local noop = old_reset_timer + assert.falsy(UIManager._startAutoSuspend) + assert.falsy(UIManager._stopAutoSuspend) + assert.truthy(old_reset_timer) + G_reader_settings:saveSetting("auto_suspend_timeout_seconds", 3600) + + UIManager:quit() + -- should skip on non-kobo devices + UIManager:_initAutoSuspend() + assert.is.same(noop, UIManager._startAutoSuspend) + assert.is.same(noop, UIManager._stopAutoSuspend) + assert.truthy(old_reset_timer) + assert.is.same(#UIManager._task_queue, 0) + -- now test kobo devices + local old_is_kobo = Device.isKobo + Device.isKobo = function() return true end + UIManager:_initAutoSuspend() + assert.truthy(UIManager._startAutoSuspend) + assert.truthy(UIManager._stopAutoSuspend) + assert.is_not.same(UIManager._resetAutoSuspendTimer, old_reset_timer) + assert.is.same(#UIManager._task_queue, 1) + assert.is.same(UIManager._task_queue[1].action, + UIManager.auto_suspend_action) + Device.isKobo = old_is_kobo + end) end)