api-clipboard-spec.ts 5.5 KB

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