2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// For these tests we use a fake DBus daemon to verify libnotify interaction
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// with the session bus. This requires python-dbusmock to be installed and
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// running at $DBUS_SESSION_BUS_ADDRESS.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								//
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-27 15:49:02 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								// script/spec-runner.js spawns dbusmock, which sets DBUS_SESSION_BUS_ADDRESS.
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								//
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// See https://pypi.python.org/pypi/python-dbusmock to read about dbusmock.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const { expect } = require('chai')
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const dbus = require('dbus-native')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const Promise = require('bluebird')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const { remote } = require('electron')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const { app } = remote.require('electron')
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const skip = process.platform !== 'linux' ||
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								             process.arch === 'ia32' ||
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-04 12:01:16 -04:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								             process.arch.indexOf('arm') === 0 ||
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								             !process.env.DBUS_SESSION_BUS_ADDRESS;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								(skip ? describe.skip : describe)('Notification module (dbus)', () => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  let mock, Notification, getCalls, reset
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  const realAppName = app.getName()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  const realAppVersion = app.getVersion()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  const appName = 'api-notification-dbus-spec'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  const serviceName = 'org.freedesktop.Notifications'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  before(async () => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // init app
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    app.setName(appName)
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    app.setDesktopName(`${appName}.desktop`)
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // init dbus
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const path = '/org/freedesktop/Notifications'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const iface = 'org.freedesktop.DBus.Mock'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const bus = dbus.sessionBus()
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    console.log(`session bus: ${process.env.DBUS_SESSION_BUS_ADDRESS}`)
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    const service = bus.getService(serviceName)
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    const getInterface = Promise.promisify(service.getInterface, { context: service })
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    mock = await getInterface(path, iface)
							 | 
						
					
						
							
								
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    getCalls = Promise.promisify(mock.GetCalls, { context: mock })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    reset = Promise.promisify(mock.Reset, { context: mock })
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  after(async () => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // cleanup dbus
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    await reset()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    // cleanup app
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    app.setName(realAppName)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    app.setVersion(realAppVersion)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  describe(`Notification module using ${serviceName}`, () => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    function onMethodCalled (done) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      function cb (name) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        console.log(`onMethodCalled: ${name}`)
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        if (name === 'Notify') {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          mock.removeListener('MethodCalled', cb)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          console.log('done')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          done()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return cb
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    function unmarshalDBusNotifyHints (dbusHints) {
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-02 03:56:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const o = {}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      for (const hint of dbusHints) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        const key = hint[0]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        const value = hint[1][1][0]
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        o[key] = value
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return o
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    function unmarshalDBusNotifyArgs (dbusArgs) {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      return {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        app_name: dbusArgs[0][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        replaces_id: dbusArgs[1][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        app_icon: dbusArgs[2][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        title: dbusArgs[3][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        body: dbusArgs[4][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        actions: dbusArgs[5][1][0],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        hints: unmarshalDBusNotifyHints(dbusArgs[6][1][0])
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    before(done => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      mock.on('MethodCalled', onMethodCalled(done))
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      // lazy load Notification after we listen to MethodCalled mock signal
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      Notification = require('electron').remote.Notification
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const n = new Notification({
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        title: 'title',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        subtitle: 'subtitle',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        body: 'body',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        replyPlaceholder: 'replyPlaceholder',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        sound: 'sound',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        closeButtonText: 'closeButtonText'
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      n.show()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    it(`should call ${serviceName} to show notifications`, async () => {
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const calls = await getCalls()
							 | 
						
					
						
							
								
									
										
										
										
											2018-07-10 14:20:03 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      expect(calls).to.be.an('array').of.lengthOf.at.least(1)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-02 03:56:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const lastCall = calls[calls.length - 1]
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      const methodName = lastCall[1]
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      expect(methodName).to.equal('Notify')
							 | 
						
					
						
							
								
									
										
										
										
											2018-07-10 14:20:03 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2018-10-02 03:56:31 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      const args = unmarshalDBusNotifyArgs(lastCall[2])
							 | 
						
					
						
							
								
									
										
										
										
											2018-06-28 15:40:30 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      expect(args).to.deep.equal({
							 | 
						
					
						
							
								
									
										
										
										
											2018-03-12 09:33:06 +09:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        app_name: appName,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        replaces_id: 0,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        app_icon: '',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        title: 'title',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        body: 'body',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        actions: [],
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        hints: {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          'append': 'true',
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								          'desktop-entry': appName
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  })
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								})
							 |