Skip to content

Commit

Permalink
🐛 Fix plugin activation when displayPluginControls is disabled
Browse files Browse the repository at this point in the history
Fixes #458
  • Loading branch information
abe33 committed Mar 14, 2016
1 parent 2d82e1e commit 966eb29
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 85 deletions.
33 changes: 26 additions & 7 deletions lib/mixins/plugin-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,36 @@ export default class PluginManagement extends Mixin {
const plugin = this.plugins[name]
const pluginActive = plugin.isActive()
const settingActive = atom.config.get(`minimap.plugins.${name}`)
const event = { name: name, plugin: plugin }

if (settingActive && !pluginActive) {
plugin.activatePlugin()
this.emitter.emit('did-activate-plugin', event)
} else if (pluginActive && !settingActive) {
plugin.deactivatePlugin()
this.emitter.emit('did-deactivate-plugin', event)
if (atom.config.get('minimap.displayPluginsControls')) {
if (settingActive && !pluginActive) {
this.activatePlugin(name, plugin)
} else if (pluginActive && !settingActive) {
this.deactivatePlugin(name, plugin)
}
} else {
if (!pluginActive) {
this.activatePlugin(name, plugin)
} else if (pluginActive) {
this.deactivatePlugin(name, plugin)
}
}
}

activatePlugin (name, plugin) {
const event = { name: name, plugin: plugin }

plugin.activatePlugin()
this.emitter.emit('did-activate-plugin', event)
}

deactivatePlugin (name, plugin) {
const event = { name: name, plugin: plugin }

plugin.deactivatePlugin()
this.emitter.emit('did-deactivate-plugin', event)
}

/**
* When the `minimap.displayPluginsControls` setting is toggled,
* this function will register the commands and setting to manage the plugin
Expand Down
223 changes: 145 additions & 78 deletions spec/minimap-main-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,126 +112,193 @@ describe('Minimap package', () => {
describe('plugins', () => {
let [registerHandler, unregisterHandler, plugin] = []

beforeEach(() => {
atom.config.set('minimap.displayPluginsControls', true)
atom.config.set('minimap.plugins.dummy', undefined)

plugin = {
active: false,
activatePlugin () { this.active = true },
deactivatePlugin () { this.active = false },
isActive () { return this.active }
}

spyOn(plugin, 'activatePlugin').andCallThrough()
spyOn(plugin, 'deactivatePlugin').andCallThrough()

registerHandler = jasmine.createSpy('register handler')
unregisterHandler = jasmine.createSpy('unregister handler')
})

describe('when registered', () => {
describe('when the displayPluginsControls setting is enabled', () => {
beforeEach(() => {
minimapPackage.onDidAddPlugin(registerHandler)
minimapPackage.onDidRemovePlugin(unregisterHandler)
minimapPackage.registerPlugin('dummy', plugin)
})
atom.config.set('minimap.displayPluginsControls', true)
atom.config.set('minimap.plugins.dummy', undefined)

it('makes the plugin available in the minimap', () => {
expect(minimapPackage.plugins['dummy']).toBe(plugin)
})
plugin = {
active: false,
activatePlugin () { this.active = true },
deactivatePlugin () { this.active = false },
isActive () { return this.active }
}

it('emits an event', () => {
expect(registerHandler).toHaveBeenCalled()
})
spyOn(plugin, 'activatePlugin').andCallThrough()
spyOn(plugin, 'deactivatePlugin').andCallThrough()

it('creates a default config for the plugin', () => {
expect(minimapPackage.config.plugins.properties.dummy).toBeDefined()
expect(minimapPackage.config.plugins.properties.dummyDecorationsZIndex).toBeDefined()
registerHandler = jasmine.createSpy('register handler')
unregisterHandler = jasmine.createSpy('unregister handler')
})

it('sets the corresponding config', () => {
expect(atom.config.get('minimap.plugins.dummy')).toBeTruthy()
expect(atom.config.get('minimap.plugins.dummyDecorationsZIndex')).toEqual(0)
})

describe('triggering the corresponding plugin command', () => {
describe('when registered', () => {
beforeEach(() => {
atom.commands.dispatch(workspaceElement, 'minimap:toggle-dummy')
minimapPackage.onDidAddPlugin(registerHandler)
minimapPackage.onDidRemovePlugin(unregisterHandler)
minimapPackage.registerPlugin('dummy', plugin)
})

it('receives a deactivation call', () => {
expect(plugin.deactivatePlugin).toHaveBeenCalled()
it('makes the plugin available in the minimap', () => {
expect(minimapPackage.plugins['dummy']).toBe(plugin)
})
})

describe('and then unregistered', () => {
beforeEach(() => {
minimapPackage.unregisterPlugin('dummy')
it('emits an event', () => {
expect(registerHandler).toHaveBeenCalled()
})

it('has been unregistered', () => {
expect(minimapPackage.plugins['dummy']).toBeUndefined()
it('creates a default config for the plugin', () => {
expect(minimapPackage.config.plugins.properties.dummy).toBeDefined()
expect(minimapPackage.config.plugins.properties.dummyDecorationsZIndex).toBeDefined()
})

it('emits an event', () => {
expect(unregisterHandler).toHaveBeenCalled()
it('sets the corresponding config', () => {
expect(atom.config.get('minimap.plugins.dummy')).toBeTruthy()
expect(atom.config.get('minimap.plugins.dummyDecorationsZIndex')).toEqual(0)
})

describe('when the config is modified', () => {
describe('triggering the corresponding plugin command', () => {
beforeEach(() => {
atom.config.set('minimap.plugins.dummy', false)
atom.commands.dispatch(workspaceElement, 'minimap:toggle-dummy')
})

it('does not activates the plugin', () => {
expect(plugin.deactivatePlugin).not.toHaveBeenCalled()
it('receives a deactivation call', () => {
expect(plugin.deactivatePlugin).toHaveBeenCalled()
})
})

describe('and then unregistered', () => {
beforeEach(() => {
minimapPackage.unregisterPlugin('dummy')
})

it('has been unregistered', () => {
expect(minimapPackage.plugins['dummy']).toBeUndefined()
})

it('emits an event', () => {
expect(unregisterHandler).toHaveBeenCalled()
})

describe('when the config is modified', () => {
beforeEach(() => {
atom.config.set('minimap.plugins.dummy', false)
})

it('does not activates the plugin', () => {
expect(plugin.deactivatePlugin).not.toHaveBeenCalled()
})
})
})

describe('on minimap deactivation', () => {
beforeEach(() => {
expect(plugin.active).toBeTruthy()
minimapPackage.deactivate()
})

it('deactivates all the plugins', () => {
expect(plugin.active).toBeFalsy()
})
})
})

describe('on minimap deactivation', () => {
describe('when the config for it is false', () => {
beforeEach(() => {
expect(plugin.active).toBeTruthy()
minimapPackage.deactivate()
atom.config.set('minimap.plugins.dummy', false)
minimapPackage.registerPlugin('dummy', plugin)
})

it('deactivates all the plugins', () => {
expect(plugin.active).toBeFalsy()
it('does not receive an activation call', () => {
expect(plugin.activatePlugin).not.toHaveBeenCalled()
})
})
})

describe('when the config for it is false', () => {
beforeEach(() => {
atom.config.set('minimap.plugins.dummy', false)
minimapPackage.registerPlugin('dummy', plugin)
})
describe('the registered plugin', () => {
beforeEach(() => {
minimapPackage.registerPlugin('dummy', plugin)
})

it('receives an activation call', () => {
expect(plugin.activatePlugin).toHaveBeenCalled()
})

it('does not receive an activation call', () => {
expect(plugin.activatePlugin).not.toHaveBeenCalled()
it('activates the plugin', () => {
expect(plugin.active).toBeTruthy()
})

describe('when the config is modified after registration', () => {
beforeEach(() => {
atom.config.set('minimap.plugins.dummy', false)
})

it('receives a deactivation call', () => {
expect(plugin.deactivatePlugin).toHaveBeenCalled()
})
})
})
})

describe('the registered plugin', () => {
describe('when the displayPluginsControls setting is disabled', () => {
beforeEach(() => {
minimapPackage.registerPlugin('dummy', plugin)
})
atom.config.set('minimap.displayPluginsControls', false)
atom.config.set('minimap.plugins.dummy', undefined)

it('receives an activation call', () => {
expect(plugin.activatePlugin).toHaveBeenCalled()
})
plugin = {
active: false,
activatePlugin () { this.active = true },
deactivatePlugin () { this.active = false },
isActive () { return this.active }
}

spyOn(plugin, 'activatePlugin').andCallThrough()
spyOn(plugin, 'deactivatePlugin').andCallThrough()

it('activates the plugin', () => {
expect(plugin.active).toBeTruthy()
registerHandler = jasmine.createSpy('register handler')
unregisterHandler = jasmine.createSpy('unregister handler')
})

describe('when the config is modified after registration', () => {
describe('when registered', () => {
beforeEach(() => {
atom.config.set('minimap.plugins.dummy', false)
minimapPackage.onDidAddPlugin(registerHandler)
minimapPackage.onDidRemovePlugin(unregisterHandler)
minimapPackage.registerPlugin('dummy', plugin)
})

it('receives a deactivation call', () => {
expect(plugin.deactivatePlugin).toHaveBeenCalled()
it('makes the plugin available in the minimap', () => {
expect(minimapPackage.plugins['dummy']).toBe(plugin)
})

it('emits an event', () => {
expect(registerHandler).toHaveBeenCalled()
})

it('still activates the package', () => {
expect(plugin.isActive()).toBeTruthy()
})

describe('and then unregistered', () => {
beforeEach(() => {
minimapPackage.unregisterPlugin('dummy')
})

it('has been unregistered', () => {
expect(minimapPackage.plugins['dummy']).toBeUndefined()
})

it('emits an event', () => {
expect(unregisterHandler).toHaveBeenCalled()
})
})

describe('on minimap deactivation', () => {
beforeEach(() => {
expect(plugin.active).toBeTruthy()
minimapPackage.deactivate()
})

it('deactivates all the plugins', () => {
expect(plugin.active).toBeFalsy()
})
})
})
})
Expand Down

0 comments on commit 966eb29

Please sign in to comment.