forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-tray-spec.ts
233 lines (196 loc) · 7.09 KB
/
api-tray-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { expect } from 'chai';
import { Menu, Tray } from 'electron/main';
import { nativeImage } from 'electron/common';
import { ifdescribe, ifit } from './lib/spec-helpers';
import * as path from 'node:path';
describe('tray module', () => {
let tray: Tray;
beforeEach(() => { tray = new Tray(nativeImage.createEmpty()); });
afterEach(() => {
tray.destroy();
tray = null as any;
});
describe('new Tray', () => {
it('sets the correct class name on the prototype', () => {
expect(Tray.prototype.constructor.name).to.equal('Tray');
});
it('throws a descriptive error for a missing file', () => {
const badPath = path.resolve('I', 'Do', 'Not', 'Exist');
expect(() => {
tray = new Tray(badPath);
}).to.throw(/Failed to load image from path (.+)/);
});
ifit(process.platform === 'win32')('throws a descriptive error if an invalid guid is given', () => {
expect(() => {
tray = new Tray(nativeImage.createEmpty(), 'I am not a guid');
}).to.throw('Invalid GUID format');
});
ifit(process.platform === 'win32')('accepts a valid guid', () => {
expect(() => {
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);
});
});
ifdescribe(process.platform === 'darwin')('tray get/set ignoreDoubleClickEvents', () => {
it('returns false by default', () => {
const ignored = tray.getIgnoreDoubleClickEvents();
expect(ignored).to.be.false('ignored');
});
it('can be set to true', () => {
tray.setIgnoreDoubleClickEvents(true);
const ignored = tray.getIgnoreDoubleClickEvents();
expect(ignored).to.be.true('not ignored');
});
});
describe('tray.setContextMenu(menu)', () => {
it('accepts both null and Menu as parameters', () => {
expect(() => { tray.setContextMenu(new Menu()); }).to.not.throw();
expect(() => { tray.setContextMenu(null); }).to.not.throw();
});
});
describe('tray.destroy()', () => {
it('destroys a tray', () => {
expect(tray.isDestroyed()).to.be.false('tray should not be destroyed');
tray.destroy();
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) {
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => {
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/);
});
});
describe('tray.closeContextMenu()', () => {
ifit(process.platform === 'win32')('does not crash when called more than once', function (done) {
tray.setContextMenu(Menu.buildFromTemplate([{ label: 'Test' }]));
setTimeout(() => {
tray.closeContextMenu();
tray.closeContextMenu();
done();
});
tray.popUpContextMenu();
});
});
describe('tray.getBounds()', () => {
afterEach(() => { tray.destroy(); });
ifit(process.platform !== 'linux')('returns a bounds object', function () {
const bounds = tray.getBounds();
expect(bounds).to.be.an('object').and.to.have.all.keys('x', 'y', 'width', 'height');
});
});
describe('tray.setImage(image)', () => {
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 (.+)/);
});
it('accepts empty image', () => {
tray.setImage(nativeImage.createEmpty());
});
});
describe('tray.setPressedImage(image)', () => {
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 (.+)/);
});
it('accepts empty image', () => {
tray.setPressedImage(nativeImage.createEmpty());
});
});
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()
});
});
});
ifdescribe(process.platform === 'darwin')('tray get/set title', () => {
it('sets/gets non-empty title', () => {
const title = 'Hello World!';
tray.setTitle(title);
const newTitle = tray.getTitle();
expect(newTitle).to.equal(title);
});
it('sets/gets empty title', () => {
const title = '';
tray.setTitle(title);
const newTitle = tray.getTitle();
expect(newTitle).to.equal(title);
});
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'/);
});
});
});