2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/spec/unit/readerbookmark_spec.lua

161 lines
6.9 KiB
Lua
Raw Normal View History

require("commonrequire")
local DocumentRegistry = require("document/documentregistry")
local ReaderUI = require("apps/reader/readerui")
local UIManager = require("ui/uimanager")
local Screen = require("device").screen
local Geom = require("ui/geometry")
local DEBUG = require("dbg")
local sample_epub = "spec/front/unit/data/juliet.epub"
local sample_pdf = "spec/front/unit/data/sample.pdf"
describe("Readerbookmark module", function()
local function highlight_text(readerui, pos0, pos1)
readerui.highlight:onHold(nil, { pos = pos0 })
readerui.highlight:onHoldPan(nil, { pos = pos1 })
readerui.highlight:onHoldRelease()
assert.truthy(readerui.highlight.highlight_dialog)
readerui.highlight:onHighlight()
UIManager:scheduleIn(1, function()
UIManager:close(readerui.highlight.highlight_dialog)
UIManager:close(readerui)
end)
UIManager:run()
end
local function toggler_dogear(readerui)
readerui.bookmark:onToggleBookmark()
UIManager:scheduleIn(1, function()
UIManager:close(readerui)
end)
UIManager:run()
end
local function show_bookmark_menu(readerui)
UIManager:scheduleIn(1, function()
UIManager:close(readerui.bookmark.bookmark_menu)
UIManager:close(readerui)
end)
UIManager:run()
end
describe("bookmark for EPUB document", function()
local page = 10
local readerui
setup(function()
readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_epub),
}
end)
before_each(function()
UIManager:quit()
UIManager:show(readerui)
readerui.rolling:gotoPage(10)
end)
it("should show dogear after togglering non-bookmarked page", function()
toggler_dogear(readerui)
Screen:shot("screenshots/reader_bookmark_dogear_epub.png")
assert.truthy(readerui.view.dogear_visible)
end)
it("should not show dogear after togglering bookmarked page", function()
toggler_dogear(readerui)
Screen:shot("screenshots/reader_bookmark_nodogear_epub.png")
assert.truthy(not readerui.view.dogear_visible)
end)
it("should sort bookmarks with descending page numbers", function()
local pages = {1, 20, 5, 30, 10, 40, 15, 25, 35, 45}
for _, page in ipairs(pages) do
readerui.rolling:gotoPage(page)
toggler_dogear(readerui)
end
readerui.bookmark:onShowBookmark()
show_bookmark_menu(readerui)
Screen:shot("screenshots/reader_bookmark_10marks_epub.png")
assert.are.same(10, #readerui.bookmark.bookmarks)
end)
it("should keep descending page numbers after removing bookmarks", function()
local pages = {1, 30, 10, 40, 20}
for _, page in ipairs(pages) do
readerui.rolling:gotoPage(page)
toggler_dogear(readerui)
end
readerui.bookmark:onShowBookmark()
show_bookmark_menu(readerui)
Screen:shot("screenshots/reader_bookmark_5marks_epub.png")
assert.are.same(5, #readerui.bookmark.bookmarks)
end)
it("should add bookmark by highlighting", function()
highlight_text(readerui, Geom:new{ x = 260, y = 60 }, Geom:new{ x = 260, y = 90 })
readerui.bookmark:onShowBookmark()
show_bookmark_menu(readerui)
Screen:shot("screenshots/reader_bookmark_6marks_epub.png")
assert.are.same(6, #readerui.bookmark.bookmarks)
end)
it("should get previous bookmark for certain page", function()
local xpointer = readerui.document:getXPointer()
local bm_xpointer = readerui.bookmark:getPreviousBookmarkedPage(xpointer)
assert.are.same(5, readerui.document:getPageFromXPointer(bm_xpointer))
end)
it("should get next bookmark for certain page", function()
local xpointer = readerui.document:getXPointer()
local bm_xpointer = readerui.bookmark:getNextBookmarkedPage(xpointer)
assert.are.same(15, readerui.document:getPageFromXPointer(bm_xpointer))
end)
end)
describe("bookmark for PDF document", function()
local readerui
setup(function()
readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_pdf),
}
end)
before_each(function()
UIManager:quit()
UIManager:show(readerui)
readerui.paging:gotoPage(10)
end)
it("should show dogear after togglering non-bookmarked page", function()
toggler_dogear(readerui)
Screen:shot("screenshots/reader_bookmark_dogear_pdf.png")
assert.truthy(readerui.view.dogear_visible)
end)
it("should not show dogear after togglering bookmarked page", function()
toggler_dogear(readerui)
Screen:shot("screenshots/reader_bookmark_nodogear_pdf.png")
assert.truthy(not readerui.view.dogear_visible)
end)
it("should sort bookmarks with descending page numbers", function()
local pages = {1, 20, 5, 30, 10, 40, 15, 25, 35, 45}
for _, page in ipairs(pages) do
readerui.paging:gotoPage(page)
toggler_dogear(readerui)
end
readerui.bookmark:onShowBookmark()
show_bookmark_menu(readerui)
Screen:shot("screenshots/reader_bookmark_10marks_pdf.png")
assert.are.same(10, #readerui.bookmark.bookmarks)
end)
it("should keep descending page numbers after removing bookmarks", function()
local pages = {1, 30, 10, 40, 20}
for _, page in ipairs(pages) do
readerui.paging:gotoPage(page)
toggler_dogear(readerui)
end
readerui.bookmark:onShowBookmark()
show_bookmark_menu(readerui)
Screen:shot("screenshots/reader_bookmark_5marks_pdf.png")
assert.are.same(5, #readerui.bookmark.bookmarks)
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)
Screen:shot("screenshots/reader_bookmark_6marks_pdf.png")
assert.are.same(6, #readerui.bookmark.bookmarks)
end)
it("should get previous bookmark for certain page", function()
assert.are.same(5, readerui.bookmark:getPreviousBookmarkedPage(10))
end)
it("should get next bookmark for certain page", function()
assert.are.same(15, readerui.bookmark:getNextBookmarkedPage(10))
end)
end)
end)