summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/test/unit/test_tree-map-02.js
blob: 4f7200f0ac19e2d72a014038a2593e1b1856be85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { drawText } = require("devtools/client/memory/components/tree-map/draw");

function run_test() {
  run_next_test();
}

add_task(function* () {
  // Mock out the Canvas2dContext
  let ctx = {
    fillText: (...args) => fillTextValues.push(args),
    measureText: (text) => {
      let width = text ? text.length * 10 : 0;
      return { width };
    }
  };
  let node = {
    x: 20,
    y: 30,
    dx: 500,
    dy: 70,
    name: "Example Node",
    totalBytes: 1200,
    totalCount: 100
  };
  let ratio = 0;
  let borderWidth = () => 1;
  let dragZoom = {
    offsetX: 0,
    offsetY: 0,
    zoom: 0
  };
  let fillTextValues = [];
  let padding = [10, 10];

  drawText(ctx, node, borderWidth, ratio, dragZoom, padding);
  deepEqual(fillTextValues[0], ["Example Node", 11.5, 21.5],
    "Fills in the full node name");
  deepEqual(fillTextValues[1], ["1KiB 100 count", 141.5, 21.5],
    "Includes the full byte and count information");

  fillTextValues = [];
  node.dx = 250;
  drawText(ctx, node, borderWidth, ratio, dragZoom, padding);

  deepEqual(fillTextValues[0], ["Example Node", 11.5, 21.5],
    "Fills in the full node name");
  deepEqual(fillTextValues[1], undefined,
    "Drops off the byte and count information if not enough room");

  fillTextValues = [];
  node.dx = 100;
  drawText(ctx, node, borderWidth, ratio, dragZoom, padding);

  deepEqual(fillTextValues[0], ["Exampl...", 11.5, 21.5],
    "Cuts the name with ellipsis");
  deepEqual(fillTextValues[1], undefined,
    "Drops off the byte and count information if not enough room");

  fillTextValues = [];
  node.dx = 40;
  drawText(ctx, node, borderWidth, ratio, dragZoom, padding);

  deepEqual(fillTextValues[0], ["...", 11.5, 21.5],
    "Shows only ellipsis when smaller");
  deepEqual(fillTextValues[1], undefined,
    "Drops off the byte and count information if not enough room");

  fillTextValues = [];
  node.dx = 20;
  drawText(ctx, node, borderWidth, ratio, dragZoom, padding);

  deepEqual(fillTextValues[0], undefined,
    "Draw nothing when not enough room");
  deepEqual(fillTextValues[1], undefined,
    "Drops off the byte and count information if not enough room");
});