Bug 1747402 - Added check for when time value is less than zero in getFormattedTimeStatus. r=mtigley
authorTyler Kabaker <kabakert@msu.edu>
Sun, 16 Jan 2022 21:09:07 +0000
changeset 604640 fd384d84f3d05d364a74fa6967e416767c8e8759
parent 604639 fe5a01aaed0bdd5816161624518cd25ef836ec92
child 604641 6193897887751446f58fdca18847e70675ade591
push id39155
push usersmolnar@mozilla.com
push dateMon, 17 Jan 2022 03:58:08 +0000
treeherdermozilla-central@fd384d84f3d0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmtigley
bugs1747402
milestone98.0a1
first release with
nightly linux64
fd384d84f3d0 / 98.0a1 / 20220117035808 / files
nightly mac
fd384d84f3d0 / 98.0a1 / 20220117035808 / files
nightly win32
fd384d84f3d0 / 98.0a1 / 20220117035808 / files
nightly win64
fd384d84f3d0 / 98.0a1 / 20220117035808 / files
nightly linux32
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1747402 - Added check for when time value is less than zero in getFormattedTimeStatus. r=mtigley Differential Revision: https://phabricator.services.mozilla.com/D136065
toolkit/mozapps/downloads/DownloadUtils.jsm
toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
--- a/toolkit/mozapps/downloads/DownloadUtils.jsm
+++ b/toolkit/mozapps/downloads/DownloadUtils.jsm
@@ -567,17 +567,17 @@ var DownloadUtils = {
    *        id: "downloading-file-opens-in-minutes-and-seconds",
    *        args: { minutes: 2, seconds: 30 },
    *      },
    *   };
    */
   getFormattedTimeStatus: function DU_getFormattedTimeStatus(aSeconds) {
     aSeconds = Math.floor(aSeconds);
     let l10n;
-    if (!isFinite(aSeconds)) {
+    if (!isFinite(aSeconds) || aSeconds < 0) {
       l10n = {
         id: "downloading-file-opens-in-some-time",
       };
     } else if (aSeconds < 60) {
       l10n = {
         id: "downloading-file-opens-in-seconds",
         args: { seconds: aSeconds },
       };
--- a/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
+++ b/toolkit/mozapps/downloads/tests/unit/test_DownloadUtils.js
@@ -71,16 +71,25 @@ function testStatus(aFunc, aCurr, aMore,
   // Make sure the lastSeconds matches
   if (last == Infinity) {
     Assert.equal(last, aTest[1]);
   } else {
     Assert.ok(Math.abs(last - aTest[1]) < 0.1);
   }
 }
 
+function testFormattedTimeStatus(aSec, aExpected) {
+  dump("Formatted Time Status Test: [" + aSec + "]\n");
+
+  let status = DownloadUtils.getFormattedTimeStatus(aSec);
+  dump("Formatted Time Status Test Returns: (" + status.l10n.id + ")\n");
+
+  Assert.equal(status.l10n.id, aExpected);
+}
+
 function testURI(aURI, aDisp, aHost) {
   dump("URI Test: " + [aURI, aDisp, aHost] + "\n");
 
   let [disp, host] = DownloadUtils.getURIHost(aURI);
 
   // Make sure we have the right display host and full host
   Assert.equal(disp, aDisp);
   Assert.equal(host, aHost);
@@ -358,16 +367,22 @@ function run_test() {
     "Unknown time left -- 1.4 of 2.9 KB",
     Infinity,
   ]);
   testStatus(statusFunc, 8, 5, 0, [
     "Unknown time left -- 9.2 of 9.3 GB",
     Infinity,
   ]);
 
+  testFormattedTimeStatus(-1, "downloading-file-opens-in-some-time");
+  // Passing in null will return a status of file-opens-in-seconds, as Math.floor(null) = 0
+  testFormattedTimeStatus(null, "downloading-file-opens-in-seconds");
+  testFormattedTimeStatus(0, "downloading-file-opens-in-seconds");
+  testFormattedTimeStatus(30, "downloading-file-opens-in-seconds");
+
   testURI("http://www.mozilla.org/", "mozilla.org", "www.mozilla.org");
   testURI(
     "http://www.city.mikasa.hokkaido.jp/",
     "city.mikasa.hokkaido.jp",
     "www.city.mikasa.hokkaido.jp"
   );
   testURI("data:text/html,Hello World", "data resource", "data resource");
   testURI(