2013-07-23 03:59:07 +00:00
|
|
|
assert = require 'assert'
|
2013-12-06 14:23:02 +00:00
|
|
|
path = require 'path'
|
2013-09-26 03:27:09 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
{ipcRenderer, remote} = require 'electron'
|
|
|
|
{ipcMain, BrowserWindow} = remote.require 'electron'
|
2013-07-23 03:59:07 +00:00
|
|
|
|
2015-08-22 16:50:54 +00:00
|
|
|
comparePaths = (path1, path2) ->
|
|
|
|
if process.platform is 'win32'
|
|
|
|
# Paths in Windows are case insensitive.
|
|
|
|
path1 = path1.toLowerCase()
|
|
|
|
path2 = path2.toLowerCase()
|
|
|
|
assert.equal path1, path2
|
|
|
|
|
2013-12-06 14:23:02 +00:00
|
|
|
describe 'ipc module', ->
|
|
|
|
fixtures = path.join __dirname, 'fixtures'
|
2013-08-14 05:27:50 +00:00
|
|
|
|
2013-07-23 03:59:07 +00:00
|
|
|
describe 'remote.require', ->
|
|
|
|
it 'should returns same object for the same module', ->
|
2015-11-12 10:28:04 +00:00
|
|
|
dialog1 = remote.require 'electron'
|
|
|
|
dialog2 = remote.require 'electron'
|
2013-07-23 03:59:07 +00:00
|
|
|
assert.equal dialog1, dialog2
|
2013-07-26 07:53:00 +00:00
|
|
|
|
|
|
|
it 'should work when object contains id property', ->
|
2013-08-14 05:27:50 +00:00
|
|
|
a = remote.require path.join(fixtures, 'module', 'id.js')
|
2013-07-26 07:53:00 +00:00
|
|
|
assert.equal a.id, 1127
|
|
|
|
|
2014-06-22 06:55:41 +00:00
|
|
|
it 'should search module from the user app', ->
|
2015-08-22 16:50:54 +00:00
|
|
|
comparePaths path.normalize(remote.process.mainModule.filename), path.resolve(__dirname, 'static', 'main.js')
|
|
|
|
comparePaths path.normalize(remote.process.mainModule.paths[0]), path.resolve(__dirname, 'static', 'node_modules')
|
2014-06-22 06:55:41 +00:00
|
|
|
|
2013-08-25 09:22:36 +00:00
|
|
|
describe 'remote.createFunctionWithReturnValue', ->
|
|
|
|
it 'should be called in browser synchronously', ->
|
|
|
|
buf = new Buffer('test')
|
|
|
|
call = remote.require path.join(fixtures, 'module', 'call.js')
|
|
|
|
result = call.call remote.createFunctionWithReturnValue(buf)
|
|
|
|
assert.equal result.constructor.name, 'Buffer'
|
|
|
|
|
|
|
|
describe 'remote object in renderer', ->
|
2013-08-14 05:27:50 +00:00
|
|
|
it 'can change its properties', ->
|
|
|
|
property = remote.require path.join(fixtures, 'module', 'property.js')
|
|
|
|
assert.equal property.property, 1127
|
|
|
|
property.property = 1007
|
|
|
|
assert.equal property.property, 1007
|
|
|
|
property2 = remote.require path.join(fixtures, 'module', 'property.js')
|
|
|
|
assert.equal property2.property, 1007
|
|
|
|
|
2013-08-14 13:19:26 +00:00
|
|
|
# Restore.
|
|
|
|
property.property = 1127
|
|
|
|
|
2013-08-25 12:44:52 +00:00
|
|
|
it 'can construct an object from its member', ->
|
|
|
|
call = remote.require path.join(fixtures, 'module', 'call.js')
|
|
|
|
obj = new call.constructor
|
|
|
|
assert.equal obj.test, 'test'
|
|
|
|
|
2013-08-25 09:22:36 +00:00
|
|
|
describe 'remote value in browser', ->
|
|
|
|
it 'keeps its constructor name for objects', ->
|
|
|
|
buf = new Buffer('test')
|
|
|
|
print_name = remote.require path.join(fixtures, 'module', 'print_name.js')
|
|
|
|
assert.equal print_name.print(buf), 'Buffer'
|
|
|
|
|
2015-07-31 05:45:53 +00:00
|
|
|
describe 'remote promise', ->
|
|
|
|
it 'can be used as promise in each side', (done) ->
|
2015-07-31 05:32:07 +00:00
|
|
|
promise = remote.require path.join(fixtures, 'module', 'promise.js')
|
2015-07-31 05:45:53 +00:00
|
|
|
promise.twicePromise(Promise.resolve(1234))
|
2015-07-31 05:32:07 +00:00
|
|
|
.then (value) =>
|
|
|
|
assert.equal value, 2468
|
|
|
|
done()
|
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
describe 'ipc.sender.send', ->
|
2013-07-26 07:53:00 +00:00
|
|
|
it 'should work when sending an object containing id property', (done) ->
|
|
|
|
obj = id: 1, name: 'ly'
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.once 'message', (event, message) ->
|
2013-07-26 07:53:00 +00:00
|
|
|
assert.deepEqual message, obj
|
|
|
|
done()
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcRenderer.send 'message', obj
|
2013-09-22 04:06:41 +00:00
|
|
|
|
|
|
|
describe 'ipc.sendSync', ->
|
|
|
|
it 'can be replied by setting event.returnValue', ->
|
2015-11-12 10:28:04 +00:00
|
|
|
msg = ipcRenderer.sendSync 'echo', 'test'
|
2013-09-22 04:06:41 +00:00
|
|
|
assert.equal msg, 'test'
|
2013-09-26 03:27:09 +00:00
|
|
|
|
2014-04-17 09:14:05 +00:00
|
|
|
it 'does not crash when reply is not sent and browser is destroyed', (done) ->
|
2015-04-25 02:45:16 +00:00
|
|
|
@timeout 10000
|
2013-09-26 03:27:09 +00:00
|
|
|
w = new BrowserWindow(show: false)
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.once 'send-sync-message', (event) ->
|
2013-09-26 03:27:09 +00:00
|
|
|
event.returnValue = null
|
|
|
|
w.destroy()
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'send-sync-message.html')
|
2015-10-27 22:15:46 +00:00
|
|
|
|
|
|
|
describe 'remote listeners', ->
|
|
|
|
it 'can be added and removed correctly', ->
|
|
|
|
count = 0
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
listener = () ->
|
|
|
|
count += 1
|
|
|
|
w.removeListener 'blur', listener
|
|
|
|
w.on 'blur', listener
|
|
|
|
w.emit 'blur'
|
|
|
|
w.emit 'blur'
|
|
|
|
assert.equal count, 1
|
|
|
|
w.destroy()
|