electron/spec/api-power-monitor-spec.ts

183 lines
6.9 KiB
TypeScript
Raw Normal View History

// 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/spec-runner.js 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.
2020-03-20 20:28:31 +00:00
import { expect } from 'chai';
import * as dbus from 'dbus-native';
2020-06-26 20:59:54 +00:00
import { ifdescribe, delay } from './spec-helpers';
2020-03-20 20:28:31 +00:00
import { promisify } from 'util';
describe('powerMonitor', () => {
2020-03-20 20:28:31 +00:00
let logindMock: any, dbusMockPowerMonitor: any, getCalls: any, emitSignal: any, reset: any;
ifdescribe(process.platform === 'linux' && process.env.DBUS_SYSTEM_BUS_ADDRESS != null)('when powerMonitor module is loaded with dbus mock', () => {
before(async () => {
2020-03-20 20:28:31 +00:00
const systemBus = dbus.systemBus();
const loginService = systemBus.getService('org.freedesktop.login1');
const getInterface = promisify(loginService.getInterface.bind(loginService));
logindMock = await getInterface('/org/freedesktop/login1', 'org.freedesktop.DBus.Mock');
getCalls = promisify(logindMock.GetCalls.bind(logindMock));
emitSignal = promisify(logindMock.EmitSignal.bind(logindMock));
reset = promisify(logindMock.Reset.bind(logindMock));
});
after(async () => {
2020-03-20 20:28:31 +00:00
await reset();
});
2019-08-28 20:54:50 +00:00
function onceMethodCalled (done: () => void) {
function cb () {
2020-03-20 20:28:31 +00:00
logindMock.removeListener('MethodCalled', cb);
}
2020-03-20 20:28:31 +00:00
done();
return cb;
}
before(done => {
2020-03-20 20:28:31 +00:00
logindMock.on('MethodCalled', onceMethodCalled(done));
// lazy load powerMonitor after we listen to MethodCalled mock signal
2020-03-20 20:28:31 +00:00
dbusMockPowerMonitor = require('electron').powerMonitor;
});
2020-03-24 16:03:29 +00:00
it('should call Inhibit to delay suspend once a listener is added', async () => {
// No calls to dbus until a listener is added
{
const calls = await getCalls();
expect(calls).to.be.an('array').that.has.lengthOf(0);
}
// Add a dummy listener to engage the monitors
dbusMockPowerMonitor.on('dummy-event', () => {});
try {
let retriesRemaining = 3;
// There doesn't seem to be a way to get a notification when a call
// happens, so poll `getCalls` a few times to reduce flake.
let calls: any[] = [];
while (retriesRemaining-- > 0) {
calls = await getCalls();
if (calls.length > 0) break;
2020-06-26 20:59:54 +00:00
await delay(1000);
2020-03-24 16:03:29 +00:00
}
expect(calls).to.be.an('array').that.has.lengthOf(1);
expect(calls[0].slice(1)).to.deep.equal([
'Inhibit', [
[[{ type: 's', child: [] }], ['sleep']],
[[{ type: 's', child: [] }], ['electron']],
[[{ type: 's', child: [] }], ['Application cleanup before suspend']],
[[{ type: 's', child: [] }], ['delay']]
]
]);
} finally {
dbusMockPowerMonitor.removeAllListeners('dummy-event');
}
2020-03-20 20:28:31 +00:00
});
describe('when PrepareForSleep(true) signal is sent by logind', () => {
it('should emit "suspend" event', (done) => {
2020-03-20 20:28:31 +00:00
dbusMockPowerMonitor.once('suspend', () => done());
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
2020-03-20 20:28:31 +00:00
'b', [['b', true]]);
});
describe('when PrepareForSleep(false) signal is sent by logind', () => {
it('should emit "resume" event', done => {
2020-03-20 20:28:31 +00:00
dbusMockPowerMonitor.once('resume', () => done());
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
2020-03-20 20:28:31 +00:00
'b', [['b', false]]);
});
it('should have called Inhibit again', async () => {
2020-03-20 20:28:31 +00:00
const calls = await getCalls();
expect(calls).to.be.an('array').that.has.lengthOf(2);
expect(calls[1].slice(1)).to.deep.equal([
'Inhibit', [
2018-09-13 16:10:51 +00:00
[[{ type: 's', child: [] }], ['sleep']],
[[{ type: 's', child: [] }], ['electron']],
[[{ type: 's', child: [] }], ['Application cleanup before suspend']],
[[{ type: 's', child: [] }], ['delay']]
]
2020-03-20 20:28:31 +00:00
]);
});
});
});
describe('when a listener is added to shutdown event', () => {
before(async () => {
2020-03-20 20:28:31 +00:00
const calls = await getCalls();
expect(calls).to.be.an('array').that.has.lengthOf(2);
dbusMockPowerMonitor.once('shutdown', () => { });
});
it('should call Inhibit to delay shutdown', async () => {
2020-03-20 20:28:31 +00:00
const calls = await getCalls();
expect(calls).to.be.an('array').that.has.lengthOf(3);
expect(calls[2].slice(1)).to.deep.equal([
'Inhibit', [
2018-09-13 16:10:51 +00:00
[[{ type: 's', child: [] }], ['shutdown']],
[[{ type: 's', child: [] }], ['electron']],
[[{ type: 's', child: [] }], ['Ensure a clean shutdown']],
[[{ type: 's', child: [] }], ['delay']]
]
2020-03-20 20:28:31 +00:00
]);
});
describe('when PrepareForShutdown(true) signal is sent by logind', () => {
it('should emit "shutdown" event', done => {
2020-03-20 20:28:31 +00:00
dbusMockPowerMonitor.once('shutdown', () => { done(); });
emitSignal('org.freedesktop.login1.Manager', 'PrepareForShutdown',
2020-03-20 20:28:31 +00:00
'b', [['b', true]]);
});
});
});
});
describe('when powerMonitor module is loaded', () => {
2019-11-01 20:37:02 +00:00
// eslint-disable-next-line no-undef
2020-03-20 20:28:31 +00:00
let powerMonitor: typeof Electron.powerMonitor;
before(() => {
2020-03-20 20:28:31 +00:00
powerMonitor = require('electron').powerMonitor;
});
describe('powerMonitor.getSystemIdleState', () => {
it('gets current system idle state', () => {
// this function is not mocked out, so we can test the result's
// form and type but not its value.
2020-03-20 20:28:31 +00:00
const idleState = powerMonitor.getSystemIdleState(1);
expect(idleState).to.be.a('string');
const validIdleStates = ['active', 'idle', 'locked', 'unknown'];
expect(validIdleStates).to.include(idleState);
});
it('does not accept non positive integer threshold', () => {
expect(() => {
2020-03-20 20:28:31 +00:00
powerMonitor.getSystemIdleState(-1);
}).to.throw(/must be greater than 0/);
expect(() => {
2020-03-20 20:28:31 +00:00
powerMonitor.getSystemIdleState(NaN);
}).to.throw(/conversion failure/);
expect(() => {
2020-03-20 20:28:31 +00:00
powerMonitor.getSystemIdleState('a' as any);
}).to.throw(/conversion failure/);
});
});
describe('powerMonitor.getSystemIdleTime', () => {
it('returns current system idle time', () => {
2020-03-20 20:28:31 +00:00
const idleTime = powerMonitor.getSystemIdleTime();
expect(idleTime).to.be.at.least(0);
});
});
describe('powerMonitor.onBatteryPower', () => {
it('returns a boolean', () => {
expect(powerMonitor.onBatteryPower).to.be.a('boolean');
expect(powerMonitor.isOnBatteryPower()).to.be.a('boolean');
});
});
2020-03-20 20:28:31 +00:00
});
});