api-browser-window-spec.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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 qs = require('querystring')
  7. const http = require('http')
  8. const {closeWindow} = require('./window-helpers')
  9. const {ipcRenderer, remote, screen} = require('electron')
  10. const {app, ipcMain, BrowserWindow, protocol, webContents} = remote
  11. const isCI = remote.getGlobal('isCi')
  12. describe('BrowserWindow module', function () {
  13. var fixtures = path.resolve(__dirname, 'fixtures')
  14. var w = null
  15. var server, postData
  16. before(function (done) {
  17. const filePath = path.join(fixtures, 'pages', 'a.html')
  18. const fileStats = fs.statSync(filePath)
  19. postData = [
  20. {
  21. type: 'rawData',
  22. bytes: new Buffer('username=test&file=')
  23. },
  24. {
  25. type: 'file',
  26. filePath: filePath,
  27. offset: 0,
  28. length: fileStats.size,
  29. modificationTime: fileStats.mtime.getTime() / 1000
  30. }
  31. ]
  32. server = http.createServer(function (req, res) {
  33. function respond () {
  34. if (req.method === 'POST') {
  35. let body = ''
  36. req.on('data', (data) => {
  37. if (data) {
  38. body += data
  39. }
  40. })
  41. req.on('end', () => {
  42. let parsedData = qs.parse(body)
  43. fs.readFile(filePath, (err, data) => {
  44. if (err) return
  45. if (parsedData.username === 'test' &&
  46. parsedData.file === data.toString()) {
  47. res.end()
  48. }
  49. })
  50. })
  51. } else {
  52. res.end()
  53. }
  54. }
  55. setTimeout(respond, req.url.includes('slow') ? 200 : 0)
  56. })
  57. server.listen(0, '127.0.0.1', function () {
  58. server.url = 'http://127.0.0.1:' + server.address().port
  59. done()
  60. })
  61. })
  62. after(function () {
  63. server.close()
  64. server = null
  65. })
  66. beforeEach(function () {
  67. w = new BrowserWindow({
  68. show: false,
  69. width: 400,
  70. height: 400,
  71. webPreferences: {
  72. backgroundThrottling: false
  73. }
  74. })
  75. })
  76. afterEach(function () {
  77. return closeWindow(w).then(function () { w = null })
  78. })
  79. describe('BrowserWindow.close()', function () {
  80. it('should emit unload handler', function (done) {
  81. w.webContents.on('did-finish-load', function () {
  82. w.close()
  83. })
  84. w.once('closed', function () {
  85. var test = path.join(fixtures, 'api', 'unload')
  86. var content = fs.readFileSync(test)
  87. fs.unlinkSync(test)
  88. assert.equal(String(content), 'unload')
  89. done()
  90. })
  91. w.loadURL('file://' + path.join(fixtures, 'api', 'unload.html'))
  92. })
  93. it('should emit beforeunload handler', function (done) {
  94. w.once('onbeforeunload', function () {
  95. done()
  96. })
  97. w.webContents.on('did-finish-load', function () {
  98. w.close()
  99. })
  100. w.loadURL('file://' + path.join(fixtures, 'api', 'beforeunload-false.html'))
  101. })
  102. })
  103. describe('window.close()', function () {
  104. it('should emit unload handler', function (done) {
  105. w.once('closed', function () {
  106. var test = path.join(fixtures, 'api', 'close')
  107. var content = fs.readFileSync(test)
  108. fs.unlinkSync(test)
  109. assert.equal(String(content), 'close')
  110. done()
  111. })
  112. w.loadURL('file://' + path.join(fixtures, 'api', 'close.html'))
  113. })
  114. it('should emit beforeunload handler', function (done) {
  115. w.once('onbeforeunload', function () {
  116. done()
  117. })
  118. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
  119. })
  120. })
  121. describe('BrowserWindow.destroy()', function () {
  122. it('prevents users to access methods of webContents', function () {
  123. const contents = w.webContents
  124. w.destroy()
  125. assert.throws(function () {
  126. contents.getId()
  127. }, /Object has been destroyed/)
  128. })
  129. })
  130. describe('BrowserWindow.loadURL(url)', function () {
  131. it('should emit did-start-loading event', function (done) {
  132. w.webContents.on('did-start-loading', function () {
  133. done()
  134. })
  135. w.loadURL('about:blank')
  136. })
  137. it('should emit ready-to-show event', function (done) {
  138. w.on('ready-to-show', function () {
  139. done()
  140. })
  141. w.loadURL('about:blank')
  142. })
  143. it('should emit did-get-response-details event', function (done) {
  144. // expected {fileName: resourceType} pairs
  145. var expectedResources = {
  146. 'did-get-response-details.html': 'mainFrame',
  147. 'logo.png': 'image'
  148. }
  149. var responses = 0
  150. w.webContents.on('did-get-response-details', function (event, status, newUrl, oldUrl, responseCode, method, referrer, headers, resourceType) {
  151. responses++
  152. var fileName = newUrl.slice(newUrl.lastIndexOf('/') + 1)
  153. var expectedType = expectedResources[fileName]
  154. assert(!!expectedType, `Unexpected response details for ${newUrl}`)
  155. assert(typeof status === 'boolean', 'status should be boolean')
  156. assert.equal(responseCode, 200)
  157. assert.equal(method, 'GET')
  158. assert(typeof referrer === 'string', 'referrer should be string')
  159. assert(!!headers, 'headers should be present')
  160. assert(typeof headers === 'object', 'headers should be object')
  161. assert.equal(resourceType, expectedType, 'Incorrect resourceType')
  162. if (responses === Object.keys(expectedResources).length) {
  163. done()
  164. }
  165. })
  166. w.loadURL('file://' + path.join(fixtures, 'pages', 'did-get-response-details.html'))
  167. })
  168. it('should emit did-fail-load event for files that do not exist', function (done) {
  169. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  170. assert.equal(code, -6)
  171. assert.equal(desc, 'ERR_FILE_NOT_FOUND')
  172. assert.equal(isMainFrame, true)
  173. done()
  174. })
  175. w.loadURL('file://a.txt')
  176. })
  177. it('should emit did-fail-load event for invalid URL', function (done) {
  178. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  179. assert.equal(desc, 'ERR_INVALID_URL')
  180. assert.equal(code, -300)
  181. assert.equal(isMainFrame, true)
  182. done()
  183. })
  184. w.loadURL('http://example:port')
  185. })
  186. it('should set `mainFrame = false` on did-fail-load events in iframes', function (done) {
  187. w.webContents.on('did-fail-load', function (event, code, desc, url, isMainFrame) {
  188. assert.equal(isMainFrame, false)
  189. done()
  190. })
  191. w.loadURL('file://' + path.join(fixtures, 'api', 'did-fail-load-iframe.html'))
  192. })
  193. it('does not crash in did-fail-provisional-load handler', function (done) {
  194. w.webContents.once('did-fail-provisional-load', function () {
  195. w.loadURL('http://127.0.0.1:11111')
  196. done()
  197. })
  198. w.loadURL('http://127.0.0.1:11111')
  199. })
  200. describe('POST navigations', function () {
  201. afterEach(() => {
  202. w.webContents.session.webRequest.onBeforeSendHeaders(null)
  203. })
  204. it('supports specifying POST data', function (done) {
  205. w.webContents.on('did-finish-load', () => done())
  206. w.loadURL(server.url, {postData: postData})
  207. })
  208. it('sets the content type header on URL encoded forms', function (done) {
  209. w.webContents.on('did-finish-load', () => {
  210. w.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
  211. assert.equal(details.requestHeaders['content-type'], 'application/x-www-form-urlencoded')
  212. done()
  213. })
  214. w.webContents.executeJavaScript(`
  215. form = document.createElement('form')
  216. form.method = 'POST'
  217. form.target = '_blank'
  218. form.submit()
  219. `)
  220. })
  221. w.loadURL(server.url)
  222. })
  223. it('sets the content type header on multi part forms', function (done) {
  224. w.webContents.on('did-finish-load', () => {
  225. w.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
  226. assert(details.requestHeaders['content-type'].startsWith('multipart/form-data; boundary=----WebKitFormBoundary'))
  227. done()
  228. })
  229. w.webContents.executeJavaScript(`
  230. form = document.createElement('form')
  231. form.method = 'POST'
  232. form.target = '_blank'
  233. form.enctype = 'multipart/form-data'
  234. file = document.createElement('input')
  235. file.type = 'file'
  236. file.name = 'file'
  237. form.appendChild(file)
  238. form.submit()
  239. `)
  240. })
  241. w.loadURL(server.url)
  242. })
  243. })
  244. })
  245. describe('will-navigate event', function () {
  246. it('allows the window to be closed from the event listener', (done) => {
  247. ipcRenderer.send('close-on-will-navigate', w.id)
  248. ipcRenderer.once('closed-on-will-navigate', () => {
  249. done()
  250. })
  251. w.loadURL('file://' + fixtures + '/pages/will-navigate.html')
  252. })
  253. })
  254. describe('BrowserWindow.show()', function () {
  255. if (isCI) {
  256. return
  257. }
  258. it('should focus on window', function () {
  259. w.show()
  260. assert(w.isFocused())
  261. })
  262. it('should make the window visible', function () {
  263. w.show()
  264. assert(w.isVisible())
  265. })
  266. it('emits when window is shown', function (done) {
  267. w.once('show', function () {
  268. assert.equal(w.isVisible(), true)
  269. done()
  270. })
  271. w.show()
  272. })
  273. })
  274. describe('BrowserWindow.hide()', function () {
  275. if (isCI) {
  276. return
  277. }
  278. it('should defocus on window', function () {
  279. w.hide()
  280. assert(!w.isFocused())
  281. })
  282. it('should make the window not visible', function () {
  283. w.show()
  284. w.hide()
  285. assert(!w.isVisible())
  286. })
  287. it('emits when window is hidden', function (done) {
  288. w.show()
  289. w.once('hide', function () {
  290. assert.equal(w.isVisible(), false)
  291. done()
  292. })
  293. w.hide()
  294. })
  295. })
  296. describe('BrowserWindow.showInactive()', function () {
  297. it('should not focus on window', function () {
  298. w.showInactive()
  299. assert(!w.isFocused())
  300. })
  301. })
  302. describe('BrowserWindow.focus()', function () {
  303. it('does not make the window become visible', function () {
  304. assert.equal(w.isVisible(), false)
  305. w.focus()
  306. assert.equal(w.isVisible(), false)
  307. })
  308. })
  309. describe('BrowserWindow.blur()', function () {
  310. it('removes focus from window', function () {
  311. w.blur()
  312. assert(!w.isFocused())
  313. })
  314. })
  315. describe('BrowserWindow.capturePage(rect, callback)', function () {
  316. it('calls the callback with a Buffer', function (done) {
  317. w.capturePage({
  318. x: 0,
  319. y: 0,
  320. width: 100,
  321. height: 100
  322. }, function (image) {
  323. assert.equal(image.isEmpty(), true)
  324. done()
  325. })
  326. })
  327. })
  328. describe('BrowserWindow.setSize(width, height)', function () {
  329. it('sets the window size', function (done) {
  330. var size = [300, 400]
  331. w.once('resize', function () {
  332. assertBoundsEqual(w.getSize(), size)
  333. done()
  334. })
  335. w.setSize(size[0], size[1])
  336. })
  337. })
  338. describe('BrowserWindow.setMinimum/MaximumSize(width, height)', function () {
  339. it('sets the maximum and minimum size of the window', function () {
  340. assert.deepEqual(w.getMinimumSize(), [0, 0])
  341. assert.deepEqual(w.getMaximumSize(), [0, 0])
  342. w.setMinimumSize(100, 100)
  343. assertBoundsEqual(w.getMinimumSize(), [100, 100])
  344. assertBoundsEqual(w.getMaximumSize(), [0, 0])
  345. w.setMaximumSize(900, 600)
  346. assertBoundsEqual(w.getMinimumSize(), [100, 100])
  347. assertBoundsEqual(w.getMaximumSize(), [900, 600])
  348. })
  349. })
  350. describe('BrowserWindow.setAspectRatio(ratio)', function () {
  351. it('resets the behaviour when passing in 0', function (done) {
  352. var size = [300, 400]
  353. w.setAspectRatio(1 / 2)
  354. w.setAspectRatio(0)
  355. w.once('resize', function () {
  356. assertBoundsEqual(w.getSize(), size)
  357. done()
  358. })
  359. w.setSize(size[0], size[1])
  360. })
  361. })
  362. describe('BrowserWindow.setPosition(x, y)', function () {
  363. it('sets the window position', function (done) {
  364. var pos = [10, 10]
  365. w.once('move', function () {
  366. var newPos = w.getPosition()
  367. assert.equal(newPos[0], pos[0])
  368. assert.equal(newPos[1], pos[1])
  369. done()
  370. })
  371. w.setPosition(pos[0], pos[1])
  372. })
  373. })
  374. describe('BrowserWindow.setContentSize(width, height)', function () {
  375. it('sets the content size', function () {
  376. var size = [400, 400]
  377. w.setContentSize(size[0], size[1])
  378. var after = w.getContentSize()
  379. assert.equal(after[0], size[0])
  380. assert.equal(after[1], size[1])
  381. })
  382. it('works for a frameless window', function () {
  383. w.destroy()
  384. w = new BrowserWindow({
  385. show: false,
  386. frame: false,
  387. width: 400,
  388. height: 400
  389. })
  390. var size = [400, 400]
  391. w.setContentSize(size[0], size[1])
  392. var after = w.getContentSize()
  393. assert.equal(after[0], size[0])
  394. assert.equal(after[1], size[1])
  395. })
  396. })
  397. describe('BrowserWindow.setContentBounds(bounds)', function () {
  398. it('sets the content size and position', function (done) {
  399. var bounds = {x: 10, y: 10, width: 250, height: 250}
  400. w.once('resize', function () {
  401. assertBoundsEqual(w.getContentBounds(), bounds)
  402. done()
  403. })
  404. w.setContentBounds(bounds)
  405. })
  406. it('works for a frameless window', function (done) {
  407. w.destroy()
  408. w = new BrowserWindow({
  409. show: false,
  410. frame: false,
  411. width: 300,
  412. height: 300
  413. })
  414. var bounds = {x: 10, y: 10, width: 250, height: 250}
  415. w.once('resize', function () {
  416. assert.deepEqual(w.getContentBounds(), bounds)
  417. done()
  418. })
  419. w.setContentBounds(bounds)
  420. })
  421. })
  422. describe('BrowserWindow.setProgressBar(progress)', function () {
  423. it('sets the progress', function () {
  424. assert.doesNotThrow(function () {
  425. if (process.platform === 'darwin') {
  426. app.dock.setIcon(path.join(fixtures, 'assets', 'logo.png'))
  427. }
  428. w.setProgressBar(0.5)
  429. if (process.platform === 'darwin') {
  430. app.dock.setIcon(null)
  431. }
  432. w.setProgressBar(-1)
  433. })
  434. })
  435. it('sets the progress using "paused" mode', function () {
  436. assert.doesNotThrow(function () {
  437. w.setProgressBar(0.5, {mode: 'paused'})
  438. })
  439. })
  440. it('sets the progress using "error" mode', function () {
  441. assert.doesNotThrow(function () {
  442. w.setProgressBar(0.5, {mode: 'error'})
  443. })
  444. })
  445. it('sets the progress using "normal" mode', function () {
  446. assert.doesNotThrow(function () {
  447. w.setProgressBar(0.5, {mode: 'normal'})
  448. })
  449. })
  450. })
  451. describe('BrowserWindow.setAlwaysOnTop(flag, level)', function () {
  452. it('sets the window as always on top', function () {
  453. assert.equal(w.isAlwaysOnTop(), false)
  454. w.setAlwaysOnTop(true, 'screen-saver')
  455. assert.equal(w.isAlwaysOnTop(), true)
  456. w.setAlwaysOnTop(false)
  457. assert.equal(w.isAlwaysOnTop(), false)
  458. w.setAlwaysOnTop(true)
  459. assert.equal(w.isAlwaysOnTop(), true)
  460. })
  461. })
  462. describe('BrowserWindow.setAutoHideCursor(autoHide)', () => {
  463. if (process.platform !== 'darwin') {
  464. it('is not available on non-macOS platforms', () => {
  465. assert.ok(!w.setAutoHideCursor)
  466. })
  467. return
  468. }
  469. it('allows changing cursor auto-hiding', () => {
  470. assert.doesNotThrow(() => {
  471. w.setAutoHideCursor(false)
  472. w.setAutoHideCursor(true)
  473. })
  474. })
  475. })
  476. describe('BrowserWindow.setVibrancy(type)', function () {
  477. it('allows setting, changing, and removing the vibrancy', function () {
  478. assert.doesNotThrow(function () {
  479. w.setVibrancy('light')
  480. w.setVibrancy('dark')
  481. w.setVibrancy(null)
  482. w.setVibrancy('ultra-dark')
  483. w.setVibrancy('')
  484. })
  485. })
  486. })
  487. describe('BrowserWindow.setAppDetails(options)', function () {
  488. it('supports setting the app details', function () {
  489. if (process.platform !== 'win32') return
  490. const iconPath = path.join(fixtures, 'assets', 'icon.ico')
  491. assert.doesNotThrow(function () {
  492. w.setAppDetails({appId: 'my.app.id'})
  493. w.setAppDetails({appIconPath: iconPath, appIconIndex: 0})
  494. w.setAppDetails({appIconPath: iconPath})
  495. w.setAppDetails({relaunchCommand: 'my-app.exe arg1 arg2', relaunchDisplayName: 'My app name'})
  496. w.setAppDetails({relaunchCommand: 'my-app.exe arg1 arg2'})
  497. w.setAppDetails({relaunchDisplayName: 'My app name'})
  498. w.setAppDetails({
  499. appId: 'my.app.id',
  500. appIconPath: iconPath,
  501. appIconIndex: 0,
  502. relaunchCommand: 'my-app.exe arg1 arg2',
  503. relaunchDisplayName: 'My app name'
  504. })
  505. w.setAppDetails({})
  506. })
  507. assert.throws(function () {
  508. w.setAppDetails()
  509. }, /Insufficient number of arguments\./)
  510. })
  511. })
  512. describe('BrowserWindow.fromId(id)', function () {
  513. it('returns the window with id', function () {
  514. assert.equal(w.id, BrowserWindow.fromId(w.id).id)
  515. })
  516. })
  517. describe('BrowserWindow.fromWebContents(webContents)', function () {
  518. let contents = null
  519. beforeEach(function () {
  520. contents = webContents.create({})
  521. })
  522. afterEach(function () {
  523. contents.destroy()
  524. })
  525. it('returns the window with the webContents', function () {
  526. assert.equal(BrowserWindow.fromWebContents(w.webContents).id, w.id)
  527. assert.equal(BrowserWindow.fromWebContents(contents), undefined)
  528. })
  529. })
  530. describe('BrowserWindow.fromDevToolsWebContents(webContents)', function () {
  531. let contents = null
  532. beforeEach(function () {
  533. contents = webContents.create({})
  534. })
  535. afterEach(function () {
  536. contents.destroy()
  537. })
  538. it('returns the window with the webContents', function (done) {
  539. w.webContents.once('devtools-opened', () => {
  540. assert.equal(BrowserWindow.fromDevToolsWebContents(w.devToolsWebContents).id, w.id)
  541. assert.equal(BrowserWindow.fromDevToolsWebContents(w.webContents), undefined)
  542. assert.equal(BrowserWindow.fromDevToolsWebContents(contents), undefined)
  543. done()
  544. })
  545. w.webContents.openDevTools()
  546. })
  547. })
  548. describe('"useContentSize" option', function () {
  549. it('make window created with content size when used', function () {
  550. w.destroy()
  551. w = new BrowserWindow({
  552. show: false,
  553. width: 400,
  554. height: 400,
  555. useContentSize: true
  556. })
  557. var contentSize = w.getContentSize()
  558. assert.equal(contentSize[0], 400)
  559. assert.equal(contentSize[1], 400)
  560. })
  561. it('make window created with window size when not used', function () {
  562. var size = w.getSize()
  563. assert.equal(size[0], 400)
  564. assert.equal(size[1], 400)
  565. })
  566. it('works for a frameless window', function () {
  567. w.destroy()
  568. w = new BrowserWindow({
  569. show: false,
  570. frame: false,
  571. width: 400,
  572. height: 400,
  573. useContentSize: true
  574. })
  575. var contentSize = w.getContentSize()
  576. assert.equal(contentSize[0], 400)
  577. assert.equal(contentSize[1], 400)
  578. var size = w.getSize()
  579. assert.equal(size[0], 400)
  580. assert.equal(size[1], 400)
  581. })
  582. })
  583. describe('"title-bar-style" option', function () {
  584. if (process.platform !== 'darwin') {
  585. return
  586. }
  587. if (parseInt(os.release().split('.')[0]) < 14) {
  588. return
  589. }
  590. it('creates browser window with hidden title bar', function () {
  591. w.destroy()
  592. w = new BrowserWindow({
  593. show: false,
  594. width: 400,
  595. height: 400,
  596. titleBarStyle: 'hidden'
  597. })
  598. var contentSize = w.getContentSize()
  599. assert.equal(contentSize[1], 400)
  600. })
  601. it('creates browser window with hidden inset title bar', function () {
  602. w.destroy()
  603. w = new BrowserWindow({
  604. show: false,
  605. width: 400,
  606. height: 400,
  607. titleBarStyle: 'hidden-inset'
  608. })
  609. var contentSize = w.getContentSize()
  610. assert.equal(contentSize[1], 400)
  611. })
  612. })
  613. describe('enableLargerThanScreen" option', function () {
  614. if (process.platform === 'linux') {
  615. return
  616. }
  617. beforeEach(function () {
  618. w.destroy()
  619. w = new BrowserWindow({
  620. show: true,
  621. width: 400,
  622. height: 400,
  623. enableLargerThanScreen: true
  624. })
  625. })
  626. it('can move the window out of screen', function () {
  627. w.setPosition(-10, -10)
  628. var after = w.getPosition()
  629. assert.equal(after[0], -10)
  630. assert.equal(after[1], -10)
  631. })
  632. it('can set the window larger than screen', function () {
  633. var size = screen.getPrimaryDisplay().size
  634. size.width += 100
  635. size.height += 100
  636. w.setSize(size.width, size.height)
  637. assertBoundsEqual(w.getSize(), [size.width, size.height])
  638. })
  639. })
  640. describe('"zoomToPageWidth" option', function () {
  641. it('sets the window width to the page width when used', function () {
  642. if (process.platform !== 'darwin') return
  643. w.destroy()
  644. w = new BrowserWindow({
  645. show: false,
  646. width: 500,
  647. height: 400,
  648. zoomToPageWidth: true
  649. })
  650. w.maximize()
  651. assert.equal(w.getSize()[0], 500)
  652. })
  653. })
  654. describe('"web-preferences" option', function () {
  655. afterEach(function () {
  656. ipcMain.removeAllListeners('answer')
  657. })
  658. describe('"preload" option', function () {
  659. it('loads the script before other scripts in window', function (done) {
  660. var preload = path.join(fixtures, 'module', 'set-global.js')
  661. ipcMain.once('answer', function (event, test) {
  662. assert.equal(test, 'preload')
  663. done()
  664. })
  665. w.destroy()
  666. w = new BrowserWindow({
  667. show: false,
  668. webPreferences: {
  669. preload: preload
  670. }
  671. })
  672. w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
  673. })
  674. it('can successfully delete the Buffer global', function (done) {
  675. var preload = path.join(fixtures, 'module', 'delete-buffer.js')
  676. ipcMain.once('answer', function (event, test) {
  677. assert.equal(test.toString(), 'buffer')
  678. done()
  679. })
  680. w.destroy()
  681. w = new BrowserWindow({
  682. show: false,
  683. webPreferences: {
  684. preload: preload
  685. }
  686. })
  687. w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
  688. })
  689. })
  690. describe('"node-integration" option', function () {
  691. it('disables node integration when specified to false', function (done) {
  692. var preload = path.join(fixtures, 'module', 'send-later.js')
  693. ipcMain.once('answer', function (event, test) {
  694. assert.equal(test, 'undefined')
  695. done()
  696. })
  697. w.destroy()
  698. w = new BrowserWindow({
  699. show: false,
  700. webPreferences: {
  701. preload: preload,
  702. nodeIntegration: false
  703. }
  704. })
  705. w.loadURL('file://' + path.join(fixtures, 'api', 'blank.html'))
  706. })
  707. })
  708. describe('"sandbox" option', function () {
  709. function waitForEvents (emitter, events, callback) {
  710. let count = events.length
  711. for (let event of events) {
  712. emitter.once(event, () => {
  713. if (!--count) callback()
  714. })
  715. }
  716. }
  717. const preload = path.join(fixtures, 'module', 'preload-sandbox.js')
  718. // http protocol to simulate accessing another domain. This is required
  719. // because the code paths for cross domain popups is different.
  720. function crossDomainHandler (request, callback) {
  721. callback({
  722. mimeType: 'text/html',
  723. data: `<html><body><h1>${request.url}</h1></body></html>`
  724. })
  725. }
  726. before(function (done) {
  727. protocol.interceptStringProtocol('http', crossDomainHandler, function () {
  728. done()
  729. })
  730. })
  731. after(function (done) {
  732. protocol.uninterceptProtocol('http', function () {
  733. done()
  734. })
  735. })
  736. it('exposes ipcRenderer to preload script', function (done) {
  737. ipcMain.once('answer', function (event, test) {
  738. assert.equal(test, 'preload')
  739. done()
  740. })
  741. w.destroy()
  742. w = new BrowserWindow({
  743. show: false,
  744. webPreferences: {
  745. sandbox: true,
  746. preload: preload
  747. }
  748. })
  749. w.loadURL('file://' + path.join(fixtures, 'api', 'preload.html'))
  750. })
  751. it('exposes "exit" event to preload script', function (done) {
  752. w.destroy()
  753. w = new BrowserWindow({
  754. show: false,
  755. webPreferences: {
  756. sandbox: true,
  757. preload: preload
  758. }
  759. })
  760. let htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
  761. const pageUrl = 'file://' + htmlPath
  762. w.loadURL(pageUrl)
  763. ipcMain.once('answer', function (event, url) {
  764. let expectedUrl = pageUrl
  765. if (process.platform === 'win32') {
  766. expectedUrl = 'file:///' + htmlPath.replace(/\\/g, '/')
  767. }
  768. assert.equal(url, expectedUrl)
  769. done()
  770. })
  771. })
  772. it('should open windows in same domain with cross-scripting enabled', function (done) {
  773. w.destroy()
  774. w = new BrowserWindow({
  775. show: false,
  776. webPreferences: {
  777. sandbox: true,
  778. preload: preload
  779. }
  780. })
  781. let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open')
  782. const pageUrl = 'file://' + htmlPath
  783. w.loadURL(pageUrl)
  784. w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
  785. let expectedUrl = pageUrl
  786. if (process.platform === 'win32') {
  787. expectedUrl = 'file:///' + htmlPath.replace(/\\/g, '/')
  788. }
  789. assert.equal(url, expectedUrl)
  790. assert.equal(frameName, 'popup!')
  791. assert.equal(options.x, 50)
  792. assert.equal(options.y, 60)
  793. assert.equal(options.width, 500)
  794. assert.equal(options.height, 600)
  795. ipcMain.once('answer', function (event, html) {
  796. assert.equal(html, '<h1>scripting from opener</h1>')
  797. done()
  798. })
  799. })
  800. })
  801. it('should open windows in another domain with cross-scripting disabled', function (done) {
  802. w.destroy()
  803. w = new BrowserWindow({
  804. show: false,
  805. webPreferences: {
  806. sandbox: true,
  807. preload: preload
  808. }
  809. })
  810. let htmlPath = path.join(fixtures, 'api', 'sandbox.html?window-open-external')
  811. const pageUrl = 'file://' + htmlPath
  812. let popupWindow
  813. w.loadURL(pageUrl)
  814. w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
  815. assert.equal(url, 'http://www.google.com/#q=electron')
  816. assert.equal(options.x, 55)
  817. assert.equal(options.y, 65)
  818. assert.equal(options.width, 505)
  819. assert.equal(options.height, 605)
  820. ipcMain.once('child-loaded', function (event, openerIsNull, html) {
  821. assert(openerIsNull)
  822. assert.equal(html, '<h1>http://www.google.com/#q=electron</h1>')
  823. ipcMain.once('answer', function (event, exceptionMessage) {
  824. assert(/Blocked a frame with origin/.test(exceptionMessage))
  825. // FIXME this popup window should be closed in sandbox.html
  826. closeWindow(popupWindow, {assertSingleWindow: false}).then(() => {
  827. popupWindow = null
  828. done()
  829. })
  830. })
  831. w.webContents.send('child-loaded')
  832. })
  833. })
  834. app.once('browser-window-created', function (event, window) {
  835. popupWindow = window
  836. })
  837. })
  838. it('should set ipc event sender correctly', function (done) {
  839. w.destroy()
  840. w = new BrowserWindow({
  841. show: false,
  842. webPreferences: {
  843. sandbox: true,
  844. preload: preload
  845. }
  846. })
  847. let htmlPath = path.join(fixtures, 'api', 'sandbox.html?verify-ipc-sender')
  848. const pageUrl = 'file://' + htmlPath
  849. w.loadURL(pageUrl)
  850. w.webContents.once('new-window', (e, url, frameName, disposition, options) => {
  851. let parentWc = w.webContents
  852. let childWc = options.webContents
  853. assert.notEqual(parentWc, childWc)
  854. ipcMain.once('parent-ready', function (event) {
  855. assert.equal(parentWc, event.sender)
  856. parentWc.send('verified')
  857. })
  858. ipcMain.once('child-ready', function (event) {
  859. assert.equal(childWc, event.sender)
  860. childWc.send('verified')
  861. })
  862. waitForEvents(ipcMain, [
  863. 'parent-answer',
  864. 'child-answer'
  865. ], done)
  866. })
  867. })
  868. describe('event handling', function () {
  869. it('works for window events', function (done) {
  870. waitForEvents(w, [
  871. 'page-title-updated'
  872. ], done)
  873. w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?window-events'))
  874. })
  875. it('works for web contents events', function (done) {
  876. waitForEvents(w.webContents, [
  877. 'did-navigate',
  878. 'did-fail-load',
  879. 'did-stop-loading'
  880. ], done)
  881. w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-stop'))
  882. waitForEvents(w.webContents, [
  883. 'did-finish-load',
  884. 'did-frame-finish-load',
  885. 'did-navigate-in-page',
  886. 'will-navigate',
  887. 'did-start-loading',
  888. 'did-stop-loading',
  889. 'did-frame-finish-load',
  890. 'dom-ready'
  891. ], done)
  892. w.loadURL('file://' + path.join(fixtures, 'api', 'sandbox.html?webcontents-events'))
  893. })
  894. })
  895. it('can print to PDF', function (done) {
  896. w.destroy()
  897. w = new BrowserWindow({
  898. show: false,
  899. webPreferences: {
  900. sandbox: true,
  901. preload: preload
  902. }
  903. })
  904. w.loadURL('data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E')
  905. w.webContents.once('did-finish-load', function () {
  906. w.webContents.printToPDF({}, function (error, data) {
  907. assert.equal(error, null)
  908. assert.equal(data instanceof Buffer, true)
  909. assert.notEqual(data.length, 0)
  910. done()
  911. })
  912. })
  913. })
  914. it('supports calling preventDefault on new-window events', (done) => {
  915. w.destroy()
  916. w = new BrowserWindow({
  917. show: false,
  918. webPreferences: {
  919. sandbox: true
  920. }
  921. })
  922. const initialWebContents = webContents.getAllWebContents()
  923. ipcRenderer.send('prevent-next-new-window', w.webContents.id)
  924. w.webContents.once('new-window', () => {
  925. assert.deepEqual(webContents.getAllWebContents(), initialWebContents)
  926. done()
  927. })
  928. w.loadURL('file://' + path.join(fixtures, 'pages', 'window-open.html'))
  929. })
  930. })
  931. })
  932. describe('beforeunload handler', function () {
  933. it('returning undefined would not prevent close', function (done) {
  934. w.once('closed', function () {
  935. done()
  936. })
  937. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-undefined.html'))
  938. })
  939. it('returning false would prevent close', function (done) {
  940. w.once('onbeforeunload', function () {
  941. done()
  942. })
  943. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-false.html'))
  944. })
  945. it('returning empty string would prevent close', function (done) {
  946. w.once('onbeforeunload', function () {
  947. done()
  948. })
  949. w.loadURL('file://' + path.join(fixtures, 'api', 'close-beforeunload-empty-string.html'))
  950. })
  951. })
  952. describe('new-window event', function () {
  953. if (isCI && process.platform === 'darwin') {
  954. return
  955. }
  956. it('emits when window.open is called', function (done) {
  957. w.webContents.once('new-window', function (e, url, frameName, disposition, options, additionalFeatures) {
  958. e.preventDefault()
  959. assert.equal(url, 'http://host/')
  960. assert.equal(frameName, 'host')
  961. assert.equal(additionalFeatures[0], 'this-is-not-a-standard-feature')
  962. done()
  963. })
  964. w.loadURL('file://' + fixtures + '/pages/window-open.html')
  965. })
  966. it('emits when window.open is called with no webPreferences', function (done) {
  967. w.destroy()
  968. w = new BrowserWindow({ show: false })
  969. w.webContents.once('new-window', function (e, url, frameName, disposition, options, additionalFeatures) {
  970. e.preventDefault()
  971. assert.equal(url, 'http://host/')
  972. assert.equal(frameName, 'host')
  973. assert.equal(additionalFeatures[0], 'this-is-not-a-standard-feature')
  974. done()
  975. })
  976. w.loadURL('file://' + fixtures + '/pages/window-open.html')
  977. })
  978. it('emits when link with target is called', function (done) {
  979. w.webContents.once('new-window', function (e, url, frameName) {
  980. e.preventDefault()
  981. assert.equal(url, 'http://host/')
  982. assert.equal(frameName, 'target')
  983. done()
  984. })
  985. w.loadURL('file://' + fixtures + '/pages/target-name.html')
  986. })
  987. })
  988. describe('maximize event', function () {
  989. if (isCI) {
  990. return
  991. }
  992. it('emits when window is maximized', function (done) {
  993. w.once('maximize', function () {
  994. done()
  995. })
  996. w.show()
  997. w.maximize()
  998. })
  999. })
  1000. describe('unmaximize event', function () {
  1001. if (isCI) {
  1002. return
  1003. }
  1004. it('emits when window is unmaximized', function (done) {
  1005. w.once('unmaximize', function () {
  1006. done()
  1007. })
  1008. w.show()
  1009. w.maximize()
  1010. w.unmaximize()
  1011. })
  1012. })
  1013. describe('minimize event', function () {
  1014. if (isCI) {
  1015. return
  1016. }
  1017. it('emits when window is minimized', function (done) {
  1018. w.once('minimize', function () {
  1019. done()
  1020. })
  1021. w.show()
  1022. w.minimize()
  1023. })
  1024. })
  1025. describe('beginFrameSubscription method', function () {
  1026. // This test is too slow, only test it on CI.
  1027. if (!isCI) return
  1028. it('subscribes to frame updates', function (done) {
  1029. let called = false
  1030. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  1031. w.webContents.on('dom-ready', function () {
  1032. w.webContents.beginFrameSubscription(function (data) {
  1033. // This callback might be called twice.
  1034. if (called) return
  1035. called = true
  1036. assert.notEqual(data.length, 0)
  1037. w.webContents.endFrameSubscription()
  1038. done()
  1039. })
  1040. })
  1041. })
  1042. it('subscribes to frame updates (only dirty rectangle)', function (done) {
  1043. let called = false
  1044. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  1045. w.webContents.on('dom-ready', function () {
  1046. w.webContents.beginFrameSubscription(true, function (data) {
  1047. // This callback might be called twice.
  1048. if (called) return
  1049. called = true
  1050. assert.notEqual(data.length, 0)
  1051. w.webContents.endFrameSubscription()
  1052. done()
  1053. })
  1054. })
  1055. })
  1056. it('throws error when subscriber is not well defined', function (done) {
  1057. w.loadURL('file://' + fixtures + '/api/frame-subscriber.html')
  1058. try {
  1059. w.webContents.beginFrameSubscription(true, true)
  1060. } catch (e) {
  1061. done()
  1062. }
  1063. })
  1064. })
  1065. describe('savePage method', function () {
  1066. const savePageDir = path.join(fixtures, 'save_page')
  1067. const savePageHtmlPath = path.join(savePageDir, 'save_page.html')
  1068. const savePageJsPath = path.join(savePageDir, 'save_page_files', 'test.js')
  1069. const savePageCssPath = path.join(savePageDir, 'save_page_files', 'test.css')
  1070. after(function () {
  1071. try {
  1072. fs.unlinkSync(savePageCssPath)
  1073. fs.unlinkSync(savePageJsPath)
  1074. fs.unlinkSync(savePageHtmlPath)
  1075. fs.rmdirSync(path.join(savePageDir, 'save_page_files'))
  1076. fs.rmdirSync(savePageDir)
  1077. } catch (e) {
  1078. // Ignore error
  1079. }
  1080. })
  1081. it('should save page to disk', function (done) {
  1082. w.webContents.on('did-finish-load', function () {
  1083. w.webContents.savePage(savePageHtmlPath, 'HTMLComplete', function (error) {
  1084. assert.equal(error, null)
  1085. assert(fs.existsSync(savePageHtmlPath))
  1086. assert(fs.existsSync(savePageJsPath))
  1087. assert(fs.existsSync(savePageCssPath))
  1088. done()
  1089. })
  1090. })
  1091. w.loadURL('file://' + fixtures + '/pages/save_page/index.html')
  1092. })
  1093. })
  1094. describe('BrowserWindow options argument is optional', function () {
  1095. it('should create a window with default size (800x600)', function () {
  1096. w.destroy()
  1097. w = new BrowserWindow()
  1098. var size = w.getSize()
  1099. assert.equal(size[0], 800)
  1100. assert.equal(size[1], 600)
  1101. })
  1102. })
  1103. describe('window states', function () {
  1104. it('does not resize frameless windows when states change', function () {
  1105. w.destroy()
  1106. w = new BrowserWindow({
  1107. frame: false,
  1108. width: 300,
  1109. height: 200,
  1110. show: false
  1111. })
  1112. w.setMinimizable(false)
  1113. w.setMinimizable(true)
  1114. assert.deepEqual(w.getSize(), [300, 200])
  1115. w.setResizable(false)
  1116. w.setResizable(true)
  1117. assert.deepEqual(w.getSize(), [300, 200])
  1118. w.setMaximizable(false)
  1119. w.setMaximizable(true)
  1120. assert.deepEqual(w.getSize(), [300, 200])
  1121. w.setFullScreenable(false)
  1122. w.setFullScreenable(true)
  1123. assert.deepEqual(w.getSize(), [300, 200])
  1124. w.setClosable(false)
  1125. w.setClosable(true)
  1126. assert.deepEqual(w.getSize(), [300, 200])
  1127. })
  1128. describe('resizable state', function () {
  1129. it('can be changed with resizable option', function () {
  1130. w.destroy()
  1131. w = new BrowserWindow({show: false, resizable: false})
  1132. assert.equal(w.isResizable(), false)
  1133. if (process.platform === 'darwin') {
  1134. assert.equal(w.isMaximizable(), true)
  1135. }
  1136. })
  1137. it('can be changed with setResizable method', function () {
  1138. assert.equal(w.isResizable(), true)
  1139. w.setResizable(false)
  1140. assert.equal(w.isResizable(), false)
  1141. w.setResizable(true)
  1142. assert.equal(w.isResizable(), true)
  1143. })
  1144. it('works for a frameless window', () => {
  1145. w.destroy()
  1146. w = new BrowserWindow({show: false, frame: false})
  1147. assert.equal(w.isResizable(), true)
  1148. if (process.platform === 'win32') {
  1149. w.destroy()
  1150. w = new BrowserWindow({show: false, thickFrame: false})
  1151. assert.equal(w.isResizable(), false)
  1152. }
  1153. })
  1154. })
  1155. describe('loading main frame state', function () {
  1156. it('is true when the main frame is loading', function (done) {
  1157. w.webContents.on('did-start-loading', function () {
  1158. assert.equal(w.webContents.isLoadingMainFrame(), true)
  1159. done()
  1160. })
  1161. w.webContents.loadURL(server.url)
  1162. })
  1163. it('is false when only a subframe is loading', function (done) {
  1164. w.webContents.once('did-finish-load', function () {
  1165. assert.equal(w.webContents.isLoadingMainFrame(), false)
  1166. w.webContents.on('did-start-loading', function () {
  1167. assert.equal(w.webContents.isLoadingMainFrame(), false)
  1168. done()
  1169. })
  1170. w.webContents.executeJavaScript(`
  1171. var iframe = document.createElement('iframe')
  1172. iframe.src = '${server.url}/page2'
  1173. document.body.appendChild(iframe)
  1174. `)
  1175. })
  1176. w.webContents.loadURL(server.url)
  1177. })
  1178. it('is true when navigating to pages from the same origin', function (done) {
  1179. w.webContents.once('did-finish-load', function () {
  1180. assert.equal(w.webContents.isLoadingMainFrame(), false)
  1181. w.webContents.on('did-start-loading', function () {
  1182. assert.equal(w.webContents.isLoadingMainFrame(), true)
  1183. done()
  1184. })
  1185. w.webContents.loadURL(`${server.url}/page2`)
  1186. })
  1187. w.webContents.loadURL(server.url)
  1188. })
  1189. })
  1190. })
  1191. describe('window states (excluding Linux)', function () {
  1192. // Not implemented on Linux.
  1193. if (process.platform === 'linux') return
  1194. describe('movable state', function () {
  1195. it('can be changed with movable option', function () {
  1196. w.destroy()
  1197. w = new BrowserWindow({show: false, movable: false})
  1198. assert.equal(w.isMovable(), false)
  1199. })
  1200. it('can be changed with setMovable method', function () {
  1201. assert.equal(w.isMovable(), true)
  1202. w.setMovable(false)
  1203. assert.equal(w.isMovable(), false)
  1204. w.setMovable(true)
  1205. assert.equal(w.isMovable(), true)
  1206. })
  1207. })
  1208. describe('minimizable state', function () {
  1209. it('can be changed with minimizable option', function () {
  1210. w.destroy()
  1211. w = new BrowserWindow({show: false, minimizable: false})
  1212. assert.equal(w.isMinimizable(), false)
  1213. })
  1214. it('can be changed with setMinimizable method', function () {
  1215. assert.equal(w.isMinimizable(), true)
  1216. w.setMinimizable(false)
  1217. assert.equal(w.isMinimizable(), false)
  1218. w.setMinimizable(true)
  1219. assert.equal(w.isMinimizable(), true)
  1220. })
  1221. })
  1222. describe('maximizable state', function () {
  1223. it('can be changed with maximizable option', function () {
  1224. w.destroy()
  1225. w = new BrowserWindow({show: false, maximizable: false})
  1226. assert.equal(w.isMaximizable(), false)
  1227. })
  1228. it('can be changed with setMaximizable method', function () {
  1229. assert.equal(w.isMaximizable(), true)
  1230. w.setMaximizable(false)
  1231. assert.equal(w.isMaximizable(), false)
  1232. w.setMaximizable(true)
  1233. assert.equal(w.isMaximizable(), true)
  1234. })
  1235. it('is not affected when changing other states', function () {
  1236. w.setMaximizable(false)
  1237. assert.equal(w.isMaximizable(), false)
  1238. w.setMinimizable(false)
  1239. assert.equal(w.isMaximizable(), false)
  1240. w.setClosable(false)
  1241. assert.equal(w.isMaximizable(), false)
  1242. w.setMaximizable(true)
  1243. assert.equal(w.isMaximizable(), true)
  1244. w.setClosable(true)
  1245. assert.equal(w.isMaximizable(), true)
  1246. w.setFullScreenable(false)
  1247. assert.equal(w.isMaximizable(), true)
  1248. w.setResizable(false)
  1249. assert.equal(w.isMaximizable(), true)
  1250. })
  1251. })
  1252. describe('fullscreenable state', function () {
  1253. // Only implemented on macOS.
  1254. if (process.platform !== 'darwin') return
  1255. it('can be changed with fullscreenable option', function () {
  1256. w.destroy()
  1257. w = new BrowserWindow({show: false, fullscreenable: false})
  1258. assert.equal(w.isFullScreenable(), false)
  1259. })
  1260. it('can be changed with setFullScreenable method', function () {
  1261. assert.equal(w.isFullScreenable(), true)
  1262. w.setFullScreenable(false)
  1263. assert.equal(w.isFullScreenable(), false)
  1264. w.setFullScreenable(true)
  1265. assert.equal(w.isFullScreenable(), true)
  1266. })
  1267. })
  1268. describe('kiosk state', function () {
  1269. // Only implemented on macOS.
  1270. if (process.platform !== 'darwin') return
  1271. it('can be changed with setKiosk method', function () {
  1272. w.destroy()
  1273. w = new BrowserWindow()
  1274. w.setKiosk(true)
  1275. assert.equal(w.isKiosk(), true)
  1276. w.setKiosk(false)
  1277. assert.equal(w.isKiosk(), false)
  1278. })
  1279. })
  1280. describe('fullscreen state', function () {
  1281. // Only implemented on macOS.
  1282. if (process.platform !== 'darwin') return
  1283. it('can be changed with setFullScreen method', function () {
  1284. w.destroy()
  1285. w = new BrowserWindow()
  1286. w.setFullScreen(true)
  1287. assert.equal(w.isFullScreen(), true)
  1288. w.setFullScreen(false)
  1289. assert.equal(w.isFullScreen(), false)
  1290. })
  1291. it('should not be changed by setKiosk method', function () {
  1292. w.setFullScreen(true)
  1293. assert.equal(w.isFullScreen(), true)
  1294. w.setKiosk(true)
  1295. w.setKiosk(false)
  1296. assert.equal(w.isFullScreen(), true)
  1297. })
  1298. })
  1299. describe('closable state', function () {
  1300. it('can be changed with closable option', function () {
  1301. w.destroy()
  1302. w = new BrowserWindow({show: false, closable: false})
  1303. assert.equal(w.isClosable(), false)
  1304. })
  1305. it('can be changed with setClosable method', function () {
  1306. assert.equal(w.isClosable(), true)
  1307. w.setClosable(false)
  1308. assert.equal(w.isClosable(), false)
  1309. w.setClosable(true)
  1310. assert.equal(w.isClosable(), true)
  1311. })
  1312. })
  1313. describe('hasShadow state', function () {
  1314. // On Window there is no shadow by default and it can not be changed
  1315. // dynamically.
  1316. it('can be changed with hasShadow option', function () {
  1317. w.destroy()
  1318. let hasShadow = process.platform !== 'darwin'
  1319. w = new BrowserWindow({show: false, hasShadow: hasShadow})
  1320. assert.equal(w.hasShadow(), hasShadow)
  1321. })
  1322. it('can be changed with setHasShadow method', function () {
  1323. if (process.platform !== 'darwin') return
  1324. assert.equal(w.hasShadow(), true)
  1325. w.setHasShadow(false)
  1326. assert.equal(w.hasShadow(), false)
  1327. w.setHasShadow(true)
  1328. assert.equal(w.hasShadow(), true)
  1329. })
  1330. })
  1331. })
  1332. describe('BrowserWindow.restore()', function () {
  1333. it('should restore the previous window size', function () {
  1334. if (w != null) w.destroy()
  1335. w = new BrowserWindow({
  1336. minWidth: 800,
  1337. width: 800
  1338. })
  1339. const initialSize = w.getSize()
  1340. w.minimize()
  1341. w.restore()
  1342. assertBoundsEqual(w.getSize(), initialSize)
  1343. })
  1344. })
  1345. describe('BrowserWindow.unmaximize()', function () {
  1346. it('should restore the previous window position', function () {
  1347. if (w != null) w.destroy()
  1348. w = new BrowserWindow()
  1349. const initialPosition = w.getPosition()
  1350. w.maximize()
  1351. w.unmaximize()
  1352. assertBoundsEqual(w.getPosition(), initialPosition)
  1353. })
  1354. })
  1355. describe('parent window', function () {
  1356. let c = null
  1357. beforeEach(function () {
  1358. if (c != null) c.destroy()
  1359. c = new BrowserWindow({show: false, parent: w})
  1360. })
  1361. afterEach(function () {
  1362. if (c != null) c.destroy()
  1363. c = null
  1364. })
  1365. describe('parent option', function () {
  1366. it('sets parent window', function () {
  1367. assert.equal(c.getParentWindow(), w)
  1368. })
  1369. it('adds window to child windows of parent', function () {
  1370. assert.deepEqual(w.getChildWindows(), [c])
  1371. })
  1372. it('removes from child windows of parent when window is closed', function (done) {
  1373. c.once('closed', () => {
  1374. assert.deepEqual(w.getChildWindows(), [])
  1375. done()
  1376. })
  1377. c.close()
  1378. })
  1379. })
  1380. describe('win.setParentWindow(parent)', function () {
  1381. if (process.platform === 'win32') return
  1382. beforeEach(function () {
  1383. if (c != null) c.destroy()
  1384. c = new BrowserWindow({show: false})
  1385. })
  1386. it('sets parent window', function () {
  1387. assert.equal(w.getParentWindow(), null)
  1388. assert.equal(c.getParentWindow(), null)
  1389. c.setParentWindow(w)
  1390. assert.equal(c.getParentWindow(), w)
  1391. c.setParentWindow(null)
  1392. assert.equal(c.getParentWindow(), null)
  1393. })
  1394. it('adds window to child windows of parent', function () {
  1395. assert.deepEqual(w.getChildWindows(), [])
  1396. c.setParentWindow(w)
  1397. assert.deepEqual(w.getChildWindows(), [c])
  1398. c.setParentWindow(null)
  1399. assert.deepEqual(w.getChildWindows(), [])
  1400. })
  1401. it('removes from child windows of parent when window is closed', function (done) {
  1402. c.once('closed', () => {
  1403. assert.deepEqual(w.getChildWindows(), [])
  1404. done()
  1405. })
  1406. c.setParentWindow(w)
  1407. c.close()
  1408. })
  1409. })
  1410. describe('modal option', function () {
  1411. // The isEnabled API is not reliable on macOS.
  1412. if (process.platform === 'darwin') return
  1413. beforeEach(function () {
  1414. if (c != null) c.destroy()
  1415. c = new BrowserWindow({show: false, parent: w, modal: true})
  1416. })
  1417. it('disables parent window', function () {
  1418. assert.equal(w.isEnabled(), true)
  1419. c.show()
  1420. assert.equal(w.isEnabled(), false)
  1421. })
  1422. it('enables parent window when closed', function (done) {
  1423. c.once('closed', () => {
  1424. assert.equal(w.isEnabled(), true)
  1425. done()
  1426. })
  1427. c.show()
  1428. c.close()
  1429. })
  1430. it('disables parent window recursively', function () {
  1431. let c2 = new BrowserWindow({show: false, parent: w, modal: true})
  1432. c.show()
  1433. assert.equal(w.isEnabled(), false)
  1434. c2.show()
  1435. assert.equal(w.isEnabled(), false)
  1436. c.destroy()
  1437. assert.equal(w.isEnabled(), false)
  1438. c2.destroy()
  1439. assert.equal(w.isEnabled(), true)
  1440. })
  1441. })
  1442. })
  1443. describe('window.webContents.send(channel, args...)', function () {
  1444. it('throws an error when the channel is missing', function () {
  1445. assert.throws(function () {
  1446. w.webContents.send()
  1447. }, 'Missing required channel argument')
  1448. assert.throws(function () {
  1449. w.webContents.send(null)
  1450. }, 'Missing required channel argument')
  1451. })
  1452. })
  1453. describe('dev tool extensions', function () {
  1454. describe('BrowserWindow.addDevToolsExtension', function () {
  1455. let showPanelIntervalId
  1456. beforeEach(function () {
  1457. BrowserWindow.removeDevToolsExtension('foo')
  1458. assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), false)
  1459. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
  1460. BrowserWindow.addDevToolsExtension(extensionPath)
  1461. assert.equal(BrowserWindow.getDevToolsExtensions().hasOwnProperty('foo'), true)
  1462. w.webContents.on('devtools-opened', function () {
  1463. showPanelIntervalId = setInterval(function () {
  1464. if (w && w.devToolsWebContents) {
  1465. var showLastPanel = function () {
  1466. var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id
  1467. WebInspector.inspectorView.showPanel(lastPanelId)
  1468. }
  1469. w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`)
  1470. } else {
  1471. clearInterval(showPanelIntervalId)
  1472. }
  1473. }, 100)
  1474. })
  1475. w.loadURL('about:blank')
  1476. })
  1477. afterEach(function () {
  1478. clearInterval(showPanelIntervalId)
  1479. })
  1480. it('throws errors for missing manifest.json files', function () {
  1481. assert.throws(function () {
  1482. BrowserWindow.addDevToolsExtension(path.join(__dirname, 'does-not-exist'))
  1483. }, /ENOENT: no such file or directory/)
  1484. })
  1485. it('throws errors for invalid manifest.json files', function () {
  1486. assert.throws(function () {
  1487. BrowserWindow.addDevToolsExtension(path.join(__dirname, 'fixtures', 'devtools-extensions', 'bad-manifest'))
  1488. }, /Unexpected token }/)
  1489. })
  1490. describe('when the devtools is docked', function () {
  1491. it('creates the extension', function (done) {
  1492. w.webContents.openDevTools({mode: 'bottom'})
  1493. ipcMain.once('answer', function (event, message) {
  1494. assert.equal(message.runtimeId, 'foo')
  1495. assert.equal(message.tabId, w.webContents.id)
  1496. assert.equal(message.i18nString, 'foo - bar (baz)')
  1497. assert.deepEqual(message.storageItems, {
  1498. local: {
  1499. set: {hello: 'world', world: 'hello'},
  1500. remove: {world: 'hello'},
  1501. clear: {}
  1502. },
  1503. sync: {
  1504. set: {foo: 'bar', bar: 'foo'},
  1505. remove: {foo: 'bar'},
  1506. clear: {}
  1507. }
  1508. })
  1509. done()
  1510. })
  1511. })
  1512. })
  1513. describe('when the devtools is undocked', function () {
  1514. it('creates the extension', function (done) {
  1515. w.webContents.openDevTools({mode: 'undocked'})
  1516. ipcMain.once('answer', function (event, message, extensionId) {
  1517. assert.equal(message.runtimeId, 'foo')
  1518. assert.equal(message.tabId, w.webContents.id)
  1519. done()
  1520. })
  1521. })
  1522. })
  1523. })
  1524. it('works when used with partitions', function (done) {
  1525. if (w != null) {
  1526. w.destroy()
  1527. }
  1528. w = new BrowserWindow({
  1529. show: false,
  1530. webPreferences: {
  1531. partition: 'temp'
  1532. }
  1533. })
  1534. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
  1535. BrowserWindow.removeDevToolsExtension('foo')
  1536. BrowserWindow.addDevToolsExtension(extensionPath)
  1537. let showPanelIntervalId = null
  1538. w.webContents.once('devtools-opened', function () {
  1539. showPanelIntervalId = setInterval(function () {
  1540. if (w && w.devToolsWebContents) {
  1541. var showLastPanel = function () {
  1542. var lastPanelId = WebInspector.inspectorView._tabbedPane._tabs.peekLast().id
  1543. WebInspector.inspectorView.showPanel(lastPanelId)
  1544. }
  1545. w.devToolsWebContents.executeJavaScript(`(${showLastPanel})()`)
  1546. } else {
  1547. clearInterval(showPanelIntervalId)
  1548. }
  1549. }, 100)
  1550. })
  1551. w.loadURL('about:blank')
  1552. w.webContents.openDevTools({mode: 'bottom'})
  1553. ipcMain.once('answer', function (event, message) {
  1554. clearInterval(showPanelIntervalId)
  1555. assert.equal(message.runtimeId, 'foo')
  1556. done()
  1557. })
  1558. })
  1559. it('serializes the registered extensions on quit', function () {
  1560. var extensionName = 'foo'
  1561. var extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', extensionName)
  1562. var serializedPath = path.join(app.getPath('userData'), 'DevTools Extensions')
  1563. BrowserWindow.addDevToolsExtension(extensionPath)
  1564. app.emit('will-quit')
  1565. assert.deepEqual(JSON.parse(fs.readFileSync(serializedPath)), [extensionPath])
  1566. BrowserWindow.removeDevToolsExtension(extensionName)
  1567. app.emit('will-quit')
  1568. assert.equal(fs.existsSync(serializedPath), false)
  1569. })
  1570. })
  1571. describe('window.webContents.executeJavaScript', function () {
  1572. var expected = 'hello, world!'
  1573. var expectedErrorMsg = 'woops!'
  1574. var code = `(() => "${expected}")()`
  1575. var asyncCode = `(() => new Promise(r => setTimeout(() => r("${expected}"), 500)))()`
  1576. var badAsyncCode = `(() => new Promise((r, e) => setTimeout(() => e("${expectedErrorMsg}"), 500)))()`
  1577. it('doesnt throw when no calback is provided', function () {
  1578. const result = ipcRenderer.sendSync('executeJavaScript', code, false)
  1579. assert.equal(result, 'success')
  1580. })
  1581. it('returns result when calback is provided', function (done) {
  1582. ipcRenderer.send('executeJavaScript', code, true)
  1583. ipcRenderer.once('executeJavaScript-response', function (event, result) {
  1584. assert.equal(result, expected)
  1585. done()
  1586. })
  1587. })
  1588. it('returns result if the code returns an asyncronous promise', function (done) {
  1589. ipcRenderer.send('executeJavaScript', asyncCode, true)
  1590. ipcRenderer.once('executeJavaScript-response', function (event, result) {
  1591. assert.equal(result, expected)
  1592. done()
  1593. })
  1594. })
  1595. it('resolves the returned promise with the result', function (done) {
  1596. ipcRenderer.send('executeJavaScript', code, true)
  1597. ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
  1598. assert.equal(result, expected)
  1599. done()
  1600. })
  1601. })
  1602. it('resolves the returned promise with the result if the code returns an asyncronous promise', function (done) {
  1603. ipcRenderer.send('executeJavaScript', asyncCode, true)
  1604. ipcRenderer.once('executeJavaScript-promise-response', function (event, result) {
  1605. assert.equal(result, expected)
  1606. done()
  1607. })
  1608. })
  1609. it('rejects the returned promise if an async error is thrown', function (done) {
  1610. ipcRenderer.send('executeJavaScript', badAsyncCode, true)
  1611. ipcRenderer.once('executeJavaScript-promise-error', function (event, error) {
  1612. assert.equal(error, expectedErrorMsg)
  1613. done()
  1614. })
  1615. })
  1616. it('works after page load and during subframe load', function (done) {
  1617. w.webContents.once('did-finish-load', function () {
  1618. // initiate a sub-frame load, then try and execute script during it
  1619. w.webContents.executeJavaScript(`
  1620. var iframe = document.createElement('iframe')
  1621. iframe.src = '${server.url}/slow'
  1622. document.body.appendChild(iframe)
  1623. `, function () {
  1624. w.webContents.executeJavaScript('console.log(\'hello\')', function () {
  1625. done()
  1626. })
  1627. })
  1628. })
  1629. w.loadURL(server.url)
  1630. })
  1631. it('executes after page load', function (done) {
  1632. w.webContents.executeJavaScript(code, function (result) {
  1633. assert.equal(result, expected)
  1634. done()
  1635. })
  1636. w.loadURL(server.url)
  1637. })
  1638. it('works with result objects that have DOM class prototypes', function (done) {
  1639. w.webContents.executeJavaScript('document.location', function (result) {
  1640. assert.equal(result.origin, server.url)
  1641. assert.equal(result.protocol, 'http:')
  1642. done()
  1643. })
  1644. w.loadURL(server.url)
  1645. })
  1646. })
  1647. describe('previewFile', function () {
  1648. it('opens the path in Quick Look on macOS', function () {
  1649. if (process.platform !== 'darwin') return
  1650. assert.doesNotThrow(function () {
  1651. w.previewFile(__filename)
  1652. w.closeFilePreview()
  1653. })
  1654. })
  1655. })
  1656. describe('contextIsolation option', () => {
  1657. const expectedContextData = {
  1658. preloadContext: {
  1659. preloadProperty: 'number',
  1660. pageProperty: 'undefined',
  1661. typeofRequire: 'function',
  1662. typeofProcess: 'object',
  1663. typeofArrayPush: 'function',
  1664. typeofFunctionApply: 'function'
  1665. },
  1666. pageContext: {
  1667. preloadProperty: 'undefined',
  1668. pageProperty: 'string',
  1669. typeofRequire: 'undefined',
  1670. typeofProcess: 'undefined',
  1671. typeofArrayPush: 'number',
  1672. typeofFunctionApply: 'boolean',
  1673. typeofPreloadExecuteJavaScriptProperty: 'number',
  1674. typeofOpenedWindow: 'object',
  1675. documentHidden: true,
  1676. documentVisibilityState: 'hidden'
  1677. }
  1678. }
  1679. beforeEach(() => {
  1680. if (w != null) w.destroy()
  1681. w = new BrowserWindow({
  1682. show: false,
  1683. webPreferences: {
  1684. contextIsolation: true,
  1685. preload: path.join(fixtures, 'api', 'isolated-preload.js')
  1686. }
  1687. })
  1688. })
  1689. it('separates the page context from the Electron/preload context', (done) => {
  1690. ipcMain.once('isolated-world', (event, data) => {
  1691. assert.deepEqual(data, expectedContextData)
  1692. done()
  1693. })
  1694. w.loadURL('file://' + fixtures + '/api/isolated.html')
  1695. })
  1696. it('recreates the contexts on reload', (done) => {
  1697. w.webContents.once('did-finish-load', () => {
  1698. ipcMain.once('isolated-world', (event, data) => {
  1699. assert.deepEqual(data, expectedContextData)
  1700. done()
  1701. })
  1702. w.webContents.reload()
  1703. })
  1704. w.loadURL('file://' + fixtures + '/api/isolated.html')
  1705. })
  1706. it('enables context isolation on child windows', function (done) {
  1707. app.once('browser-window-created', function (event, window) {
  1708. assert.equal(window.webContents.getWebPreferences().contextIsolation, true)
  1709. done()
  1710. })
  1711. w.loadURL('file://' + fixtures + '/pages/window-open.html')
  1712. })
  1713. })
  1714. describe('offscreen rendering', function () {
  1715. beforeEach(function () {
  1716. if (w != null) w.destroy()
  1717. w = new BrowserWindow({
  1718. show: false,
  1719. webPreferences: {
  1720. backgroundThrottling: false,
  1721. offscreen: true
  1722. }
  1723. })
  1724. })
  1725. it('creates offscreen window', function (done) {
  1726. w.webContents.once('paint', function (event, rect, data, size) {
  1727. assert.notEqual(data.length, 0)
  1728. done()
  1729. })
  1730. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1731. })
  1732. describe('window.webContents.isOffscreen()', function () {
  1733. it('is true for offscreen type', function () {
  1734. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1735. assert.equal(w.webContents.isOffscreen(), true)
  1736. })
  1737. it('is false for regular window', function () {
  1738. let c = new BrowserWindow({show: false})
  1739. assert.equal(c.webContents.isOffscreen(), false)
  1740. c.destroy()
  1741. })
  1742. })
  1743. describe('window.webContents.isPainting()', function () {
  1744. it('returns whether is currently painting', function (done) {
  1745. w.webContents.once('paint', function (event, rect, data, size) {
  1746. assert.equal(w.webContents.isPainting(), true)
  1747. done()
  1748. })
  1749. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1750. })
  1751. })
  1752. describe('window.webContents.stopPainting()', function () {
  1753. it('stops painting', function (done) {
  1754. w.webContents.on('dom-ready', function () {
  1755. w.webContents.stopPainting()
  1756. assert.equal(w.webContents.isPainting(), false)
  1757. done()
  1758. })
  1759. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1760. })
  1761. })
  1762. describe('window.webContents.startPainting()', function () {
  1763. it('starts painting', function (done) {
  1764. w.webContents.on('dom-ready', function () {
  1765. w.webContents.stopPainting()
  1766. w.webContents.startPainting()
  1767. w.webContents.once('paint', function (event, rect, data, size) {
  1768. assert.equal(w.webContents.isPainting(), true)
  1769. done()
  1770. })
  1771. })
  1772. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1773. })
  1774. })
  1775. describe('window.webContents.getFrameRate()', function () {
  1776. it('has default frame rate', function (done) {
  1777. w.webContents.once('paint', function (event, rect, data, size) {
  1778. assert.equal(w.webContents.getFrameRate(), 60)
  1779. done()
  1780. })
  1781. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1782. })
  1783. })
  1784. describe('window.webContents.setFrameRate(frameRate)', function () {
  1785. it('sets custom frame rate', function (done) {
  1786. w.webContents.on('dom-ready', function () {
  1787. w.webContents.setFrameRate(30)
  1788. w.webContents.once('paint', function (event, rect, data, size) {
  1789. assert.equal(w.webContents.getFrameRate(), 30)
  1790. done()
  1791. })
  1792. })
  1793. w.loadURL('file://' + fixtures + '/api/offscreen-rendering.html')
  1794. })
  1795. })
  1796. })
  1797. })
  1798. const assertBoundsEqual = (actual, expect) => {
  1799. if (!isScaleFactorRounding()) {
  1800. assert.deepEqual(expect, actual)
  1801. } else if (Array.isArray(actual)) {
  1802. assertWithinDelta(actual[0], expect[0], 1, 'x')
  1803. assertWithinDelta(actual[1], expect[1], 1, 'y')
  1804. } else {
  1805. assertWithinDelta(actual.x, expect.x, 1, 'x')
  1806. assertWithinDelta(actual.y, expect.y, 1, 'y')
  1807. assertWithinDelta(actual.width, expect.width, 1, 'width')
  1808. assertWithinDelta(actual.height, expect.height, 1, 'height')
  1809. }
  1810. }
  1811. const assertWithinDelta = (actual, expect, delta, label) => {
  1812. const result = Math.abs(actual - expect)
  1813. assert.ok(result <= delta, `${label} value of ${expect} was not within ${delta} of ${actual}`)
  1814. }
  1815. // Is the display's scale factor possibly causing rounding of pixel coordinate
  1816. // values?
  1817. const isScaleFactorRounding = () => {
  1818. const {scaleFactor} = screen.getPrimaryDisplay()
  1819. // Return true if scale factor is non-integer value
  1820. if (Math.round(scaleFactor) !== scaleFactor) return true
  1821. // Return true if scale factor is odd number above 2
  1822. return scaleFactor > 2 && scaleFactor % 2 === 1
  1823. }