api-clipboard-spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. expect(clipboard.readImage().toDataURL()).to.equal(i.toDataURL());
  19. });
  20. });
  21. describe('clipboard.readText()', () => {
  22. it('returns unicode string correctly', () => {
  23. const text = '千江有水千江月,万里无云万里天';
  24. clipboard.writeText(text);
  25. expect(clipboard.readText()).to.equal(text);
  26. });
  27. });
  28. describe('clipboard.readHTML()', () => {
  29. it('returns markup correctly', () => {
  30. const text = '<string>Hi</string>';
  31. 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>';
  32. clipboard.writeHTML(text);
  33. expect(clipboard.readHTML()).to.equal(markup);
  34. });
  35. });
  36. describe('clipboard.readRTF', () => {
  37. it('returns rtf text correctly', () => {
  38. const rtf = '{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\nThis is some {\\b bold} text.\\par\n}';
  39. clipboard.writeRTF(rtf);
  40. expect(clipboard.readRTF()).to.equal(rtf);
  41. });
  42. });
  43. describe('clipboard.readBookmark', () => {
  44. before(function () {
  45. if (process.platform === 'linux') {
  46. this.skip();
  47. }
  48. });
  49. it('returns title and url', () => {
  50. clipboard.writeBookmark('a title', 'https://electronjs.org');
  51. expect(clipboard.readBookmark()).to.deep.equal({
  52. title: 'a title',
  53. url: 'https://electronjs.org'
  54. });
  55. clipboard.writeText('no bookmark');
  56. expect(clipboard.readBookmark()).to.deep.equal({
  57. title: '',
  58. url: ''
  59. });
  60. });
  61. });
  62. describe('clipboard.write()', () => {
  63. it('returns data correctly', () => {
  64. const text = 'test';
  65. const rtf = '{\\rtf1\\utf8 text}';
  66. const p = path.join(fixtures, 'assets', 'logo.png');
  67. const i = nativeImage.createFromPath(p);
  68. 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>';
  69. const bookmark = { title: 'a title', url: 'test' };
  70. clipboard.write({
  71. text: 'test',
  72. html: '<b>Hi</b>',
  73. rtf: '{\\rtf1\\utf8 text}',
  74. bookmark: 'a title',
  75. image: p
  76. });
  77. expect(clipboard.readText()).to.equal(text);
  78. expect(clipboard.readHTML()).to.equal(markup);
  79. expect(clipboard.readRTF()).to.equal(rtf);
  80. expect(clipboard.readImage().toDataURL()).to.equal(i.toDataURL());
  81. if (process.platform !== 'linux') {
  82. expect(clipboard.readBookmark()).to.deep.equal(bookmark);
  83. }
  84. });
  85. });
  86. describe('clipboard.read/writeFindText(text)', () => {
  87. before(function () {
  88. if (process.platform !== 'darwin') {
  89. this.skip();
  90. }
  91. });
  92. it('reads and write text to the find pasteboard', () => {
  93. clipboard.writeFindText('find this');
  94. expect(clipboard.readFindText()).to.equal('find this');
  95. });
  96. });
  97. describe('clipboard.readBuffer(format)', () => {
  98. it('writes a Buffer for the specified format', function () {
  99. const buffer = Buffer.from('writeBuffer', 'utf8');
  100. clipboard.writeBuffer('public.utf8-plain-text', buffer);
  101. expect(buffer.equals(clipboard.readBuffer('public.utf8-plain-text'))).to.equal(true);
  102. });
  103. it('throws an error when a non-Buffer is specified', () => {
  104. expect(() => {
  105. clipboard.writeBuffer('public.utf8-plain-text', 'hello');
  106. }).to.throw(/buffer must be a node Buffer/);
  107. });
  108. });
  109. });