api-clipboard-spec.ts 5.2 KB

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