From 5bf1464f09ee9c9ede18153f7319e36b3cf77e77 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Wed, 25 Dec 2024 15:46:11 +0100 Subject: [PATCH] tests: cleanup readerrolling tests Fix error trace: "ReaderUI instance mismatch". --- spec/unit/readerrolling_spec.lua | 46 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/spec/unit/readerrolling_spec.lua b/spec/unit/readerrolling_spec.lua index 3a60e7c05..436fbd669 100644 --- a/spec/unit/readerrolling_spec.lua +++ b/spec/unit/readerrolling_spec.lua @@ -21,6 +21,10 @@ describe("Readerrolling module", function() rolling = readerui.rolling end) + teardown(function() + readerui:onClose() + end) + describe("test in portrait screen mode", function() it("should goto portrait screen mode", function() readerui:handleEvent(Event:new("SetRotationMode", Screen.DEVICE_ROTATED_UPRIGHT)) @@ -63,9 +67,11 @@ describe("Readerrolling module", function() it("should emit EndOfBook event at the end of sample epub", function() local called = false + local saved_handler = readerui.onEndOfBook readerui.onEndOfBook = function() called = true end + finally(function() readerui.onEndOfBook = saved_handler end) -- check beginning of the book rolling:onGotoPage(1) assert.is.falsy(called) @@ -79,42 +85,43 @@ describe("Readerrolling module", function() 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" - -- Unsafe second // ReaderUI instance! - local txt_readerui = ReaderUI:new{ + local old_instance = readerui + finally(function() + ReaderUI.instance = old_instance + readerui = old_instance + rolling = readerui.rolling + end) + ReaderUI.instance = nil + readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_txt), } + rolling = readerui.rolling local called = false - txt_readerui.onEndOfBook = function() + readerui.onEndOfBook = function() called = true end - local txt_rolling = txt_readerui.rolling -- check beginning of the book - txt_rolling:onGotoPage(1) + rolling:onGotoPage(1) assert.is.falsy(called) - txt_rolling:onGotoViewRel(-1) - txt_rolling:onGotoViewRel(-1) + rolling:onGotoViewRel(-1) + rolling:onGotoViewRel(-1) assert.is.falsy(called) -- not at the end of the book - txt_rolling:onGotoPage(3) + rolling:onGotoPage(3) assert.is.falsy(called) - txt_rolling:onGotoViewRel(1) + rolling:onGotoViewRel(1) assert.is.falsy(called) -- at the end of the book - txt_rolling:onGotoPage(txt_readerui.document:getPageCount()) + rolling:onGotoPage(readerui.document:getPageCount()) assert.is.falsy(called) - txt_rolling:onGotoViewRel(1) + rolling:onGotoViewRel(1) assert.is.truthy(called) - readerui.onEndOfBook = nil - txt_readerui:closeDocument() - txt_readerui:onClose() - -- Restore the ref to the original ReaderUI instance - ReaderUI.instance = readerui + readerui:onClose() end) end) @@ -214,14 +221,11 @@ describe("Readerrolling module", function() end local test_book = "spec/front/unit/data/sample.txt" require("docsettings"):open(test_book):purge() - readerui:closeDocument() readerui:onClose() - local tmp_readerui = ReaderUI:new{ + readerui = ReaderUI:new{ document = DocumentRegistry:openDocument(test_book), } ReaderView.onPageUpdate = saved_handler - tmp_readerui:closeDocument() - tmp_readerui:onClose() end) end) end)