2013-08-29 03:40:07 +00:00
|
|
|
assert = require 'assert'
|
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
|
|
|
remote = require 'remote'
|
|
|
|
BrowserWindow = remote.require 'browser-window'
|
|
|
|
|
|
|
|
fixtures = path.resolve __dirname, '..', 'fixtures'
|
|
|
|
|
|
|
|
describe 'window module', ->
|
|
|
|
describe 'BrowserWindow.close()', ->
|
|
|
|
it 'should emit unload handler', (done) ->
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
w.on 'loading-state-changed', (event, isLoading) ->
|
|
|
|
if (!isLoading)
|
|
|
|
w.close()
|
|
|
|
w.on 'destroyed', ->
|
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) ->
|
2013-08-29 03:40:07 +00:00
|
|
|
w = new BrowserWindow(show: false)
|
2013-09-02 08:28:36 +00:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
w.destroy()
|
|
|
|
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()', ->
|
|
|
|
xit 'should emit unload handler', (done) ->
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
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) ->
|
|
|
|
w = new BrowserWindow(show: false)
|
2013-09-02 08:47:53 +00:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
w.destroy()
|
|
|
|
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) ->
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
count = 0
|
|
|
|
w.on 'loading-state-changed', (event, isLoading) ->
|
|
|
|
if count == 0
|
|
|
|
assert.equal isLoading, true
|
|
|
|
else if count == 1
|
|
|
|
assert.equal isLoading, false
|
2013-08-29 10:03:14 +00:00
|
|
|
w.close()
|
2013-08-29 03:47:07 +00:00
|
|
|
done()
|
|
|
|
else
|
2013-08-29 10:03:14 +00:00
|
|
|
w.close()
|
2013-08-29 03:47:07 +00:00
|
|
|
assert false
|
|
|
|
|
|
|
|
++count
|
|
|
|
w.loadUrl 'about:blank'
|
2013-09-02 08:28:36 +00:00
|
|
|
|
|
|
|
describe 'beforeunload handler', ->
|
|
|
|
it 'returning true would not prevent close', (done) ->
|
|
|
|
w = new BrowserWindow(show: false)
|
|
|
|
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 = new BrowserWindow(show: false)
|
|
|
|
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 = new BrowserWindow(show: false)
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
w.destroy()
|
|
|
|
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 = new BrowserWindow(show: false)
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
w.destroy()
|
|
|
|
done()
|
2013-09-02 08:54:54 +00:00
|
|
|
w.loadUrl 'file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html')
|