electron/spec/api-clipboard-spec.js

130 lines
4.5 KiB
JavaScript
Raw Normal View History

2016-03-25 20:03:49 +00:00
const assert = require('assert')
const path = require('path')
const fs = require('fs')
const {Buffer} = require('buffer')
2016-01-12 02:40:23 +00:00
2017-03-16 23:16:31 +00:00
const {clipboard, nativeImage} = require('electron')
2016-01-12 02:40:23 +00:00
2016-03-25 20:03:49 +00:00
describe('clipboard module', function () {
var fixtures = path.resolve(__dirname, 'fixtures')
2016-03-25 20:03:49 +00:00
describe('clipboard.readImage()', function () {
it('returns NativeImage intance', function () {
var p = path.join(fixtures, 'assets', 'logo.png')
var i = nativeImage.createFromPath(p)
clipboard.writeImage(p)
assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
})
})
2016-03-25 20:03:49 +00:00
describe('clipboard.readText()', function () {
it('returns unicode string correctly', function () {
var text = '千江有水千江月,万里无云万里天'
clipboard.writeText(text)
assert.equal(clipboard.readText(), text)
})
})
2016-05-26 21:23:50 +00:00
describe('clipboard.readHTML()', function () {
2016-03-25 20:03:49 +00:00
it('returns markup correctly', function () {
var text = '<string>Hi</string>'
var 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)
assert.equal(clipboard.readHTML(), markup)
2016-03-25 20:03:49 +00:00
})
})
2016-05-26 21:23:50 +00:00
describe('clipboard.readRTF', function () {
2016-03-25 20:03:49 +00:00
it('returns rtf text correctly', function () {
var 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)
assert.equal(clipboard.readRTF(), rtf)
2016-03-25 20:03:49 +00:00
})
})
2016-06-24 22:10:32 +00:00
describe('clipboard.readBookmark', function () {
it('returns title and url', function () {
if (process.platform === 'linux') return
clipboard.writeBookmark('a title', 'https://electron.atom.io')
2016-06-24 22:10:32 +00:00
assert.deepEqual(clipboard.readBookmark(), {
title: 'a title',
url: 'https://electron.atom.io'
})
clipboard.writeText('no bookmark')
assert.deepEqual(clipboard.readBookmark(), {
title: '',
url: ''
2016-06-24 22:10:32 +00:00
})
})
})
2016-03-25 20:03:49 +00:00
describe('clipboard.write()', function () {
it('returns data correctly', function () {
var text = 'test'
var rtf = '{\\rtf1\\utf8 text}'
var p = path.join(fixtures, 'assets', 'logo.png')
var i = nativeImage.createFromPath(p)
var 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>'
2016-06-24 22:14:28 +00:00
var bookmark = {title: 'a title', url: 'test'}
const pdfType = process.platform === 'darwin' ? 'com.adobe.pdf' : 'application/pdf'
const pdf = fs.readFileSync(path.join(fixtures, 'assets', 'cat.pdf'))
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',
image: p,
2017-04-21 07:28:23 +00:00
buffers: {
[pdfType]: pdf
}
2016-03-25 20:03:49 +00:00
})
assert.equal(clipboard.readText(), text)
2016-05-26 21:23:50 +00:00
assert.equal(clipboard.readHTML(), markup)
assert.equal(clipboard.readRTF(), rtf)
2016-03-25 20:03:49 +00:00
assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
assert.deepEqual(clipboard.readBuffer(pdfType), pdf)
if (process.platform !== 'linux') {
assert.deepEqual(clipboard.readBookmark(), bookmark)
}
2016-03-25 20:03:49 +00:00
})
})
2016-10-25 04:59:04 +00:00
describe('clipboard.read/writeFindText(text)', function () {
it('reads and write text to the find pasteboard', function () {
if (process.platform !== 'darwin') return
2016-10-25 04:59:04 +00:00
clipboard.writeFindText('find this')
assert.equal(clipboard.readFindText(), 'find this')
})
})
2017-04-21 05:47:04 +00:00
describe('clipboard.writeBuffer(format, buffer)', () => {
it('writes a Buffer for the specified format', () => {
if (process.platform !== 'darwin') return
const buffer = Buffer.from('writeBuffer', 'utf8')
clipboard.writeBuffer('public.utf8-plain-text', buffer)
assert.equal(clipboard.readText(), 'writeBuffer')
})
it('throws an error when a non-Buffer is specified', () => {
assert.throws(() => {
clipboard.writeBuffer('public.utf8-plain-text', 'hello')
}, /buffer must be a node Buffer/)
})
2017-04-21 05:47:04 +00:00
})
describe('clipboard.readBuffer(format)', function () {
it('returns a Buffer of the content for the specified format', function () {
if (process.platform !== 'darwin') return
const buffer = Buffer.from('this is binary', 'utf8')
clipboard.writeText(buffer.toString())
assert(buffer.equals(clipboard.readBuffer('public.utf8-plain-text')))
})
})
2016-03-25 20:03:49 +00:00
})