forked from electron/electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-clipboard-spec.js
128 lines (111 loc) · 4.26 KB
/
api-clipboard-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const { expect } = require('chai');
const path = require('path');
const { Buffer } = require('buffer');
const { clipboard, nativeImage } = require('electron');
describe('clipboard module', () => {
const fixtures = path.resolve(__dirname, 'fixtures');
// FIXME(zcbenz): Clipboard tests are failing on WOA.
beforeEach(function () {
if (process.platform === 'win32' && process.arch === 'arm64') {
this.skip();
}
});
describe('clipboard.readImage()', () => {
it('returns NativeImage instance', () => {
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());
});
});
describe('clipboard.readText()', () => {
it('returns unicode string correctly', () => {
const text = '千江有水千江月,万里无云万里天';
clipboard.writeText(text);
expect(clipboard.readText()).to.equal(text);
});
});
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>';
clipboard.writeHTML(text);
expect(clipboard.readHTML()).to.equal(markup);
});
});
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}';
clipboard.writeRTF(rtf);
expect(clipboard.readRTF()).to.equal(rtf);
});
});
describe('clipboard.readBookmark', () => {
before(function () {
if (process.platform === 'linux') {
this.skip();
}
});
it('returns title and url', () => {
clipboard.writeBookmark('a title', 'https://electronjs.org');
expect(clipboard.readBookmark()).to.deep.equal({
title: 'a title',
url: 'https://electronjs.org'
});
clipboard.writeText('no bookmark');
expect(clipboard.readBookmark()).to.deep.equal({
title: '',
url: ''
});
});
});
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>';
const bookmark = { title: 'a title', url: 'test' };
clipboard.write({
text: 'test',
html: '<b>Hi</b>',
rtf: '{\\rtf1\\utf8 text}',
bookmark: 'a title',
image: p
});
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') {
expect(clipboard.readBookmark()).to.deep.equal(bookmark);
}
});
});
describe('clipboard.read/writeFindText(text)', () => {
before(function () {
if (process.platform !== 'darwin') {
this.skip();
}
});
it('reads and write text to the find pasteboard', () => {
clipboard.writeFindText('find this');
expect(clipboard.readFindText()).to.equal('find this');
});
});
describe('clipboard.readBuffer(format)', () => {
it('writes a Buffer for the specified format', function () {
const buffer = Buffer.from('writeBuffer', 'utf8');
clipboard.writeBuffer('public.utf8-plain-text', buffer);
expect(buffer.equals(clipboard.readBuffer('public.utf8-plain-text'))).to.equal(true);
});
it('throws an error when a non-Buffer is specified', () => {
expect(() => {
clipboard.writeBuffer('public.utf8-plain-text', 'hello');
}).to.throw(/buffer must be a node Buffer/);
});
});
});