api-native-image-spec.js 24 KB

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