api-power-monitor-spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. describe('powerMonitor', () => {
  14. let logindMock, dbusMockPowerMonitor, getCalls, emitSignal, reset
  15. if (!skip) {
  16. before(async () => {
  17. const systemBus = dbus.systemBus()
  18. const loginService = systemBus.getService('org.freedesktop.login1')
  19. const getInterface = Promise.promisify(loginService.getInterface, {context: loginService})
  20. logindMock = await getInterface('/org/freedesktop/login1', 'org.freedesktop.DBus.Mock')
  21. getCalls = Promise.promisify(logindMock.GetCalls, {context: logindMock})
  22. emitSignal = Promise.promisify(logindMock.EmitSignal, {context: logindMock})
  23. reset = Promise.promisify(logindMock.Reset, {context: logindMock})
  24. })
  25. after(async () => {
  26. await reset()
  27. })
  28. }
  29. (skip ? describe.skip : describe)('when powerMonitor module is loaded with dbus mock', () => {
  30. function onceMethodCalled (done) {
  31. function cb () {
  32. logindMock.removeListener('MethodCalled', cb)
  33. }
  34. done()
  35. return cb
  36. }
  37. before((done) => {
  38. logindMock.on('MethodCalled', onceMethodCalled(done))
  39. // lazy load powerMonitor after we listen to MethodCalled mock signal
  40. dbusMockPowerMonitor = require('electron').remote.powerMonitor
  41. })
  42. it('should call Inhibit to delay suspend', async () => {
  43. const calls = await getCalls()
  44. assert.equal(calls.length, 1)
  45. assert.deepEqual(calls[0].slice(1), [
  46. 'Inhibit', [
  47. [[{type: 's', child: []}], ['sleep']],
  48. [[{type: 's', child: []}], ['electron']],
  49. [[{type: 's', child: []}], ['Application cleanup before suspend']],
  50. [[{type: 's', child: []}], ['delay']]
  51. ]
  52. ])
  53. })
  54. describe('when PrepareForSleep(true) signal is sent by logind', () => {
  55. it('should emit "suspend" event', (done) => {
  56. dbusMockPowerMonitor.once('suspend', () => done())
  57. emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
  58. 'b', [['b', true]])
  59. })
  60. describe('when PrepareForSleep(false) signal is sent by logind', () => {
  61. it('should emit "resume" event', (done) => {
  62. dbusMockPowerMonitor.once('resume', () => done())
  63. emitSignal('org.freedesktop.login1.Manager', 'PrepareForSleep',
  64. 'b', [['b', false]])
  65. })
  66. it('should have called Inhibit again', async () => {
  67. const calls = await getCalls()
  68. assert.equal(calls.length, 2)
  69. assert.deepEqual(calls[1].slice(1), [
  70. 'Inhibit', [
  71. [[{type: 's', child: []}], ['sleep']],
  72. [[{type: 's', child: []}], ['electron']],
  73. [[{type: 's', child: []}], ['Application cleanup before suspend']],
  74. [[{type: 's', child: []}], ['delay']]
  75. ]
  76. ])
  77. })
  78. })
  79. })
  80. describe('when a listener is added to shutdown event', () => {
  81. before(async () => {
  82. const calls = await getCalls()
  83. assert.equal(calls.length, 2)
  84. dbusMockPowerMonitor.once('shutdown', () => { })
  85. })
  86. it('should call Inhibit to delay shutdown', async () => {
  87. const calls = await getCalls()
  88. assert.equal(calls.length, 3)
  89. assert.deepEqual(calls[2].slice(1), [
  90. 'Inhibit', [
  91. [[{type: 's', child: []}], ['shutdown']],
  92. [[{type: 's', child: []}], ['electron']],
  93. [[{type: 's', child: []}], ['Ensure a clean shutdown']],
  94. [[{type: 's', child: []}], ['delay']]
  95. ]
  96. ])
  97. })
  98. describe('when PrepareForShutdown(true) signal is sent by logind', () => {
  99. it('should emit "shutdown" event', (done) => {
  100. dbusMockPowerMonitor.once('shutdown', () => { done() })
  101. emitSignal('org.freedesktop.login1.Manager', 'PrepareForShutdown',
  102. 'b', [['b', true]])
  103. })
  104. })
  105. })
  106. })
  107. describe('when powerMonitor module is loaded', () => {
  108. let powerMonitor
  109. before(() => {
  110. powerMonitor = require('electron').remote.powerMonitor
  111. })
  112. describe('powerMonitor.querySystemIdleState', () => {
  113. it('notify current system idle state', (done) => {
  114. powerMonitor.querySystemIdleState(1, (idleState) => {
  115. assert.ok(idleState)
  116. done()
  117. })
  118. })
  119. it('does not accept non positive integer threshold', () => {
  120. assert.throws(() => {
  121. powerMonitor.querySystemIdleState(-1, (idleState) => {
  122. })
  123. })
  124. assert.throws(() => {
  125. powerMonitor.querySystemIdleState(NaN, (idleState) => {
  126. })
  127. })
  128. assert.throws(() => {
  129. powerMonitor.querySystemIdleState('a', (idleState) => {
  130. })
  131. })
  132. })
  133. })
  134. describe('powerMonitor.querySystemIdleTime', () => {
  135. it('notify current system idle time', (done) => {
  136. powerMonitor.querySystemIdleTime((idleTime) => {
  137. assert.ok(idleTime >= 0)
  138. done()
  139. })
  140. })
  141. })
  142. })
  143. })