api-browser-window-spec.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. 'use strict'
  2. const assert = require('assert')
  3. const fs = require('fs')
  4. const path = require('path')
  5. const os = require('os')
  6. const http = require('http')
  7. const {closeWindow} = require('./window-helpers')
  8. const remote = require('electron').remote
  9. const screen = require('electron').screen
  10. const app = remote.require('electron').app
  11. const ipcMain = remote.require('electron').ipcMain
  12. const ipcRenderer = require('electron').ipcRenderer
  13. const BrowserWindow = remote.require('electron').BrowserWindow
  14. const isCI = remote.getGlobal('isCi')
  15. describe('browser-window module', function () {
  16. var fixtures = path.resolve(__dirname, 'fixtures')
  17. var w = null
  18. var server
  19. before(function (done) {
  20. server = http.createServer(function (req, res) {
  21. function respond () { res.end('') }
  22. setTimeout(respond, req.url.includes('slow') ? 200 : 0)
  23. })
  24. server.listen(0, '127.0.0.1', function () {
  25. server.url = 'http://127.0.0.1:' + server.address().port
  26. done()
  27. })
  28. })
  29. after(function () {
  30. server.close()
  31. server = null
  32. })
  33. beforeEach(function () {
  34. w = new BrowserWindow({
  35. show: false,
  36. width: 400,
  37. height: 400,
  38. webPreferences: {
  39. backgroundThrottling: false
  40. }
  41. })
  42. })
  43. afterEach(function () {
  44. return closeWindow(w).then(function () { w = null })
  45. })
  46. describe('BrowserWindow.close()', function () {
  47. it('should emit unload handler', function (done) {
  48. w.webContents.on('did-finish-load', function () {
  49. w.close()
  50. })
  51. w.once('closed', function () {
  52. var test = path.join(fixtures, 'api', 'unload')
  53. var content = fs.readFileSync(test)
  54. fs.unlinkSync(test)
  55. assert.equal(String(content), 'unload')
  56. done()
  57. })
  58. w.loadURL('file://' + path.join(fixtures, 'api', 'unload.html'))
  59. })
  60. it('should emit beforeunload handler', function (done) {
  61. w.once('onbeforeunload', function () {
  62. done()
  63. })
  64. w.webContents.on('did-finish-load', function () {
  65. w.close()
  66. })
  67. w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false.html'))
  68. })
  69. })
  70. describe('window.close()', function () {
  71. it('should emit unload handler', function (done) {
  72. w.once('closed', function () {
  73. var test = path.join(fixtures, 'api', 'close')
  74. var content = fs.readFileSync(test)
  75. fs.unlinkSync(test)
  76. assert.equal(String(content), 'close')
  77. done()
  78. })
  79. w.loadURL('file://' + path.join(fixtures, 'api', 'close.html'))
  80. })
  81. it('should emit beforeunload handler', function (done) {
  82. w.once('onbeforeunload', function () {
  83. done()
  84. })
  85. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
  86. })
  87. })
  88. describe('BrowserWindow.destroy()', function () {
  89. it('prevents users to access methods of webContents', function () {
  90. var webContents = w.webContents
  91. w.destroy()
  92. assert.throws(function () {
  93. webContents.getId()
  94. }, /Object has been destroyed/)
  95. })
  96. })
  97. describe('BrowserWindow.loadURL(url)', function () {
  98. it('should emit did-start-loading event', function (done) {
  99. w.webContents.on('did-start-loading', function () {
  100. done()
  101. })
  102. w.loadURL('about:blank')
  103. })
  104. it('should emit did-get-response-details event', function (done) {
  105. // expected {fileName: resourceType} pairs
  106. var expectedResources = {
  107. 'did-get-response-details.html': 'mainFrame',
  108. 'logo.png': 'image'
  109. }
  110. var responses = 0
  111. w.webContents.on('did-get-response-details', function (event, status, newUrl, oldUrl, responseCode, method, referrer, headers, resourceType) {
  112. responses++
  113. var fileName = newUrl.slice(newUrl.lastIndexOf('/') + 1)
  114. var expectedType = expectedResources[fileName]
  115. assert(!!expectedType, `Unexpected response details for ${newUrl}`)
  116. assert(typeof status === 'boolean', 'status should be boolean')
  117. assert.equal(responseCode, 200)
  118. assert.equal(method, 'GET')
  119. assert(typeof referrer === 'string', 'referrer should be string')
  120. assert(!!headers, 'headers should be present')
  121. assert(typeof headers === 'object', 'headers should be object')
  122. assert.equal(resourceType, expectedType, 'Incorrect resourceType')
  123. if (responses === Object.keys(expectedResources).length) {
  124. done()
  125. }
  126. })
  127. w.loadURL('file://' + path.join(fixtures, 'pages', 'did-get-response-details.html'))
  128. })
  129. it('should emit did-fail-load event for files that do not exist', function (done) {
  130. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  131. assert.equal(code, -6)
  132. assert.equal(desc, 'ERR_FILE_NOT_FOUND')
  133. assert.equal(isMainFrame, true)
  134. done()
  135. })
  136. w.loadURL('file://a.txt')
  137. })
  138. it('should emit did-fail-load event for invalid URL', function (done) {
  139. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  140. assert.equal(desc, 'ERR_INVALID_URL')
  141. assert.equal(code, -300)
  142. assert.equal(isMainFrame, true)
  143. done()
  144. })
  145. w.loadURL('http://example:port')
  146. })
  147. it('should set `mainFrame = false` on did-fail-load events in iframes', function (done) {
  148. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  149. assert.equal(isMainFrame, false)
  150. done()
  151. })
  152. w.loadURL('file://' + path.join(fixtures, 'api', 'did-fail-load-iframe.html'))
  153. })
  154. it('does not crash in did-fail-provisional-load handler', function (done) {
  155. this.timeout(10000)
  156. w.webContents.once('did-fail-provisional-load', function () {
  157. w.loadURL('http://127.0.0.1:11111')
  158. done()
  159. })
  160. w.loadURL('http://127.0.0.1:11111')
  161. })
  162. })
  163. describe('BrowserWindow.show()', function () {
  164. if (isCI) {
  165. return
  166. }
  167. it('should focus on window', function () {
  168. w.show()
  169. assert(w.isFocused())
  170. })
  171. it('should make the window visible', function () {
  172. w.show()
  173. assert(w.isVisible())
  174. })
  175. it('emits when window is shown', function (done) {
  176. this.timeout(10000)
  177. w.once('show', function () {
  178. assert.equal(w.isVisible(), true)
  179. done()
  180. })
  181. w.show()
  182. })
  183. })
  184. describe('BrowserWindow.hide()', function () {
  185. if (isCI) {
  186. return
  187. }
  188. it('should defocus on window', function () {
  189. w.hide()
  190. assert(!w.isFocused())
  191. })
  192. it('should make the window not visible', function () {
  193. w.show()
  194. w.hide()
  195. assert(!w.isVisible())
  196. })
  197. it('emits when window is hidden', function (done) {
  198. this.timeout(10000)
  199. w.show()
  200. w.once('hide', function () {
  201. assert.equal(w.isVisible(), false)
  202. done()
  203. })
  204. w.hide()
  205. })
  206. })
  207. describe('BrowserWindow.showInactive()', function () {
  208. it('should not focus on window', function () {
  209. w.showInactive()
  210. assert(!w.isFocused())
  211. })
  212. })
  213. describe('BrowserWindow.focus()', function () {
  214. it('does not make the window become visible', function () {
  215. assert.equal(w.isVisible(), false)
  216. w.focus()
  217. assert.equal(w.isVisible(), false)
  218. })
  219. })
  220. describe('BrowserWindow.blur()', function () {
  221. it('removes focus from window', function () {
  222. w.blur()
  223. assert(!w.isFocused())
  224. })
  225. })
  226. describe('BrowserWindow.capturePage(rect, callback)', function () {
  227. it('calls the callback with a Buffer', function (done) {
  228. w.capturePage({
  229. x: 0,
  230. y: 0,
  231. width: 100,
  232. height: 100
  233. }, function (image) {
  234. assert.equal(image.isEmpty(), true)
  235. done()
  236. })
  237. })
  238. })
  239. describe('BrowserWindow.setSize(width, height)', function () {
  240. it('sets the window size', function (done) {
  241. var size = [300, 400]
  242. w.once('resize', function () {
  243. assertBoundsEqual(w.getSize(), size)
  244. done()
  245. })
  246. w.setSize(size[0], size[1])
  247. })
  248. })
  249. describe('BrowserWindow.setMinimum/MaximumSize(width, height)', function () {
  250. it('sets the maximum and minimum size of the window', function () {
  251. assert.deepEqual(w.getMinimumSize(), [0, 0])
  252. assert.deepEqual(w.getMaximumSize(), [0, 0])
  253. w.setMinimumSize(100, 100)
  254. assertBoundsEqual(w.getMinimumSize(), [100, 100])
  255. assertBoundsEqual(w.getMaximumSize(), [0, 0])
  256. w.setMaximumSize(900, 600)
  257. assertBoundsEqual(w.getMinimumSize(), [100, 100])
  258. assertBoundsEqual(w.getMaximumSize(), [900, 600])
  259. })
  260. })
  261. describe('BrowserWindow.setAspectRatio(ratio)', function () {
  262. it('resets the behaviour when passing in 0', function (done) {
  263. var size = [300, 400]
  264. w.setAspectRatio(1 / 2)
  265. w.setAspectRatio(0)
  266. w.once('resize', function () {
  267. assertBoundsEqual(w.getSize(), size)
  268. done()
  269. })
  270. w.setSize(size[0], size[1])
  271. })
  272. })
  273. describe('BrowserWindow.setPosition(x, y)', function () {
  274. it('sets the window position', function (done) {
  275. var pos = [10, 10]
  276. w.once('move', function () {
  277. var newPos = w.getPosition()
  278. assert.equal(newPos[0], pos[0])
  279. assert.equal(newPos[1], pos[1])
  280. done()
  281. })
  282. w.setPosition(pos[0], pos[1])
  283. })
  284. })
  285. describe('BrowserWindow.setContentSize(width, height)', function () {
  286. it('sets the content size', function () {
  287. var size = [400, 400]
  288. w.setContentSize(size[0], size[1])
  289. var after = w.getContentSize()
  290. assert.equal(after[0], size[0])
  291. assert.equal(after[1], size[1])
  292. })
  293. it('works for a frameless window', function () {
  294. w.destroy()
  295. w = new BrowserWindow({
  296. show: false,
  297. frame: false,
  298. width: 400,
  299. height: 400
  300. })
  301. var size = [400, 400]
  302. w.setContentSize(size[0], size[1])
  303. var after = w.getContentSize()
  304. assert.equal(after[0], size[0])
  305. assert.equal(after[1], size[1])
  306. })
  307. })
  308. describe('BrowserWindow.setContentBounds(bounds)', function () {
  309. it('sets the content size and position', function (done) {
  310. var bounds = {x: 10, y: 10, width: 250, height: 250}
  311. w.once('resize', function () {
  312. assertBoundsEqual(w.getContentBounds(), bounds)
  313. done()
  314. })
  315. w.setContentBounds(bounds)
  316. })
  317. it('works for a frameless window', function (done) {
  318. w.destroy()
  319. w = new BrowserWindow({
  320. show: false,
  321. frame: false,
  322. width: 300,
  323. height: 300
  324. })
  325. var bounds = {x: 10, y: 10, width: 250, height: 250}
  326. w.once('resize', function () {
  327. assert.deepEqual(w.getContentBounds(), bounds)
  328. done()
  329. })
  330. w.setContentBounds(bounds)
  331. })
  332. })
  333. describe('BrowserWindow.setProgressBar(progress)', function () {
  334. it('sets the progress', function () {
  335. assert.doesNotThrow(function () {
  336. if (process.platform === 'darwin') {
  337. app.dock.setIcon(path.join(fixtures, 'assets', 'logo.png'))
  338. }
  339. w.setProgressBar(0.5)
  340. if (process.platform === 'darwin') {
  341. app.dock.setIcon(null)
  342. }
  343. w.setProgressBar(-1)
  344. })
  345. })
  346. it('sets the progress using "paused" mode', function () {
  347. assert.doesNotThrow(function () {
  348. w.setProgressBar(0.5, {mode: 'paused'})
  349. })
  350. })
  351. it('sets the progress using "error" mode', function () {
  352. assert.doesNotThrow(function () {
  353. w.setProgressBar(0.5, {mode: 'error'})
  354. })
  355. })
  356. it('sets the progress using "normal" mode', function () {
  357. assert.doesNotThrow(function () {
  358. w.setProgressBar(0.5, {mode: 'normal'})
  359. })
  360. })
  361. })
  362. describe('BrowserWindow.fromId(id)', function () {
  363. it('returns the window with id', function () {
  364. assert.equal(w.id, BrowserWindow.fromId(w.id).id)
  365. })
  366. })
  367. describe('"useContentSize" option', function () {
  368. it('make window created with content size when used', function () {
  369. w.destroy()
  370. w = new BrowserWindow({
  371. show: false,
  372. width: 400,
  373. height: 400,
  374. useContentSize: true
  375. })
  376. var contentSize = w.getContentSize()
  377. assert.equal(contentSize[0], 400)
  378. assert.equal(contentSize[1], 400)
  379. })
  380. it('make window created with window size when not used', function () {
  381. var size = w.getSize()
  382. assert.equal(size[0], 400)
  383. assert.equal(size[1], 400)
  384. })
  385. it('works for a frameless window', function () {
  386. w.destroy()
  387. w = new BrowserWindow({
  388. show: false,
  389. frame: false,
  390. width: 400,
  391. height: 400,
  392. useContentSize: true
  393. })
  394. var contentSize = w.getContentSize()
  395. assert.equal(contentSize[0], 400)
  396. assert.equal(contentSize[1], 400)
  397. var size = w.getSize()
  398. assert.equal(size[0], 400)
  399. assert.equal(size[1], 400)
  400. })
  401. })
  402. describe('"title-bar-style" option', function () {
  403. if (process.platform !== 'darwin') {
  404. return
  405. }
  406. if (parseInt(os.release().split('.')[0]) < 14) {
  407. return
  408. }
  409. it('creates browser window with hidden title bar', function () {
  410. w.destroy()
  411. w = new BrowserWindow({
  412. show: false,
  413. width: 400,
  414. height: 400,
  415. titleBarStyle: 'hidden'
  416. })
  417. var contentSize = w.getContentSize()
  418. assert.equal(contentSize[1], 400)
  419. })
  420. it('creates browser window with hidden inset title bar', function () {
  421. w.destroy()
  422. w = new BrowserWindow({
  423. show: false,
  424. width: 400,
  425. height: 400,
  426. titleBarStyle: 'hidden-inset'
  427. })
  428. var contentSize = w.getContentSize()
  429. assert.equal(contentSize[1], 400)
  430. })
  431. })
  432. describe('enableLargerThanScreen" option', function () {
  433. if (process.platform === 'linux') {
  434. return
  435. }
  436. beforeEach(function () {
  437. w.destroy()
  438. w = new BrowserWindow({
  439. show: true,
  440. width: 400,
  441. height: 400,
  442. enableLargerThanScreen: true
  443. })
  444. })
  445. it('can move the window out of screen', function () {
  446. w.setPosition(-10, -10)
  447. var after = w.getPosition()
  448. assert.equal(after[0], -10)
  449. assert.equal(after[1], -10)
  450. })
  451. it('can set the window larger than screen', function () {
  452. var size = screen.getPrimaryDisplay().size
  453. size.width += 100
  454. size.height += 100
  455. w.setSize(size.width, size.height)
  456. assertBoundsEqual(w.getSize(), [size.width, size.height])
  457. })
  458. })
  459. describe('"web-preferences" option', function () {
  460. afterEach(function () {
  461. ipcMain.removeAllListeners('answer')
  462. })
  463. describe('"preload" option', function () {
  464. it('loads the script before other scripts in window', function (done) {
  465. var preload = path.join(fixtures, 'module', 'set-global.js')
  466. ipcMain.once('answer', function (event, test) {
  467. assert.equal(test, 'preload')
  468. done()
  469. })
  470. w.destroy()
  471. w = new BrowserWindow({
  472. show: false,
  473. webPreferences: {
  474. preload: preload
  475. }
  476. })
  477. w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
  478. })
  479. it('can successfully delete the Buffer global', function (done) {
  480. var preload = path.join(fixtures, 'module', 'delete-buffer.js')
  481. ipcMain.once('answer', function (event, test) {
  482. assert.equal(test.toString(), 'buffer')
  483. done()
  484. })
  485. w.destroy()
  486. w = new BrowserWindow({
  487. show: false,
  488. webPreferences: {
  489. preload: preload
  490. }
  491. })
  492. w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
  493. })
  494. })
  495. describe('"node-integration" option', function () {
  496. it('disables node integration when specified to false', function (done) {
  497. var preload = path.join(fixtures, 'module', 'send-later.js')
  498. ipcMain.once('answer', function (event, test) {
  499. assert.equal(test, 'undefined')
  500. done()
  501. })
  502. w.destroy()
  503. w = new BrowserWindow({
  504. show: false,
  505. webPreferences: {
  506. preload: preload,
  507. nodeIntegration: false
  508. }
  509. })
  510. w.loadURL('file://' + path.join(fixtures, 'api', 'blank.html'))
  511. })
  512. })
  513. })
  514. describe('beforeunload handler', function () {
  515. it('returning undefined would not prevent close', function (done) {
  516. w.once('closed', function () {
  517. done()
  518. })
  519. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
  520. })
  521. it('returning false would prevent close', function (done) {
  522. w.once('onbeforeunload', function () {
  523. done()
  524. })
  525. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
  526. })
  527. it('returning empty string would prevent close', function (done) {
  528. w.once('onbeforeunload', function () {
  529. done()
  530. })
  531. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html'))
  532. })
  533. })
  534. describe('new-window event', function () {
  535. if (isCI && process.platform === 'darwin') {
  536. return
  537. }
  538. it('emits when window.open is called', function (done) {
  539. w.webContents.once('new-window', function (e, url, frameName) {
  540. e.preventDefault()
  541. assert.equal(url, 'http://host/')
  542. assert.equal(frameName, 'host')
  543. done()
  544. })
  545. w.loadURL('file://' + fixtures + '/pages/window-open.html')
  546. })
  547. it('emits when link with target is called', function (done) {
  548. this.timeout(10000)
  549. w.webContents.once('new-window', function (e, url, frameName) {
  550. e.preventDefault()
  551. assert.equal(url, 'http://host/')
  552. assert.equal(frameName, 'target')
  553. done()
  554. })
  555. w.loadURL('file://' + fixtures + '/pages/target-name.html')
  556. })
  557. })
  558. describe('maximize event', function () {
  559. if (isCI) {
  560. return
  561. }
  562. it('emits when window is maximized', function (done) {
  563. this.timeout(10000)
  564. w.once('maximize', function () {
  565. done()
  566. })
  567. w.show()
  568. w.maximize()
  569. })
  570. })
  571. describe('unmaximize event', function () {
  572. if (isCI) {
  573. return
  574. }
  575. it('emits when window is unmaximized', function (done) {
  576. this.timeout(10000)
  577. w.once('unmaximize', function () {
  578. done()
  579. })
  580. w.show()
  581. w.maximize()
  582. w.unmaximize()
  583. })
  584. })
  585. describe('minimize event', function () {
  586. if (isCI) {
  587. return
  588. }
  589. it('emits when window is minimized', function (done) {
  590. this.timeout(10000)
  591. w.once('minimize', function () {
  592. done()
  593. })
  594. w.show()
  595. w.minimize()
  596. })
  597. })
  598. describe('beginFrameSubscription method', function () {
  599. // This test is too slow, only test it on CI.
  600. if (!isCI) return
  601. this.timeout(20000)
  602. it('subscribes to frame updates', function (done) {
  603. let called = false
  604. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  605. w.webContents.on('dom-ready', function () {
  606. w.webContents.beginFrameSubscription(function (data) {
  607. // This callback might be called twice.
  608. if (called) return
  609. called = true
  610. assert.notEqual(data.length, 0)
  611. w.webContents.endFrameSubscription()
  612. done()
  613. })
  614. })
  615. })
  616. it('subscribes to frame updates (only dirty rectangle)', function (done) {
  617. let called = false
  618. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  619. w.webContents.on('dom-ready', function () {
  620. w.webContents.beginFrameSubscription(true, function (data) {
  621. // This callback might be called twice.
  622. if (called) return
  623. called = true
  624. assert.notEqual(data.length, 0)
  625. w.webContents.endFrameSubscription()
  626. done()
  627. })
  628. })
  629. })
  630. it('throws error when subscriber is not well defined', function (done) {
  631. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  632. try {
  633. w.webContents.beginFrameSubscription(true, true)
  634. } catch (e) {
  635. done()
  636. }
  637. })
  638. })
  639. describe('savePage method', function () {
  640. const savePageDir = path.join(fixtures, 'save_page')
  641. const savePageHtmlPath = path.join(savePageDir, 'save_page.html')
  642. const savePageJsPath = path.join(savePageDir, 'save_page_files', 'test.js')
  643. const savePageCssPath = path.join(savePageDir, 'save_page_files', 'test.css')
  644. after(function () {
  645. try {
  646. fs.unlinkSync(savePageCssPath)
  647. fs.unlinkSync(savePageJsPath)
  648. fs.unlinkSync(savePageHtmlPath)
  649. fs.rmdirSync(path.join(savePageDir, 'save_page_files'))
  650. fs.rmdirSync(savePageDir)
  651. } catch (e) {
  652. // Ignore error
  653. }
  654. })
  655. it('should save page to disk', function (done) {
  656. w.webContents.on('did-finish-load', function () {
  657. w.webContents.savePage(savePageHtmlPath, 'HTMLComplete', function (error) {
  658. assert.equal(error, null)
  659. assert(fs.existsSync(savePageHtmlPath))
  660. assert(fs.existsSync(savePageJsPath))
  661. assert(fs.existsSync(savePageCssPath))
  662. done()
  663. })
  664. })
  665. w.loadURL('file://' + fixtures + '/pages/save_page/index.html')
  666. })
  667. })
  668. describe('BrowserWindow options argument is optional', function () {
  669. it('should create a window with default size (800x600)', function () {
  670. w.destroy()
  671. w = new BrowserWindow()
  672. var size = w.getSize()
  673. assert.equal(size[0], 800)
  674. assert.equal(size[1], 600)
  675. })
  676. })
  677. describe('window states', function () {
  678. it('does not resize frameless windows when states change', function () {
  679. w.destroy()
  680. w = new BrowserWindow({
  681. frame: false,
  682. width: 300,
  683. height: 200,
  684. show: false
  685. })
  686. w.setMinimizable(false)
  687. w.setMinimizable(true)
  688. assert.deepEqual(w.getSize(), [300, 200])
  689. w.setResizable(false)
  690. w.setResizable(true)
  691. assert.deepEqual(w.getSize(), [300, 200])
  692. w.setMaximizable(false)
  693. w.setMaximizable(true)
  694. assert.deepEqual(w.getSize(), [300, 200])
  695. w.setFullScreenable(false)
  696. w.setFullScreenable(true)
  697. assert.deepEqual(w.getSize(), [300, 200])
  698. w.setClosable(false)
  699. w.setClosable(true)
  700. assert.deepEqual(w.getSize(), [300, 200])
  701. })
  702. describe('resizable state', function () {
  703. it('can be changed with resizable option', function () {
  704. w.destroy()
  705. w = new BrowserWindow({show: false, resizable: false})
  706. assert.equal(w.isResizable(), false)
  707. if (process.platform === 'darwin') {
  708. assert.equal(w.isMaximizable(), true)
  709. }
  710. })
  711. it('can be changed with setResizable method', function () {
  712. assert.equal(w.isResizable(), true)
  713. w.setResizable(false)
  714. assert.equal(w.isResizable(), false)
  715. w.setResizable(true)
  716. assert.equal(w.isResizable(), true)
  717. })
  718. })
  719. describe('loading main frame state', function () {
  720. it('is true when the main frame is loading', function (done) {
  721. w.webContents.on('did-start-loading', function () {
  722. assert.equal(w.webContents.isLoadingMainFrame(), true)
  723. done()
  724. })
  725. w.webContents.loadURL(server.url)
  726. })
  727. it('is false when only a subframe is loading', function (done) {
  728. w.webContents.once('did-finish-load', function () {
  729. assert.equal(w.webContents.isLoadingMainFrame(), false)
  730. w.webContents.on('did-start-loading', function () {
  731. assert.equal(w.webContents.isLoadingMainFrame(), false)
  732. done()
  733. })
  734. w.webContents.executeJavaScript(`
  735. var iframe = document.createElement('iframe')
  736. iframe.src = '${server.url}/page2'
  737. document.body.appendChild(iframe)
  738. `)
  739. })
  740. w.webContents.loadURL(server.url)
  741. })
  742. it('is true when navigating to pages from the same origin', function (done) {
  743. w.webContents.once('did-finish-load', function () {
  744. assert.equal(w.webContents.isLoadingMainFrame(), false)
  745. w.webContents.on('did-start-loading', function () {
  746. assert.equal(w.webContents.isLoadingMainFrame(), true)
  747. done()
  748. })
  749. w.webContents.loadURL(`${server.url}/page2`)
  750. })
  751. w.webContents.loadURL(server.url)
  752. })
  753. })
  754. })
  755. describe('window states (excluding Linux)', function () {
  756. // Not implemented on Linux.
  757. if (process.platform === 'linux') return
  758. describe('movable state', function () {
  759. it('can be changed with movable option', function () {
  760. w.destroy()
  761. w = new BrowserWindow({show: false, movable: false})
  762. assert.equal(w.isMovable(), false)
  763. })
  764. it('can be changed with setMovable method', function () {
  765. assert.equal(w.isMovable(), true)
  766. w.setMovable(false)
  767. assert.equal(w.isMovable(), false)
  768. w.setMovable(true)
  769. assert.equal(w.isMovable(), true)
  770. })
  771. })
  772. describe('minimizable state', function () {
  773. it('can be changed with minimizable option', function () {
  774. w.destroy()
  775. w = new BrowserWindow({show: false, minimizable: false})
  776. assert.equal(w.isMinimizable(), false)
  777. })
  778. it('can be changed with setMinimizable method', function () {
  779. assert.equal(w.isMinimizable(), true)
  780. w.setMinimizable(false)
  781. assert.equal(w.isMinimizable(), false)
  782. w.setMinimizable(true)
  783. assert.equal(w.isMinimizable(), true)
  784. })
  785. })
  786. describe('maximizable state', function () {
  787. it('can be changed with maximizable option', function () {
  788. w.destroy()
  789. w = new BrowserWindow({show: false, maximizable: false})
  790. assert.equal(w.isMaximizable(), false)
  791. })
  792. it('can be changed with setMaximizable method', function () {
  793. assert.equal(w.isMaximizable(), true)
  794. w.setMaximizable(false)
  795. assert.equal(w.isMaximizable(), false)
  796. w.setMaximizable(true)
  797. assert.equal(w.isMaximizable(), true)
  798. })
  799. it('is not affected when changing other states', function () {
  800. w.setMaximizable(false)
  801. assert.equal(w.isMaximizable(), false)
  802. w.setMinimizable(false)
  803. assert.equal(w.isMaximizable(), false)
  804. w.setClosable(false)
  805. assert.equal(w.isMaximizable(), false)
  806. w.setMaximizable(true)
  807. assert.equal(w.isMaximizable(), true)
  808. w.setClosable(true)
  809. assert.equal(w.isMaximizable(), true)
  810. w.setFullScreenable(false)
  811. assert.equal(w.isMaximizable(), true)
  812. w.setResizable(false)
  813. assert.equal(w.isMaximizable(), true)
  814. })
  815. })
  816. describe('fullscreenable state', function () {
  817. // Only implemented on macOS.
  818. if (process.platform !== 'darwin') return
  819. it('can be changed with fullscreenable option', function () {
  820. w.destroy()
  821. w = new BrowserWindow({show: false, fullscreenable: false})
  822. assert.equal(w.isFullScreenable(), false)
  823. })
  824. it('can be changed with setFullScreenable method', function () {
  825. assert.equal(w.isFullScreenable(), true)
  826. w.setFullScreenable(false)
  827. assert.equal(w.isFullScreenable(), false)
  828. w.setFullScreenable(true)
  829. assert.equal(w.isFullScreenable(), true)
  830. })
  831. })
  832. describe('closable state', function () {
  833. it('can be changed with closable option', function () {
  834. w.destroy()
  835. w = new BrowserWindow({show: false, closable: false})
  836. assert.equal(w.isClosable(), false)
  837. })
  838. it('can be changed with setClosable method', function () {
  839. assert.equal(w.isClosable(), true)
  840. w.setClosable(false)
  841. assert.equal(w.isClosable(), false)
  842. w.setClosable(true)
  843. assert.equal(w.isClosable(), true)
  844. })
  845. })
  846. describe('hasShadow state', function () {
  847. // On Window there is no shadow by default and it can not be changed
  848. // dynamically.
  849. it('can be changed with hasShadow option', function () {
  850. w.destroy()
  851. let hasShadow = process.platform !== 'darwin'
  852. w = new BrowserWindow({show: false, hasShadow: hasShadow})
  853. assert.equal(w.hasShadow(), hasShadow)
  854. })
  855. it('can be changed with setHasShadow method', function () {
  856. if (process.platform !== 'darwin') return
  857. assert.equal(w.hasShadow(), true)
  858. w.setHasShadow(false)
  859. assert.equal(w.hasShadow(), false)
  860. w.setHasShadow(true)
  861. assert.equal(w.hasShadow(), true)
  862. })
  863. })
  864. })
  865. describe('parent window', function () {
  866. let c = null
  867. beforeEach(function () {
  868. if (c != null) c.destroy()
  869. c = new BrowserWindow({show: false, parent: w})
  870. })
  871. afterEach(function () {
  872. if (c != null) c.destroy()
  873. c = null
  874. })
  875. describe('parent option', function () {
  876. it('sets parent window', function () {
  877. assert.equal(c.getParentWindow(), w)
  878. })
  879. it('adds window to child windows of parent', function () {
  880. assert.deepEqual(w.getChildWindows(), [c])
  881. })
  882. it('removes from child windows of parent when window is closed', function (done) {
  883. c.once('closed', () => {
  884. assert.deepEqual(w.getChildWindows(), [])
  885. done()
  886. })
  887. c.close()
  888. })
  889. })
  890. describe('win.setParentWindow(parent)', function () {
  891. if (process.platform === 'win32') return
  892. beforeEach(function () {
  893. if (c != null) c.destroy()
  894. c = new BrowserWindow({show: false})
  895. })
  896. it('sets parent window', function () {
  897. assert.equal(w.getParentWindow(), null)
  898. assert.equal(c.getParentWindow(), null)
  899. c.setParentWindow(w)
  900. assert.equal(c.getParentWindow(), w)
  901. c.setParentWindow(null)
  902. assert.equal(c.getParentWindow(), null)
  903. })
  904. it('adds window to child windows of parent', function () {
  905. assert.deepEqual(w.getChildWindows(), [])
  906. c.setParentWindow(w)
  907. assert.deepEqual(w.getChildWindows(), [c])
  908. c.setParentWindow(null)
  909. assert.deepEqual(w.getChildWindows(), [])
  910. })
  911. it('removes from child windows of parent when window is closed', function (done) {
  912. c.once('closed', () => {
  913. assert.deepEqual(w.getChildWindows(), [])
  914. done()
  915. })
  916. c.setParentWindow(w)
  917. c.close()
  918. })
  919. })
  920. describe('modal option', function () {
  921. // The isEnabled API is not reliable on macOS.
  922. if (process.platform === 'darwin') return
  923. beforeEach(function () {
  924. if (c != null) c.destroy()
  925. c = new BrowserWindow({show: false, parent: w, modal: true})
  926. })
  927. it('disables parent window', function () {
  928. assert.equal(w.isEnabled(), true)
  929. c.show()
  930. assert.equal(w.isEnabled(), false)
  931. })
  932. it('enables parent window when closed', function (done) {
  933. c.once('closed', () => {
  934. assert.equal(w.isEnabled(), true)
  935. done()
  936. })
  937. c.show()
  938. c.close()
  939. })
  940. it('disables parent window recursively', function () {
  941. let c2 = new BrowserWindow({show: false, parent: w, modal: true})
  942. c.show()
  943. assert.equal(w.isEnabled(), false)
  944. c2.show()
  945. assert.equal(w.isEnabled(), false)
  946. c.destroy()
  947. assert.equal(w.isEnabled(), false)
  948. c2.destroy()
  949. assert.equal(w.isEnabled(), true)
  950. })
  951. })
  952. })
  953. describe('window.webContents.send(channel, args...)', function () {
  954. it('throws an error when the channel is missing', function () {
  955. assert.throws(function () {
  956. w.webContents.send()
  957. }, 'Missing required channel argument')
  958. assert.throws(function () {
  959. w.webContents.send(null)
  960. }, 'Missing required channel argument')
  961. })
  962. })
  963. describe('dev tool extensions', function () {
  964. describe('BrowserWindow.addDevToolsExtension', function () {
  965. let showPanelIntevalId
  966. this.timeout(10000)
  967. beforeEach(function () {
  968. BrowserWindow.removeDevToolsExtension('foo')
  969. assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false)
  970. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
  971. BrowserWindow.addDevToolsExtension(extensionPath)
  972. assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true)
  973. w.webContents.on('devtools-opened', function () {
  974. showPanelIntevalId = setInterval(function () {
  975. if (w && w.devToolsWebContents) {
  976. var showLastPanel = function () {
  977. var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id
  978. WebInspector.inspectorView.showPanel(lastPanelId)
  979. }
  980. w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`)
  981. } else {
  982. clearInterval(showPanelIntevalId)
  983. }
  984. }, 100)
  985. })
  986. w.loadURL('about:blank')
  987. })
  988. afterEach(function () {
  989. clearInterval(showPanelIntevalId)
  990. })
  991. it('throws errors for missing manifest.json files', function () {
  992. assert.throws(function () {
  993. BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist'))
  994. }, /ENOENT: no such file or directory/)
  995. })
  996. it('throws errors for invalid manifest.json files', function () {
  997. assert.throws(function () {
  998. BrowserWindow.addDevToolsExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
  999. }, /Unexpected token }/)
  1000. })
  1001. describe('when the devtools is docked', function () {
  1002. it('creates the extension', function (done) {
  1003. w.webContents.openDevTools({mode: 'bottom'})
  1004. ipcMain.once('answer', function (event, message) {
  1005. assert.equal(message.runtimeId, 'foo')
  1006. assert.equal(message.tabId, w.webContents.id)
  1007. assert.equal(message.i18nString, 'foo - bar (baz)')
  1008. assert.deepEqual(message.storageItems, {
  1009. local: {hello: 'world'},
  1010. sync: {foo: 'bar'}
  1011. })
  1012. done()
  1013. })
  1014. })
  1015. })
  1016. describe('when the devtools is undocked', function () {
  1017. it('creates the extension', function (done) {
  1018. w.webContents.openDevTools({mode: 'undocked'})
  1019. ipcMain.once('answer', function (event, message, extensionId) {
  1020. assert.equal(message.runtimeId, 'foo')
  1021. assert.equal(message.tabId, w.webContents.id)
  1022. done()
  1023. })
  1024. })
  1025. })
  1026. })
  1027. it('works when used with partitions', function (done) {
  1028. this.timeout(10000)
  1029. if (w != null) {
  1030. w.destroy()
  1031. }
  1032. w = new BrowserWindow({
  1033. show: false,
  1034. webPreferences: {
  1035. partition: 'temp'
  1036. }
  1037. })
  1038. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
  1039. BrowserWindow.removeDevToolsExtension('foo')
  1040. BrowserWindow.addDevToolsExtension(extensionPath)
  1041. w.webContents.on('devtools-opened', function () {
  1042. var showPanelIntevalId = setInterval(function () {
  1043. if (w && w.devToolsWebContents) {
  1044. var showLastPanel = function () {
  1045. var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id
  1046. WebInspector.inspectorView.showPanel(lastPanelId)
  1047. }
  1048. w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`)
  1049. } else {
  1050. clearInterval(showPanelIntevalId)
  1051. }
  1052. }, 100)
  1053. })
  1054. w.loadURL('about:blank')
  1055. w.webContents.openDevTools({mode: 'bottom'})
  1056. ipcMain.once('answer', function (event, message) {
  1057. assert.equal(message.runtimeId, 'foo')
  1058. done()
  1059. })
  1060. })
  1061. it('serializes the registered extensions on quit', function () {
  1062. var extensionName = 'foo'
  1063. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', extensionName)
  1064. var serializedPath = path.join(app.getPath('userData'), 'DevTools Extensions')
  1065. BrowserWindow.addDevToolsExtension(extensionPath)
  1066. app.emit('will-quit')
  1067. assert.deepEqual(JSON.parse(fs.readFileSync(serializedPath)), [extensionPath])
  1068. BrowserWindow.removeDevToolsExtension(extensionName)
  1069. app.emit('will-quit')
  1070. assert.equal(fs.existsSync(serializedPath), false)
  1071. })
  1072. })
  1073. describe('window.webContents.executeJavaScript', function () {
  1074. var expected = 'hello, world!'
  1075. var code = '(() => "' + expected + '")()'
  1076. it('doesnt throw when no calback is provided', function () {
  1077. const result = ipcRenderer.sendSync('executeJavaScript', code, false)
  1078. assert.equal(result, 'success')
  1079. })
  1080. it('returns result when calback is provided', function (done) {
  1081. ipcRenderer.send('executeJavaScript', code, true)
  1082. ipcRenderer.once('executeJavaScript-response', function (event, result) {
  1083. assert.equal(result, expected)
  1084. done()
  1085. })
  1086. })
  1087. it('works after page load and during subframe load', function (done) {
  1088. w.webContents.once('did-finish-load', function () {
  1089. // initiate a sub-frame load, then try and execute script during it
  1090. w.webContents.executeJavaScript(`
  1091. var iframe = document.createElement('iframe')
  1092. iframe.src = '${server.url}/slow'
  1093. document.body.appendChild(iframe)
  1094. `, function () {
  1095. w.webContents.executeJavaScript('console.log(\'hello\')', function () {
  1096. done()
  1097. })
  1098. })
  1099. })
  1100. w.loadURL(server.url)
  1101. })
  1102. it('executes after page load', function (done) {
  1103. w.webContents.executeJavaScript(code, function (result) {
  1104. assert.equal(result, expected)
  1105. done()
  1106. })
  1107. w.loadURL(server.url)
  1108. })
  1109. it('works with result objects that have DOM class prototypes', function (done) {
  1110. w.webContents.executeJavaScript('document.location', function (result) {
  1111. assert.equal(result.origin, server.url)
  1112. assert.equal(result.protocol, 'http:')
  1113. done()
  1114. })
  1115. w.loadURL(server.url)
  1116. })
  1117. })
  1118. describe('offscreen rendering', function () {
  1119. this.timeout(10000)
  1120. beforeEach(function () {
  1121. if (w != null) w.destroy()
  1122. w = new BrowserWindow({
  1123. show: false,
  1124. webPreferences: {
  1125. backgroundThrottling: false,
  1126. offscreen: true
  1127. }
  1128. })
  1129. })
  1130. it('creates offscreen window', function (done) {
  1131. w.webContents.once('paint', function (event, rect, data, size) {
  1132. assert.notEqual(data.length, 0)
  1133. done()
  1134. })
  1135. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1136. })
  1137. describe('window.webContents.isOffscreen()', function () {
  1138. it('is true for offscreen type', function () {
  1139. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1140. assert.equal(w.webContents.isOffscreen(), true)
  1141. })
  1142. it('is false for regular window', function () {
  1143. let c = new BrowserWindow({show: false})
  1144. assert.equal(c.webContents.isOffscreen(), false)
  1145. c.destroy()
  1146. })
  1147. })
  1148. describe('window.webContents.isPainting()', function () {
  1149. it('returns whether is currently painting', function (done) {
  1150. w.webContents.once('paint', function (event, rect, data, size) {
  1151. assert.equal(w.webContents.isPainting(), true)
  1152. done()
  1153. })
  1154. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1155. })
  1156. })
  1157. describe('window.webContents.stopPainting()', function () {
  1158. it('stops painting', function (done) {
  1159. w.webContents.on('dom-ready', function () {
  1160. w.webContents.stopPainting()
  1161. assert.equal(w.webContents.isPainting(), false)
  1162. done()
  1163. })
  1164. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1165. })
  1166. })
  1167. describe('window.webContents.startPainting()', function () {
  1168. it('starts painting', function (done) {
  1169. w.webContents.on('dom-ready', function () {
  1170. w.webContents.stopPainting()
  1171. w.webContents.startPainting()
  1172. w.webContents.once('paint', function (event, rect, data, size) {
  1173. assert.equal(w.webContents.isPainting(), true)
  1174. done()
  1175. })
  1176. })
  1177. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1178. })
  1179. })
  1180. describe('window.webContents.getFrameRate()', function () {
  1181. it('has default frame rate', function (done) {
  1182. w.webContents.once('paint', function (event, rect, data, size) {
  1183. assert.equal(w.webContents.getFrameRate(), 60)
  1184. done()
  1185. })
  1186. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1187. })
  1188. })
  1189. describe('window.webContents.setFrameRate(frameRate)', function () {
  1190. it('sets custom frame rate', function (done) {
  1191. w.webContents.on('dom-ready', function () {
  1192. w.webContents.setFrameRate(30)
  1193. w.webContents.once('paint', function (event, rect, data, size) {
  1194. assert.equal(w.webContents.getFrameRate(), 30)
  1195. done()
  1196. })
  1197. })
  1198. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1199. })
  1200. })
  1201. })
  1202. })
  1203. const assertBoundsEqual = (actual, expect) => {
  1204. if (!isScaleFactorRounding()) {
  1205. assert.deepEqual(expect, actual)
  1206. } else if (Array.isArray(actual)) {
  1207. assertWithinDelta(actual[0], expect[0], 1, 'x')
  1208. assertWithinDelta(actual[1], expect[1], 1, 'y')
  1209. } else {
  1210. assertWithinDelta(actual.x, expect.x, 1, 'x')
  1211. assertWithinDelta(actual.y, expect.y, 1, 'y')
  1212. assertWithinDelta(actual.width, expect.width, 1, 'width')
  1213. assertWithinDelta(actual.height, expect.height, 1, 'height')
  1214. }
  1215. }
  1216. const assertWithinDelta = (actual, expect, delta, label) => {
  1217. const result = Math.abs(actual - expect)
  1218. assert.ok(result <= delta, `${label} value of ${expect} was not within ${delta} of ${actual}`)
  1219. }
  1220. // Is the display's scale factor possibly causing rounding of pixel coordinate
  1221. // values?
  1222. const isScaleFactorRounding = () => {
  1223. const {scaleFactor} = screen.getPrimaryDisplay()
  1224. // Return true if scale factor is non-integer value
  1225. if (Math.round(scaleFactor) !== scaleFactor) return true
  1226. // Return true if scale factor is odd number above 2
  1227. return scaleFactor > 2 && scaleFactor % 2 === 1
  1228. }