browser_flame-graph-04.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. "use strict";
  4. // Tests that text metrics in the flame graph widget work properly.
  5. const HTML_NS = "http://www.w3.org/1999/xhtml";
  6. const {ELLIPSIS} = require("devtools/shared/l10n");
  7. const {FlameGraph} = require("devtools/client/shared/widgets/FlameGraph");
  8. const {FLAME_GRAPH_BLOCK_TEXT_FONT_SIZE} = require("devtools/client/shared/widgets/FlameGraph");
  9. const {FLAME_GRAPH_BLOCK_TEXT_FONT_FAMILY} = require("devtools/client/shared/widgets/FlameGraph");
  10. add_task(function* () {
  11. yield addTab("about:blank");
  12. yield performTest();
  13. gBrowser.removeCurrentTab();
  14. });
  15. function* performTest() {
  16. let [host,, doc] = yield createHost();
  17. let graph = new FlameGraph(doc.body, 1);
  18. yield graph.ready();
  19. testGraph(graph);
  20. yield graph.destroy();
  21. host.destroy();
  22. }
  23. function testGraph(graph) {
  24. is(graph._averageCharWidth, getAverageCharWidth(),
  25. "The average char width was calculated correctly.");
  26. is(graph._overflowCharWidth, getCharWidth(ELLIPSIS),
  27. "The ellipsis char width was calculated correctly.");
  28. let text = "This text is maybe overflowing";
  29. let text1000px = graph._getFittedText(text, 1000);
  30. let text50px = graph._getFittedText(text, 50);
  31. let text10px = graph._getFittedText(text, 10);
  32. let text1px = graph._getFittedText(text, 1);
  33. is(graph._getTextWidthApprox(text), getAverageCharWidth() * text.length,
  34. "The approximate width was calculated correctly.");
  35. info("Text at 1000px width: " + text1000px);
  36. info("Text at 50px width : " + text50px);
  37. info("Text at 10px width : " + text10px);
  38. info("Text at 1px width : " + text1px);
  39. is(text1000px, text,
  40. "The fitted text for 1000px width is correct.");
  41. isnot(text50px, text,
  42. "The fitted text for 50px width is correct (1).");
  43. ok(text50px.includes(ELLIPSIS),
  44. "The fitted text for 50px width is correct (2).");
  45. is(graph._getFittedText(text, FLAME_GRAPH_BLOCK_TEXT_FONT_SIZE + 1), ELLIPSIS,
  46. "The fitted text for text font size width is correct.");
  47. is(graph._getFittedText(text, 1), "",
  48. "The fitted text for 1px width is correct.");
  49. }
  50. function getAverageCharWidth() {
  51. let letterWidthsSum = 0;
  52. let start = " ".charCodeAt(0);
  53. let end = "z".charCodeAt(0) + 1;
  54. for (let i = start; i < end; i++) {
  55. let char = String.fromCharCode(i);
  56. letterWidthsSum += getCharWidth(char);
  57. }
  58. return letterWidthsSum / (end - start);
  59. }
  60. function getCharWidth(char) {
  61. let canvas = document.createElementNS(HTML_NS, "canvas");
  62. let ctx = canvas.getContext("2d");
  63. let fontSize = FLAME_GRAPH_BLOCK_TEXT_FONT_SIZE;
  64. let fontFamily = FLAME_GRAPH_BLOCK_TEXT_FONT_FAMILY;
  65. ctx.font = fontSize + "px " + fontFamily;
  66. return ctx.measureText(char).width;
  67. }