electron/spec/api-native-image-spec.js

65 lines
2.5 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
'use strict'
2016-03-25 20:03:49 +00:00
const assert = require('assert')
const nativeImage = require('electron').nativeImage
const path = require('path')
describe('nativeImage module', () => {
describe('createFromPath(path)', () => {
2016-03-07 22:19:24 +00:00
it('returns an empty image for invalid paths', () => {
2016-03-25 20:03:49 +00:00
assert(nativeImage.createFromPath('').isEmpty())
assert(nativeImage.createFromPath('does-not-exist.png').isEmpty())
2016-06-28 18:46:04 +00:00
assert(nativeImage.createFromPath('does-not-exist.ico').isEmpty())
assert(nativeImage.createFromPath(__dirname).isEmpty())
assert(nativeImage.createFromPath(__filename).isEmpty())
2016-03-25 20:03:49 +00:00
})
2016-03-07 22:19:24 +00:00
it('loads images from paths relative to the current working directory', () => {
2016-03-25 20:03:49 +00:00
const imagePath = `.${path.sep}${path.join('spec', 'fixtures', 'assets', 'logo.png')}`
const image = nativeImage.createFromPath(imagePath)
assert(!image.isEmpty())
assert.equal(image.getSize().height, 190)
assert.equal(image.getSize().width, 538)
})
2016-03-07 22:19:24 +00:00
it('loads images from paths with `.` segments', () => {
2016-03-25 20:03:49 +00:00
const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`
const image = nativeImage.createFromPath(imagePath)
assert(!image.isEmpty())
assert.equal(image.getSize().height, 190)
assert.equal(image.getSize().width, 538)
})
2016-03-07 22:19:24 +00:00
2016-03-07 22:22:55 +00:00
it('loads images from paths with `..` segments', () => {
2016-03-25 20:03:49 +00:00
const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
const image = nativeImage.createFromPath(imagePath)
assert(!image.isEmpty())
assert.equal(image.getSize().height, 190)
assert.equal(image.getSize().width, 538)
})
2016-03-14 03:25:49 +00:00
2016-06-18 13:26:26 +00:00
it('Gets an NSImage pointer on macOS', () => {
2016-03-25 20:03:49 +00:00
if (process.platform !== 'darwin') return
2016-03-14 03:25:49 +00:00
2016-03-25 20:03:49 +00:00
const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`
const image = nativeImage.createFromPath(imagePath)
const nsimage = image.getNativeHandle()
2016-03-14 03:25:49 +00:00
2016-03-25 20:03:49 +00:00
assert.equal(nsimage.length, 8)
2016-03-14 03:25:49 +00:00
// If all bytes are null, that's Bad
2016-03-28 23:19:18 +00:00
assert.equal(nsimage.reduce((acc, x) => acc || (x !== 0), false), true)
2016-03-25 20:03:49 +00:00
})
2016-06-28 18:58:34 +00:00
it('loads images from .ico files on Windows', () => {
if (process.platform !== 'win32') return
const imagePath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')
const image = nativeImage.createFromPath(imagePath)
assert(!image.isEmpty())
assert.equal(image.getSize().height, 256)
assert.equal(image.getSize().width, 256)
})
2016-03-25 20:03:49 +00:00
})
})