api-native-image-spec.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. ifit(process.platform === 'darwin')('Gets an NSImage pointer on macOS', function () {
  310. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`;
  311. const image = nativeImage.createFromPath(imagePath);
  312. const nsimage = image.getNativeHandle();
  313. expect(nsimage).to.have.lengthOf(8);
  314. // If all bytes are null, that's Bad
  315. const allBytesAreNotNull = nsimage.reduce((acc, x) => acc || (x !== 0), false);
  316. expect(allBytesAreNotNull);
  317. });
  318. ifit(process.platform === 'win32')('loads images from .ico files on Windows', function () {
  319. const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico');
  320. const image = nativeImage.createFromPath(imagePath);
  321. expect(image.isEmpty()).to.be.false();
  322. expect(image.getSize()).to.deep.equal({ width: 256, height: 256 });
  323. });
  324. });
  325. describe('createFromNamedImage(name)', () => {
  326. it('returns empty for invalid options', () => {
  327. const image = nativeImage.createFromNamedImage('totally_not_real');
  328. expect(image.isEmpty()).to.be.true();
  329. });
  330. ifit(process.platform !== 'darwin')('returns empty on non-darwin platforms', function () {
  331. const image = nativeImage.createFromNamedImage('NSActionTemplate');
  332. expect(image.isEmpty()).to.be.true();
  333. });
  334. ifit(process.platform === 'darwin')('returns a valid image on darwin', function () {
  335. const image = nativeImage.createFromNamedImage('NSActionTemplate');
  336. expect(image.isEmpty()).to.be.false();
  337. });
  338. ifit(process.platform === 'darwin')('returns allows an HSL shift for a valid image on darwin', function () {
  339. const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8]);
  340. expect(image.isEmpty()).to.be.false();
  341. });
  342. });
  343. describe('resize(options)', () => {
  344. it('returns a resized image', () => {
  345. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
  346. for (const [resizeTo, expectedSize] of new Map([
  347. [{}, { width: 538, height: 190 }],
  348. [{ width: 269 }, { width: 269, height: 95 }],
  349. [{ width: 600 }, { width: 600, height: 212 }],
  350. [{ height: 95 }, { width: 269, height: 95 }],
  351. [{ height: 200 }, { width: 566, height: 200 }],
  352. [{ width: 80, height: 65 }, { width: 80, height: 65 }],
  353. [{ width: 600, height: 200 }, { width: 600, height: 200 }],
  354. [{ width: 0, height: 0 }, { width: 0, height: 0 }],
  355. [{ width: -1, height: -1 }, { width: 0, height: 0 }]
  356. ])) {
  357. const actualSize = image.resize(resizeTo).getSize();
  358. expect(actualSize).to.deep.equal(expectedSize);
  359. }
  360. });
  361. it('returns an empty image when called on an empty image', () => {
  362. expect(nativeImage.createEmpty().resize({ width: 1, height: 1 }).isEmpty()).to.be.true();
  363. expect(nativeImage.createEmpty().resize({ width: 0, height: 0 }).isEmpty()).to.be.true();
  364. });
  365. it('supports a quality option', () => {
  366. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
  367. const good = image.resize({ width: 100, height: 100, quality: 'good' });
  368. const better = image.resize({ width: 100, height: 100, quality: 'better' });
  369. const best = image.resize({ width: 100, height: 100, quality: 'best' });
  370. expect(good.toPNG()).to.have.lengthOf.at.most(better.toPNG().length);
  371. expect(better.toPNG()).to.have.lengthOf.below(best.toPNG().length);
  372. });
  373. });
  374. describe('crop(bounds)', () => {
  375. it('returns an empty image when called on an empty image', () => {
  376. expect(nativeImage.createEmpty().crop({ width: 1, height: 2, x: 0, y: 0 }).isEmpty()).to.be.true();
  377. expect(nativeImage.createEmpty().crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true();
  378. });
  379. it('returns an empty image when the bounds are invalid', () => {
  380. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
  381. expect(image.crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true();
  382. expect(image.crop({ width: -1, height: 10, x: 0, y: 0 }).isEmpty()).to.be.true();
  383. expect(image.crop({ width: 10, height: -35, x: 0, y: 0 }).isEmpty()).to.be.true();
  384. expect(image.crop({ width: 100, height: 100, x: 1000, y: 1000 }).isEmpty()).to.be.true();
  385. });
  386. it('returns a cropped image', () => {
  387. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
  388. const cropA = image.crop({ width: 25, height: 64, x: 0, y: 0 });
  389. const cropB = image.crop({ width: 25, height: 64, x: 30, y: 40 });
  390. expect(cropA.getSize()).to.deep.equal({ width: 25, height: 64 });
  391. expect(cropB.getSize()).to.deep.equal({ width: 25, height: 64 });
  392. expect(cropA.toPNG().equals(cropB.toPNG())).to.be.false();
  393. });
  394. it('toBitmap() returns a buffer of the right size', () => {
  395. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'));
  396. const crop = image.crop({ width: 25, height: 64, x: 0, y: 0 });
  397. expect(crop.toBitmap().length).to.equal(25 * 64 * 4);
  398. });
  399. });
  400. describe('getAspectRatio()', () => {
  401. it('returns an aspect ratio of an empty image', () => {
  402. expect(nativeImage.createEmpty().getAspectRatio()).to.equal(1.0);
  403. });
  404. it('returns an aspect ratio of an image', () => {
  405. const imageData = getImage({ filename: 'logo.png' });
  406. // imageData.width / imageData.height = 2.831578947368421
  407. const expectedAspectRatio = 2.8315789699554443;
  408. const image = nativeImage.createFromPath(imageData.path);
  409. expect(image.getAspectRatio()).to.equal(expectedAspectRatio);
  410. });
  411. });
  412. ifdescribe(process.platform !== 'linux')('createThumbnailFromPath(path, size)', () => {
  413. it('throws when invalid size is passed', async () => {
  414. const badSize = { width: -1, height: -1 };
  415. await expect(
  416. nativeImage.createThumbnailFromPath('path', badSize)
  417. ).to.eventually.be.rejectedWith('size must not be empty');
  418. });
  419. it('throws when a bad path is passed', async () => {
  420. const badPath = process.platform === 'win32' ? '\\hey\\hi\\hello' : '/hey/hi/hello';
  421. const goodSize = { width: 100, height: 100 };
  422. await expect(
  423. nativeImage.createThumbnailFromPath(badPath, goodSize)
  424. ).to.eventually.be.rejected();
  425. });
  426. it('returns native image given valid params', async () => {
  427. const goodPath = path.join(__dirname, 'fixtures', 'assets', 'logo.png');
  428. const goodSize = { width: 100, height: 100 };
  429. const result = await nativeImage.createThumbnailFromPath(goodPath, goodSize);
  430. expect(result.isEmpty()).to.equal(false);
  431. });
  432. });
  433. describe('addRepresentation()', () => {
  434. it('does not add representation when the buffer is too small', () => {
  435. const image = nativeImage.createEmpty();
  436. image.addRepresentation({
  437. buffer: Buffer.from([1, 2, 3, 4]),
  438. width: 100,
  439. height: 100
  440. });
  441. expect(image.isEmpty()).to.be.true();
  442. });
  443. it('supports adding a buffer representation for a scale factor', () => {
  444. const image = nativeImage.createEmpty();
  445. const imageDataOne = getImage({ width: 1, height: 1 });
  446. image.addRepresentation({
  447. scaleFactor: 1.0,
  448. buffer: nativeImage.createFromPath(imageDataOne.path).toPNG()
  449. });
  450. expect(image.getScaleFactors()).to.deep.equal([1]);
  451. const imageDataTwo = getImage({ width: 2, height: 2 });
  452. image.addRepresentation({
  453. scaleFactor: 2.0,
  454. buffer: nativeImage.createFromPath(imageDataTwo.path).toPNG()
  455. });
  456. expect(image.getScaleFactors()).to.deep.equal([1, 2]);
  457. const imageDataThree = getImage({ width: 3, height: 3 });
  458. image.addRepresentation({
  459. scaleFactor: 3.0,
  460. buffer: nativeImage.createFromPath(imageDataThree.path).toPNG()
  461. });
  462. expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]);
  463. image.addRepresentation({
  464. scaleFactor: 4.0,
  465. buffer: 'invalid'
  466. });
  467. // this one failed, so it shouldn't show up in the scale factors
  468. expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]);
  469. expect(image.isEmpty()).to.be.false();
  470. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 });
  471. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
  472. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
  473. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl);
  474. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl);
  475. });
  476. it('supports adding a data URL representation for a scale factor', () => {
  477. const image = nativeImage.createEmpty();
  478. const imageDataOne = getImage({ width: 1, height: 1 });
  479. image.addRepresentation({
  480. scaleFactor: 1.0,
  481. dataURL: imageDataOne.dataUrl
  482. });
  483. const imageDataTwo = getImage({ width: 2, height: 2 });
  484. image.addRepresentation({
  485. scaleFactor: 2.0,
  486. dataURL: imageDataTwo.dataUrl
  487. });
  488. const imageDataThree = getImage({ width: 3, height: 3 });
  489. image.addRepresentation({
  490. scaleFactor: 3.0,
  491. dataURL: imageDataThree.dataUrl
  492. });
  493. image.addRepresentation({
  494. scaleFactor: 4.0,
  495. dataURL: 'invalid'
  496. });
  497. expect(image.isEmpty()).to.be.false();
  498. expect(image.getSize()).to.deep.equal({ width: 1, height: 1 });
  499. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
  500. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
  501. expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl);
  502. expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl);
  503. });
  504. it('supports adding a representation to an existing image', () => {
  505. const imageDataOne = getImage({ width: 1, height: 1 });
  506. const image = nativeImage.createFromPath(imageDataOne.path);
  507. const imageDataTwo = getImage({ width: 2, height: 2 });
  508. image.addRepresentation({
  509. scaleFactor: 2.0,
  510. dataURL: imageDataTwo.dataUrl
  511. });
  512. const imageDataThree = getImage({ width: 3, height: 3 });
  513. image.addRepresentation({
  514. scaleFactor: 2.0,
  515. dataURL: imageDataThree.dataUrl
  516. });
  517. expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
  518. expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
  519. });
  520. });
  521. });