2013-08-29 11:40:07 +08:00
|
|
|
assert = require 'assert'
|
2013-12-06 22:23:02 +08:00
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
2015-04-29 19:19:31 +05:30
|
|
|
http = require 'http'
|
|
|
|
url = require 'url'
|
2015-09-14 14:15:41 +02:00
|
|
|
os = require 'os'
|
2013-12-06 22:23:02 +08:00
|
|
|
|
2015-11-12 18:28:04 +08:00
|
|
|
{remote, screen} = require 'electron'
|
|
|
|
{ipcMain, BrowserWindow} = remote.require 'electron'
|
2013-08-29 11:40:07 +08:00
|
|
|
|
2015-12-10 10:33:18 -08:00
|
|
|
isCI = remote.getGlobal('isCi')
|
2014-10-17 23:10:10 +08:00
|
|
|
|
2013-12-06 22:23:02 +08:00
|
|
|
describe 'browser-window module', ->
|
|
|
|
fixtures = path.resolve __dirname, 'fixtures'
|
2013-08-29 11:40:07 +08:00
|
|
|
|
2014-04-24 16:08:59 +08:00
|
|
|
w = null
|
|
|
|
beforeEach ->
|
|
|
|
w.destroy() if w?
|
2014-05-15 16:41:13 +08:00
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400)
|
2014-04-24 16:08:59 +08:00
|
|
|
afterEach ->
|
|
|
|
w.destroy() if w?
|
|
|
|
w = null
|
|
|
|
|
2013-08-29 11:40:07 +08:00
|
|
|
describe 'BrowserWindow.close()', ->
|
|
|
|
it 'should emit unload handler', (done) ->
|
2014-04-25 12:22:16 +08:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
|
|
|
w.close()
|
2014-04-23 08:50:33 +08:00
|
|
|
w.on 'closed', ->
|
2013-08-29 18:03:14 +08:00
|
|
|
test = path.join(fixtures, 'api', 'unload')
|
2013-08-29 11:40:07 +08:00
|
|
|
content = fs.readFileSync(test)
|
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal String(content), 'unload'
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'unload.html')
|
2013-08-29 11:40:07 +08:00
|
|
|
|
2013-09-02 16:28:36 +08:00
|
|
|
it 'should emit beforeunload handler', (done) ->
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2014-04-25 12:22:16 +08:00
|
|
|
w.webContents.on 'did-finish-load', ->
|
|
|
|
w.close()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'beforeunload-false.html')
|
2013-09-02 16:28:36 +08:00
|
|
|
|
|
|
|
describe 'window.close()', ->
|
2014-02-17 18:18:52 +08:00
|
|
|
it 'should emit unload handler', (done) ->
|
2013-09-02 16:28:36 +08:00
|
|
|
w.on 'closed', ->
|
2013-08-29 11:40:07 +08:00
|
|
|
test = path.join(fixtures, 'api', 'close')
|
|
|
|
content = fs.readFileSync(test)
|
|
|
|
fs.unlinkSync(test)
|
|
|
|
assert.equal String(content), 'close'
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close.html')
|
2013-08-29 11:47:07 +08:00
|
|
|
|
2013-09-02 16:28:36 +08:00
|
|
|
it 'should emit beforeunload handler', (done) ->
|
2013-09-02 16:47:53 +08:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html')
|
2013-09-02 16:28:36 +08:00
|
|
|
|
2015-11-13 16:03:40 +08:00
|
|
|
describe 'BrowserWindow.loadURL(url)', ->
|
2014-04-25 12:22:16 +08:00
|
|
|
it 'should emit did-start-loading event', (done) ->
|
|
|
|
w.webContents.on 'did-start-loading', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'about:blank'
|
2013-09-02 16:28:36 +08:00
|
|
|
|
2015-03-14 08:58:30 +05:30
|
|
|
it 'should emit did-fail-load event', (done) ->
|
|
|
|
w.webContents.on 'did-fail-load', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://a.txt'
|
2015-03-14 08:58:30 +05:30
|
|
|
|
2014-09-08 15:28:34 +08:00
|
|
|
describe 'BrowserWindow.show()', ->
|
2014-10-17 22:55:09 +08:00
|
|
|
it 'should focus on window', ->
|
2014-10-17 23:10:10 +08:00
|
|
|
return if isCI
|
2014-09-08 15:28:34 +08:00
|
|
|
w.show()
|
2014-10-17 22:55:09 +08:00
|
|
|
assert w.isFocused()
|
|
|
|
|
|
|
|
describe 'BrowserWindow.showInactive()', ->
|
|
|
|
it 'should not focus on window', ->
|
|
|
|
w.showInactive()
|
2014-09-08 15:28:34 +08:00
|
|
|
assert !w.isFocused()
|
|
|
|
|
2013-10-03 09:39:17 +08: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 14:42:39 +08:00
|
|
|
describe 'BrowserWindow.capturePage(rect, callback)', ->
|
2013-12-15 16:32:49 +08:00
|
|
|
it 'calls the callback with a Buffer', (done) ->
|
2013-11-22 14:42:39 +08:00
|
|
|
w.capturePage {x: 0, y: 0, width: 100, height: 100}, (image) ->
|
2015-02-12 14:37:32 +08:00
|
|
|
assert.equal image.isEmpty(), true
|
2013-12-15 16:32:49 +08:00
|
|
|
done()
|
2013-11-22 14:42:39 +08:00
|
|
|
|
2014-05-15 16:41:13 +08:00
|
|
|
describe 'BrowserWindow.setSize(width, height)', ->
|
2015-05-09 21:25:10 +05:30
|
|
|
it 'sets the window size', (done) ->
|
2015-05-28 13:45:16 +08:00
|
|
|
size = [300, 400]
|
|
|
|
w.once 'resize', ->
|
2015-05-09 23:33:16 +05:30
|
|
|
newSize = w.getSize()
|
|
|
|
assert.equal newSize[0], size[0]
|
|
|
|
assert.equal newSize[1], size[1]
|
2015-05-09 21:25:10 +05:30
|
|
|
done()
|
2014-05-15 16:41:13 +08:00
|
|
|
w.setSize size[0], size[1]
|
2015-05-09 21:25:10 +05:30
|
|
|
|
|
|
|
describe 'BrowserWindow.setPosition(x, y)', ->
|
|
|
|
it 'sets the window position', (done) ->
|
|
|
|
pos = [10, 10]
|
2015-05-28 13:45:16 +08:00
|
|
|
w.once 'move', ->
|
2015-05-09 23:33:16 +05:30
|
|
|
newPos = w.getPosition()
|
|
|
|
assert.equal newPos[0], pos[0]
|
|
|
|
assert.equal newPos[1], pos[1]
|
2015-05-09 21:25:10 +05:30
|
|
|
done()
|
|
|
|
w.setPosition pos[0], pos[1]
|
2014-05-15 16:41:13 +08:00
|
|
|
|
|
|
|
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]
|
|
|
|
|
2015-07-21 10:16:02 +08:00
|
|
|
it 'works for framless window', ->
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow(show: false, frame: false, width: 400, height: 400)
|
|
|
|
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 09:51:32 +08:00
|
|
|
describe 'BrowserWindow.fromId(id)', ->
|
|
|
|
it 'returns the window with id', ->
|
|
|
|
assert.equal w.id, BrowserWindow.fromId(w.id).id
|
|
|
|
|
2015-08-05 14:20:08 +08:00
|
|
|
describe 'BrowserWindow.setResizable(resizable)', ->
|
|
|
|
it 'does not change window size for frameless window', ->
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow(show: true, frame: false)
|
|
|
|
s = w.getSize()
|
|
|
|
w.setResizable not w.isResizable()
|
|
|
|
assert.deepEqual s, w.getSize()
|
|
|
|
|
2015-11-11 10:33:59 +08:00
|
|
|
describe '"useContentSize" option', ->
|
2014-05-15 16:41:13 +08:00
|
|
|
it 'make window created with content size when used', ->
|
|
|
|
w.destroy()
|
2015-11-11 10:33:59 +08:00
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400, useContentSize: true)
|
2014-05-15 16:41:13 +08:00
|
|
|
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 20:58:15 +08:00
|
|
|
size = w.getSize()
|
|
|
|
assert.equal size[0], 400
|
|
|
|
assert.equal size[1], 400
|
2014-05-15 16:41:13 +08:00
|
|
|
|
2015-07-21 10:16:02 +08:00
|
|
|
it 'works for framless window', ->
|
|
|
|
w.destroy()
|
2015-11-11 10:33:59 +08:00
|
|
|
w = new BrowserWindow(show: false, frame: false, width: 400, height: 400, useContentSize: true)
|
2015-07-21 10:16:02 +08:00
|
|
|
contentSize = w.getContentSize()
|
|
|
|
assert.equal contentSize[0], 400
|
|
|
|
assert.equal contentSize[1], 400
|
|
|
|
size = w.getSize()
|
|
|
|
assert.equal size[0], 400
|
|
|
|
assert.equal size[1], 400
|
|
|
|
|
2015-09-14 14:15:41 +02:00
|
|
|
describe '"title-bar-style" option', ->
|
|
|
|
return if process.platform isnt 'darwin'
|
|
|
|
return if parseInt(os.release().split('.')[0]) < 14 # only run these tests on Yosemite or newer
|
|
|
|
|
|
|
|
it 'creates browser window with hidden title bar', ->
|
|
|
|
w.destroy()
|
2015-11-11 10:33:59 +08:00
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400, titleBarStyle: 'hidden')
|
2015-09-14 14:15:41 +02:00
|
|
|
contentSize = w.getContentSize()
|
|
|
|
assert.equal contentSize[1], 400
|
|
|
|
|
|
|
|
it 'creates browser window with hidden inset title bar', ->
|
|
|
|
w.destroy()
|
2015-11-11 10:33:59 +08:00
|
|
|
w = new BrowserWindow(show: false, width: 400, height: 400, titleBarStyle: 'hidden-inset')
|
2015-09-14 14:15:41 +02:00
|
|
|
contentSize = w.getContentSize()
|
|
|
|
assert.equal contentSize[1], 400
|
|
|
|
|
2015-11-11 10:33:59 +08:00
|
|
|
describe '"enableLargerThanScreen" option', ->
|
2014-08-20 12:23:20 +08:00
|
|
|
return if process.platform is 'linux'
|
|
|
|
|
2014-08-17 12:23:00 +08:00
|
|
|
beforeEach ->
|
|
|
|
w.destroy()
|
2015-11-11 10:33:59 +08:00
|
|
|
w = new BrowserWindow(show: true, width: 400, height: 400, enableLargerThanScreen: true)
|
2014-08-17 12:23:00 +08:00
|
|
|
|
|
|
|
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', ->
|
2015-11-12 18:28:04 +08:00
|
|
|
size = screen.getPrimaryDisplay().size
|
2014-08-17 12:23:00 +08:00
|
|
|
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
|
|
|
|
|
2015-09-10 15:30:41 +08:00
|
|
|
describe '"web-preferences" option', ->
|
|
|
|
afterEach ->
|
2015-11-12 18:28:04 +08:00
|
|
|
ipcMain.removeAllListeners('answer')
|
2015-09-10 15:30:41 +08:00
|
|
|
|
|
|
|
describe '"preload" option', ->
|
|
|
|
it 'loads the script before other scripts in window', (done) ->
|
|
|
|
preload = path.join fixtures, 'module', 'set-global.js'
|
2015-11-12 18:28:04 +08:00
|
|
|
ipcMain.once 'answer', (event, test) ->
|
2015-09-10 15:30:41 +08:00
|
|
|
assert.equal(test, 'preload')
|
|
|
|
done()
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow
|
|
|
|
show: false
|
2015-11-11 10:33:59 +08:00
|
|
|
webPreferences:
|
2015-09-10 15:30:41 +08:00
|
|
|
preload: preload
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'preload.html')
|
2015-09-10 15:30:41 +08:00
|
|
|
|
|
|
|
describe '"node-integration" option', ->
|
|
|
|
it 'disables node integration when specified to false', (done) ->
|
|
|
|
preload = path.join fixtures, 'module', 'send-later.js'
|
2015-11-12 18:28:04 +08:00
|
|
|
ipcMain.once 'answer', (event, test) ->
|
2015-09-10 15:30:41 +08:00
|
|
|
assert.equal(test, 'undefined')
|
|
|
|
done()
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow
|
|
|
|
show: false
|
2015-11-11 10:33:59 +08:00
|
|
|
webPreferences:
|
2015-09-10 15:30:41 +08:00
|
|
|
preload: preload
|
2015-11-11 10:33:59 +08:00
|
|
|
nodeIntegration: false
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'blank.html')
|
2014-11-12 15:38:50 +08:00
|
|
|
|
2013-09-02 16:28:36 +08:00
|
|
|
describe 'beforeunload handler', ->
|
|
|
|
it 'returning true would not prevent close', (done) ->
|
|
|
|
w.on 'closed', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close-beforeunload-true.html')
|
2013-09-02 16:28:36 +08:00
|
|
|
|
2013-09-02 16:54:54 +08:00
|
|
|
it 'returning non-empty string would not prevent close', (done) ->
|
|
|
|
w.on 'closed', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close-beforeunload-string.html')
|
2013-09-02 16:54:54 +08:00
|
|
|
|
2013-09-02 16:28:36 +08:00
|
|
|
it 'returning false would prevent close', (done) ->
|
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html')
|
2013-09-02 16:28:36 +08:00
|
|
|
|
2013-09-02 16:54:54 +08:00
|
|
|
it 'returning empty string would prevent close', (done) ->
|
2013-09-02 16:28:36 +08:00
|
|
|
w.on 'onbeforeunload', ->
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL 'file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html')
|
2014-10-27 22:07:57 +08:00
|
|
|
|
|
|
|
describe 'new-window event', ->
|
2015-06-10 12:52:04 +08:00
|
|
|
return if isCI and process.platform is 'darwin'
|
2014-10-27 22:07:57 +08:00
|
|
|
it 'emits when window.open is called', (done) ->
|
|
|
|
w.webContents.once 'new-window', (e, url, frameName) ->
|
|
|
|
e.preventDefault()
|
2015-07-10 12:14:25 +08:00
|
|
|
assert.equal url, 'http://host/'
|
2014-10-27 22:07:57 +08:00
|
|
|
assert.equal frameName, 'host'
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL "file://#{fixtures}/pages/window-open.html"
|
2014-10-27 22:07:57 +08:00
|
|
|
|
|
|
|
it 'emits when link with target is called', (done) ->
|
2015-12-02 18:40:57 -08:00
|
|
|
@timeout 10000
|
2014-10-27 22:07:57 +08:00
|
|
|
w.webContents.once 'new-window', (e, url, frameName) ->
|
|
|
|
e.preventDefault()
|
|
|
|
assert.equal url, 'http://host/'
|
|
|
|
assert.equal frameName, 'target'
|
|
|
|
done()
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL "file://#{fixtures}/pages/target-name.html"
|
2014-11-25 14:52:49 +08:00
|
|
|
|
|
|
|
describe 'maximize event', ->
|
2015-07-03 11:40:20 +08:00
|
|
|
return if isCI
|
2014-11-25 14:52:49 +08:00
|
|
|
it 'emits when window is maximized', (done) ->
|
2014-11-25 15:06:57 +08:00
|
|
|
@timeout 10000
|
2014-11-25 14:52:49 +08:00
|
|
|
w.once 'maximize', -> done()
|
2014-11-25 15:06:57 +08:00
|
|
|
w.show()
|
2014-11-25 14:52:49 +08:00
|
|
|
w.maximize()
|
|
|
|
|
|
|
|
describe 'unmaximize event', ->
|
2015-07-03 11:40:20 +08:00
|
|
|
return if isCI
|
2014-11-25 14:52:49 +08:00
|
|
|
it 'emits when window is unmaximized', (done) ->
|
2014-11-25 15:06:57 +08:00
|
|
|
@timeout 10000
|
2014-11-25 14:52:49 +08:00
|
|
|
w.once 'unmaximize', -> done()
|
2014-11-25 15:06:57 +08:00
|
|
|
w.show()
|
|
|
|
w.maximize()
|
2014-11-25 14:52:49 +08:00
|
|
|
w.unmaximize()
|
|
|
|
|
|
|
|
describe 'minimize event', ->
|
2015-07-03 11:40:20 +08:00
|
|
|
return if isCI
|
2014-11-25 14:52:49 +08:00
|
|
|
it 'emits when window is minimized', (done) ->
|
2014-11-25 15:06:57 +08:00
|
|
|
@timeout 10000
|
2014-11-25 14:52:49 +08:00
|
|
|
w.once 'minimize', -> done()
|
2014-11-25 15:06:57 +08:00
|
|
|
w.show()
|
2014-11-25 14:52:49 +08:00
|
|
|
w.minimize()
|
2014-12-17 15:03:34 -08:00
|
|
|
|
2015-09-22 14:55:35 +08:00
|
|
|
xdescribe 'beginFrameSubscription method', ->
|
2015-09-18 18:51:49 +08:00
|
|
|
it 'subscribes frame updates', (done) ->
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL "file://#{fixtures}/api/blank.html"
|
2015-09-18 18:51:49 +08:00
|
|
|
w.webContents.beginFrameSubscription (data) ->
|
|
|
|
assert.notEqual data.length, 0
|
|
|
|
w.webContents.endFrameSubscription()
|
|
|
|
done()
|
2015-10-14 12:41:31 +08:00
|
|
|
|
|
|
|
describe 'save page', ->
|
2015-10-18 11:28:05 +08:00
|
|
|
savePageDir = path.join fixtures, 'save_page'
|
|
|
|
savePageHtmlPath = path.join savePageDir, 'save_page.html'
|
|
|
|
savePageJsPath = path.join savePageDir, 'save_page_files', 'test.js'
|
|
|
|
savePageCssPath = path.join savePageDir, 'save_page_files', 'test.css'
|
2015-10-14 12:41:31 +08:00
|
|
|
it 'should save page', (done) ->
|
|
|
|
w.webContents.on 'did-finish-load', ->
|
2015-10-18 11:28:05 +08:00
|
|
|
w.webContents.savePage savePageHtmlPath, 'HTMLComplete', (error) ->
|
2015-10-14 12:41:31 +08:00
|
|
|
assert.equal error, null
|
2015-10-18 11:28:05 +08:00
|
|
|
assert fs.existsSync savePageHtmlPath
|
|
|
|
assert fs.existsSync savePageJsPath
|
|
|
|
assert fs.existsSync savePageCssPath
|
|
|
|
fs.unlinkSync savePageCssPath
|
|
|
|
fs.unlinkSync savePageJsPath
|
|
|
|
fs.unlinkSync savePageHtmlPath
|
|
|
|
fs.rmdirSync path.join savePageDir, 'save_page_files'
|
|
|
|
fs.rmdirSync savePageDir
|
2015-10-14 12:41:31 +08:00
|
|
|
done()
|
|
|
|
|
2015-11-13 16:03:40 +08:00
|
|
|
w.loadURL "file://#{fixtures}/pages/save_page/index.html"
|
2015-11-21 06:58:17 +02:00
|
|
|
|
|
|
|
describe 'BrowserWindow options argument is optional', ->
|
|
|
|
it 'should create a window with default size (800x600)', ->
|
|
|
|
w.destroy()
|
|
|
|
w = new BrowserWindow()
|
|
|
|
size = w.getSize()
|
|
|
|
assert.equal size[0], 800
|
|
|
|
assert.equal size[1], 600
|