2
0
mirror of https://github.com/koreader/koreader synced 2024-11-11 19:11:14 +00:00
koreader/spec/unit/readerview_spec.lua
chrox 9ab005a1d3 fix unit test of readerlink and readerpaging
and have more confidence with the unit testing framework.

Now `make testfront` won't retry on failure and testing files are
ordered in each run so that it's possible to reproduce testing failure.

And this patch also fix flush settings not working before suspend issue:
at some point the `FlushSettings` event is sent to `UIManager` instead
of `ReaderUI`, but `UIManager` only delegated events to active widgets
and `ReaderUI` is actually not an active widgets thus will miss the event.

This patch also add a verbose debug mode with "-v" as a switch to turn
on this mode. With verbose mode on, event handling will be logged.
2016-08-12 17:05:07 +08:00

144 lines
4.7 KiB
Lua

describe("Readerview module", function()
local DocumentRegistry, Blitbuffer, ReaderUI, UIManager, Event
setup(function()
require("commonrequire")
DocumentRegistry = require("document/documentregistry")
Blitbuffer = require("ffi/blitbuffer")
ReaderUI = require("apps/reader/readerui")
UIManager = require("ui/uimanager")
Event = require("ui/event")
end)
it("should stop hinting on document close event", function()
local sample_epub = "spec/front/unit/data/leaves.epub"
local readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_epub),
}
for i = #UIManager._task_queue, 1, -1 do
local task = UIManager._task_queue[i]
if task.action == readerui.view.emitHintPageEvent then
error("UIManager's task queue should be emtpy.")
end
end
local bb = Blitbuffer.new(1000, 1000)
readerui.view:drawSinglePage(bb, 0, 0)
local found = false
for i = #UIManager._task_queue, 1, -1 do
local task = UIManager._task_queue[i]
if task.action == readerui.view.emitHintPageEvent then
found = true
end
end
assert.is.truthy(found)
readerui:onClose()
assert.is.falsy(readerui.view.hinting)
for i = #UIManager._task_queue, 1, -1 do
local task = UIManager._task_queue[i]
if task.action == readerui.view.emitHintPageEvent then
error("UIManager's task queue should be emtpy.")
end
end
end)
it("should return and restore view context in page mode", function()
local sample_pdf = "spec/front/unit/data/2col.pdf"
local readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_pdf),
}
readerui:handleEvent(Event:new("SetScrollMode", false))
readerui.zooming:setZoomMode("page")
local view = readerui.view
local ctx = view:getViewContext()
local zoom = ctx[1].zoom
ctx[1].zoom = nil
local saved_ctx = {
{
page = 1,
pos = 0,
gamma = 1,
offset = {
x = 17, y = 0,
h = 0, w = 0,
},
rotation = 0,
},
-- visible_area
{
x = 0, y = 0,
h = 800, w = 566,
},
-- page_area
{
x = 0, y = 0,
h = 800, w = 566,
},
}
assert.are.same(saved_ctx, ctx)
assertAlmostEquals(zoom, 0.95011876484561, 0.0001)
assert.is.same(view.state.page, 1)
assert.is.same(view.visible_area.x, 0)
assert.is.same(view.visible_area.y, 0)
saved_ctx[1].page = 2
saved_ctx[1].zoom = zoom
saved_ctx[2].y = 10
view:restoreViewContext(saved_ctx)
assert.is.same(view.state.page, 2)
assert.is.same(view.visible_area.x, 0)
assert.is.same(view.visible_area.y, 10)
end)
it("should return and restore view context in scroll mode", function()
local sample_pdf = "spec/front/unit/data/2col.pdf"
local readerui = ReaderUI:new{
document = DocumentRegistry:openDocument(sample_pdf),
}
readerui:handleEvent(Event:new("SetScrollMode", true))
readerui.zooming:setZoomMode("page")
local view = readerui.view
local ctx = view:getViewContext()
local zoom = ctx[1].zoom
ctx[1].zoom = nil
local saved_ctx = {
{
gamma = 1,
offset = {x = 17, y = 0},
page = 1,
page_area = {
h = 800,
w = 566,
x = 0,
y = 0,
},
rotation = 0,
visible_area = {
h = 800,
w = 566,
x = 0,
y = 0,
},
},
}
assert.are.same(saved_ctx, ctx)
assertAlmostEquals(zoom, 0.95011876484561, 0.0001)
assert.is.same(view.state.page, 1)
assert.is.same(view.visible_area.x, 0)
assert.is.same(view.visible_area.y, 0)
saved_ctx[1].page = 2
saved_ctx[1].zoom = zoom
saved_ctx[1].visible_area.y = 10
view:restoreViewContext(saved_ctx)
assert.is.same(#view.page_states, 1)
assert.is.same(view.page_states[1].page, 2)
assert.is.same(view.page_states[1].visible_area.x, 0)
assert.is.same(view.page_states[1].visible_area.y, 10)
end)
end)