api-native-image-spec.js 20 KB

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