Commit 745be4ef authored by csillag's avatar csillag

Add/update test suite for scrolling

parent 025cc7ea
...@@ -194,14 +194,14 @@ describe 'Annotator.Guest', -> ...@@ -194,14 +194,14 @@ describe 'Annotator.Guest', ->
assert.calledWith(highlights[1].setFocused, false) assert.calledWith(highlights[1].setFocused, false)
describe 'on "scrollToAnnotation" event', -> describe 'on "scrollToAnnotation" event', ->
it 'scrolls to the highLight with the matching tag', -> it 'scrolls to the anchor with the matching tag', ->
guest = createGuest() guest = createGuest()
highlights = [ anchors = [
{annotation: {$$tag: 'tag1'}, scrollTo: sandbox.stub()} {annotation: {$$tag: 'tag1'}, scrollIntoView: sandbox.stub()}
] ]
sandbox.stub(guest.anchoring, 'getHighlights').returns(highlights) sandbox.stub(guest.anchoring, 'getAnchors').returns(anchors)
emitGuestEvent('scrollToAnnotation', 'ctx', 'tag1') emitGuestEvent('scrollToAnnotation', 'ctx', 'tag1')
assert.called(highlights[0].scrollTo) assert.called(anchors[0].scrollIntoView)
describe 'on "getDocumentInfo" event', -> describe 'on "getDocumentInfo" event', ->
guest = null guest = null
......
...@@ -37,6 +37,8 @@ describe 'Annotator.Plugin.EnhancedAnchoring', -> ...@@ -37,6 +37,8 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
am.chooseAccessPolicy() am.chooseAccessPolicy()
am.document.setPageIndex = sinon.spy()
am.strategies.push am.strategies.push
name: "dummy anchoring strategy" name: "dummy anchoring strategy"
code: (annotation, target) -> code: (annotation, target) ->
...@@ -57,6 +59,11 @@ describe 'Annotator.Plugin.EnhancedAnchoring', -> ...@@ -57,6 +59,11 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
anchor: anchor anchor: anchor
page: page page: page
removeFromDocument: sinon.spy() removeFromDocument: sinon.spy()
scrollIntoView: sinon.spy ->
new Promise (resolve, reject) =>
setTimeout =>
@anchor.anchoring.document.setPageIndex @page
resolve()
afterEach -> afterEach ->
sandbox.restore() sandbox.restore()
...@@ -219,16 +226,52 @@ describe 'Annotator.Plugin.EnhancedAnchoring', -> ...@@ -219,16 +226,52 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
assert.include hls, anchor2.highlight[anchor2.startPage] assert.include hls, anchor2.highlight[anchor2.startPage]
assert.notInclude hls, anchor3.highlight[anchor3.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', -> 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 class DummyDocumentAccess
@applicable: -> true @applicable: -> true
isPageMapped: (index) -> index in @_rendered 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: -> constructor: ->
@_rendered = [] @_rendered = []
@currentIndex = -10
# Helper function to trigger a page rendering # Helper function to trigger a page rendering
# This is an asynchronous method; returns a promise. # This is an asynchronous method; returns a promise.
...@@ -634,3 +677,29 @@ describe 'Annotator.Plugin.EnhancedAnchoring', -> ...@@ -634,3 +677,29 @@ describe 'Annotator.Plugin.EnhancedAnchoring', ->
anchor.realize() anchor.realize()
assert anchor.fullyRealized 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