api-tray-spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { expect } from 'chai'
  2. import { Menu, Tray, nativeImage } from 'electron'
  3. import { ifdescribe, ifit } from './spec-helpers'
  4. describe('tray module', () => {
  5. let tray: Tray;
  6. beforeEach(() => { tray = new Tray(nativeImage.createEmpty()) })
  7. afterEach(() => {
  8. tray.destroy()
  9. tray = null as any
  10. })
  11. ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
  12. it('returns false by default', () => {
  13. const ignored = tray.getIgnoreDoubleClickEvents()
  14. expect(ignored).to.be.false('ignored')
  15. })
  16. it('can be set to true', () => {
  17. tray.setIgnoreDoubleClickEvents(true)
  18. const ignored = tray.getIgnoreDoubleClickEvents()
  19. expect(ignored).to.be.true('not ignored')
  20. })
  21. })
  22. describe('tray.setContextMenu(menu)', () => {
  23. it('accepts both null and Menu as parameters', () => {
  24. expect(() => { tray.setContextMenu(new Menu()) }).to.not.throw()
  25. expect(() => { tray.setContextMenu(null) }).to.not.throw()
  26. })
  27. })
  28. describe('tray.destroy()', () => {
  29. it('destroys a tray', () => {
  30. expect(tray.isDestroyed()).to.be.false('tray should not be destroyed')
  31. tray.destroy()
  32. expect(tray.isDestroyed()).to.be.true('tray should be destroyed')
  33. })
  34. })
  35. describe('tray.popUpContextMenu()', () => {
  36. ifit(process.platform === 'win32')('can be called when menu is showing', function (done) {
  37. tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]))
  38. setTimeout(() => {
  39. tray.popUpContextMenu()
  40. done()
  41. })
  42. tray.popUpContextMenu()
  43. })
  44. it('can be called with a menu', () => {
  45. const menu = Menu.buildFromTemplate([{ label: 'Test' }])
  46. expect(() => {
  47. tray.popUpContextMenu(menu)
  48. }).to.not.throw()
  49. })
  50. })
  51. describe('tray.getBounds()', () => {
  52. afterEach(() => { tray.destroy() })
  53. ifit(process.platform !== 'linux') ('returns a bounds object', function () {
  54. const bounds = tray.getBounds()
  55. expect(bounds).to.be.an('object').and.to.have.all.keys('x', 'y', 'width', 'height');
  56. })
  57. })
  58. describe('tray.setImage(image)', () => {
  59. it('accepts empty image', () => {
  60. tray.setImage(nativeImage.createEmpty())
  61. })
  62. })
  63. describe('tray.setPressedImage(image)', () => {
  64. it('accepts empty image', () => {
  65. tray.setPressedImage(nativeImage.createEmpty())
  66. })
  67. })
  68. ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
  69. it('sets/gets non-empty title', () => {
  70. const title = 'Hello World!'
  71. tray.setTitle(title)
  72. const newTitle = tray.getTitle()
  73. expect(newTitle).to.equal(title)
  74. })
  75. it('sets/gets empty title', () => {
  76. const title = ''
  77. tray.setTitle(title)
  78. const newTitle = tray.getTitle()
  79. expect(newTitle).to.equal(title)
  80. })
  81. })
  82. })