api-native-image-spec.js 23 KB

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