From f1b522acb994ba3d91bdc8d53125e5179a05df59 Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Wed, 25 Dec 2024 15:46:06 +0100 Subject: [PATCH] tests: factorize UI related helpers --- .luacheckrc | 2 ++ spec/unit/commonrequire.lua | 14 +++++++++ spec/unit/readerbookmark_spec.lua | 24 +++++++-------- spec/unit/readerdictionary_spec.lua | 11 ++----- spec/unit/readerhighlight_spec.lua | 46 +++++++++++++---------------- spec/unit/readerlink_spec.lua | 11 +------ 6 files changed, 51 insertions(+), 57 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index caa831c89..a6aac4852 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -30,6 +30,8 @@ files["spec/unit/*"].globals = { "package", "disable_plugins", "load_plugin", + "fastforward_ui_events", + "screenshot", } -- TODO: clean up and enforce max line width (631) diff --git a/spec/unit/commonrequire.lua b/spec/unit/commonrequire.lua index 8c20efcdb..53c0de9db 100644 --- a/spec/unit/commonrequire.lua +++ b/spec/unit/commonrequire.lua @@ -75,3 +75,17 @@ function load_plugin(name) end assert(false) end + +function fastforward_ui_events() + local UIManager = require("ui/uimanager") + -- Fast forward all scheduled tasks. + UIManager:shiftScheduledTasksBy(-1e9) + -- Fix hang when running tests with our docker base image SDL. + UIManager:setInputTimeout(0) + -- And run the UI manager's input loop once. + UIManager:handleInput() +end + +function screenshot(screen, filename) + screen:shot(DataStorage:getDataDir() .. "/screenshots/" .. filename) +end diff --git a/spec/unit/readerbookmark_spec.lua b/spec/unit/readerbookmark_spec.lua index 80b5806b0..fba9bbc15 100644 --- a/spec/unit/readerbookmark_spec.lua +++ b/spec/unit/readerbookmark_spec.lua @@ -20,10 +20,6 @@ describe("ReaderBookmark module", function() Util.copyFile("spec/front/unit/data/sample.pdf", sample_pdf) end) - local function screenshot(filename) - Screen:shot(DataStorage:getDataDir() .. "/screenshots/" .. filename) - end - local function highlight_text(readerui, pos0, pos1) readerui.highlight:onHold(nil, { pos = pos0 }) readerui.highlight:onHoldPan(nil, { pos = pos1 }) @@ -79,13 +75,13 @@ describe("ReaderBookmark module", function() it("should show dogear after toggling non-bookmarked page", function() assert.falsy(readerui.view.dogear_visible) toggler_dogear(readerui) - screenshot("reader_bookmark_dogear_epub.png") + screenshot(Screen, "reader_bookmark_dogear_epub.png") assert.truthy(readerui.view.dogear_visible) end) it("should not show dogear after toggling bookmarked page", function() assert.truthy(readerui.view.dogear_visible) toggler_dogear(readerui) - screenshot("reader_bookmark_nodogear_epub.png") + screenshot(Screen, "reader_bookmark_nodogear_epub.png") assert.falsy(readerui.view.dogear_visible) end) it("should sort bookmarks with ascending page numbers", function() @@ -96,7 +92,7 @@ describe("ReaderBookmark module", function() end readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_10marks_epub.png") + screenshot(Screen, "reader_bookmark_10marks_epub.png") assert.are.same(10, #readerui.annotation.annotations) assert.are.same(15, readerui.document:getPageFromXPointer(readerui.annotation.annotations[4].page)) end) @@ -108,7 +104,7 @@ describe("ReaderBookmark module", function() end readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_5marks_epub.png") + screenshot(Screen, "reader_bookmark_5marks_epub.png") assert.are.same(5, #readerui.annotation.annotations) end) it("should add bookmark by highlighting", function() @@ -117,7 +113,7 @@ describe("ReaderBookmark module", function() Geom:new{ x = 260, y = 90 }) readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_6marks_epub.png") + screenshot(Screen, "reader_bookmark_6marks_epub.png") assert.are.same(6, #readerui.annotation.annotations) end) it("should get previous bookmark for certain page", function() @@ -154,12 +150,12 @@ describe("ReaderBookmark module", function() end) it("should show dogear after toggling non-bookmarked page", function() toggler_dogear(readerui) - screenshot("reader_bookmark_dogear_pdf.png") + screenshot(Screen, "reader_bookmark_dogear_pdf.png") assert.truthy(readerui.view.dogear_visible) end) it("should not show dogear after toggling bookmarked page", function() toggler_dogear(readerui) - screenshot("reader_bookmark_nodogear_pdf.png") + screenshot(Screen, "reader_bookmark_nodogear_pdf.png") assert.truthy(not readerui.view.dogear_visible) end) it("should sort bookmarks with ascending page numbers", function() @@ -170,7 +166,7 @@ describe("ReaderBookmark module", function() end readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_10marks_pdf.png") + screenshot(Screen, "reader_bookmark_10marks_pdf.png") assert.are.same(10, #readerui.annotation.annotations) assert.are.same(15, readerui.annotation.annotations[4].page) end) @@ -182,14 +178,14 @@ describe("ReaderBookmark module", function() end readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_5marks_pdf.png") + screenshot(Screen, "reader_bookmark_5marks_pdf.png") assert.are.same(5, #readerui.annotation.annotations) end) it("should add bookmark by highlighting", function() highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) readerui.bookmark:onShowBookmark() show_bookmark_menu(readerui) - screenshot("reader_bookmark_6marks_pdf.png") + screenshot(Screen, "reader_bookmark_6marks_pdf.png") assert.are.same(6, #readerui.annotation.annotations) end) it("should get previous bookmark for certain page", function() diff --git a/spec/unit/readerdictionary_spec.lua b/spec/unit/readerdictionary_spec.lua index edff228b7..35c4dece3 100644 --- a/spec/unit/readerdictionary_spec.lua +++ b/spec/unit/readerdictionary_spec.lua @@ -1,21 +1,16 @@ describe("Readerdictionary module", function() - local DataStorage, DocumentRegistry, ReaderUI, UIManager, Screen + local DocumentRegistry, ReaderUI, UIManager, Screen setup(function() require("commonrequire") disable_plugins() load_plugin("japanese.koplugin") - DataStorage = require("datastorage") DocumentRegistry = require("document/documentregistry") ReaderUI = require("apps/reader/readerui") UIManager = require("ui/uimanager") Screen = require("device").screen end) - local function screenshot(filename) - Screen:shot(DataStorage:getDataDir() .. "/screenshots/" .. filename) - end - local readerui, rolling, dictionary setup(function() local sample_epub = "spec/front/unit/data/leaves.epub" @@ -42,7 +37,7 @@ describe("Readerdictionary module", function() ReaderUI.instance = readerui end) UIManager:run() - screenshot("screenshots/reader_dictionary.png") + screenshot(Screen, "reader_dictionary.png") end) it("should attempt to deinflect (Japanese) word on lookup", function() UIManager:quit() @@ -72,6 +67,6 @@ describe("Readerdictionary module", function() ReaderUI.instance = readerui end) UIManager:run() - screenshot("screenshots/reader_dictionary_japanese.png") + screenshot(Screen, "reader_dictionary_japanese.png") end) end) diff --git a/spec/unit/readerhighlight_spec.lua b/spec/unit/readerhighlight_spec.lua index ae08436fc..7d131314f 100644 --- a/spec/unit/readerhighlight_spec.lua +++ b/spec/unit/readerhighlight_spec.lua @@ -18,10 +18,6 @@ describe("Readerhighlight module", function() require("ffi/util").copyFile("spec/front/unit/data/sample.pdf", sample_pdf) end) - local function screenshot(filename) - Screen:shot(DataStorage:getDataDir() .. "/screenshots/" .. filename) - end - local function highlight_single_word(readerui, pos0) local s = spy.on(readerui.languagesupport, "improveWordSelection") @@ -112,7 +108,7 @@ describe("Readerhighlight module", function() end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 400, y = 70 }) - screenshot("reader_highlight_single_word_epub.png") + screenshot(Screen, "reader_highlight_single_word_epub.png") assert.spy(selection_spy).was_called() assert.truthy(#readerui.annotation.annotations == 1) end) @@ -120,7 +116,7 @@ describe("Readerhighlight module", function() highlight_text(readerui, Geom:new{ x = 400, y = 110 }, Geom:new{ x = 400, y = 170 }) - screenshot("reader_highlight_text_epub.png") + screenshot(Screen, "reader_highlight_text_epub.png") assert.spy(selection_spy).was_called() assert.truthy(#readerui.annotation.annotations == 1) end) @@ -129,7 +125,7 @@ describe("Readerhighlight module", function() Geom:new{ x = 130, y = 100 }, Geom:new{ x = 350, y = 395 }, Geom:new{ x = 80, y = 265 }) - screenshot("reader_tap_highlight_text_epub.png") + screenshot(Screen, "reader_tap_highlight_text_epub.png") assert.spy(selection_spy).was_called() end) end) @@ -163,16 +159,16 @@ describe("Readerhighlight module", function() Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf.png") + screenshot(Screen, "reader_tap_highlight_text_pdf.png") end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf.png") + screenshot(Screen, "reader_highlight_single_word_pdf.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should highlight text", function() highlight_text(readerui, Geom:new{ x = 260, y = 170 }, Geom:new{ x = 260, y = 250 }) - screenshot("reader_highlight_text_pdf.png") + screenshot(Screen, "reader_highlight_text_pdf.png") assert.truthy(#readerui.annotation.annotations == 1) end) end) @@ -188,16 +184,16 @@ describe("Readerhighlight module", function() end) it("should respond to tap gesture", function() tap_highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf_scanned.png") + screenshot(Screen, "reader_tap_highlight_text_pdf_scanned.png") end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf_scanned.png") + screenshot(Screen, "reader_highlight_single_word_pdf_scanned.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should highlight text", function() highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot("reader_highlight_text_pdf_scanned.png") + screenshot(Screen, "reader_highlight_text_pdf_scanned.png") assert.truthy(#readerui.annotation.annotations == 1) end) end) @@ -218,16 +214,16 @@ describe("Readerhighlight module", function() end) it("should response on tap gesture", function() tap_highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf_reflowed.png") + screenshot(Screen, "reader_tap_highlight_text_pdf_reflowed.png") end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf_reflowed.png") + screenshot(Screen, "reader_highlight_single_word_pdf_reflowed.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should highlight text", function() highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot("reader_highlight_text_pdf_reflowed.png") + screenshot(Screen, "reader_highlight_text_pdf_reflowed.png") assert.truthy(#readerui.annotation.annotations == 1) end) end) @@ -261,11 +257,11 @@ describe("Readerhighlight module", function() end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf_scroll.png") + screenshot(Screen, "reader_highlight_single_word_pdf_scroll.png") end) it("should highlight text", function() highlight_text(readerui, Geom:new{ x = 260, y = 170 }, Geom:new{ x = 260, y = 250 }) - screenshot("reader_highlight_text_pdf_scroll.png") + screenshot(Screen, "reader_highlight_text_pdf_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should response on tap gesture", function() @@ -273,7 +269,7 @@ describe("Readerhighlight module", function() Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf_scroll.png") + screenshot(Screen, "reader_tap_highlight_text_pdf_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) end) @@ -290,17 +286,17 @@ describe("Readerhighlight module", function() end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf_scanned_scroll.png") + screenshot(Screen, "reader_highlight_single_word_pdf_scanned_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should highlight text", function() highlight_text(readerui, Geom:new{x = 192, y = 186}, Geom:new{x = 280, y = 186}) - screenshot("reader_highlight_text_pdf_scanned_scroll.png") + screenshot(Screen, "reader_highlight_text_pdf_scanned_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should response on tap gesture", function() tap_highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf_scanned_scroll.png") + screenshot(Screen, "reader_tap_highlight_text_pdf_scanned_scroll.png") end) end) describe("for reflowed page", function() @@ -320,17 +316,17 @@ describe("Readerhighlight module", function() end) it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot("reader_highlight_single_word_pdf_reflowed_scroll.png") + screenshot(Screen, "reader_highlight_single_word_pdf_reflowed_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should highlight text", function() highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot("reader_highlight_text_pdf_reflowed_scroll.png") + screenshot(Screen, "reader_highlight_text_pdf_reflowed_scroll.png") assert.truthy(#readerui.annotation.annotations == 1) end) it("should response on tap gesture", function() tap_highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - screenshot("reader_tap_highlight_text_pdf_reflowed_scroll.png") + screenshot(Screen, "reader_tap_highlight_text_pdf_reflowed_scroll.png") end) end) end) diff --git a/spec/unit/readerlink_spec.lua b/spec/unit/readerlink_spec.lua index 408649c99..7102595f8 100644 --- a/spec/unit/readerlink_spec.lua +++ b/spec/unit/readerlink_spec.lua @@ -1,5 +1,5 @@ describe("ReaderLink module", function() - local DocumentRegistry, ReaderUI, UIManager, sample_epub, sample_pdf, Event, Screen + local DocumentRegistry, ReaderUI, sample_epub, sample_pdf, Event, Screen setup(function() require("commonrequire") @@ -8,7 +8,6 @@ describe("ReaderLink module", function() DocumentRegistry = require("document/documentregistry") Event = require("ui/event") ReaderUI = require("apps/reader/readerui") - UIManager = require("ui/uimanager") Screen = require("device").screen sample_epub = "spec/front/unit/data/leaves.epub" sample_pdf = "spec/front/unit/data/paper.pdf" @@ -16,18 +15,10 @@ describe("ReaderLink module", function() local readerui - local function fastforward_ui_events() - -- Fast forward all scheduled tasks. - UIManager:shiftScheduledTasksBy(-1e9) - UIManager:run() - end - after_each(function() readerui:closeDocument() readerui:onClose() readerui = nil - UIManager:quit() - UIManager._exit_code = nil end) describe("with epub", function()