api-tray-spec.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import { expect } from 'chai';
  2. import { Menu, Tray } from 'electron/main';
  3. import { nativeImage } from 'electron/common';
  4. import { ifdescribe, ifit } from './lib/spec-helpers';
  5. import * as path from 'node:path';
  6. import { setTimeout } from 'node:timers/promises';
  7. describe('tray module', () => {
  8. let tray: Tray;
  9. beforeEach(() => { tray = new Tray(nativeImage.createEmpty()); });
  10. afterEach(() => {
  11. tray.destroy();
  12. tray = null as any;
  13. });
  14. describe('new Tray', () => {
  15. it('sets the correct class name on the prototype', () => {
  16. expect(Tray.prototype.constructor.name).to.equal('Tray');
  17. });
  18. it('throws a descriptive error for a missing file', () => {
  19. const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
  20. expect(() => {
  21. tray = new Tray(badPath);
  22. }).to.throw(/Failed to load image from path (.+)/);
  23. });
  24. ifit(process.platform === 'win32')('throws a descriptive error if an invalid guid is given', () => {
  25. expect(() => {
  26. tray = new Tray(nativeImage.createEmpty(), 'I am not a guid');
  27. }).to.throw('Invalid GUID format');
  28. });
  29. ifit(process.platform === 'win32')('accepts a valid guid', () => {
  30. expect(() => {
  31. tray = new Tray(nativeImage.createEmpty(), '0019A433-3526-48BA-A66C-676742C0FEFB');
  32. }).to.not.throw();
  33. });
  34. it('is an instance of Tray', () => {
  35. expect(tray).to.be.an.instanceOf(Tray);
  36. });
  37. });
  38. ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
  39. it('returns false by default', () => {
  40. const ignored = tray.getIgnoreDoubleClickEvents();
  41. expect(ignored).to.be.false('ignored');
  42. });
  43. it('can be set to true', () => {
  44. tray.setIgnoreDoubleClickEvents(true);
  45. const ignored = tray.getIgnoreDoubleClickEvents();
  46. expect(ignored).to.be.true('not ignored');
  47. });
  48. });
  49. describe('tray.setContextMenu(menu)', () => {
  50. it('accepts both null and Menu as parameters', () => {
  51. expect(() => { tray.setContextMenu(new Menu()); }).to.not.throw();
  52. expect(() => { tray.setContextMenu(null); }).to.not.throw();
  53. });
  54. });
  55. describe('tray.destroy()', () => {
  56. it('destroys a tray', () => {
  57. expect(tray.isDestroyed()).to.be.false('tray should not be destroyed');
  58. tray.destroy();
  59. expect(tray.isDestroyed()).to.be.true('tray should be destroyed');
  60. });
  61. });
  62. describe('tray.popUpContextMenu()', () => {
  63. ifit(process.platform === 'win32')('can be called when menu is showing', async function () {
  64. tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
  65. const timeout = setTimeout();
  66. tray.popUpContextMenu();
  67. await timeout;
  68. tray.popUpContextMenu();
  69. });
  70. it('can be called with a menu', () => {
  71. const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
  72. expect(() => {
  73. tray.popUpContextMenu(menu);
  74. }).to.not.throw();
  75. });
  76. it('can be called with a position', () => {
  77. expect(() => {
  78. tray.popUpContextMenu({ x: 0, y: 0 } as any);
  79. }).to.not.throw();
  80. });
  81. it('can be called with a menu and a position', () => {
  82. const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
  83. expect(() => {
  84. tray.popUpContextMenu(menu, { x: 0, y: 0 });
  85. }).to.not.throw();
  86. });
  87. it('throws an error on invalid arguments', () => {
  88. expect(() => {
  89. tray.popUpContextMenu({} as any);
  90. }).to.throw(/index 0/);
  91. const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
  92. expect(() => {
  93. tray.popUpContextMenu(menu, {} as any);
  94. }).to.throw(/index 1/);
  95. });
  96. });
  97. describe('tray.closeContextMenu()', () => {
  98. ifit(process.platform === 'win32')('does not crash when called more than once', async function () {
  99. tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
  100. const timeout = setTimeout();
  101. tray.popUpContextMenu();
  102. await timeout;
  103. tray.closeContextMenu();
  104. tray.closeContextMenu();
  105. });
  106. });
  107. describe('tray.getBounds()', () => {
  108. afterEach(() => { tray.destroy(); });
  109. ifit(process.platform !== 'linux')('returns a bounds object', function () {
  110. const bounds = tray.getBounds();
  111. expect(bounds).to.be.an('object').and.to.have.all.keys('x', 'y', 'width', 'height');
  112. });
  113. });
  114. describe('tray.setImage(image)', () => {
  115. it('throws a descriptive error for a missing file', () => {
  116. const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
  117. expect(() => {
  118. tray.setImage(badPath);
  119. }).to.throw(/Failed to load image from path (.+)/);
  120. });
  121. it('accepts empty image', () => {
  122. tray.setImage(nativeImage.createEmpty());
  123. });
  124. });
  125. describe('tray.setPressedImage(image)', () => {
  126. it('throws a descriptive error for a missing file', () => {
  127. const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
  128. expect(() => {
  129. tray.setPressedImage(badPath);
  130. }).to.throw(/Failed to load image from path (.+)/);
  131. });
  132. it('accepts empty image', () => {
  133. tray.setPressedImage(nativeImage.createEmpty());
  134. });
  135. });
  136. ifdescribe(process.platform === 'win32')('tray.displayBalloon(image)', () => {
  137. it('throws a descriptive error for a missing file', () => {
  138. const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
  139. expect(() => {
  140. tray.displayBalloon({
  141. title: 'title',
  142. content: 'wow content',
  143. icon: badPath
  144. });
  145. }).to.throw(/Failed to load image from path (.+)/);
  146. });
  147. it('accepts an empty image', () => {
  148. tray.displayBalloon({
  149. title: 'title',
  150. content: 'wow content',
  151. icon: nativeImage.createEmpty()
  152. });
  153. });
  154. });
  155. ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
  156. it('sets/gets non-empty title', () => {
  157. const title = 'Hello World!';
  158. tray.setTitle(title);
  159. const newTitle = tray.getTitle();
  160. expect(newTitle).to.equal(title);
  161. });
  162. it('sets/gets empty title', () => {
  163. const title = '';
  164. tray.setTitle(title);
  165. const newTitle = tray.getTitle();
  166. expect(newTitle).to.equal(title);
  167. });
  168. it('can have an options object passed in', () => {
  169. expect(() => {
  170. tray.setTitle('Hello World!', {});
  171. }).to.not.throw();
  172. });
  173. it('throws when the options parameter is not an object', () => {
  174. expect(() => {
  175. tray.setTitle('Hello World!', 'test' as any);
  176. }).to.throw(/setTitle options must be an object/);
  177. });
  178. it('can have a font type option set', () => {
  179. expect(() => {
  180. tray.setTitle('Hello World!', { fontType: 'monospaced' });
  181. tray.setTitle('Hello World!', { fontType: 'monospacedDigit' });
  182. }).to.not.throw();
  183. });
  184. it('throws when the font type is specified but is not a string', () => {
  185. expect(() => {
  186. tray.setTitle('Hello World!', { fontType: 5.4 as any });
  187. }).to.throw(/fontType must be one of 'monospaced' or 'monospacedDigit'/);
  188. });
  189. it('throws on invalid font types', () => {
  190. expect(() => {
  191. tray.setTitle('Hello World!', { fontType: 'blep' as any });
  192. }).to.throw(/fontType must be one of 'monospaced' or 'monospacedDigit'/);
  193. });
  194. });
  195. });