api-tray-spec.ts 7.1 KB

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