api-tray-spec.ts 7.0 KB

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