api-touch-bar-spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { BaseWindow, BrowserWindow, TouchBar } from 'electron/main';
  2. import { expect } from 'chai';
  3. import * as path from 'node:path';
  4. import { closeWindow } from './lib/window-helpers';
  5. const { TouchBarButton, TouchBarColorPicker, TouchBarGroup, TouchBarLabel, TouchBarOtherItemsProxy, TouchBarPopover, TouchBarScrubber, TouchBarSegmentedControl, TouchBarSlider, TouchBarSpacer } = TouchBar;
  6. describe('TouchBar module', () => {
  7. it('throws an error when created without an options object', () => {
  8. expect(() => {
  9. const touchBar = new (TouchBar as any)();
  10. touchBar.toString();
  11. }).to.throw('Must specify options object as first argument');
  12. });
  13. it('throws an error when created with invalid items', () => {
  14. expect(() => {
  15. const touchBar = new TouchBar({ items: [1, true, {}, []] as any });
  16. touchBar.toString();
  17. }).to.throw('Each item must be an instance of TouchBarItem');
  18. });
  19. it('throws an error when an invalid escape item is set', () => {
  20. expect(() => {
  21. const touchBar = new TouchBar({ items: [], escapeItem: 'esc' as any });
  22. touchBar.toString();
  23. }).to.throw('Escape item must be an instance of TouchBarItem');
  24. expect(() => {
  25. const touchBar = new TouchBar({ items: [] });
  26. touchBar.escapeItem = 'esc' as any;
  27. }).to.throw('Escape item must be an instance of TouchBarItem');
  28. });
  29. it('throws an error if multiple OtherItemProxy items are added', () => {
  30. expect(() => {
  31. const touchBar = new TouchBar({ items: [new TouchBarOtherItemsProxy(), new TouchBarOtherItemsProxy()] });
  32. touchBar.toString();
  33. }).to.throw('Must only have one OtherItemsProxy per TouchBar');
  34. });
  35. it('throws an error if the same TouchBarItem is added multiple times', () => {
  36. expect(() => {
  37. const item = new TouchBarLabel({ label: 'Label' });
  38. const touchBar = new TouchBar({ items: [item, item] });
  39. touchBar.toString();
  40. }).to.throw('Cannot add a single instance of TouchBarItem multiple times in a TouchBar');
  41. });
  42. describe('Window behavior', () => {
  43. for (const WindowType of [BrowserWindow, BaseWindow]) {
  44. describe(`in ${WindowType.name}`, () => {
  45. let window: BaseWindow | BrowserWindow;
  46. beforeEach(() => {
  47. window = new WindowType({ show: false });
  48. });
  49. afterEach(async () => {
  50. window.setTouchBar(null);
  51. await closeWindow(window);
  52. window = null as unknown as BaseWindow | BrowserWindow;
  53. });
  54. it('can be added to and removed from a window', () => {
  55. const label = new TouchBarLabel({ label: 'bar' });
  56. const touchBar = new TouchBar({
  57. items: [
  58. new TouchBarButton({ label: 'foo', backgroundColor: '#F00', click: () => { } }),
  59. new TouchBarButton({
  60. icon: path.join(__dirname, 'fixtures', 'assets', 'logo.png'),
  61. iconPosition: 'right',
  62. click: () => { }
  63. }),
  64. new TouchBarColorPicker({ selectedColor: '#F00', change: () => { } }),
  65. new TouchBarGroup({ items: new TouchBar({ items: [new TouchBarLabel({ label: 'hello' })] }) }),
  66. label,
  67. new TouchBarOtherItemsProxy(),
  68. new TouchBarPopover({ items: new TouchBar({ items: [new TouchBarButton({ label: 'pop' })] }) }),
  69. new TouchBarSlider({ label: 'slide', value: 5, minValue: 2, maxValue: 75, change: () => { } }),
  70. new TouchBarSpacer({ size: 'large' }),
  71. new TouchBarSegmentedControl({
  72. segmentStyle: 'capsule',
  73. segments: [{ label: 'baz', enabled: false }],
  74. selectedIndex: 5
  75. }),
  76. new TouchBarSegmentedControl({ segments: [] }),
  77. new TouchBarScrubber({
  78. items: [{ label: 'foo' }, { label: 'bar' }, { label: 'baz' }],
  79. selectedStyle: 'outline',
  80. mode: 'fixed',
  81. showArrowButtons: true
  82. })
  83. ]
  84. });
  85. const escapeButton = new TouchBarButton({ label: 'foo' });
  86. window.setTouchBar(touchBar);
  87. touchBar.escapeItem = escapeButton;
  88. label.label = 'baz';
  89. escapeButton.label = 'hello';
  90. window.setTouchBar(null);
  91. window.setTouchBar(new TouchBar({ items: [new TouchBarLabel({ label: 'two' })] }));
  92. touchBar.escapeItem = null;
  93. });
  94. it('calls the callback on the items when a window interaction event fires', (done) => {
  95. const button = new TouchBarButton({
  96. label: 'bar',
  97. click: () => {
  98. done();
  99. }
  100. });
  101. const touchBar = new TouchBar({ items: [button] });
  102. window.setTouchBar(touchBar);
  103. window.emit('-touch-bar-interaction', {}, (button as any).id);
  104. });
  105. it('calls the callback on the escape item when a window interaction event fires', (done) => {
  106. const button = new TouchBarButton({
  107. label: 'bar',
  108. click: () => {
  109. done();
  110. }
  111. });
  112. const touchBar = new TouchBar({ escapeItem: button });
  113. window.setTouchBar(touchBar);
  114. window.emit('-touch-bar-interaction', {}, (button as any).id);
  115. });
  116. });
  117. };
  118. });
  119. });