2016-04-19 06:50:36 +00:00
|
|
|
describe("Readersearch module", function()
|
|
|
|
local sample_epub = "spec/front/unit/data/juliet.epub"
|
|
|
|
local sample_pdf = "spec/front/unit/data/sample.pdf"
|
2020-07-01 20:17:41 +00:00
|
|
|
local DocumentRegistry, ReaderUI, Screen, dbg
|
2014-11-05 04:28:11 +00:00
|
|
|
|
2016-04-19 06:50:36 +00:00
|
|
|
setup(function()
|
|
|
|
require("commonrequire")
|
|
|
|
DocumentRegistry = require("document/documentregistry")
|
|
|
|
ReaderUI = require("apps/reader/readerui")
|
2020-07-01 20:17:41 +00:00
|
|
|
Screen = require("device").screen
|
2016-04-19 06:50:36 +00:00
|
|
|
dbg = require("dbg")
|
|
|
|
end)
|
2014-11-05 04:28:11 +00:00
|
|
|
|
|
|
|
describe("search API for EPUB documents", function()
|
2020-12-19 04:32:23 +00:00
|
|
|
local readerui, doc, search, rolling
|
2014-11-05 04:28:11 +00:00
|
|
|
setup(function()
|
2020-12-19 04:32:23 +00:00
|
|
|
readerui = ReaderUI:new{
|
2020-07-01 20:17:41 +00:00
|
|
|
dimen = Screen:getSize(),
|
2014-11-05 04:28:11 +00:00
|
|
|
document = DocumentRegistry:openDocument(sample_epub),
|
|
|
|
}
|
|
|
|
doc = readerui.document
|
|
|
|
search = readerui.search
|
|
|
|
rolling = readerui.rolling
|
|
|
|
end)
|
2020-12-19 04:32:23 +00:00
|
|
|
teardown(function()
|
|
|
|
readerui:closeDocument()
|
|
|
|
readerui:onClose()
|
|
|
|
end)
|
2014-11-05 04:28:11 +00:00
|
|
|
it("should search backward", function()
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(10)
|
2014-11-05 04:28:11 +00:00
|
|
|
assert.truthy(search:searchFromCurrent("Verona", 1))
|
|
|
|
for i = 1, 100, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromCurrent("Verona", 1)
|
|
|
|
if words then
|
|
|
|
for _, word in ipairs(words) do
|
|
|
|
local pageno = doc:getPageFromXPointer(word.start)
|
2016-04-19 06:50:36 +00:00
|
|
|
--dbg("found at pageno", pageno)
|
2014-11-05 04:28:11 +00:00
|
|
|
assert.truthy(pageno <= i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should search forward", function()
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(10)
|
2014-11-05 04:28:11 +00:00
|
|
|
assert.truthy(search:searchFromCurrent("Verona", 0))
|
|
|
|
for i = 1, 100, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromCurrent("Verona", 0)
|
|
|
|
if words then
|
|
|
|
for _, word in ipairs(words) do
|
|
|
|
local pageno = doc:getPageFromXPointer(word.start)
|
2016-04-19 06:50:36 +00:00
|
|
|
--dbg("found at pageno", pageno)
|
2014-11-05 04:28:11 +00:00
|
|
|
assert.truthy(pageno >= i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find the first occurrence", function()
|
|
|
|
for i = 10, 100, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromStart("Verona")
|
|
|
|
assert.truthy(words)
|
|
|
|
local pageno = doc:getPageFromXPointer(words[1].start)
|
2016-06-01 13:29:31 +00:00
|
|
|
assert.truthy(pageno < 10)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
|
|
|
for i = 1, 5, 1 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromStart("Verona")
|
|
|
|
assert(words == nil)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find the last occurrence", function()
|
2018-06-02 17:28:00 +00:00
|
|
|
-- local logger = require("logger")
|
2019-02-03 10:25:34 +00:00
|
|
|
-- logger.warn("nb of pages", doc:getPageCount())
|
|
|
|
-- 20190202: currently 242 pages
|
2014-11-18 08:08:39 +00:00
|
|
|
for i = 100, 180, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromEnd("Verona")
|
|
|
|
assert.truthy(words)
|
|
|
|
local pageno = doc:getPageFromXPointer(words[1].start)
|
2018-06-02 17:28:00 +00:00
|
|
|
-- logger.info("last match on page", pageno)
|
2018-07-24 19:01:45 +00:00
|
|
|
assert.truthy(pageno > 185)
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
2018-12-05 12:01:49 +00:00
|
|
|
for i = 290, 335, 1 do
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(i)
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromEnd("Verona")
|
|
|
|
assert(words == nil)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find all occurrences", function()
|
|
|
|
local count = 0
|
2016-03-27 22:39:47 +00:00
|
|
|
rolling:onGotoPage(1)
|
2018-02-04 19:57:13 +00:00
|
|
|
local cur_page = doc:getCurrentPage()
|
2014-11-05 04:28:11 +00:00
|
|
|
local words = search:searchFromCurrent("Verona", 0)
|
|
|
|
while words do
|
2018-02-04 19:57:13 +00:00
|
|
|
local new_page = nil
|
2014-11-05 04:28:11 +00:00
|
|
|
for _, word in ipairs(words) do
|
2016-04-19 06:50:36 +00:00
|
|
|
--dbg("found word", word.start)
|
2018-02-04 19:57:13 +00:00
|
|
|
local word_page = doc:getPageFromXPointer(word.start)
|
|
|
|
if word_page ~= cur_page then -- ignore words on current page
|
|
|
|
if not new_page then -- first word on a new page
|
|
|
|
new_page = word_page
|
|
|
|
count = count + 1
|
|
|
|
doc:gotoXPointer(word.start) -- go to this new page
|
|
|
|
else -- new page seen
|
|
|
|
if word_page == new_page then -- only count words on this new page
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not new_page then -- no word seen on any new page
|
|
|
|
break
|
2014-11-05 04:28:11 +00:00
|
|
|
end
|
2018-02-04 19:57:13 +00:00
|
|
|
cur_page = doc:getCurrentPage()
|
2014-11-05 04:28:11 +00:00
|
|
|
words = search:searchNext("Verona", 0)
|
|
|
|
end
|
2015-09-05 09:32:34 +00:00
|
|
|
assert.are.equal(13, count)
|
2014-11-05 04:28:11 +00:00
|
|
|
end)
|
|
|
|
end)
|
2016-04-24 04:16:37 +00:00
|
|
|
|
2014-11-05 04:28:11 +00:00
|
|
|
describe("search API for PDF documents", function()
|
2020-12-19 04:32:23 +00:00
|
|
|
local readerui, doc, search, paging
|
2014-11-17 09:58:25 +00:00
|
|
|
setup(function()
|
2020-12-19 04:32:23 +00:00
|
|
|
readerui = ReaderUI:new{
|
2020-07-01 20:17:41 +00:00
|
|
|
dimen = Screen:getSize(),
|
2014-11-17 09:58:25 +00:00
|
|
|
document = DocumentRegistry:openDocument(sample_pdf),
|
|
|
|
}
|
|
|
|
doc = readerui.document
|
|
|
|
search = readerui.search
|
|
|
|
paging = readerui.paging
|
|
|
|
end)
|
2020-12-19 04:32:23 +00:00
|
|
|
teardown(function()
|
|
|
|
readerui:closeDocument()
|
|
|
|
readerui:onClose()
|
|
|
|
end)
|
2014-11-17 09:58:25 +00:00
|
|
|
it("should match single word with case insensitive option in one page", function()
|
|
|
|
assert.are.equal(9, #doc.koptinterface:findAllMatches(doc, "what", true, 20))
|
|
|
|
assert.are.equal(51, #doc.koptinterface:findAllMatches(doc, "the", true, 20))
|
|
|
|
assert.are.equal(0, #doc.koptinterface:findAllMatches(doc, "xxxx", true, 20))
|
|
|
|
end)
|
|
|
|
it("should match single word with case sensitive option in one page", function()
|
|
|
|
assert.are.equal(7, #doc.koptinterface:findAllMatches(doc, "what", false, 20))
|
|
|
|
assert.are.equal(49, #doc.koptinterface:findAllMatches(doc, "the", false, 20))
|
|
|
|
assert.are.equal(0, #doc.koptinterface:findAllMatches(doc, "xxxx", false, 20))
|
|
|
|
end)
|
|
|
|
it("should match phrase in one page", function()
|
|
|
|
assert.are.equal(2*2, #doc.koptinterface:findAllMatches(doc, "mean that", true, 20))
|
|
|
|
end)
|
|
|
|
it("should match whole phrase in one page", function()
|
|
|
|
assert.are.equal(1*3, #doc.koptinterface:findAllMatches(doc, "mean that the", true, 20))
|
|
|
|
end)
|
|
|
|
it("should not match empty string", function()
|
|
|
|
assert.are.equal(0, #doc.koptinterface:findAllMatches(doc, "", true, 1))
|
|
|
|
end)
|
|
|
|
it("should not match on page without text layer", function()
|
|
|
|
assert.are.equal(0, #doc.koptinterface:findAllMatches(doc, "e", true, 1))
|
|
|
|
end)
|
|
|
|
it("should search backward", function()
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(20)
|
2014-11-17 09:58:25 +00:00
|
|
|
assert.truthy(search:searchFromCurrent("test", 1))
|
|
|
|
for i = 1, 40, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromCurrent("test", 1)
|
|
|
|
if words then
|
2016-04-19 06:50:36 +00:00
|
|
|
dbg("search backward: found at page", words.page)
|
2014-11-17 09:58:25 +00:00
|
|
|
assert.truthy(words.page <= i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should search forward", function()
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(20)
|
2014-11-17 09:58:25 +00:00
|
|
|
assert.truthy(search:searchFromCurrent("test", 0))
|
|
|
|
for i = 1, 40, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromCurrent("test", 0)
|
|
|
|
if words then
|
2016-04-19 06:50:36 +00:00
|
|
|
dbg("search forward: found at page", words.page)
|
2014-11-17 09:58:25 +00:00
|
|
|
assert.truthy(words.page >= i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find the first occurrence", function()
|
|
|
|
for i = 20, 40, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromStart("test")
|
|
|
|
assert.truthy(words)
|
|
|
|
assert.are.equal(10, words.page)
|
|
|
|
end
|
|
|
|
for i = 1, 10, 2 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromStart("test")
|
|
|
|
assert(words == nil)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find the last occurrence", function()
|
|
|
|
for i = 10, 30, 10 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromEnd("test")
|
|
|
|
assert.truthy(words)
|
|
|
|
assert.are.equal(32, words.page)
|
|
|
|
end
|
|
|
|
for i = 40, 50, 2 do
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(i)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromEnd("test")
|
|
|
|
assert(words == nil)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
it("should find all occurrences", function()
|
|
|
|
local count = 0
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(1)
|
2014-11-17 09:58:25 +00:00
|
|
|
local words = search:searchFromCurrent("test", 0)
|
|
|
|
while words do
|
|
|
|
count = count + #words
|
2016-04-19 06:50:36 +00:00
|
|
|
--dbg("found words", #words, words.page)
|
2016-03-27 22:39:47 +00:00
|
|
|
paging:onGotoPage(words.page)
|
2014-11-17 09:58:25 +00:00
|
|
|
words = search:searchNext("test", 0)
|
|
|
|
end
|
|
|
|
assert.are.equal(11, count)
|
|
|
|
end)
|
2014-11-05 04:28:11 +00:00
|
|
|
end)
|
|
|
|
end)
|