api-crash-reporter-spec.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const assert = require('assert')
  2. const http = require('http')
  3. const multiparty = require('multiparty')
  4. const path = require('path')
  5. const url = require('url')
  6. const {closeWindow} = require('./window-helpers')
  7. const remote = require('electron').remote
  8. const app = remote.require('electron').app
  9. const crashReporter = remote.require('electron').crashReporter
  10. const BrowserWindow = remote.require('electron').BrowserWindow
  11. describe('crashReporter module', function () {
  12. var fixtures = path.resolve(__dirname, 'fixtures')
  13. var w = null
  14. beforeEach(function () {
  15. w = new BrowserWindow({
  16. show: false
  17. })
  18. })
  19. afterEach(function () {
  20. return closeWindow(w).then(function () { w = null })
  21. })
  22. if (process.mas) {
  23. return
  24. }
  25. var isCI = remote.getGlobal('isCi')
  26. if (isCI) {
  27. return
  28. }
  29. it('should send minidump when renderer crashes', function (done) {
  30. this.timeout(120000)
  31. var called = false
  32. var server = http.createServer(function (req, res) {
  33. server.close()
  34. var form = new multiparty.Form()
  35. form.parse(req, function (error, fields) {
  36. if (error) throw error
  37. if (called) return
  38. called = true
  39. assert.equal(fields['prod'], 'Electron')
  40. assert.equal(fields['ver'], process.versions.electron)
  41. assert.equal(fields['process_type'], 'renderer')
  42. assert.equal(fields['platform'], process.platform)
  43. assert.equal(fields['extra1'], 'extra1')
  44. assert.equal(fields['extra2'], 'extra2')
  45. assert.equal(fields['_productName'], 'Zombies')
  46. assert.equal(fields['_companyName'], 'Umbrella Corporation')
  47. assert.equal(fields['_version'], app.getVersion())
  48. res.end('abc-123-def', () => {
  49. assert.equal(crashReporter.getLastCrashReport().id, 'abc-123-def')
  50. assert.notEqual(crashReporter.getUploadedReports().length, 0)
  51. assert.equal(crashReporter.getUploadedReports()[0].id, 'abc-123-def')
  52. done()
  53. })
  54. })
  55. })
  56. var port = remote.process.port
  57. server.listen(port, '127.0.0.1', function () {
  58. port = server.address().port
  59. remote.process.port = port
  60. const crashUrl = url.format({
  61. protocol: 'file',
  62. pathname: path.join(fixtures, 'api', 'crash.html'),
  63. search: '?port=' + port
  64. })
  65. if (process.platform === 'darwin') {
  66. crashReporter.start({
  67. companyName: 'Umbrella Corporation',
  68. submitURL: 'http://127.0.0.1:' + port
  69. })
  70. }
  71. w.loadURL(crashUrl)
  72. })
  73. })
  74. describe('.start(options)', function () {
  75. it('requires that the companyName and submitURL options be specified', function () {
  76. assert.throws(function () {
  77. crashReporter.start({
  78. companyName: 'Missing submitURL'
  79. })
  80. }, /submitURL is a required option to crashReporter\.start/)
  81. assert.throws(function () {
  82. crashReporter.start({
  83. submitURL: 'Missing companyName'
  84. })
  85. }, /companyName is a required option to crashReporter\.start/)
  86. })
  87. it('can be called multiple times', function () {
  88. assert.doesNotThrow(function () {
  89. crashReporter.start({
  90. companyName: 'Umbrella Corporation',
  91. submitURL: 'http://127.0.0.1/crashes'
  92. })
  93. crashReporter.start({
  94. companyName: 'Umbrella Corporation 2',
  95. submitURL: 'http://127.0.0.1/more-crashes'
  96. })
  97. })
  98. })
  99. })
  100. })