api-clipboard-spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const { expect } = require('chai');
  2. const path = require('path');
  3. const { Buffer } = require('buffer');
  4. const { clipboard, nativeImage } = require('electron');
  5. describe('clipboard module', () => {
  6. const fixtures = path.resolve(__dirname, 'fixtures');
  7. // FIXME(zcbenz): Clipboard tests are failing on WOA.
  8. beforeEach(function () {
  9. if (process.platform === 'win32' && process.arch === 'arm64') {
  10. this.skip();
  11. }
  12. });
  13. describe('clipboard.readImage()', () => {
  14. it('returns NativeImage instance', () => {
  15. const p = path.join(fixtures, 'assets', 'logo.png');
  16. const i = nativeImage.createFromPath(p);
  17. clipboard.writeImage(p);
  18. const readImage = clipboard.readImage();
  19. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  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. 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>';
  33. clipboard.writeHTML(text);
  34. expect(clipboard.readHTML()).to.equal(markup);
  35. });
  36. });
  37. describe('clipboard.readRTF', () => {
  38. it('returns rtf text correctly', () => {
  39. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
  40. clipboard.writeRTF(rtf);
  41. expect(clipboard.readRTF()).to.equal(rtf);
  42. });
  43. });
  44. describe('clipboard.readBookmark', () => {
  45. before(function () {
  46. if (process.platform === 'linux') {
  47. this.skip();
  48. }
  49. });
  50. it('returns title and url', () => {
  51. clipboard.writeBookmark('a title', 'https://electronjs.org');
  52. expect(clipboard.readBookmark()).to.deep.equal({
  53. title: 'a title',
  54. url: 'https://electronjs.org'
  55. });
  56. clipboard.writeText('no bookmark');
  57. expect(clipboard.readBookmark()).to.deep.equal({
  58. title: '',
  59. url: ''
  60. });
  61. });
  62. });
  63. describe('clipboard.write()', () => {
  64. it('returns data correctly', () => {
  65. const text = 'test';
  66. const rtf = '{\\rtf1\\utf8 text}';
  67. const p = path.join(fixtures, 'assets', 'logo.png');
  68. const i = nativeImage.createFromPath(p);
  69. 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>';
  70. const bookmark = { title: 'a title', url: 'test' };
  71. clipboard.write({
  72. text: 'test',
  73. html: '<b>Hi</b>',
  74. rtf: '{\\rtf1\\utf8 text}',
  75. bookmark: 'a title',
  76. image: p
  77. });
  78. expect(clipboard.readText()).to.equal(text);
  79. expect(clipboard.readHTML()).to.equal(markup);
  80. expect(clipboard.readRTF()).to.equal(rtf);
  81. const readImage = clipboard.readImage();
  82. expect(readImage.toDataURL()).to.equal(i.toDataURL());
  83. if (process.platform !== 'linux') {
  84. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  85. }
  86. });
  87. });
  88. describe('clipboard.read/writeFindText(text)', () => {
  89. before(function () {
  90. if (process.platform !== 'darwin') {
  91. this.skip();
  92. }
  93. });
  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. });
  111. });