Merge pull request #10942 from electron/upgrade_specs

[WIP] Upgrade specs
This commit is contained in:
Shelley Vohr 2017-10-26 22:12:19 -04:00 committed by GitHub
commit e2d5aaf0f3
10 changed files with 165 additions and 201 deletions

View file

@ -4,45 +4,45 @@ const {Buffer} = require('buffer')
const {clipboard, nativeImage} = require('electron') const {clipboard, nativeImage} = require('electron')
describe('clipboard module', function () { describe('clipboard module', () => {
var fixtures = path.resolve(__dirname, 'fixtures') const fixtures = path.resolve(__dirname, 'fixtures')
describe('clipboard.readImage()', function () { describe('clipboard.readImage()', () => {
it('returns NativeImage intance', function () { it('returns NativeImage instance', () => {
var p = path.join(fixtures, 'assets', 'logo.png') const p = path.join(fixtures, 'assets', 'logo.png')
var i = nativeImage.createFromPath(p) const i = nativeImage.createFromPath(p)
clipboard.writeImage(p) clipboard.writeImage(p)
assert.equal(clipboard.readImage().toDataURL(), i.toDataURL()) assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
}) })
}) })
describe('clipboard.readText()', function () { describe('clipboard.readText()', () => {
it('returns unicode string correctly', function () { it('returns unicode string correctly', () => {
var text = '千江有水千江月,万里无云万里天' const text = '千江有水千江月,万里无云万里天'
clipboard.writeText(text) clipboard.writeText(text)
assert.equal(clipboard.readText(), text) assert.equal(clipboard.readText(), text)
}) })
}) })
describe('clipboard.readHTML()', function () { describe('clipboard.readHTML()', () => {
it('returns markup correctly', function () { it('returns markup correctly', () => {
var text = '<string>Hi</string>' const text = '<string>Hi</string>'
var markup = process.platform === 'darwin' ? "<meta charset='utf-8'><string>Hi</string>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><string>Hi</string>' : '<string>Hi</string>' const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><string>Hi</string>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><string>Hi</string>' : '<string>Hi</string>'
clipboard.writeHTML(text) clipboard.writeHTML(text)
assert.equal(clipboard.readHTML(), markup) assert.equal(clipboard.readHTML(), markup)
}) })
}) })
describe('clipboard.readRTF', function () { describe('clipboard.readRTF', () => {
it('returns rtf text correctly', function () { it('returns rtf text correctly', () => {
var rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}' const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}'
clipboard.writeRTF(rtf) clipboard.writeRTF(rtf)
assert.equal(clipboard.readRTF(), rtf) assert.equal(clipboard.readRTF(), rtf)
}) })
}) })
describe('clipboard.readBookmark', function () { describe('clipboard.readBookmark', () => {
it('returns title and url', function () { it('returns title and url', () => {
if (process.platform === 'linux') return if (process.platform === 'linux') return
clipboard.writeBookmark('a title', 'https://electron.atom.io') clipboard.writeBookmark('a title', 'https://electron.atom.io')
@ -59,14 +59,14 @@ describe('clipboard module', function () {
}) })
}) })
describe('clipboard.write()', function () { describe('clipboard.write()', () => {
it('returns data correctly', function () { it('returns data correctly', () => {
var text = 'test' const text = 'test'
var rtf = '{\\rtf1\\utf8 text}' const rtf = '{\\rtf1\\utf8 text}'
var p = path.join(fixtures, 'assets', 'logo.png') const p = path.join(fixtures, 'assets', 'logo.png')
var i = nativeImage.createFromPath(p) const i = nativeImage.createFromPath(p)
var markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><b>Hi</b>' : '<b>Hi</b>' const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><b>Hi</b>' : '<b>Hi</b>'
var bookmark = {title: 'a title', url: 'test'} const bookmark = {title: 'a title', url: 'test'}
clipboard.write({ clipboard.write({
text: 'test', text: 'test',
html: '<b>Hi</b>', html: '<b>Hi</b>',
@ -85,8 +85,8 @@ describe('clipboard module', function () {
}) })
}) })
describe('clipboard.read/writeFindText(text)', function () { describe('clipboard.read/writeFindText(text)', () => {
it('reads and write text to the find pasteboard', function () { it('reads and write text to the find pasteboard', () => {
if (process.platform !== 'darwin') return if (process.platform !== 'darwin') return
clipboard.writeFindText('find this') clipboard.writeFindText('find this')
@ -110,8 +110,8 @@ describe('clipboard module', function () {
}) })
}) })
describe('clipboard.readBuffer(format)', function () { describe('clipboard.readBuffer(format)', () => {
it('returns a Buffer of the content for the specified format', function () { it('returns a Buffer of the content for the specified format', () => {
if (process.platform !== 'darwin') return if (process.platform !== 'darwin') return
const buffer = Buffer.from('this is binary', 'utf8') const buffer = Buffer.from('this is binary', 'utf8')

View file

@ -11,46 +11,42 @@ const {closeWindow} = require('./window-helpers')
const {remote} = require('electron') const {remote} = require('electron')
const {app, BrowserWindow, crashReporter} = remote.require('electron') const {app, BrowserWindow, crashReporter} = remote.require('electron')
describe('crashReporter module', function () { describe('crashReporter module', () => {
if (process.mas || process.env.DISABLE_CRASH_REPORTER_TESTS) { if (process.mas || process.env.DISABLE_CRASH_REPORTER_TESTS) return
return
}
var originalTempDirectory = null let originalTempDirectory = null
var tempDirectory = null let tempDirectory = null
before(function () { before(() => {
tempDirectory = temp.mkdirSync('electronCrashReporterSpec-') tempDirectory = temp.mkdirSync('electronCrashReporterSpec-')
originalTempDirectory = app.getPath('temp') originalTempDirectory = app.getPath('temp')
app.setPath('temp', tempDirectory) app.setPath('temp', tempDirectory)
}) })
after(function () { after(() => {
app.setPath('temp', originalTempDirectory) app.setPath('temp', originalTempDirectory)
}) })
var fixtures = path.resolve(__dirname, 'fixtures') const fixtures = path.resolve(__dirname, 'fixtures')
const generateSpecs = (description, browserWindowOpts) => { const generateSpecs = (description, browserWindowOpts) => {
describe(description, function () { describe(description, () => {
var w = null let w = null
var stopServer = null let stopServer = null
beforeEach(function () { beforeEach(() => {
stopServer = null stopServer = null
w = new BrowserWindow(Object.assign({ w = new BrowserWindow(Object.assign({
show: false show: false
}, browserWindowOpts)) }, browserWindowOpts))
}) })
afterEach(function () { afterEach(() => closeWindow(w).then(() => { w = null }))
return closeWindow(w).then(function () { w = null })
})
afterEach(function () { afterEach(() => {
stopCrashService() stopCrashService()
}) })
afterEach(function (done) { afterEach((done) => {
if (stopServer != null) { if (stopServer != null) {
stopServer(done) stopServer(done)
} else { } else {
@ -123,12 +119,8 @@ describe('crashReporter module', function () {
crashReporter.setUploadToServer(false) crashReporter.setUploadToServer(false)
} }
const testDone = (uploaded) => { const testDone = (uploaded) => {
if (uploaded) { if (uploaded) return done(new Error('Uploaded crash report'))
return done(new Error('Uploaded crash report')) if (process.platform === 'darwin') crashReporter.setUploadToServer(true)
}
if (process.platform === 'darwin') {
crashReporter.setUploadToServer(true)
}
assert(fs.existsSync(dumpFile)) assert(fs.existsSync(dumpFile))
done() done()
} }
@ -136,13 +128,10 @@ describe('crashReporter module', function () {
let pollInterval let pollInterval
const pollDumpFile = () => { const pollDumpFile = () => {
fs.readdir(crashesDir, (err, files) => { fs.readdir(crashesDir, (err, files) => {
if (err) { if (err) return
return
}
const dumps = files.filter((file) => /\.dmp$/.test(file) && !existingDumpFiles.has(file)) const dumps = files.filter((file) => /\.dmp$/.test(file) && !existingDumpFiles.has(file))
if (!dumps.length) { if (!dumps.length) return
return
}
assert.equal(1, dumps.length) assert.equal(1, dumps.length)
dumpFile = path.join(crashesDir, dumps[0]) dumpFile = path.join(crashesDir, dumps[0])
clearInterval(pollInterval) clearInterval(pollInterval)
@ -210,22 +199,18 @@ describe('crashReporter module', function () {
} }
}) })
describe('.start(options)', function () { describe('.start(options)', () => {
it('requires that the companyName and submitURL options be specified', function () { it('requires that the companyName and submitURL options be specified', () => {
assert.throws(function () { assert.throws(() => {
crashReporter.start({ crashReporter.start({companyName: 'Missing submitURL'})
companyName: 'Missing submitURL'
})
}, /submitURL is a required option to crashReporter\.start/) }, /submitURL is a required option to crashReporter\.start/)
assert.throws(function () { assert.throws(() => {
crashReporter.start({ crashReporter.start({submitURL: 'Missing companyName'})
submitURL: 'Missing companyName'
})
}, /companyName is a required option to crashReporter\.start/) }, /companyName is a required option to crashReporter\.start/)
}) })
it('can be called multiple times', function () { it('can be called multiple times', () => {
assert.doesNotThrow(function () { assert.doesNotThrow(() => {
crashReporter.start({ crashReporter.start({
companyName: 'Umbrella Corporation', companyName: 'Umbrella Corporation',
submitURL: 'http://127.0.0.1/crashes' submitURL: 'http://127.0.0.1/crashes'
@ -239,12 +224,12 @@ describe('crashReporter module', function () {
}) })
}) })
describe('.get/setUploadToServer', function () { describe('.get/setUploadToServer', () => {
it('throws an error when called from the renderer process', function () { it('throws an error when called from the renderer process', () => {
assert.throws(() => require('electron').crashReporter.getUploadToServer()) assert.throws(() => require('electron').crashReporter.getUploadToServer())
}) })
it('can be read/set from the main process', function () { it('can be read/set from the main process', () => {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
crashReporter.start({ crashReporter.start({
companyName: 'Umbrella Corporation', companyName: 'Umbrella Corporation',
@ -279,9 +264,9 @@ const waitForCrashReport = () => {
} }
const startServer = ({callback, processType, done}) => { const startServer = ({callback, processType, done}) => {
var called = false let called = false
var server = http.createServer((req, res) => { let server = http.createServer((req, res) => {
var form = new multiparty.Form() const form = new multiparty.Form()
form.parse(req, (error, fields) => { form.parse(req, (error, fields) => {
if (error) throw error if (error) throw error
if (called) return if (called) return
@ -335,7 +320,7 @@ const startServer = ({callback, processType, done}) => {
for (const connection of activeConnections) { for (const connection of activeConnections) {
connection.destroy() connection.destroy()
} }
server.close(function () { server.close(() => {
done() done()
}) })
} }

View file

@ -4,11 +4,11 @@ const path = require('path')
const {closeWindow} = require('./window-helpers') const {closeWindow} = require('./window-helpers')
const BrowserWindow = require('electron').remote.BrowserWindow const BrowserWindow = require('electron').remote.BrowserWindow
describe('debugger module', function () { describe('debugger module', () => {
var fixtures = path.resolve(__dirname, 'fixtures') const fixtures = path.resolve(__dirname, 'fixtures')
var w = null let w = null
beforeEach(function () { beforeEach(() => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
@ -16,13 +16,11 @@ describe('debugger module', function () {
}) })
}) })
afterEach(function () { afterEach(() => closeWindow(w).then(() => { w = null }))
return closeWindow(w).then(function () { w = null })
})
describe('debugger.attach', function () { describe('debugger.attach', () => {
it('fails when devtools is already open', function (done) { it('fails when devtools is already open', (done) => {
w.webContents.on('did-finish-load', function () { w.webContents.on('did-finish-load', () => {
w.webContents.openDevTools() w.webContents.openDevTools()
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
@ -31,10 +29,10 @@ describe('debugger module', function () {
done() done()
} }
}) })
w.webContents.loadURL('file://' + path.join(fixtures, 'pages', 'a.html')) w.webContents.loadURL(`file://${path.join(fixtures, 'pages', 'a.html')}`)
}) })
it('fails when protocol version is not supported', function (done) { it('fails when protocol version is not supported', (done) => {
try { try {
w.webContents.debugger.attach('2.0') w.webContents.debugger.attach('2.0')
} catch (err) { } catch (err) {
@ -43,20 +41,20 @@ describe('debugger module', function () {
} }
}) })
it('attaches when no protocol version is specified', function (done) { it('attaches when no protocol version is specified', (done) => {
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
} catch (err) { } catch (err) {
done('unexpected error : ' + err) done(`unexpected error : ${err}`)
} }
assert(w.webContents.debugger.isAttached()) assert(w.webContents.debugger.isAttached())
done() done()
}) })
}) })
describe('debugger.detach', function () { describe('debugger.detach', () => {
it('fires detach event', function (done) { it('fires detach event', (done) => {
w.webContents.debugger.on('detach', function (e, reason) { w.webContents.debugger.on('detach', (e, reason) => {
assert.equal(reason, 'target closed') assert.equal(reason, 'target closed')
assert(!w.webContents.debugger.isAttached()) assert(!w.webContents.debugger.isAttached())
done() done()
@ -64,23 +62,23 @@ describe('debugger module', function () {
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
} catch (err) { } catch (err) {
done('unexpected error : ' + err) done(`unexpected error : ${err}`)
} }
w.webContents.debugger.detach() w.webContents.debugger.detach()
}) })
}) })
describe('debugger.sendCommand', function () { describe('debugger.sendCommand', () => {
let server let server
afterEach(function () { afterEach(() => {
if (server != null) { if (server != null) {
server.close() server.close()
server = null server = null
} }
}) })
it('retuns response', function (done) { it('retuns response', (done) => {
w.webContents.loadURL('about:blank') w.webContents.loadURL('about:blank')
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
@ -100,9 +98,9 @@ describe('debugger module', function () {
w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback) w.webContents.debugger.sendCommand('Runtime.evaluate', params, callback)
}) })
it('fires message event', function (done) { it('fires message event', (done) => {
var url = process.platform !== 'win32' const url = process.platform !== 'win32'
? 'file://' + path.join(fixtures, 'pages', 'a.html') ? `file://${path.join(fixtures, 'pages', 'a.html')}`
: 'file:///' + path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/') : 'file:///' + path.join(fixtures, 'pages', 'a.html').replace(/\\/g, '/')
w.webContents.loadURL(url) w.webContents.loadURL(url)
try { try {
@ -110,7 +108,7 @@ describe('debugger module', function () {
} catch (err) { } catch (err) {
done('unexpected error : ' + err) done('unexpected error : ' + err)
} }
w.webContents.debugger.on('message', function (e, method, params) { w.webContents.debugger.on('message', (e, method, params) => {
if (method === 'Console.messageAdded') { if (method === 'Console.messageAdded') {
assert.equal(params.message.level, 'log') assert.equal(params.message.level, 'log')
assert.equal(params.message.url, url) assert.equal(params.message.url, url)
@ -122,25 +120,25 @@ describe('debugger module', function () {
w.webContents.debugger.sendCommand('Console.enable') w.webContents.debugger.sendCommand('Console.enable')
}) })
it('returns error message when command fails', function (done) { it('returns error message when command fails', (done) => {
w.webContents.loadURL('about:blank') w.webContents.loadURL('about:blank')
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
} catch (err) { } catch (err) {
done('unexpected error : ' + err) done(`unexpected error : ${err}`)
} }
w.webContents.debugger.sendCommand('Test', function (err) { w.webContents.debugger.sendCommand('Test', (err) => {
assert.equal(err.message, "'Test' wasn't found") assert.equal(err.message, "'Test' wasn't found")
w.webContents.debugger.detach() w.webContents.debugger.detach()
done() done()
}) })
}) })
it('handles invalid unicode characters in message', function (done) { it('handles invalid unicode characters in message', (done) => {
try { try {
w.webContents.debugger.attach() w.webContents.debugger.attach()
} catch (err) { } catch (err) {
done('unexpected error : ' + err) done(`unexpected error : ${err}`)
} }
w.webContents.debugger.on('message', (event, method, params) => { w.webContents.debugger.on('message', (event, method, params) => {

View file

@ -1,27 +1,26 @@
const assert = require('assert') const assert = require('assert')
const deprecations = require('electron').deprecations const {deprecations, deprecate} = require('electron')
describe('deprecations', function () { describe('deprecations', () => {
beforeEach(function () { beforeEach(() => {
deprecations.setHandler(null) deprecations.setHandler(null)
process.throwDeprecation = true process.throwDeprecation = true
}) })
it('allows a deprecation handler function to be specified', function () { it('allows a deprecation handler function to be specified', () => {
var messages = [] const messages = []
deprecations.setHandler(function (message) { deprecations.setHandler((message) => {
messages.push(message) messages.push(message)
}) })
require('electron').deprecate.log('this is deprecated') deprecate.log('this is deprecated')
assert.deepEqual(messages, ['this is deprecated']) assert.deepEqual(messages, ['this is deprecated'])
}) })
it('throws an exception if no deprecation handler is specified', function () { it('throws an exception if no deprecation handler is specified', () => {
assert.throws(function () { assert.throws(() => {
require('electron').deprecate.log('this is deprecated') deprecate.log('this is deprecated')
}, /this is deprecated/) }, /this is deprecated/)
}) })
}) })

View file

@ -1,33 +1,31 @@
const assert = require('assert') const assert = require('assert')
const desktopCapturer = require('electron').desktopCapturer const {desktopCapturer, remote} = require('electron')
const isCI = require('electron').remote.getGlobal('isCi') const isCI = remote.getGlobal('isCi')
describe('desktopCapturer', function () { describe('desktopCapturer', () => {
if (isCI && process.platform === 'win32') { if (isCI && process.platform === 'win32') return
return
}
it('should return a non-empty array of sources', function (done) { it('should return a non-empty array of sources', (done) => {
desktopCapturer.getSources({ desktopCapturer.getSources({
types: ['window', 'screen'] types: ['window', 'screen']
}, function (error, sources) { }, (error, sources) => {
assert.equal(error, null) assert.equal(error, null)
assert.notEqual(sources.length, 0) assert.notEqual(sources.length, 0)
done() done()
}) })
}) })
it('throws an error for invalid options', function (done) { it('throws an error for invalid options', (done) => {
desktopCapturer.getSources(['window', 'screen'], function (error) { desktopCapturer.getSources(['window', 'screen'], (error) => {
assert.equal(error.message, 'Invalid options') assert.equal(error.message, 'Invalid options')
done() done()
}) })
}) })
it('does not throw an error when called more than once (regression)', function (done) { it('does not throw an error when called more than once (regression)', (done) => {
var callCount = 0 let callCount = 0
var callback = function (error, sources) { const callback = (error, sources) => {
callCount++ callCount++
assert.equal(error, null) assert.equal(error, null)
assert.notEqual(sources.length, 0) assert.notEqual(sources.length, 0)
@ -38,9 +36,9 @@ describe('desktopCapturer', function () {
desktopCapturer.getSources({types: ['window', 'screen']}, callback) desktopCapturer.getSources({types: ['window', 'screen']}, callback)
}) })
it('responds to subsequest calls of different options', function (done) { it('responds to subsequest calls of different options', (done) => {
var callCount = 0 let callCount = 0
var callback = function (error, sources) { const callback = (error, sources) => {
callCount++ callCount++
assert.equal(error, null) assert.equal(error, null)
if (callCount === 2) done() if (callCount === 2) done()

View file

@ -1,12 +1,10 @@
const {globalShortcut} = require('electron').remote const {globalShortcut} = require('electron').remote
const assert = require('assert')
const assert = require('assert')
const isCI = require('electron').remote.getGlobal('isCi') const isCI = require('electron').remote.getGlobal('isCi')
describe('globalShortcut module', () => { describe('globalShortcut module', () => {
if (isCI && process.platform === 'win32') { if (isCI && process.platform === 'win32') return
return
}
beforeEach(() => { beforeEach(() => {
globalShortcut.unregisterAll() globalShortcut.unregisterAll()

View file

@ -1,19 +1,18 @@
const assert = require('assert') const assert = require('assert')
describe('process module', function () { describe('process module', () => {
describe('process.getCPUUsage()', function () { describe('process.getCPUUsage()', () => {
it('returns a cpu usage object', function () { it('returns a cpu usage object', () => {
const cpuUsage = process.getCPUUsage() const cpuUsage = process.getCPUUsage()
assert.equal(typeof cpuUsage.percentCPUUsage, 'number') assert.equal(typeof cpuUsage.percentCPUUsage, 'number')
assert.equal(typeof cpuUsage.idleWakeupsPerSecond, 'number') assert.equal(typeof cpuUsage.idleWakeupsPerSecond, 'number')
}) })
}) })
describe('process.getIOCounters()', function () { describe('process.getIOCounters()', () => {
it('returns an io counters object', function () { it('returns an io counters object', () => {
if (process.platform === 'darwin') { if (process.platform === 'darwin') return
return
}
const ioCounters = process.getIOCounters() const ioCounters = process.getIOCounters()
assert.equal(typeof ioCounters.readOperationCount, 'number') assert.equal(typeof ioCounters.readOperationCount, 'number')
assert.equal(typeof ioCounters.writeOperationCount, 'number') assert.equal(typeof ioCounters.writeOperationCount, 'number')

View file

@ -1,31 +1,29 @@
const assert = require('assert') const assert = require('assert')
const screen = require('electron').screen const {screen} = require('electron')
describe('screen module', function () { describe('screen module', () => {
describe('screen.getCursorScreenPoint()', function () { describe('screen.getCursorScreenPoint()', () => {
it('returns a point object', function () { it('returns a point object', () => {
var point = screen.getCursorScreenPoint() const point = screen.getCursorScreenPoint()
assert.equal(typeof point.x, 'number') assert.equal(typeof point.x, 'number')
assert.equal(typeof point.y, 'number') assert.equal(typeof point.y, 'number')
}) })
}) })
describe('screen.getPrimaryDisplay()', function () { describe('screen.getPrimaryDisplay()', () => {
it('returns a display object', function () { it('returns a display object', () => {
var display = screen.getPrimaryDisplay() const display = screen.getPrimaryDisplay()
assert.equal(typeof display.scaleFactor, 'number') assert.equal(typeof display.scaleFactor, 'number')
assert(display.size.width > 0) assert(display.size.width > 0)
assert(display.size.height > 0) assert(display.size.height > 0)
}) })
}) })
describe('screen.getMenuBarHeight()', function () { describe('screen.getMenuBarHeight()', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') return
return
}
it('returns an integer', function () { it('returns an integer', () => {
var screenHeight = screen.getMenuBarHeight() const screenHeight = screen.getMenuBarHeight()
assert.equal(typeof screenHeight, 'number') assert.equal(typeof screenHeight, 'number')
}) })
}) })

View file

@ -2,41 +2,37 @@ const assert = require('assert')
const {remote} = require('electron') const {remote} = require('electron')
const {systemPreferences} = remote const {systemPreferences} = remote
describe('systemPreferences module', function () { describe('systemPreferences module', () => {
describe('systemPreferences.getAccentColor', function () { describe('systemPreferences.getAccentColor', () => {
if (process.platform !== 'win32') { if (process.platform !== 'win32') return
return
}
it('should return a non-empty string', function () { it('should return a non-empty string', () => {
let accentColor = systemPreferences.getAccentColor() let accentColor = systemPreferences.getAccentColor()
assert.notEqual(accentColor, null) assert.notEqual(accentColor, null)
assert(accentColor.length > 0) assert(accentColor.length > 0)
}) })
}) })
describe('systemPreferences.getColor(id)', function () { describe('systemPreferences.getColor(id)', () => {
if (process.platform !== 'win32') { if (process.platform !== 'win32') return
return
}
it('throws an error when the id is invalid', function () { it('throws an error when the id is invalid', () => {
assert.throws(function () { assert.throws(() => {
systemPreferences.getColor('not-a-color') systemPreferences.getColor('not-a-color')
}, /Unknown color: not-a-color/) }, /Unknown color: not-a-color/)
}) })
it('returns a hex RGB color string', function () { it('returns a hex RGB color string', () => {
assert.equal(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true) assert.equal(/^#[0-9A-F]{6}$/i.test(systemPreferences.getColor('window')), true)
}) })
}) })
describe('systemPreferences.getUserDefault(key, type)', function () { describe('systemPreferences.getUserDefault(key, type)', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') {
return return
} }
it('returns values for known user defaults', function () { it('returns values for known user defaults', () => {
const locale = systemPreferences.getUserDefault('AppleLocale', 'string') const locale = systemPreferences.getUserDefault('AppleLocale', 'string')
assert.equal(typeof locale, 'string') assert.equal(typeof locale, 'string')
assert(locale.length > 0) assert(locale.length > 0)
@ -46,7 +42,7 @@ describe('systemPreferences module', function () {
assert(languages.length > 0) assert(languages.length > 0)
}) })
it('returns values for unknown user defaults', function () { it('returns values for unknown user defaults', () => {
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false) assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'boolean'), false)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0) assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'integer'), 0)
assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0) assert.equal(systemPreferences.getUserDefault('UserDefaultDoesNotExist', 'float'), 0)
@ -60,12 +56,9 @@ describe('systemPreferences module', function () {
}) })
describe('systemPreferences.setUserDefault(key, type, value)', () => { describe('systemPreferences.setUserDefault(key, type, value)', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') return
return
}
const KEY = 'SystemPreferencesTest' const KEY = 'SystemPreferencesTest'
const TEST_CASES = [ const TEST_CASES = [
['string', 'abc'], ['string', 'abc'],
['boolean', true], ['boolean', true],
@ -101,9 +94,7 @@ describe('systemPreferences module', function () {
}) })
describe('systemPreferences.setUserDefault(key, type, value)', () => { describe('systemPreferences.setUserDefault(key, type, value)', () => {
if (process.platform !== 'darwin') { if (process.platform !== 'darwin') return
return
}
it('removes keys', () => { it('removes keys', () => {
const KEY = 'SystemPreferencesTest' const KEY = 'SystemPreferencesTest'
@ -117,8 +108,8 @@ describe('systemPreferences module', function () {
}) })
}) })
describe('systemPreferences.isInvertedColorScheme()', function () { describe('systemPreferences.isInvertedColorScheme()', () => {
it('returns a boolean', function () { it('returns a boolean', () => {
assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean') assert.equal(typeof systemPreferences.isInvertedColorScheme(), 'boolean')
}) })
}) })

View file

@ -6,22 +6,22 @@ const {closeWindow} = require('./window-helpers')
const {TouchBarButton, TouchBarColorPicker, TouchBarGroup} = TouchBar const {TouchBarButton, TouchBarColorPicker, TouchBarGroup} = TouchBar
const {TouchBarLabel, TouchBarPopover, TouchBarScrubber, TouchBarSegmentedControl, TouchBarSlider, TouchBarSpacer} = TouchBar const {TouchBarLabel, TouchBarPopover, TouchBarScrubber, TouchBarSegmentedControl, TouchBarSlider, TouchBarSpacer} = TouchBar
describe('TouchBar module', function () { describe('TouchBar module', () => {
it('throws an error when created without an options object', function () { it('throws an error when created without an options object', () => {
assert.throws(() => { assert.throws(() => {
const touchBar = new TouchBar() const touchBar = new TouchBar()
touchBar.toString() touchBar.toString()
}, /Must specify options object as first argument/) }, /Must specify options object as first argument/)
}) })
it('throws an error when created with invalid items', function () { it('throws an error when created with invalid items', () => {
assert.throws(() => { assert.throws(() => {
const touchBar = new TouchBar({items: [1, true, {}, []]}) const touchBar = new TouchBar({items: [1, true, {}, []]})
touchBar.toString() touchBar.toString()
}, /Each item must be an instance of TouchBarItem/) }, /Each item must be an instance of TouchBarItem/)
}) })
it('throws an error when an invalid escape item is set', function () { it('throws an error when an invalid escape item is set', () => {
assert.throws(() => { assert.throws(() => {
const touchBar = new TouchBar({items: [], escapeItem: 'esc'}) const touchBar = new TouchBar({items: [], escapeItem: 'esc'})
touchBar.toString() touchBar.toString()
@ -33,19 +33,19 @@ describe('TouchBar module', function () {
}, /Escape item must be an instance of TouchBarItem/) }, /Escape item must be an instance of TouchBarItem/)
}) })
describe('BrowserWindow behavior', function () { describe('BrowserWindow behavior', () => {
let window let window
beforeEach(function () { beforeEach(() => {
window = new BrowserWindow() window = new BrowserWindow()
}) })
afterEach(function () { afterEach(() => {
window.setTouchBar(null) window.setTouchBar(null)
return closeWindow(window).then(function () { window = null }) return closeWindow(window).then(() => { window = null })
}) })
it('can be added to and removed from a window', function () { it('can be added to and removed from a window', () => {
const label = new TouchBarLabel({label: 'bar'}) const label = new TouchBarLabel({label: 'bar'})
const touchBar = new TouchBar([ const touchBar = new TouchBar([
new TouchBarButton({label: 'foo', backgroundColor: '#F00', click: () => {}}), new TouchBarButton({label: 'foo', backgroundColor: '#F00', click: () => {}}),
@ -73,9 +73,7 @@ describe('TouchBar module', function () {
showArrowButtons: true showArrowButtons: true
}) })
]) ])
const escapeButton = new TouchBarButton({ const escapeButton = new TouchBarButton({label: 'foo'})
label: 'foo'
})
window.setTouchBar(touchBar) window.setTouchBar(touchBar)
touchBar.escapeItem = escapeButton touchBar.escapeItem = escapeButton
label.label = 'baz' label.label = 'baz'
@ -85,7 +83,7 @@ describe('TouchBar module', function () {
touchBar.escapeItem = null touchBar.escapeItem = null
}) })
it('calls the callback on the items when a window interaction event fires', function (done) { it('calls the callback on the items when a window interaction event fires', (done) => {
const button = new TouchBarButton({ const button = new TouchBarButton({
label: 'bar', label: 'bar',
click: () => { click: () => {
@ -97,7 +95,7 @@ describe('TouchBar module', function () {
window.emit('-touch-bar-interaction', {}, button.id) window.emit('-touch-bar-interaction', {}, button.id)
}) })
it('calls the callback on the escape item when a window interaction event fires', function (done) { it('calls the callback on the escape item when a window interaction event fires', (done) => {
const button = new TouchBarButton({ const button = new TouchBarButton({
label: 'bar', label: 'bar',
click: () => { click: () => {