From 66d2930e5e82b6ffa97bb10fd36382b8c802218a Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Sun, 13 Aug 2023 13:30:54 +0200 Subject: [PATCH] Revert "Tests: "Unbreak" some more tests" This reverts commit f4dd0f92b2e5b2c9b48b220dc53fdbd8502bed15. --- spec/unit/readerhighlight_spec.lua | 4 - spec/unit/readerlink_spec.lua | 92 ++++++++----------- spec/unit/readerpaging_spec.lua | 138 +++++++++++++---------------- 3 files changed, 100 insertions(+), 134 deletions(-) diff --git a/spec/unit/readerhighlight_spec.lua b/spec/unit/readerhighlight_spec.lua index cb826b050..7a3c47ceb 100644 --- a/spec/unit/readerhighlight_spec.lua +++ b/spec/unit/readerhighlight_spec.lua @@ -12,10 +12,6 @@ describe("Readerhighlight module", function() UIManager = require("ui/uimanager") end) - teardown(function() - UIManager:quit() - end) - local function highlight_single_word(pos0) local readerui = ReaderUI.instance local s = spy.on(readerui.languagesupport, "improveWordSelection") diff --git a/spec/unit/readerlink_spec.lua b/spec/unit/readerlink_spec.lua index 2ab3c28d1..f181c9d13 100644 --- a/spec/unit/readerlink_spec.lua +++ b/spec/unit/readerlink_spec.lua @@ -1,15 +1,6 @@ describe("ReaderLink module", function() local DocumentRegistry, ReaderUI, UIManager, sample_epub, sample_pdf, Event, Screen - local purgeSidecar = function() - local purgeDir = require("ffi/util").purgeDir - local removePath = require("util").removePath - local DocSettings = require("docsettings") - purgeDir(DocSettings:getSidecarDir(sample_pdf)) - removePath(DocSettings:getSidecarDir(sample_pdf)) - os.remove(DocSettings:getHistoryPath(sample_pdf)) - end - setup(function() require("commonrequire") package.unloadAll() @@ -21,29 +12,6 @@ describe("ReaderLink module", function() Screen = require("device").screen sample_epub = "spec/front/unit/data/leaves.epub" sample_pdf = "spec/front/unit/data/paper.pdf" - - purgeSidecar() - end) - - teardown(function() - purgeSidecar() - end) - - before_each(function() - -- Yes, out of order quits, because of the LinkBox mess - -- This is awful and doesn't make sense. - UIManager:quit() - -- This is yet another nonsensical hack to game the UI loop... - UIManager._exit_code = nil - end) - - after_each(function() - local readerui = ReaderUI.instance - - if readerui then - readerui:closeDocument() - readerui:onClose() - end end) it("should jump to links in epub #nocov", function() @@ -52,12 +20,15 @@ describe("ReaderLink module", function() document = DocumentRegistry:openDocument(sample_epub), } readerui.rolling:onGotoPage(5) - assert.is.same(5, readerui.rolling.current_page) - readerui.link:onTap(nil, {pos = {x = 255, y = 170}}) - assert.is.same(35, readerui.rolling.current_page) + readerui.link:onTap(nil, {pos = {x = 320, y = 190}}) + assert.is.same(37, readerui.rolling.current_page) + readerui:closeDocument() + readerui:onClose() end) - it("should jump to links in pdf page mode #nocov", function() + it("should jump to links in pdf page mode", function() + UIManager:quit() + UIManager._exit_code = nil local readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), @@ -65,15 +36,16 @@ describe("ReaderLink module", function() readerui:handleEvent(Event:new("SetScrollMode", false)) readerui:handleEvent(Event:new("SetZoomMode", "page")) readerui.paging:onGotoPage(1) - assert.is.same(1, readerui.paging.current_page) - readerui.link:onTap(nil, {pos = {x = 292, y = 584}}) - -- Outside of CRe, LinkBox only jumps after FOLLOW_LINK_TIMEOUT... We delay run to cadge that. Maybe. Sometimes. Somehow. - -- THIS TEST IS BROKEN AND DOESN'T MAKE SENSE. + readerui.link:onTap(nil, {pos = {x = 363, y = 565}}) UIManager:run() - assert.is.near(22, readerui.paging.current_page, 21) -- This *sometimes* doesn't work *at all*, hence the large range. + assert.is.same(22, readerui.paging.current_page) + readerui:closeDocument() + readerui:onClose() end) - it("should jump to links in pdf scroll mode #nocov", function() + it("should jump to links in pdf scroll mode", function() + UIManager:quit() + UIManager._exit_code = nil local readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), @@ -86,7 +58,10 @@ describe("ReaderLink module", function() UIManager:run() -- its really hard to get the exact page number in scroll mode -- page positions may have unexpected impact on page number - assert.is.near(22, readerui.paging.current_page, 2) + assert.truthy(readerui.paging.current_page == 21 + or readerui.paging.current_page == 20) + readerui:closeDocument() + readerui:onClose() end) it("should be able to go back after link jump in epub #nocov", function() @@ -95,15 +70,17 @@ describe("ReaderLink module", function() document = DocumentRegistry:openDocument(sample_epub), } readerui.rolling:onGotoPage(5) - assert.is.same(5, readerui.rolling.current_page) readerui.link:onTap(nil, {pos = {x = 320, y = 190}}) - UIManager:run() assert.is.same(37, readerui.rolling.current_page) readerui.link:onGoBackLink() assert.is.same(5, readerui.rolling.current_page) + readerui:closeDocument() + readerui:onClose() end) - it("should be able to go back after link jump in pdf page mode #nocov", function() + it("should be able to go back after link jump in pdf page mode", function() + UIManager:quit() + UIManager._exit_code = nil local readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), @@ -111,15 +88,18 @@ describe("ReaderLink module", function() readerui:handleEvent(Event:new("SetScrollMode", false)) readerui:handleEvent(Event:new("SetZoomMode", "page")) readerui.paging:onGotoPage(1) - assert.is.same(1, readerui.paging.current_page) - readerui.link:onTap(nil, {pos = {x = 228, y = 534}}) + readerui.link:onTap(nil, {pos = {x = 363, y = 565}}) UIManager:run() - assert.is.near(21, readerui.paging.current_page, 2) + assert.is.same(22, readerui.paging.current_page) readerui.link:onGoBackLink() assert.is.same(1, readerui.paging.current_page) + readerui:closeDocument() + readerui:onClose() end) - it("should be able to go back after link jump in pdf scroll mode #nocov", function() + it("should be able to go back after link jump in pdf scroll mode", function() + UIManager:quit() + UIManager._exit_code = nil local readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), @@ -128,15 +108,19 @@ describe("ReaderLink module", function() readerui:handleEvent(Event:new("SetZoomMode", "page")) readerui.paging:onGotoPage(1) assert.is.same(1, readerui.paging.current_page) - assert.is.same(1, readerui.paging.current_page) readerui.link:onTap(nil, {pos = {x = 228, y = 534}}) UIManager:run() - assert.is.near(22, readerui.paging.current_page, 2) + assert.truthy(readerui.paging.current_page == 21 + or readerui.paging.current_page == 20) readerui.link:onGoBackLink() assert.is.same(1, readerui.paging.current_page) + readerui:closeDocument() + readerui:onClose() end) - it("should be able to go back to the same position after link jump in pdf scroll mode #nocov", function() + it("should be able to go back to the same position after link jump in pdf scroll mode", function() + UIManager:quit() + UIManager._exit_code = nil local expected_page_states = { { gamma = 1, @@ -208,5 +192,7 @@ describe("ReaderLink module", function() readerui.link:onGoBackLink() assert.is.same(3, readerui.paging.current_page) assert.are.same(expected_page_states, readerui.view.page_states) + readerui:closeDocument() + readerui:onClose() end) end) diff --git a/spec/unit/readerpaging_spec.lua b/spec/unit/readerpaging_spec.lua index c66464ca5..0d61e9f71 100644 --- a/spec/unit/readerpaging_spec.lua +++ b/spec/unit/readerpaging_spec.lua @@ -1,7 +1,7 @@ describe("Readerpaging module", function() local sample_pdf = "spec/front/unit/data/sample.pdf" - local sample_djvu = "spec/front/unit/data/djvu3spec.djvu" - local UIManager, Event, DocumentRegistry, ReaderUI, Screen + local readerui, UIManager, Event, DocumentRegistry, ReaderUI, Screen + local paging setup(function() require("commonrequire") @@ -12,132 +12,116 @@ describe("Readerpaging module", function() DocumentRegistry = require("document/documentregistry") ReaderUI = require("apps/reader/readerui") Screen = require("device").screen - - local purgeDir = require("ffi/util").purgeDir - local DocSettings = require("docsettings") - purgeDir(DocSettings:getSidecarDir(sample_pdf)) - os.remove(DocSettings:getHistoryPath(sample_pdf)) end) - describe("Page mode on a PDF", function() - before_each(function() - local readerui = ReaderUI:new{ + describe("Page mode", function() + setup(function() + readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), } - - UIManager:show(readerui) + paging = readerui.paging end) - after_each(function() - local readerui = ReaderUI.instance - - if readerui then - readerui:closeDocument() - readerui:onClose() - end + teardown(function() + readerui:closeDocument() + readerui:onClose() end) it("should emit EndOfBook event at the end", function() - local readerui = ReaderUI.instance - local paging = readerui.paging - - local s = spy.on(readerui.status, "onEndOfBook") - + UIManager:quit() + UIManager:show(readerui) UIManager:nextTick(function() - UIManager:quit() + UIManager:close(readerui) + -- We haven't torn it down yet + ReaderUI.instance = readerui end) UIManager:run() readerui:handleEvent(Event:new("SetScrollMode", false)) readerui.zooming:setZoomMode("pageheight") paging:onGotoPage(readerui.document:getPageCount()) + local called = false + readerui.onEndOfBook = function() + called = true + end paging:onGotoViewRel(1) - assert.spy(s).was_called() + assert.is.truthy(called) + readerui.onEndOfBook = nil + UIManager:quit() end) end) - describe("Scroll mode on a PDF", function() + describe("Scroll mode", function() setup(function() local purgeDir = require("ffi/util").purgeDir local DocSettings = require("docsettings") purgeDir(DocSettings:getSidecarDir(sample_pdf)) os.remove(DocSettings:getHistoryPath(sample_pdf)) - end) - before_each(function() - local readerui = ReaderUI:new{ + + readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), } - - UIManager:show(readerui) + paging = readerui.paging end) - after_each(function() - local readerui = ReaderUI.instance - - if readerui then - readerui:closeDocument() - readerui:onClose() - end + teardown(function() + readerui:closeDocument() + readerui:onClose() end) it("should emit EndOfBook event at the end", function() - local readerui = ReaderUI.instance - local paging = readerui.paging - - local s = spy.on(readerui.status, "onEndOfBook") - + UIManager:quit() + UIManager:show(readerui) UIManager:nextTick(function() - UIManager:quit() + UIManager:close(readerui) + -- We haven't torn it down yet + ReaderUI.instance = readerui end) UIManager:run() paging.page_positions = {} readerui:handleEvent(Event:new("SetScrollMode", true)) paging:onGotoPage(readerui.document:getPageCount()) readerui.zooming:setZoomMode("pageheight") + local called = false + readerui.onEndOfBook = function() + called = true + end paging:onGotoViewRel(1) paging:onGotoViewRel(1) - assert.spy(s).was_called() + assert.is.truthy(called) + readerui.onEndOfBook = nil + UIManager:quit() end) - end) - describe("Scroll mode on a DjVu", function() - setup(function() - local purgeDir = require("ffi/util").purgeDir - local DocSettings = require("docsettings") - purgeDir(DocSettings:getSidecarDir(sample_djvu)) - os.remove(DocSettings:getHistoryPath(sample_djvu)) - end) - before_each(function() - local readerui = ReaderUI:new{ + it("should scroll backward on the first page without crash", function() + local sample_djvu = "spec/front/unit/data/djvu3spec.djvu" + -- Unsafe second // ReaderUI instance! + local tmp_readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_djvu), } - - UIManager:show(readerui) - end) - after_each(function() - local readerui = ReaderUI.instance - - if readerui then - readerui:closeDocument() - readerui:onClose() - end - end) - - it("should scroll backward on the first page without crash", function() - local readerui = ReaderUI.instance - local paging = readerui.paging - - paging:onScrollPanRel(-100) + tmp_readerui.paging:onScrollPanRel(-100) + tmp_readerui:closeDocument() + tmp_readerui:onClose() + -- Restore the ref to the original ReaderUI instance + ReaderUI.instance = readerui end) it("should scroll forward on the last page without crash", function() - local readerui = ReaderUI.instance - local paging = readerui.paging - - paging:onGotoPage(readerui.document:getPageCount()) + local sample_djvu = "spec/front/unit/data/djvu3spec.djvu" + -- Unsafe second // ReaderUI instance! + local tmp_readerui = ReaderUI:new{ + dimen = Screen:getSize(), + document = DocumentRegistry:openDocument(sample_djvu), + } + paging = tmp_readerui.paging + paging:onGotoPage(tmp_readerui.document:getPageCount()) paging:onScrollPanRel(120) paging:onScrollPanRel(-1) paging:onScrollPanRel(120) + tmp_readerui:closeDocument() + tmp_readerui:onClose() + -- Restore the ref to the original ReaderUI instance + ReaderUI.instance = readerui end) end) end)