2018-09-13 16:10:51 +00:00
|
|
|
const { expect } = require('chai')
|
2016-03-25 20:03:49 +00:00
|
|
|
const path = require('path')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { Buffer } = require('buffer')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { clipboard, nativeImage } = require('electron')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard module', () => {
|
|
|
|
const fixtures = path.resolve(__dirname, 'fixtures')
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2020-02-25 17:15:50 +00:00
|
|
|
// FIXME(zcbenz): Clipboard tests are failing on WOA.
|
|
|
|
beforeEach(function () {
|
|
|
|
if (process.platform === 'win32' && process.arch === 'arm64') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.readImage()', () => {
|
2017-10-27 02:09:38 +00:00
|
|
|
it('returns NativeImage instance', () => {
|
2017-10-27 00:12:51 +00:00
|
|
|
const p = path.join(fixtures, 'assets', 'logo.png')
|
|
|
|
const i = nativeImage.createFromPath(p)
|
2016-03-25 20:03:49 +00:00
|
|
|
clipboard.writeImage(p)
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readImage().toDataURL()).to.equal(i.toDataURL())
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.readText()', () => {
|
|
|
|
it('returns unicode string correctly', () => {
|
|
|
|
const text = '千江有水千江月,万里无云万里天'
|
2016-03-25 20:03:49 +00:00
|
|
|
clipboard.writeText(text)
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readText()).to.equal(text)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.readHTML()', () => {
|
|
|
|
it('returns markup correctly', () => {
|
|
|
|
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>'
|
2016-05-26 21:23:50 +00:00
|
|
|
clipboard.writeHTML(text)
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readHTML()).to.equal(markup)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:12:51 +00: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-26 21:23:50 +00:00
|
|
|
clipboard.writeRTF(rtf)
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readRTF()).to.equal(rtf)
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-02-17 01:09:41 +00:00
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.readBookmark', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform === 'linux') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-06-25 00:16:38 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('returns title and url', () => {
|
2017-11-19 12:01:33 +00:00
|
|
|
clipboard.writeBookmark('a title', 'https://electronjs.org')
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readBookmark()).to.deep.equal({
|
2016-06-24 22:10:32 +00:00
|
|
|
title: 'a title',
|
2017-11-19 12:01:33 +00:00
|
|
|
url: 'https://electronjs.org'
|
2016-06-24 22:32:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
clipboard.writeText('no bookmark')
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readBookmark()).to.deep.equal({
|
2016-06-24 22:32:52 +00:00
|
|
|
title: '',
|
|
|
|
url: ''
|
2016-06-24 22:10:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-10-27 00:12:51 +00: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)
|
|
|
|
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>'
|
2018-09-13 16:10:51 +00:00
|
|
|
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>',
|
2016-02-05 08:06:21 +00:00
|
|
|
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
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
2018-06-17 21:47:51 +00:00
|
|
|
|
|
|
|
expect(clipboard.readText()).to.equal(text)
|
|
|
|
expect(clipboard.readHTML()).to.equal(markup)
|
|
|
|
expect(clipboard.readRTF()).to.equal(rtf)
|
|
|
|
expect(clipboard.readImage().toDataURL()).to.equal(i.toDataURL())
|
2016-06-25 00:16:38 +00:00
|
|
|
|
|
|
|
if (process.platform !== 'linux') {
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readBookmark()).to.deep.equal(bookmark)
|
2016-06-25 00:16:38 +00:00
|
|
|
}
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-25 04:59:04 +00:00
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.read/writeFindText(text)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2016-10-25 04:59:04 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('reads and write text to the find pasteboard', () => {
|
2016-10-25 04:59:04 +00:00
|
|
|
clipboard.writeFindText('find this')
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readFindText()).to.equal('find this')
|
2016-10-25 04:59:04 +00:00
|
|
|
})
|
|
|
|
})
|
2017-03-16 21:54:48 +00:00
|
|
|
|
2017-04-21 05:47:04 +00:00
|
|
|
describe('clipboard.writeBuffer(format, buffer)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
it('writes a Buffer for the specified format', function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
// FIXME(alexeykuzmin): Skip the test.
|
|
|
|
// this.skip()
|
|
|
|
return
|
|
|
|
}
|
2017-04-21 05:47:04 +00:00
|
|
|
|
|
|
|
const buffer = Buffer.from('writeBuffer', 'utf8')
|
|
|
|
clipboard.writeBuffer('public.utf8-plain-text', buffer)
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(clipboard.readText()).to.equal('writeBuffer')
|
2017-04-21 05:47:04 +00:00
|
|
|
})
|
2017-05-22 20:53:58 +00:00
|
|
|
|
|
|
|
it('throws an error when a non-Buffer is specified', () => {
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(() => {
|
2017-05-22 20:53:58 +00:00
|
|
|
clipboard.writeBuffer('public.utf8-plain-text', 'hello')
|
2018-06-17 21:47:51 +00:00
|
|
|
}).to.throw(/buffer must be a node Buffer/)
|
2017-05-22 20:53:58 +00:00
|
|
|
})
|
2017-04-21 05:47:04 +00:00
|
|
|
})
|
|
|
|
|
2017-10-27 00:12:51 +00:00
|
|
|
describe('clipboard.readBuffer(format)', () => {
|
2017-11-15 21:05:46 +00:00
|
|
|
before(function () {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
this.skip()
|
|
|
|
}
|
|
|
|
})
|
2017-03-16 21:54:48 +00:00
|
|
|
|
2017-11-15 21:05:46 +00:00
|
|
|
it('returns a Buffer of the content for the specified format', () => {
|
2017-03-16 21:54:48 +00:00
|
|
|
const buffer = Buffer.from('this is binary', 'utf8')
|
|
|
|
clipboard.writeText(buffer.toString())
|
2018-06-17 21:47:51 +00:00
|
|
|
expect(buffer.equals(clipboard.readBuffer('public.utf8-plain-text'))).to.equal(true)
|
2017-03-16 21:54:48 +00:00
|
|
|
})
|
|
|
|
})
|
2016-03-25 20:03:49 +00:00
|
|
|
})
|