diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mailnews/base/test/unit/test_formatFileSize.js | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comm/mailnews/base/test/unit/test_formatFileSize.js')
-rw-r--r-- | comm/mailnews/base/test/unit/test_formatFileSize.js | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/comm/mailnews/base/test/unit/test_formatFileSize.js b/comm/mailnews/base/test/unit/test_formatFileSize.js new file mode 100644 index 0000000000..1269fd33c3 --- /dev/null +++ b/comm/mailnews/base/test/unit/test_formatFileSize.js @@ -0,0 +1,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); + }); +}); |