Commit f99fa198 authored by Jeremie Boulic's avatar Jeremie Boulic Committed by Commit Bot

Update QuickView file size with extra precision digit

Add optional argument to the QuickView file_metadata_formatter.js to
add extra precision to the file size shown in the metadata box.

Change the underlying bytesToString() formatting function to add the
desired number of precision digits to the file size text if needed.

Test: browser_tests --gtest_filter="*FileManagerJsTest.UtilTest"
Bug: 977068
Change-Id: Iaa6a08fbcbd7663ce2ec8d9347fc659894343926
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1760840Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Commit-Queue: Noel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703562}
parent 683c09fa
......@@ -219,9 +219,10 @@ util.removeFileOrDirectory = (entry, onSuccess, onError) => {
* number separators.
*
* @param {number} bytes The number of bytes.
* @param {number=} addedPrecision The number of precision digits to add.
* @return {string} Localized string.
*/
util.bytesToString = bytes => {
util.bytesToString = (bytes, addedPrecision = 0) => {
// Translation identifiers for size units.
const UNITS = [
'SIZE_BYTES',
......@@ -242,12 +243,18 @@ util.bytesToString = bytes => {
Math.pow(2, 50),
];
// Rounding with precision.
const round = (value, decimals) => {
const scale = Math.pow(10, decimals);
return Math.round(value * scale) / scale;
};
const str = (n, u) => {
return strf(u, n.toLocaleString());
};
const fmt = (s, u) => {
const rounded = Math.round(bytes / s * 10) / 10;
const rounded = round(bytes / s, 1 + addedPrecision);
return str(rounded, u);
};
......@@ -256,9 +263,11 @@ util.bytesToString = bytes => {
return str(bytes, UNITS[0]);
}
// Up to 1MB is displayed as rounded up number of KBs.
// Up to 1MB is displayed as rounded up number of KBs, or with the desired
// number of precision digits.
if (bytes < STEPS[2]) {
const rounded = Math.ceil(bytes / STEPS[1]);
const rounded = addedPrecision ? round(bytes / STEPS[1], addedPrecision) :
Math.ceil(bytes / STEPS[1]);
return str(rounded, UNITS[1]);
}
......
......@@ -193,7 +193,7 @@ function testEntryDebugString() {
}
/**
* Tests the formatting of util.bytesToString
* Tests the formatting of util.bytesToString.
*/
function testBytesToString() {
const KB = 2 ** 10;
......@@ -208,7 +208,7 @@ function testBytesToString() {
assertEquals(util.bytesToString(KB - 1), '1,023 bytes');
assertEquals(util.bytesToString(KB), '1 KB');
// Up to 1MB is displayed as a number of KBs.
// Up to 1MB is displayed as a number of KBs with no precision digit.
assertEquals(util.bytesToString(2 * KB), '2 KB');
assertEquals(util.bytesToString(2 * KB + 1), '3 KB');
assertEquals(util.bytesToString(MB - KB), '1,023 KB');
......@@ -216,7 +216,7 @@ function testBytesToString() {
assertEquals(util.bytesToString(MB - 1), '1,024 KB');
assertEquals(util.bytesToString(MB), '1 MB');
// Up to 1GB is displayed as a number of MBs.
// Up to 1GB is displayed as a number of MBs with <= 1 precision digit.
assertEquals(util.bytesToString(2.55 * MB - 1), '2.5 MB');
assertEquals(util.bytesToString(2.55 * MB), '2.6 MB');
assertEquals(util.bytesToString(GB - 0.05 * MB - 1), '1,023.9 MB');
......@@ -224,7 +224,7 @@ function testBytesToString() {
assertEquals(util.bytesToString(GB - 1), '1,024 MB');
assertEquals(util.bytesToString(GB), '1 GB');
// Up to 1TB is displayed as a number of GBs.
// Up to 1TB is displayed as a number of GBs with <= 1 precision digit.
assertEquals(util.bytesToString(2.55 * GB - 1), '2.5 GB');
assertEquals(util.bytesToString(2.55 * GB), '2.6 GB');
assertEquals(util.bytesToString(TB - 0.05 * GB - 1), '1,023.9 GB');
......@@ -232,11 +232,84 @@ function testBytesToString() {
assertEquals(util.bytesToString(TB - 1), '1,024 GB');
assertEquals(util.bytesToString(TB), '1 TB');
// Up to 1PB is displayed as a number of GBs.
// Up to 1PB is displayed as a number of TBs with <= 1 precision digit.
assertEquals(util.bytesToString(2.55 * TB - 1), '2.5 TB');
assertEquals(util.bytesToString(2.55 * TB), '2.6 TB');
assertEquals(util.bytesToString(PB - 0.05 * TB - 1), '1,023.9 TB');
assertEquals(util.bytesToString(PB - 0.05 * TB), '1,024 TB');
assertEquals(util.bytesToString(PB - 1), '1,024 TB');
assertEquals(util.bytesToString(PB), '1 PB');
// Above 1PB is displayed as a number of PBs with <= 1 precision digit.
assertEquals(util.bytesToString(2.55 * PB - 1), '2.5 PB');
assertEquals(util.bytesToString(2.55 * PB), '2.6 PB');
}
/**
* Tests the formatting of util.bytesToString with added precision digits.
*
* Useful information regarding the below assertions:
* 647 bytes = 0.6318359375 KB.
* bytesToString() internally uses Number.toLocaleString() which outputs at most
* 3 precision digits in en-US.
*/
function testBytesToStringWithAddedPrecision() {
const KB = 2 ** 10;
const MB = 2 ** 20;
const GB = 2 ** 30;
const TB = 2 ** 40;
const PB = 2 ** 50;
// Up to 1KB formatting unchanged.
assertEquals(util.bytesToString(0, 1), '0 bytes');
assertEquals(util.bytesToString(10, 2), '10 bytes');
assertEquals(util.bytesToString(KB - 1, 3), '1,023 bytes');
// Exact values, no trailing 0s regardless of precision.
assertEquals(util.bytesToString(2 * KB, 1), '2 KB');
assertEquals(util.bytesToString(2 * MB, 2), '2 MB');
assertEquals(util.bytesToString(2 * GB, 3), '2 GB');
assertEquals(util.bytesToString(2 * TB, 4), '2 TB');
assertEquals(util.bytesToString(2 * PB, 5), '2 PB');
// up to MB, initially no precision digits.
assertEquals(util.bytesToString(2 * KB + 647), '3 KB');
assertEquals(util.bytesToString(2 * KB + 647, 0), '3 KB');
assertEquals(util.bytesToString(2 * KB + 647, 1), '2.6 KB');
assertEquals(util.bytesToString(2 * KB + 647, 2), '2.63 KB');
assertEquals(util.bytesToString(2 * KB + 647, 3), '2.632 KB');
assertEquals(util.bytesToString(2 * KB + 647, 4), '2.632 KB');
assertEquals(util.bytesToString(200 * KB + 647, 1), '200.6 KB');
// From 1MB, up to 1 precision digit + added precision.
assertEquals(util.bytesToString(2 * MB + 647 * KB), '2.6 MB');
assertEquals(util.bytesToString(2 * MB + 647 * KB, 0), '2.6 MB');
assertEquals(util.bytesToString(2 * MB + 647 * KB, 1), '2.63 MB');
assertEquals(util.bytesToString(2 * MB + 647 * KB, 2), '2.632 MB');
assertEquals(util.bytesToString(2 * MB + 647 * KB, 3), '2.632 MB');
assertEquals(util.bytesToString(200 * MB + 647 * KB, 1), '200.63 MB');
// Up to 1TB.
assertEquals(util.bytesToString(2 * GB + 647 * MB), '2.6 GB');
assertEquals(util.bytesToString(2 * GB + 647 * MB, 0), '2.6 GB');
assertEquals(util.bytesToString(2 * GB + 647 * MB, 1), '2.63 GB');
assertEquals(util.bytesToString(2 * GB + 647 * MB, 2), '2.632 GB');
assertEquals(util.bytesToString(2 * GB + 647 * MB, 3), '2.632 GB');
assertEquals(util.bytesToString(200 * GB + 647 * MB, 1), '200.63 GB');
// Up to 1PB.
assertEquals(util.bytesToString(2 * TB + 647 * GB), '2.6 TB');
assertEquals(util.bytesToString(2 * TB + 647 * GB, 0), '2.6 TB');
assertEquals(util.bytesToString(2 * TB + 647 * GB, 1), '2.63 TB');
assertEquals(util.bytesToString(2 * TB + 647 * GB, 2), '2.632 TB');
assertEquals(util.bytesToString(2 * TB + 647 * GB, 3), '2.632 TB');
assertEquals(util.bytesToString(200 * TB + 647 * GB, 1), '200.63 TB');
// Above 1PB.
assertEquals(util.bytesToString(2 * PB + 647 * TB), '2.6 PB');
assertEquals(util.bytesToString(2 * PB + 647 * TB, 0), '2.6 PB');
assertEquals(util.bytesToString(2 * PB + 647 * TB, 1), '2.63 PB');
assertEquals(util.bytesToString(2 * PB + 647 * TB, 2), '2.632 PB');
assertEquals(util.bytesToString(2 * PB + 647 * TB, 3), '2.632 PB');
assertEquals(util.bytesToString(200 * PB + 647 * TB, 1), '200.63 PB');
}
......@@ -141,7 +141,7 @@ class MetadataBoxController {
this.setDirectorySize_(directory, isSameEntry);
} else if (item.size) {
this.metadataBox_.size =
this.fileMetadataFormatter_.formatSize(item.size, item.hosted);
this.fileMetadataFormatter_.formatSize(item.size, item.hosted, true);
this.metadataBox_.metadataRendered('size');
}
......@@ -274,7 +274,7 @@ class MetadataBoxController {
}
this.metadataBox_.size =
this.fileMetadataFormatter_.formatSize(size, true);
this.fileMetadataFormatter_.formatSize(size, true, true);
this.metadataBox_.isSizeLoading = false;
this.metadataBox_.metadataRendered('size');
});
......
......@@ -73,9 +73,11 @@ class FileMetadataFormatter extends cr.EventTarget {
* Generates a formatted filesize text.
* @param {number=} size
* @param {boolean=} hosted
* @param {boolean=} addPrecision addPrecision used to optionally add more
* precision digits to the formatted filesize text.
* @return {string} A string that represents a file size.
*/
formatSize(size, hosted) {
formatSize(size, hosted, addPrecision = false) {
if (size === null || size === undefined) {
return '...';
} else if (size === -1) {
......@@ -83,7 +85,7 @@ class FileMetadataFormatter extends cr.EventTarget {
} else if (size === 0 && hosted) {
return '--';
} else {
return util.bytesToString(size);
return util.bytesToString(size, addPrecision ? 1 : 0);
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment