api-native-image-spec.js 24 KB

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