pulsar/spec/workspace-view-spec.coffee

324 lines
13 KiB
CoffeeScript
Raw Normal View History

2014-02-24 05:19:00 +04:00
{$, $$, WorkspaceView, View} = require 'atom'
2013-10-03 01:25:32 +04:00
Q = require 'q'
path = require 'path'
temp = require 'temp'
EditorView = require '../src/editor-view'
2014-01-14 00:29:00 +04:00
PaneView = require '../src/pane-view'
Workspace = require '../src/workspace'
2013-11-26 22:11:31 +04:00
describe "WorkspaceView", ->
pathToOpen = null
beforeEach ->
atom.project.setPath(atom.project.resolve('dir'))
pathToOpen = atom.project.resolve('a')
atom.workspace = new Workspace
atom.workspaceView = new WorkspaceView(atom.workspace)
2013-11-26 22:11:31 +04:00
atom.workspaceView.enableKeymap()
atom.workspaceView.focus()
waitsForPromise ->
atom.workspace.open(pathToOpen)
describe "@deserialize()", ->
viewState = null
simulateReload = ->
workspaceState = atom.workspace.serialize()
projectState = atom.project.serialize()
atom.workspaceView.remove()
atom.project = atom.deserializers.deserialize(projectState)
atom.workspace = Workspace.deserialize(workspaceState)
atom.workspaceView = new WorkspaceView(atom.workspace)
atom.workspaceView.attachToDom()
2013-11-26 22:11:31 +04:00
describe "when the serialized WorkspaceView has an unsaved buffer", ->
it "constructs the view with the same panes", ->
2013-11-26 22:11:31 +04:00
atom.workspaceView.attachToDom()
waitsForPromise ->
atom.workspace.open()
runs ->
editorView1 = atom.workspaceView.getActiveView()
buffer = editorView1.getEditor().getBuffer()
editorView1.splitRight()
expect(atom.workspaceView.getActivePaneView()).toBe atom.workspaceView.getPaneViews()[1]
simulateReload()
expect(atom.workspaceView.getEditorViews().length).toBe 2
expect(atom.workspaceView.getActivePaneView()).toBe atom.workspaceView.getPaneViews()[1]
expect(atom.workspaceView.title).toBe "untitled - #{atom.project.getPath()}"
describe "when there are open editors", ->
it "constructs the view with the same panes", ->
2013-11-26 22:11:31 +04:00
atom.workspaceView.attachToDom()
pane1 = atom.workspaceView.getActivePaneView()
pane2 = pane1.splitRight()
pane3 = pane2.splitRight()
2014-04-30 03:59:15 +04:00
pane4 = null
waitsForPromise ->
atom.workspace.open('b').then (editor) ->
pane2.activateItem(editor)
waitsForPromise ->
atom.workspace.open('../sample.js').then (editor) ->
pane3.activateItem(editor)
runs ->
pane3.activeItem.setCursorScreenPosition([2, 4])
2014-04-30 03:59:15 +04:00
pane4 = pane2.splitDown()
waitsForPromise ->
atom.workspace.open('../sample.txt').then (editor) ->
pane4.activateItem(editor)
runs ->
pane4.activeItem.setCursorScreenPosition([0, 2])
pane2.focus()
simulateReload()
expect(atom.workspaceView.getEditorViews().length).toBe 4
editorView1 = atom.workspaceView.panes.find('.pane-row > .pane .editor:eq(0)').view()
editorView3 = atom.workspaceView.panes.find('.pane-row > .pane .editor:eq(1)').view()
editorView2 = atom.workspaceView.panes.find('.pane-row > .pane-column > .pane .editor:eq(0)').view()
editorView4 = atom.workspaceView.panes.find('.pane-row > .pane-column > .pane .editor:eq(1)').view()
expect(editorView1.getEditor().getPath()).toBe atom.project.resolve('a')
expect(editorView2.getEditor().getPath()).toBe atom.project.resolve('b')
expect(editorView3.getEditor().getPath()).toBe atom.project.resolve('../sample.js')
expect(editorView3.getEditor().getCursorScreenPosition()).toEqual [2, 4]
expect(editorView4.getEditor().getPath()).toBe atom.project.resolve('../sample.txt')
expect(editorView4.getEditor().getCursorScreenPosition()).toEqual [0, 2]
# ensure adjust pane dimensions is called
expect(editorView1.width()).toBeGreaterThan 0
expect(editorView2.width()).toBeGreaterThan 0
expect(editorView3.width()).toBeGreaterThan 0
expect(editorView4.width()).toBeGreaterThan 0
# ensure correct editorView is focused again
expect(editorView2).toHaveFocus()
expect(editorView1).not.toHaveFocus()
expect(editorView3).not.toHaveFocus()
expect(editorView4).not.toHaveFocus()
expect(atom.workspaceView.title).toBe "#{path.basename(editorView2.getEditor().getPath())} - #{atom.project.getPath()}"
describe "where there are no open editors", ->
it "constructs the view with no open editors", ->
atom.workspaceView.getActivePaneView().remove()
expect(atom.workspaceView.getEditorViews().length).toBe 0
simulateReload()
expect(atom.workspaceView.getEditorViews().length).toBe 0
describe "focus", ->
2013-10-16 21:16:23 +04:00
beforeEach ->
2013-11-26 22:11:31 +04:00
atom.workspaceView.attachToDom()
2013-10-16 21:16:23 +04:00
it "hands off focus to the active pane", ->
activePane = atom.workspaceView.getActivePaneView()
$('body').focus()
expect(activePane).not.toHaveFocus()
atom.workspaceView.focus()
expect(activePane).toHaveFocus()
describe "keymap wiring", ->
commandHandler = null
beforeEach ->
commandHandler = jasmine.createSpy('commandHandler')
2013-11-26 22:11:31 +04:00
atom.workspaceView.on('foo-command', commandHandler)
atom.keymaps.add('name', '*': {'x': 'foo-command'})
2013-11-26 22:11:31 +04:00
describe "when a keydown event is triggered in the WorkspaceView", ->
it "triggers matching keybindings for that event", ->
2013-11-26 22:11:31 +04:00
event = keydownEvent 'x', target: atom.workspaceView[0]
2013-11-26 22:11:31 +04:00
atom.workspaceView.trigger(event)
expect(commandHandler).toHaveBeenCalled()
describe "window title", ->
describe "when the project has no path", ->
it "sets the title to 'untitled'", ->
atom.project.setPath(undefined)
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe 'untitled'
describe "when the project has a path", ->
beforeEach ->
waitsForPromise ->
atom.workspace.open('b')
describe "when there is an active pane item", ->
it "sets the title to the pane item's title plus the project path", ->
2014-04-22 21:01:24 +04:00
item = atom.workspace.getActivePaneItem()
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe "#{item.getTitle()} - #{atom.project.getPath()}"
describe "when the title of the active pane item changes", ->
it "updates the window title based on the item's new title", ->
2014-04-22 21:01:24 +04:00
editor = atom.workspace.getActivePaneItem()
editor.buffer.setPath(path.join(temp.dir, 'hi'))
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe "#{editor.getTitle()} - #{atom.project.getPath()}"
describe "when the active pane's item changes", ->
it "updates the title to the new item's title plus the project path", ->
atom.workspaceView.getActivePaneView().activateNextItem()
2014-04-22 21:01:24 +04:00
item = atom.workspace.getActivePaneItem()
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe "#{item.getTitle()} - #{atom.project.getPath()}"
describe "when the last pane item is removed", ->
it "updates the title to contain the project's path", ->
atom.workspaceView.getActivePaneView().remove()
2014-04-22 21:01:24 +04:00
expect(atom.workspace.getActivePaneItem()).toBeUndefined()
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe atom.project.getPath()
describe "when an inactive pane's item changes", ->
it "does not update the title", ->
pane = atom.workspaceView.getActivePaneView()
pane.splitRight()
2013-11-26 22:11:31 +04:00
initialTitle = atom.workspaceView.title
pane.activateNextItem()
2013-11-26 22:11:31 +04:00
expect(atom.workspaceView.title).toBe initialTitle
describe "when the root view is deserialized", ->
it "updates the title to contain the project's path", ->
workspaceView2 = new WorkspaceView(atom.workspace.testSerialization())
2014-04-22 21:01:24 +04:00
item = atom.workspace.getActivePaneItem()
2013-11-26 22:11:31 +04:00
expect(workspaceView2.title).toBe "#{item.getTitle()} - #{atom.project.getPath()}"
workspaceView2.remove()
describe "window:toggle-invisibles event", ->
it "shows/hides invisibles in all open and future editors", ->
2013-11-26 22:11:31 +04:00
atom.workspaceView.height(200)
atom.workspaceView.attachToDom()
2014-01-11 03:30:11 +04:00
rightEditorView = atom.workspaceView.getActiveView()
2014-06-19 03:54:44 +04:00
rightEditorView.getEditor().setText("\t ")
2014-01-11 03:30:11 +04:00
leftEditorView = rightEditorView.splitLeft()
expect(rightEditorView.find(".line:first").text()).toBe " "
expect(leftEditorView.find(".line:first").text()).toBe " "
2014-06-09 10:11:16 +04:00
{invisibles} = rightEditorView.component.state
{space, tab, eol} = invisibles
2014-06-19 03:54:44 +04:00
withInvisiblesShowing = "#{tab} #{space}#{space}#{eol}"
2013-11-26 22:11:31 +04:00
atom.workspaceView.trigger "window:toggle-invisibles"
2014-01-11 03:30:11 +04:00
expect(rightEditorView.find(".line:first").text()).toBe withInvisiblesShowing
expect(leftEditorView.find(".line:first").text()).toBe withInvisiblesShowing
2014-01-11 03:30:11 +04:00
lowerLeftEditorView = leftEditorView.splitDown()
expect(lowerLeftEditorView.find(".line:first").text()).toBe withInvisiblesShowing
2013-11-26 22:11:31 +04:00
atom.workspaceView.trigger "window:toggle-invisibles"
2014-01-11 03:30:11 +04:00
expect(rightEditorView.find(".line:first").text()).toBe " "
expect(leftEditorView.find(".line:first").text()).toBe " "
2014-01-11 03:30:11 +04:00
lowerRightEditorView = rightEditorView.splitDown()
expect(lowerRightEditorView.find(".line:first").text()).toBe " "
describe ".eachEditorView(callback)", ->
beforeEach ->
2013-11-26 22:11:31 +04:00
atom.workspaceView.attachToDom()
it "invokes the callback for existing editor", ->
count = 0
callbackEditor = null
callback = (editor) ->
callbackEditor = editor
count++
atom.workspaceView.eachEditorView(callback)
expect(count).toBe 1
2013-11-26 22:11:31 +04:00
expect(callbackEditor).toBe atom.workspaceView.getActiveView()
it "invokes the callback for new editor", ->
count = 0
callbackEditor = null
callback = (editor) ->
callbackEditor = editor
count++
atom.workspaceView.eachEditorView(callback)
count = 0
callbackEditor = null
2013-11-26 22:11:31 +04:00
atom.workspaceView.getActiveView().splitRight()
expect(count).toBe 1
2013-11-26 22:11:31 +04:00
expect(callbackEditor).toBe atom.workspaceView.getActiveView()
it "does not invoke the callback for mini editors", ->
editorViewCreatedHandler = jasmine.createSpy('editorViewCreatedHandler')
atom.workspaceView.eachEditorView(editorViewCreatedHandler)
editorViewCreatedHandler.reset()
miniEditor = new EditorView(mini: true)
atom.workspaceView.append(miniEditor)
expect(editorViewCreatedHandler).not.toHaveBeenCalled()
it "returns a subscription that can be disabled", ->
count = 0
callback = (editor) -> count++
subscription = atom.workspaceView.eachEditorView(callback)
expect(count).toBe 1
2013-11-26 22:11:31 +04:00
atom.workspaceView.getActiveView().splitRight()
expect(count).toBe 2
subscription.off()
2013-11-26 22:11:31 +04:00
atom.workspaceView.getActiveView().splitRight()
expect(count).toBe 2
2013-12-13 05:30:22 +04:00
describe "core:close", ->
it "closes the active pane item until all that remains is a single empty pane", ->
atom.config.set('core.destroyEmptyPanes', true)
2014-04-23 03:47:17 +04:00
paneView1 = atom.workspaceView.getActivePaneView()
editorView = atom.workspaceView.getActiveView()
editorView.splitRight()
paneView2 = atom.workspaceView.getActivePaneView()
expect(paneView1).not.toBe paneView2
expect(atom.workspaceView.getPaneViews()).toHaveLength 2
2014-04-23 03:47:17 +04:00
atom.workspaceView.trigger('core:close')
expect(atom.workspaceView.getActivePaneView().getItems()).toHaveLength 1
expect(atom.workspaceView.getPaneViews()).toHaveLength 1
2013-12-13 05:30:22 +04:00
atom.workspaceView.trigger('core:close')
2014-04-23 03:47:17 +04:00
expect(atom.workspaceView.getActivePaneView().getItems()).toHaveLength 0
expect(atom.workspaceView.getPaneViews()).toHaveLength 1
describe "the scrollbar visibility class", ->
it "has a class based on the style of the scrollbar", ->
scrollbarStyle = require 'scrollbar-style'
scrollbarStyle.emitValue 'legacy'
expect(atom.workspaceView).toHaveClass 'scrollbars-visible-always'
scrollbarStyle.emitValue 'overlay'
expect(atom.workspaceView).toHaveClass 'scrollbars-visible-when-scrolling'
describe "editor font styling", ->
[editorNode, editor] = []
beforeEach ->
atom.workspaceView.attachToDom()
editorNode = atom.workspaceView.find('.editor')[0]
editor = atom.workspaceView.find('.editor').view().getEditor()
it "updates the font-size based on the 'editor.fontSize' config value", ->
initialCharWidth = editor.getDefaultCharWidth()
expect(getComputedStyle(editorNode).fontSize).toBe atom.config.get('editor.fontSize') + 'px'
atom.config.set('editor.fontSize', atom.config.get('editor.fontSize') + 5)
expect(getComputedStyle(editorNode).fontSize).toBe atom.config.get('editor.fontSize') + 'px'
expect(editor.getDefaultCharWidth()).toBeGreaterThan initialCharWidth
it "updates the font-family based on the 'editor.fontFamily' config value", ->
initialCharWidth = editor.getDefaultCharWidth()
expect(getComputedStyle(editorNode).fontFamily).toBe atom.config.get('editor.fontFamily')
atom.config.set('editor.fontFamily', 'sans-serif')
expect(getComputedStyle(editorNode).fontFamily).toBe atom.config.get('editor.fontFamily')
expect(editor.getDefaultCharWidth()).not.toBe initialCharWidth
it "updates the line-height based on the 'editor.lineHeight' config value", ->
initialLineHeight = editor.getLineHeightInPixels()
atom.config.set('editor.lineHeight', '30px')
expect(getComputedStyle(editorNode).lineHeight).toBe atom.config.get('editor.lineHeight')
expect(editor.getLineHeightInPixels()).not.toBe initialLineHeight