api-touch-bar-spec.ts 4.0 KB

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