From 54f5166254b081be6e63213b6aeabb63417f01bd Mon Sep 17 00:00:00 2001 From: chrox Date: Wed, 7 Oct 2015 22:42:12 +0800 Subject: [PATCH] add koptinterface spec --- frontend/document/koptinterface.lua | 1 + spec/unit/commonrequire.lua | 27 ++++++++++ spec/unit/document_spec.lua | 6 +++ spec/unit/koptinterface_spec.lua | 83 +++++++++++++++++++++++++++++ spec/unit/kosync_spec.lua | 2 +- 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 spec/unit/koptinterface_spec.lua diff --git a/frontend/document/koptinterface.lua b/frontend/document/koptinterface.lua index 94bc73cdb..dd5046071 100644 --- a/frontend/document/koptinterface.lua +++ b/frontend/document/koptinterface.lua @@ -206,6 +206,7 @@ function KoptInterface:getCachedContext(doc, pageno) -- If kctx is not cached, create one and get reflowed bmp in foreground. local kc = self:createContext(doc, pageno, bbox) local page = doc._document:openPage(pageno) + DEBUG("reflowing page", pageno, "in foreground") -- reflow page --local secs, usecs = util.gettime() page:reflow(kc, 0) diff --git a/spec/unit/commonrequire.lua b/spec/unit/commonrequire.lua index 2e0f49531..6f6b2c188 100644 --- a/spec/unit/commonrequire.lua +++ b/spec/unit/commonrequire.lua @@ -21,3 +21,30 @@ Input.dummy = true -- turn on debug local DEBUG = require("dbg") --DEBUG:turnOn() + +function assertAlmostEquals(expected, actual, margin) + if type(actual) ~= 'number' or type(expected) ~= 'number' + or type(margin) ~= 'number' then + error('assertAlmostEquals: must supply only number arguments.', 2) + end + + assert(math.abs(expected - actual) <= margin, + 'Values are not almost equal\n' + .. 'Expected: ' .. expected .. ' with margin of ' .. margin + .. ', received: ' .. actual + ) +end + +function assertNotAlmostEquals(expected, actual, margin) + if type(actual) ~= 'number' or type(expected) ~= 'number' + or type(margin) ~= 'number' then + error('assertAlmostEquals: must supply only number arguments.', 2) + end + + assert(math.abs(expected - actual) > margin, + 'Values are almost equal\n' + .. 'Expected: ' .. expected .. ' with margin of ' .. margin + .. ', received: ' .. actual + ) +end + diff --git a/spec/unit/document_spec.lua b/spec/unit/document_spec.lua index 68e99a8e5..836fb94de 100644 --- a/spec/unit/document_spec.lua +++ b/spec/unit/document_spec.lua @@ -13,6 +13,12 @@ describe("PDF document module", function() assert.are.same(dimen.w, 567) assert.are.same(dimen.h, 1418) end) + it("should get cover image", function() + local image = doc:getCoverPageImage() + assert.truthy(image) + assert.are.same(320, image:getWidth()) + assert.are.same(800, image:getHeight()) + end) local pos0 = {page = 1, x = 0, y = 20} local pos1 = {page = 1, x = 300, y = 120} local pboxes = { diff --git a/spec/unit/koptinterface_spec.lua b/spec/unit/koptinterface_spec.lua new file mode 100644 index 000000000..02cb51a1d --- /dev/null +++ b/spec/unit/koptinterface_spec.lua @@ -0,0 +1,83 @@ +require("commonrequire") +local DocumentRegistry = require("document/documentregistry") +local Koptinterface = require("document/koptinterface") +local Cache = require("cache") +local DEBUG = require("dbg") +DEBUG:turnOn() + +describe("Koptinterface module", function() + local sample_pdf = "spec/front/unit/data/tall.pdf" + local doc + + before_each(function() + doc = DocumentRegistry:openDocument(sample_pdf) + Cache:clear() + end) + + after_each(function() + doc:close() + end) + + it("should get auto bbox", function() + local auto_bbox = Koptinterface:getAutoBBox(doc, 1) + assertAlmostEquals(22, auto_bbox.x0, 0.5) + assertAlmostEquals(38, auto_bbox.y0, 0.5) + assertAlmostEquals(548, auto_bbox.x1, 0.5) + assertAlmostEquals(1387, auto_bbox.y1, 0.5) + end) + + it("should get semi auto bbox", function() + local semiauto_bbox = Koptinterface:getSemiAutoBBox(doc, 1) + local page_bbox = doc:getPageBBox(1) + doc.bbox[1] = { + x0 = page_bbox.x0 + 10, + y0 = page_bbox.y0 + 10, + x1 = page_bbox.x1 - 10, + y1 = page_bbox.y1 - 10, + } + + local bbox = Koptinterface:getSemiAutoBBox(doc, 1) + assertNotAlmostEquals(semiauto_bbox.x0, bbox.x0, 0.5) + assertNotAlmostEquals(semiauto_bbox.y0, bbox.y0, 0.5) + assertNotAlmostEquals(semiauto_bbox.x1, bbox.x1, 0.5) + assertNotAlmostEquals(semiauto_bbox.y1, bbox.y1, 0.5) + end) + + it("should render optimized page to de-watermark", function() + local page_dimen = doc:getPageDimensions(1, 1.0, 0) + local tile = Koptinterface:renderOptimizedPage(doc, 1, nil, + 1.0, 0, 0) + assert.truthy(tile) + assert.are.same(page_dimen, tile.excerpt) + end) + + it("should reflow page in foreground", function() + doc.configurable.text_wrap = 1 + local kc = Koptinterface:getCachedContext(doc, 1) + assert.truthy(kc) + end) + + it("should hint reflowed page in background", function() + doc.configurable.text_wrap = 1 + Koptinterface:hintReflowedPage(doc, 1, 1.0, 0, 1.0, 0) + -- and wait for reflowing to complete + local kc = Koptinterface:getCachedContext(doc, 1) + assert.truthy(kc) + end) + + it("should get native text boxes", function() + local kc = Koptinterface:getCachedContext(doc, 1) + local boxes = Koptinterface:getNativeTextBoxes(doc, 1) + local lines_in_native_page = #boxes + assert.truthy(lines_in_native_page == 60) + end) + + it("should get reflow text boxes", function() + doc.configurable.text_wrap = 1 + local kc = Koptinterface:getCachedContext(doc, 1) + local boxes = Koptinterface:getReflowedTextBoxes(doc, 1) + local lines_in_reflowed_page = #boxes + assert.truthy(lines_in_reflowed_page > 60) + end) + +end) diff --git a/spec/unit/kosync_spec.lua b/spec/unit/kosync_spec.lua index 279b3339d..99e9adb7d 100644 --- a/spec/unit/kosync_spec.lua +++ b/spec/unit/kosync_spec.lua @@ -1,6 +1,5 @@ require("commonrequire") local UIManager = require("ui/uimanager") -local HTTPClient = require("httpclient") local DEBUG = require("dbg") local md5 = require("MD5") --DEBUG:turnOn() @@ -58,6 +57,7 @@ local service = [[ ]] describe("KOSync modules #notest #nocov", function() + local HTTPClient = require("httpclient") local Spore = require("Spore") local client = Spore.new_from_string(service) package.loaded['Spore.Middleware.GinClient'] = {}