2013-08-29 03:40:07 +00:00
|
|
|
assert = require 'assert'
|
2013-12-06 14:23:02 +00:00
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
2013-08-29 03:40:07 +00:00
|
|
|
remote = require 'remote'
|
2015-04-29 13:49:31 +00:00
|
|
|
http = require 'http'
|
|
|
|
url = require 'url'
|
2013-12-06 14:23:02 +00:00
|
|
|
|
2013-08-29 03:40:07 +00:00
|
|
|
BrowserWindow = remote.require 'browser-window'
|
|
|
|
|
2015-03-17 12:55:53 +00:00
|
|
|
isCI = remote.process.argv[2] == '--ci'
|
2014-10-17 15:10:10 +00:00
|
|
|
|
2013-12-06 14:23:02 +00:00
|
|
|
describe 'browser-window module', ->
|
|
|
|
fixtures = path.resolve __dirname, 'fixtures'
|
2013-08-29 03:40:07 +00:00
|
|
|
|
2014-04-24 08:08:59 +00:00
|
|
|
w = null
|
|
|
|
beforeEach ->
|
|
|
|
w.destroy() if w?
|
2014-05-15 08:41:13 +00:00
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400)
|
2014-04-24 08:08:59 +00:00
|
|
|
afterEach ->
|
|
|
|
w.destroy() if w?
|
|
|
|
w = null
|
|
|
|
|
2013-08-29 03:40:07 +00:00
|
|
|
describe 'BrowserWindow.close()', ->
|
|
|
|
it 'should emit unload handler', (done) ->
|
2014-04-25 04:22:16 +00:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
|
|
|
w.close()
|
2014-04-23 00:50:33 +00:00
|
|
|
w.on 'closed', ->
|
2013-08-29 10:03:14 +00:00
|
|
|
test = path.join(fixtures, 'api', 'unload')
|
2013-08-29 03:40:07 +00:00
|
|
|
content = fs.readFileSync(test)
|
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal String(content), 'unload'
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'unload.html')
|
|
|
|
|
2013-09-02 08:28:36 +00:00
|
|
|
it 'should emit beforeunload handler', (done) ->
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2014-04-25 04:22:16 +00:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
|
|
|
w.close()
|
2013-09-02 08:28:36 +00:00
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'beforeunload-false.html')
|
|
|
|
|
|
|
|
describe 'window.close()', ->
|
2014-02-17 10:18:52 +00:00
|
|
|
it 'should emit unload handler', (done) ->
|
2013-09-02 08:28:36 +00:00
|
|
|
w.on 'closed', ->
|
2013-08-29 03:40:07 +00:00
|
|
|
test = path.join(fixtures, 'api', 'close')
|
|
|
|
content = fs.readFileSync(test)
|
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal String(content), 'close'
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close.html')
|
2013-08-29 03:47:07 +00:00
|
|
|
|
2013-09-02 08:28:36 +00:00
|
|
|
it 'should emit beforeunload handler', (done) ->
|
2013-09-02 08:47:53 +00:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html')
|
2013-09-02 08:28:36 +00:00
|
|
|
|
2013-08-29 03:47:07 +00:00
|
|
|
describe 'BrowserWindow.loadUrl(url)', ->
|
2014-04-25 04:22:16 +00:00
|
|
|
it 'should emit did-start-loading event', (done) ->
|
|
|
|
w.webContents.on 'did-start-loading', ->
|
|
|
|
done()
|
2013-08-29 03:47:07 +00:00
|
|
|
w.loadUrl 'about:blank'
|
2013-09-02 08:28:36 +00:00
|
|
|
|
2015-03-14 03:28:30 +00:00
|
|
|
it 'should emit did-fail-load event', (done) ->
|
|
|
|
w.webContents.on 'did-fail-load', ->
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://a.txt'
|
|
|
|
|
2014-09-08 07:28:34 +00:00
|
|
|
describe 'BrowserWindow.show()', ->
|
2014-10-17 14:55:09 +00:00
|
|
|
it 'should focus on window', ->
|
2014-10-17 15:10:10 +00:00
|
|
|
return if isCI
|
2014-09-08 07:28:34 +00:00
|
|
|
w.show()
|
2014-10-17 14:55:09 +00:00
|
|
|
assert w.isFocused()
|
|
|
|
|
|
|
|
describe 'BrowserWindow.showInactive()', ->
|
|
|
|
it 'should not focus on window', ->
|
|
|
|
w.showInactive()
|
2014-09-08 07:28:34 +00:00
|
|
|
assert !w.isFocused()
|
|
|
|
|
2013-10-03 01:39:17 +00:00
|
|
|
describe 'BrowserWindow.focus()', ->
|
|
|
|
it 'does not make the window become visible', ->
|
|
|
|
assert.equal w.isVisible(), false
|
|
|
|
w.focus()
|
|
|
|
assert.equal w.isVisible(), false
|
|
|
|
|
2013-11-22 06:42:39 +00:00
|
|
|
describe 'BrowserWindow.capturePage(rect, callback)', ->
|
2013-12-15 08:32:49 +00:00
|
|
|
it 'calls the callback with a Buffer', (done) ->
|
2013-11-22 06:42:39 +00:00
|
|
|
w.capturePage {x: 0, y: 0, width: 100, height: 100}, (image) ->
|
2015-02-12 06:37:32 +00:00
|
|
|
assert.equal image.isEmpty(), true
|
2013-12-15 08:32:49 +00:00
|
|
|
done()
|
2013-11-22 06:42:39 +00:00
|
|
|
|
2014-05-15 08:41:13 +00:00
|
|
|
describe 'BrowserWindow.setSize(width, height)', ->
|
|
|
|
it 'sets the window size', ->
|
|
|
|
size = [400, 400]
|
|
|
|
w.setSize size[0], size[1]
|
|
|
|
after = w.getSize()
|
|
|
|
assert.equal after[0], size[0]
|
|
|
|
assert.equal after[1], size[1]
|
|
|
|
|
|
|
|
describe 'BrowserWindow.setContentSize(width, height)', ->
|
|
|
|
it 'sets the content size', ->
|
|
|
|
size = [400, 400]
|
|
|
|
w.setContentSize size[0], size[1]
|
|
|
|
after = w.getContentSize()
|
|
|
|
assert.equal after[0], size[0]
|
|
|
|
assert.equal after[1], size[1]
|
|
|
|
|
2014-05-22 01:51:32 +00:00
|
|
|
describe 'BrowserWindow.fromId(id)', ->
|
|
|
|
it 'returns the window with id', ->
|
|
|
|
assert.equal w.id, BrowserWindow.fromId(w.id).id
|
|
|
|
|
2014-05-15 08:41:13 +00:00
|
|
|
describe '"use-content-size" option', ->
|
|
|
|
it 'make window created with content size when used', ->
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400, 'use-content-size': true)
|
|
|
|
contentSize = w.getContentSize()
|
|
|
|
assert.equal contentSize[0], 400
|
|
|
|
assert.equal contentSize[1], 400
|
|
|
|
|
|
|
|
it 'make window created with window size when not used', ->
|
2014-07-21 12:58:15 +00:00
|
|
|
size = w.getSize()
|
|
|
|
assert.equal size[0], 400
|
|
|
|
assert.equal size[1], 400
|
2014-05-15 08:41:13 +00:00
|
|
|
|
2014-08-17 04:23:00 +00:00
|
|
|
describe '"enable-larger-than-screen" option', ->
|
2014-08-20 04:23:20 +00:00
|
|
|
return if process.platform is 'linux'
|
|
|
|
|
2014-08-17 04:23:00 +00:00
|
|
|
beforeEach ->
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow(show: true, width: 400, height: 400, 'enable-larger-than-screen': true)
|
|
|
|
|
|
|
|
it 'can move the window out of screen', ->
|
|
|
|
w.setPosition -10, -10
|
|
|
|
after = w.getPosition()
|
|
|
|
assert.equal after[0], -10
|
|
|
|
assert.equal after[1], -10
|
|
|
|
|
|
|
|
it 'can set the window larger than screen', ->
|
|
|
|
size = require('screen').getPrimaryDisplay().size
|
|
|
|
size.width += 100
|
|
|
|
size.height += 100
|
|
|
|
w.setSize size.width, size.height
|
|
|
|
after = w.getSize()
|
|
|
|
assert.equal after[0], size.width
|
|
|
|
assert.equal after[1], size.height
|
|
|
|
|
2014-11-12 07:38:50 +00:00
|
|
|
describe '"preload" options', ->
|
|
|
|
it 'loads the script before other scripts in window', (done) ->
|
|
|
|
preload = path.join fixtures, 'module', 'set-global.js'
|
|
|
|
remote.require('ipc').once 'preload', (event, test) ->
|
|
|
|
assert.equal(test, 'preload')
|
|
|
|
done()
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400, preload: preload)
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'preload.html')
|
|
|
|
|
2013-09-02 08:28:36 +00:00
|
|
|
describe 'beforeunload handler', ->
|
|
|
|
it 'returning true would not prevent close', (done) ->
|
|
|
|
w.on 'closed', ->
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-true.html')
|
|
|
|
|
2013-09-02 08:54:54 +00:00
|
|
|
it 'returning non-empty string would not prevent close', (done) ->
|
|
|
|
w.on 'closed', ->
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-string.html')
|
|
|
|
|
2013-09-02 08:28:36 +00:00
|
|
|
it 'returning false would prevent close', (done) ->
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html')
|
|
|
|
|
2013-09-02 08:54:54 +00:00
|
|
|
it 'returning empty string would prevent close', (done) ->
|
2013-09-02 08:28:36 +00:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2013-09-02 08:54:54 +00:00
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html')
|
2014-10-27 14:07:57 +00:00
|
|
|
|
|
|
|
describe 'new-window event', ->
|
|
|
|
it 'emits when window.open is called', (done) ->
|
|
|
|
w.webContents.once 'new-window', (e, url, frameName) ->
|
|
|
|
e.preventDefault()
|
|
|
|
assert.equal url, 'http://host'
|
|
|
|
assert.equal frameName, 'host'
|
|
|
|
done()
|
|
|
|
w.loadUrl "file://#{fixtures}/pages/window-open.html"
|
|
|
|
|
|
|
|
it 'emits when link with target is called', (done) ->
|
|
|
|
w.webContents.once 'new-window', (e, url, frameName) ->
|
|
|
|
e.preventDefault()
|
|
|
|
assert.equal url, 'http://host/'
|
|
|
|
assert.equal frameName, 'target'
|
|
|
|
done()
|
|
|
|
w.loadUrl "file://#{fixtures}/pages/target-name.html"
|
2014-11-25 06:52:49 +00:00
|
|
|
|
|
|
|
describe 'maximize event', ->
|
2014-11-25 07:17:48 +00:00
|
|
|
return if isCI and process.platform is 'linux'
|
2014-11-25 06:52:49 +00:00
|
|
|
it 'emits when window is maximized', (done) ->
|
2014-11-25 07:06:57 +00:00
|
|
|
@timeout 10000
|
2014-11-25 06:52:49 +00:00
|
|
|
w.once 'maximize', -> done()
|
2014-11-25 07:06:57 +00:00
|
|
|
w.show()
|
2014-11-25 06:52:49 +00:00
|
|
|
w.maximize()
|
|
|
|
|
|
|
|
describe 'unmaximize event', ->
|
2014-11-25 07:17:48 +00:00
|
|
|
return if isCI and process.platform is 'linux'
|
2014-11-25 06:52:49 +00:00
|
|
|
it 'emits when window is unmaximized', (done) ->
|
2014-11-25 07:06:57 +00:00
|
|
|
@timeout 10000
|
2014-11-25 06:52:49 +00:00
|
|
|
w.once 'unmaximize', -> done()
|
2014-11-25 07:06:57 +00:00
|
|
|
w.show()
|
|
|
|
w.maximize()
|
2014-11-25 06:52:49 +00:00
|
|
|
w.unmaximize()
|
|
|
|
|
|
|
|
describe 'minimize event', ->
|
2014-11-25 07:17:48 +00:00
|
|
|
return if isCI and process.platform is 'linux'
|
2014-11-25 06:52:49 +00:00
|
|
|
it 'emits when window is minimized', (done) ->
|
2014-11-25 07:06:57 +00:00
|
|
|
@timeout 10000
|
2014-11-25 06:52:49 +00:00
|
|
|
w.once 'minimize', -> done()
|
2014-11-25 07:06:57 +00:00
|
|
|
w.show()
|
2014-11-25 06:52:49 +00:00
|
|
|
w.minimize()
|
2014-12-17 23:03:34 +00:00
|
|
|
|
|
|
|
describe 'will-navigate event', ->
|
|
|
|
it 'emits when user starts a navigation', (done) ->
|
2015-04-25 02:45:16 +00:00
|
|
|
@timeout 10000
|
2014-12-17 23:03:34 +00:00
|
|
|
w.webContents.on 'will-navigate', (event, url) ->
|
|
|
|
event.preventDefault()
|
|
|
|
assert.equal url, 'https://www.github.com/'
|
|
|
|
done()
|
|
|
|
w.loadUrl "file://#{fixtures}/pages/will-navigate.html"
|
2015-04-29 13:49:31 +00:00
|
|
|
|
|
|
|
describe 'dom-ready event', ->
|
|
|
|
it 'emits when document is loaded', (done) ->
|
|
|
|
server = http.createServer (req, res) ->
|
|
|
|
action = url.parse(req.url, true).pathname
|
|
|
|
if action == '/logo.png'
|
|
|
|
img = fs.readFileSync(path.join(fixtures, 'assets', 'logo.png'))
|
|
|
|
res.writeHead(200, {'Content-Type': 'image/png'})
|
|
|
|
setTimeout ->
|
|
|
|
res.end(img, 'binary')
|
|
|
|
, 2000
|
|
|
|
server.close()
|
|
|
|
server.listen 62542, '127.0.0.1'
|
|
|
|
remote.require('ipc').on 'dom-ready', (e, state) ->
|
|
|
|
assert.equal state, 'interactive'
|
|
|
|
done()
|
|
|
|
w.webContents.on 'did-finish-load', ->
|
|
|
|
w.close()
|
|
|
|
w.loadUrl "file://#{fixtures}/pages/f.html"
|