Start porting scroll-related specs

This commit is contained in:
Antonio Scandurra 2015-09-23 09:39:33 +02:00
parent d3b1d309ba
commit 57a006d19b
5 changed files with 188 additions and 113 deletions

View File

@ -3371,6 +3371,172 @@ describe "TextEditorComponent", ->
expect(line1LeafNodes[0].classList.contains('indent-guide')).toBe false
expect(line1LeafNodes[1].classList.contains('indent-guide')).toBe false
fffdescribe "autoscroll", ->
beforeEach ->
editor.setVerticalScrollMargin(2)
editor.setHorizontalScrollMargin(2)
component.setLineHeight("10px")
component.setFontSize(17)
component.measureDimensions()
nextAnimationFrame()
# Why does this set an incorrect width? :confused:
wrapperNode.style.width = "108px"
wrapperNode.style.height = 5.5 * 10 + "px"
component.measureDimensions()
nextAnimationFrame()
component.presenter.setHorizontalScrollbarHeight(0)
component.presenter.setVerticalScrollbarWidth(0)
nextAnimationFrame() # perform requested update
afterEach ->
atom.themes.removeStylesheet("test")
describe "moving cursors", ->
it "scrolls down when the last cursor gets closer than ::verticalScrollMargin to the bottom of the editor", ->
expect(wrapperNode.getScrollTop()).toBe 0
expect(wrapperNode.getScrollBottom()).toBe 5.5 * 10
editor.setCursorScreenPosition([2, 0])
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe 5.5 * 10
editor.moveDown()
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe 6 * 10
editor.moveDown()
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe 7 * 10
it "scrolls up when the last cursor gets closer than ::verticalScrollMargin to the top of the editor", ->
editor.setCursorScreenPosition([11, 0])
nextAnimationFrame()
wrapperNode.setScrollBottom(wrapperNode.getScrollHeight())
nextAnimationFrame()
editor.moveUp()
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe wrapperNode.getScrollHeight()
editor.moveUp()
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 7 * 10
editor.moveUp()
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 6 * 10
it "scrolls right when the last cursor gets closer than ::horizontalScrollMargin to the right of the editor", ->
expect(wrapperNode.getScrollLeft()).toBe 0
expect(wrapperNode.getScrollRight()).toBe 5.5 * 10
editor.setCursorScreenPosition([0, 2])
nextAnimationFrame()
expect(wrapperNode.getScrollRight()).toBe 5.5 * 10
editor.moveRight()
nextAnimationFrame()
expect(wrapperNode.getScrollRight()).toBe 6 * 10
editor.moveRight()
nextAnimationFrame()
expect(wrapperNode.getScrollRight()).toBe 7 * 10
it "scrolls left when the last cursor gets closer than ::horizontalScrollMargin to the left of the editor", ->
wrapperNode.setScrollRight(wrapperNode.getScrollWidth())
nextAnimationFrame()
expect(wrapperNode.getScrollRight()).toBe wrapperNode.getScrollWidth()
editor.setCursorScreenPosition([6, 62], autoscroll: false)
nextAnimationFrame()
editor.moveLeft()
nextAnimationFrame()
expect(wrapperNode.getScrollLeft()).toBe 59 * 10
editor.moveLeft()
nextAnimationFrame()
expect(wrapperNode.getScrollLeft()).toBe 58 * 10
it "scrolls down when inserting lines makes the document longer than the editor's height", ->
editor.setCursorScreenPosition([13, Infinity])
editor.insertNewline()
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe 14 * 10
editor.insertNewline()
nextAnimationFrame()
expect(wrapperNode.getScrollBottom()).toBe 15 * 10
it "autoscrolls to the cursor when it moves due to undo", ->
editor.insertText('abc')
wrapperNode.setScrollTop(Infinity)
nextAnimationFrame()
editor.undo()
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
it "doesn't scroll when the cursor moves into the visible area", ->
editor.setCursorBufferPosition([0, 0])
nextAnimationFrame()
wrapperNode.setScrollTop(40)
nextAnimationFrame()
editor.setCursorBufferPosition([6, 0])
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 40
it "honors the autoscroll option on cursor and selection manipulation methods", ->
expect(wrapperNode.getScrollTop()).toBe 0
editor.addCursorAtScreenPosition([11, 11], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.addCursorAtBufferPosition([11, 11], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.setCursorScreenPosition([11, 11], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.setCursorBufferPosition([11, 11], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.addSelectionForBufferRange([[11, 11], [11, 11]], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.addSelectionForScreenRange([[11, 11], [11, 12]], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.setSelectedBufferRange([[11, 0], [11, 1]], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.setSelectedScreenRange([[11, 0], [11, 6]], autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.clearSelections(autoscroll: false)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.addSelectionForScreenRange([[0, 0], [0, 4]])
nextAnimationFrame()
editor.getCursors()[0].setScreenPosition([11, 11], autoscroll: true)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBeGreaterThan 0
editor.getCursors()[0].setBufferPosition([0, 0], autoscroll: true)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
editor.getSelections()[0].setScreenRange([[11, 0], [11, 4]], autoscroll: true)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBeGreaterThan 0
editor.getSelections()[0].setBufferRange([[0, 0], [0, 4]], autoscroll: true)
nextAnimationFrame()
expect(wrapperNode.getScrollTop()).toBe 0
describe "middle mouse paste on Linux", ->
originalPlatform = null

View File

@ -909,118 +909,6 @@ describe "TextEditor", ->
cursor2 = editor.addCursorAtBufferPosition([1, 4])
expect(cursor2.marker).toBe cursor1.marker
describe "autoscroll", ->
beforeEach ->
editor.setVerticalScrollMargin(2)
editor.setHorizontalScrollMargin(2)
editor.setLineHeightInPixels(10)
editor.setDefaultCharWidth(10)
editor.setHorizontalScrollbarHeight(0)
editor.setHeight(5.5 * 10)
editor.setWidth(5.5 * 10)
it "scrolls down when the last cursor gets closer than ::verticalScrollMargin to the bottom of the editor", ->
expect(editor.getScrollTop()).toBe 0
expect(editor.getScrollBottom()).toBe 5.5 * 10
editor.setCursorScreenPosition([2, 0])
expect(editor.getScrollBottom()).toBe 5.5 * 10
editor.moveDown()
expect(editor.getScrollBottom()).toBe 6 * 10
editor.moveDown()
expect(editor.getScrollBottom()).toBe 7 * 10
it "scrolls up when the last cursor gets closer than ::verticalScrollMargin to the top of the editor", ->
editor.setCursorScreenPosition([11, 0])
editor.setScrollBottom(editor.getScrollHeight())
editor.moveUp()
expect(editor.getScrollBottom()).toBe editor.getScrollHeight()
editor.moveUp()
expect(editor.getScrollTop()).toBe 7 * 10
editor.moveUp()
expect(editor.getScrollTop()).toBe 6 * 10
it "scrolls right when the last cursor gets closer than ::horizontalScrollMargin to the right of the editor", ->
expect(editor.getScrollLeft()).toBe 0
expect(editor.getScrollRight()).toBe 5.5 * 10
editor.setCursorScreenPosition([0, 2])
expect(editor.getScrollRight()).toBe 5.5 * 10
editor.moveRight()
expect(editor.getScrollRight()).toBe 6 * 10
editor.moveRight()
expect(editor.getScrollRight()).toBe 7 * 10
it "scrolls left when the last cursor gets closer than ::horizontalScrollMargin to the left of the editor", ->
editor.setScrollRight(editor.getScrollWidth())
expect(editor.getScrollRight()).toBe editor.getScrollWidth()
editor.setCursorScreenPosition([6, 62], autoscroll: false)
editor.moveLeft()
expect(editor.getScrollLeft()).toBe 59 * 10
editor.moveLeft()
expect(editor.getScrollLeft()).toBe 58 * 10
it "scrolls down when inserting lines makes the document longer than the editor's height", ->
editor.setCursorScreenPosition([13, Infinity])
editor.insertNewline()
expect(editor.getScrollBottom()).toBe 14 * 10
editor.insertNewline()
expect(editor.getScrollBottom()).toBe 15 * 10
it "autoscrolls to the cursor when it moves due to undo", ->
editor.insertText('abc')
editor.setScrollTop(Infinity)
editor.undo()
expect(editor.getScrollTop()).toBe 0
it "doesn't scroll when the cursor moves into the visible area", ->
editor.setCursorBufferPosition([0, 0])
editor.setScrollTop(40)
expect(editor.getVisibleRowRange()).toEqual([4, 9])
editor.setCursorBufferPosition([6, 0])
expect(editor.getScrollTop()).toBe 40
it "honors the autoscroll option on cursor and selection manipulation methods", ->
expect(editor.getScrollTop()).toBe 0
editor.addCursorAtScreenPosition([11, 11], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.addCursorAtBufferPosition([11, 11], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.setCursorScreenPosition([11, 11], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.setCursorBufferPosition([11, 11], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.addSelectionForBufferRange([[11, 11], [11, 11]], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.addSelectionForScreenRange([[11, 11], [11, 12]], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.setSelectedBufferRange([[11, 0], [11, 1]], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.setSelectedScreenRange([[11, 0], [11, 6]], autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.clearSelections(autoscroll: false)
expect(editor.getScrollTop()).toBe 0
editor.addSelectionForScreenRange([[0, 0], [0, 4]])
editor.getCursors()[0].setScreenPosition([11, 11], autoscroll: true)
expect(editor.getScrollTop()).toBeGreaterThan 0
editor.getCursors()[0].setBufferPosition([0, 0], autoscroll: true)
expect(editor.getScrollTop()).toBe 0
editor.getSelections()[0].setScreenRange([[11, 0], [11, 4]], autoscroll: true)
expect(editor.getScrollTop()).toBeGreaterThan 0
editor.getSelections()[0].setBufferRange([[0, 0], [0, 4]], autoscroll: true)
expect(editor.getScrollTop()).toBe 0
describe '.logCursorScope()', ->
beforeEach ->
spyOn(atom.notifications, 'addInfo')

View File

@ -382,6 +382,12 @@ class TextEditorComponent
getScrollBottom: ->
@presenter.getScrollBottom()
getScrollHeight: ->
@presenter.getScrollHeight()
getScrollWidth: ->
@presenter.getScrollWidth()
onMouseDown: (event) =>
unless event.button is 0 or (event.button is 1 and process.platform is 'linux')
# Only handle mouse down events for left mouse button on all platforms

View File

@ -243,6 +243,12 @@ class TextEditorElement extends HTMLElement
getScrollBottom: ->
@component.getScrollBottom()
getScrollHeight: ->
@component.getScrollHeight()
getScrollWidth: ->
@component.getScrollWidth()
stopEventPropagation = (commandListeners) ->
newCommandListeners = {}
for commandName, commandListener of commandListeners

View File

@ -870,7 +870,10 @@ class TextEditorPresenter
@explicitHeight - @horizontalScrollbarHeight
getClientWidth: ->
@clientWidth ? @contentFrameWidth
if @clientWidth
@clientWidth
else
@contentFrameWidth - @verticalScrollbarWidth
getScrollBottom: -> @getScrollTop() + @getClientHeight()
setScrollBottom: (scrollBottom) ->
@ -882,6 +885,12 @@ class TextEditorPresenter
@setScrollLeft(scrollRight - @getClientWidth())
@getScrollRight()
getScrollHeight: ->
@scrollHeight
getScrollWidth: ->
@scrollWidth
setHorizontalScrollbarHeight: (horizontalScrollbarHeight) ->
unless @measuredHorizontalScrollbarHeight is horizontalScrollbarHeight
oldHorizontalScrollbarHeight = @measuredHorizontalScrollbarHeight