<commit_before>PaneContainer = require '../src/pane-container'
PaneView = require '../src/pane-view'
fs = require 'fs-plus'
{Emitter, Disposable} = require 'event-kit'
{$, View} = require '../src/space-pen-extensions'
path = require 'path'
temp = require 'temp'

describe "PaneView", ->
  [container, containerModel, view1, view2, editor1, editor2, pane, paneModel, deserializerDisposable] = []

  class TestView extends View
    @deserialize: ({id, text}) -> new TestView({id, text})
    @content: ({id, text}) -> @div class: 'test-view', id: id, tabindex: -1, text
    initialize: ({@id, @text}) ->
      @emitter = new Emitter
    serialize: -> {deserializer: 'TestView', @id, @text}
    getURI: -> @id
    isEqual: (other) -> other? and @id is other.id and @text is other.text
    changeTitle: ->
      @emitter.emit 'did-change-title', 'title'
    onDidChangeTitle: (callback) ->
      @emitter.on 'did-change-title', callback
    onDidChangeModified: -> new Disposable(->)

  beforeEach ->
    jasmine.snapshotDeprecations()

    deserializerDisposable = atom.deserializers.add(TestView)
    container = atom.views.getView(new PaneContainer).__spacePenView
    containerModel = container.model
    view1 = new TestView(id: 'view-1', text: 'View 1')
    view2 = new TestView(id: 'view-2', text: 'View 2')
    waitsForPromise ->
      atom.workspace.open('sample.js').then (o) -> editor1 = o

    waitsForPromise ->
      atom.workspace.open('sample.txt').then (o) -> editor2 = o

    runs ->
      pane = container.getRoot()
      paneModel = pane.getModel()
      paneModel.addItems([view1, editor1, view2, editor2])

  afterEach ->
    deserializerDisposable.dispose()
    jasmine.restoreDeprecationsSnapshot()

  describe "when an item is moved within the same pane", ->
    it "emits a 'pane:item-moved' event with the item and the new index", ->
      pane.on 'pane:item-moved', itemMovedHandler = jasmine.createSpy("itemMovedHandler")
      paneModel.moveItem(view1, 2)
      expect(itemMovedHandler).toHaveBeenCalled()
      expect(itemMovedHandler.argsForCall[0][1..2]).toEqual [view1, 2]

  describe "when an item is moved to another pane", ->
    it "detaches the item's view rather than removing it", ->
      container.attachToDom()
      expect(view1.is(':visible')).toBe true
      paneModel2 = paneModel.splitRight()
      view1.data('preservative', 1234)
      paneModel.moveItemToPane(view1, paneModel2, 1)
      expect(view1.data('preservative')).toBe 1234
      paneModel2.activateItemAtIndex(1)
      expect(view1.data('preservative')).toBe 1234
      expect(view1.is(':visible')).toBe true

  describe "when the title of the active item changes", ->
    describe 'when there is no onDidChangeTitle method (deprecated)', ->
      beforeEach ->
        jasmine.snapshotDeprecations()

        view1.onDidChangeTitle = null
        view2.onDidChangeTitle = null

        pane.activateItem(view2)
        pane.activateItem(view1)

      afterEach ->
        jasmine.restoreDeprecationsSnapshot()

      it "emits pane:active-item-title-changed", ->
        activeItemTitleChangedHandler = jasmine.createSpy("activeItemTitleChangedHandler")
        pane.on 'pane:active-item-title-changed', activeItemTitleChangedHandler

        expect(pane.getActiveItem()).toBe view1

        view2.trigger 'title-changed'
        expect(activeItemTitleChangedHandler).not.toHaveBeenCalled()

        view1.trigger 'title-changed'
        expect(activeItemTitleChangedHandler).toHaveBeenCalled()
        activeItemTitleChangedHandler.reset()

        pane.activateItem(view2)
        view2.trigger 'title-changed'
        expect(activeItemTitleChangedHandler).toHaveBeenCalled()

    describe 'when there is a onDidChangeTitle method', ->
      it "emits pane:active-item-title-changed", ->
        activeItemTitleChangedHandler = jasmine.createSpy("activeItemTitleChangedHandler")
        pane.on 'pane:active-item-title-changed', activeItemTitleChangedHandler

        expect(pane.getActiveItem()).toBe view1
        view2.changeTitle()
        expect(activeItemTitleChangedHandler).not.toHaveBeenCalled()

        view1.changeTitle()
        expect(activeItemTitleChangedHandler).toHaveBeenCalled()
        activeItemTitleChangedHandler.reset()

        pane.activateItem(view2)
        view2.changeTitle()
        expect(activeItemTitleChangedHandler).toHaveBeenCalled()

  describe "when an unmodifed buffer's path is deleted", ->
    it "removes the pane item", ->
      editor = null
      jasmine.unspy(window, 'setTimeout')
      filePath = path.join(temp.mkdirSync(), 'file.txt')
      fs.writeFileSync(filePath, '')

      waitsForPromise ->
        atom.workspace.open(filePath).then (o) -> editor = o

      runs ->
        pane.activateItem(editor)
        expect(pane.items).toHaveLength(5)
        fs.removeSync(filePath)

      waitsFor ->
        pane.items.length is 4

  describe "when a pane is destroyed", ->
    [pane2, pane2Model] = []

    beforeEach ->
      pane2Model = paneModel.splitRight() # Can't destroy the last pane, so we add another
      pane2 = atom.views.getView(pane2Model).__spacePenView

    it "triggers a 'pane:removed' event with the pane", ->
      removedHandler = jasmine.createSpy("removedHandler")
      container.on 'pane:removed', removedHandler
      paneModel.destroy()
      expect(removedHandler).toHaveBeenCalled()
      expect(removedHandler.argsForCall[0][1]).toBe pane

    describe "if the destroyed pane has focus", ->
      [paneToLeft, paneToRight] = []

      it "focuses the next pane", ->
        container.attachToDom()
        pane2.activate()
        expect(pane.hasFocus()).toBe false
        expect(pane2.hasFocus()).toBe true
        pane2Model.destroy()
        expect(pane.hasFocus()).toBe true

  describe "::getNextPane()", ->
    it "returns the next pane if one exists, wrapping around from the last pane to the first", ->
      pane.activateItem(editor1)
      expect(pane.getNextPane()).toBeUndefined
      pane2 = pane.splitRight(pane.copyActiveItem())
      expect(pane.getNextPane()).toBe pane2
      expect(pane2.getNextPane()).toBe pane

  describe "when the pane's active status changes", ->
    [pane2, pane2Model] = []

    beforeEach ->
      pane2Model = paneModel.splitRight(items: [pane.copyActiveItem()])
      pane2 = atom.views.getView(pane2Model).__spacePenView
      expect(pane2Model.isActive()).toBe true

    it "adds or removes the .active class as appropriate", ->
      expect(pane).not.toHaveClass('active')
      paneModel.activate()
      expect(pane).toHaveClass('active')
      pane2Model.activate()
      expect(pane).not.toHaveClass('active')

    it "triggers 'pane:became-active' or 'pane:became-inactive' according to the current status", ->
      pane.on 'pane:became-active', becameActiveHandler = jasmine.createSpy("becameActiveHandler")
      pane.on 'pane:became-inactive', becameInactiveHandler = jasmine.createSpy("becameInactiveHandler")
      paneModel.activate()

      expect(becameActiveHandler.callCount).toBe 1
      expect(becameInactiveHandler.callCount).toBe 0

      pane2Model.activate()
      expect(becameActiveHandler.callCount).toBe 1
      expect(becameInactiveHandler.callCount).toBe 1

  describe "when the pane is focused", ->
    beforeEach ->
      container.attachToDom()<commit_msg>Remove some tests for outdated pane behaviors<commit_after>PaneContainer = require '../src/pane-container'
PaneView = require '../src/pane-view'
fs = require 'fs-plus'
{Emitter, Disposable} = require 'event-kit'
{$, View} = require '../src/space-pen-extensions'
path = require 'path'
temp = require 'temp'

describe "PaneView", ->
  [container, containerModel, view1, view2, editor1, editor2, pane, paneModel, deserializerDisposable] = []

  class TestView extends View
    @deserialize: ({id, text}) -> new TestView({id, text})
    @content: ({id, text}) -> @div class: 'test-view', id: id, tabindex: -1, text
    initialize: ({@id, @text}) ->
      @emitter = new Emitter
    serialize: -> {deserializer: 'TestView', @id, @text}
    getURI: -> @id
    isEqual: (other) -> other? and @id is other.id and @text is other.text
    changeTitle: ->
      @emitter.emit 'did-change-title', 'title'
    onDidChangeTitle: (callback) ->
      @emitter.on 'did-change-title', callback
    onDidChangeModified: -> new Disposable(->)

  beforeEach ->
    jasmine.snapshotDeprecations()

    deserializerDisposable = atom.deserializers.add(TestView)
    container = atom.views.getView(new PaneContainer).__spacePenView
    containerModel = container.model
    view1 = new TestView(id: 'view-1', text: 'View 1')
    view2 = new TestView(id: 'view-2', text: 'View 2')
    waitsForPromise ->
      atom.workspace.open('sample.js').then (o) -> editor1 = o

    waitsForPromise ->
      atom.workspace.open('sample.txt').then (o) -> editor2 = o

    runs ->
      pane = container.getRoot()
      paneModel = pane.getModel()
      paneModel.addItems([view1, editor1, view2, editor2])

  afterEach ->
    deserializerDisposable.dispose()
    jasmine.restoreDeprecationsSnapshot()

  describe "when a pane is destroyed", ->
    [pane2, pane2Model] = []

    beforeEach ->
      pane2Model = paneModel.splitRight() # Can't destroy the last pane, so we add another
      pane2 = atom.views.getView(pane2Model).__spacePenView

    describe "if the destroyed pane has focus", ->
      [paneToLeft, paneToRight] = []

      it "focuses the next pane", ->
        container.attachToDom()
        pane2.activate()
        expect(pane.hasFocus()).toBe false
        expect(pane2.hasFocus()).toBe true
        pane2Model.destroy()
        expect(pane.hasFocus()).toBe true

  describe "when the pane's active status changes", ->
    [pane2, pane2Model] = []

    beforeEach ->
      pane2Model = paneModel.splitRight(items: [pane.copyActiveItem()])
      pane2 = atom.views.getView(pane2Model).__spacePenView
      expect(pane2Model.isActive()).toBe true

    it "adds or removes the .active class as appropriate", ->
      expect(pane).not.toHaveClass('active')
      paneModel.activate()
      expect(pane).toHaveClass('active')
      pane2Model.activate()
      expect(pane).not.toHaveClass('active')

  describe "when the pane is focused", ->
    beforeEach ->
      container.attachToDom()
{'fraction_non_alphanumeric': 0.12697796330453354, 'fraction_numerical': 0.01174643238520532, 'mean_word_length': 3.30685618729097, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7289833', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3150, 'n_words': 689}