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'
|
2013-12-06 14:23:02 +00:00
|
|
|
|
2013-08-29 03:40:07 +00:00
|
|
|
BrowserWindow = remote.require 'browser-window'
|
|
|
|
|
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?
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
afterEach ->
|
|
|
|
w.destroy() if w?
|
|
|
|
w = null
|
|
|
|
|
2013-08-29 03:40:07 +00:00
|
|
|
describe 'BrowserWindow.close()', ->
|
|
|
|
it 'should emit unload handler', (done) ->
|
|
|
|
w.on 'loading-state-changed', (event, isLoading) ->
|
|
|
|
if (!isLoading)
|
|
|
|
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()
|
|
|
|
w.on 'loading-state-changed', (event, isLoading) ->
|
|
|
|
if (!isLoading)
|
|
|
|
w.close()
|
|
|
|
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)', ->
|
|
|
|
it 'should emit loading-state-changed event', (done) ->
|
|
|
|
count = 0
|
|
|
|
w.on 'loading-state-changed', (event, isLoading) ->
|
|
|
|
if count == 0
|
|
|
|
assert.equal isLoading, true
|
|
|
|
else if count == 1
|
|
|
|
assert.equal isLoading, false
|
|
|
|
done()
|
|
|
|
else
|
|
|
|
assert false
|
|
|
|
|
|
|
|
++count
|
|
|
|
w.loadUrl 'about:blank'
|
2013-09-02 08:28:36 +00:00
|
|
|
|
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) ->
|
2013-12-15 08:32:49 +00:00
|
|
|
assert.equal image.constructor.name, 'Buffer'
|
|
|
|
done()
|
2013-11-22 06:42:39 +00:00
|
|
|
|
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')
|