api-touch-bar-spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import * as path from 'node:path';
  2. import { BrowserWindow, TouchBar } from 'electron/main';
  3. import { closeWindow } from './lib/window-helpers';
  4. import { expect } from 'chai';
  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('BrowserWindow behavior', () => {
  43. let window: BrowserWindow;
  44. beforeEach(() => {
  45. window = new BrowserWindow({ show: false });
  46. });
  47. afterEach(async () => {
  48. window.setTouchBar(null);
  49. await closeWindow(window);
  50. window = null as unknown as BrowserWindow;
  51. });
  52. it('can be added to and removed from a window', () => {
  53. const label = new TouchBarLabel({ label: 'bar' });
  54. const touchBar = new TouchBar({
  55. items: [
  56. new TouchBarButton({ label: 'foo', backgroundColor: '#F00', click: () => { } }),
  57. new TouchBarButton({
  58. icon: path.join(__dirname, 'fixtures', 'assets', 'logo.png'),
  59. iconPosition: 'right',
  60. click: () => { }
  61. }),
  62. new TouchBarColorPicker({ selectedColor: '#F00', change: () => { } }),
  63. new TouchBarGroup({ items: new TouchBar({ items: [new TouchBarLabel({ label: 'hello' })] }) }),
  64. label,
  65. new TouchBarOtherItemsProxy(),
  66. new TouchBarPopover({ items: new TouchBar({ items: [new TouchBarButton({ label: 'pop' })] }) }),
  67. new TouchBarSlider({ label: 'slide', value: 5, minValue: 2, maxValue: 75, change: () => { } }),
  68. new TouchBarSpacer({ size: 'large' }),
  69. new TouchBarSegmentedControl({
  70. segmentStyle: 'capsule',
  71. segments: [{ label: 'baz', enabled: false }],
  72. selectedIndex: 5
  73. }),
  74. new TouchBarSegmentedControl({ segments: [] }),
  75. new TouchBarScrubber({
  76. items: [{ label: 'foo' }, { label: 'bar' }, { label: 'baz' }],
  77. selectedStyle: 'outline',
  78. mode: 'fixed',
  79. showArrowButtons: true
  80. })
  81. ]
  82. });
  83. const escapeButton = new TouchBarButton({ label: 'foo' });
  84. window.setTouchBar(touchBar);
  85. touchBar.escapeItem = escapeButton;
  86. label.label = 'baz';
  87. escapeButton.label = 'hello';
  88. window.setTouchBar(null);
  89. window.setTouchBar(new TouchBar({ items: [new TouchBarLabel({ label: 'two' })] }));
  90. touchBar.escapeItem = null;
  91. });
  92. it('calls the callback on the items when a window interaction event fires', (done) => {
  93. const button = new TouchBarButton({
  94. label: 'bar',
  95. click: () => {
  96. done();
  97. }
  98. });
  99. const touchBar = new TouchBar({ items: [button] });
  100. window.setTouchBar(touchBar);
  101. window.emit('-touch-bar-interaction', {}, (button as any).id);
  102. });
  103. it('calls the callback on the escape item when a window interaction event fires', (done) => {
  104. const button = new TouchBarButton({
  105. label: 'bar',
  106. click: () => {
  107. done();
  108. }
  109. });
  110. const touchBar = new TouchBar({ escapeItem: button });
  111. window.setTouchBar(touchBar);
  112. window.emit('-touch-bar-interaction', {}, (button as any).id);
  113. });
  114. });
  115. });