Skip to content

Commit

Permalink
💚 Fix random failures in scroll tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abe33 committed Mar 10, 2015
1 parent 8ed9aae commit e1ef779
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
28 changes: 16 additions & 12 deletions lib/minimap-element.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,18 @@ class MinimapElement extends HTMLElement
y = pageY - target.getBoundingClientRect().top
row = Math.floor(y / @minimap.getLineHeight()) + @minimap.getFirstVisibleScreenRow()

scrollTop = row * @minimap.textEditor.getLineHeightInPixels() - @minimap.textEditor.getHeight() / 2
textEditor = @minimap.getTextEditor()

scrollTop = row * textEditor.getLineHeightInPixels() - textEditor.getHeight() / 2

from = @minimap.textEditor.getScrollTop()
to = scrollTop
step = (now) =>
@minimap.textEditor.setScrollTop(now)
if atom.config.get('minimap.scrollAnimation')
duration = 300
from = textEditor.getScrollTop()
to = scrollTop
step = (now) => textEditor.setScrollTop(now)
@animate(from: from, to: to, duration: duration, step: step)
else
duration = 0

@animate(from: from, to: to, duration: duration, step: step)
textEditor.setScrollTop(scrollTop)

relayMousewheelEvent: (e) =>
editorElement = atom.views.getView(@minimap.textEditor)
Expand Down Expand Up @@ -451,22 +451,26 @@ class MinimapElement extends HTMLElement
else
"scale(#{x}, #{y})"

getTime: -> new Date()

animate: ({from, to, duration, step}) ->
start = new Date()
start = @getTime()

swing = (progress) ->
return 0.5 - Math.cos( progress * Math.PI ) / 2

update = ->
passed = new Date() - start
update = =>
passed = @getTime() - start
if duration == 0
progress = 1
else
progress = passed / duration
progress = 1 if progress > 1
delta = swing(progress)
step(from + (to-from)*delta)
requestAnimationFrame(update) if progress < 1

if progress < 1
requestAnimationFrame(update)

update()

Expand Down
22 changes: 16 additions & 6 deletions spec/minimap-element-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe 'MinimapElement', ->
# ## ## ## ## ## ## ###### ## ##

describe 'when attached to the text editor element', ->
[noAnimationFrame, nextAnimationFrame, canvas, visibleArea] = []
[noAnimationFrame, nextAnimationFrame, lastFn, canvas, visibleArea] = []

beforeEach ->
# Comment after body below to leave the created text editor and minimap
Expand All @@ -82,6 +82,7 @@ describe 'MinimapElement', ->

requestAnimationFrameSafe = window.requestAnimationFrame
spyOn(window, 'requestAnimationFrame').andCallFake (fn) ->
lastFn = fn
nextAnimationFrame = ->
nextAnimationFrame = noAnimationFrame
fn()
Expand Down Expand Up @@ -284,8 +285,8 @@ describe 'MinimapElement', ->
editor.setScrollTop(0)
editor.setScrollLeft(0)

sleep(150)
runs -> nextAnimationFrame()
minimapElement.measureHeightAndWidth()
nextAnimationFrame()

describe 'using the mouse scrollwheel over the minimap', ->
beforeEach ->
Expand All @@ -298,22 +299,31 @@ describe 'MinimapElement', ->

describe 'pressing the mouse on the minimap canvas (without scroll animation)', ->
beforeEach ->
t = 0
spyOn(minimapElement, 'getTime').andCallFake -> n = t; t += 100; n
spyOn(minimapElement, 'requestUpdate').andCallFake ->

atom.config.set 'minimap.scrollAnimation', false

canvas = minimapElement.canvas
mousedown(canvas)
waitsFor -> nextAnimationFrame isnt noAnimationFrame
runs -> nextAnimationFrame()

it 'scrolls the editor to the line below the mouse', ->
expect(editor.getScrollTop()).toEqual(360)

describe 'pressing the mouse on the minimap canvas (with scroll animation)', ->
beforeEach ->
expect(editor.getScrollTop()).toEqual(0)

t = 0
spyOn(minimapElement, 'getTime').andCallFake -> n = t; t += 100; n
spyOn(minimapElement, 'requestUpdate').andCallFake ->

atom.config.set 'minimap.scrollAnimation', true
atom.config.set 'minimap.scrollAnimationDuration', 300

canvas = minimapElement.canvas
mousedown(canvas)

waitsFor -> nextAnimationFrame isnt noAnimationFrame

it 'scrolls the editor gradually to the line below the mouse', ->
Expand Down

0 comments on commit e1ef779

Please sign in to comment.