From 56899eab8e7e7378f01180f6192e2140a1fe4060 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sat, 23 Oct 2021 21:13:18 +1100 Subject: [PATCH] spec: add tests for LanguageSupport This primarily consists of some spies added to ensure that the LanguageSupport plugin is actually being called at the right time. Signed-off-by: Aleksa Sarai --- spec/unit/readerdictionary_spec.lua | 33 +++++++++++++++++++++++++++-- spec/unit/readerhighlight_spec.lua | 16 +++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/spec/unit/readerdictionary_spec.lua b/spec/unit/readerdictionary_spec.lua index ac8bc645c..445fbc0b2 100644 --- a/spec/unit/readerdictionary_spec.lua +++ b/spec/unit/readerdictionary_spec.lua @@ -24,7 +24,6 @@ describe("Readerdictionary module", function() readerui:onClose() end) it("should show quick lookup window", function() - local name = "screenshots/reader_dictionary.png" UIManager:quit() UIManager:show(readerui) rolling:onGotoPage(100) @@ -36,6 +35,36 @@ describe("Readerdictionary module", function() ReaderUI.instance = readerui end) UIManager:run() - Screen:shot(name) + Screen:shot("screenshots/reader_dictionary.png") + end) + it("should attempt to deinflect (Japanese) word on lookup", function() + UIManager:quit() + UIManager:show(readerui) + rolling:onGotoPage(100) + + local word = "喋っている" + local s = spy.on(readerui.languagesupport, "extraDictionaryFormCandidates") + + -- We can't use onLookupWord because we need to check whether + -- extraDictionaryFormCandidates was called synchronously. + dictionary:stardictLookup(word) + + assert.spy(s).was_called() + assert.spy(s).was_called_with(match.is_ref(readerui.languagesupport), word) + if readerui.languagesupport.plugins["japanese_support"] then + -- @todo This should probably check against a set or sorted list + -- of the candidates we'd expect. + assert.spy(s).was_returned_with(match.is_not_nil()) + end + readerui.languagesupport.extraDictionaryFormCandidates:revert() + + UIManager:scheduleIn(1, function() + UIManager:close(dictionary.dict_window) + UIManager:close(readerui) + -- We haven't torn it down yet + ReaderUI.instance = readerui + end) + UIManager:run() + Screen:shot("screenshots/reader_dictionary_japanese.png") end) end) diff --git a/spec/unit/readerhighlight_spec.lua b/spec/unit/readerhighlight_spec.lua index a7423ac6b..e82e83b0d 100644 --- a/spec/unit/readerhighlight_spec.lua +++ b/spec/unit/readerhighlight_spec.lua @@ -13,9 +13,18 @@ describe("Readerhighlight module", function() end) local function highlight_single_word(readerui, pos0) + local s = spy.on(readerui.languagesupport, "improveWordSelection") + readerui.highlight:onHold(nil, { pos = pos0 }) readerui.highlight:onHoldRelease() readerui.highlight:onHighlight() + + 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:scheduleIn(1, function() UIManager:close(readerui.dictionary.dict_window) UIManager:close(readerui) @@ -72,13 +81,14 @@ describe("Readerhighlight module", function() describe("highlight for EPUB documents", function() local page = 10 - local readerui + local readerui, selection_spy setup(function() local sample_epub = "spec/front/unit/data/juliet.epub" readerui = ReaderUI:new{ dimen = Screen:getSize(), document = DocumentRegistry:openDocument(sample_epub), } + selection_spy = spy.on(readerui.languagesupport, "improveWordSelection") end) teardown(function() readerui:closeDocument() @@ -88,6 +98,7 @@ describe("Readerhighlight module", 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 @@ -99,6 +110,7 @@ describe("Readerhighlight module", function() it("should highlight single word", function() highlight_single_word(readerui, Geom:new{ x = 400, y = 70 }) Screen:shot("screenshots/reader_highlight_single_word_epub.png") + assert.spy(selection_spy).was_called() assert.truthy(readerui.view.highlight.saved[page]) end) it("should highlight text", function() @@ -106,6 +118,7 @@ describe("Readerhighlight module", function() Geom:new{ x = 400, y = 110 }, Geom:new{ x = 400, y = 170 }) Screen:shot("screenshots/reader_highlight_text_epub.png") + assert.spy(selection_spy).was_called() assert.truthy(readerui.view.highlight.saved[page]) end) it("should response on tap gesture", function() @@ -114,6 +127,7 @@ describe("Readerhighlight module", function() Geom:new{ x = 350, y = 395 }, Geom:new{ x = 80, y = 265 }) Screen:shot("screenshots/reader_tap_highlight_text_epub.png") + assert.spy(selection_spy).was_called() end) end)