Commit 745be4ef authored by csillag's avatar csillag

Add/update test suite for scrolling

parent 025cc7ea
......@@ -194,14 +194,14 @@ describe 'Annotator.Guest', ->
assert.calledWith(highlights[1].setFocused, false)
describe 'on "scrollToAnnotation" event', ->
it 'scrolls to the highLight with the matching tag', ->
it 'scrolls to the anchor with the matching tag', ->
guest = createGuest()
highlights = [
{annotation: {$$tag: 'tag1'}, scrollTo: sandbox.stub()}
anchors = [
{annotation: {$$tag: 'tag1'}, scrollIntoView: sandbox.stub()}
]
sandbox.stub(guest.anchoring, 'getHighlights').returns(highlights)
sandbox.stub(guest.anchoring, 'getAnchors').returns(anchors)
emitGuestEvent('scrollToAnnotation', 'ctx', 'tag1')
assert.called(highlights[0].scrollTo)
assert.called(anchors[0].scrollIntoView)
describe 'on "getDocumentInfo" event', ->
guest = null
......
......@@ -37,6 +37,8 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
am.chooseAccessPolicy()
am.document.setPageIndex = sinon.spy()
am.strategies.push
name: "dummy anchoring strategy"
code: (annotation, target) ->
......@@ -57,6 +59,11 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
anchor: anchor
page: page
removeFromDocument: sinon.spy()
scrollIntoView: sinon.spy ->
new Promise (resolve, reject) =>
setTimeout =>
@anchor.anchoring.document.setPageIndex @page
resolve()
afterEach ->
sandbox.restore()
......@@ -219,16 +226,52 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
assert.include hls, anchor2.highlight[anchor2.startPage]
assert.notInclude hls, anchor3.highlight[anchor3.startPage]
describe 'Anchor.scrollIntoView()', ->
it 'calls scrollIntoView() on the highlight', ->
am = createAnchoringManager()
ann = createTestAnnotation "a1"
anchor = am.createAnchor(ann, ann.target[0]).result
anchor.scrollIntoView().then ->
assert.called anchor.highlight[anchor.startPage].scrollIntoView
describe 'two-phased anchoring', ->
# Simple lazy rendering document simulation for testing,
# which emulates the user movement prediction (and page rendering)
# behavior of PDF.js
class DummyDocumentAccess
@applicable: -> true
isPageMapped: (index) -> index in @_rendered
getPageIndex: -> @currentIndex
setPageIndex: (index) ->
# Do this when the wanted page is ready
onPageReady = =>
# Try to find out where will the reader proceed
prediction = if @currentIndex < index
index + 1 # If we went forward, we'll want the next page, too
else
index - 1 # if we want backward, we'll want the prev. page, too
# Set the page index where we want to be
@currentIndex = index
# Render the predicted next page
renderPage(this, prediction) unless @isPageMapped prediction
# Try to move where we need to be
if @isPageMapped index
onPageReady()
else
renderPage(this, index).then onPageReady
constructor: ->
@_rendered = []
@currentIndex = -10
# Helper function to trigger a page rendering
# This is an asynchronous method; returns a promise.
......@@ -634,3 +677,29 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
anchor.realize()
assert anchor.fullyRealized
describe 'when scrolling to a virtual anchor', ->
it 'gets the right page rendered', ->
am = createAnchoringManagerAndLazyDocument()
ann = createTestAnnotationForPages "a1", [1]
anchor = am.createAnchor(ann, ann.target[0]).result
anchor.scrollIntoView().then ->
assert am.document.isPageMapped 1
it 'scrolls to the right page', ->
am = createAnchoringManagerAndLazyDocument()
ann = createTestAnnotationForPages "a1", [1]
anchor = am.createAnchor(ann, ann.target[0]).result
anchor.scrollIntoView().then ->
assert.equal am.document.getPageIndex(), 1
it 'calls scrollIntoView() on the highlight', ->
am = createAnchoringManagerAndLazyDocument()
ann = createTestAnnotationForPages "a1", [1]
anchor = am.createAnchor(ann, ann.target[0]).result
anchor.scrollIntoView().then ->
assert.called anchor.highlight[1].scrollIntoView
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment