api-clipboard-spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const { expect } = require('chai');
  2. const path = require('path');
  3. const { Buffer } = require('buffer');
  4. const { ifdescribe } = 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>" : '<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. describe('clipboard.readBookmark', () => {
  41. before(function () {
  42. if (process.platform === 'linux') {
  43. this.skip();
  44. }
  45. });
  46. it('returns title and url', () => {
  47. clipboard.writeBookmark('a title', 'https://electronjs.org');
  48. const readBookmark = clipboard.readBookmark();
  49. if (process.platform !== 'win32') {
  50. expect(readBookmark.title).to.equal('a title');
  51. }
  52. expect(clipboard.readBookmark().url).to.equal('https://electronjs.org');
  53. clipboard.writeText('no bookmark');
  54. expect(clipboard.readBookmark()).to.deep.equal({
  55. title: '',
  56. url: ''
  57. });
  58. });
  59. });
  60. describe('clipboard.write()', () => {
  61. it('returns data correctly', () => {
  62. const text = 'test';
  63. const rtf = '{\\rtf1\\utf8 text}';
  64. const p = path.join(fixtures, 'assets', 'logo.png');
  65. const i = nativeImage.createFromPath(p);
  66. const markup = process.platform === 'darwin' ? "<meta charset='utf-8'><b>Hi</b>" : '<b>Hi</b>';
  67. const bookmark = { title: 'a title', url: 'test' };
  68. clipboard.write({
  69. text: 'test',
  70. html: '<b>Hi</b>',
  71. rtf: '{\\rtf1\\utf8 text}',
  72. bookmark: 'a title',
  73. image: p
  74. });
  75. expect(clipboard.readText()).to.equal(text);
  76. expect(clipboard.readHTML()).to.equal(markup);
  77. expect(clipboard.readRTF()).to.equal(rtf);
  78. const readImage = clipboard.readImage();
  79. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  80. if (process.platform !== 'linux') {
  81. if (process.platform !== 'win32') {
  82. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  83. } else {
  84. expect(clipboard.readBookmark().url).to.equal(bookmark.url);
  85. }
  86. }
  87. });
  88. });
  89. describe('clipboard.read/writeFindText(text)', () => {
  90. before(function () {
  91. if (process.platform !== 'darwin') {
  92. this.skip();
  93. }
  94. });
  95. it('reads and write text to the find pasteboard', () => {
  96. clipboard.writeFindText('find this');
  97. expect(clipboard.readFindText()).to.equal('find this');
  98. });
  99. });
  100. describe('clipboard.readBuffer(format)', () => {
  101. it('writes a Buffer for the specified format', function () {
  102. const buffer = Buffer.from('writeBuffer', 'utf8');
  103. clipboard.writeBuffer('public/utf8-plain-text', buffer);
  104. expect(buffer.equals(clipboard.readBuffer('public/utf8-plain-text'))).to.equal(true);
  105. });
  106. it('throws an error when a non-Buffer is specified', () => {
  107. expect(() => {
  108. clipboard.writeBuffer('public/utf8-plain-text', 'hello');
  109. }).to.throw(/buffer must be a node Buffer/);
  110. });
  111. });
  112. });