api-power-monitor-spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // For these tests we use a fake DBus daemon to verify powerMonitor module
  2. // interaction with the system bus. This requires python-dbusmock installed and
  3. // running (with the DBUS_SYSTEM_BUS_ADDRESS environment variable set).
  4. // script/test.py will take care of spawning the fake DBus daemon and setting
  5. // DBUS_SYSTEM_BUS_ADDRESS when python-dbusmock is installed.
  6. //
  7. // See https://pypi.python.org/pypi/python-dbusmock for more information about
  8. // python-dbusmock.
  9. const assert = require('assert')
  10. const dbus = require('dbus-native')
  11. const Promise = require('bluebird')
  12. const skip = process.platform !== 'linux' || !process.env.DBUS_SYSTEM_BUS_ADDRESS;
  13. (skip ? describe.skip : describe)('powerMonitor', () => {
  14. let logindMock, powerMonitor, getCalls, emitSignal, reset
  15. before(async () => {
  16. const systemBus = dbus.systemBus()
  17. const loginService = systemBus.getService('org.freedesktop.login1')
  18. const getInterface = Promise.promisify(loginService.getInterface, {context: loginService})
  19. logindMock = await getInterface('/org/freedesktop/login1', 'org.freedesktop.DBus.Mock')
  20. getCalls = Promise.promisify(logindMock.GetCalls, {context: logindMock})
  21. emitSignal = Promise.promisify(logindMock.EmitSignal, {context: logindMock})
  22. reset = Promise.promisify(logindMock.Reset, {context: logindMock})
  23. })
  24. after(async () => {
  25. await reset()
  26. })
  27. describe('when powerMonitor module is loaded', () => {
  28. function onceMethodCalled (done) {
  29. function cb () {
  30. logindMock.removeListener('MethodCalled', cb)
  31. }
  32. done()
  33. return cb
  34. }
  35. before((done) => {
  36. logindMock.on('MethodCalled', onceMethodCalled(done))
  37. // lazy load powerMonitor after we listen to MethodCalled mock signal
  38. powerMonitor = require('electron').remote.powerMonitor
  39. })
  40. it('should call Inhibit to delay suspend', async () => {
  41. const calls = await getCalls()
  42. assert.equal(calls.length, 1)
  43. assert.deepEqual(calls[0].slice(1), [
  44. 'Inhibit', [
  45. [[{type: 's', child: []}], ['sleep']],
  46. [[{type: 's', child: []}], ['electron']],
  47. [[{type: 's', child: []}], ['Application cleanup before suspend']],
  48. [[{type: 's', child: []}], ['delay']]
  49. ]
  50. ])
  51. })
  52. describe('when PrepareForSleep(true) signal is sent by logind', () => {
  53. it('should emit "suspend" event', (done) => {
  54. powerMonitor.once('suspend', () => done())
  55. emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
  56. 'b', [['b', true]])
  57. })
  58. describe('when PrepareForSleep(false) signal is sent by logind', () => {
  59. it('should emit "resume" event', (done) => {
  60. powerMonitor.once('resume', () => done())
  61. emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
  62. 'b', [['b', false]])
  63. })
  64. it('should have called Inhibit again', async () => {
  65. const calls = await getCalls()
  66. assert.equal(calls.length, 2)
  67. assert.deepEqual(calls[1].slice(1), [
  68. 'Inhibit', [
  69. [[{type: 's', child: []}], ['sleep']],
  70. [[{type: 's', child: []}], ['electron']],
  71. [[{type: 's', child: []}], ['Application cleanup before suspend']],
  72. [[{type: 's', child: []}], ['delay']]
  73. ]
  74. ])
  75. })
  76. })
  77. })
  78. describe('when a listener is added to shutdown event', () => {
  79. before(async () => {
  80. const calls = await getCalls()
  81. assert.equal(calls.length, 2)
  82. powerMonitor.once('shutdown', () => { })
  83. })
  84. it('should call Inhibit to delay shutdown', async () => {
  85. const calls = await getCalls()
  86. assert.equal(calls.length, 3)
  87. assert.deepEqual(calls[2].slice(1), [
  88. 'Inhibit', [
  89. [[{type: 's', child: []}], ['shutdown']],
  90. [[{type: 's', child: []}], ['electron']],
  91. [[{type: 's', child: []}], ['Ensure a clean shutdown']],
  92. [[{type: 's', child: []}], ['delay']]
  93. ]
  94. ])
  95. })
  96. describe('when PrepareForShutdown(true) signal is sent by logind', () => {
  97. it('should emit "shutdown" event', (done) => {
  98. powerMonitor.once('shutdown', () => { done() })
  99. emitSignal('org.freedesktop.login1.Manager', 'PrepareForShutdown',
  100. 'b', [['b', true]])
  101. })
  102. })
  103. })
  104. })
  105. })