summaryrefslogtreecommitdiffstats
path: root/devtools/client/memory/test/unit/test_utils.js
blob: f6deddfc9078120cc4c4afcfd7d327fe755f9c27 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

/**
 * Tests the task creator `takeSnapshotAndCensus()` for the whole flow of
 * taking a snapshot, and its sub-actions. Tests the formatNumber and
 * formatPercent methods.
 */

let utils = require("devtools/client/memory/utils");
let { snapshotState: states, viewState } = require("devtools/client/memory/constants");
let { Preferences } = require("resource://gre/modules/Preferences.jsm");

function run_test() {
  run_next_test();
}

add_task(function* () {
  let s1 = utils.createSnapshot({ view: { state: viewState.CENSUS } });
  let s2 = utils.createSnapshot({ view: { state: viewState.CENSUS } });
  equal(s1.state, states.SAVING, "utils.createSnapshot() creates snapshot in saving state");
  ok(s1.id !== s2.id, "utils.createSnapshot() creates snapshot with unique ids");

  let custom = { by: "internalType", then: { by: "count", bytes: true }};
  Preferences.set("devtools.memory.custom-census-displays", JSON.stringify({ "My Display": custom }));

  equal(utils.getCustomCensusDisplays()["My Display"].by, custom.by,
        "utils.getCustomCensusDisplays() returns custom displays");

  ok(true, "test formatNumber util functions");
  equal(utils.formatNumber(12), "12", "formatNumber returns 12 for 12");

  equal(utils.formatNumber(0), "0", "formatNumber returns 0 for 0");
  equal(utils.formatNumber(-0), "0", "formatNumber returns 0 for -0");
  equal(utils.formatNumber(+0), "0", "formatNumber returns 0 for +0");

  equal(utils.formatNumber(1234567), "1 234 567",
    "formatNumber adds a space every 3rd digit");
  equal(utils.formatNumber(12345678), "12 345 678",
    "formatNumber adds a space every 3rd digit");
  equal(utils.formatNumber(123456789), "123 456 789",
    "formatNumber adds a space every 3rd digit");

  equal(utils.formatNumber(12, true), "+12",
    "formatNumber can display number sign");
  equal(utils.formatNumber(-12, true), "-12",
    "formatNumber can display number sign (negative)");

  ok(true, "test formatPercent util functions");
  equal(utils.formatPercent(12), "12%", "formatPercent returns 12% for 12");
  equal(utils.formatPercent(12345), "12 345%",
    "formatPercent returns 12 345% for 12345");

  equal(utils.formatAbbreviatedBytes(12), "12B", "Formats bytes");
  equal(utils.formatAbbreviatedBytes(12345), "12KiB", "Formats kilobytes");
  equal(utils.formatAbbreviatedBytes(12345678), "11MiB", "Formats megabytes");
  equal(utils.formatAbbreviatedBytes(12345678912), "11GiB", "Formats gigabytes");

  equal(utils.hslToStyle(0.5, 0.6, 0.7),
    "hsl(180,60%,70%)", "hslToStyle converts an array to a style string");
  equal(utils.hslToStyle(0, 0, 0),
    "hsl(0,0%,0%)", "hslToStyle converts an array to a style string");
  equal(utils.hslToStyle(1, 1, 1),
    "hsl(360,100%,100%)", "hslToStyle converts an array to a style string");

  equal(utils.lerp(5, 7, 0), 5, "lerp return first number for 0");
  equal(utils.lerp(5, 7, 1), 7, "lerp return second number for 1");
  equal(utils.lerp(5, 7, 0.5), 6, "lerp interpolates the numbers for 0.5");
});