2017-12-04 22:52:07 +03: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).
|
2018-09-27 08:49:02 +03:00
|
|
|
// script/spec-runner.js will take care of spawning the fake DBus daemon and setting
|
2017-12-04 22:52:07 +03:00
|
|
|
// DBUS_SYSTEM_BUS_ADDRESS when python-dbusmock is installed.
|
|
|
|
//
|
|
|
|
// See https://pypi.python.org/pypi/python-dbusmock for more information about
|
|
|
|
// python-dbusmock.
|
2019-08-28 23:54:50 +03:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as dbus from 'dbus-native';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { ifdescribe, delay } from './lib/spec-helpers';
|
2019-08-28 23:54:50 +03:00
|
|
|
import { promisify } from 'util';
|
2017-12-04 22:52:07 +03:00
|
|
|
|
2018-10-09 08:32:19 +03:00
|
|
|
describe('powerMonitor', () => {
|
2019-08-28 23:54:50 +03:00
|
|
|
let logindMock: any, dbusMockPowerMonitor: any, getCalls: any, emitSignal: any, reset: any;
|
2017-12-04 22:52:07 +03:00
|
|
|
|
2022-10-13 17:09:09 +03:00
|
|
|
ifdescribe(process.platform === 'linux' && process.env.DBUS_SYSTEM_BUS_ADDRESS != null)('when powerMonitor module is loaded with dbus mock', () => {
|
2018-03-14 08:42:08 +03:00
|
|
|
before(async () => {
|
|
|
|
const systemBus = dbus.systemBus();
|
|
|
|
const loginService = systemBus.getService('org.freedesktop.login1');
|
2019-08-28 23:54:50 +03:00
|
|
|
const getInterface = promisify(loginService.getInterface.bind(loginService));
|
2018-03-14 08:42:08 +03:00
|
|
|
logindMock = await getInterface('/org/freedesktop/login1', 'org.freedesktop.DBus.Mock');
|
2019-08-28 23:54:50 +03:00
|
|
|
getCalls = promisify(logindMock.GetCalls.bind(logindMock));
|
|
|
|
emitSignal = promisify(logindMock.EmitSignal.bind(logindMock));
|
|
|
|
reset = promisify(logindMock.Reset.bind(logindMock));
|
2018-03-14 08:42:08 +03:00
|
|
|
});
|
|
|
|
|
2018-10-09 08:32:19 +03:00
|
|
|
after(async () => {
|
|
|
|
await reset();
|
|
|
|
});
|
2018-03-14 08:42:08 +03:00
|
|
|
|
2019-08-28 23:54:50 +03:00
|
|
|
function onceMethodCalled (done: () => void) {
|
2017-12-04 22:52:07 +03:00
|
|
|
function cb () {
|
|
|
|
logindMock.removeListener('MethodCalled', cb);
|
|
|
|
}
|
|
|
|
done();
|
|
|
|
return cb;
|
|
|
|
}
|
|
|
|
|
2018-08-31 23:52:32 +03:00
|
|
|
before(done => {
|
2017-12-04 22:52:07 +03:00
|
|
|
logindMock.on('MethodCalled', onceMethodCalled(done));
|
|
|
|
// lazy load powerMonitor after we listen to MethodCalled mock signal
|
2019-08-28 23:54:50 +03:00
|
|
|
dbusMockPowerMonitor = require('electron').powerMonitor;
|
2017-12-04 22:52:07 +03:00
|
|
|
});
|
|
|
|
|
2020-03-24 19:03:29 +03: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 23:59:54 +03:00
|
|
|
await delay(1000);
|
2020-03-24 19:03:29 +03: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');
|
|
|
|
}
|
2017-12-04 22:52:07 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when PrepareForSleep(true) signal is sent by logind', () => {
|
|
|
|
it('should emit "suspend" event', (done) => {
|
2018-03-14 08:42:08 +03:00
|
|
|
dbusMockPowerMonitor.once('suspend', () => done());
|
2017-12-04 22:52:07 +03:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
|
|
|
|
'b', [['b', true]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when PrepareForSleep(false) signal is sent by logind', () => {
|
2018-08-31 23:52:32 +03:00
|
|
|
it('should emit "resume" event', done => {
|
2018-03-14 08:42:08 +03:00
|
|
|
dbusMockPowerMonitor.once('resume', () => done());
|
2017-12-04 22:52:07 +03:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
|
|
|
|
'b', [['b', false]]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should have called Inhibit again', async () => {
|
|
|
|
const calls = await getCalls();
|
2018-08-31 23:52:32 +03:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(2);
|
|
|
|
expect(calls[1].slice(1)).to.deep.equal([
|
2017-12-04 22:52:07 +03:00
|
|
|
'Inhibit', [
|
2018-09-13 19:10:51 +03:00
|
|
|
[[{ type: 's', child: [] }], ['sleep']],
|
|
|
|
[[{ type: 's', child: [] }], ['electron']],
|
|
|
|
[[{ type: 's', child: [] }], ['Application cleanup before suspend']],
|
|
|
|
[[{ type: 's', child: [] }], ['delay']]
|
2017-12-04 22:52:07 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-12 20:06:45 +03:00
|
|
|
|
|
|
|
describe('when a listener is added to shutdown event', () => {
|
|
|
|
before(async () => {
|
|
|
|
const calls = await getCalls();
|
2018-08-31 23:52:32 +03:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(2);
|
2018-03-14 08:42:08 +03:00
|
|
|
dbusMockPowerMonitor.once('shutdown', () => { });
|
2017-12-12 20:06:45 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call Inhibit to delay shutdown', async () => {
|
|
|
|
const calls = await getCalls();
|
2018-08-31 23:52:32 +03:00
|
|
|
expect(calls).to.be.an('array').that.has.lengthOf(3);
|
|
|
|
expect(calls[2].slice(1)).to.deep.equal([
|
2017-12-12 20:06:45 +03:00
|
|
|
'Inhibit', [
|
2018-09-13 19:10:51 +03:00
|
|
|
[[{ type: 's', child: [] }], ['shutdown']],
|
|
|
|
[[{ type: 's', child: [] }], ['electron']],
|
|
|
|
[[{ type: 's', child: [] }], ['Ensure a clean shutdown']],
|
|
|
|
[[{ type: 's', child: [] }], ['delay']]
|
2017-12-12 20:06:45 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when PrepareForShutdown(true) signal is sent by logind', () => {
|
2018-08-31 23:52:32 +03:00
|
|
|
it('should emit "shutdown" event', done => {
|
2018-03-14 08:42:08 +03:00
|
|
|
dbusMockPowerMonitor.once('shutdown', () => { done(); });
|
2017-12-12 20:06:45 +03:00
|
|
|
emitSignal('org.freedesktop.login1.Manager', 'PrepareForShutdown',
|
|
|
|
'b', [['b', true]]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-12-04 22:52:07 +03:00
|
|
|
});
|
2018-03-14 08:42:08 +03:00
|
|
|
|
|
|
|
describe('when powerMonitor module is loaded', () => {
|
2019-11-01 23:37:02 +03:00
|
|
|
// eslint-disable-next-line no-undef
|
2019-08-28 23:54:50 +03:00
|
|
|
let powerMonitor: typeof Electron.powerMonitor;
|
2018-03-14 08:42:08 +03:00
|
|
|
before(() => {
|
2019-08-28 23:54:50 +03:00
|
|
|
powerMonitor = require('electron').powerMonitor;
|
2018-03-14 08:42:08 +03:00
|
|
|
});
|
2019-02-27 23:54:01 +03:00
|
|
|
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.
|
|
|
|
const idleState = powerMonitor.getSystemIdleState(1);
|
|
|
|
expect(idleState).to.be.a('string');
|
2020-03-20 18:12:18 +03:00
|
|
|
const validIdleStates = ['active', 'idle', 'locked', 'unknown'];
|
2019-02-27 23:54:01 +03:00
|
|
|
expect(validIdleStates).to.include(idleState);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not accept non positive integer threshold', () => {
|
|
|
|
expect(() => {
|
|
|
|
powerMonitor.getSystemIdleState(-1);
|
|
|
|
}).to.throw(/must be greater than 0/);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
powerMonitor.getSystemIdleState(NaN);
|
|
|
|
}).to.throw(/conversion failure/);
|
|
|
|
|
|
|
|
expect(() => {
|
2019-08-28 23:54:50 +03:00
|
|
|
powerMonitor.getSystemIdleState('a' as any);
|
2019-02-27 23:54:01 +03:00
|
|
|
}).to.throw(/conversion failure/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('powerMonitor.getSystemIdleTime', () => {
|
2020-11-17 03:31:46 +03:00
|
|
|
it('returns current system idle time', () => {
|
2019-02-27 23:54:01 +03:00
|
|
|
const idleTime = powerMonitor.getSystemIdleTime();
|
|
|
|
expect(idleTime).to.be.at.least(0);
|
|
|
|
});
|
|
|
|
});
|
2020-11-17 03:31:46 +03:00
|
|
|
|
|
|
|
describe('powerMonitor.onBatteryPower', () => {
|
|
|
|
it('returns a boolean', () => {
|
|
|
|
expect(powerMonitor.onBatteryPower).to.be.a('boolean');
|
|
|
|
expect(powerMonitor.isOnBatteryPower()).to.be.a('boolean');
|
|
|
|
});
|
|
|
|
});
|
2018-03-14 08:42:08 +03:00
|
|
|
});
|
2017-12-04 22:52:07 +03:00
|
|
|
});
|