api-touch-bar-spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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', function () {
  8. it('throws an error when created without an options object', function () {
  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', function () {
  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', function () {
  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', function () {
  31. let window
  32. beforeEach(function () {
  33. window = new BrowserWindow()
  34. })
  35. afterEach(function () {
  36. window.setTouchBar(null)
  37. return closeWindow(window).then(function () { window = null })
  38. })
  39. it('can be added to and removed from a window', function () {
  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({
  68. label: 'foo'
  69. })
  70. window.setTouchBar(touchBar)
  71. touchBar.escapeItem = escapeButton
  72. label.label = 'baz'
  73. escapeButton.label = 'hello'
  74. window.setTouchBar()
  75. window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
  76. touchBar.escapeItem = null
  77. })
  78. it('calls the callback on the items when a window interaction event fires', function (done) {
  79. const button = new TouchBarButton({
  80. label: 'bar',
  81. click: () => {
  82. done()
  83. }
  84. })
  85. const touchBar = new TouchBar({items: [button]})
  86. window.setTouchBar(touchBar)
  87. window.emit('-touch-bar-interaction', {}, button.id)
  88. })
  89. it('calls the callback on the escape item when a window interaction event fires', function (done) {
  90. const button = new TouchBarButton({
  91. label: 'bar',
  92. click: () => {
  93. done()
  94. }
  95. })
  96. const touchBar = new TouchBar({escapeItem: button})
  97. window.setTouchBar(touchBar)
  98. window.emit('-touch-bar-interaction', {}, button.id)
  99. })
  100. })
  101. })