api-clipboard-spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import { expect } from 'chai';
  2. import * as path from 'node:path';
  3. import { Buffer } from 'node:buffer';
  4. import { ifdescribe, ifit } from './lib/spec-helpers';
  5. import { clipboard, nativeImage } from 'electron/common';
  6. // FIXME(zcbenz): Clipboard tests are failing on WOA.
  7. ifdescribe(process.platform !== 'win32' || process.arch !== 'arm64')('clipboard module', () => {
  8. const fixtures = path.resolve(__dirname, 'fixtures');
  9. describe('clipboard.readImage()', () => {
  10. it('returns NativeImage instance', () => {
  11. const p = path.join(fixtures, 'assets', 'logo.png');
  12. const i = nativeImage.createFromPath(p);
  13. clipboard.writeImage(i);
  14. const readImage = clipboard.readImage();
  15. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  16. });
  17. it('works for empty image', () => {
  18. clipboard.writeText('Not an Image');
  19. expect(clipboard.readImage().isEmpty()).to.be.true();
  20. });
  21. });
  22. describe('clipboard.readText()', () => {
  23. it('returns unicode string correctly', () => {
  24. const text = '千江有水千江月,万里无云万里天';
  25. clipboard.writeText(text);
  26. expect(clipboard.readText()).to.equal(text);
  27. });
  28. });
  29. describe('clipboard.readHTML()', () => {
  30. it('returns markup correctly', () => {
  31. const text = '<string>Hi</string>';
  32. const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><string>Hi</string>" : '<string>Hi</string>';
  33. clipboard.writeHTML(text);
  34. expect(clipboard.readHTML()).to.equal(markup);
  35. });
  36. });
  37. describe('clipboard.readRTF', () => {
  38. it('returns rtf text correctly', () => {
  39. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
  40. clipboard.writeRTF(rtf);
  41. expect(clipboard.readRTF()).to.equal(rtf);
  42. });
  43. });
  44. ifdescribe(process.platform !== 'linux')('clipboard.readBookmark', () => {
  45. it('returns title and url', () => {
  46. clipboard.writeBookmark('a title', 'https://electronjs.org');
  47. const readBookmark = clipboard.readBookmark();
  48. if (process.platform !== 'win32') {
  49. expect(readBookmark.title).to.equal('a title');
  50. }
  51. expect(clipboard.readBookmark().url).to.equal('https://electronjs.org');
  52. clipboard.writeText('no bookmark');
  53. expect(clipboard.readBookmark()).to.deep.equal({
  54. title: '',
  55. url: ''
  56. });
  57. });
  58. });
  59. describe('clipboard.read()', () => {
  60. ifit(process.platform !== 'linux')('does not crash when reading various custom clipboard types', () => {
  61. const type = process.platform === 'darwin' ? 'NSFilenamesPboardType' : 'FileNameW';
  62. expect(() => {
  63. clipboard.read(type);
  64. }).to.not.throw();
  65. });
  66. it('can read data written with writeBuffer', () => {
  67. const testText = 'Testing read';
  68. const buffer = Buffer.from(testText, 'utf8');
  69. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  70. expect(clipboard.read('public/utf8-plain-text')).to.equal(testText);
  71. });
  72. });
  73. describe('clipboard.write()', () => {
  74. it('returns data correctly', () => {
  75. const text = 'test';
  76. const rtf = '{\\rtf1\\utf8 text}';
  77. const p = path.join(fixtures, 'assets', 'logo.png');
  78. const i = nativeImage.createFromPath(p);
  79. const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : '<b>Hi</b>';
  80. const bookmark = { title: 'a title', url: 'test' };
  81. clipboard.write({
  82. text: 'test',
  83. html: '<b>Hi</b>',
  84. rtf: '{\\rtf1\\utf8 text}',
  85. bookmark: 'a title',
  86. image: i
  87. });
  88. expect(clipboard.readText()).to.equal(text);
  89. expect(clipboard.readHTML()).to.equal(markup);
  90. expect(clipboard.readRTF()).to.equal(rtf);
  91. const readImage = clipboard.readImage();
  92. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  93. if (process.platform !== 'linux') {
  94. if (process.platform !== 'win32') {
  95. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  96. } else {
  97. expect(clipboard.readBookmark().url).to.equal(bookmark.url);
  98. }
  99. }
  100. });
  101. });
  102. ifdescribe(process.platform === 'darwin')('clipboard.read/writeFindText(text)', () => {
  103. it('reads and write text to the find pasteboard', () => {
  104. clipboard.writeFindText('find this');
  105. expect(clipboard.readFindText()).to.equal('find this');
  106. });
  107. });
  108. describe('clipboard.readBuffer(format)', () => {
  109. it('writes a Buffer for the specified format', function () {
  110. const buffer = Buffer.from('writeBuffer', 'utf8');
  111. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  112. expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
  113. });
  114. it('throws an error when a non-Buffer is specified', () => {
  115. expect(() => {
  116. clipboard.writeBuffer('public/utf8-plain-text', 'hello' as any);
  117. }).to.throw(/buffer must be a node Buffer/);
  118. });
  119. ifit(process.platform !== 'win32')('writes a Buffer using a raw format that is used by native apps', function () {
  120. const message = 'Hello from Electron!';
  121. const buffer = Buffer.from(message);
  122. let rawFormat = 'text/plain';
  123. if (process.platform === 'darwin') {
  124. rawFormat = 'public.utf8-plain-text';
  125. }
  126. clipboard.writeBuffer(rawFormat, buffer);
  127. expect(clipboard.readText()).to.equal(message);
  128. });
  129. });
  130. });