api-native-image-spec.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. 'use strict'
  2. const assert = require('assert')
  3. const {nativeImage} = require('electron')
  4. const path = require('path')
  5. describe('nativeImage module', () => {
  6. describe('createEmpty()', () => {
  7. it('returns an empty image', () => {
  8. assert(nativeImage.createEmpty().isEmpty())
  9. })
  10. })
  11. describe('createFromBuffer(buffer, scaleFactor)', () => {
  12. it('returns an empty image when the buffer is empty', () => {
  13. assert(nativeImage.createFromBuffer(Buffer.from([])).isEmpty())
  14. })
  15. it('returns an image created from the given buffer', () => {
  16. const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  17. const imageB = nativeImage.createFromBuffer(imageA.toPNG())
  18. assert.deepEqual(imageB.getSize(), {width: 538, height: 190})
  19. assert(imageA.toBitmap().equals(imageB.toBitmap()))
  20. const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100))
  21. assert.deepEqual(imageC.getSize(), {width: 538, height: 190})
  22. const imageD = nativeImage.createFromBuffer(imageA.toBitmap(),
  23. {width: 538, height: 190})
  24. assert.deepEqual(imageD.getSize(), {width: 538, height: 190})
  25. const imageE = nativeImage.createFromBuffer(imageA.toBitmap(),
  26. {width: 100, height: 200})
  27. assert.deepEqual(imageE.getSize(), {width: 100, height: 200})
  28. const imageF = nativeImage.createFromBuffer(imageA.toBitmap())
  29. assert(imageF.isEmpty())
  30. const imageG = nativeImage.createFromBuffer(imageA.toPNG(),
  31. {width: 100, height: 200})
  32. assert.deepEqual(imageG.getSize(), {width: 538, height: 190})
  33. const imageH = nativeImage.createFromBuffer(imageA.toJPEG(100),
  34. {width: 100, height: 200})
  35. assert.deepEqual(imageH.getSize(), {width: 538, height: 190})
  36. const imageI = nativeImage.createFromBuffer(imageA.toBitmap(),
  37. {width: 538, height: 190, scaleFactor: 2.0})
  38. assert.deepEqual(imageI.getSize(), {width: 269, height: 95})
  39. const imageJ = nativeImage.createFromBuffer(imageA.toPNG(), 2.0)
  40. assert.deepEqual(imageJ.getSize(), {width: 269, height: 95})
  41. })
  42. })
  43. describe('createFromDataURL(dataURL)', () => {
  44. it('returns an empty image when the dataURL is empty', () => {
  45. assert(nativeImage.createFromDataURL('').isEmpty())
  46. })
  47. it('returns an image created from the given buffer', () => {
  48. const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  49. const imageB = nativeImage.createFromDataURL(imageA.toDataURL())
  50. assert.deepEqual(imageB.getSize(), {width: 538, height: 190})
  51. assert(imageA.toBitmap().equals(imageB.toBitmap()))
  52. })
  53. })
  54. describe('createFromPath(path)', () => {
  55. it('returns an empty image for invalid paths', () => {
  56. assert(nativeImage.createFromPath('').isEmpty())
  57. assert(nativeImage.createFromPath('does-not-exist.png').isEmpty())
  58. assert(nativeImage.createFromPath('does-not-exist.ico').isEmpty())
  59. assert(nativeImage.createFromPath(__dirname).isEmpty())
  60. assert(nativeImage.createFromPath(__filename).isEmpty())
  61. })
  62. it('loads images from paths relative to the current working directory', () => {
  63. const imagePath = `.${path.sep}${path.join('spec', 'fixtures', 'assets', 'logo.png')}`
  64. const image = nativeImage.createFromPath(imagePath)
  65. assert(!image.isEmpty())
  66. assert.deepEqual(image.getSize(), {width: 538, height: 190})
  67. })
  68. it('loads images from paths with `.` segments', () => {
  69. const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`
  70. const image = nativeImage.createFromPath(imagePath)
  71. assert(!image.isEmpty())
  72. assert.deepEqual(image.getSize(), {width: 538, height: 190})
  73. })
  74. it('loads images from paths with `..` segments', () => {
  75. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  76. const image = nativeImage.createFromPath(imagePath)
  77. assert(!image.isEmpty())
  78. assert.deepEqual(image.getSize(), {width: 538, height: 190})
  79. })
  80. it('Gets an NSImage pointer on macOS', () => {
  81. if (process.platform !== 'darwin') return
  82. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  83. const image = nativeImage.createFromPath(imagePath)
  84. const nsimage = image.getNativeHandle()
  85. assert.equal(nsimage.length, 8)
  86. // If all bytes are null, that's Bad
  87. assert.equal(nsimage.reduce((acc, x) => acc || (x !== 0), false), true)
  88. })
  89. it('loads images from .ico files on Windows', () => {
  90. if (process.platform !== 'win32') return
  91. const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
  92. const image = nativeImage.createFromPath(imagePath)
  93. assert(!image.isEmpty())
  94. assert.deepEqual(image.getSize(), {width: 256, height: 256})
  95. })
  96. })
  97. describe('resize(options)', () => {
  98. it('returns a resized image', () => {
  99. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  100. assert.deepEqual(image.resize({}).getSize(), {width: 538, height: 190})
  101. assert.deepEqual(image.resize({width: 269}).getSize(), {width: 269, height: 95})
  102. assert.deepEqual(image.resize({width: 600}).getSize(), {width: 600, height: 212})
  103. assert.deepEqual(image.resize({height: 95}).getSize(), {width: 269, height: 95})
  104. assert.deepEqual(image.resize({height: 200}).getSize(), {width: 566, height: 200})
  105. assert.deepEqual(image.resize({width: 80, height: 65}).getSize(), {width: 80, height: 65})
  106. assert.deepEqual(image.resize({width: 600, height: 200}).getSize(), {width: 600, height: 200})
  107. assert.deepEqual(image.resize({width: 0, height: 0}).getSize(), {width: 0, height: 0})
  108. assert.deepEqual(image.resize({width: -1, height: -1}).getSize(), {width: 0, height: 0})
  109. })
  110. it('returns an empty image when called on an empty image', () => {
  111. assert(nativeImage.createEmpty().resize({width: 1, height: 1}).isEmpty())
  112. assert(nativeImage.createEmpty().resize({width: 0, height: 0}).isEmpty())
  113. })
  114. it('supports a quality option', () => {
  115. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  116. const good = image.resize({width: 100, height: 100, quality: 'good'})
  117. const better = image.resize({width: 100, height: 100, quality: 'better'})
  118. const best = image.resize({width: 100, height: 100, quality: 'best'})
  119. assert(good.toPNG().length <= better.toPNG().length)
  120. assert(better.toPNG().length < best.toPNG().length)
  121. })
  122. })
  123. describe('crop(bounds)', () => {
  124. it('returns an empty image when called on an empty image', () => {
  125. assert(nativeImage.createEmpty().crop({width: 1, height: 2, x: 0, y: 0}).isEmpty())
  126. assert(nativeImage.createEmpty().crop({width: 0, height: 0, x: 0, y: 0}).isEmpty())
  127. })
  128. it('returns an empty image when the bounds are invalid', () => {
  129. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  130. assert(image.crop({width: 0, height: 0, x: 0, y: 0}).isEmpty())
  131. assert(image.crop({width: -1, height: 10, x: 0, y: 0}).isEmpty())
  132. assert(image.crop({width: 10, height: -35, x: 0, y: 0}).isEmpty())
  133. assert(image.crop({width: 100, height: 100, x: 1000, y: 1000}).isEmpty())
  134. })
  135. it('returns a cropped image', () => {
  136. const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
  137. const cropA = image.crop({width: 25, height: 64, x: 0, y: 0})
  138. const cropB = image.crop({width: 25, height: 64, x: 30, y: 40})
  139. assert.deepEqual(cropA.getSize(), {width: 25, height: 64})
  140. assert.deepEqual(cropB.getSize(), {width: 25, height: 64})
  141. assert(!cropA.toPNG().equals(cropB.toPNG()))
  142. })
  143. })
  144. describe('getAspectRatio()', () => {
  145. it('returns the aspect ratio of the image', () => {
  146. assert.equal(nativeImage.createEmpty().getAspectRatio(), 1.0)
  147. assert.equal(nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')).getAspectRatio(), 2.8315789699554443)
  148. })
  149. })
  150. })