api-native-image-spec.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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('isMacTemplateImage property', () => {
  95. before(function () {
  96. if (process.platform !== 'darwin') this.skip()
  97. })
  98. it('returns whether the image is a template image', () => {
  99. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  100. expect(image.isMacTemplateImage).to.be.a('boolean')
  101. expect(image.isTemplateImage).to.be.a('function')
  102. expect(image.setTemplateImage).to.be.a('function')
  103. })
  104. it('correctly recognizes a template image', () => {
  105. const templateImage = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo_Template.png'))
  106. expect(templateImage.isMacTemplateImage).to.be.true()
  107. })
  108. it('sets a template image', function () {
  109. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  110. expect(image.isMacTemplateImage).to.be.false()
  111. image.isMacTemplateImage = true
  112. expect(image.isMacTemplateImage).to.be.true()
  113. })
  114. })
  115. describe('createEmpty()', () => {
  116. it('returns an empty image', () => {
  117. const empty = nativeImage.createEmpty()
  118. expect(empty.isEmpty()).to.be.true()
  119. expect(empty.getAspectRatio()).to.equal(1)
  120. expect(empty.toDataURL()).to.equal('data:image/png;base64,')
  121. expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,')
  122. expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 })
  123. expect(empty.getBitmap()).to.be.empty()
  124. expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty()
  125. expect(empty.toBitmap()).to.be.empty()
  126. expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty()
  127. expect(empty.toJPEG(100)).to.be.empty()
  128. expect(empty.toPNG()).to.be.empty()
  129. expect(empty.toPNG({ scaleFactor: 2.0 })).to.be.empty()
  130. if (process.platform === 'darwin') {
  131. expect(empty.getNativeHandle()).to.be.empty()
  132. }
  133. })
  134. })
  135. describe('createFromBitmap(buffer, options)', () => {
  136. it('returns an empty image when the buffer is empty', () => {
  137. expect(nativeImage.createFromBitmap(Buffer.from([]), { width: 0, height: 0 }).isEmpty()).to.be.true()
  138. })
  139. it('returns an image created from the given buffer', () => {
  140. const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  141. const imageB = nativeImage.createFromBitmap(imageA.toBitmap(), imageA.getSize())
  142. expect(imageB.getSize()).to.deep.equal({ width: 538, height: 190 })
  143. const imageC = nativeImage.createFromBuffer(imageA.toBitmap(), { ...imageA.getSize(), scaleFactor: 2.0 })
  144. expect(imageC.getSize()).to.deep.equal({ width: 269, height: 95 })
  145. })
  146. it('throws on invalid arguments', () => {
  147. expect(() => nativeImage.createFromBitmap(null, {})).to.throw('buffer must be a node Buffer')
  148. expect(() => nativeImage.createFromBitmap([12, 14, 124, 12], {})).to.throw('buffer must be a node Buffer')
  149. expect(() => nativeImage.createFromBitmap(Buffer.from([]), {})).to.throw('width is required')
  150. expect(() => nativeImage.createFromBitmap(Buffer.from([]), { width: 1 })).to.throw('height is required')
  151. expect(() => nativeImage.createFromBitmap(Buffer.from([]), { width: 1, height: 1 })).to.throw('invalid buffer size')
  152. })
  153. })
  154. describe('createFromBuffer(buffer, options)', () => {
  155. it('returns an empty image when the buffer is empty', () => {
  156. expect(nativeImage.createFromBuffer(Buffer.from([])).isEmpty()).to.be.true()
  157. })
  158. it('returns an empty image when the buffer is too small', () => {
  159. const image = nativeImage.createFromBuffer(Buffer.from([1, 2, 3, 4]), { width: 100, height: 100 })
  160. expect(image.isEmpty()).to.be.true()
  161. })
  162. it('returns an image created from the given buffer', () => {
  163. const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  164. const imageB = nativeImage.createFromBuffer(imageA.toPNG())
  165. expect(imageB.getSize()).to.deep.equal({ width: 538, height: 190 })
  166. expect(imageA.toBitmap().equals(imageB.toBitmap())).to.be.true()
  167. const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100))
  168. expect(imageC.getSize()).to.deep.equal({ width: 538, height: 190 })
  169. const imageD = nativeImage.createFromBuffer(imageA.toBitmap(),
  170. { width: 538, height: 190 })
  171. expect(imageD.getSize()).to.deep.equal({ width: 538, height: 190 })
  172. const imageE = nativeImage.createFromBuffer(imageA.toBitmap(),
  173. { width: 100, height: 200 })
  174. expect(imageE.getSize()).to.deep.equal({ width: 100, height: 200 })
  175. const imageF = nativeImage.createFromBuffer(imageA.toBitmap())
  176. expect(imageF.isEmpty()).to.be.true()
  177. const imageG = nativeImage.createFromBuffer(imageA.toPNG(),
  178. { width: 100, height: 200 })
  179. expect(imageG.getSize()).to.deep.equal({ width: 538, height: 190 })
  180. const imageH = nativeImage.createFromBuffer(imageA.toJPEG(100),
  181. { width: 100, height: 200 })
  182. expect(imageH.getSize()).to.deep.equal({ width: 538, height: 190 })
  183. const imageI = nativeImage.createFromBuffer(imageA.toBitmap(),
  184. { width: 538, height: 190, scaleFactor: 2.0 })
  185. expect(imageI.getSize()).to.deep.equal({ width: 269, height: 95 })
  186. })
  187. it('throws on invalid arguments', () => {
  188. expect(() => nativeImage.createFromBuffer(null)).to.throw('buffer must be a node Buffer')
  189. expect(() => nativeImage.createFromBuffer([12, 14, 124, 12])).to.throw('buffer must be a node Buffer')
  190. })
  191. })
  192. describe('createFromDataURL(dataURL)', () => {
  193. it('returns an empty image from the empty string', () => {
  194. expect(nativeImage.createFromDataURL('').isEmpty()).to.be.true()
  195. })
  196. it('returns an image created from the given string', () => {
  197. const imagesData = getImages({ hasDataUrl: true })
  198. for (const imageData of imagesData) {
  199. const imageFromPath = nativeImage.createFromPath(imageData.path)
  200. const imageFromDataUrl = nativeImage.createFromDataURL(imageData.dataUrl)
  201. expect(imageFromDataUrl.isEmpty()).to.be.false()
  202. expect(imageFromDataUrl.getSize()).to.deep.equal(imageFromPath.getSize())
  203. expect(imageFromDataUrl.toBitmap()).to.satisfy(
  204. bitmap => imageFromPath.toBitmap().equals(bitmap))
  205. expect(imageFromDataUrl.toDataURL()).to.equal(imageFromPath.toDataURL())
  206. }
  207. })
  208. })
  209. describe('toDataURL()', () => {
  210. it('returns a PNG data URL', () => {
  211. const imagesData = getImages({ hasDataUrl: true })
  212. for (const imageData of imagesData) {
  213. const imageFromPath = nativeImage.createFromPath(imageData.path)
  214. const scaleFactors = [1.0, 2.0]
  215. for (const scaleFactor of scaleFactors) {
  216. expect(imageFromPath.toDataURL({ scaleFactor })).to.equal(imageData.dataUrl)
  217. }
  218. }
  219. })
  220. it('returns a data URL at 1x scale factor by default', () => {
  221. const imageData = getImage({ filename: 'logo.png' })
  222. const image = nativeImage.createFromPath(imageData.path)
  223. const imageOne = nativeImage.createFromBuffer(image.toPNG(), {
  224. width: image.getSize().width,
  225. height: image.getSize().height,
  226. scaleFactor: 2.0
  227. })
  228. expect(imageOne.getSize()).to.deep.equal(
  229. { width: imageData.width / 2, height: imageData.height / 2 })
  230. const imageTwo = nativeImage.createFromDataURL(imageOne.toDataURL())
  231. expect(imageTwo.getSize()).to.deep.equal(
  232. { width: imageData.width, height: imageData.height })
  233. expect(imageOne.toBitmap().equals(imageTwo.toBitmap())).to.be.true()
  234. })
  235. it('supports a scale factor', () => {
  236. const imageData = getImage({ filename: 'logo.png' })
  237. const image = nativeImage.createFromPath(imageData.path)
  238. const expectedSize = { width: imageData.width, height: imageData.height }
  239. const imageFromDataUrlOne = nativeImage.createFromDataURL(
  240. image.toDataURL({ scaleFactor: 1.0 }))
  241. expect(imageFromDataUrlOne.getSize()).to.deep.equal(expectedSize)
  242. const imageFromDataUrlTwo = nativeImage.createFromDataURL(
  243. image.toDataURL({ scaleFactor: 2.0 }))
  244. expect(imageFromDataUrlTwo.getSize()).to.deep.equal(expectedSize)
  245. })
  246. })
  247. describe('toPNG()', () => {
  248. it('returns a buffer at 1x scale factor by default', () => {
  249. const imageData = getImage({ filename: 'logo.png' })
  250. const imageA = nativeImage.createFromPath(imageData.path)
  251. const imageB = nativeImage.createFromBuffer(imageA.toPNG(), {
  252. width: imageA.getSize().width,
  253. height: imageA.getSize().height,
  254. scaleFactor: 2.0
  255. })
  256. expect(imageB.getSize()).to.deep.equal(
  257. { width: imageData.width / 2, height: imageData.height / 2 })
  258. const imageC = nativeImage.createFromBuffer(imageB.toPNG())
  259. expect(imageC.getSize()).to.deep.equal(
  260. { width: imageData.width, height: imageData.height })
  261. expect(imageB.toBitmap().equals(imageC.toBitmap())).to.be.true()
  262. })
  263. it('supports a scale factor', () => {
  264. const imageData = getImage({ filename: 'logo.png' })
  265. const image = nativeImage.createFromPath(imageData.path)
  266. const imageFromBufferOne = nativeImage.createFromBuffer(
  267. image.toPNG({ scaleFactor: 1.0 }))
  268. expect(imageFromBufferOne.getSize()).to.deep.equal(
  269. { width: imageData.width, height: imageData.height })
  270. const imageFromBufferTwo = nativeImage.createFromBuffer(
  271. image.toPNG({ scaleFactor: 2.0 }), { scaleFactor: 2.0 })
  272. expect(imageFromBufferTwo.getSize()).to.deep.equal(
  273. { width: imageData.width / 2, height: imageData.height / 2 })
  274. })
  275. })
  276. describe('createFromPath(path)', () => {
  277. it('returns an empty image for invalid paths', () => {
  278. expect(nativeImage.createFromPath('').isEmpty()).to.be.true()
  279. expect(nativeImage.createFromPath('does-not-exist.png').isEmpty()).to.be.true()
  280. expect(nativeImage.createFromPath('does-not-exist.ico').isEmpty()).to.be.true()
  281. expect(nativeImage.createFromPath(__dirname).isEmpty()).to.be.true()
  282. expect(nativeImage.createFromPath(__filename).isEmpty()).to.be.true()
  283. })
  284. it('loads images from paths relative to the current working directory', () => {
  285. const imagePath = path.relative('.', path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  286. const image = nativeImage.createFromPath(imagePath)
  287. expect(image.isEmpty()).to.be.false()
  288. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  289. })
  290. it('loads images from paths with `.` segments', () => {
  291. const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`
  292. const image = nativeImage.createFromPath(imagePath)
  293. expect(image.isEmpty()).to.be.false()
  294. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  295. })
  296. it('loads images from paths with `..` segments', () => {
  297. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  298. const image = nativeImage.createFromPath(imagePath)
  299. expect(image.isEmpty()).to.be.false()
  300. expect(image.getSize()).to.deep.equal({ width: 538, height: 190 })
  301. })
  302. it('Gets an NSImage pointer on macOS', function () {
  303. if (process.platform !== 'darwin') {
  304. // FIXME(alexeykuzmin): Skip the test.
  305. // this.skip()
  306. return
  307. }
  308. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  309. const image = nativeImage.createFromPath(imagePath)
  310. const nsimage = image.getNativeHandle()
  311. expect(nsimage).to.have.lengthOf(8)
  312. // If all bytes are null, that's Bad
  313. const allBytesAreNotNull = nsimage.reduce((acc, x) => acc || (x !== 0), false)
  314. expect(allBytesAreNotNull)
  315. })
  316. it('loads images from .ico files on Windows', function () {
  317. if (process.platform !== 'win32') {
  318. // FIXME(alexeykuzmin): Skip the test.
  319. // this.skip()
  320. return
  321. }
  322. const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
  323. const image = nativeImage.createFromPath(imagePath)
  324. expect(image.isEmpty()).to.be.false()
  325. expect(image.getSize()).to.deep.equal({ width: 256, height: 256 })
  326. })
  327. })
  328. describe('createFromNamedImage(name)', () => {
  329. it('returns empty for invalid options', () => {
  330. const image = nativeImage.createFromNamedImage('totally_not_real')
  331. expect(image.isEmpty()).to.be.true()
  332. })
  333. it('returns empty on non-darwin platforms', function () {
  334. if (process.platform === 'darwin') {
  335. // FIXME(alexeykuzmin): Skip the test.
  336. // this.skip()
  337. return
  338. }
  339. const image = nativeImage.createFromNamedImage('NSActionTemplate')
  340. expect(image.isEmpty()).to.be.true()
  341. })
  342. it('returns a valid image on darwin', function () {
  343. if (process.platform !== 'darwin') {
  344. // FIXME(alexeykuzmin): Skip the test.
  345. // this.skip()
  346. return
  347. }
  348. const image = nativeImage.createFromNamedImage('NSActionTemplate')
  349. expect(image.isEmpty()).to.be.false()
  350. })
  351. it('returns allows an HSL shift for a valid image on darwin', function () {
  352. if (process.platform !== 'darwin') {
  353. // FIXME(alexeykuzmin): Skip the test.
  354. // this.skip()
  355. return
  356. }
  357. const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8])
  358. expect(image.isEmpty()).to.be.false()
  359. })
  360. })
  361. describe('resize(options)', () => {
  362. it('returns a resized image', () => {
  363. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  364. for (const [resizeTo, expectedSize] of new Map([
  365. [{}, { width: 538, height: 190 }],
  366. [{ width: 269 }, { width: 269, height: 95 }],
  367. [{ width: 600 }, { width: 600, height: 212 }],
  368. [{ height: 95 }, { width: 269, height: 95 }],
  369. [{ height: 200 }, { width: 566, height: 200 }],
  370. [{ width: 80, height: 65 }, { width: 80, height: 65 }],
  371. [{ width: 600, height: 200 }, { width: 600, height: 200 }],
  372. [{ width: 0, height: 0 }, { width: 0, height: 0 }],
  373. [{ width: -1, height: -1 }, { width: 0, height: 0 }]
  374. ])) {
  375. const actualSize = image.resize(resizeTo).getSize()
  376. expect(actualSize).to.deep.equal(expectedSize)
  377. }
  378. })
  379. it('returns an empty image when called on an empty image', () => {
  380. expect(nativeImage.createEmpty().resize({ width: 1, height: 1 }).isEmpty()).to.be.true()
  381. expect(nativeImage.createEmpty().resize({ width: 0, height: 0 }).isEmpty()).to.be.true()
  382. })
  383. it('supports a quality option', () => {
  384. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  385. const good = image.resize({ width: 100, height: 100, quality: 'good' })
  386. const better = image.resize({ width: 100, height: 100, quality: 'better' })
  387. const best = image.resize({ width: 100, height: 100, quality: 'best' })
  388. expect(good.toPNG()).to.have.lengthOf.at.most(better.toPNG().length)
  389. expect(better.toPNG()).to.have.lengthOf.below(best.toPNG().length)
  390. })
  391. })
  392. describe('crop(bounds)', () => {
  393. it('returns an empty image when called on an empty image', () => {
  394. expect(nativeImage.createEmpty().crop({ width: 1, height: 2, x: 0, y: 0 }).isEmpty()).to.be.true()
  395. expect(nativeImage.createEmpty().crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true()
  396. })
  397. it('returns an empty image when the bounds are invalid', () => {
  398. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  399. expect(image.crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true()
  400. expect(image.crop({ width: -1, height: 10, x: 0, y: 0 }).isEmpty()).to.be.true()
  401. expect(image.crop({ width: 10, height: -35, x: 0, y: 0 }).isEmpty()).to.be.true()
  402. expect(image.crop({ width: 100, height: 100, x: 1000, y: 1000 }).isEmpty()).to.be.true()
  403. })
  404. it('returns a cropped image', () => {
  405. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  406. const cropA = image.crop({ width: 25, height: 64, x: 0, y: 0 })
  407. const cropB = image.crop({ width: 25, height: 64, x: 30, y: 40 })
  408. expect(cropA.getSize()).to.deep.equal({ width: 25, height: 64 })
  409. expect(cropB.getSize()).to.deep.equal({ width: 25, height: 64 })
  410. expect(cropA.toPNG().equals(cropB.toPNG())).to.be.false()
  411. })
  412. })
  413. describe('getAspectRatio()', () => {
  414. it('returns an aspect ratio of an empty image', () => {
  415. expect(nativeImage.createEmpty().getAspectRatio()).to.equal(1.0)
  416. })
  417. it('returns an aspect ratio of an image', () => {
  418. const imageData = getImage({ filename: 'logo.png' })
  419. // imageData.width / imageData.height = 2.831578947368421
  420. const expectedAspectRatio = 2.8315789699554443
  421. const image = nativeImage.createFromPath(imageData.path)
  422. expect(image.getAspectRatio()).to.equal(expectedAspectRatio)
  423. })
  424. })
  425. describe('addRepresentation()', () => {
  426. it('does not add representation when the buffer is too small', () => {
  427. const image = nativeImage.createEmpty()
  428. image.addRepresentation({
  429. buffer: Buffer.from([1, 2, 3, 4]),
  430. width: 100,
  431. height: 100
  432. })
  433. expect(image.isEmpty()).to.be.true()
  434. })
  435. it('supports adding a buffer representation for a scale factor', () => {
  436. const image = nativeImage.createEmpty()
  437. const imageDataOne = getImage({ width: 1, height: 1 })
  438. image.addRepresentation({
  439. scaleFactor: 1.0,
  440. buffer: nativeImage.createFromPath(imageDataOne.path).toPNG()
  441. })
  442. const imageDataTwo = getImage({ width: 2, height: 2 })
  443. image.addRepresentation({
  444. scaleFactor: 2.0,
  445. buffer: nativeImage.createFromPath(imageDataTwo.path).toPNG()
  446. })
  447. const imageDataThree = getImage({ width: 3, height: 3 })
  448. image.addRepresentation({
  449. scaleFactor: 3.0,
  450. buffer: nativeImage.createFromPath(imageDataThree.path).toPNG()
  451. })
  452. image.addRepresentation({
  453. scaleFactor: 4.0,
  454. buffer: 'invalid'
  455. })
  456. expect(image.isEmpty()).to.be.false()
  457. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 })
  458. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  459. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  460. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl)
  461. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl)
  462. })
  463. it('supports adding a data URL representation for a scale factor', () => {
  464. const image = nativeImage.createEmpty()
  465. const imageDataOne = getImage({ width: 1, height: 1 })
  466. image.addRepresentation({
  467. scaleFactor: 1.0,
  468. dataURL: imageDataOne.dataUrl
  469. })
  470. const imageDataTwo = getImage({ width: 2, height: 2 })
  471. image.addRepresentation({
  472. scaleFactor: 2.0,
  473. dataURL: imageDataTwo.dataUrl
  474. })
  475. const imageDataThree = getImage({ width: 3, height: 3 })
  476. image.addRepresentation({
  477. scaleFactor: 3.0,
  478. dataURL: imageDataThree.dataUrl
  479. })
  480. image.addRepresentation({
  481. scaleFactor: 4.0,
  482. dataURL: 'invalid'
  483. })
  484. expect(image.isEmpty()).to.be.false()
  485. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 })
  486. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  487. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  488. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl)
  489. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl)
  490. })
  491. it('supports adding a representation to an existing image', () => {
  492. const imageDataOne = getImage({ width: 1, height: 1 })
  493. const image = nativeImage.createFromPath(imageDataOne.path)
  494. const imageDataTwo = getImage({ width: 2, height: 2 })
  495. image.addRepresentation({
  496. scaleFactor: 2.0,
  497. dataURL: imageDataTwo.dataUrl
  498. })
  499. const imageDataThree = getImage({ width: 3, height: 3 })
  500. image.addRepresentation({
  501. scaleFactor: 2.0,
  502. dataURL: imageDataThree.dataUrl
  503. })
  504. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl)
  505. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl)
  506. })
  507. })
  508. })