api-tray-spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { expect } from 'chai';
  2. import { Menu, Tray, nativeImage } from 'electron';
  3. import { ifdescribe, ifit } from './spec-helpers';
  4. import * as path from 'path';
  5. describe('tray module', () => {
  6. let tray: Tray;
  7. beforeEach(() => { tray = new Tray(nativeImage.createEmpty()); });
  8. afterEach(() => {
  9. tray.destroy();
  10. tray = null as any;
  11. });
  12. describe('new Tray', () => {
  13. it('throws a descriptive error for a missing file', () => {
  14. const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
  15. expect(() => {
  16. tray = new Tray(badPath);
  17. }).to.throw(/Image could not be created from .*/);
  18. });
  19. ifit(process.platform === 'win32')('throws a descriptive error if an invlaid guid is given', () => {
  20. expect(() => {
  21. tray = new Tray(nativeImage.createEmpty(), 'I am not a guid');
  22. }).to.throw('Invalid GUID format');
  23. });
  24. ifit(process.platform === 'win32')('accepts a valid guid', () => {
  25. expect(() => {
  26. tray = new Tray(nativeImage.createEmpty(), '0019A433-3526-48BA-A66C-676742C0FEFB');
  27. }).to.not.throw();
  28. });
  29. });
  30. ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
  31. it('returns false by default', () => {
  32. const ignored = tray.getIgnoreDoubleClickEvents();
  33. expect(ignored).to.be.false('ignored');
  34. });
  35. it('can be set to true', () => {
  36. tray.setIgnoreDoubleClickEvents(true);
  37. const ignored = tray.getIgnoreDoubleClickEvents();
  38. expect(ignored).to.be.true('not ignored');
  39. });
  40. });
  41. describe('tray.setContextMenu(menu)', () => {
  42. it('accepts both null and Menu as parameters', () => {
  43. expect(() => { tray.setContextMenu(new Menu()); }).to.not.throw();
  44. expect(() => { tray.setContextMenu(null); }).to.not.throw();
  45. });
  46. });
  47. describe('tray.destroy()', () => {
  48. it('destroys a tray', () => {
  49. expect(tray.isDestroyed()).to.be.false('tray should not be destroyed');
  50. tray.destroy();
  51. expect(tray.isDestroyed()).to.be.true('tray should be destroyed');
  52. });
  53. });
  54. describe('tray.popUpContextMenu()', () => {
  55. ifit(process.platform === 'win32')('can be called when menu is showing', function (done) {
  56. tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
  57. setTimeout(() => {
  58. tray.popUpContextMenu();
  59. done();
  60. });
  61. tray.popUpContextMenu();
  62. });
  63. it('can be called with a menu', () => {
  64. const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
  65. expect(() => {
  66. tray.popUpContextMenu(menu);
  67. }).to.not.throw();
  68. });
  69. });
  70. describe('tray.closeContextMenu()', () => {
  71. ifit(process.platform === 'win32')('does not crash when called more than once', function (done) {
  72. tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
  73. setTimeout(() => {
  74. tray.closeContextMenu();
  75. tray.closeContextMenu();
  76. done();
  77. });
  78. tray.popUpContextMenu();
  79. });
  80. });
  81. describe('tray.getBounds()', () => {
  82. afterEach(() => { tray.destroy(); });
  83. ifit(process.platform !== 'linux')('returns a bounds object', function () {
  84. const bounds = tray.getBounds();
  85. expect(bounds).to.be.an('object').and.to.have.all.keys('x', 'y', 'width', 'height');
  86. });
  87. });
  88. describe('tray.setImage(image)', () => {
  89. it('accepts empty image', () => {
  90. tray.setImage(nativeImage.createEmpty());
  91. });
  92. });
  93. describe('tray.setPressedImage(image)', () => {
  94. it('accepts empty image', () => {
  95. tray.setPressedImage(nativeImage.createEmpty());
  96. });
  97. });
  98. ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
  99. it('sets/gets non-empty title', () => {
  100. const title = 'Hello World!';
  101. tray.setTitle(title);
  102. const newTitle = tray.getTitle();
  103. expect(newTitle).to.equal(title);
  104. });
  105. it('sets/gets empty title', () => {
  106. const title = '';
  107. tray.setTitle(title);
  108. const newTitle = tray.getTitle();
  109. expect(newTitle).to.equal(title);
  110. });
  111. });
  112. });