From 2e4158b651d95730e041a145c2f1ad4e18208c4c Mon Sep 17 00:00:00 2001 From: Benoit Pierre Date: Wed, 25 Dec 2024 15:46:21 +0100 Subject: [PATCH] tests: cleanup readerhighlight tests --- spec/unit/readerhighlight_spec.lua | 296 ++++++++++++++--------------- 1 file changed, 144 insertions(+), 152 deletions(-) diff --git a/spec/unit/readerhighlight_spec.lua b/spec/unit/readerhighlight_spec.lua index 7d131314f..e7c6e9b4b 100644 --- a/spec/unit/readerhighlight_spec.lua +++ b/spec/unit/readerhighlight_spec.lua @@ -18,26 +18,23 @@ describe("Readerhighlight module", function() require("ffi/util").copyFile("spec/front/unit/data/sample.pdf", sample_pdf) end) - local function highlight_single_word(readerui, pos0) - local s = spy.on(readerui.languagesupport, "improveWordSelection") + local readerui + local function highlight_single_word(screenshot_filename, pos0) + local s = spy.on(readerui.languagesupport, "improveWordSelection") + -- Select a word. readerui.highlight:onHold(nil, { pos = pos0 }) readerui.highlight:onHoldRelease() readerui.highlight:saveHighlight() - + fastforward_ui_events() + screenshot(Screen, screenshot_filename) assert.spy(s).was_called() assert.spy(s).was_called_with(match.is_ref(readerui.languagesupport), match.is_ref(readerui.highlight.selected_text)) -- Reset in case we're called more than once. readerui.languagesupport.improveWordSelection:revert() - - UIManager:close(readerui.dictionary.dict_window) - UIManager:close(readerui) - -- We haven't torn it down yet - ReaderUI.instance = readerui - UIManager:quit() end - local function highlight_text(readerui, pos0, pos1) + local function highlight_text(screenshot_filename, pos0, pos1) readerui.highlight:onHold(nil, { pos = pos0 }) readerui.highlight:onHoldPan(nil, { pos = pos1 }) local next_slot @@ -50,187 +47,177 @@ describe("Readerhighlight module", function() end end readerui.highlight:onHoldRelease() + fastforward_ui_events() + screenshot(Screen, screenshot_filename) assert.truthy(readerui.highlight.highlight_dialog) assert.truthy(UIManager._window_stack[next_slot].widget == readerui.highlight.highlight_dialog) readerui.highlight:saveHighlight() - UIManager:close(readerui.highlight.highlight_dialog) - UIManager:close(readerui) - -- We haven't torn it down yet - ReaderUI.instance = readerui - UIManager:quit() end - local function tap_highlight_text(readerui, pos0, pos1, pos2) + local function tap_highlight_text(screenshot_filename, pos0, pos1, pos2) + -- Highlight some text. readerui.highlight:onHold(nil, { pos = pos0 }) readerui.highlight:onHoldPan(nil, { pos = pos1 }) readerui.highlight:onHoldRelease() readerui.highlight:saveHighlight() readerui.highlight:clear() + -- Close dialog. UIManager:close(readerui.highlight.highlight_dialog) - readerui:paintTo(Screen.bb, 0, 0) + fastforward_ui_events() + -- Tap it. readerui.highlight:onTap(nil, { pos = pos2 }) + fastforward_ui_events() + screenshot(Screen, screenshot_filename) assert.truthy(readerui.highlight.edit_highlight_dialog) - UIManager:close(readerui.highlight.edit_highlight_dialog) - UIManager:close(readerui) - -- We haven't torn it down yet - ReaderUI.instance = readerui - UIManager:quit() end describe("highlight for EPUB documents", function() - local page = 10 - local readerui, selection_spy + local selection_spy setup(function() - local sample_epub = "spec/front/unit/data/juliet.epub" readerui = ReaderUI:new{ dimen = Screen:getSize(), - document = DocumentRegistry:openDocument(sample_epub), + document = DocumentRegistry:openDocument("spec/front/unit/data/juliet.epub"), } selection_spy = spy.on(readerui.languagesupport, "improveWordSelection") end) teardown(function() - readerui:closeDocument() readerui:onClose() end) before_each(function() - UIManager:quit() - readerui.rolling:onGotoPage(page) UIManager:show(readerui) - selection_spy:clear() - --- @fixme HACK: Mock UIManager:run x and y for readerui.dimen - --- @todo Refactor readerview's dimen handling so we can get rid of - -- this workaround - readerui:paintTo(Screen.bb, 0, 0) + readerui.rolling:onGotoPage(10) end) after_each(function() + selection_spy:clear() readerui.highlight:clear() readerui.annotation.annotations = {} + UIManager:quit() end) - it("should highlight single word", function() - highlight_single_word(readerui, Geom:new{ x = 400, y = 70 }) - screenshot(Screen, "reader_highlight_single_word_epub.png") + it("should highlight word", function() + highlight_single_word("readerhighlight_epub_word.png", + Geom:new{ x = 400, y = 70 }) assert.spy(selection_spy).was_called() - assert.truthy(#readerui.annotation.annotations == 1) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals('thy', readerui.annotation.annotations[1].text) end) it("should highlight text", function() - highlight_text(readerui, + highlight_text("readerhighlight_epub_text.png", Geom:new{ x = 400, y = 110 }, Geom:new{ x = 400, y = 170 }) - screenshot(Screen, "reader_highlight_text_epub.png") assert.spy(selection_spy).was_called() - assert.truthy(#readerui.annotation.annotations == 1) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals('Montagues.\nSAMPSON', readerui.annotation.annotations[1].text) end) it("should response on tap gesture", function() - tap_highlight_text(readerui, - Geom:new{ x = 130, y = 100 }, - Geom:new{ x = 350, y = 395 }, - Geom:new{ x = 80, y = 265 }) - screenshot(Screen, "reader_tap_highlight_text_epub.png") + tap_highlight_text("readerhighlight_epub_tap.png", + Geom:new{ x = 106, y = 271 }, + Geom:new{ x = 370, y = 314 }, + Geom:new{ x = 190, y = 305 }) assert.spy(selection_spy).was_called() + assert.Equals('GREGORY\nHow! turn thy back and run?', readerui.annotation.annotations[1].text) end) end) describe("highlight for PDF documents in page mode", function() - local readerui setup(function() readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_pdf), _testsuite = true, } + readerui.hinting.view.hinting = false readerui:handleEvent(Event:new("SetScrollMode", false)) end) teardown(function() - readerui:closeDocument() readerui:onClose() end) + before_each(function() + UIManager:show(readerui) + end) + after_each(function() + readerui.highlight:clear() + readerui.annotation.annotations = {} + UIManager:quit() + end) describe("for scanned page with text layer", function() before_each(function() - UIManager:quit() - UIManager:show(readerui) readerui.paging:onGotoPage(10) end) - after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} + it("should highlight word", function() + highlight_single_word("readerhighlight_pdf_layer_word.png", + Geom:new{ x = 260, y = 70 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals('Penn', readerui.annotation.annotations[1].text) + end) + it("should highlight text", function() + highlight_text("readerhighlight_pdf_layer_text.png", + Geom:new{ x = 430, y = 210 }, + Geom:new{ x = 60, y = 236 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals('to take care of the London', readerui.annotation.annotations[1].text) end) it("should response on tap gesture", function() - tap_highlight_text(readerui, + tap_highlight_text("readerhighlight_pdf_layer_tap.png", Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }, Geom:new{ x = 280, y = 110 }) - 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(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(Screen, "reader_highlight_text_pdf.png") - assert.truthy(#readerui.annotation.annotations == 1) end) end) describe("for scanned page without text layer", function() before_each(function() - UIManager:quit() - UIManager:show(readerui) readerui.paging:onGotoPage(28) end) - after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} - 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(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(Screen, "reader_highlight_single_word_pdf_scanned.png") - assert.truthy(#readerui.annotation.annotations == 1) + it("should highlight word", function() + highlight_single_word("readerhighlight_pdf_ocr_word.png", + Geom:new{ x = 450, y = 60 }) + assert.Equals(1, #readerui.annotation.annotations) + --- @fixme: OCR should automatically kicks in. + -- assert.Equals('synthesis', readerui.annotation.annotations[1].text) end) it("should highlight text", function() - highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot(Screen, "reader_highlight_text_pdf_scanned.png") - assert.truthy(#readerui.annotation.annotations == 1) + highlight_text("readerhighlight_pdf_ocr_text.png", + Geom:new{ x = 150, y = 100 }, + Geom:new{ x = 560, y = 80 }) + assert.Equals(1, #readerui.annotation.annotations) + --- @fixme: OCR should automatically kicks in. + -- assert.Equals('that completely changes', readerui.annotation.annotations[1].text) + end) + it("should respond to tap gesture", function() + tap_highlight_text("readerhighlight_pdf_ocr_tap.png", + Geom:new{ x = 500, y = 120 }, + Geom:new{ x = 100, y = 150 }, + Geom:new{ x = 530, y = 125 }) end) end) describe("for reflowed page", function() before_each(function() - UIManager:quit() readerui.document.configurable.text_wrap = 1 - UIManager:show(readerui) readerui.paging:onGotoPage(31) end) after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} readerui.document.configurable.text_wrap = 0 - UIManager:close(readerui) -- close to flush settings - -- We haven't torn it down yet - ReaderUI.instance = readerui 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(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(Screen, "reader_highlight_single_word_pdf_reflowed.png") - assert.truthy(#readerui.annotation.annotations == 1) + it("should highlight word", function() + highlight_single_word("readerhighlight_pdf_reflow_word.png", + Geom:new{ x = 260, y = 70 }) + assert.Equals(1, #readerui.annotation.annotations) end) it("should highlight text", function() - highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot(Screen, "reader_highlight_text_pdf_reflowed.png") - assert.truthy(#readerui.annotation.annotations == 1) + highlight_text("readerhighlight_pdf_reflow_text.png", + Geom:new{ x = 260, y = 70 }, + Geom:new{ x = 260, y = 150 }) + assert.Equals(1, #readerui.annotation.annotations) + end) + it("should response on tap gesture", function() + tap_highlight_text("readerhighlight_pdf_reflow_tap.png", + Geom:new{ x = 260, y = 70 }, + Geom:new{ x = 360, y = 75 }, + Geom:new{ x = 310, y = 80 }) end) end) end) describe("highlight for PDF documents in scroll mode", function() - local readerui setup(function() readerui = ReaderUI:new{ dimen = Screen:getSize(), @@ -238,95 +225,100 @@ describe("Readerhighlight module", function() _testsuite = true, } readerui.document.configurable.trim_page = 3 + readerui.hinting.view.hinting = false readerui:handleEvent(Event:new("SetScrollMode", true)) + readerui.zooming:setZoomMode("contentwidth") end) teardown(function() - readerui:closeDocument() readerui:onClose() end) + before_each(function() + UIManager:show(readerui) + end) + after_each(function() + readerui.highlight:clear() + readerui.annotation.annotations = {} + UIManager:quit() + end) describe("for scanned page with text layer", function() before_each(function() - UIManager:quit() - UIManager:show(readerui) readerui.paging:onGotoPage(10) - readerui.zooming:setZoomMode("contentwidth") end) - after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} - end) - it("should highlight single word", function() - highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot(Screen, "reader_highlight_single_word_pdf_scroll.png") + it("should highlight word", function() + highlight_single_word("readerhighlight_pdf_scroll_layer_word.png", + Geom:new{ x = 318, y = 62 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals("VOLTAIRE", readerui.annotation.annotations[1].text) end) it("should highlight text", function() - highlight_text(readerui, Geom:new{ x = 260, y = 170 }, Geom:new{ x = 260, y = 250 }) - screenshot(Screen, "reader_highlight_text_pdf_scroll.png") - assert.truthy(#readerui.annotation.annotations == 1) + highlight_text("readerhighlight_pdf_scroll_layer_text.png", + Geom:new{ x = 86, y = 158 }, + Geom:new{ x = 402, y = 145 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals("The patriarch, George Fox,", readerui.annotation.annotations[1].text) 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(Screen, "reader_tap_highlight_text_pdf_scroll.png") - assert.truthy(#readerui.annotation.annotations == 1) + tap_highlight_text("readerhighlight_pdf_scroll_layer_tap.png", + Geom:new{ x = 544, y = 601 }, + Geom:new{ x = 344, y = 626 }, + Geom:new{ x = 130, y = 625 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals("Will iam Penn returned soon to England", readerui.annotation.annotations[1].text) end) end) describe("for scanned page without text layer", function() before_each(function() - UIManager:quit() - UIManager:show(readerui) readerui.paging:onGotoPage(28) - readerui.zooming:setZoomMode("contentwidth") end) - after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} - end) - it("should highlight single word", function() - highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot(Screen, "reader_highlight_single_word_pdf_scanned_scroll.png") - assert.truthy(#readerui.annotation.annotations == 1) + it("should highlight word", function() + highlight_single_word("readerhighlight_pdf_scroll_ocr_word.png", + Geom:new{ x = 107, y = 59 }) + assert.Equals(1, #readerui.annotation.annotations) + --- @fixme: OCR should automatically kicks in. + -- assert.Equals("geometers", readerui.annotation.annotations[1].text) end) it("should highlight text", function() - highlight_text(readerui, Geom:new{x = 192, y = 186}, Geom:new{x = 280, y = 186}) - screenshot(Screen, "reader_highlight_text_pdf_scanned_scroll.png") - assert.truthy(#readerui.annotation.annotations == 1) + highlight_text("readerhighlight_pdf_scroll_ocr_text.png", + Geom:new{x = 192, y = 186}, Geom:new{x = 262, y = 189}) + assert.Equals(1, #readerui.annotation.annotations) + --- @fixme: OCR should automatically kicks in. + -- assert.Equals("concrete objects", readerui.annotation.annotations[1].text) 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(Screen, "reader_tap_highlight_text_pdf_scanned_scroll.png") + tap_highlight_text("readerhighlight_pdf_scroll_ocr_tap.png", + Geom:new{ x = 500, y = 125 }, + Geom:new{ x = 105, y = 150 }, + Geom:new{ x = 520, y = 130 }) + assert.Equals(1, #readerui.annotation.annotations) + --- @fixme: OCR should automatically kicks in. + -- assert.Equals("objects of knowledge", readerui.annotation.annotations[1].text) end) end) describe("for reflowed page", function() before_each(function() - UIManager:quit() readerui.document.configurable.text_wrap = 1 - UIManager:show(readerui) readerui.paging:onGotoPage(31) end) after_each(function() - readerui.highlight:clear() - readerui.annotation.annotations = {} readerui.document.configurable.text_wrap = 0 - UIManager:close(readerui) -- close to flush settings - -- We haven't torn it down yet - ReaderUI.instance = readerui end) - it("should highlight single word", function() - highlight_single_word(readerui, Geom:new{ x = 260, y = 70 }) - screenshot(Screen, "reader_highlight_single_word_pdf_reflowed_scroll.png") - assert.truthy(#readerui.annotation.annotations == 1) + it("should highlight word", function() + highlight_single_word("reader_highlight_single_word_pdf_reflowed_scroll.png", + Geom:new{ x = 260, y = 70 }) + assert.Equals(1, #readerui.annotation.annotations) + assert.Equals("hedging", readerui.annotation.annotations[1].text) end) it("should highlight text", function() - highlight_text(readerui, Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) - screenshot(Screen, "reader_highlight_text_pdf_reflowed_scroll.png") + highlight_text("reader_highlight_text_pdf_reflowed_scroll.png", + Geom:new{ x = 260, y = 70 }, Geom:new{ x = 260, y = 150 }) 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(Screen, "reader_tap_highlight_text_pdf_reflowed_scroll.png") + tap_highlight_text("reader_tap_highlight_text_pdf_reflowed_scroll.png", + Geom:new{ x = 239, y = 72 }, + Geom:new{ x = 383, y = 75 }, + Geom:new{ x = 312, y = 66 }) + assert.Equals('hedging using futures', readerui.annotation.annotations[1].text) end) end) end)