api-clipboard-spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. clipboard.writeHTML(text);
  33. expect(clipboard.readHTML()).to.equal(text);
  34. });
  35. });
  36. describe('clipboard.readRTF', () => {
  37. it('returns rtf text correctly', () => {
  38. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
  39. clipboard.writeRTF(rtf);
  40. expect(clipboard.readRTF()).to.equal(rtf);
  41. });
  42. });
  43. ifdescribe(process.platform !== 'linux')('clipboard.readBookmark', () => {
  44. it('returns title and url', () => {
  45. clipboard.writeBookmark('a title', 'https://electronjs.org');
  46. const readBookmark = clipboard.readBookmark();
  47. if (process.platform !== 'win32') {
  48. expect(readBookmark.title).to.equal('a title');
  49. }
  50. expect(clipboard.readBookmark().url).to.equal('https://electronjs.org');
  51. clipboard.writeText('no bookmark');
  52. expect(clipboard.readBookmark()).to.deep.equal({
  53. title: '',
  54. url: ''
  55. });
  56. });
  57. });
  58. describe('clipboard.read()', () => {
  59. ifit(process.platform !== 'linux')('does not crash when reading various custom clipboard types', () => {
  60. const type = process.platform === 'darwin' ? 'NSFilenamesPboardType' : 'FileNameW';
  61. expect(() => {
  62. clipboard.read(type);
  63. }).to.not.throw();
  64. });
  65. it('can read data written with writeBuffer', () => {
  66. const testText = 'Testing read';
  67. const buffer = Buffer.from(testText, 'utf8');
  68. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  69. expect(clipboard.read('public/utf8-plain-text')).to.equal(testText);
  70. });
  71. });
  72. describe('clipboard.write()', () => {
  73. it('returns data correctly', () => {
  74. const text = 'test';
  75. const rtf = '{\\rtf1\\utf8 text}';
  76. const p = path.join(fixtures, 'assets', 'logo.png');
  77. const i = nativeImage.createFromPath(p);
  78. const markup = '<b>Hi</b>';
  79. const bookmark = { title: 'a title', url: 'test' };
  80. clipboard.write({
  81. text: 'test',
  82. html: '<b>Hi</b>',
  83. rtf: '{\\rtf1\\utf8 text}',
  84. bookmark: 'a title',
  85. image: i
  86. });
  87. expect(clipboard.readText()).to.equal(text);
  88. expect(clipboard.readHTML()).to.equal(markup);
  89. expect(clipboard.readRTF()).to.equal(rtf);
  90. const readImage = clipboard.readImage();
  91. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  92. if (process.platform !== 'linux') {
  93. if (process.platform !== 'win32') {
  94. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  95. } else {
  96. expect(clipboard.readBookmark().url).to.equal(bookmark.url);
  97. }
  98. }
  99. });
  100. });
  101. ifdescribe(process.platform === 'darwin')('clipboard.read/writeFindText(text)', () => {
  102. it('reads and write text to the find pasteboard', () => {
  103. clipboard.writeFindText('find this');
  104. expect(clipboard.readFindText()).to.equal('find this');
  105. });
  106. });
  107. describe('clipboard.readBuffer(format)', () => {
  108. it('writes a Buffer for the specified format', function () {
  109. const buffer = Buffer.from('writeBuffer', 'utf8');
  110. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  111. expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
  112. });
  113. it('throws an error when a non-Buffer is specified', () => {
  114. expect(() => {
  115. clipboard.writeBuffer('public/utf8-plain-text', 'hello' as any);
  116. }).to.throw(/buffer must be a node Buffer/);
  117. });
  118. ifit(process.platform !== 'win32')('writes a Buffer using a raw format that is used by native apps', function () {
  119. const message = 'Hello from Electron!';
  120. const buffer = Buffer.from(message);
  121. let rawFormat = 'text/plain';
  122. if (process.platform === 'darwin') {
  123. rawFormat = 'public.utf8-plain-text';
  124. }
  125. clipboard.writeBuffer(rawFormat, buffer);
  126. expect(clipboard.readText()).to.equal(message);
  127. });
  128. });
  129. });