api-clipboard-spec.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const assert = require('assert')
  2. const path = require('path')
  3. const clipboard = require('electron').clipboard
  4. const nativeImage = require('electron').nativeImage
  5. describe('clipboard module', function () {
  6. var fixtures = path.resolve(__dirname, 'fixtures')
  7. describe('clipboard.readImage()', function () {
  8. it('returns NativeImage intance', function () {
  9. var p = path.join(fixtures, 'assets', 'logo.png')
  10. var i = nativeImage.createFromPath(p)
  11. clipboard.writeImage(p)
  12. assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
  13. })
  14. })
  15. describe('clipboard.readText()', function () {
  16. it('returns unicode string correctly', function () {
  17. var text = '千江有水千江月,万里无云万里天'
  18. clipboard.writeText(text)
  19. assert.equal(clipboard.readText(), text)
  20. })
  21. })
  22. describe('clipboard.readHTML()', function () {
  23. it('returns markup correctly', function () {
  24. var text = '<string>Hi</string>'
  25. var 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', function () {
  31. it('returns rtf text correctly', function () {
  32. var 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', function () {
  38. it('returns title and url', function () {
  39. if (process.platform === 'linux') return
  40. clipboard.writeBookmark('a title', 'http://electron.atom.io')
  41. assert.deepEqual(clipboard.readBookmark(), {
  42. title: 'a title',
  43. url: 'http://electron.atom.io'
  44. })
  45. clipboard.writeText('no bookmark')
  46. assert.deepEqual(clipboard.readBookmark(), {
  47. title: '',
  48. url: ''
  49. })
  50. })
  51. })
  52. describe('clipboard.write()', function () {
  53. it('returns data correctly', function () {
  54. var text = 'test'
  55. var rtf = '{\\rtf1\\utf8 text}'
  56. var p = path.join(fixtures, 'assets', 'logo.png')
  57. var i = nativeImage.createFromPath(p)
  58. var 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>'
  59. var bookmark = {title: 'a title', url: 'test'}
  60. clipboard.write({
  61. text: 'test',
  62. html: '<b>Hi</b>',
  63. rtf: '{\\rtf1\\utf8 text}',
  64. bookmark: 'a title',
  65. image: p
  66. })
  67. assert.equal(clipboard.readText(), text)
  68. assert.equal(clipboard.readHTML(), markup)
  69. assert.equal(clipboard.readRTF(), rtf)
  70. assert.equal(clipboard.readImage().toDataURL(), i.toDataURL())
  71. if (process.platform !== 'linux') {
  72. assert.deepEqual(clipboard.readBookmark(), bookmark)
  73. }
  74. })
  75. })
  76. describe('clipboard.read/writeFindText(text)', function () {
  77. it('reads and write text to the find pasteboard', function () {
  78. if (process.platform !== 'darwin') return
  79. clipboard.writeFindText('find this')
  80. assert.equal(clipboard.readFindText(), 'find this')
  81. })
  82. })
  83. })