api-touch-bar-spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const assert = require('assert')
  2. const path = require('path')
  3. const {BrowserWindow, TouchBar} = require('electron').remote
  4. const {closeWindow} = require('./window-helpers')
  5. const {TouchBarButton, TouchBarColorPicker, TouchBarGroup} = TouchBar
  6. const {TouchBarLabel, TouchBarPopover, TouchBarScrubber, TouchBarSegmentedControl, TouchBarSlider, TouchBarSpacer} = TouchBar
  7. describe('TouchBar module', () => {
  8. it('throws an error when created without an options object', () => {
  9. assert.throws(() => {
  10. const touchBar = new TouchBar()
  11. touchBar.toString()
  12. }, /Must specify options object as first argument/)
  13. })
  14. it('throws an error when created with invalid items', () => {
  15. assert.throws(() => {
  16. const touchBar = new TouchBar({items: [1, true, {}, []]})
  17. touchBar.toString()
  18. }, /Each item must be an instance of TouchBarItem/)
  19. })
  20. it('throws an error when an invalid escape item is set', () => {
  21. assert.throws(() => {
  22. const touchBar = new TouchBar({items: [], escapeItem: 'esc'})
  23. touchBar.toString()
  24. }, /Escape item must be an instance of TouchBarItem/)
  25. assert.throws(() => {
  26. const touchBar = new TouchBar({items: []})
  27. touchBar.escapeItem = 'esc'
  28. }, /Escape item must be an instance of TouchBarItem/)
  29. })
  30. describe('BrowserWindow behavior', () => {
  31. let window
  32. beforeEach(() => {
  33. window = new BrowserWindow()
  34. })
  35. afterEach(() => {
  36. window.setTouchBar(null)
  37. return closeWindow(window).then(() => { window = null })
  38. })
  39. it('can be added to and removed from a window', () => {
  40. const label = new TouchBarLabel({label: 'bar'})
  41. const touchBar = new TouchBar([
  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([new TouchBarLabel({label: 'hello'})])}),
  50. label,
  51. new TouchBarPopover({items: new TouchBar([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()
  73. window.setTouchBar(new TouchBar([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.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.id)
  97. })
  98. })
  99. })