api-touch-bar-spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import * as path from 'path'
  2. import { BrowserWindow, TouchBar } from 'electron'
  3. import { closeWindow } from './window-helpers'
  4. import { expect } from 'chai'
  5. const { TouchBarButton, TouchBarColorPicker, TouchBarGroup, TouchBarLabel, 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 the same TouchBarItem is added multiple times', () => {
  30. expect(() => {
  31. const item = new TouchBarLabel({ label: 'Label' })
  32. const touchBar = new TouchBar({ items: [item, item] })
  33. touchBar.toString()
  34. }).to.throw('Cannot add a single instance of TouchBarItem multiple times in a TouchBar')
  35. })
  36. describe('BrowserWindow behavior', () => {
  37. let window: BrowserWindow
  38. beforeEach(() => {
  39. window = new BrowserWindow({show: false})
  40. })
  41. afterEach(async () => {
  42. window.setTouchBar(null)
  43. await closeWindow(window)
  44. window = null as unknown as BrowserWindow
  45. })
  46. it('can be added to and removed from a window', () => {
  47. const label = new TouchBarLabel({ label: 'bar' })
  48. const touchBar = new TouchBar({ items: [
  49. new TouchBarButton({ label: 'foo', backgroundColor: '#F00', click: () => {} }),
  50. new TouchBarButton({
  51. icon: path.join(__dirname, 'fixtures', 'assets', 'logo.png'),
  52. iconPosition: 'right',
  53. click: () => {}
  54. }),
  55. new TouchBarColorPicker({ selectedColor: '#F00', change: () => {} }),
  56. new TouchBarGroup({ items: new TouchBar({ items: [new TouchBarLabel({ label: 'hello' })] }) }),
  57. label,
  58. new TouchBarPopover({ items: new TouchBar({ items: [new TouchBarButton({ label: 'pop' })] }) }),
  59. new TouchBarSlider({ label: 'slide', value: 5, minValue: 2, maxValue: 75, change: () => {} }),
  60. new TouchBarSpacer({ size: 'large' }),
  61. new TouchBarSegmentedControl({
  62. segmentStyle: 'capsule',
  63. segments: [{ label: 'baz', enabled: false }],
  64. selectedIndex: 5
  65. }),
  66. new TouchBarSegmentedControl({ segments: [] }),
  67. new TouchBarScrubber({
  68. items: [{ label: 'foo' }, { label: 'bar' }, { label: 'baz' }],
  69. selectedStyle: 'outline',
  70. mode: 'fixed',
  71. showArrowButtons: true
  72. })
  73. ]})
  74. const escapeButton = new TouchBarButton({ label: 'foo' })
  75. window.setTouchBar(touchBar)
  76. touchBar.escapeItem = escapeButton
  77. label.label = 'baz'
  78. escapeButton.label = 'hello'
  79. window.setTouchBar(null)
  80. window.setTouchBar(new TouchBar({items: [new TouchBarLabel({ label: 'two' })]}))
  81. touchBar.escapeItem = null
  82. })
  83. it('calls the callback on the items when a window interaction event fires', (done) => {
  84. const button = new TouchBarButton({
  85. label: 'bar',
  86. click: () => {
  87. done()
  88. }
  89. })
  90. const touchBar = new TouchBar({ items: [button] })
  91. window.setTouchBar(touchBar)
  92. window.emit('-touch-bar-interaction', {}, (button as any).id)
  93. })
  94. it('calls the callback on the escape item 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({ escapeItem: button })
  102. window.setTouchBar(touchBar)
  103. window.emit('-touch-bar-interaction', {}, (button as any).id)
  104. })
  105. })
  106. })