summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/test/unit/test_formatFileSize.js
blob: 1269fd33c39a8c783535196c2c1ed2e8b7d536db (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Tests for the formatFileSize method.
 */

var gStringBundle = Services.strings.createBundle(
  "chrome://messenger/locale/messenger.properties"
);

var gMessenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);

function isDigit(c) {
  return "0123456789".includes(c);
}

function test_formatFileSize(aArgs) {
  const strings = {
    b: "byteAbbreviation2",
    kb: "kiloByteAbbreviation2",
    mb: "megaByteAbbreviation2",
    gb: "gigaByteAbbreviation2",
    tb: "teraByteAbbreviation2",
    pb: "petaByteAbbreviation2",
  };

  let actual = gMessenger.formatFileSize(aArgs.bytes, aArgs.useKB);
  let expected = gStringBundle
    .GetStringFromName(strings[aArgs.units])
    .replace("%.*f", aArgs.mantissa);

  // If the actual string contains a non-numeric character at the position
  // where we'd expect a decimal separator, assume it is a localized separator
  // and just convert it to a dot for easy comparing.
  let separatorPos = aArgs.mantissa.indexOf(".");
  if (!isDigit(actual.charAt(separatorPos))) {
    actual =
      actual.substring(0, separatorPos) + "." + actual.substr(separatorPos + 1);
  }

  Assert.equal(actual, expected);
}

var test_data = [
  { bytes: 0, useKB: false, mantissa: "0", units: "b" },
  { bytes: 1, useKB: false, mantissa: "1", units: "b" },
  { bytes: 10, useKB: false, mantissa: "10", units: "b" },
  { bytes: 999, useKB: false, mantissa: "999", units: "b" },
  { bytes: 1000, useKB: false, mantissa: "1.0", units: "kb" },
  { bytes: 1024, useKB: false, mantissa: "1.0", units: "kb" },
  { bytes: 10 * 1024, useKB: false, mantissa: "10.0", units: "kb" },
  { bytes: 999 * 1024, useKB: false, mantissa: "999", units: "kb" },
  { bytes: 1000 * 1024, useKB: false, mantissa: "1.0", units: "mb" },
  { bytes: 1024 * 1024, useKB: false, mantissa: "1.0", units: "mb" },
  { bytes: 10 * 1024 * 1024, useKB: false, mantissa: "10.0", units: "mb" },
  { bytes: 999 * 1024 * 1024, useKB: false, mantissa: "999", units: "mb" },
  { bytes: 1000 * 1024 * 1024, useKB: false, mantissa: "1.0", units: "gb" },
  { bytes: 1024 * 1024 * 1024, useKB: false, mantissa: "1.0", units: "gb" },
  {
    bytes: 10 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "10.0",
    units: "gb",
  },
  {
    bytes: 999 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "999",
    units: "gb",
  },
  {
    bytes: 1000 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1.0",
    units: "tb",
  },
  {
    bytes: 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1.0",
    units: "tb",
  },
  {
    bytes: 10 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "10.0",
    units: "tb",
  },
  {
    bytes: 999 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "999",
    units: "tb",
  },
  {
    bytes: 1000 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1.0",
    units: "pb",
  },
  {
    bytes: 1000 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1.0",
    units: "pb",
  },
  {
    bytes: 1024 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1.0",
    units: "pb",
  },
  {
    bytes: 10 * 1024 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "10.0",
    units: "pb",
  },
  {
    bytes: 999 * 1024 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "999",
    units: "pb",
  },
  {
    bytes: 1000 * 1024 * 1024 * 1024 * 1024 * 1024,
    useKB: false,
    mantissa: "1000",
    units: "pb",
  },

  { bytes: 0, useKB: true, mantissa: "0", units: "kb" },
  { bytes: 1, useKB: true, mantissa: "0.1", units: "kb" },
  { bytes: 500, useKB: true, mantissa: "0.5", units: "kb" },
  { bytes: 999, useKB: true, mantissa: "1.0", units: "kb" },
];

add_task(function test_format_file_size() {
  test_data.map(entry => {
    test_formatFileSize(entry);
  });
});