test_tree-map-02.js (2389B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { 7 drawText, 8 } = require("resource://devtools/client/memory/components/tree-map/draw.js"); 9 10 add_task(async function () { 11 // Mock out the Canvas2dContext 12 const ctx = { 13 fillText: (...args) => fillTextValues.push(args), 14 measureText: text => { 15 const width = text ? text.length * 10 : 0; 16 return { width }; 17 }, 18 }; 19 const node = { 20 x: 20, 21 y: 30, 22 dx: 500, 23 dy: 70, 24 name: "Example Node", 25 totalBytes: 1200, 26 totalCount: 100, 27 }; 28 const ratio = 0; 29 const borderWidth = () => 1; 30 const dragZoom = { 31 offsetX: 0, 32 offsetY: 0, 33 zoom: 0, 34 }; 35 let fillTextValues = []; 36 const padding = [10, 10]; 37 38 drawText(ctx, node, borderWidth, ratio, dragZoom, padding); 39 deepEqual( 40 fillTextValues[0], 41 ["Example Node", 11.5, 21.5], 42 "Fills in the full node name" 43 ); 44 deepEqual( 45 fillTextValues[1], 46 ["1KiB 100 count", 141.5, 21.5], 47 "Includes the full byte and count information" 48 ); 49 50 fillTextValues = []; 51 node.dx = 250; 52 drawText(ctx, node, borderWidth, ratio, dragZoom, padding); 53 54 deepEqual( 55 fillTextValues[0], 56 ["Example Node", 11.5, 21.5], 57 "Fills in the full node name" 58 ); 59 deepEqual( 60 fillTextValues[1], 61 undefined, 62 "Drops off the byte and count information if not enough room" 63 ); 64 65 fillTextValues = []; 66 node.dx = 100; 67 drawText(ctx, node, borderWidth, ratio, dragZoom, padding); 68 69 deepEqual( 70 fillTextValues[0], 71 ["Exampl...", 11.5, 21.5], 72 "Cuts the name with ellipsis" 73 ); 74 deepEqual( 75 fillTextValues[1], 76 undefined, 77 "Drops off the byte and count information if not enough room" 78 ); 79 80 fillTextValues = []; 81 node.dx = 40; 82 drawText(ctx, node, borderWidth, ratio, dragZoom, padding); 83 84 deepEqual( 85 fillTextValues[0], 86 ["...", 11.5, 21.5], 87 "Shows only ellipsis when smaller" 88 ); 89 deepEqual( 90 fillTextValues[1], 91 undefined, 92 "Drops off the byte and count information if not enough room" 93 ); 94 95 fillTextValues = []; 96 node.dx = 20; 97 drawText(ctx, node, borderWidth, ratio, dragZoom, padding); 98 99 deepEqual(fillTextValues[0], undefined, "Draw nothing when not enough room"); 100 deepEqual( 101 fillTextValues[1], 102 undefined, 103 "Drops off the byte and count information if not enough room" 104 ); 105 });