electron/spec/api-native-image-spec.ts
trop[bot] 85a8bfaa31
chore: bump chromium to 139.0.7256.0 (38-x-y) (#47615)
* chore: bump chromium in DEPS to 139.0.7242.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: update render_widget_host_view_mac.patch

no code changes; just updating patch context

Do a cleanup pass on the history swiper code | 6604367

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update mas_avoid_private_macos_api_usage.patch.patch

no code changes; just updating patch context

[tracing] Delete base/trace_event/base_tracing.h | 6624012

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update chore_provide_iswebcontentscreationoverridden_with_full_params.patch

no manual changes; just updating patch context

[ActorFramework] Refactor Actor Task Management | 6618684

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update fix_move_autopipsettingshelper_behind_branding_buildflag.patch

[pip] Tuck picture-in-picture windows when a file dialog is open | 6449682

Reland "[document pip] Restrict the size that a website can request" | 6372104

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update feat_corner_smoothing_css_rule_and_blink_painting.patch

Xref: corner-shape: constraint radii based on opposite corner overlap | 6592572

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update revert_code_health_clean_up_stale_macwebcontentsocclusion.patch

no manual changes; just updating patch context

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: update fix_rename_sqlite_win32_exports_to_avoid_conflicts_with_node_js.patch

no code changes; just updating patch context

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: e patches all

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* Plumb Verify2QwacBinding and hook it up in QwacWebContentsObserver

6624719

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Remove host delegate OnMainFrameCreatedForBackgroundPage

6631123

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Extensions: Rename GetResourceURL to ResolveExtensionURL

6625053

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Consolidate NativeFrameViewMac

6614239

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* ICWYU

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Remove dead code WidgetAXTreeIDMap

6619701

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Reland "extensions: Add `WillPrepareForEvaluation` to setup MojoJS"

6630056

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* NavigationThrottleRunner2: Remove MaybeAddThrottle

6628079

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Tuck picture-in-picture windows when a file dialog is open

6449682

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* build: fix snapshot_blob.bin build error

xref: https://issues.chromium.org/issues/416540976

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: e patches all

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* build: freeup disk space on macos

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 139.0.7244.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: update printing.patch

no manual changes; just updating patch context

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: remove upstreamed ignore_parse_errors_for_resolveshortcutproperties.patch

Prevent Windows crash on unexpected shortcut type | 6633298

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: e patches all

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* Revert "Reland "extensions: Add `WillPrepareForEvaluation` to setup MojoJS""

This reverts commit 77c4f967a637f7e8970114f91311f9fddede0f7c.

Revert CL for the high confidence crash culprit for http://crash/28f897bb9743dfe0 | 6641819

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* Fix spec's expected base64-encoded PNG strings to match upstream changes.

[rust png] Enable by default. | 6085801

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: bump chromium in DEPS to 139.0.7246.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: e patches all

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: bump chromium in DEPS to 139.0.7248.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* siso: Enable Siso by default for non-Google builds

6638830

Disabling for now until we are ready to build siso on all platforms.

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Revert "revert Don't use static variable for UseExternalPopupMenus"

This reverts commit e91e3894e6c34cc0ffe69ed45417c0ebec882fb1.

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Update mac_sdk_min to match minimum required SDK version

6493969
(cherry picked from commit 3e7cbe912d8fe1062d68ed06968aaee22013985f)

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Use default window styling on Mac

6648665

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* Reland "Force the unintentional renderer process creation check by default"

6626905

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* fixup: Reland "Force the unintentional renderer process creation check by default

6626905

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 139.0.7249.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* fixup: Reland "Force the unintentional renderer process creation check by default

6626905

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: update patches

Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>

* chore: bump chromium in DEPS to 139.0.7250.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: bump chromium in DEPS to 139.0.7252.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: bump chromium in DEPS to 139.0.7254.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* 6638187: browser level TOCTOU check for coordinate target

6638187

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: fixup patch indices

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: add missing base/notimplemented includes

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* 6652910: [Frame Cleanup] Push down/hide implementation-specific API

6652910

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: bump chromium in DEPS to 139.0.7256.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* chore: fix lint

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fixup! 6652910: [Frame Cleanup] Push down/hide implementation-specific API

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* fix: move HandleScope location

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: bump chromium in DEPS to 139.0.7258.0

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>

* fixup! [NonClientFrameView] Consolidate NativeFrameViewMac

Co-authored-by: deepak1556 <hop2deep@gmail.com>

* Revert "chore: bump chromium in DEPS to 139.0.7258.0"

This reverts commit 264b2e934f4b2705c47d9761010052b95d9dd5de.

Co-authored-by: deepak1556 <hop2deep@gmail.com>

* chore: update patches

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
Co-authored-by: patchup[bot] <73610968+patchup[bot]@users.noreply.github.com>
2025-06-30 12:29:54 -04:00

608 lines
25 KiB
TypeScript

import { nativeImage } from 'electron/common';
import { expect } from 'chai';
import * as path from 'node:path';
import { ifdescribe, ifit, itremote, useRemoteContext } from './lib/spec-helpers';
import { expectDeprecationMessages } from './lib/warning-helpers';
describe('nativeImage module', () => {
const fixturesPath = path.join(__dirname, 'fixtures');
const imageLogo = {
path: path.join(fixturesPath, 'assets', 'logo.png'),
width: 538,
height: 190
};
const image1x1 = {
dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADElEQVR4nGJhAAIAAAAA//8MRDDqAAAABklEQVQDAAAZAAXV/wceAAAAAElFTkSuQmCC',
path: path.join(fixturesPath, 'assets', '1x1.png'),
height: 1,
width: 1
};
const image2x2 = {
dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAE0lEQVR4nGL5//8/AwMDCwMYAAAAAP//fpSNxQAAAAZJREFUAwAkPgMGdYltawAAAABJRU5ErkJggg==',
path: path.join(fixturesPath, 'assets', '2x2.jpg'),
height: 2,
width: 2
};
const image3x3 = {
dataUrl: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAMAAAADCAYAAABWKLW/AAABCWlDQ1BfAAB4nJWQsUrDUBSGv6oggqCDg4PDHVyVakEcnKoQXGOE6pbEGIUkDUlKVxdxFsRZxGfQBxFdXPoIDuLsfxswXTr0XM49H+f+nPtzoDVCsdCGNKsK1+ma3tm5WRzR0hmHH5Y500Oq389a+77F7LF0EZWh6rfSK/S5Rh6J1+Kae5aDmhPLwyqvxDeWC889FD+IV+MJDiY4zAurfxEfpMkgbHyzHGWnJ6r7yg1KXBy6GN0+KQMq8ZBr1SvRLttKg0eh90z6SyKsZVOP7D9p1A/M3ze94BHe7mD9q+ltPsPKLbx+NL1mh7lf+P/bmet0png2Y88OfZ2YRE4Mx3IVyqX12maHvT9v1kU+i+h9qQAAAA9JREFUeJxiYUACLDg5AAAAAP//htosgQAAAAZJREFUAwABXwANF48ZOwAAAABJRU5ErkJggg==',
path: path.join(fixturesPath, 'assets', '3x3.png'),
height: 3,
width: 3
};
const dataUrlImages = [
image1x1,
image2x2,
image3x3
];
ifdescribe(process.platform === 'darwin')('isMacTemplateImage state', () => {
describe('with properties', () => {
it('correctly recognizes a template image', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
expect(image.isMacTemplateImage).to.be.false();
const templateImage = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo_Template.png'));
expect(templateImage.isMacTemplateImage).to.be.true();
});
it('sets a template image', function () {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
expect(image.isMacTemplateImage).to.be.false();
image.isMacTemplateImage = true;
expect(image.isMacTemplateImage).to.be.true();
});
});
describe('with functions', () => {
it('correctly recognizes a template image', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
expect(image.isTemplateImage()).to.be.false();
const templateImage = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo_Template.png'));
expect(templateImage.isTemplateImage()).to.be.true();
});
it('sets a template image', function () {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
expect(image.isTemplateImage()).to.be.false();
image.setTemplateImage(true);
expect(image.isTemplateImage()).to.be.true();
});
});
});
describe('createEmpty()', () => {
it('returns an empty image', async () => {
const empty = nativeImage.createEmpty();
expect(empty.isEmpty()).to.be.true();
expect(empty.getAspectRatio()).to.equal(1);
expect(empty.toDataURL()).to.equal('data:image/png;base64,');
expect(empty.toDataURL({ scaleFactor: 2.0 })).to.equal('data:image/png;base64,');
expect(empty.getSize()).to.deep.equal({ width: 0, height: 0 });
await expectDeprecationMessages(
() => {
expect(empty.getBitmap()).to.be.empty();
expect(empty.getBitmap({ scaleFactor: 2.0 })).to.be.empty();
},
'getBitmap() is deprecated, use toBitmap() instead.'
);
expect(empty.toBitmap()).to.be.empty();
expect(empty.toBitmap({ scaleFactor: 2.0 })).to.be.empty();
expect(empty.toJPEG(100)).to.be.empty();
expect(empty.toPNG()).to.be.empty();
expect(empty.toPNG({ scaleFactor: 2.0 })).to.be.empty();
if (process.platform === 'darwin') {
expect(empty.getNativeHandle()).to.be.empty();
}
});
});
describe('createFromBitmap(buffer, options)', () => {
it('returns an empty image when the buffer is empty', () => {
expect(nativeImage.createFromBitmap(Buffer.from([]), { width: 0, height: 0 }).isEmpty()).to.be.true();
});
it('returns an image created from the given buffer', () => {
const imageA = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
const imageB = nativeImage.createFromBitmap(imageA.toBitmap(), imageA.getSize());
expect(imageB.getSize()).to.deep.equal({ width: 538, height: 190 });
const imageC = nativeImage.createFromBuffer(imageA.toBitmap(), { ...imageA.getSize(), scaleFactor: 2.0 });
expect(imageC.getSize()).to.deep.equal({ width: 269, height: 95 });
});
it('throws on invalid arguments', () => {
expect(() => nativeImage.createFromBitmap(null as any, {} as any)).to.throw('buffer must be a node Buffer');
expect(() => nativeImage.createFromBitmap([12, 14, 124, 12] as any, {} as any)).to.throw('buffer must be a node Buffer');
expect(() => nativeImage.createFromBitmap(Buffer.from([]), {} as any)).to.throw('width is required');
expect(() => nativeImage.createFromBitmap(Buffer.from([]), { width: 1 } as any)).to.throw('height is required');
expect(() => nativeImage.createFromBitmap(Buffer.from([]), { width: 1, height: 1 })).to.throw('invalid buffer size');
});
});
describe('createFromBuffer(buffer, options)', () => {
it('returns an empty image when the buffer is empty', () => {
expect(nativeImage.createFromBuffer(Buffer.from([])).isEmpty()).to.be.true();
});
it('returns an empty image when the buffer is too small', () => {
const image = nativeImage.createFromBuffer(Buffer.from([1, 2, 3, 4]), { width: 100, height: 100 });
expect(image.isEmpty()).to.be.true();
});
it('returns an image created from the given buffer', () => {
const imageA = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
const imageB = nativeImage.createFromBuffer(imageA.toPNG());
expect(imageB.getSize()).to.deep.equal({ width: 538, height: 190 });
expect(imageA.toBitmap().equals(imageB.toBitmap())).to.be.true();
const imageC = nativeImage.createFromBuffer(imageA.toJPEG(100));
expect(imageC.getSize()).to.deep.equal({ width: 538, height: 190 });
const imageD = nativeImage.createFromBuffer(imageA.toBitmap(),
{ width: 538, height: 190 });
expect(imageD.getSize()).to.deep.equal({ width: 538, height: 190 });
const imageE = nativeImage.createFromBuffer(imageA.toBitmap(),
{ width: 100, height: 200 });
expect(imageE.getSize()).to.deep.equal({ width: 100, height: 200 });
const imageF = nativeImage.createFromBuffer(imageA.toBitmap());
expect(imageF.isEmpty()).to.be.true();
const imageG = nativeImage.createFromBuffer(imageA.toPNG(),
{ width: 100, height: 200 });
expect(imageG.getSize()).to.deep.equal({ width: 538, height: 190 });
const imageH = nativeImage.createFromBuffer(imageA.toJPEG(100),
{ width: 100, height: 200 });
expect(imageH.getSize()).to.deep.equal({ width: 538, height: 190 });
const imageI = nativeImage.createFromBuffer(imageA.toBitmap(),
{ width: 538, height: 190, scaleFactor: 2.0 });
expect(imageI.getSize()).to.deep.equal({ width: 269, height: 95 });
});
it('throws on invalid arguments', () => {
expect(() => nativeImage.createFromBuffer(null as any)).to.throw('buffer must be a node Buffer');
expect(() => nativeImage.createFromBuffer([12, 14, 124, 12] as any)).to.throw('buffer must be a node Buffer');
});
});
describe('createFromDataURL(dataURL)', () => {
it('returns an empty image from the empty string', () => {
expect(nativeImage.createFromDataURL('').isEmpty()).to.be.true();
});
it('returns an image created from the given string', () => {
for (const imageData of dataUrlImages) {
const imageFromPath = nativeImage.createFromBuffer(
nativeImage.createFromPath(imageData.path).toPNG());
const imageFromDataUrl = nativeImage.createFromDataURL(imageData.dataUrl!);
expect(imageFromDataUrl.isEmpty()).to.be.false();
expect(imageFromDataUrl.getSize()).to.deep.equal(imageFromPath.getSize());
expect(imageFromDataUrl.toBitmap()).to.satisfy(
(bitmap: any) => imageFromPath.toBitmap().equals(bitmap));
expect(imageFromDataUrl.toDataURL()).to.equal(imageFromPath.toDataURL());
}
});
});
describe('toDataURL()', () => {
it('returns a PNG data URL', () => {
for (const imageData of dataUrlImages) {
const imageFromPath = nativeImage.createFromBuffer(
nativeImage.createFromPath(imageData.path!).toPNG());
const scaleFactors = [1.0, 2.0];
for (const scaleFactor of scaleFactors) {
expect(imageFromPath.toDataURL({ scaleFactor })).to.equal(imageData.dataUrl);
}
}
});
it('returns a data URL at 1x scale factor by default', () => {
const imageData = imageLogo;
const image = nativeImage.createFromPath(imageData.path);
const imageOne = nativeImage.createFromBuffer(image.toPNG(), {
width: image.getSize().width,
height: image.getSize().height,
scaleFactor: 2.0
});
expect(imageOne.getSize()).to.deep.equal(
{ width: imageData.width / 2, height: imageData.height / 2 });
const imageTwo = nativeImage.createFromDataURL(imageOne.toDataURL());
expect(imageTwo.getSize()).to.deep.equal(
{ width: imageData.width, height: imageData.height });
expect(imageOne.toBitmap().equals(imageTwo.toBitmap())).to.be.true();
});
it('supports a scale factor', () => {
const imageData = imageLogo;
const image = nativeImage.createFromPath(imageData.path);
const expectedSize = { width: imageData.width, height: imageData.height };
const imageFromDataUrlOne = nativeImage.createFromDataURL(
image.toDataURL({ scaleFactor: 1.0 }));
expect(imageFromDataUrlOne.getSize()).to.deep.equal(expectedSize);
const imageFromDataUrlTwo = nativeImage.createFromDataURL(
image.toDataURL({ scaleFactor: 2.0 }));
expect(imageFromDataUrlTwo.getSize()).to.deep.equal(expectedSize);
});
});
describe('toPNG()', () => {
it('returns a buffer at 1x scale factor by default', () => {
const imageData = imageLogo;
const imageA = nativeImage.createFromPath(imageData.path);
const imageB = nativeImage.createFromBuffer(imageA.toPNG(), {
width: imageA.getSize().width,
height: imageA.getSize().height,
scaleFactor: 2.0
});
expect(imageB.getSize()).to.deep.equal(
{ width: imageData.width / 2, height: imageData.height / 2 });
const imageC = nativeImage.createFromBuffer(imageB.toPNG());
expect(imageC.getSize()).to.deep.equal(
{ width: imageData.width, height: imageData.height });
expect(imageB.toBitmap().equals(imageC.toBitmap())).to.be.true();
});
it('supports a scale factor', () => {
const imageData = imageLogo;
const image = nativeImage.createFromPath(imageData.path);
const imageFromBufferOne = nativeImage.createFromBuffer(
image.toPNG({ scaleFactor: 1.0 }));
expect(imageFromBufferOne.getSize()).to.deep.equal(
{ width: imageData.width, height: imageData.height });
const imageFromBufferTwo = nativeImage.createFromBuffer(
image.toPNG({ scaleFactor: 2.0 }), { scaleFactor: 2.0 });
expect(imageFromBufferTwo.getSize()).to.deep.equal(
{ width: imageData.width / 2, height: imageData.height / 2 });
});
});
describe('createFromPath(path)', () => {
it('returns an empty image for invalid paths', () => {
expect(nativeImage.createFromPath('').isEmpty()).to.be.true();
expect(nativeImage.createFromPath('does-not-exist.png').isEmpty()).to.be.true();
expect(nativeImage.createFromPath('does-not-exist.ico').isEmpty()).to.be.true();
expect(nativeImage.createFromPath(__dirname).isEmpty()).to.be.true();
expect(nativeImage.createFromPath(__filename).isEmpty()).to.be.true();
});
it('loads images from paths relative to the current working directory', () => {
const imagePath = path.relative('.', path.join(fixturesPath, 'assets', 'logo.png'));
const image = nativeImage.createFromPath(imagePath);
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 538, height: 190 });
});
it('loads images from paths with `.` segments', () => {
const imagePath = `${path.join(fixturesPath)}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`;
const image = nativeImage.createFromPath(imagePath);
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 538, height: 190 });
});
it('loads images from paths with `..` segments', () => {
const imagePath = `${path.join(fixturesPath, 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`;
const image = nativeImage.createFromPath(imagePath);
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 538, height: 190 });
});
ifit(process.platform === 'darwin')('Gets an NSImage pointer on macOS', function () {
const imagePath = `${path.join(fixturesPath, 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`;
const image = nativeImage.createFromPath(imagePath);
const nsimage = image.getNativeHandle();
expect(nsimage).to.have.lengthOf(8);
// If all bytes are null, that's Bad
const allBytesAreNotNull = nsimage.reduce((acc, x) => acc || (x !== 0), false);
expect(allBytesAreNotNull);
});
ifit(process.platform === 'win32')('loads images from .ico files on Windows', function () {
const imagePath = path.join(fixturesPath, 'assets', 'icon.ico');
const image = nativeImage.createFromPath(imagePath);
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 256, height: 256 });
});
});
describe('createFromNamedImage(name)', () => {
it('returns empty for invalid options', () => {
const image = nativeImage.createFromNamedImage('totally_not_real');
expect(image.isEmpty()).to.be.true();
});
ifit(process.platform !== 'darwin')('returns empty on non-darwin platforms', function () {
const image = nativeImage.createFromNamedImage('NSActionTemplate');
expect(image.isEmpty()).to.be.true();
});
ifit(process.platform === 'darwin')('returns a valid image on darwin', function () {
const image = nativeImage.createFromNamedImage('NSActionTemplate');
expect(image.isEmpty()).to.be.false();
});
ifit(process.platform === 'darwin')('returns allows an HSL shift for a valid image on darwin', function () {
const image = nativeImage.createFromNamedImage('NSActionTemplate', [0.5, 0.2, 0.8]);
expect(image.isEmpty()).to.be.false();
});
});
describe('resize(options)', () => {
it('returns a resized image', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
for (const [resizeTo, expectedSize] of new Map([
[{}, { width: 538, height: 190 }],
[{ width: 269 }, { width: 269, height: 95 }],
[{ width: 600 }, { width: 600, height: 212 }],
[{ height: 95 }, { width: 269, height: 95 }],
[{ height: 200 }, { width: 566, height: 200 }],
[{ width: 80, height: 65 }, { width: 80, height: 65 }],
[{ width: 600, height: 200 }, { width: 600, height: 200 }],
[{ width: 0, height: 0 }, { width: 0, height: 0 }],
[{ width: -1, height: -1 }, { width: 0, height: 0 }]
])) {
const actualSize = image.resize(resizeTo).getSize();
expect(actualSize).to.deep.equal(expectedSize);
}
});
it('returns an empty image when called on an empty image', () => {
expect(nativeImage.createEmpty().resize({ width: 1, height: 1 }).isEmpty()).to.be.true();
expect(nativeImage.createEmpty().resize({ width: 0, height: 0 }).isEmpty()).to.be.true();
});
it('supports a quality option', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, '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' });
expect(good.toPNG()).to.have.lengthOf.at.most(better.toPNG().length);
expect(better.toPNG()).to.have.lengthOf.below(best.toPNG().length);
});
});
describe('crop(bounds)', () => {
it('returns an empty image when called on an empty image', () => {
expect(nativeImage.createEmpty().crop({ width: 1, height: 2, x: 0, y: 0 }).isEmpty()).to.be.true();
expect(nativeImage.createEmpty().crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true();
});
it('returns an empty image when the bounds are invalid', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
expect(image.crop({ width: 0, height: 0, x: 0, y: 0 }).isEmpty()).to.be.true();
expect(image.crop({ width: -1, height: 10, x: 0, y: 0 }).isEmpty()).to.be.true();
expect(image.crop({ width: 10, height: -35, x: 0, y: 0 }).isEmpty()).to.be.true();
expect(image.crop({ width: 100, height: 100, x: 1000, y: 1000 }).isEmpty()).to.be.true();
});
it('returns a cropped image', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, '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 });
expect(cropA.getSize()).to.deep.equal({ width: 25, height: 64 });
expect(cropB.getSize()).to.deep.equal({ width: 25, height: 64 });
expect(cropA.toPNG().equals(cropB.toPNG())).to.be.false();
});
it('toBitmap() returns a buffer of the right size', () => {
const image = nativeImage.createFromPath(path.join(fixturesPath, 'assets', 'logo.png'));
const crop = image.crop({ width: 25, height: 64, x: 0, y: 0 });
expect(crop.toBitmap().length).to.equal(25 * 64 * 4);
});
});
describe('getAspectRatio()', () => {
it('returns an aspect ratio of an empty image', () => {
expect(nativeImage.createEmpty().getAspectRatio()).to.equal(1.0);
});
it('returns an aspect ratio of an image', () => {
const imageData = imageLogo;
// imageData.width / imageData.height = 2.831578947368421
const expectedAspectRatio = 2.8315789699554443;
const image = nativeImage.createFromPath(imageData.path);
expect(image.getAspectRatio()).to.equal(expectedAspectRatio);
});
});
ifdescribe(process.platform !== 'linux')('createThumbnailFromPath(path, size)', () => {
useRemoteContext({ webPreferences: { contextIsolation: false, nodeIntegration: true } });
it('throws when invalid size is passed', async () => {
const badSize = { width: -1, height: -1 };
await expect(
nativeImage.createThumbnailFromPath('path', badSize)
).to.eventually.be.rejectedWith('size must not be empty');
});
it('throws when a bad path is passed', async () => {
const badPath = process.platform === 'win32' ? '\\hey\\hi\\hello' : '/hey/hi/hello';
const goodSize = { width: 100, height: 100 };
await expect(
nativeImage.createThumbnailFromPath(badPath, goodSize)
).to.eventually.be.rejected();
});
it('returns native image given valid params', async () => {
const goodPath = path.join(fixturesPath, 'assets', 'logo.png');
const goodSize = { width: 100, height: 100 };
const result = await nativeImage.createThumbnailFromPath(goodPath, goodSize);
expect(result.isEmpty()).to.equal(false);
});
it('returns the correct size if larger than the initial image', async () => {
// capybara.png is a 128x128 image.
const imgPath = path.join(fixturesPath, 'assets', 'capybara.png');
const size = { width: 256, height: 256 };
const result = await nativeImage.createThumbnailFromPath(imgPath, size);
expect(result.getSize()).to.deep.equal(size);
});
it('returns the correct size if is the same as the initial image', async () => {
// capybara.png is a 128x128 image.
const imgPath = path.join(fixturesPath, 'assets', 'capybara.png');
const size = { width: 128, height: 128 };
const result = await nativeImage.createThumbnailFromPath(imgPath, size);
expect(result.getSize()).to.deep.equal(size);
});
it('returns the correct size if smaller than the initial image', async () => {
// capybara.png is a 128x128 image.
const imgPath = path.join(fixturesPath, 'assets', 'capybara.png');
const maxSize = { width: 64, height: 64 };
const result = await nativeImage.createThumbnailFromPath(imgPath, maxSize);
expect(result.getSize()).to.deep.equal(maxSize);
});
itremote('works in the renderer', async (path: string) => {
const { nativeImage } = require('electron');
const goodSize = { width: 100, height: 100 };
const result = await nativeImage.createThumbnailFromPath(path, goodSize);
expect(result.isEmpty()).to.equal(false);
}, [path.join(fixturesPath, 'assets', 'logo.png')]);
});
describe('addRepresentation()', () => {
it('does not add representation when the buffer is too small', () => {
const image = nativeImage.createEmpty();
image.addRepresentation({
buffer: Buffer.from([1, 2, 3, 4]),
width: 100,
height: 100
});
expect(image.isEmpty()).to.be.true();
});
it('supports adding a buffer representation for a scale factor', () => {
const image = nativeImage.createEmpty();
const imageDataOne = image1x1;
image.addRepresentation({
scaleFactor: 1.0,
buffer: nativeImage.createFromPath(imageDataOne.path).toPNG()
});
expect(image.getScaleFactors()).to.deep.equal([1]);
const imageDataTwo = image2x2;
image.addRepresentation({
scaleFactor: 2.0,
buffer: nativeImage.createFromPath(imageDataTwo.path).toPNG()
});
expect(image.getScaleFactors()).to.deep.equal([1, 2]);
const imageDataThree = image3x3;
image.addRepresentation({
scaleFactor: 3.0,
buffer: nativeImage.createFromPath(imageDataThree.path).toPNG()
});
expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]);
image.addRepresentation({
scaleFactor: 4.0,
buffer: 'invalid' as any
});
// this one failed, so it shouldn't show up in the scale factors
expect(image.getScaleFactors()).to.deep.equal([1, 2, 3]);
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 1, height: 1 });
expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl);
expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl);
});
it('supports adding a data URL representation for a scale factor', () => {
const image = nativeImage.createEmpty();
const imageDataOne = image1x1;
image.addRepresentation({
scaleFactor: 1.0,
dataURL: imageDataOne.dataUrl
});
const imageDataTwo = image2x2;
image.addRepresentation({
scaleFactor: 2.0,
dataURL: imageDataTwo.dataUrl
});
const imageDataThree = image3x3;
image.addRepresentation({
scaleFactor: 3.0,
dataURL: imageDataThree.dataUrl
});
image.addRepresentation({
scaleFactor: 4.0,
dataURL: 'invalid'
});
expect(image.isEmpty()).to.be.false();
expect(image.getSize()).to.deep.equal({ width: 1, height: 1 });
expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
expect(image.toDataURL({ scaleFactor: 3.0 })).to.equal(imageDataThree.dataUrl);
expect(image.toDataURL({ scaleFactor: 4.0 })).to.equal(imageDataThree.dataUrl);
});
it('supports adding a representation to an existing image', () => {
const imageDataOne = image1x1;
const image = nativeImage.createFromPath(imageDataOne.path);
const imageDataTwo = image2x2;
image.addRepresentation({
scaleFactor: 2.0,
dataURL: imageDataTwo.dataUrl
});
const imageDataThree = image3x3;
image.addRepresentation({
scaleFactor: 2.0,
dataURL: imageDataThree.dataUrl
});
expect(image.toDataURL({ scaleFactor: 1.0 })).to.equal(imageDataOne.dataUrl);
expect(image.toDataURL({ scaleFactor: 2.0 })).to.equal(imageDataTwo.dataUrl);
});
});
});