api-app-spec.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. const assert = require('assert')
  2. const ChildProcess = require('child_process')
  3. const https = require('https')
  4. const net = require('net')
  5. const fs = require('fs')
  6. const path = require('path')
  7. const {remote} = require('electron')
  8. const {closeWindow} = require('./window-helpers')
  9. const {app, BrowserWindow, ipcMain} = remote
  10. describe('electron module', function () {
  11. it('does not expose internal modules to require', function () {
  12. assert.throws(function () {
  13. require('clipboard')
  14. }, /Cannot find module 'clipboard'/)
  15. })
  16. describe('require("electron")', function () {
  17. let window = null
  18. beforeEach(function () {
  19. window = new BrowserWindow({
  20. show: false,
  21. width: 400,
  22. height: 400
  23. })
  24. })
  25. afterEach(function () {
  26. return closeWindow(window).then(function () { window = null })
  27. })
  28. it('always returns the internal electron module', function (done) {
  29. ipcMain.once('answer', function () {
  30. done()
  31. })
  32. window.loadURL('file://' + path.join(__dirname, 'fixtures', 'api', 'electron-module-app', 'index.html'))
  33. })
  34. })
  35. })
  36. describe('app module', function () {
  37. describe('app.getVersion()', function () {
  38. it('returns the version field of package.json', function () {
  39. assert.equal(app.getVersion(), '0.1.0')
  40. })
  41. })
  42. describe('app.setVersion(version)', function () {
  43. it('overrides the version', function () {
  44. assert.equal(app.getVersion(), '0.1.0')
  45. app.setVersion('test-version')
  46. assert.equal(app.getVersion(), 'test-version')
  47. app.setVersion('0.1.0')
  48. })
  49. })
  50. describe('app.getName()', function () {
  51. it('returns the name field of package.json', function () {
  52. assert.equal(app.getName(), 'Electron Test')
  53. })
  54. })
  55. describe('app.setName(name)', function () {
  56. it('overrides the name', function () {
  57. assert.equal(app.getName(), 'Electron Test')
  58. app.setName('test-name')
  59. assert.equal(app.getName(), 'test-name')
  60. app.setName('Electron Test')
  61. })
  62. })
  63. describe('app.getLocale()', function () {
  64. it('should not be empty', function () {
  65. assert.notEqual(app.getLocale(), '')
  66. })
  67. })
  68. describe('app.exit(exitCode)', function () {
  69. var appProcess = null
  70. afterEach(function () {
  71. appProcess != null ? appProcess.kill() : void 0
  72. })
  73. it('emits a process exit event with the code', function (done) {
  74. var appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app')
  75. var electronPath = remote.getGlobal('process').execPath
  76. var output = ''
  77. appProcess = ChildProcess.spawn(electronPath, [appPath])
  78. appProcess.stdout.on('data', function (data) {
  79. output += data
  80. })
  81. appProcess.on('close', function (code) {
  82. if (process.platform !== 'win32') {
  83. assert.notEqual(output.indexOf('Exit event with code: 123'), -1)
  84. }
  85. assert.equal(code, 123)
  86. done()
  87. })
  88. })
  89. })
  90. describe('app.relaunch', function () {
  91. let server = null
  92. const socketPath = process.platform === 'win32' ? '\\\\.\\pipe\\electron-app-relaunch' : '/tmp/electron-app-relaunch'
  93. beforeEach(function (done) {
  94. fs.unlink(socketPath, () => {
  95. server = net.createServer()
  96. server.listen(socketPath)
  97. done()
  98. })
  99. })
  100. afterEach(function (done) {
  101. server.close(() => {
  102. if (process.platform === 'win32') {
  103. done()
  104. } else {
  105. fs.unlink(socketPath, () => {
  106. done()
  107. })
  108. }
  109. })
  110. })
  111. it('relaunches the app', function (done) {
  112. this.timeout(100000)
  113. let state = 'none'
  114. server.once('error', (error) => {
  115. done(error)
  116. })
  117. server.on('connection', (client) => {
  118. client.once('data', function (data) {
  119. if (String(data) === 'false' && state === 'none') {
  120. state = 'first-launch'
  121. } else if (String(data) === 'true' && state === 'first-launch') {
  122. done()
  123. } else {
  124. done(`Unexpected state: ${state}`)
  125. }
  126. })
  127. })
  128. const appPath = path.join(__dirname, 'fixtures', 'api', 'relaunch')
  129. ChildProcess.spawn(remote.process.execPath, [appPath])
  130. })
  131. })
  132. describe('app.setUserActivity(type, userInfo)', function () {
  133. if (process.platform !== 'darwin') {
  134. return
  135. }
  136. it('sets the current activity', function () {
  137. app.setUserActivity('com.electron.testActivity', {testData: '123'})
  138. assert.equal(app.getCurrentActivityType(), 'com.electron.testActivity')
  139. })
  140. })
  141. describe('app.importCertificate', function () {
  142. if (process.platform !== 'linux') return
  143. this.timeout(5000)
  144. var w = null
  145. var certPath = path.join(__dirname, 'fixtures', 'certificates')
  146. var options = {
  147. key: fs.readFileSync(path.join(certPath, 'server.key')),
  148. cert: fs.readFileSync(path.join(certPath, 'server.pem')),
  149. ca: [
  150. fs.readFileSync(path.join(certPath, 'rootCA.pem')),
  151. fs.readFileSync(path.join(certPath, 'intermediateCA.pem'))
  152. ],
  153. requestCert: true,
  154. rejectUnauthorized: false
  155. }
  156. var server = https.createServer(options, function (req, res) {
  157. if (req.client.authorized) {
  158. res.writeHead(200)
  159. res.end('authorized')
  160. }
  161. })
  162. afterEach(function () {
  163. return closeWindow(w).then(function () { w = null })
  164. })
  165. it('can import certificate into platform cert store', function (done) {
  166. let options = {
  167. certificate: path.join(certPath, 'client.p12'),
  168. password: 'electron'
  169. }
  170. w = new BrowserWindow({
  171. show: false
  172. })
  173. w.webContents.on('did-finish-load', function () {
  174. server.close()
  175. done()
  176. })
  177. app.on('select-client-certificate', function (event, webContents, url, list, callback) {
  178. assert.equal(list.length, 1)
  179. assert.equal(list[0].issuerName, 'Intermediate CA')
  180. callback(list[0])
  181. })
  182. app.importCertificate(options, function (result) {
  183. assert(!result)
  184. server.listen(0, '127.0.0.1', function () {
  185. var port = server.address().port
  186. w.loadURL(`https://127.0.0.1:${port}`)
  187. })
  188. })
  189. })
  190. })
  191. describe('BrowserWindow events', function () {
  192. var w = null
  193. afterEach(function () {
  194. return closeWindow(w).then(function () { w = null })
  195. })
  196. it('should emit browser-window-focus event when window is focused', function (done) {
  197. app.once('browser-window-focus', function (e, window) {
  198. assert.equal(w.id, window.id)
  199. done()
  200. })
  201. w = new BrowserWindow({
  202. show: false
  203. })
  204. w.emit('focus')
  205. })
  206. it('should emit browser-window-blur event when window is blured', function (done) {
  207. app.once('browser-window-blur', function (e, window) {
  208. assert.equal(w.id, window.id)
  209. done()
  210. })
  211. w = new BrowserWindow({
  212. show: false
  213. })
  214. w.emit('blur')
  215. })
  216. it('should emit browser-window-created event when window is created', function (done) {
  217. app.once('browser-window-created', function (e, window) {
  218. setImmediate(function () {
  219. assert.equal(w.id, window.id)
  220. done()
  221. })
  222. })
  223. w = new BrowserWindow({
  224. show: false
  225. })
  226. })
  227. it('should emit web-contents-created event when a webContents is created', function (done) {
  228. app.once('web-contents-created', function (e, webContents) {
  229. setImmediate(function () {
  230. assert.equal(w.webContents.id, webContents.id)
  231. done()
  232. })
  233. })
  234. w = new BrowserWindow({
  235. show: false
  236. })
  237. })
  238. })
  239. describe('app.setBadgeCount API', function () {
  240. const shouldFail = process.platform === 'win32' ||
  241. (process.platform === 'linux' && !app.isUnityRunning())
  242. it('returns false when failed', function () {
  243. assert.equal(app.setBadgeCount(42), !shouldFail)
  244. })
  245. it('should set a badge count', function () {
  246. app.setBadgeCount(42)
  247. assert.equal(app.getBadgeCount(), shouldFail ? 0 : 42)
  248. })
  249. })
  250. describe('app.get/setLoginItemSettings API', function () {
  251. if (process.platform === 'linux') return
  252. beforeEach(function () {
  253. app.setLoginItemSettings({openAtLogin: false})
  254. })
  255. afterEach(function () {
  256. app.setLoginItemSettings({openAtLogin: false})
  257. })
  258. it('returns the login item status of the app', function () {
  259. app.setLoginItemSettings({openAtLogin: true})
  260. assert.deepEqual(app.getLoginItemSettings(), {
  261. openAtLogin: true,
  262. openAsHidden: false,
  263. wasOpenedAtLogin: false,
  264. wasOpenedAsHidden: false,
  265. restoreState: false
  266. })
  267. app.setLoginItemSettings({openAtLogin: true, openAsHidden: true})
  268. assert.deepEqual(app.getLoginItemSettings(), {
  269. openAtLogin: true,
  270. openAsHidden: process.platform === 'darwin', // Only available on macOS
  271. wasOpenedAtLogin: false,
  272. wasOpenedAsHidden: false,
  273. restoreState: false
  274. })
  275. app.setLoginItemSettings({})
  276. assert.deepEqual(app.getLoginItemSettings(), {
  277. openAtLogin: false,
  278. openAsHidden: false,
  279. wasOpenedAtLogin: false,
  280. wasOpenedAsHidden: false,
  281. restoreState: false
  282. })
  283. })
  284. })
  285. describe('isAccessibilitySupportEnabled API', function () {
  286. it('returns whether the Chrome has accessibility APIs enabled', function () {
  287. assert.equal(typeof app.isAccessibilitySupportEnabled(), 'boolean')
  288. })
  289. })
  290. })