api-native-image-spec.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. 'use strict'
  2. const chai = require('chai')
  3. const dirtyChai = require('dirty-chai')
  4. const { nativeImage } = require('electron')
  5. const path = require('path')
  6. const { expect } = chai
  7. chai.use(dirtyChai)
  8. describe('nativeImage module', () => {
  9. const ImageFormat = {
  10. PNG: 'png',
  11. JPEG: 'jpeg'
  12. }
  13. const images = [
  14. {
  15. filename: 'logo.png',
  16. format: ImageFormat.PNG,
  17. hasAlphaChannel: true,
  18. hasDataUrl: false,
  19. width: 538,
  20. height: 190
  21. },
  22. {
  23. dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYlWNgAAIAAAUAAdafFs0AAAAASUVORK5CYII=',
  24. filename: '1x1.png',
  25. format: ImageFormat.PNG,
  26. hasAlphaChannel: true,
  27. hasDataUrl: true,
  28. height: 1,
  29. width: 1
  30. },
  31. {
  32. dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAFklEQVQYlWP8//8/AwMDEwMDAwMDAwAkBgMBBMzldwAAAABJRU5ErkJggg==',
  33. filename: '2x2.jpg',
  34. format: ImageFormat.JPEG,
  35. hasAlphaChannel: false,
  36. hasDataUrl: true,
  37. height: 2,
  38. width: 2
  39. },
  40. {
  41. dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAAADElEQVQYlWNgIAoAAAAnAAGZWEMnAAAAAElFTkSuQmCC',
  42. filename: '3x3.png',
  43. format: ImageFormat.PNG,
  44. hasAlphaChannel: true,
  45. hasDataUrl: true,
  46. height: 3,
  47. width: 3
  48. }
  49. ]
  50. /**
  51. * @param {?string} filename
  52. * @returns {?string} Full path.
  53. */
  54. const getImagePathFromFilename = (filename) => {
  55. return (filename === null) ? null
  56. : path.join(__dirname, 'fixtures', 'assets', filename)
  57. }
  58. /**
  59. * @param {!Object} image
  60. * @param {Object} filters
  61. * @returns {boolean}
  62. */
  63. const imageMatchesTheFilters = (image, filters = null) => {
  64. if (filters === null) {
  65. return true
  66. }
  67. return Object.entries(filters)
  68. .every(([key, value]) => image[key] === value)
  69. }
  70. /**
  71. * @param {!Object} filters
  72. * @returns {!Array} A matching images list.
  73. */
  74. const getImages = (filters) => {
  75. const matchingImages = images
  76. .filter(i => imageMatchesTheFilters(i, filters))
  77. // Add `.path` property to every image.
  78. matchingImages
  79. .forEach(i => { i.path = getImagePathFromFilename(i.filename) })
  80. return matchingImages
  81. }
  82. /**
  83. * @param {!Object} filters
  84. * @returns {Object} A matching image if any.
  85. */
  86. const getImage = (filters) => {
  87. const matchingImages = getImages(filters)
  88. let matchingImage = null
  89. if (matchingImages.length > 0) {
  90. matchingImage = matchingImages[0]
  91. }
  92. return matchingImage
  93. }
  94. describe('createEmpty()', () => {
  95. it('returns an empty image', () => {
  96. const empty = nativeImage.createEmpty()
  97. expect(empty.isEmpty())
  98. expect(empty.getAspectRatio()).to.equal(1)
  99. expect(empty.toDataURL()).to.equal('data:image/png;base64,')
  100. expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,')
  101. expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 })
  102. expect(empty.getBitmap()).to.be.empty()
  103. expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty()
  104. expect(empty.toBitmap()).to.be.empty()
  105. expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty()
  106. expect(empty.toJPEG(100)).to.be.empty()
  107. expect(empty.toPNG()).to.be.empty()
  108. expect(empty.toPNG({ scaleFactor: 2.0 })).to.be.empty()
  109. if (process.platform === 'darwin') {
  110. expect(empty.getNativeHandle()).to.be.empty()
  111. }
  112. })
  113. })
  114. describe('createFromBuffer(buffer, scaleFactor)', () => {
  115. it('returns an empty image when the buffer is empty', () => {
  116. expect(nativeImage.createFromBuffer(Buffer.from([])).isEmpty())
  117. })
  118. it('returns an image created from the given buffer', () => {
  119. const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  120. const imageB = nativeImage.createFromBuffer(imageA.toPNG())
  121. expect(imageB.getSize()).to.deep.equal({ width: 538, height: 190 })
  122. expect(imageA.toBitmap().equals(imageB.toBitmap())).to.be.true()
  123. const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100))
  124. expect(imageC.getSize()).to.deep.equal({ width: 538, height: 190 })
  125. const imageD = nativeImage.createFromBuffer(imageA.toBitmap(),
  126. { width: 538, height: 190 })
  127. expect(imageD.getSize()).to.deep.equal({ width: 538, height: 190 })
  128. const imageE = nativeImage.createFromBuffer(imageA.toBitmap(),
  129. { width: 100, height: 200 })
  130. expect(imageE.getSize()).to.deep.equal({ width: 100, height: 200 })
  131. const imageF = nativeImage.createFromBuffer(imageA.toBitmap())
  132. expect(imageF.isEmpty())
  133. const imageG = nativeImage.createFromBuffer(imageA.toPNG(),
  134. { width: 100, height: 200 })
  135. expect(imageG.getSize()).to.deep.equal({ width: 538, height: 190 })
  136. const imageH = nativeImage.createFromBuffer(imageA.toJPEG(100),
  137. { width: 100, height: 200 })
  138. expect(imageH.getSize()).to.deep.equal({ width: 538, height: 190 })
  139. const imageI = nativeImage.createFromBuffer(imageA.toBitmap(),
  140. { width: 538, height: 190, scaleFactor: 2.0 })
  141. expect(imageI.getSize()).to.deep.equal({ width: 269, height: 95 })
  142. })
  143. })
  144. describe('createFromDataURL(dataURL)', () => {
  145. it('returns an empty image from the empty string', () => {
  146. expect(nativeImage.createFromDataURL('').isEmpty())
  147. })
  148. it('returns an image created from the given string', () => {
  149. const imagesData = getImages({ hasDataUrl: true })
  150. for (const imageData of imagesData) {
  151. const imageFromPath = nativeImage.createFromPath(imageData.path)
  152. const imageFromDataUrl = nativeImage.createFromDataURL(imageData.dataUrl)
  153. expect(imageFromDataUrl.isEmpty())
  154. expect(imageFromDataUrl.getSize()).to.deep.equal(imageFromPath.getSize())
  155. expect(imageFromDataUrl.toBitmap()).to.satisfy(
  156. bitmap => imageFromPath.toBitmap().equals(bitmap))
  157. expect(imageFromDataUrl.toDataURL()).to.equal(imageFromPath.toDataURL())
  158. }
  159. })
  160. })
  161. describe('toDataURL()', () => {
  162. it('returns a PNG data URL', () => {
  163. const imagesData = getImages({ hasDataUrl: true })
  164. for (const imageData of imagesData) {
  165. const imageFromPath = nativeImage.createFromPath(imageData.path)
  166. const scaleFactors = [1.0, 2.0]
  167. for (const scaleFactor of scaleFactors) {
  168. expect(imageFromPath.toDataURL({ scaleFactor })).to.equal(imageData.dataUrl)
  169. }
  170. }
  171. })
  172. it('returns a data URL at 1x scale factor by default', () => {
  173. const imageData = getImage({ filename: 'logo.png' })
  174. const image = nativeImage.createFromPath(imageData.path)
  175. const imageOne = nativeImage.createFromBuffer(image.toPNG(), {
  176. width: image.getSize().width,
  177. height: image.getSize().height,
  178. scaleFactor: 2.0
  179. })
  180. expect(imageOne.getSize()).to.deep.equal(
  181. { width: imageData.width / 2, height: imageData.height / 2 })
  182. const imageTwo = nativeImage.createFromDataURL(imageOne.toDataURL())
  183. expect(imageTwo.getSize()).to.deep.equal(
  184. { width: imageData.width, height: imageData.height })
  185. expect(imageOne.toBitmap().equals(imageTwo.toBitmap())).to.be.true()
  186. })
  187. it('supports a scale factor', () => {
  188. const imageData = getImage({ filename: 'logo.png' })
  189. const image = nativeImage.createFromPath(imageData.path)
  190. const expectedSize = { width: imageData.width, height: imageData.height }
  191. const imageFromDataUrlOne = nativeImage.createFromDataURL(
  192. image.toDataURL({ scaleFactor: 1.0 }))
  193. expect(imageFromDataUrlOne.getSize()).to.deep.equal(expectedSize)
  194. const imageFromDataUrlTwo = nativeImage.createFromDataURL(
  195. image.toDataURL({ scaleFactor: 2.0 }))
  196. expect(imageFromDataUrlTwo.getSize()).to.deep.equal(expectedSize)
  197. })
  198. })
  199. describe('toPNG()', () => {
  200. it('returns a buffer at 1x scale factor by default', () => {
  201. const imageData = getImage({ filename: 'logo.png' })
  202. const imageA = nativeImage.createFromPath(imageData.path)
  203. const imageB = nativeImage.createFromBuffer(imageA.toPNG(), {
  204. width: imageA.getSize().width,
  205. height: imageA.getSize().height,
  206. scaleFactor: 2.0
  207. })
  208. expect(imageB.getSize()).to.deep.equal(
  209. { width: imageData.width / 2, height: imageData.height / 2 })
  210. const imageC = nativeImage.createFromBuffer(imageB.toPNG())
  211. expect(imageC.getSize()).to.deep.equal(
  212. { width: imageData.width, height: imageData.height })
  213. expect(imageB.toBitmap().equals(imageC.toBitmap())).to.be.true()
  214. })
  215. it('supports a scale factor', () => {
  216. const imageData = getImage({ filename: 'logo.png' })
  217. const image = nativeImage.createFromPath(imageData.path)
  218. const imageFromBufferOne = nativeImage.createFromBuffer(
  219. image.toPNG({ scaleFactor: 1.0 }))
  220. expect(imageFromBufferOne.getSize()).to.deep.equal(
  221. { width: imageData.width, height: imageData.height })
  222. const imageFromBufferTwo = nativeImage.createFromBuffer(
  223. image.toPNG({ scaleFactor: 2.0 }), { scaleFactor: 2.0 })
  224. expect(imageFromBufferTwo.getSize()).to.deep.equal(
  225. { width: imageData.width / 2, height: imageData.height / 2 })
  226. })
  227. })
  228. describe('createFromPath(path)', () => {
  229. it('returns an empty image for invalid paths', () => {
  230. expect(nativeImage.createFromPath('').isEmpty())
  231. expect(nativeImage.createFromPath('does-not-exist.png').isEmpty())
  232. expect(nativeImage.createFromPath('does-not-exist.ico').isEmpty())
  233. expect(nativeImage.createFromPath(__dirname).isEmpty())
  234. expect(nativeImage.createFromPath(__filename).isEmpty())
  235. })
  236. it('loads images from paths relative to the current working directory', () => {
  237. const imagePath = path.relative('.', path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  238. const image = nativeImage.createFromPath(imagePath)
  239. expect(image.isEmpty()).to.be.false()
  240. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  241. })
  242. it('loads images from paths with `.` segments', () => {
  243. const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`
  244. const image = nativeImage.createFromPath(imagePath)
  245. expect(image.isEmpty()).to.be.false()
  246. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  247. })
  248. it('loads images from paths with `..` segments', () => {
  249. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  250. const image = nativeImage.createFromPath(imagePath)
  251. expect(image.isEmpty()).to.be.false()
  252. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  253. })
  254. it('Gets an NSImage pointer on macOS', function () {
  255. if (process.platform !== 'darwin') {
  256. // FIXME(alexeykuzmin): Skip the test.
  257. // this.skip()
  258. return
  259. }
  260. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  261. const image = nativeImage.createFromPath(imagePath)
  262. const nsimage = image.getNativeHandle()
  263. expect(nsimage).to.have.lengthOf(8)
  264. // If all bytes are null, that's Bad
  265. const allBytesAreNotNull = nsimage.reduce((acc, x) => acc || (x !== 0), false)
  266. expect(allBytesAreNotNull)
  267. })
  268. it('loads images from .ico files on Windows', function () {
  269. if (process.platform !== 'win32') {
  270. // FIXME(alexeykuzmin): Skip the test.
  271. // this.skip()
  272. return
  273. }
  274. const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
  275. const image = nativeImage.createFromPath(imagePath)
  276. expect(image.isEmpty()).to.be.false()
  277. expect(image.getSize()).to.deep.equal({ width: 256, height: 256 })
  278. })
  279. })
  280. describe('createFromNamedImage(name)', () => {
  281. it('returns empty for invalid options', () => {
  282. const image = nativeImage.createFromNamedImage('totally_not_real')
  283. expect(image.isEmpty())
  284. })
  285. it('returns empty on non-darwin platforms', function () {
  286. if (process.platform === 'darwin') {
  287. // FIXME(alexeykuzmin): Skip the test.
  288. // this.skip()
  289. return
  290. }
  291. const image = nativeImage.createFromNamedImage('NSActionTemplate')
  292. expect(image.isEmpty())
  293. })
  294. it('returns a valid image on darwin', function () {
  295. if (process.platform !== 'darwin') {
  296. // FIXME(alexeykuzmin): Skip the test.
  297. // this.skip()
  298. return
  299. }
  300. const image = nativeImage.createFromNamedImage('NSActionTemplate')
  301. expect(image.isEmpty()).to.be.false()
  302. })
  303. it('returns allows an HSL shift for a valid image on darwin', function () {
  304. if (process.platform !== 'darwin') {
  305. // FIXME(alexeykuzmin): Skip the test.
  306. // this.skip()
  307. return
  308. }
  309. const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8])
  310. expect(image.isEmpty()).to.be.false()
  311. })
  312. })
  313. describe('resize(options)', () => {
  314. it('returns a resized image', () => {
  315. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  316. for (const [resizeTo, expectedSize] of new Map([
  317. [{}, { width: 538, height: 190 }],
  318. [{ width: 269 }, { width: 269, height: 95 }],
  319. [{ width: 600 }, { width: 600, height: 212 }],
  320. [{ height: 95 }, { width: 269, height: 95 }],
  321. [{ height: 200 }, { width: 566, height: 200 }],
  322. [{ width: 80, height: 65 }, { width: 80, height: 65 }],
  323. [{ width: 600, height: 200 }, { width: 600, height: 200 }],
  324. [{ width: 0, height: 0 }, { width: 0, height: 0 }],
  325. [{ width: -1, height: -1 }, { width: 0, height: 0 }]
  326. ])) {
  327. const actualSize = image.resize(resizeTo).getSize()
  328. expect(actualSize).to.deep.equal(expectedSize)
  329. }
  330. })
  331. it('returns an empty image when called on an empty image', () => {
  332. expect(nativeImage.createEmpty().resize({ width: 1, height: 1 }).isEmpty())
  333. expect(nativeImage.createEmpty().resize({ width: 0, height: 0 }).isEmpty())
  334. })
  335. it('supports a quality option', () => {
  336. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  337. const good = image.resize({ width: 100, height: 100, quality: 'good' })
  338. const better = image.resize({ width: 100, height: 100, quality: 'better' })
  339. const best = image.resize({ width: 100, height: 100, quality: 'best' })
  340. expect(good.toPNG()).to.have.lengthOf.at.most(better.toPNG().length)
  341. expect(better.toPNG()).to.have.lengthOf.below(best.toPNG().length)
  342. })
  343. })
  344. describe('crop(bounds)', () => {
  345. it('returns an empty image when called on an empty image', () => {
  346. expect(nativeImage.createEmpty().crop({ width: 1, height: 2, x: 0, y: 0 }).isEmpty())
  347. expect(nativeImage.createEmpty().crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty())
  348. })
  349. it('returns an empty image when the bounds are invalid', () => {
  350. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  351. expect(image.crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty())
  352. expect(image.crop({ width: -1, height: 10, x: 0, y: 0 }).isEmpty())
  353. expect(image.crop({ width: 10, height: -35, x: 0, y: 0 }).isEmpty())
  354. expect(image.crop({ width: 100, height: 100, x: 1000, y: 1000 }).isEmpty())
  355. })
  356. it('returns a cropped image', () => {
  357. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  358. const cropA = image.crop({ width: 25, height: 64, x: 0, y: 0 })
  359. const cropB = image.crop({ width: 25, height: 64, x: 30, y: 40 })
  360. expect(cropA.getSize()).to.deep.equal({ width: 25, height: 64 })
  361. expect(cropB.getSize()).to.deep.equal({ width: 25, height: 64 })
  362. expect(cropA.toPNG().equals(cropB.toPNG())).to.be.false()
  363. })
  364. })
  365. describe('getAspectRatio()', () => {
  366. it('returns an aspect ratio of an empty image', () => {
  367. expect(nativeImage.createEmpty().getAspectRatio()).to.equal(1.0)
  368. })
  369. it('returns an aspect ratio of an image', () => {
  370. const imageData = getImage({ filename: 'logo.png' })
  371. // imageData.width / imageData.height = 2.831578947368421
  372. const expectedAspectRatio = 2.8315789699554443
  373. const image = nativeImage.createFromPath(imageData.path)
  374. expect(image.getAspectRatio()).to.equal(expectedAspectRatio)
  375. })
  376. })
  377. describe('addRepresentation()', () => {
  378. it('supports adding a buffer representation for a scale factor', () => {
  379. const image = nativeImage.createEmpty()
  380. const imageDataOne = getImage({ width: 1, height: 1 })
  381. image.addRepresentation({
  382. scaleFactor: 1.0,
  383. buffer: nativeImage.createFromPath(imageDataOne.path).toPNG()
  384. })
  385. const imageDataTwo = getImage({ width: 2, height: 2 })
  386. image.addRepresentation({
  387. scaleFactor: 2.0,
  388. buffer: nativeImage.createFromPath(imageDataTwo.path).toPNG()
  389. })
  390. const imageDataThree = getImage({ width: 3, height: 3 })
  391. image.addRepresentation({
  392. scaleFactor: 3.0,
  393. buffer: nativeImage.createFromPath(imageDataThree.path).toPNG()
  394. })
  395. image.addRepresentation({
  396. scaleFactor: 4.0,
  397. buffer: 'invalid'
  398. })
  399. expect(image.isEmpty()).to.be.false()
  400. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 })
  401. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  402. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  403. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl)
  404. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl)
  405. })
  406. it('supports adding a data URL representation for a scale factor', () => {
  407. const image = nativeImage.createEmpty()
  408. const imageDataOne = getImage({ width: 1, height: 1 })
  409. image.addRepresentation({
  410. scaleFactor: 1.0,
  411. dataURL: imageDataOne.dataUrl
  412. })
  413. const imageDataTwo = getImage({ width: 2, height: 2 })
  414. image.addRepresentation({
  415. scaleFactor: 2.0,
  416. dataURL: imageDataTwo.dataUrl
  417. })
  418. const imageDataThree = getImage({ width: 3, height: 3 })
  419. image.addRepresentation({
  420. scaleFactor: 3.0,
  421. dataURL: imageDataThree.dataUrl
  422. })
  423. image.addRepresentation({
  424. scaleFactor: 4.0,
  425. dataURL: 'invalid'
  426. })
  427. expect(image.isEmpty()).to.be.false()
  428. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 })
  429. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  430. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  431. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl)
  432. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl)
  433. })
  434. it('supports adding a representation to an existing image', () => {
  435. const imageDataOne = getImage({ width: 1, height: 1 })
  436. const image = nativeImage.createFromPath(imageDataOne.path)
  437. const imageDataTwo = getImage({ width: 2, height: 2 })
  438. image.addRepresentation({
  439. scaleFactor: 2.0,
  440. dataURL: imageDataTwo.dataUrl
  441. })
  442. const imageDataThree = getImage({ width: 3, height: 3 })
  443. image.addRepresentation({
  444. scaleFactor: 2.0,
  445. dataURL: imageDataThree.dataUrl
  446. })
  447. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  448. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  449. })
  450. })
  451. })