api-native-image-spec.js 22 KB

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