2022-07-26 10:37:37 +03:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 17:42:27 +03:00
|
|
|
import * as path from 'node:path';
|
|
|
|
import { Buffer } from 'node:buffer';
|
2023-01-26 00:01:25 +03:00
|
|
|
import { ifdescribe, ifit } from './lib/spec-helpers';
|
2022-07-26 10:37:37 +03:00
|
|
|
import { clipboard, nativeImage } from 'electron/common';
|
2016-01-12 05:40:23 +03:00
|
|
|
|
2021-07-06 15:17:13 +03:00
|
|
|
// FIXME(zcbenz): Clipboard tests are failing on WOA.
|
|
|
|
ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard module', () => {
|
2017-10-27 03:12:51 +03:00
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures');
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2017-10-27 03:12:51 +03:00
|
|
|
describe('clipboard.readImage()', () => {
|
2017-10-27 05:09:38 +03:00
|
|
|
it('returns NativeImage instance', () => {
|
2017-10-27 03:12:51 +03:00
|
|
|
const p = path.join(fixtures, 'assets', 'logo.png');
|
|
|
|
const i = nativeImage.createFromPath(p);
|
2022-07-26 10:37:37 +03:00
|
|
|
clipboard.writeImage(i);
|
2020-04-14 02:39:26 +03:00
|
|
|
const readImage = clipboard.readImage();
|
|
|
|
expect(readImage.toDataURL()).to.equal(i.toDataURL());
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2023-08-14 11:35:37 +03:00
|
|
|
|
|
|
|
it('works for empty image', () => {
|
|
|
|
clipboard.writeText('Not an Image');
|
|
|
|
expect(clipboard.readImage().isEmpty()).to.be.true();
|
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2017-10-27 03:12:51 +03:00
|
|
|
describe('clipboard.readText()', () => {
|
|
|
|
it('returns unicode string correctly', () => {
|
|
|
|
const text = '千江有水千江月,万里无云万里天';
|
2016-03-25 23:03:49 +03:00
|
|
|
clipboard.writeText(text);
|
2018-06-18 00:47:51 +03:00
|
|
|
expect(clipboard.readText()).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2017-10-27 03:12:51 +03:00
|
|
|
describe('clipboard.readHTML()', () => {
|
|
|
|
it('returns markup correctly', () => {
|
2024-05-10 18:21:10 +03:00
|
|
|
let text = '<string>Hi</string>';
|
|
|
|
// CL: https://chromium-review.googlesource.com/c/chromium/src/+/5187335
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
text = '<meta charset=\'utf-8\'><string>Hi</string>';
|
|
|
|
}
|
|
|
|
clipboard.writeHTML('<string>Hi</string>');
|
2024-02-14 20:33:32 +03:00
|
|
|
expect(clipboard.readHTML()).to.equal(text);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2017-10-27 03:12:51 +03:00
|
|
|
describe('clipboard.readRTF', () => {
|
|
|
|
it('returns rtf text correctly', () => {
|
|
|
|
const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
|
2016-05-27 00:23:50 +03:00
|
|
|
clipboard.writeRTF(rtf);
|
2018-06-18 00:47:51 +03:00
|
|
|
expect(clipboard.readRTF()).to.equal(rtf);
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
2016-02-17 04:09:41 +03:00
|
|
|
|
2021-10-26 15:14:08 +03:00
|
|
|
ifdescribe(process.platform !== 'linux')('clipboard.readBookmark', () => {
|
2017-11-16 00:05:46 +03:00
|
|
|
it('returns title and url', () => {
|
2017-11-19 15:01:33 +03:00
|
|
|
clipboard.writeBookmark('a title', 'https://electronjs.org');
|
2021-09-01 22:55:07 +03:00
|
|
|
|
|
|
|
const readBookmark = clipboard.readBookmark();
|
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
expect(readBookmark.title).to.equal('a title');
|
|
|
|
}
|
|
|
|
expect(clipboard.readBookmark().url).to.equal('https://electronjs.org');
|
2016-06-25 01:32:52 +03:00
|
|
|
|
|
|
|
clipboard.writeText('no bookmark');
|
2018-06-18 00:47:51 +03:00
|
|
|
expect(clipboard.readBookmark()).to.deep.equal({
|
2016-06-25 01:32:52 +03:00
|
|
|
title: '',
|
|
|
|
url: ''
|
2016-06-25 01:10:32 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-01 17:00:36 +03:00
|
|
|
describe('clipboard.read()', () => {
|
|
|
|
ifit(process.platform !== 'linux')('does not crash when reading various custom clipboard types', () => {
|
2021-10-26 15:14:08 +03:00
|
|
|
const type = process.platform === 'darwin' ? 'NSFilenamesPboardType' : 'FileNameW';
|
|
|
|
|
|
|
|
expect(() => {
|
2022-07-26 10:37:37 +03:00
|
|
|
clipboard.read(type);
|
2021-10-26 15:14:08 +03:00
|
|
|
}).to.not.throw();
|
|
|
|
});
|
2021-11-01 17:00:36 +03:00
|
|
|
it('can read data written with writeBuffer', () => {
|
|
|
|
const testText = 'Testing read';
|
|
|
|
const buffer = Buffer.from(testText, 'utf8');
|
|
|
|
clipboard.writeBuffer('public/utf8-plain-text', buffer);
|
|
|
|
expect(clipboard.read('public/utf8-plain-text')).to.equal(testText);
|
|
|
|
});
|
2021-10-26 15:14:08 +03:00
|
|
|
});
|
|
|
|
|
2017-10-27 03:12:51 +03:00
|
|
|
describe('clipboard.write()', () => {
|
|
|
|
it('returns data correctly', () => {
|
|
|
|
const text = 'test';
|
|
|
|
const rtf = '{\\rtf1\\utf8 text}';
|
|
|
|
const p = path.join(fixtures, 'assets', 'logo.png');
|
|
|
|
const i = nativeImage.createFromPath(p);
|
2024-05-10 18:21:10 +03:00
|
|
|
let markup = '<b>Hi</b>';
|
|
|
|
// CL: https://chromium-review.googlesource.com/c/chromium/src/+/5187335
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
markup = '<meta charset=\'utf-8\'><b>Hi</b>';
|
|
|
|
}
|
2018-09-13 19:10:51 +03:00
|
|
|
const bookmark = { title: 'a title', url: 'test' };
|
2016-01-12 05:40:23 +03:00
|
|
|
clipboard.write({
|
2016-03-25 23:03:49 +03:00
|
|
|
text: 'test',
|
2016-01-12 05:40:23 +03:00
|
|
|
html: '<b>Hi</b>',
|
2016-02-05 11:06:21 +03:00
|
|
|
rtf: '{\\rtf1\\utf8 text}',
|
2016-06-25 01:14:28 +03:00
|
|
|
bookmark: 'a title',
|
2022-07-26 10:37:37 +03:00
|
|
|
image: i
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
2018-06-18 00:47:51 +03:00
|
|
|
|
|
|
|
expect(clipboard.readText()).to.equal(text);
|
|
|
|
expect(clipboard.readHTML()).to.equal(markup);
|
|
|
|
expect(clipboard.readRTF()).to.equal(rtf);
|
2020-04-14 02:39:26 +03:00
|
|
|
const readImage = clipboard.readImage();
|
|
|
|
expect(readImage.toDataURL()).to.equal(i.toDataURL());
|
2016-06-25 03:16:38 +03:00
|
|
|
|
|
|
|
if (process.platform !== 'linux') {
|
2021-09-01 22:55:07 +03:00
|
|
|
if (process.platform !== 'win32') {
|
|
|
|
expect(clipboard.readBookmark()).to.deep.equal(bookmark);
|
|
|
|
} else {
|
|
|
|
expect(clipboard.readBookmark().url).to.equal(bookmark.url);
|
|
|
|
}
|
2016-06-25 03:16:38 +03:00
|
|
|
}
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|
|
|
|
});
|
2016-10-25 07:59:04 +03:00
|
|
|
|
2021-10-26 15:14:08 +03:00
|
|
|
ifdescribe(process.platform === 'darwin')('clipboard.read/writeFindText(text)', () => {
|
2017-11-16 00:05:46 +03:00
|
|
|
it('reads and write text to the find pasteboard', () => {
|
2016-10-25 07:59:04 +03:00
|
|
|
clipboard.writeFindText('find this');
|
2018-06-18 00:47:51 +03:00
|
|
|
expect(clipboard.readFindText()).to.equal('find this');
|
2016-10-25 07:59:04 +03:00
|
|
|
});
|
|
|
|
});
|
2017-03-17 00:54:48 +03:00
|
|
|
|
2020-07-09 01:02:42 +03:00
|
|
|
describe('clipboard.readBuffer(format)', () => {
|
2017-11-16 00:05:46 +03:00
|
|
|
it('writes a Buffer for the specified format', function () {
|
2017-04-21 08:47:04 +03:00
|
|
|
const buffer = Buffer.from('writeBuffer', 'utf8');
|
2021-07-06 15:17:13 +03:00
|
|
|
clipboard.writeBuffer('public/utf8-plain-text', buffer);
|
|
|
|
expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
|
2017-04-21 08:47:04 +03:00
|
|
|
});
|
2017-05-22 23:53:58 +03:00
|
|
|
|
|
|
|
it('throws an error when a non-Buffer is specified', () => {
|
2018-06-18 00:47:51 +03:00
|
|
|
expect(() => {
|
2022-07-26 10:37:37 +03:00
|
|
|
clipboard.writeBuffer('public/utf8-plain-text', 'hello' as any);
|
2018-06-18 00:47:51 +03:00
|
|
|
}).to.throw(/buffer must be a node Buffer/);
|
2017-05-22 23:53:58 +03:00
|
|
|
});
|
2021-11-04 21:19:30 +03:00
|
|
|
|
|
|
|
ifit(process.platform !== 'win32')('writes a Buffer using a raw format that is used by native apps', function () {
|
|
|
|
const message = 'Hello from Electron!';
|
|
|
|
const buffer = Buffer.from(message);
|
|
|
|
let rawFormat = 'text/plain';
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
rawFormat = 'public.utf8-plain-text';
|
|
|
|
}
|
|
|
|
clipboard.writeBuffer(rawFormat, buffer);
|
|
|
|
expect(clipboard.readText()).to.equal(message);
|
|
|
|
});
|
2017-04-21 08:47:04 +03:00
|
|
|
});
|
2016-03-25 23:03:49 +03:00
|
|
|
});
|