api-clipboard-spec.js 4.4 KB

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