2020-04-07 00:04:09 +00:00
|
|
|
import { nativeImage } from 'electron/common';
|
2024-10-03 03:52:01 +00:00
|
|
|
import { Menu, Tray } from 'electron/main';
|
|
|
|
|
|
|
|
import { expect } from 'chai';
|
|
|
|
|
2023-06-15 14:42:27 +00:00
|
|
|
import * as path from 'node:path';
|
2023-09-04 10:33:29 +00:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2018-08-27 16:58:47 +00:00
|
|
|
|
2024-10-03 03:52:01 +00:00
|
|
|
import { ifdescribe, ifit } from './lib/spec-helpers';
|
|
|
|
|
2018-08-27 16:58:47 +00:00
|
|
|
describe('tray module', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
let tray: Tray;
|
2018-08-27 16:58:47 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
beforeEach(() => { tray = new Tray(nativeImage.createEmpty()); });
|
2018-08-27 16:58:47 +00:00
|
|
|
|
2019-06-28 14:43:05 +00:00
|
|
|
afterEach(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.destroy();
|
|
|
|
tray = null as any;
|
|
|
|
});
|
2019-06-28 14:43:05 +00:00
|
|
|
|
2019-11-23 03:16:43 +00:00
|
|
|
describe('new Tray', () => {
|
2023-07-10 09:49:20 +00:00
|
|
|
it('sets the correct class name on the prototype', () => {
|
|
|
|
expect(Tray.prototype.constructor.name).to.equal('Tray');
|
|
|
|
});
|
|
|
|
|
2019-11-23 03:16:43 +00:00
|
|
|
it('throws a descriptive error for a missing file', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
|
2019-11-23 03:16:43 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray = new Tray(badPath);
|
2021-01-04 20:58:31 +00:00
|
|
|
}).to.throw(/Failed to load image from path (.+)/);
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2020-01-31 05:37:03 +00:00
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
ifit(process.platform === 'win32')('throws a descriptive error if an invalid guid is given', () => {
|
2020-01-31 05:37:03 +00:00
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray = new Tray(nativeImage.createEmpty(), 'I am not a guid');
|
|
|
|
}).to.throw('Invalid GUID format');
|
|
|
|
});
|
2020-01-31 05:37:03 +00:00
|
|
|
|
|
|
|
ifit(process.platform === 'win32')('accepts a valid guid', () => {
|
|
|
|
expect(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray = new Tray(nativeImage.createEmpty(), '0019A433-3526-48BA-A66C-676742C0FEFB');
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
2020-03-30 01:32:02 +00:00
|
|
|
|
|
|
|
it('is an instance of Tray', () => {
|
|
|
|
expect(tray).to.be.an.instanceOf(Tray);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-11-23 03:16:43 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
|
|
|
|
it('returns false by default', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const ignored = tray.getIgnoreDoubleClickEvents();
|
|
|
|
expect(ignored).to.be.false('ignored');
|
|
|
|
});
|
2019-05-08 22:40:30 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
it('can be set to true', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.setIgnoreDoubleClickEvents(true);
|
2019-07-25 04:22:08 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const ignored = tray.getIgnoreDoubleClickEvents();
|
|
|
|
expect(ignored).to.be.true('not ignored');
|
|
|
|
});
|
|
|
|
});
|
2018-08-27 16:58:47 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
describe('tray.setContextMenu(menu)', () => {
|
|
|
|
it('accepts both null and Menu as parameters', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(() => { tray.setContextMenu(new Menu()); }).to.not.throw();
|
|
|
|
expect(() => { tray.setContextMenu(null); }).to.not.throw();
|
|
|
|
});
|
|
|
|
});
|
2018-08-30 09:16:56 +00:00
|
|
|
|
2019-05-08 22:40:30 +00:00
|
|
|
describe('tray.destroy()', () => {
|
|
|
|
it('destroys a tray', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(tray.isDestroyed()).to.be.false('tray should not be destroyed');
|
|
|
|
tray.destroy();
|
2019-05-08 22:40:30 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(tray.isDestroyed()).to.be.true('tray should be destroyed');
|
|
|
|
});
|
|
|
|
});
|
2019-05-08 22:40:30 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
describe('tray.popUpContextMenu()', () => {
|
2023-09-04 10:33:29 +00:00
|
|
|
ifit(process.platform === 'win32')('can be called when menu is showing', async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
|
2023-09-04 10:33:29 +00:00
|
|
|
const timeout = setTimeout();
|
|
|
|
tray.popUpContextMenu();
|
|
|
|
await timeout;
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.popUpContextMenu();
|
|
|
|
});
|
2020-03-27 01:30:21 +00:00
|
|
|
|
|
|
|
it('can be called with a menu', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
|
|
|
|
expect(() => {
|
|
|
|
tray.popUpContextMenu(menu);
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
2020-03-30 01:32:02 +00:00
|
|
|
|
|
|
|
it('can be called with a position', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.popUpContextMenu({ x: 0, y: 0 } as any);
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can be called with a menu and a position', () => {
|
|
|
|
const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
|
|
|
|
expect(() => {
|
|
|
|
tray.popUpContextMenu(menu, { x: 0, y: 0 });
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws an error on invalid arguments', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.popUpContextMenu({} as any);
|
|
|
|
}).to.throw(/index 0/);
|
|
|
|
const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
|
|
|
|
expect(() => {
|
|
|
|
tray.popUpContextMenu(menu, {} as any);
|
|
|
|
}).to.throw(/index 1/);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2019-03-22 04:56:22 +00:00
|
|
|
|
2020-01-22 23:25:17 +00:00
|
|
|
describe('tray.closeContextMenu()', () => {
|
2023-09-04 10:33:29 +00:00
|
|
|
ifit(process.platform === 'win32')('does not crash when called more than once', async function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
|
2023-09-04 10:33:29 +00:00
|
|
|
const timeout = setTimeout();
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.popUpContextMenu();
|
2023-09-04 10:33:29 +00:00
|
|
|
await timeout;
|
|
|
|
tray.closeContextMenu();
|
|
|
|
tray.closeContextMenu();
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2020-01-22 23:25:17 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
describe('tray.getBounds()', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
afterEach(() => { tray.destroy(); });
|
2019-05-08 22:40:30 +00:00
|
|
|
|
2019-11-01 20:37:02 +00:00
|
|
|
ifit(process.platform !== 'linux')('returns a bounds object', function () {
|
2020-03-20 20:28:31 +00:00
|
|
|
const bounds = tray.getBounds();
|
|
|
|
expect(bounds).to.be.an('object').and.to.have.all.keys('x', 'y', 'width', 'height');
|
|
|
|
});
|
|
|
|
});
|
2018-08-30 09:16:56 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
describe('tray.setImage(image)', () => {
|
2021-01-04 20:58:31 +00:00
|
|
|
it('throws a descriptive error for a missing file', () => {
|
|
|
|
const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
|
|
|
|
expect(() => {
|
|
|
|
tray.setImage(badPath);
|
|
|
|
}).to.throw(/Failed to load image from path (.+)/);
|
|
|
|
});
|
|
|
|
|
2018-08-30 09:16:56 +00:00
|
|
|
it('accepts empty image', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.setImage(nativeImage.createEmpty());
|
|
|
|
});
|
|
|
|
});
|
2018-08-30 09:16:56 +00:00
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
describe('tray.setPressedImage(image)', () => {
|
2021-01-04 20:58:31 +00:00
|
|
|
it('throws a descriptive error for a missing file', () => {
|
|
|
|
const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
|
|
|
|
expect(() => {
|
|
|
|
tray.setPressedImage(badPath);
|
|
|
|
}).to.throw(/Failed to load image from path (.+)/);
|
|
|
|
});
|
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
it('accepts empty image', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tray.setPressedImage(nativeImage.createEmpty());
|
|
|
|
});
|
|
|
|
});
|
2018-08-30 09:16:56 +00:00
|
|
|
|
2021-01-04 20:58:31 +00:00
|
|
|
ifdescribe(process.platform === 'win32')('tray.displayBalloon(image)', () => {
|
|
|
|
it('throws a descriptive error for a missing file', () => {
|
|
|
|
const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
|
|
|
|
expect(() => {
|
|
|
|
tray.displayBalloon({
|
|
|
|
title: 'title',
|
|
|
|
content: 'wow content',
|
|
|
|
icon: badPath
|
|
|
|
});
|
|
|
|
}).to.throw(/Failed to load image from path (.+)/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('accepts an empty image', () => {
|
|
|
|
tray.displayBalloon({
|
|
|
|
title: 'title',
|
|
|
|
content: 'wow content',
|
|
|
|
icon: nativeImage.createEmpty()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-07-25 04:22:08 +00:00
|
|
|
ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
|
2019-03-18 19:40:34 +00:00
|
|
|
it('sets/gets non-empty title', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const title = 'Hello World!';
|
|
|
|
tray.setTitle(title);
|
|
|
|
const newTitle = tray.getTitle();
|
2019-03-18 19:40:34 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
expect(newTitle).to.equal(title);
|
|
|
|
});
|
2019-03-18 19:40:34 +00:00
|
|
|
|
|
|
|
it('sets/gets empty title', () => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const title = '';
|
|
|
|
tray.setTitle(title);
|
|
|
|
const newTitle = tray.getTitle();
|
|
|
|
|
|
|
|
expect(newTitle).to.equal(title);
|
|
|
|
});
|
2020-08-23 21:39:29 +00:00
|
|
|
|
|
|
|
it('can have an options object passed in', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.setTitle('Hello World!', {});
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when the options parameter is not an object', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.setTitle('Hello World!', 'test' as any);
|
|
|
|
}).to.throw(/setTitle options must be an object/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can have a font type option set', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.setTitle('Hello World!', { fontType: 'monospaced' });
|
|
|
|
tray.setTitle('Hello World!', { fontType: 'monospacedDigit' });
|
|
|
|
}).to.not.throw();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when the font type is specified but is not a string', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.setTitle('Hello World!', { fontType: 5.4 as any });
|
|
|
|
}).to.throw(/fontType must be one of 'monospaced' or 'monospacedDigit'/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws on invalid font types', () => {
|
|
|
|
expect(() => {
|
|
|
|
tray.setTitle('Hello World!', { fontType: 'blep' as any });
|
|
|
|
}).to.throw(/fontType must be one of 'monospaced' or 'monospacedDigit'/);
|
|
|
|
});
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|