api-content-tracing-spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. const { remote } = require('electron')
  2. const chai = require('chai')
  3. const dirtyChai = require('dirty-chai')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const { expect } = chai
  7. const { app, contentTracing } = remote
  8. chai.use(dirtyChai)
  9. const timeout = async (milliseconds) => {
  10. return new Promise((resolve) => {
  11. setTimeout(resolve, milliseconds)
  12. })
  13. }
  14. const getPathInATempFolder = (filename) => {
  15. return path.join(app.getPath('temp'), filename)
  16. }
  17. describe('contentTracing', () => {
  18. beforeEach(function () {
  19. // FIXME: The tests are skipped on arm/arm64.
  20. if (process.platform === 'linux' &&
  21. ['arm', 'arm64'].includes(process.arch)) {
  22. this.skip()
  23. }
  24. })
  25. const startRecording = async (options) => {
  26. return new Promise((resolve) => {
  27. contentTracing.startRecording(options, () => {
  28. resolve()
  29. })
  30. })
  31. }
  32. const stopRecording = async (filePath) => {
  33. return new Promise((resolve) => {
  34. contentTracing.stopRecording(filePath, (resultFilePath) => {
  35. resolve(resultFilePath)
  36. })
  37. })
  38. }
  39. const record = async (options, outputFilePath, recordTimeInMilliseconds = 1e3) => {
  40. await app.whenReady()
  41. await startRecording(options)
  42. await timeout(recordTimeInMilliseconds)
  43. const resultFilePath = await stopRecording(outputFilePath)
  44. return resultFilePath
  45. }
  46. const outputFilePath = getPathInATempFolder('trace.json')
  47. beforeEach(() => {
  48. if (fs.existsSync(outputFilePath)) {
  49. fs.unlinkSync(outputFilePath)
  50. }
  51. })
  52. describe('startRecording', function () {
  53. this.timeout(5e3)
  54. const getFileSizeInKiloBytes = (filePath) => {
  55. const stats = fs.statSync(filePath)
  56. const fileSizeInBytes = stats.size
  57. const fileSizeInKiloBytes = fileSizeInBytes / 1024
  58. return fileSizeInKiloBytes
  59. }
  60. it('accepts an empty config', async () => {
  61. const config = {}
  62. await record(config, outputFilePath)
  63. expect(fs.existsSync(outputFilePath)).to.be.true()
  64. const fileSizeInKiloBytes = getFileSizeInKiloBytes(outputFilePath)
  65. expect(fileSizeInKiloBytes).to.be.above(0,
  66. `the trace output file is empty, check "${outputFilePath}"`)
  67. })
  68. it('accepts a trace config', async () => {
  69. // (alexeykuzmin): All categories are excluded on purpose,
  70. // so only metadata gets into the output file.
  71. const config = {
  72. excluded_categories: ['*']
  73. }
  74. await record(config, outputFilePath)
  75. expect(fs.existsSync(outputFilePath)).to.be.true()
  76. // If the `excluded_categories` param above is not respected
  77. // the file size will be above 50KB.
  78. const fileSizeInKiloBytes = getFileSizeInKiloBytes(outputFilePath)
  79. const expectedMaximumFileSize = 10 // Depends on a platform.
  80. expect(fileSizeInKiloBytes).to.be.above(0,
  81. `the trace output file is empty, check "${outputFilePath}"`)
  82. expect(fileSizeInKiloBytes).to.be.below(expectedMaximumFileSize,
  83. `the trace output file is suspiciously large (${fileSizeInKiloBytes}KB),
  84. check "${outputFilePath}"`)
  85. })
  86. it('accepts "categoryFilter" and "traceOptions" as a config', async () => {
  87. // (alexeykuzmin): All categories are excluded on purpose,
  88. // so only metadata gets into the output file.
  89. const config = {
  90. categoryFilter: '__ThisIsANonexistentCategory__',
  91. traceOptions: ''
  92. }
  93. await record(config, outputFilePath)
  94. expect(fs.existsSync(outputFilePath)).to.be.true()
  95. // If the `categoryFilter` param above is not respected
  96. // the file size will be above 50KB.
  97. const fileSizeInKiloBytes = getFileSizeInKiloBytes(outputFilePath)
  98. const expectedMaximumFileSize = 10 // Depends on a platform.
  99. expect(fileSizeInKiloBytes).to.be.above(0,
  100. `the trace output file is empty, check "${outputFilePath}"`)
  101. expect(fileSizeInKiloBytes).to.be.below(expectedMaximumFileSize,
  102. `the trace output file is suspiciously large (${fileSizeInKiloBytes}KB),
  103. check "${outputFilePath}"`)
  104. })
  105. })
  106. describe('stopRecording', function () {
  107. this.timeout(5e3)
  108. it('calls its callback with a result file path', async () => {
  109. const resultFilePath = await record(/* options */ {}, outputFilePath)
  110. expect(resultFilePath).to.be.a('string').and.be.equal(outputFilePath)
  111. })
  112. // FIXME(alexeykuzmin): https://github.com/electron/electron/issues/16019
  113. xit('creates a temporary file when an empty string is passed', async function () {
  114. const resultFilePath = await record(/* options */ {}, /* outputFilePath */ '')
  115. expect(resultFilePath).to.be.a('string').that.is.not.empty()
  116. })
  117. })
  118. })