2016-03-25 20:03:49 +00:00
|
|
|
'use strict'
|
2016-03-04 22:54:12 +00:00
|
|
|
|
2016-03-25 20:03:49 +00:00
|
|
|
const assert = require('assert')
|
2016-10-04 17:50:33 +00:00
|
|
|
const {nativeImage} = require('electron')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2016-03-04 20:03:13 +00:00
|
|
|
|
2016-03-04 22:54:12 +00:00
|
|
|
describe('nativeImage module', () => {
|
2016-10-04 18:05:07 +00:00
|
|
|
describe('createEmpty()', () => {
|
|
|
|
it('returns an empty image', () => {
|
|
|
|
assert(nativeImage.createEmpty().isEmpty())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('createFromBuffer(buffer, scaleFactor)', () => {
|
|
|
|
it('returns an empty image when the buffer is empty', () => {
|
|
|
|
assert(nativeImage.createFromBuffer(Buffer.from([])).isEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an image created from the given buffer', () => {
|
|
|
|
const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
|
|
|
|
const imageB = nativeImage.createFromBuffer(imageA.toPNG())
|
|
|
|
assert.deepEqual(imageB.getSize(), {width: 538, height: 190})
|
|
|
|
assert(imageA.toBitmap().equals(imageB.toBitmap()))
|
|
|
|
|
|
|
|
const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100))
|
|
|
|
assert.deepEqual(imageC.getSize(), {width: 538, height: 190})
|
2016-12-09 23:12:36 +00:00
|
|
|
|
|
|
|
const imageD = nativeImage.createFromBuffer(imageA.toBitmap(), 538, 190)
|
|
|
|
assert.deepEqual(imageD.getSize(), {width: 538, height: 190})
|
2016-10-04 18:05:07 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('createFromDataURL(dataURL)', () => {
|
|
|
|
it('returns an empty image when the dataURL is empty', () => {
|
|
|
|
assert(nativeImage.createFromDataURL('').isEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an image created from the given buffer', () => {
|
|
|
|
const imageA = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
const imageB = nativeImage.createFromDataURL(imageA.toDataURL())
|
|
|
|
assert.deepEqual(imageB.getSize(), {width: 538, height: 190})
|
|
|
|
assert(imageA.toBitmap().equals(imageB.toBitmap()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-03-04 22:54:12 +00:00
|
|
|
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())
|
2016-09-21 17:48:24 +00:00
|
|
|
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())
|
2016-10-04 17:52:02 +00:00
|
|
|
assert.deepEqual(image.getSize(), {width: 538, height: 190})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
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())
|
2016-10-04 17:52:02 +00:00
|
|
|
assert.deepEqual(image.getSize(), {width: 538, height: 190})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
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())
|
2016-10-04 17:52:02 +00:00
|
|
|
assert.deepEqual(image.getSize(), {width: 538, height: 190})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
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())
|
2016-10-04 17:52:02 +00:00
|
|
|
assert.deepEqual(image.getSize(), {width: 256, height: 256})
|
2016-06-28 18:58:34 +00:00
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2016-10-04 17:50:33 +00:00
|
|
|
|
|
|
|
describe('resize(options)', () => {
|
|
|
|
it('returns a resized image', () => {
|
|
|
|
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
assert.deepEqual(image.resize({}).getSize(), {width: 538, height: 190})
|
2016-10-05 16:15:06 +00:00
|
|
|
assert.deepEqual(image.resize({width: 269}).getSize(), {width: 269, height: 95})
|
|
|
|
assert.deepEqual(image.resize({width: 600}).getSize(), {width: 600, height: 212})
|
|
|
|
assert.deepEqual(image.resize({height: 95}).getSize(), {width: 269, height: 95})
|
|
|
|
assert.deepEqual(image.resize({height: 200}).getSize(), {width: 566, height: 200})
|
2016-10-04 17:50:33 +00:00
|
|
|
assert.deepEqual(image.resize({width: 80, height: 65}).getSize(), {width: 80, height: 65})
|
2016-10-05 16:15:06 +00:00
|
|
|
assert.deepEqual(image.resize({width: 600, height: 200}).getSize(), {width: 600, height: 200})
|
2016-10-04 19:50:20 +00:00
|
|
|
assert.deepEqual(image.resize({width: 0, height: 0}).getSize(), {width: 0, height: 0})
|
|
|
|
assert.deepEqual(image.resize({width: -1, height: -1}).getSize(), {width: 0, height: 0})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns an empty image when called on an empty image', () => {
|
|
|
|
assert(nativeImage.createEmpty().resize({width: 1, height: 1}).isEmpty())
|
|
|
|
assert(nativeImage.createEmpty().resize({width: 0, height: 0}).isEmpty())
|
2016-10-04 17:50:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('supports a quality option', () => {
|
|
|
|
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
const good = image.resize({width: 100, height: 100, quality: 'good'})
|
|
|
|
const better = image.resize({width: 100, height: 100, quality: 'better'})
|
|
|
|
const best = image.resize({width: 100, height: 100, quality: 'best'})
|
|
|
|
assert(good.toPNG().length <= better.toPNG().length)
|
|
|
|
assert(better.toPNG().length < best.toPNG().length)
|
|
|
|
})
|
|
|
|
})
|
2016-10-04 18:28:47 +00:00
|
|
|
|
|
|
|
describe('crop(bounds)', () => {
|
2016-10-04 19:50:20 +00:00
|
|
|
it('returns an empty image when called on an empty image', () => {
|
|
|
|
assert(nativeImage.createEmpty().crop({width: 1, height: 2, x: 0, y: 0}).isEmpty())
|
|
|
|
assert(nativeImage.createEmpty().crop({width: 0, height: 0, x: 0, y: 0}).isEmpty())
|
|
|
|
})
|
|
|
|
|
2016-10-04 18:28:47 +00:00
|
|
|
it('returns an empty image when the bounds are invalid', () => {
|
|
|
|
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
assert(image.crop({width: 0, height: 0, x: 0, y: 0}).isEmpty())
|
|
|
|
assert(image.crop({width: -1, height: 10, x: 0, y: 0}).isEmpty())
|
|
|
|
assert(image.crop({width: 10, height: -35, x: 0, y: 0}).isEmpty())
|
|
|
|
assert(image.crop({width: 100, height: 100, x: 1000, y: 1000}).isEmpty())
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns a cropped image', () => {
|
|
|
|
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
|
|
|
|
const cropA = image.crop({width: 25, height: 64, x: 0, y: 0})
|
|
|
|
const cropB = image.crop({width: 25, height: 64, x: 30, y: 40})
|
|
|
|
assert.deepEqual(cropA.getSize(), {width: 25, height: 64})
|
|
|
|
assert.deepEqual(cropB.getSize(), {width: 25, height: 64})
|
|
|
|
assert(!cropA.toPNG().equals(cropB.toPNG()))
|
|
|
|
})
|
|
|
|
})
|
2016-10-05 16:21:28 +00:00
|
|
|
|
|
|
|
describe('getAspectRatio()', () => {
|
|
|
|
it('returns the aspect ratio of the image', () => {
|
|
|
|
assert.equal(nativeImage.createEmpty().getAspectRatio(), 1.0)
|
|
|
|
assert.equal(nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')).getAspectRatio(), 2.8315789699554443)
|
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|