electron/spec-main/api-tray-spec.ts

163 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import { Menu, Tray, nativeImage } from 'electron';
import { ifdescribe, ifit } from './spec-helpers';
import * as path from 'path';
describe('tray module', () => {
2020-03-20 20:28:31 +00:00
let tray: Tray;
2020-03-20 20:28:31 +00:00
beforeEach(() => { tray = new Tray(nativeImage.createEmpty()); });
afterEach(() => {
2020-03-20 20:28:31 +00:00
tray.destroy();
tray = null as any;
});
describe('new Tray', () => {
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');
expect(() => {
2020-03-20 20:28:31 +00:00
tray = new Tray(badPath);
}).to.throw(/Error processing argument at index 0/);
2020-03-20 20:28:31 +00:00
});
ifit(process.platform === 'win32')('throws a descriptive error if an invlaid guid is given', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
tray = new Tray(nativeImage.createEmpty(), 'I am not a guid');
}).to.throw('Invalid GUID format');
});
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();
});
it('is an instance of Tray', () => {
expect(tray).to.be.an.instanceOf(Tray);
});
2020-03-20 20:28:31 +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');
});
it('can be set to true', () => {
2020-03-20 20:28:31 +00:00
tray.setIgnoreDoubleClickEvents(true);
2020-03-20 20:28:31 +00:00
const ignored = tray.getIgnoreDoubleClickEvents();
expect(ignored).to.be.true('not ignored');
});
});
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();
});
});
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();
2020-03-20 20:28:31 +00:00
expect(tray.isDestroyed()).to.be.true('tray should be destroyed');
});
});
describe('tray.popUpContextMenu()', () => {
ifit(process.platform === 'win32')('can be called when menu is showing', function (done) {
2020-03-20 20:28:31 +00:00
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => {
2020-03-20 20:28:31 +00:00
tray.popUpContextMenu();
done();
});
tray.popUpContextMenu();
});
it('can be called with a menu', () => {
const menu = Menu.buildFromTemplate([{ label: 'Test' }]);
expect(() => {
tray.popUpContextMenu(menu);
}).to.not.throw();
});
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
});
describe('tray.closeContextMenu()', () => {
ifit(process.platform === 'win32')('does not crash when called more than once', function (done) {
2020-03-20 20:28:31 +00:00
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => {
2020-03-20 20:28:31 +00:00
tray.closeContextMenu();
tray.closeContextMenu();
done();
});
tray.popUpContextMenu();
});
});
describe('tray.getBounds()', () => {
2020-03-20 20:28:31 +00:00
afterEach(() => { tray.destroy(); });
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');
});
});
describe('tray.setImage(image)', () => {
it('accepts empty image', () => {
2020-03-20 20:28:31 +00:00
tray.setImage(nativeImage.createEmpty());
});
});
describe('tray.setPressedImage(image)', () => {
it('accepts empty image', () => {
2020-03-20 20:28:31 +00:00
tray.setPressedImage(nativeImage.createEmpty());
});
});
ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
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();
2020-03-20 20:28:31 +00:00
expect(newTitle).to.equal(title);
});
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);
});
});
});