diff --git a/frontend/device/gesturedetector.lua b/frontend/device/gesturedetector.lua index 73ae15de1..0824118b3 100644 --- a/frontend/device/gesturedetector.lua +++ b/frontend/device/gesturedetector.lua @@ -195,7 +195,7 @@ end function GestureDetector:isSwipe(slot) if not self.first_tevs[slot] or not self.last_tevs[slot] then return end - local tv_diff = self.first_tevs[slot].timev - self.last_tevs[slot].timev + local tv_diff = self.last_tevs[slot].timev - self.first_tevs[slot].timev if (tv_diff.sec == 0) and (tv_diff.usec < self.SWIPE_INTERVAL) then local x_diff = self.last_tevs[slot].x - self.first_tevs[slot].x local y_diff = self.last_tevs[slot].y - self.first_tevs[slot].y diff --git a/frontend/ui/timeval.lua b/frontend/ui/timeval.lua index 1c4cb171b..4394f6dd0 100644 --- a/frontend/ui/timeval.lua +++ b/frontend/ui/timeval.lua @@ -13,6 +13,7 @@ A simple module to module to compare and do arithmetic with time values. local tv_duration_seconds_float = tv_duration.sec + tv_duration.usec/1000000 ]] +local dbg = require("dbg") local util = require("ffi/util") --[[-- @@ -110,6 +111,12 @@ function TimeVal:__sub(time_b) return diff end +dbg:guard(TimeVal, '__sub', + function(self, time_b) + assert(self.sec > time_b.sec or (self.sec == time_b.sec and self.usec >= time_b.usec), + "Subtract the first timeval from the latest, not vice versa.") + end) + function TimeVal:__add(time_b) local sum = TimeVal:new{} diff --git a/spec/unit/timeval_spec.lua b/spec/unit/timeval_spec.lua new file mode 100644 index 000000000..f21cf3cbe --- /dev/null +++ b/spec/unit/timeval_spec.lua @@ -0,0 +1,34 @@ +describe("TimeVal module", function() + local TimeVal, dbg, dbg_on + setup(function() + require("commonrequire") + TimeVal = require("ui/timeval") + dbg = require("dbg") + dbg_on = dbg.is_on + end) + + after_each(function() + if dbg_on then + dbg:turnOn() + else + dbg:turnOff() + end + end) + + it("should subtract", function() + local timev1 = TimeVal:new{ sec = 5, usec = 5000} + local timev2 = TimeVal:new{ sec = 10, usec = 6000} + + assert.is.same({sec = 5,usec = 1000}, timev2 - timev1) + assert.is.same({sec = -5,usec = -1000}, timev1 - timev2) + end) + + it("should guard against reverse subtraction logic", function() + dbg:turnOn() + TimeVal = package.reload("ui/timeval") + local timev1 = TimeVal:new{ sec = 5, usec = 5000} + local timev2 = TimeVal:new{ sec = 10, usec = 5000} + + assert.has.errors(function() return timev1 - timev2 end) + end) +end)