Skip to content

Commit c349aef

Browse files
committed
Add initial touch bar specs
1 parent 0023695 commit c349aef

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

docs/api/touch-bar.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ Creates a new touch bar with the specified items. Use
1313

1414
## Examples
1515

16-
The `TouchBar` class is only available in the main process, it is not currently
17-
possible to use in the renderer process **even** through the remote module.
18-
1916
Below is an example of a simple slot machine touch bar game with a button
2017
and some labels.
2118

lib/browser/api/touch-bar.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TouchBar extends EventEmitter {
2424
super()
2525

2626
if (!Array.isArray(items)) {
27-
throw new Error('The items object provided has to be an array')
27+
throw new Error('Must specify items array as first argument')
2828
}
2929

3030
this.windowListeners = {}
@@ -42,7 +42,7 @@ class TouchBar extends EventEmitter {
4242
}
4343
items.forEach((item) => {
4444
if (!(item instanceof TouchBarItem)) {
45-
throw new Error('Each item must be an instance of a TouchBarItem')
45+
throw new Error('Each item must be an instance of TouchBarItem')
4646
}
4747
this.ordereredItems.push(item)
4848
registerItem(item)
@@ -121,7 +121,9 @@ TouchBar.TouchBarButton = class TouchBarButton extends TouchBarItem {
121121
this._addLiveProperty('backgroundColor', backgroundColor)
122122
this._addLiveProperty('icon', icon)
123123
if (typeof click === 'function') {
124-
this.onInteraction = config.click
124+
this.onInteraction = () => {
125+
config.click()
126+
}
125127
}
126128
}
127129
}

spec/api-touch-bar-spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const assert = require('assert')
2+
const {BrowserWindow, TouchBar} = require('electron').remote
3+
const {closeWindow} = require('./window-helpers')
4+
5+
const {TouchBarButton, TouchBarColorPicker, TouchBarGroup} = TouchBar
6+
const {TouchBarLabel, TouchBarPopover, TouchBarSlider, TouchBarSpacer} = TouchBar
7+
8+
describe('TouchBar module', function () {
9+
it('throws an error when created without an items array', function () {
10+
assert.throws(() => {
11+
const touchBar = new TouchBar()
12+
touchBar.toString()
13+
}, /Must specify items array as first argument/)
14+
})
15+
16+
it('throws an error when created with invalid items', function () {
17+
assert.throws(() => {
18+
const touchBar = new TouchBar([1, true, {}, []])
19+
touchBar.toString()
20+
}, /Each item must be an instance of TouchBarItem/)
21+
})
22+
23+
describe('BrowserWindow behavior', function () {
24+
let window
25+
26+
beforeEach(function () {
27+
window = new BrowserWindow()
28+
})
29+
30+
afterEach(function () {
31+
window.setTouchBar(null)
32+
return closeWindow(window).then(function () { window = null })
33+
})
34+
35+
it('can be added to and removed from a window', function () {
36+
const touchBar = new TouchBar([
37+
new TouchBarButton({label: 'foo', backgroundColor: '#F00', click: () => {}}),
38+
new TouchBarColorPicker({selectedColor: '#F00', change: () => {}}),
39+
new TouchBarGroup({items: new TouchBar([new TouchBarLabel({label: 'hello'})])}),
40+
new TouchBarLabel({label: 'bar'}),
41+
new TouchBarPopover({items: new TouchBar([new TouchBarButton({label: 'pop'})])}),
42+
new TouchBarSlider({label: 'slide', value: 5, minValue: 2, maxValue: 75, change: () => {}}),
43+
new TouchBarSpacer({size: 'large'})
44+
])
45+
window.setTouchBar(touchBar)
46+
window.setTouchBar()
47+
window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
48+
})
49+
})
50+
})

0 commit comments

Comments
 (0)