api-native-image-spec.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict'
  2. const assert = require('assert')
  3. const nativeImage = require('electron').nativeImage
  4. const path = require('path')
  5. describe('nativeImage module', () => {
  6. describe('createFromPath(path)', () => {
  7. it('returns an empty image for invalid paths', () => {
  8. assert(nativeImage.createFromPath('').isEmpty())
  9. assert(nativeImage.createFromPath('does-not-exist.png').isEmpty())
  10. assert(nativeImage.createFromPath('does-not-exist.ico').isEmpty())
  11. })
  12. it('loads images from paths relative to the current working directory', () => {
  13. const imagePath = `.${path.sep}${path.join('spec', 'fixtures', 'assets', 'logo.png')}`
  14. const image = nativeImage.createFromPath(imagePath)
  15. assert(!image.isEmpty())
  16. assert.equal(image.getSize().height, 190)
  17. assert.equal(image.getSize().width, 538)
  18. })
  19. it('loads images from paths with `.` segments', () => {
  20. const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`
  21. const image = nativeImage.createFromPath(imagePath)
  22. assert(!image.isEmpty())
  23. assert.equal(image.getSize().height, 190)
  24. assert.equal(image.getSize().width, 538)
  25. })
  26. it('loads images from paths with `..` segments', () => {
  27. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  28. const image = nativeImage.createFromPath(imagePath)
  29. assert(!image.isEmpty())
  30. assert.equal(image.getSize().height, 190)
  31. assert.equal(image.getSize().width, 538)
  32. })
  33. it('Gets an NSImage pointer on macOS', () => {
  34. if (process.platform !== 'darwin') return
  35. const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
  36. const image = nativeImage.createFromPath(imagePath)
  37. const nsimage = image.getNativeHandle()
  38. assert.equal(nsimage.length, 8)
  39. // If all bytes are null, that's Bad
  40. assert.equal(nsimage.reduce((acc, x) => acc || (x !== 0), false), true)
  41. })
  42. it('loads images from .ico files on Windows', () => {
  43. if (process.platform !== 'win32') return
  44. const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
  45. const image = nativeImage.createFromPath(imagePath)
  46. assert(!image.isEmpty())
  47. assert.equal(image.getSize().height, 256)
  48. assert.equal(image.getSize().width, 256)
  49. })
  50. })
  51. })