From 572412b1f6686117352020c0eac75e85f2c7604f Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Wed, 20 Apr 2016 21:41:34 -0700 Subject: [PATCH] readerrolling(fix): detect end of book properly --- .../apps/reader/modules/readerrolling.lua | 16 +++---- spec/unit/readerrolling_spec.lua | 44 ++++++++++++++++++- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/frontend/apps/reader/modules/readerrolling.lua b/frontend/apps/reader/modules/readerrolling.lua index b4a08c0c1..271f1624e 100644 --- a/frontend/apps/reader/modules/readerrolling.lua +++ b/frontend/apps/reader/modules/readerrolling.lua @@ -357,11 +357,6 @@ end function ReaderRolling:onGotoViewRel(diff) DEBUG("goto relative screen:", diff, ", in mode: ", self.view.view_mode) - local prev_xp - -- save xpointer to check whether we reach the end of the book - if diff > 0 then - prev_xp = self.xpointer - end if self.view.view_mode == "scroll" then local pan_diff = diff * self.ui.dimen.h if self.show_overlap_enable then @@ -371,15 +366,20 @@ function ReaderRolling:onGotoViewRel(diff) pan_diff = pan_diff + self.overlap end end + local old_pos = self.current_pos self:_gotoPos(self.current_pos + pan_diff) + if diff > 0 and old_pos == self.current_pos then + self.ui:handleEvent(Event:new("EndOfBook")) + end elseif self.view.view_mode == "page" then local page_count = self.ui.document:getVisiblePageCount() + local old_page = self.current_page self:_gotoPage(self.current_page + diff*page_count) + if diff > 0 and old_page == self.current_page then + self.ui:handleEvent(Event:new("EndOfBook")) + end end self.xpointer = self.ui.document:getXPointer() - if self.xpointer == prev_xp then - self.ui:handleEvent(Event:new("EndOfBook")) - end return true end diff --git a/spec/unit/readerrolling_spec.lua b/spec/unit/readerrolling_spec.lua index d9d2a444a..5d95bb623 100644 --- a/spec/unit/readerrolling_spec.lua +++ b/spec/unit/readerrolling_spec.lua @@ -51,18 +51,57 @@ describe("Readerrolling module", function() assert.are.same(toc:getPreviousChapter(i, 0), rolling.current_page) end end) - it("should emit EndOfBook event at the end", function() - rolling:onGotoPage(readerui.document:getPageCount()) + it("should emit EndOfBook event at the end of sample epub", function() local called = false readerui.onEndOfBook = function() called = true end + -- check beginning of the book + rolling:onGotoPage(1) + assert.is.falsy(called) + rolling:onGotoViewRel(-1) + rolling:onGotoViewRel(-1) + assert.is.falsy(called) + -- check end of the book + rolling:onGotoPage(readerui.document:getPageCount()) + assert.is.falsy(called) rolling:onGotoViewRel(1) + assert.is.truthy(called) rolling:onGotoViewRel(1) assert.is.truthy(called) readerui.onEndOfBook = nil end) + + it("should emit EndOfBook event at the end sample txt", function() + local sample_txt = "spec/front/unit/data/sample.txt" + local txt_readerui = ReaderUI:new{ + document = DocumentRegistry:openDocument(sample_txt), + } + local called = false + txt_readerui.onEndOfBook = function() + called = true + end + local txt_rolling = txt_readerui.rolling + -- check beginning of the book + txt_rolling:onGotoPage(1) + assert.is.falsy(called) + txt_rolling:onGotoViewRel(-1) + txt_rolling:onGotoViewRel(-1) + assert.is.falsy(called) + -- not at the end of the book + txt_rolling:onGotoPage(3) + assert.is.falsy(called) + txt_rolling:onGotoViewRel(1) + assert.is.falsy(called) + -- at the end of the book + txt_rolling:onGotoPage(txt_readerui.document:getPageCount()) + assert.is.falsy(called) + txt_rolling:onGotoViewRel(1) + assert.is.truthy(called) + readerui.onEndOfBook = nil + end) end) + describe("test in landscape screen mode", function() it("should go to landscape screen mode", function() readerui:handleEvent(Event:new("ChangeScreenMode", "landscape")) @@ -110,6 +149,7 @@ describe("Readerrolling module", function() readerui.onEndOfBook = nil end) end) + describe("switching screen mode should not change current page number", function() it("for portrait-landscape-portrait switching", function() for i = 80, 100, 10 do