electron/spec/api-clipboard-spec.js

124 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
const { expect } = require('chai');
const path = require('path');
const { Buffer } = require('buffer');
chore: bump chromium to 93.0.4566.0 (main) (#29980) * chore: bump chromium in DEPS to 93.0.4562.0 * chore: update patches * [base] Made Value::Take{Dict,List}() rvalue ref-qualified. https://chromium-review.googlesource.com/c/chromium/src/+/2988105 * Revert "Reland "Roll src/buildtools/third_party/libc++/trunk/ 8fa879467..79a2e924d (426 commits)"" https://chromium-review.googlesource.com/c/chromium/src/+/2995482 This reverts commit 9691d6c265f010791c37d374248559969980ec65 and 797723ec838709ddeba0c104e30727ee0b7ac8ca * Pass gfx::Insets to GetHTComponentForFrame https://chromium-review.googlesource.com/c/chromium/src/+/2984243 * chore: bump chromium in DEPS to 93.0.4563.0 * [Clipboard API] Clipboard Custom Formats implementation Part 2. https://chromium-review.googlesource.com/c/chromium/src/+/2967649 * chore: update patches * chore: bump chromium in DEPS to 93.0.4564.0 * chore: bump chromium in DEPS to 93.0.4565.0 * chore: update patches * Prevent use of base::NoDestructor for trivially-destructible types https://chromium-review.googlesource.com/c/chromium/src/+/2998672 * chore: update patches * fixup! [Clipboard API] Clipboard Custom Formats implementation Part 2. * chore: bump chromium in DEPS to 93.0.4566.0 * chore: update patches * chore: add missing header * ci: do not run clipboard tests on WOA Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2021-07-06 12:17:13 +00:00
const { ifdescribe } = require('./spec-helpers');
2016-01-12 02:40:23 +00:00
2020-03-20 20:28:31 +00:00
const { clipboard, nativeImage } = require('electron');
2016-01-12 02:40:23 +00:00
chore: bump chromium to 93.0.4566.0 (main) (#29980) * chore: bump chromium in DEPS to 93.0.4562.0 * chore: update patches * [base] Made Value::Take{Dict,List}() rvalue ref-qualified. https://chromium-review.googlesource.com/c/chromium/src/+/2988105 * Revert "Reland "Roll src/buildtools/third_party/libc++/trunk/ 8fa879467..79a2e924d (426 commits)"" https://chromium-review.googlesource.com/c/chromium/src/+/2995482 This reverts commit 9691d6c265f010791c37d374248559969980ec65 and 797723ec838709ddeba0c104e30727ee0b7ac8ca * Pass gfx::Insets to GetHTComponentForFrame https://chromium-review.googlesource.com/c/chromium/src/+/2984243 * chore: bump chromium in DEPS to 93.0.4563.0 * [Clipboard API] Clipboard Custom Formats implementation Part 2. https://chromium-review.googlesource.com/c/chromium/src/+/2967649 * chore: update patches * chore: bump chromium in DEPS to 93.0.4564.0 * chore: bump chromium in DEPS to 93.0.4565.0 * chore: update patches * Prevent use of base::NoDestructor for trivially-destructible types https://chromium-review.googlesource.com/c/chromium/src/+/2998672 * chore: update patches * fixup! [Clipboard API] Clipboard Custom Formats implementation Part 2. * chore: bump chromium in DEPS to 93.0.4566.0 * chore: update patches * chore: add missing header * ci: do not run clipboard tests on WOA Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2021-07-06 12:17:13 +00:00
// FIXME(zcbenz): Clipboard tests are failing on WOA.
ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard module', () => {
2020-03-20 20:28:31 +00:00
const fixtures = path.resolve(__dirname, 'fixtures');
2017-10-27 00:12:51 +00:00
describe('clipboard.readImage()', () => {
2017-10-27 02:09:38 +00:00
it('returns NativeImage instance', () => {
2020-03-20 20:28:31 +00:00
const p = path.join(fixtures, 'assets', 'logo.png');
const i = nativeImage.createFromPath(p);
clipboard.writeImage(p);
const readImage = clipboard.readImage();
expect(readImage.toDataURL()).to.equal(i.toDataURL());
2020-03-20 20:28:31 +00:00
});
});
2017-10-27 00:12:51 +00:00
describe('clipboard.readText()', () => {
it('returns unicode string correctly', () => {
2020-03-20 20:28:31 +00:00
const text = '千江有水千江月,万里无云万里天';
clipboard.writeText(text);
expect(clipboard.readText()).to.equal(text);
});
});
2017-10-27 00:12:51 +00:00
describe('clipboard.readHTML()', () => {
it('returns markup correctly', () => {
2020-03-20 20:28:31 +00:00
const text = '<string>Hi</string>';
const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><string>Hi</string>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><string>Hi</string>' : '<string>Hi</string>';
clipboard.writeHTML(text);
expect(clipboard.readHTML()).to.equal(markup);
});
});
2017-10-27 00:12:51 +00:00
describe('clipboard.readRTF', () => {
it('returns rtf text correctly', () => {
2020-03-20 20:28:31 +00:00
const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
clipboard.writeRTF(rtf);
expect(clipboard.readRTF()).to.equal(rtf);
});
});
2017-10-27 00:12:51 +00:00
describe('clipboard.readBookmark', () => {
before(function () {
if (process.platform === 'linux') {
2020-03-20 20:28:31 +00:00
this.skip();
}
2020-03-20 20:28:31 +00:00
});
it('returns title and url', () => {
2020-03-20 20:28:31 +00:00
clipboard.writeBookmark('a title', 'https://electronjs.org');
expect(clipboard.readBookmark()).to.deep.equal({
2016-06-24 22:10:32 +00:00
title: 'a title',
url: 'https://electronjs.org'
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
clipboard.writeText('no bookmark');
expect(clipboard.readBookmark()).to.deep.equal({
title: '',
url: ''
2020-03-20 20:28:31 +00:00
});
});
});
2016-06-24 22:10:32 +00:00
2017-10-27 00:12:51 +00:00
describe('clipboard.write()', () => {
it('returns data correctly', () => {
2020-03-20 20:28:31 +00:00
const text = 'test';
const rtf = '{\\rtf1\\utf8 text}';
const p = path.join(fixtures, 'assets', 'logo.png');
const i = nativeImage.createFromPath(p);
const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; charset=utf-8"><b>Hi</b>' : '<b>Hi</b>';
const bookmark = { title: 'a title', url: 'test' };
2016-01-12 02:40:23 +00:00
clipboard.write({
2016-03-25 20:03:49 +00:00
text: 'test',
2016-01-12 02:40:23 +00:00
html: '<b>Hi</b>',
rtf: '{\\rtf1\\utf8 text}',
2016-06-24 22:14:28 +00:00
bookmark: 'a title',
2017-05-22 20:55:19 +00:00
image: p
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
expect(clipboard.readText()).to.equal(text);
expect(clipboard.readHTML()).to.equal(markup);
expect(clipboard.readRTF()).to.equal(rtf);
const readImage = clipboard.readImage();
expect(readImage.toDataURL()).to.equal(i.toDataURL());
if (process.platform !== 'linux') {
2020-03-20 20:28:31 +00:00
expect(clipboard.readBookmark()).to.deep.equal(bookmark);
}
2020-03-20 20:28:31 +00:00
});
});
2016-10-25 04:59:04 +00:00
2017-10-27 00:12:51 +00:00
describe('clipboard.read/writeFindText(text)', () => {
before(function () {
if (process.platform !== 'darwin') {
2020-03-20 20:28:31 +00:00
this.skip();
}
2020-03-20 20:28:31 +00:00
});
2016-10-25 04:59:04 +00:00
it('reads and write text to the find pasteboard', () => {
2020-03-20 20:28:31 +00:00
clipboard.writeFindText('find this');
expect(clipboard.readFindText()).to.equal('find this');
});
});
describe('clipboard.readBuffer(format)', () => {
it('writes a Buffer for the specified format', function () {
2020-03-20 20:28:31 +00:00
const buffer = Buffer.from('writeBuffer', 'utf8');
chore: bump chromium to 93.0.4566.0 (main) (#29980) * chore: bump chromium in DEPS to 93.0.4562.0 * chore: update patches * [base] Made Value::Take{Dict,List}() rvalue ref-qualified. https://chromium-review.googlesource.com/c/chromium/src/+/2988105 * Revert "Reland "Roll src/buildtools/third_party/libc++/trunk/ 8fa879467..79a2e924d (426 commits)"" https://chromium-review.googlesource.com/c/chromium/src/+/2995482 This reverts commit 9691d6c265f010791c37d374248559969980ec65 and 797723ec838709ddeba0c104e30727ee0b7ac8ca * Pass gfx::Insets to GetHTComponentForFrame https://chromium-review.googlesource.com/c/chromium/src/+/2984243 * chore: bump chromium in DEPS to 93.0.4563.0 * [Clipboard API] Clipboard Custom Formats implementation Part 2. https://chromium-review.googlesource.com/c/chromium/src/+/2967649 * chore: update patches * chore: bump chromium in DEPS to 93.0.4564.0 * chore: bump chromium in DEPS to 93.0.4565.0 * chore: update patches * Prevent use of base::NoDestructor for trivially-destructible types https://chromium-review.googlesource.com/c/chromium/src/+/2998672 * chore: update patches * fixup! [Clipboard API] Clipboard Custom Formats implementation Part 2. * chore: bump chromium in DEPS to 93.0.4566.0 * chore: update patches * chore: add missing header * ci: do not run clipboard tests on WOA Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2021-07-06 12:17:13 +00:00
clipboard.writeBuffer('public/utf8-plain-text', buffer);
expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
2020-03-20 20:28:31 +00:00
});
it('throws an error when a non-Buffer is specified', () => {
expect(() => {
chore: bump chromium to 93.0.4566.0 (main) (#29980) * chore: bump chromium in DEPS to 93.0.4562.0 * chore: update patches * [base] Made Value::Take{Dict,List}() rvalue ref-qualified. https://chromium-review.googlesource.com/c/chromium/src/+/2988105 * Revert "Reland "Roll src/buildtools/third_party/libc++/trunk/ 8fa879467..79a2e924d (426 commits)"" https://chromium-review.googlesource.com/c/chromium/src/+/2995482 This reverts commit 9691d6c265f010791c37d374248559969980ec65 and 797723ec838709ddeba0c104e30727ee0b7ac8ca * Pass gfx::Insets to GetHTComponentForFrame https://chromium-review.googlesource.com/c/chromium/src/+/2984243 * chore: bump chromium in DEPS to 93.0.4563.0 * [Clipboard API] Clipboard Custom Formats implementation Part 2. https://chromium-review.googlesource.com/c/chromium/src/+/2967649 * chore: update patches * chore: bump chromium in DEPS to 93.0.4564.0 * chore: bump chromium in DEPS to 93.0.4565.0 * chore: update patches * Prevent use of base::NoDestructor for trivially-destructible types https://chromium-review.googlesource.com/c/chromium/src/+/2998672 * chore: update patches * fixup! [Clipboard API] Clipboard Custom Formats implementation Part 2. * chore: bump chromium in DEPS to 93.0.4566.0 * chore: update patches * chore: add missing header * ci: do not run clipboard tests on WOA Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com> Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2021-07-06 12:17:13 +00:00
clipboard.writeBuffer('public/utf8-plain-text', 'hello');
2020-03-20 20:28:31 +00:00
}).to.throw(/buffer must be a node Buffer/);
});
});
});