From e1ef779d4ebd0f13b4dfe51ab91c277a6e3ca183 Mon Sep 17 00:00:00 2001 From: abe33 Date: Tue, 10 Mar 2015 12:17:25 +0100 Subject: [PATCH] :green_heart: Fix random failures in scroll tests --- lib/minimap-element.coffee | 28 ++++++++++++++++------------ spec/minimap-element-spec.coffee | 22 ++++++++++++++++------ 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/lib/minimap-element.coffee b/lib/minimap-element.coffee index 8df431fe..c7620d3d 100644 --- a/lib/minimap-element.coffee +++ b/lib/minimap-element.coffee @@ -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) @@ -451,14 +451,16 @@ 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 @@ -466,7 +468,9 @@ class MinimapElement extends HTMLElement progress = 1 if progress > 1 delta = swing(progress) step(from + (to-from)*delta) - requestAnimationFrame(update) if progress < 1 + + if progress < 1 + requestAnimationFrame(update) update() diff --git a/spec/minimap-element-spec.coffee b/spec/minimap-element-spec.coffee index a2278936..603b5551 100644 --- a/spec/minimap-element-spec.coffee +++ b/spec/minimap-element-spec.coffee @@ -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 @@ -82,6 +82,7 @@ describe 'MinimapElement', -> requestAnimationFrameSafe = window.requestAnimationFrame spyOn(window, 'requestAnimationFrame').andCallFake (fn) -> + lastFn = fn nextAnimationFrame = -> nextAnimationFrame = noAnimationFrame fn() @@ -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 -> @@ -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', ->