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