api-menu-item-spec.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. import { BrowserWindow, app, Menu, MenuItem, MenuItemConstructorOptions } from 'electron/main';
  2. import { expect } from 'chai';
  3. import { ifdescribe } from './lib/spec-helpers';
  4. import { closeAllWindows } from './lib/window-helpers';
  5. import { roleList, execute } from '../lib/browser/api/menu-item-roles';
  6. function keys<Key extends string, Value> (record: Record<Key, Value>) {
  7. return Object.keys(record) as Key[];
  8. }
  9. describe('MenuItems', () => {
  10. describe('MenuItem instance properties', () => {
  11. it('should have default MenuItem properties', () => {
  12. const item = new MenuItem({
  13. id: '1',
  14. label: 'hello',
  15. role: 'close',
  16. sublabel: 'goodbye',
  17. accelerator: 'CmdOrControl+Q',
  18. click: () => { },
  19. enabled: true,
  20. visible: true,
  21. checked: false,
  22. type: 'normal',
  23. registerAccelerator: true,
  24. submenu: [{ role: 'about' }]
  25. });
  26. expect(item).to.have.property('id').that.is.a('string');
  27. expect(item).to.have.property('label').that.is.a('string').equal('hello');
  28. expect(item).to.have.property('sublabel').that.is.a('string').equal('goodbye');
  29. expect(item).to.have.property('accelerator').that.is.a('string').equal('CmdOrControl+Q');
  30. expect(item).to.have.property('click').that.is.a('function');
  31. expect(item).to.have.property('enabled').that.is.a('boolean').and.is.true('item is enabled');
  32. expect(item).to.have.property('visible').that.is.a('boolean').and.is.true('item is visible');
  33. expect(item).to.have.property('checked').that.is.a('boolean').and.is.false('item is not checked');
  34. expect(item).to.have.property('registerAccelerator').that.is.a('boolean').and.is.true('item can register accelerator');
  35. expect(item).to.have.property('type').that.is.a('string').equal('normal');
  36. expect(item).to.have.property('commandId').that.is.a('number');
  37. expect(item).to.have.property('toolTip').that.is.a('string');
  38. expect(item).to.have.property('role').that.is.a('string');
  39. expect(item).to.have.property('icon');
  40. });
  41. });
  42. describe('MenuItem.click', () => {
  43. it('should be called with the item object passed', done => {
  44. const menu = Menu.buildFromTemplate([{
  45. label: 'text',
  46. click: (item) => {
  47. try {
  48. expect(item.constructor.name).to.equal('MenuItem');
  49. expect(item.label).to.equal('text');
  50. done();
  51. } catch (e) {
  52. done(e);
  53. }
  54. }
  55. }]);
  56. menu._executeCommand({}, menu.items[0].commandId);
  57. });
  58. });
  59. describe('MenuItem with checked/radio property', () => {
  60. it('clicking an checkbox item should flip the checked property', () => {
  61. const menu = Menu.buildFromTemplate([{
  62. label: 'text',
  63. type: 'checkbox'
  64. }]);
  65. expect(menu.items[0].checked).to.be.false('menu item checked');
  66. menu._executeCommand({}, menu.items[0].commandId);
  67. expect(menu.items[0].checked).to.be.true('menu item checked');
  68. });
  69. it('clicking an radio item should always make checked property true', () => {
  70. const menu = Menu.buildFromTemplate([{
  71. label: 'text',
  72. type: 'radio'
  73. }]);
  74. menu._executeCommand({}, menu.items[0].commandId);
  75. expect(menu.items[0].checked).to.be.true('menu item checked');
  76. menu._executeCommand({}, menu.items[0].commandId);
  77. expect(menu.items[0].checked).to.be.true('menu item checked');
  78. });
  79. describe('MenuItem group properties', () => {
  80. const template: MenuItemConstructorOptions[] = [];
  81. const findRadioGroups = (template: MenuItemConstructorOptions[]) => {
  82. const groups = [];
  83. let cur: { begin?: number, end?: number } | null = null;
  84. for (let i = 0; i <= template.length; i++) {
  85. if (cur && ((i === template.length) || (template[i].type !== 'radio'))) {
  86. cur.end = i;
  87. groups.push(cur);
  88. cur = null;
  89. } else if (!cur && i < template.length && template[i].type === 'radio') {
  90. cur = { begin: i };
  91. }
  92. }
  93. return groups;
  94. };
  95. // returns array of checked menuitems in [begin,end)
  96. const findChecked = (menuItems: MenuItem[], begin: number, end: number) => {
  97. const checked = [];
  98. for (let i = begin; i < end; i++) {
  99. if (menuItems[i].checked) checked.push(i);
  100. }
  101. return checked;
  102. };
  103. beforeEach(() => {
  104. for (let i = 0; i <= 10; i++) {
  105. template.push({
  106. label: `${i}`,
  107. type: 'radio'
  108. });
  109. }
  110. template.push({ type: 'separator' });
  111. for (let i = 12; i <= 20; i++) {
  112. template.push({
  113. label: `${i}`,
  114. type: 'radio'
  115. });
  116. }
  117. });
  118. it('at least have one item checked in each group', () => {
  119. const menu = Menu.buildFromTemplate(template);
  120. menu._menuWillShow();
  121. const groups = findRadioGroups(template);
  122. for (const g of groups) {
  123. expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin]);
  124. }
  125. });
  126. it('should assign groupId automatically', () => {
  127. const menu = Menu.buildFromTemplate(template);
  128. const usedGroupIds = new Set();
  129. const groups = findRadioGroups(template);
  130. for (const g of groups) {
  131. const groupId = (menu.items[g.begin!] as any).groupId;
  132. // groupId should be previously unused
  133. // expect(usedGroupIds.has(groupId)).to.be.false('group id present')
  134. expect(usedGroupIds).not.to.contain(groupId);
  135. usedGroupIds.add(groupId);
  136. // everything in the group should have the same id
  137. for (let i = g.begin!; i < g.end!; ++i) {
  138. expect((menu.items[i] as any).groupId).to.equal(groupId);
  139. }
  140. }
  141. });
  142. it("setting 'checked' should flip other items' 'checked' property", () => {
  143. const menu = Menu.buildFromTemplate(template);
  144. const groups = findRadioGroups(template);
  145. for (const g of groups) {
  146. expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([]);
  147. menu.items[g.begin!].checked = true;
  148. expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.begin!]);
  149. menu.items[g.end! - 1].checked = true;
  150. expect(findChecked(menu.items, g.begin!, g.end!)).to.deep.equal([g.end! - 1]);
  151. }
  152. });
  153. });
  154. });
  155. describe('MenuItem role execution', () => {
  156. afterEach(closeAllWindows);
  157. it('does not try to execute roles without a valid role property', () => {
  158. const win = new BrowserWindow({ show: false, width: 200, height: 200 });
  159. const item = new MenuItem({ role: 'asdfghjkl' as any });
  160. const canExecute = execute(item.role as any, win, win.webContents);
  161. expect(canExecute).to.be.false('can execute');
  162. });
  163. it('executes roles with native role functions', () => {
  164. const win = new BrowserWindow({ show: false, width: 200, height: 200 });
  165. const item = new MenuItem({ role: 'reload' });
  166. const canExecute = execute(item.role as any, win, win.webContents);
  167. expect(canExecute).to.be.true('can execute');
  168. });
  169. it('execute roles with non-native role functions', () => {
  170. const win = new BrowserWindow({ show: false, width: 200, height: 200 });
  171. const item = new MenuItem({ role: 'resetZoom' });
  172. const canExecute = execute(item.role as any, win, win.webContents);
  173. expect(canExecute).to.be.true('can execute');
  174. });
  175. });
  176. describe('MenuItem command id', () => {
  177. it('cannot be overwritten', () => {
  178. const item = new MenuItem({ label: 'item' });
  179. const commandId = item.commandId;
  180. expect(commandId).to.not.be.undefined('command id');
  181. expect(() => {
  182. item.commandId = `${commandId}-modified` as any;
  183. }).to.throw(/Cannot assign to read only property/);
  184. expect(item.commandId).to.equal(commandId);
  185. });
  186. });
  187. describe('MenuItem with invalid type', () => {
  188. it('throws an exception', () => {
  189. expect(() => {
  190. Menu.buildFromTemplate([{
  191. label: 'text',
  192. type: 'not-a-type' as any
  193. }]);
  194. }).to.throw(/Unknown menu item type: not-a-type/);
  195. });
  196. });
  197. describe('MenuItem with submenu type and missing submenu', () => {
  198. it('throws an exception', () => {
  199. expect(() => {
  200. Menu.buildFromTemplate([{
  201. label: 'text',
  202. type: 'submenu'
  203. }]);
  204. }).to.throw(/Invalid submenu/);
  205. });
  206. });
  207. describe('MenuItem role', () => {
  208. it('returns undefined for items without default accelerator', () => {
  209. const list = keys(roleList).filter(key => !roleList[key].accelerator);
  210. for (const role of list) {
  211. const item = new MenuItem({ role: role as any });
  212. expect(item.getDefaultRoleAccelerator()).to.be.undefined('default accelerator');
  213. }
  214. });
  215. it('returns the correct default label', () => {
  216. for (const role of keys(roleList)) {
  217. const item = new MenuItem({ role: role as any });
  218. const label: string = roleList[role].label;
  219. expect(item.label).to.equal(label);
  220. }
  221. });
  222. it('returns the correct default accelerator', () => {
  223. const list = keys(roleList).filter(key => roleList[key].accelerator);
  224. for (const role of list) {
  225. const item = new MenuItem({ role: role as any });
  226. const accelerator = roleList[role].accelerator;
  227. expect(item.getDefaultRoleAccelerator()).to.equal(accelerator);
  228. }
  229. });
  230. it('allows a custom accelerator and label to be set', () => {
  231. const item = new MenuItem({
  232. role: 'close',
  233. label: 'Custom Close!',
  234. accelerator: 'D'
  235. });
  236. expect(item.label).to.equal('Custom Close!');
  237. expect(item.accelerator).to.equal('D');
  238. expect(item.getDefaultRoleAccelerator()).to.equal('CommandOrControl+W');
  239. });
  240. });
  241. ifdescribe(process.platform === 'darwin')('MenuItem appMenu', () => {
  242. it('includes a default submenu layout when submenu is empty', () => {
  243. const item = new MenuItem({ role: 'appMenu' });
  244. expect(item.label).to.equal(app.name);
  245. expect(item.submenu!.items[0].role).to.equal('about');
  246. expect(item.submenu!.items[1].type).to.equal('separator');
  247. expect(item.submenu!.items[2].role).to.equal('services');
  248. expect(item.submenu!.items[3].type).to.equal('separator');
  249. expect(item.submenu!.items[4].role).to.equal('hide');
  250. expect(item.submenu!.items[5].role).to.equal('hideothers');
  251. expect(item.submenu!.items[6].role).to.equal('unhide');
  252. expect(item.submenu!.items[7].type).to.equal('separator');
  253. expect(item.submenu!.items[8].role).to.equal('quit');
  254. });
  255. it('overrides default layout when submenu is specified', () => {
  256. const item = new MenuItem({
  257. role: 'appMenu',
  258. submenu: [{
  259. role: 'close'
  260. }]
  261. });
  262. expect(item.label).to.equal(app.name);
  263. expect(item.submenu!.items[0].role).to.equal('close');
  264. });
  265. });
  266. describe('MenuItem fileMenu', () => {
  267. it('includes a default submenu layout when submenu is empty', () => {
  268. const item = new MenuItem({ role: 'fileMenu' });
  269. expect(item.label).to.equal('File');
  270. if (process.platform === 'darwin') {
  271. expect(item.submenu!.items[0].role).to.equal('close');
  272. } else {
  273. expect(item.submenu!.items[0].role).to.equal('quit');
  274. }
  275. });
  276. it('overrides default layout when submenu is specified', () => {
  277. const item = new MenuItem({
  278. role: 'fileMenu',
  279. submenu: [{
  280. role: 'about'
  281. }]
  282. });
  283. expect(item.label).to.equal('File');
  284. expect(item.submenu!.items[0].role).to.equal('about');
  285. });
  286. });
  287. describe('MenuItem editMenu', () => {
  288. it('includes a default submenu layout when submenu is empty', () => {
  289. const item = new MenuItem({ role: 'editMenu' });
  290. expect(item.label).to.equal('Edit');
  291. expect(item.submenu!.items[0].role).to.equal('undo');
  292. expect(item.submenu!.items[1].role).to.equal('redo');
  293. expect(item.submenu!.items[2].type).to.equal('separator');
  294. expect(item.submenu!.items[3].role).to.equal('cut');
  295. expect(item.submenu!.items[4].role).to.equal('copy');
  296. expect(item.submenu!.items[5].role).to.equal('paste');
  297. if (process.platform === 'darwin') {
  298. expect(item.submenu!.items[6].role).to.equal('pasteandmatchstyle');
  299. expect(item.submenu!.items[7].role).to.equal('delete');
  300. expect(item.submenu!.items[8].role).to.equal('selectall');
  301. expect(item.submenu!.items[9].type).to.equal('separator');
  302. expect(item.submenu!.items[10].label).to.equal('Substitutions');
  303. expect(item.submenu!.items[10].submenu!.items[0].role).to.equal('showsubstitutions');
  304. expect(item.submenu!.items[10].submenu!.items[1].type).to.equal('separator');
  305. expect(item.submenu!.items[10].submenu!.items[2].role).to.equal('togglesmartquotes');
  306. expect(item.submenu!.items[10].submenu!.items[3].role).to.equal('togglesmartdashes');
  307. expect(item.submenu!.items[10].submenu!.items[4].role).to.equal('toggletextreplacement');
  308. expect(item.submenu!.items[11].label).to.equal('Speech');
  309. expect(item.submenu!.items[11].submenu!.items[0].role).to.equal('startspeaking');
  310. expect(item.submenu!.items[11].submenu!.items[1].role).to.equal('stopspeaking');
  311. } else {
  312. expect(item.submenu!.items[6].role).to.equal('delete');
  313. expect(item.submenu!.items[7].type).to.equal('separator');
  314. expect(item.submenu!.items[8].role).to.equal('selectall');
  315. }
  316. });
  317. it('overrides default layout when submenu is specified', () => {
  318. const item = new MenuItem({
  319. role: 'editMenu',
  320. submenu: [{
  321. role: 'close'
  322. }]
  323. });
  324. expect(item.label).to.equal('Edit');
  325. expect(item.submenu!.items[0].role).to.equal('close');
  326. });
  327. });
  328. describe('MenuItem viewMenu', () => {
  329. it('includes a default submenu layout when submenu is empty', () => {
  330. const item = new MenuItem({ role: 'viewMenu' });
  331. expect(item.label).to.equal('View');
  332. expect(item.submenu!.items[0].role).to.equal('reload');
  333. expect(item.submenu!.items[1].role).to.equal('forcereload');
  334. expect(item.submenu!.items[2].role).to.equal('toggledevtools');
  335. expect(item.submenu!.items[3].type).to.equal('separator');
  336. expect(item.submenu!.items[4].role).to.equal('resetzoom');
  337. expect(item.submenu!.items[5].role).to.equal('zoomin');
  338. expect(item.submenu!.items[6].role).to.equal('zoomout');
  339. expect(item.submenu!.items[7].type).to.equal('separator');
  340. expect(item.submenu!.items[8].role).to.equal('togglefullscreen');
  341. });
  342. it('overrides default layout when submenu is specified', () => {
  343. const item = new MenuItem({
  344. role: 'viewMenu',
  345. submenu: [{
  346. role: 'close'
  347. }]
  348. });
  349. expect(item.label).to.equal('View');
  350. expect(item.submenu!.items[0].role).to.equal('close');
  351. });
  352. });
  353. describe('MenuItem windowMenu', () => {
  354. it('includes a default submenu layout when submenu is empty', () => {
  355. const item = new MenuItem({ role: 'windowMenu' });
  356. expect(item.label).to.equal('Window');
  357. expect(item.submenu!.items[0].role).to.equal('minimize');
  358. expect(item.submenu!.items[1].role).to.equal('zoom');
  359. if (process.platform === 'darwin') {
  360. expect(item.submenu!.items[2].type).to.equal('separator');
  361. expect(item.submenu!.items[3].role).to.equal('front');
  362. } else {
  363. expect(item.submenu!.items[2].role).to.equal('close');
  364. }
  365. });
  366. it('overrides default layout when submenu is specified', () => {
  367. const item = new MenuItem({
  368. role: 'windowMenu',
  369. submenu: [{ role: 'copy' }]
  370. });
  371. expect(item.label).to.equal('Window');
  372. expect(item.submenu!.items[0].role).to.equal('copy');
  373. });
  374. });
  375. describe('MenuItem with custom properties in constructor', () => {
  376. it('preserves the custom properties', () => {
  377. const template = [{
  378. label: 'menu 1',
  379. customProp: 'foo',
  380. submenu: []
  381. }];
  382. const menu = Menu.buildFromTemplate(template);
  383. menu.items[0].submenu!.append(new MenuItem({
  384. label: 'item 1',
  385. customProp: 'bar',
  386. overrideProperty: 'oops not allowed'
  387. } as any));
  388. expect((menu.items[0] as any).customProp).to.equal('foo');
  389. expect(menu.items[0].submenu!.items[0].label).to.equal('item 1');
  390. expect((menu.items[0].submenu!.items[0] as any).customProp).to.equal('bar');
  391. expect((menu.items[0].submenu!.items[0] as any).overrideProperty).to.be.a('function');
  392. });
  393. });
  394. describe('MenuItem accelerators', () => {
  395. const isDarwin = () => {
  396. return (process.platform === 'darwin');
  397. };
  398. it('should display modifiers correctly for simple keys', () => {
  399. const menu = Menu.buildFromTemplate([
  400. { label: 'text', accelerator: 'CmdOrCtrl+A' },
  401. { label: 'text', accelerator: 'Shift+A' },
  402. { label: 'text', accelerator: 'Alt+A' }
  403. ]);
  404. expect(menu._getAcceleratorTextAt(0)).to.equal(isDarwin() ? 'Command+A' : 'Ctrl+A');
  405. expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+A');
  406. expect(menu._getAcceleratorTextAt(2)).to.equal('Alt+A');
  407. });
  408. it('should display modifiers correctly for special keys', () => {
  409. const menu = Menu.buildFromTemplate([
  410. { label: 'text', accelerator: 'CmdOrCtrl+Tab' },
  411. { label: 'text', accelerator: 'Shift+Tab' },
  412. { label: 'text', accelerator: 'Alt+Tab' }
  413. ]);
  414. expect(menu._getAcceleratorTextAt(0)).to.equal(isDarwin() ? 'Command+Tab' : 'Ctrl+Tab');
  415. expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+Tab');
  416. expect(menu._getAcceleratorTextAt(2)).to.equal('Alt+Tab');
  417. });
  418. it('should not display modifiers twice', () => {
  419. const menu = Menu.buildFromTemplate([
  420. { label: 'text', accelerator: 'Shift+Shift+A' },
  421. { label: 'text', accelerator: 'Shift+Shift+Tab' }
  422. ]);
  423. expect(menu._getAcceleratorTextAt(0)).to.equal('Shift+A');
  424. expect(menu._getAcceleratorTextAt(1)).to.equal('Shift+Tab');
  425. });
  426. it('should display correctly for shifted keys', () => {
  427. const menu = Menu.buildFromTemplate([
  428. { label: 'text', accelerator: 'Control+Shift+=' },
  429. { label: 'text', accelerator: 'Control+Plus' },
  430. { label: 'text', accelerator: 'Control+Shift+3' },
  431. { label: 'text', accelerator: 'Control+#' },
  432. { label: 'text', accelerator: 'Control+Shift+/' },
  433. { label: 'text', accelerator: 'Control+?' }
  434. ]);
  435. expect(menu._getAcceleratorTextAt(0)).to.equal('Ctrl+Shift+=');
  436. expect(menu._getAcceleratorTextAt(1)).to.equal('Ctrl++');
  437. expect(menu._getAcceleratorTextAt(2)).to.equal('Ctrl+Shift+3');
  438. expect(menu._getAcceleratorTextAt(3)).to.equal('Ctrl+#');
  439. expect(menu._getAcceleratorTextAt(4)).to.equal('Ctrl+Shift+/');
  440. expect(menu._getAcceleratorTextAt(5)).to.equal('Ctrl+?');
  441. });
  442. });
  443. });