Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
electron-window: refactored, added tests
- Loading branch information
1 parent
0b43690
commit dea581a
Showing
6 changed files
with
147 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var assert = require('assert') | ||
var args = require('../args') | ||
|
||
/* global describe it */ | ||
|
||
describe('args', function () { | ||
describe('encode()', function () { | ||
it('should hash encode object', function () { | ||
var obj = {name: 'jp'} | ||
var str = args.encode(obj) | ||
assert.strictEqual(str, '%7B%22name%22%3A%22jp%22%7D') | ||
|
||
str = args.encode(null) | ||
assert.strictEqual(str, '') | ||
}) | ||
}) | ||
|
||
describe('urlWithArgs()', function () { | ||
describe('> when url', function () { | ||
it('should create a url with encoded args in hash', function () { | ||
var url = args.urlWithArgs('http://google.com', {doThat: 'ok'}) | ||
assert.strictEqual(url, 'http://google.com/#%7B%22doThat%22%3A%22ok%22%7D') | ||
|
||
url = args.urlWithArgs('http://google.com') | ||
assert.strictEqual(url, 'http://google.com/') | ||
}) | ||
}) | ||
|
||
describe('> when file', function () { | ||
it('should create a url with encoded args in hash', function () { | ||
var url = args.urlWithArgs('/tmp/index.html', {doThat: 'ok'}) | ||
assert.strictEqual(url, 'file:///tmp/index.html#%7B%22doThat%22%3A%22ok%22%7D') | ||
|
||
url = args.urlWithArgs('/tmp/index.html') | ||
assert.strictEqual(url, 'file:///tmp/index.html') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var assert = require('assert') | ||
var proxyquire = require('proxyquire') | ||
// var stubo = require('stubo') | ||
|
||
/* global describe it */ | ||
|
||
describe('main', function () { | ||
describe('_createWindow()', function () { | ||
it('should should create the window with options and set global window references', function () { | ||
function BrowserWindow (options) { this.id = 5; this._captureOptions = options } | ||
var stubs = { | ||
'browser-window': BrowserWindow | ||
} | ||
stubs['browser-window']['@noCallThru'] = true | ||
var main = proxyquire('../main', stubs) | ||
|
||
var win = main._createWindow({frame: 'something'}) | ||
assert(win instanceof BrowserWindow) | ||
assert.strictEqual(main.windows['5'], win) | ||
assert.strictEqual(win._captureOptions.frame, 'something') | ||
}) | ||
}) | ||
|
||
describe('_unref()', function () { | ||
it('should delete global reference', function () { | ||
var stubs = {'browser-window': {}} | ||
stubs['browser-window']['@noCallThru'] = true | ||
var main = proxyquire('../main', stubs) | ||
|
||
var win = {id: 3} | ||
main.windows[win.id] = win | ||
assert.strictEqual(main.windows[3], win) | ||
assert.strictEqual(Object.keys(main.windows).length, 1) | ||
|
||
main._unref.call(win) | ||
assert.strictEqual(main.windows[3], undefined) | ||
assert.strictEqual(Object.keys(main.windows).length, 0) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var assert = require('assert') | ||
var assign = require('object-assign') | ||
var path = require('path') | ||
var url = require('url') | ||
|
||
function encode (args) { | ||
args = args || null | ||
assert.strictEqual(typeof args, 'object', 'args must be an object') | ||
// stringify the args | ||
args = args ? encodeURIComponent(JSON.stringify(args)) : '' | ||
return args | ||
} | ||
|
||
function urlWithArgs (urlOrFile, args) { | ||
args = encode(args) | ||
|
||
var u | ||
if (urlOrFile.indexOf('http') === 0) { | ||
var urlData = url.parse(urlOrFile) | ||
var hash = urlData.hash || args ? args : undefined | ||
u = url.format(assign(urlData, { hash: hash })) | ||
} else { // presumably a file url | ||
u = url.format({ | ||
protocol: 'file', | ||
pathname: path.resolve(urlOrFile), | ||
slashes: true, | ||
hash: args ? args : undefined | ||
}) | ||
} | ||
|
||
return u | ||
} | ||
|
||
module.exports = { | ||
encode: encode, | ||
urlWithArgs: urlWithArgs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,56 @@ | ||
var assert = require('assert') | ||
var assign = require('object-assign') | ||
var path = require('path') | ||
var url = require('url') | ||
var BrowserWindow = require('browser-window') | ||
var wargs = require('./args') | ||
|
||
// retain global references, if not, window will be closed automatically when | ||
// garbage collected | ||
var _windows = {} | ||
|
||
function createWindow (options) { | ||
var window = new BrowserWindow(extend({ | ||
function _createWindow (options) { | ||
var opts = assign({ | ||
show: false, | ||
preload: path.join(__dirname, 'renderer-preload') | ||
}, options)) | ||
}, options) | ||
|
||
var window = new BrowserWindow(opts) | ||
_windows[window.id] = window | ||
|
||
// should not need to be called, but just in case | ||
// window.destroy() is ever called | ||
window.unref = function () { | ||
delete _windows[window.id] | ||
} | ||
return window | ||
} | ||
|
||
window.once('closed', function () { | ||
window.unref() | ||
}) | ||
// should not need to be called directly, but just in case | ||
// window.destroy() is ever called | ||
function _unref () { | ||
delete _windows[this.id] | ||
} | ||
|
||
function createWindow (options) { | ||
var window = _createWindow(options) | ||
window.unref = _unref.bind(window) | ||
window.once('closed', window.unref) | ||
|
||
window.showUrl = function (httpOrFileUrl, args, callback) { | ||
if (typeof args === 'function') { | ||
callback = args | ||
args = null | ||
} | ||
assert.strictEqual(typeof args, 'object', 'args must be an object') | ||
// stringify the args | ||
args = args ? encodeURIComponent(JSON.stringify(args)) : '' | ||
|
||
window.webContents.once('did-finish-load', function () { | ||
window.show() | ||
callback.apply(this, arguments) | ||
}) | ||
|
||
if (httpOrFileUrl.indexOf('http') === 0) { | ||
var urlData = url.parse(httpOrFileUrl) | ||
var httpUrl = url.format(extend(urlData, { | ||
hash: urlData.hash || args ? args : undefined | ||
})) | ||
window.loadUrl(httpUrl) | ||
} else { // presumably a file url | ||
var fileUrl = url.format({ | ||
protocol: 'file', | ||
pathname: path.resolve(httpOrFileUrl), | ||
slashes: true, | ||
hash: args ? args : undefined | ||
}) | ||
|
||
window.loadUrl(fileUrl) | ||
} | ||
var url = wargs.urlWithArgs(httpOrFileUrl, args) | ||
window.loadUrl(url) | ||
} | ||
|
||
return window | ||
} | ||
|
||
function extend (target, source) { | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key] | ||
}) | ||
return target | ||
} | ||
|
||
module.exports = { | ||
createWindow: createWindow, | ||
windows: _windows | ||
windows: _windows, | ||
_createWindow: _createWindow, | ||
_unref: _unref | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters