api-clipboard-spec.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const { expect } = require('chai');
  2. const path = require('path');
  3. const { Buffer } = require('buffer');
  4. const { ifdescribe, ifit } = require('./spec-helpers');
  5. const { clipboard, nativeImage } = require('electron');
  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(p);
  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>" : process.platform === 'linux' ? '<meta http-equiv="content-type" ' + 'content="text/html; 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. expect(clipboard.readBookmark()).to.deep.equal({
  44. title: 'a title',
  45. url: 'https://electronjs.org'
  46. });
  47. clipboard.writeText('no bookmark');
  48. expect(clipboard.readBookmark()).to.deep.equal({
  49. title: '',
  50. url: ''
  51. });
  52. });
  53. });
  54. describe('clipboard.read()', () => {
  55. ifit(process.platform !== 'linux')('does not crash when reading various custom clipboard types', () => {
  56. const type = process.platform === 'darwin' ? 'NSFilenamesPboardType' : 'FileNameW';
  57. expect(() => {
  58. const result = clipboard.read(type);
  59. }).to.not.throw();
  60. });
  61. it('can read data written with writeBuffer', () => {
  62. const testText = 'Testing read';
  63. const buffer = Buffer.from(testText, 'utf8');
  64. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  65. expect(clipboard.read('public/utf8-plain-text')).to.equal(testText);
  66. });
  67. });
  68. describe('clipboard.write()', () => {
  69. it('returns data correctly', () => {
  70. const text = 'test';
  71. const rtf = '{\\rtf1\\utf8 text}';
  72. const p = path.join(fixtures, 'assets', 'logo.png');
  73. const i = nativeImage.createFromPath(p);
  74. 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>';
  75. const bookmark = { title: 'a title', url: 'test' };
  76. clipboard.write({
  77. text: 'test',
  78. html: '<b>Hi</b>',
  79. rtf: '{\\rtf1\\utf8 text}',
  80. bookmark: 'a title',
  81. image: p
  82. });
  83. expect(clipboard.readText()).to.equal(text);
  84. expect(clipboard.readHTML()).to.equal(markup);
  85. expect(clipboard.readRTF()).to.equal(rtf);
  86. const readImage = clipboard.readImage();
  87. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  88. if (process.platform !== 'linux') {
  89. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  90. }
  91. });
  92. });
  93. ifdescribe(process.platform === 'darwin')('clipboard.read/writeFindText(text)', () => {
  94. it('reads and write text to the find pasteboard', () => {
  95. clipboard.writeFindText('find this');
  96. expect(clipboard.readFindText()).to.equal('find this');
  97. });
  98. });
  99. describe('clipboard.readBuffer(format)', () => {
  100. it('writes a Buffer for the specified format', function () {
  101. const buffer = Buffer.from('writeBuffer', 'utf8');
  102. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  103. expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
  104. });
  105. it('throws an error when a non-Buffer is specified', () => {
  106. expect(() => {
  107. clipboard.writeBuffer('public/utf8-plain-text', 'hello');
  108. }).to.throw(/buffer must be a node Buffer/);
  109. });
  110. ifit(process.platform !== 'win32')('writes a Buffer using a raw format that is used by native apps', function () {
  111. const message = 'Hello from Electron!';
  112. const buffer = Buffer.from(message);
  113. let rawFormat = 'text/plain';
  114. if (process.platform === 'darwin') {
  115. rawFormat = 'public.utf8-plain-text';
  116. }
  117. clipboard.writeBuffer(rawFormat, buffer);
  118. expect(clipboard.readText()).to.equal(message);
  119. });
  120. });
  121. });