2017-12-04 19:52:07 +00:00
|
|
|
// For these tests we use a fake DBus daemon to verify powerMonitor module
|
|
|
|
// interaction with the system bus. This requires python-dbusmock installed and
|
|
|
|
// running (with the DBUS_SYSTEM_BUS_ADDRESS environment variable set).
|
|
|
|
// script/test.py will take care of spawning the fake DBus daemon and setting
|
|
|
|
// DBUS_SYSTEM_BUS_ADDRESS when python-dbusmock is installed.
|
|
|
|
//
|
|
|
|
// See https://pypi.python.org/pypi/python-dbusmock for more information about
|
|
|
|
// python-dbusmock.
|
2018-08-31 20:52:32 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
2017-12-04 19:52:07 +00:00
|
|
|
const dbus = require('dbus-native')
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
|
2018-08-31 20:52:32 +00:00
|
|
|
const {expect} = chai
|
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2018-03-14 05:42:08 +00:00
|
|
|
const skip = process.platform !== 'linux' || !process.env.DBUS_SYSTEM_BUS_ADDRESS
|
2017-12-04 19:52:07 +00:00
|
|
|
|
2018-09-09 00:37:19 +00:00
|
|
|
// TODO(alexeykuzmin): [Ch66] Crashes on Linux ia32. Fix it and enable back.
|
|
|
|
xdescribe('powerMonitor', () => {
|
2018-03-14 05:42:08 +00:00
|
|
|
let logindMock, dbusMockPowerMonitor, getCalls, emitSignal, reset
|
2017-12-04 19:52:07 +00:00
|
|
|
|
2018-03-14 05:42:08 +00:00
|
|
|
if (!skip) {
|
|
|
|
before(async () => {
|
|
|
|
const systemBus = dbus.systemBus()
|
|
|
|
const loginService = systemBus.getService('org.freedesktop.login1')
|
|
|
|
const getInterface = Promise.promisify(loginService.getInterface, {context: loginService})
|
|
|
|
logindMock = await getInterface('/org/freedesktop/login1', 'org.freedesktop.DBus.Mock')
|
|
|
|
getCalls = Promise.promisify(logindMock.GetCalls, {context: logindMock})
|
|
|
|
emitSignal = Promise.promisify(logindMock.EmitSignal, {context: logindMock})
|
|
|
|
reset = Promise.promisify(logindMock.Reset, {context: logindMock})
|
|
|
|
})
|
|
|
|
|
2018-09-09 00:37:19 +00:00
|
|
|
after(reset)
|
2018-03-14 05:42:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
(skip ? describe.skip : describe)('when powerMonitor module is loaded with dbus mock', () => {
|
2017-12-04 19:52:07 +00:00
|
|
|
function onceMethodCalled (done) {
|
|
|
|
function cb () {
|
|
|
|
logindMock.removeListener('MethodCalled', cb)
|
|
|
|
}
|
|
|
|
done()
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
2018-08-31 20:52:32 +00:00
|
|
|
before(done => {
|
2017-12-04 19:52:07 +00:00
|
|
|
logindMock.on('MethodCalled', onceMethodCalled(done))
|
|
|
|
// lazy load powerMonitor after we listen to MethodCalled mock signal
|
2018-03-14 05:42:08 +00:00
|
|
|
dbusMockPowerMonitor = require('electron').remote.powerMonitor
|
2017-12-04 19:52:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should call Inhibit to delay suspend', async () => {
|
|
|
|
const calls = await getCalls()
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(1)
|
|
|
|
expect(calls[0].slice(1)).to.deep.equal([
|
2017-12-04 19:52:07 +00:00
|
|
|
'Inhibit', [
|
|
|
|
[[{type: 's', child: []}], ['sleep']],
|
|
|
|
[[{type: 's', child: []}], ['electron']],
|
|
|
|
[[{type: 's', child: []}], ['Application cleanup before suspend']],
|
|
|
|
[[{type: 's', child: []}], ['delay']]
|
|
|
|
]
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when PrepareForSleep(true) signal is sent by logind', () => {
|
|
|
|
it('should emit "suspend" event', (done) => {
|
2018-03-14 05:42:08 +00:00
|
|
|
dbusMockPowerMonitor.once('suspend', () => done())
|
2017-12-04 19:52:07 +00:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
|
|
|
|
'b', [['b', true]])
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when PrepareForSleep(false) signal is sent by logind', () => {
|
2018-08-31 20:52:32 +00:00
|
|
|
it('should emit "resume" event', done => {
|
2018-03-14 05:42:08 +00:00
|
|
|
dbusMockPowerMonitor.once('resume', () => done())
|
2017-12-04 19:52:07 +00:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
|
|
|
|
'b', [['b', false]])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should have called Inhibit again', async () => {
|
|
|
|
const calls = await getCalls()
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(2)
|
|
|
|
expect(calls[1].slice(1)).to.deep.equal([
|
2017-12-04 19:52:07 +00:00
|
|
|
'Inhibit', [
|
|
|
|
[[{type: 's', child: []}], ['sleep']],
|
|
|
|
[[{type: 's', child: []}], ['electron']],
|
|
|
|
[[{type: 's', child: []}], ['Application cleanup before suspend']],
|
|
|
|
[[{type: 's', child: []}], ['delay']]
|
|
|
|
]
|
|
|
|
])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-12-12 17:06:45 +00:00
|
|
|
|
|
|
|
describe('when a listener is added to shutdown event', () => {
|
|
|
|
before(async () => {
|
|
|
|
const calls = await getCalls()
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(2)
|
2018-03-14 05:42:08 +00:00
|
|
|
dbusMockPowerMonitor.once('shutdown', () => { })
|
2017-12-12 17:06:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should call Inhibit to delay shutdown', async () => {
|
|
|
|
const calls = await getCalls()
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(3)
|
|
|
|
expect(calls[2].slice(1)).to.deep.equal([
|
2017-12-12 17:06:45 +00:00
|
|
|
'Inhibit', [
|
|
|
|
[[{type: 's', child: []}], ['shutdown']],
|
|
|
|
[[{type: 's', child: []}], ['electron']],
|
|
|
|
[[{type: 's', child: []}], ['Ensure a clean shutdown']],
|
|
|
|
[[{type: 's', child: []}], ['delay']]
|
|
|
|
]
|
|
|
|
])
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('when PrepareForShutdown(true) signal is sent by logind', () => {
|
2018-08-31 20:52:32 +00:00
|
|
|
it('should emit "shutdown" event', done => {
|
2018-03-14 05:42:08 +00:00
|
|
|
dbusMockPowerMonitor.once('shutdown', () => { done() })
|
2017-12-12 17:06:45 +00:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForShutdown',
|
|
|
|
'b', [['b', true]])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-12-04 19:52:07 +00:00
|
|
|
})
|
2018-03-14 05:42:08 +00:00
|
|
|
|
|
|
|
describe('when powerMonitor module is loaded', () => {
|
|
|
|
let powerMonitor
|
|
|
|
before(() => {
|
|
|
|
powerMonitor = require('electron').remote.powerMonitor
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('powerMonitor.querySystemIdleState', () => {
|
2018-08-31 20:52:32 +00:00
|
|
|
it('notify current system idle state', done => {
|
|
|
|
powerMonitor.querySystemIdleState(1, idleState => {
|
2018-09-09 00:37:19 +00:00
|
|
|
expect(idleState).to.be.true()
|
2018-03-14 05:42:08 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('does not accept non positive integer threshold', () => {
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(() => {
|
|
|
|
powerMonitor.querySystemIdleState(-1, (idleState) => {})
|
|
|
|
}).to.throw()
|
2018-03-14 05:42:08 +00:00
|
|
|
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(() => {
|
|
|
|
powerMonitor.querySystemIdleState(NaN, (idleState) => {})
|
|
|
|
}).to.throw()
|
2018-03-14 05:42:08 +00:00
|
|
|
|
2018-08-31 20:52:32 +00:00
|
|
|
expect(() => {
|
|
|
|
powerMonitor.querySystemIdleState('a', (idleState) => {})
|
|
|
|
}).to.throw()
|
2018-03-14 05:42:08 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('powerMonitor.querySystemIdleTime', () => {
|
2018-08-31 20:52:32 +00:00
|
|
|
it('notify current system idle time', done => {
|
|
|
|
powerMonitor.querySystemIdleTime(idleTime => {
|
|
|
|
expect(idleTime).to.be.at.least(0)
|
2018-03-14 05:42:08 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-12-04 19:52:07 +00:00
|
|
|
})
|